View Javadoc

1   /**
2    * 
3    */
4   
5   package math.geom2d.line;
6   
7   import java.util.Iterator;
8   
9   import math.geom2d.Angle2D;
10  import math.geom2d.Point2D;
11  import math.geom2d.conic.Circle2D;
12  import math.geom2d.conic.CircleArc2D;
13  import math.geom2d.domain.BoundaryPolyCurve2D;
14  import math.geom2d.domain.PolyOrientedCurve2D;
15  import math.geom2d.domain.SmoothOrientedCurve2D;
16  
17  /**
18   * Some utility functions for manipulating Polyline2D.
19   * 
20   * @author dlegland
21   * @deprecated use math.geom2d.polygon.Polyline2DUtils instead (0.7.0)
22   * @since 0.6.3
23   */
24  @Deprecated
25  public abstract class Polyline2DUtils {
26  
27      /**
28       * Creates a curve parallel to the given polyline, at a distance d. The
29       * resulting curve is continuous, but can self-intersect. It is composed of
30       * line segments, and circle arcs.
31       * 
32       * @param polyline the source curve
33       * @param d the signed distance between the original curve and its parallel
34       * @return the curve parallel to the original curve at a distance d
35       */
36      public final static PolyOrientedCurve2D<SmoothOrientedCurve2D> createParallel(
37              math.geom2d.polygon.Polyline2D polyline, double d) {
38  
39          // Collection of parallel curves
40          PolyOrientedCurve2D<SmoothOrientedCurve2D> result = new PolyOrientedCurve2D<SmoothOrientedCurve2D>();
41          result.setClosed(polyline instanceof ClosedPolyline2D);
42  
43          // evacuate degenerate case.
44          if (polyline.getVertices().size()<2)
45              return result;
46  
47          // ----- declarations -----
48  
49          // vertices of the current edge
50          Point2D v1, v2;
51  
52          // The corresponding parallel points, and the intersection point
53          // for first curve
54          Point2D p1, p2, p0 = null;
55  
56          // The line parallel to the previous and current line segments
57          StraightLine2D line0, line;
58  
59          // Circle located at corners
60          Circle2D circle;
61  
62          Iterator<Point2D> iterator;
63  
64          // ----- Initializations -----
65  
66          if (polyline instanceof ClosedPolyline2D) {
67              // Add eventually a circle arc, and the first line segment.
68  
69              // Extract parallel to last edge
70              LineSegment2D lastEdge = polyline.getLastEdge();
71              line0 = StraightLine2D.createParallel(lastEdge, d);
72  
73              v2 = lastEdge.getLastPoint();
74              p0 = line0.getProjectedPoint(v2);
75  
76              // extract current vertices, and current parallel
77              iterator = polyline.getVertices().iterator();
78              v1 = iterator.next();
79              v2 = iterator.next();
80              line = new StraightLine2D(v1, v2).getParallel(d);
81  
82              // Check angle of the 2 lines
83              p1 = line.getProjectedPoint(v1);
84              if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
85                  // Line is going to the right -> next line segment will be
86                  // truncated
87                  p1 = line.getIntersection(line0);
88                  p0 = p1;
89              } else {
90                  // line is going to the left -> add a circle arc
91                  circle = new Circle2D(v1, Math.abs(d));
92                  result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
93                          .getPosition(p0), circle.getPosition(p1), d>0));
94              }
95  
96              p2 = line.getProjectedPoint(v2);
97              line0 = line;
98          } else {
99              // extract current vertices
100             iterator = polyline.getVertices().iterator();
101             v1 = iterator.next();
102             v2 = iterator.next();
103 
104             // current parallel
105             line0 = new StraightLine2D(v1, v2).getParallel(d);
106             p1 = line0.getProjectedPoint(v1);
107             p2 = line0.getProjectedPoint(v2);
108         }
109 
110         // ----- Main loop -----
111 
112         // Main iteration on vertices
113         while (iterator.hasNext()) {
114             // Compute line parallel to current line segment
115             v1 = v2;
116             v2 = iterator.next();
117             line = new StraightLine2D(v1, v2).getParallel(d);
118 
119             // Check angle of the 2 lines
120             if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
121                 // Line is going to the right -> add the previous line segment
122                 // truncated at corner
123                 p2 = line.getIntersection(line0);
124                 result.addCurve(new LineSegment2D(p1, p2));
125                 p1 = p2;
126             } else {
127                 // line is going to the left -> add the complete line segment
128                 // and a circle arc
129                 result.addCurve(new LineSegment2D(p1, p2));
130                 p1 = line.getProjectedPoint(v1);
131                 circle = new Circle2D(v1, Math.abs(d));
132                 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
133                         .getPosition(p2), circle.getPosition(p1), d>0));
134             }
135 
136             // Prepare for next iteration
137             p2 = line.getProjectedPoint(v2);
138             line0 = line;
139         }
140 
141         // ----- Post processing -----
142 
143         if (polyline instanceof ClosedPolyline2D) {
144             // current line segment join the last point to the first point
145             iterator = polyline.getVertices().iterator();
146             v1 = v2;
147             v2 = iterator.next();
148             line = new StraightLine2D(v1, v2).getParallel(d);
149 
150             // Check angle of the 2 lines
151             if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
152                 // Line is going to the right -> add the previous line segment
153                 // truncated at corner
154                 p2 = line.getIntersection(line0);
155                 result.addCurve(new LineSegment2D(p1, p2));
156                 p1 = p2;
157             } else {
158                 // line is going to the left -> add the complete line segment
159                 // and a circle arc
160                 result.addCurve(new LineSegment2D(p1, p2));
161                 p1 = line.getProjectedPoint(v1);
162                 circle = new Circle2D(v1, Math.abs(d));
163                 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
164                         .getPosition(p2), circle.getPosition(p1), d>0));
165             }
166 
167             // Add the last line segment
168             result.addCurve(new LineSegment2D(p1, p0));
169         } else {
170             // Add the last line segment
171             result.addCurve(new LineSegment2D(p1, p2));
172         }
173 
174         // Return the resulting curve
175         return result;
176     }
177 
178     /**
179      * Creates a curve parallel to the given polyline, at a distance d. The
180      * resulting curve is continuous, but can self-intersect. It is composed of
181      * line segments, and circle arcs.
182      * 
183      * @param polyline the source curve
184      * @param d the signed distance between the original curve and its parallel
185      * @return the curve parallel to the original curve at a distance d
186      */
187     public final static BoundaryPolyCurve2D<SmoothOrientedCurve2D> createClosedParallel(
188             ClosedPolyline2D polyline, double d) {
189 
190         // Collection of parallel curves
191         BoundaryPolyCurve2D<SmoothOrientedCurve2D> result = new BoundaryPolyCurve2D<SmoothOrientedCurve2D>();
192         result.setClosed(true);
193 
194         // evacuate degenerate case.
195         if (polyline.getVertices().size()<2)
196             return result;
197 
198         // ----- declarations -----
199 
200         // vertices of the current edge
201         Point2D v1, v2;
202 
203         // The corresponding parallel points, and the intersection point
204         // for first curve
205         Point2D p1, p2, p0 = null;
206 
207         // The line parallel to the previous and current line segments
208         StraightLine2D line0, line;
209 
210         // Circle located at corners
211         Circle2D circle;
212 
213         Iterator<Point2D> iterator;
214 
215         // ----- Initializations -----
216 
217         // Add eventually a circle arc, and the first line segment.
218 
219         // Extract parallel to last edge
220         LineSegment2D lastEdge = polyline.getLastEdge();
221         line0 = StraightLine2D.createParallel(lastEdge, d);
222 
223         v2 = lastEdge.getLastPoint();
224         p0 = line0.getProjectedPoint(v2);
225 
226         // extract current vertices, and current parallel
227         iterator = polyline.getVertices().iterator();
228         v1 = iterator.next();
229         v2 = iterator.next();
230         line = new StraightLine2D(v1, v2).getParallel(d);
231 
232         // Check angle of the 2 lines
233         p1 = line.getProjectedPoint(v1);
234         if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
235             // Line is going to the right -> next line segment will be
236             // truncated
237             p1 = line.getIntersection(line0);
238             p0 = p1;
239         } else {
240             // line is going to the left -> add a circle arc
241             circle = new Circle2D(v1, Math.abs(d));
242             result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
243                     .getPosition(p0), circle.getPosition(p1), d>0));
244         }
245 
246         p2 = line.getProjectedPoint(v2);
247         line0 = line;
248 
249         // ----- Main loop -----
250 
251         // Main iteration on vertices
252         while (iterator.hasNext()) {
253             // Compute line parallel to current line segment
254             v1 = v2;
255             v2 = iterator.next();
256             line = new StraightLine2D(v1, v2).getParallel(d);
257 
258             // Check angle of the 2 lines
259             if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
260                 // Line is going to the right -> add the previous line segment
261                 // truncated at corner
262                 p2 = line.getIntersection(line0);
263                 result.addCurve(new LineSegment2D(p1, p2));
264                 p1 = p2;
265             } else {
266                 // line is going to the left -> add the complete line segment
267                 // and a circle arc
268                 result.addCurve(new LineSegment2D(p1, p2));
269                 p1 = line.getProjectedPoint(v1);
270                 circle = new Circle2D(v1, Math.abs(d));
271                 result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
272                         .getPosition(p2), circle.getPosition(p1), d>0));
273             }
274 
275             // Prepare for next iteration
276             p2 = line.getProjectedPoint(v2);
277             line0 = line;
278         }
279 
280         // ----- Post processing -----
281 
282         // current line segment join the last point to the first point
283         iterator = polyline.getVertices().iterator();
284         v1 = v2;
285         v2 = iterator.next();
286         line = new StraightLine2D(v1, v2).getParallel(d);
287 
288         // Check angle of the 2 lines
289         if (Angle2D.getAngle(line0, line)>Math.PI^d<0) {
290             // Line is going to the right -> add the previous line segment
291             // truncated at corner
292             p2 = line.getIntersection(line0);
293             result.addCurve(new LineSegment2D(p1, p2));
294             p1 = p2;
295         } else {
296             // line is going to the left -> add the complete line segment
297             // and a circle arc
298             result.addCurve(new LineSegment2D(p1, p2));
299             p1 = line.getProjectedPoint(v1);
300             circle = new Circle2D(v1, Math.abs(d));
301             result.addCurve(new CircleArc2D(v1, Math.abs(d), circle
302                     .getPosition(p2), circle.getPosition(p1), d>0));
303         }
304 
305         // Add the last line segment
306         result.addCurve(new LineSegment2D(p1, p0));
307 
308         // Return the resulting curve
309         return result;
310     }
311 }