Project

General

Profile

Download (5.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2004, 2010 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
 *******************************************************************************/
11
package org.eclipse.draw2d.graph;
12

    
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

    
18
import org.eclipse.draw2d.PositionConstants;
19
import org.eclipse.draw2d.geometry.Point;
20
import org.eclipse.draw2d.geometry.Rectangle;
21

    
22
/**
23
 * A vertex representation for the ShortestPathRouting. Vertices are either one
24
 * of four corners on an <code>Obstacle</code>(Rectangle), or one of the two end
25
 * points of a <code>Path</code>.
26
 * 
27
 * This class is not intended to be subclassed.
28
 * 
29
 * @author Whitney Sorenson
30
 * @since 3.0
31
 */
32
class Vertex extends Point {
33

    
34
	// constants for the vertex type
35
	static final int NOT_SET = 0;
36
	static final int INNIE = 1;
37
	static final int OUTIE = 2;
38

    
39
	// for shortest path
40
	List neighbors;
41
	boolean isPermanent = false;
42
	Vertex label;
43
	double cost = 0;
44

    
45
	// for routing
46
	int nearestObstacle = 0;
47
	double offset = 0;
48
	int type = NOT_SET;
49
	int count = 0;
50
	int totalCount = 0;
51
	Obstacle obs;
52
	List paths;
53
	boolean nearestObstacleChecked = false;
54
	Map cachedCosines;
55
	int positionOnObstacle = -1;
56

    
57
	private int origX, origY;
58

    
59
	/**
60
	 * Creates a new Vertex with the given x, y position and on the given
61
	 * obstacle.
62
	 * 
63
	 * @param x
64
	 *            x point
65
	 * @param y
66
	 *            y point
67
	 * @param obs
68
	 *            obstacle - can be null
69
	 */
70
	Vertex(int x, int y, Obstacle obs) {
71
		super(x, y);
72
		origX = x;
73
		origY = y;
74
		this.obs = obs;
75
	}
76

    
77
	/**
78
	 * Creates a new Vertex with the given point position and on the given
79
	 * obstacle.
80
	 * 
81
	 * @param p
82
	 *            the point
83
	 * @param obs
84
	 *            obstacle - can be null
85
	 */
86
	Vertex(Point p, Obstacle obs) {
87
		this(p.x, p.y, obs);
88
	}
89

    
90
	/**
91
	 * Adds a path to this vertex, calculates angle between two segments and
92
	 * caches it.
93
	 * 
94
	 * @param path
95
	 *            the path
96
	 * @param start
97
	 *            the segment to this vertex
98
	 * @param end
99
	 *            the segment away from this vertex
100
	 */
101
	void addPath(Path path, Segment start, Segment end) {
102
		if (paths == null) {
103
			paths = new ArrayList();
104
			cachedCosines = new HashMap();
105
		}
106
		if (!paths.contains(path))
107
			paths.add(path);
108
		cachedCosines.put(path, new Double(start.cosine(end)));
109
	}
110

    
111
	/**
112
	 * Creates a point that represents this vertex offset by the given amount
113
	 * times the offset.
114
	 * 
115
	 * @param modifier
116
	 *            the offset
117
	 * @return a Point that has been bent around this vertex
118
	 */
119
	Point bend(int modifier) {
120
		Point point = new Point(x, y);
121
		if ((positionOnObstacle & PositionConstants.NORTH) > 0)
122
			point.y -= modifier * offset;
123
		else
124
			point.y += modifier * offset;
125
		if ((positionOnObstacle & PositionConstants.EAST) > 0)
126
			point.x += modifier * offset;
127
		else
128
			point.x -= modifier * offset;
129
		return point;
130
	}
131

    
132
	/**
133
	 * Resets all fields on this Vertex.
134
	 */
135
	void fullReset() {
136
		totalCount = 0;
137
		type = NOT_SET;
138
		count = 0;
139
		cost = 0;
140
		offset = getSpacing();
141
		nearestObstacle = 0;
142
		label = null;
143
		nearestObstacleChecked = false;
144
		isPermanent = false;
145
		if (neighbors != null)
146
			neighbors.clear();
147
		if (cachedCosines != null)
148
			cachedCosines.clear();
149
		if (paths != null)
150
			paths.clear();
151
	}
152

    
153
	/**
154
	 * Returns a Rectangle that represents the region around this vertex that
155
	 * paths will be traveling in.
156
	 * 
157
	 * @param extraOffset
158
	 *            a buffer to add to the region.
159
	 * @return the rectangle
160
	 */
161
	Rectangle getDeformedRectangle(int extraOffset) {
162
		Rectangle rect = new Rectangle(0, 0, 0, 0);
163

    
164
		if ((positionOnObstacle & PositionConstants.NORTH) > 0) {
165
			rect.y = y - extraOffset;
166
			rect.height = origY - y + extraOffset;
167
		} else {
168
			rect.y = origY;
169
			rect.height = y - origY + extraOffset;
170
		}
171
		if ((positionOnObstacle & PositionConstants.EAST) > 0) {
172
			rect.x = origX;
173
			rect.width = x - origX + extraOffset;
174
		} else {
175
			rect.x = x - extraOffset;
176
			rect.width = origX - x + extraOffset;
177
		}
178

    
179
		return rect;
180
	}
181

    
182
	private int getSpacing() {
183
		if (obs == null)
184
			return 0;
185
		return obs.getSpacing();
186
	}
187

    
188
	/**
189
	 * Grows this vertex by its offset to its maximum size.
190
	 */
191
	void grow() {
192
		int modifier;
193

    
194
		if (nearestObstacle == 0)
195
			modifier = totalCount * getSpacing();
196
		else
197
			modifier = (nearestObstacle / 2) - 1;
198

    
199
		if ((positionOnObstacle & PositionConstants.NORTH) > 0)
200
			y -= modifier;
201
		else
202
			y += modifier;
203
		if ((positionOnObstacle & PositionConstants.EAST) > 0)
204
			x += modifier;
205
		else
206
			x -= modifier;
207
	}
208

    
209
	/**
210
	 * Shrinks this vertex to its original size.
211
	 */
212
	void shrink() {
213
		x = origX;
214
		y = origY;
215
	}
216

    
217
	/**
218
	 * Updates the offset of this vertex based on its shortest distance.
219
	 */
220
	void updateOffset() {
221
		if (nearestObstacle != 0)
222
			offset = ((nearestObstacle / 2) - 1) / totalCount;
223
	}
224

    
225
	/**
226
	 * @see org.eclipse.draw2d.geometry.Point#toString()
227
	 */
228
	public String toString() {
229
		return "V(" + origX + ", " + origY + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
230
	}
231

    
232
}
(45-45/49)