View Javadoc

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