View Javadoc

1   /**
2    * File: 	PolyCirculinearCurve2D.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.util.ArrayList;
12  import java.util.Collection;
13  
14  import math.geom2d.Box2D;
15  import math.geom2d.curve.*;
16  import math.geom2d.domain.ContinuousOrientedCurve2D;
17  import math.geom2d.domain.PolyOrientedCurve2D;
18  import math.geom2d.transform.CircleInversion2D;
19  
20  
21  /**
22   * A continuous curve which is composed of several continuous circulinear
23   * curves.
24   * @author dlegland
25   *
26   */
27  public class PolyCirculinearCurve2D<T extends CirculinearContinuousCurve2D>
28  extends PolyOrientedCurve2D<T> implements CirculinearContinuousCurve2D {
29  
30      // ===================================================================
31      // constructors
32  
33      public PolyCirculinearCurve2D() {
34          super();
35      }
36  
37      public PolyCirculinearCurve2D(int size) {
38          super(size);
39      }
40  
41      public PolyCirculinearCurve2D(T[] curves) {
42          super(curves);
43      }
44  
45      public PolyCirculinearCurve2D(T[] curves, boolean closed) {
46          super(curves, closed);
47      }
48  
49      public PolyCirculinearCurve2D(Collection<? extends T> curves) {
50          super(curves);
51      }
52  
53      public PolyCirculinearCurve2D(Collection<? extends T> curves, boolean closed) {
54          super(curves, closed);
55      }
56  
57      // ===================================================================
58      // static methods
59  
60  //    /**
61  //     * Static factory for creating a new PolyCirculinearCurve2D from a
62  //     * collection of curves.
63  //     * @since 0.8.1
64  //     */
65  //    public static <T extends CirculinearContinuousCurve2D> 
66  //    PolyCirculinearCurve2D<T> create(Collection<T> curves) {
67  //    	return new PolyCirculinearCurve2D<T>(curves);
68  //    }
69  //    
70  //    /**
71  //     * Static factory for creating a new PolyCirculinearCurve2D from an array
72  //     * of curves.
73  //     * @since 0.8.1
74  //     */
75  //    public static <T extends CirculinearContinuousCurve2D> 
76  //    PolyCirculinearCurve2D<T> create(T[] curves) {
77  //    	return new PolyCirculinearCurve2D<T>(curves);
78  //    }
79  
80      
81      // ===================================================================
82      // methods implementing the CirculinearCurve2D interface
83  
84  	/* (non-Javadoc)
85  	 * @see math.geom2d.circulinear.CirculinearCurve2D#getLength()
86  	 */
87  	public double getLength() {
88  		double sum = 0;
89  		for(CirculinearCurve2D curve : this.getCurves())
90  			sum += curve.getLength();
91  		return sum;
92  	}
93  
94  	/* (non-Javadoc)
95  	 * @see math.geom2d.circulinear.CirculinearCurve2D#getLength(double)
96  	 */
97  	public double getLength(double pos) {
98  		return CirculinearCurve2DUtils.getLength(this, pos);
99  	}
100 
101 	/* (non-Javadoc)
102 	 * @see math.geom2d.circulinear.CirculinearCurve2D#getPosition(double)
103 	 */
104 	public double getPosition(double length) {
105 		return CirculinearCurve2DUtils.getPosition(this, length);
106 	}
107 
108 	/* (non-Javadoc)
109 	 * @see math.geom2d.circulinear.CirculinearShape2D#getBuffer(double)
110 	 */
111 	public CirculinearDomain2D getBuffer(double dist) {
112 		return CirculinearCurve2DUtils.computeBuffer(this, dist);
113 	}
114 
115 	/* (non-Javadoc)
116 	 * @see math.geom2d.circulinear.CirculinearContinuousCurve2D#getParallel(double)
117 	 */
118 	public CirculinearContinuousCurve2D getParallel(double d) {
119 		return CirculinearCurve2DUtils.createContinuousParallel(this, d);
120 	}
121 	
122 	/* (non-Javadoc)
123 	 * @see math.geom2d.circulinear.CirculinearCurve2D#transform(math.geom2d.transform.CircleInversion2D)
124 	 */
125 	public PolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D>
126 	transform(CircleInversion2D inv) {
127     	// Allocate array for result
128 		int n = curves.size();
129 		PolyCirculinearCurve2D<CirculinearContinuousCurve2D> result = 
130 			new PolyCirculinearCurve2D<CirculinearContinuousCurve2D>(n);
131         
132         // add each transformed curve
133         for (CirculinearContinuousCurve2D curve : curves)
134             result.addCurve((CirculinearContinuousCurve2D)curve.transform(inv));
135         return result;
136 	}
137 
138     // ===================================================================
139     // methods implementing the ContinuousCurve2D interface
140 
141     /*
142      * (non-Javadoc)
143      * 
144      * @see math.geom2d.CirculinearContinuousCurve2D#getSmoothPieces()
145      */
146     @Override
147 	public Collection<? extends CirculinearElement2D> getSmoothPieces() {
148     	// create array for storing result
149     	ArrayList<CirculinearElement2D> result = 
150     		new ArrayList<CirculinearElement2D>();
151     	
152     	// add elements of each curve
153     	for(CirculinearContinuousCurve2D curve : curves)
154     		result.addAll(curve.getSmoothPieces());
155     	
156     	// return the collection
157         return result;
158     }
159 
160     // ===================================================================
161     // methods implementing the Curve2D interface
162 
163     @Override
164     public Collection<? extends PolyCirculinearCurve2D<?>> 
165     getContinuousCurves() {
166     	return wrapCurve(this);
167     }
168 
169     @Override
170 	public CirculinearCurveSet2D<? extends CirculinearContinuousCurve2D> 
171 	clip(Box2D box) {
172         // Clip the curve
173         CurveSet2D<? extends Curve2D> set = Curve2DUtils.clipCurve(this, box);
174 
175         // Stores the result in appropriate structure
176         int n = set.getCurveNumber();
177         CirculinearCurveSet2D<CirculinearContinuousCurve2D> result = 
178         	new CirculinearCurveSet2D<CirculinearContinuousCurve2D>(n);
179 
180         // convert the result, class cast each curve
181         for (Curve2D curve : set.getCurves()) {
182             if (curve instanceof CirculinearContinuousCurve2D)
183                 result.addCurve((CirculinearContinuousCurve2D) curve);
184         }
185         
186         // return the new set of curves
187         return result;
188 	}
189 	
190 	@Override
191 	public PolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D> getReverseCurve() {
192     	int n = curves.size();
193         // create array of reversed curves
194     	CirculinearContinuousCurve2D[] curves2 = 
195     		new CirculinearContinuousCurve2D[n];
196         
197         // reverse each curve
198         for (int i = 0; i<n; i++)
199             curves2[i] = (CirculinearContinuousCurve2D)curves.get(n-1-i).getReverseCurve();
200         
201         // create the reversed final curve
202         return new PolyCirculinearCurve2D<CirculinearContinuousCurve2D>(curves2);
203 	}
204 	
205 	@Override
206 	public PolyCirculinearCurve2D<? extends CirculinearContinuousCurve2D>
207 	getSubCurve(double t0, double t1) {
208 		// Call the superclass method
209 		PolyOrientedCurve2D<? extends ContinuousOrientedCurve2D> subcurve =
210 			super.getSubCurve(t0, t1);
211 		
212 		// prepare result
213 		int n = subcurve.getCurveNumber();
214 		PolyCirculinearCurve2D<CirculinearContinuousCurve2D> result = 
215 			new PolyCirculinearCurve2D<CirculinearContinuousCurve2D>(n);
216 		
217 		// add each curve after class cast
218 		for(Curve2D curve : subcurve) {
219 			if(curve instanceof CirculinearContinuousCurve2D)
220 				result.addCurve((CirculinearContinuousCurve2D) curve);
221 		}
222 		
223 		// return the result
224 		return result;
225 	}
226 
227 }