View Javadoc

1   /**
2    * File: 	GenericCirculinearRing2D.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.Collection;
13  
14  import math.geom2d.AffineTransform2D;
15  import math.geom2d.domain.BoundaryPolyCurve2D;
16  import math.geom2d.domain.ContinuousOrientedCurve2D;
17  import math.geom2d.transform.CircleInversion2D;
18  
19  
20  /**
21   * A basic implementation of a CirculinearRing2D, which is supposed to be
22   * always bounded and closed.
23   * @author dlegland
24   *
25   */
26  public class GenericCirculinearRing2D 
27  extends PolyCirculinearCurve2D<CirculinearElement2D>
28  implements CirculinearRing2D {
29  //TODO: parametrize with curve type ?
30  	
31      // ===================================================================
32      // constructors
33  
34      public GenericCirculinearRing2D() {
35          super();
36          this.closed = true;
37      }
38  
39      public GenericCirculinearRing2D(int size) {
40          super(size);
41          this.closed = true;
42      }
43  
44      public GenericCirculinearRing2D(CirculinearElement2D[] curves) {
45          super(curves);
46          this.closed = true;
47      }
48  
49      /**
50       * @deprecated, as a ring is closed by definition (0.8.1)
51       */
52      @Deprecated
53      public GenericCirculinearRing2D(CirculinearElement2D[] curves, 
54      		boolean closed) {
55          super(curves, closed);
56      }
57  
58      public GenericCirculinearRing2D(
59      		Collection<? extends CirculinearElement2D> curves) {
60          super(curves);
61          this.closed = true;
62      }
63  
64      /**
65       * @deprecated, as a ring is closed by definition (0.8.1)
66       */
67      @Deprecated
68      public GenericCirculinearRing2D(
69      		Collection<? extends CirculinearElement2D> curves, 
70      		boolean closed) {
71          super(curves, closed);
72      }
73  
74      // ===================================================================
75      // static methods
76  
77      /**
78       * Static factory for creating a new GenericCirculinearRing2D from a collection of
79       * curves.
80       * @since 0.8.1
81       */
82      public static <T extends CirculinearElement2D> GenericCirculinearRing2D
83      create(Collection<T> curves) {
84      	return new GenericCirculinearRing2D(curves);
85      }
86      
87      /**
88       * Static factory for creating a new GenericCirculinearRing2D from an array of
89       * curves.
90       * @since 0.8.1
91       */
92      public static GenericCirculinearRing2D create(
93      		CirculinearElement2D[] curves) {
94      	return new GenericCirculinearRing2D(curves);
95      }
96  
97      
98      // ===================================================================
99      // methods specific to GenericCirculinearRing2D
100 
101     // ===================================================================
102     // static methods
103 
104 	@Override
105     public CirculinearRing2D getParallel(double dist) {
106     	return new GenericCirculinearRing2D(
107     			CirculinearCurve2DUtils.createContinuousParallel(this, dist)
108     			.getSmoothPieces());
109     }
110     
111 	public Collection<? extends GenericCirculinearRing2D> getContinuousCurves() {
112     	return wrapCurve(this);
113     }
114 	
115 	@Override
116 	public GenericCirculinearRing2D transform(CircleInversion2D inv) {
117     	// Allocate array for result
118 		GenericCirculinearRing2D result =
119 			new GenericCirculinearRing2D(curves.size());
120         
121         // add each transformed element
122         for (CirculinearElement2D element : curves)
123             result.addCurve((CirculinearElement2D)element.transform(inv));
124         return result;
125 	}
126 	
127 	/* (non-Javadoc)
128 	 * @see math.geom2d.domain.Boundary2D#fill(java.awt.Graphics2D)
129 	 */
130 	public void fill(Graphics2D g2) {
131 		g2.fill(this.getGeneralPath());
132 	}
133 
134 	/* (non-Javadoc)
135 	 * @see math.geom2d.domain.Boundary2D#getBoundaryCurves()
136 	 */
137 	public Collection<? extends CirculinearContour2D> getBoundaryCurves() {
138         return wrapCurve(this);
139 	}
140 
141 	/* (non-Javadoc)
142 	 * @see math.geom2d.domain.Boundary2D#getDomain()
143 	 */
144 	public CirculinearDomain2D getDomain() {
145 		return new GenericCirculinearDomain2D(this);
146 	}
147 
148 	@Override
149 	public GenericCirculinearRing2D getReverseCurve(){
150     	int n = curves.size();
151         // create array of reversed curves
152     	CirculinearElement2D[] curves2 = new CirculinearElement2D[n];
153         
154         // reverse each curve
155         for (int i = 0; i<n; i++)
156             curves2[i] = (CirculinearElement2D)curves.get(n-1-i).getReverseCurve();
157         
158         // create the reversed final curve
159         return new GenericCirculinearRing2D(curves2);
160 	}
161 	
162 	@Override
163 	public BoundaryPolyCurve2D<ContinuousOrientedCurve2D> 
164 	transform(AffineTransform2D trans) {
165 		// number of curves
166 		int n = this.getCurveNumber();
167 		
168 		// create result curve
169         BoundaryPolyCurve2D<ContinuousOrientedCurve2D> result =
170         	new BoundaryPolyCurve2D<ContinuousOrientedCurve2D>(n);
171         
172         // add each curve after class cast
173         for (ContinuousOrientedCurve2D curve : curves)
174             result.addCurve((ContinuousOrientedCurve2D)curve.transform(trans));
175         return result;
176 	}
177 
178 }