View Javadoc

1   /**
2    * File: 	BoundaryPolyCirculinearCurve2D.java
3    * Project: javaGeom
4    * 
5    * Distributed under the LGPL License.
6    *
7    * Created: 11 mai 09
8    */
9   package math.geom2d.circulinear;
10  
11  import java.awt.Graphics2D;
12  import java.util.ArrayList;
13  import java.util.Collection;
14  
15  import math.geom2d.AffineTransform2D;
16  import math.geom2d.Box2D;
17  import math.geom2d.curve.Curve2D;
18  import math.geom2d.curve.Curve2DUtils;
19  import math.geom2d.curve.CurveSet2D;
20  import math.geom2d.domain.BoundaryPolyCurve2D;
21  import math.geom2d.domain.ContinuousOrientedCurve2D;
22  import math.geom2d.domain.PolyOrientedCurve2D;
23  import math.geom2d.transform.CircleInversion2D;
24  
25  
26  /**
27   * A continuous boundary which is composed of several continuous circulinear
28   * curves. Instances of this class can be circulinear rings (composed of
29   * several continuous and finite circulinear curves that form a loop), or
30   * an open curve with two infinite circulinear curve at each extremity.
31   * @author dlegland
32   *
33   */
34  public class BoundaryPolyCirculinearCurve2D<T extends CirculinearContinuousCurve2D>
35  extends PolyCirculinearCurve2D<T> 
36  implements CirculinearContinuousCurve2D, CirculinearContour2D {
37  
38      // ===================================================================
39      // constructors
40  
41      public BoundaryPolyCirculinearCurve2D() {
42          super();
43      }
44  
45      public BoundaryPolyCirculinearCurve2D(int size) {
46          super(size);
47      }
48  
49      public BoundaryPolyCirculinearCurve2D(T[] curves) {
50          super(curves);
51      }
52  
53      public BoundaryPolyCirculinearCurve2D(T[] curves, boolean closed) {
54          super(curves, closed);
55      }
56  
57      public BoundaryPolyCirculinearCurve2D(Collection<? extends T> curves) {
58          super(curves);
59      }
60  
61      public BoundaryPolyCirculinearCurve2D(Collection<? extends T> curves, boolean closed) {
62          super(curves, closed);
63      }
64  
65      
66      // ===================================================================
67      // static methods
68  
69      /**
70       * Static factory for creating a new BoundaryPolyCirculinearCurve2D from a
71       * collection of curves.
72       * @since 0.8.1
73       */
74  	public static <T extends CirculinearContinuousCurve2D> 
75  	BoundaryPolyCirculinearCurve2D<T>
76  	create(Collection<T> curves) {
77  		return new BoundaryPolyCirculinearCurve2D<T>(curves);
78  	}
79  
80      /**
81       * Static factory for creating a new BoundaryPolyCirculinearCurve2D from a
82       * collection of curves.
83       * @since 0.8.1
84       */
85  	public static <T extends CirculinearContinuousCurve2D> 
86  	BoundaryPolyCirculinearCurve2D<T>
87  	create(Collection<T> curves, boolean closed) {
88  		return new BoundaryPolyCirculinearCurve2D<T>(curves, closed);
89  	}
90  
91      /**
92       * Static factory for creating a new BoundaryPolyCirculinearCurve2D from an
93       * array of curves.
94       * @since 0.8.1
95       */
96      public static <T extends CirculinearContour2D> 
97      BoundaryPolyCirculinearCurve2D<T> create(T[] curves) {
98      	return new BoundaryPolyCirculinearCurve2D<T>(curves);
99      }
100 
101     /**
102      * Static factory for creating a new BoundaryPolyCirculinearCurve2D from an
103      * array of curves.
104      * @since 0.8.1
105      */
106     public static <T extends CirculinearContour2D> 
107     BoundaryPolyCirculinearCurve2D<T> create(T[] curves, boolean closed) {
108     	return new BoundaryPolyCirculinearCurve2D<T>(curves, closed);
109     }
110 
111     
112     // ===================================================================
113     // methods implementing the CirculinearCurve2D interface
114 
115 	/* (non-Javadoc)
116 	 * @see math.geom2d.circulinear.CirculinearCurve2D#getLength()
117 	 */
118 	@Override
119 	public double getLength() {
120 		double sum = 0;
121 		for(CirculinearCurve2D curve : this.getCurves())
122 			sum += curve.getLength();
123 		return sum;
124 	}
125 
126 	/* (non-Javadoc)
127 	 * @see math.geom2d.circulinear.CirculinearCurve2D#getLength(double)
128 	 */
129 	@Override
130 	public double getLength(double pos) {
131 		return CirculinearCurve2DUtils.getLength(this, pos);
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see math.geom2d.circulinear.CirculinearCurve2D#getPosition(double)
136 	 */
137 	@Override
138 	public double getPosition(double length) {
139 		return CirculinearCurve2DUtils.getPosition(this, length);
140 	}
141 
142 	/* (non-Javadoc)
143 	 * @see math.geom2d.circulinear.CirculinearContinuousCurve2D#getParallel(double)
144 	 */
145     @Override
146 	public CirculinearRing2D getParallel(double dist) {
147     	return GenericCirculinearRing2D.create(
148     			CirculinearCurve2DUtils.createContinuousParallel(this, dist)
149     			.getSmoothPieces());
150     }
151     
152 	
153 	/* (non-Javadoc)
154 	 * @see math.geom2d.circulinear.CirculinearCurve2D#transform(math.geom2d.transform.CircleInversion2D)
155 	 */
156 	@Override
157 	public BoundaryPolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D>
158 	transform(CircleInversion2D inv) {
159     	// Allocate array for result
160 		int n = curves.size();
161 		BoundaryPolyCirculinearCurve2D<CirculinearContinuousCurve2D> result = 
162 			new BoundaryPolyCirculinearCurve2D<CirculinearContinuousCurve2D>(n);
163         
164         // add each transformed curve
165         for (CirculinearContinuousCurve2D curve : curves)
166             result.addCurve((CirculinearContinuousCurve2D)curve.transform(inv));
167         return result;
168 	}
169 
170 	// ===================================================================
171     // methods implementing the Boundary2D interface
172 
173 	/* (non-Javadoc)
174 	 * @see math.geom2d.domain.Boundary2D#fill(java.awt.Graphics2D)
175 	 */
176 	public void fill(Graphics2D g2) {
177 		g2.fill(this.getGeneralPath());
178 	}
179 
180 	/* (non-Javadoc)
181 	 * @see math.geom2d.domain.Boundary2D#getBoundaryCurves()
182 	 */
183 	public Collection<BoundaryPolyCirculinearCurve2D<T>> getBoundaryCurves() {
184         ArrayList<BoundaryPolyCirculinearCurve2D<T>> list = 
185             new ArrayList<BoundaryPolyCirculinearCurve2D<T>>(1);
186         list.add(this);
187         return list;
188 	}
189 
190 	/* (non-Javadoc)
191 	 * @see math.geom2d.domain.Boundary2D#getDomain()
192 	 */
193 	public CirculinearDomain2D getDomain() {
194 		return new GenericCirculinearDomain2D(this);
195 	}
196 
197 	// ===================================================================
198     // methods implementing the ContinuousCurve2D interface
199 
200     /*
201      * (non-Javadoc)
202      * 
203      * @see math.geom2d.CirculinearContinuousCurve2D#getSmoothPieces()
204      */
205     @Override
206 	public Collection<? extends CirculinearElement2D> getSmoothPieces() {
207     	// create array for storing result
208     	ArrayList<CirculinearElement2D> result = 
209     		new ArrayList<CirculinearElement2D>();
210     	
211     	// add elements of each curve
212     	for(CirculinearContinuousCurve2D curve : curves)
213     		result.addAll(curve.getSmoothPieces());
214     	
215     	// return the collection
216         return result;
217     }
218 
219     // ===================================================================
220     // methods implementing the Curve2D interface
221 
222     @Override
223     public Collection<? extends BoundaryPolyCirculinearCurve2D<?>> 
224     getContinuousCurves() {
225     	return wrapCurve(this);
226     }
227 
228 	@Override
229 	public BoundaryPolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D> 
230 	getReverseCurve() {
231     	int n = curves.size();
232         // create array of reversed curves
233     	CirculinearContinuousCurve2D[] curves2 = 
234     		new CirculinearContinuousCurve2D[n];
235         
236         // reverse each curve
237         for (int i = 0; i<n; i++)
238             curves2[i] = (CirculinearContinuousCurve2D)curves.get(n-1-i).getReverseCurve();
239         
240         // create the reversed final curve
241         return new BoundaryPolyCirculinearCurve2D<CirculinearContinuousCurve2D>(curves2);
242 	}
243 	
244 	@Override
245 	public PolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D>
246 	getSubCurve(double t0, double t1) {
247 		// Call the superclass method
248 		PolyOrientedCurve2D<? extends ContinuousOrientedCurve2D> subcurve =
249 			super.getSubCurve(t0, t1);
250 		
251 		// prepare result
252 		int n = subcurve.getCurveNumber();
253 		PolyCirculinearCurve2D<CirculinearContinuousCurve2D> result = 
254 			new PolyCirculinearCurve2D<CirculinearContinuousCurve2D>(n);
255 		
256 		// add each curve after class cast
257 		for(Curve2D curve : subcurve) {
258 			if(curve instanceof CirculinearContinuousCurve2D)
259 				result.addCurve((CirculinearContinuousCurve2D) curve);
260 		}
261 		
262 		// return the result
263 		return result;
264 	}
265 
266     // ===================================================================
267     // methods implementing the Shape2D interface
268 
269     @Override
270 	public CirculinearCurveSet2D<? extends CirculinearContinuousCurve2D> 
271 	clip(Box2D box) {
272         // Clip the curve
273         CurveSet2D<? extends Curve2D> set = Curve2DUtils.clipCurve(this, box);
274 
275         // Stores the result in appropriate structure
276         int n = set.getCurveNumber();
277         CirculinearCurveSet2D<CirculinearContinuousCurve2D> result = 
278         	new CirculinearCurveSet2D<CirculinearContinuousCurve2D>(n);
279 
280         // convert the result, class cast each curve
281         for (Curve2D curve : set.getCurves()) {
282             if (curve instanceof CirculinearContinuousCurve2D)
283                 result.addCurve((CirculinearContinuousCurve2D) curve);
284         }
285         
286         // return the new set of curves
287         return result;
288 	}
289 	
290 	@Override
291 	public BoundaryPolyCurve2D<? extends ContinuousOrientedCurve2D> 
292 	transform(AffineTransform2D trans) {
293 		// number of curves
294 		int n = this.getCurveNumber();
295 		
296 		// create result curve
297 		BoundaryPolyCurve2D<ContinuousOrientedCurve2D> result =
298         	new BoundaryPolyCurve2D<ContinuousOrientedCurve2D>(n);
299         
300         // add each curve after class cast
301         for (ContinuousOrientedCurve2D curve : curves)
302             result.addCurve((ContinuousOrientedCurve2D)curve.transform(trans));
303         
304         result.setClosed(this.isClosed());
305         return result;
306 	}
307 
308 }