comparison dwtx/draw2d/graph/Vertex.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2004, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.draw2d.graph.Vertex;
14
15 import tango.text.convert.Format;
16 import dwt.dwthelper.utils;
17 import dwtx.dwtxhelper.Collection;
18
19 import dwtx.draw2d.PositionConstants;
20 import dwtx.draw2d.geometry.Point;
21 import dwtx.draw2d.geometry.Rectangle;
22 import dwtx.draw2d.graph.Obstacle;
23 import dwtx.draw2d.graph.Path;
24 import dwtx.draw2d.graph.Segment;
25
26 /**
27 * A vertex representation for the ShortestPathRouting. Vertices are either one of
28 * four corners on an <code>Obstacle</code>(Rectangle), or one of the two end points of a
29 * <code>Path</code>.
30 *
31 * This class is not intended to be subclassed.
32 * @author Whitney Sorenson
33 * @since 3.0
34 */
35 class Vertex
36 : Point
37 {
38
39 // constants for the vertex type
40 static const int NOT_SET = 0;
41 static const int INNIE = 1;
42 static const int OUTIE = 2;
43
44 // for shortest path
45 List neighbors;
46 bool isPermanent = false;
47 Vertex label;
48 double cost = 0;
49
50 // for routing
51 int nearestObstacle = 0;
52 double offset = 0;
53 int type = NOT_SET;
54 int count = 0;
55 int totalCount = 0;
56 Obstacle obs;
57 List paths;
58 bool nearestObstacleChecked = false;
59 Map cachedCosines;
60 int positionOnObstacle = -1;
61
62 private int origX, origY;
63
64 /**
65 * Creates a new Vertex with the given x, y position and on the given obstacle.
66 *
67 * @param x x point
68 * @param y y point
69 * @param obs obstacle - can be null
70 */
71 this(int x, int y, Obstacle obs) {
72 super(x, y);
73 origX = x;
74 origY = y;
75 this.obs = obs;
76 }
77
78 /**
79 * Creates a new Vertex with the given point position and on the given obstacle.
80 *
81 * @param p the point
82 * @param obs obstacle - can be null
83 */
84 this(Point p, Obstacle obs) {
85 this(p.x, p.y, obs);
86 }
87
88 /**
89 * Adds a path to this vertex, calculates angle between two segments and caches it.
90 *
91 * @param path the path
92 * @param start the segment to this vertex
93 * @param end the segment away from this vertex
94 */
95 void addPath(Path path, Segment start, Segment end) {
96 if (paths is null) {
97 paths = new ArrayList();
98 cachedCosines = new HashMap();
99 }
100 if (!paths.contains(path))
101 paths.add(path);
102 cachedCosines.put(path, new Double(start.cosine(end)));
103 }
104
105 /**
106 * Creates a point that represents this vertex offset by the given amount times
107 * the offset.
108 *
109 * @param modifier the offset
110 * @return a Point that has been bent around this vertex
111 */
112 Point bend(int modifier) {
113 Point point = new Point(x, y);
114 if ((positionOnObstacle & PositionConstants.NORTH) > 0)
115 point.y -= modifier * offset;
116 else
117 point.y += modifier * offset;
118 if ((positionOnObstacle & PositionConstants.EAST) > 0)
119 point.x += modifier * offset;
120 else
121 point.x -= modifier * offset;
122 return point;
123 }
124
125 /**
126 * Resets all fields on this Vertex.
127 */
128 void fullReset() {
129 totalCount = 0;
130 type = NOT_SET;
131 count = 0;
132 cost = 0;
133 offset = getSpacing();
134 nearestObstacle = 0;
135 label = null;
136 nearestObstacleChecked = false;
137 isPermanent = false;
138 if (neighbors !is null)
139 neighbors.clear();
140 if (cachedCosines !is null)
141 cachedCosines.clear();
142 if (paths !is null)
143 paths.clear();
144 }
145
146 /**
147 * Returns a Rectangle that represents the region around this vertex that
148 * paths will be traveling in.
149 *
150 * @param extraOffset a buffer to add to the region.
151 * @return the rectangle
152 */
153 Rectangle getDeformedRectangle(int extraOffset) {
154 Rectangle rect = new Rectangle(0, 0, 0, 0);
155
156 if ((positionOnObstacle & PositionConstants.NORTH) > 0) {
157 rect.y = y - extraOffset;
158 rect.height = origY - y + extraOffset;
159 } else {
160 rect.y = origY;
161 rect.height = y - origY + extraOffset;
162 }
163 if ((positionOnObstacle & PositionConstants.EAST) > 0) {
164 rect.x = origX;
165 rect.width = x - origX + extraOffset;
166 } else {
167 rect.x = x - extraOffset;
168 rect.width = origX - x + extraOffset;
169 }
170
171 return rect;
172 }
173
174 private int getSpacing() {
175 if (obs is null)
176 return 0;
177 return obs.getSpacing();
178 }
179
180 /**
181 * Grows this vertex by its offset to its maximum size.
182 */
183 void grow() {
184 int modifier;
185
186 if (nearestObstacle is 0)
187 modifier = totalCount * getSpacing();
188 else
189 modifier = (nearestObstacle / 2) - 1;
190
191 if ((positionOnObstacle & PositionConstants.NORTH) > 0)
192 y -= modifier;
193 else
194 y += modifier;
195 if ((positionOnObstacle & PositionConstants.EAST) > 0)
196 x += modifier;
197 else
198 x -= modifier;
199 }
200
201 /**
202 * Shrinks this vertex to its original size.
203 */
204 void shrink() {
205 x = origX;
206 y = origY;
207 }
208
209 /**
210 * Updates the offset of this vertex based on its shortest distance.
211 */
212 void updateOffset() {
213 if (nearestObstacle !is 0)
214 offset = ((nearestObstacle / 2) - 1) / totalCount;
215 }
216
217 /**
218 * @see dwtx.draw2d.geometry.Point#toString()
219 */
220 public String toString() {
221 return Format("V({}, {})", origX, origY ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
222 }
223
224 }