View Javadoc

1   /* file : BoundaryPolyCurve2D.java
2    * 
3    * Project : geometry
4    *
5    * ===========================================
6    * 
7    * This library is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation, either version 2.1 of the License, or (at
10   * your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful, but 
13   * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
14   * or FITNESS FOR A PARTICULAR PURPOSE.
15   *
16   * See the GNU Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with this library. if not, write to :
20   * The Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21   * Boston, MA 02111-1307, USA.
22   * 
23   * Created on 31 mars 2007
24   *
25   */
26  
27  package math.geom2d.domain;
28  
29  import java.awt.Graphics2D;
30  import java.util.Collection;
31  
32  import math.geom2d.AffineTransform2D;
33  
34  /**
35   * A single continuous oriented curve, which defines the boundary of a planar
36   * domain. The boundary curve is composed of several continuous and oriented
37   * curves linked together to form a continuous curve. The resulting boundary
38   * curve is either a closed curve, or an infinite curve at both ends.
39   * 
40   * @author dlegland
41   */
42  public class BoundaryPolyCurve2D<T extends ContinuousOrientedCurve2D> extends PolyOrientedCurve2D<T> implements ContinuousBoundary2D {
43  
44      // ===================================================================
45      // Constructors
46  
47      public BoundaryPolyCurve2D() {
48          super();
49      }
50  
51      public BoundaryPolyCurve2D(int n) {
52          super(n);
53      }
54  
55      public BoundaryPolyCurve2D(T[] curves) {
56          super(curves);
57      }
58  
59      public BoundaryPolyCurve2D(Collection<? extends T> curves) {
60          super(curves);
61      }
62  
63      
64      // ===================================================================
65      // Static methods
66  
67      /**
68       * Static factory for creating a new BoundaryPolyCurve2D from a collection
69       * of curves.
70       * @since 0.8.1
71       */
72      public static <T extends ContinuousOrientedCurve2D> BoundaryPolyCurve2D<T> create(
73      		Collection<T> curves) {
74      	return new BoundaryPolyCurve2D<T>(curves);
75      }
76      
77      /**
78       * Static factory for creating a new BoundaryPolyCurve2D from an array of
79       * curves.
80       * @since 0.8.1
81       */
82      public static <T extends ContinuousOrientedCurve2D> BoundaryPolyCurve2D<T> create(
83      		T[] curves) {
84      	return new BoundaryPolyCurve2D<T>(curves);
85      }
86  
87      
88      // ===================================================================
89      // Methods overriding CurveSet2D methods
90  
91      /**
92       * Override the isClosed() id the following way: return true if all curves
93       * are bounded. If at least one curve is unbounded, return false.
94       */
95      @Override
96      public boolean isClosed() {
97          for (T curve : curves) {
98              if (!curve.isBounded())
99                  return false;
100         }
101         return true;
102     }
103 
104     // ===================================================================
105     // Methods implementing Boundary2D interface
106 
107     /**
108      * Returns a ArrayList<ContinuousBoundary2D> containing only
109      * <code>this</code>.
110      */
111     public Collection<? extends ContinuousBoundary2D> getBoundaryCurves() {
112     	return wrapCurve(this);
113     }
114 
115     public Domain2D getDomain() {
116         return new GenericDomain2D(this);
117     }
118 
119     public void fill(Graphics2D g2) {
120         g2.fill(this.getGeneralPath());
121     }
122 
123     // ===================================================================
124     // Methods implementing OrientedCurve2D interface
125 
126     @Override
127     public BoundaryPolyCurve2D<? extends ContinuousOrientedCurve2D> getReverseCurve() {
128         ContinuousOrientedCurve2D[] curves2 = new ContinuousOrientedCurve2D[curves
129                 .size()];
130         int n = curves.size();
131         for (int i = 0; i<n; i++)
132             curves2[i] = (ContinuousOrientedCurve2D)curves.get(n-1-i).getReverseCurve();
133         return new BoundaryPolyCurve2D<ContinuousOrientedCurve2D>(curves2);
134     }
135 
136     @Override
137     public BoundaryPolyCurve2D<ContinuousOrientedCurve2D> transform(
138             AffineTransform2D trans) {
139         BoundaryPolyCurve2D<ContinuousOrientedCurve2D> result =
140         	new BoundaryPolyCurve2D<ContinuousOrientedCurve2D>();
141         for (ContinuousOrientedCurve2D curve : curves)
142             result.addCurve((ContinuousOrientedCurve2D)curve.transform(trans));
143         return result;
144     }
145 }