Initial opendaylight infrastructure commit!!
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / layout / RadialTreeLayout.java
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  * Created on Jul 9, 2005
9  */
10
11 package edu.uci.ics.jung.algorithms.layout;
12 import java.awt.Dimension;
13 import java.awt.geom.Point2D;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import edu.uci.ics.jung.graph.Forest;
18
19 /**
20  * A radial layout for Tree or Forest graphs.
21  * 
22  * @author Tom Nelson 
23  *  
24  */
25 public class RadialTreeLayout<V,E> extends TreeLayout<V,E> {
26
27     protected Map<V,PolarPoint> polarLocations;
28
29     /**
30      * Creates an instance for the specified graph with default X and Y distances.
31      */
32     public RadialTreeLayout(Forest<V,E> g) {
33         this(g, DEFAULT_DISTX, DEFAULT_DISTY);
34     }
35
36     /**
37      * Creates an instance for the specified graph and X distance with
38      * default Y distance.
39      */
40     public RadialTreeLayout(Forest<V,E> g, int distx) {
41         this(g, distx, DEFAULT_DISTY);
42     }
43
44     /**
45      * Creates an instance for the specified graph, X distance, and Y distance.
46      */
47     public RadialTreeLayout(Forest<V,E> g, int distx, int disty) {
48         super(g, distx, disty);
49     }
50     
51         @Override
52     protected void buildTree() {
53             super.buildTree();
54             this.polarLocations = new HashMap<V, PolarPoint>();
55         setRadialLocations();
56     }
57
58     @Override
59     public void setSize(Dimension size) {
60         this.size = size;
61         buildTree();
62     }
63
64     @Override
65     protected void setCurrentPositionFor(V vertex) {
66         locations.get(vertex).setLocation(m_currentPoint);
67     }
68
69         @Override
70     public void setLocation(V v, Point2D location)
71     {
72         Point2D c = getCenter();
73         Point2D pv = new Point2D.Double(location.getX() - c.getX(), 
74                 location.getY() - c.getY());
75         PolarPoint newLocation = PolarPoint.cartesianToPolar(pv);
76         PolarPoint currentLocation = polarLocations.get(v);
77         if (currentLocation == null)
78                 polarLocations.put(v, newLocation);
79         else
80                 currentLocation.setLocation(newLocation);
81      }
82         
83         /**
84          * Returns the map from vertices to their locations in polar coordinates.
85          */
86         public Map<V,PolarPoint> getPolarLocations() {
87                 return polarLocations;
88         }
89
90         @Override
91     public Point2D transform(V v) {
92                 PolarPoint pp = polarLocations.get(v);
93                 double centerX = getSize().getWidth()/2;
94                 double centerY = getSize().getHeight()/2;
95                 Point2D cartesian = PolarPoint.polarToCartesian(pp);
96                 cartesian.setLocation(cartesian.getX()+centerX,cartesian.getY()+centerY);
97                 return cartesian;
98         }
99         
100         private Point2D getMaxXY() {
101                 double maxx = 0;
102                 double maxy = 0;
103                 for(Point2D p : locations.values()) {
104                         maxx = Math.max(maxx, p.getX());
105                         maxy = Math.max(maxy, p.getY());
106                 }
107                 return new Point2D.Double(maxx,maxy);
108         }
109         
110         private void setRadialLocations() {
111                 Point2D max = getMaxXY();
112                 double maxx = max.getX();
113                 double maxy = max.getY();
114                 maxx = Math.max(maxx, size.width);
115                 double theta = 2*Math.PI/maxx;
116
117                 double deltaRadius = size.width/2/maxy;
118                 for(Map.Entry<V, Point2D> entry : locations.entrySet()) {
119                         V v = entry.getKey();
120                         Point2D p = entry.getValue();
121                         PolarPoint polarPoint = new PolarPoint(p.getX()*theta, (p.getY() - this.distY)*deltaRadius);
122                         polarLocations.put(v, polarPoint);
123                 }
124         }
125 }