View Javadoc

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