View Javadoc

1   /* file : PolyCubicBezierCurve2D.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 8 mai 2006
24   *
25   */
26  
27  package math.geom2d.spline;
28  
29  import java.util.Collection;
30  
31  import math.geom2d.AffineTransform2D;
32  import math.geom2d.Box2D;
33  import math.geom2d.Point2D;
34  import math.geom2d.Vector2D;
35  import math.geom2d.curve.Curve2D;
36  import math.geom2d.curve.Curve2DUtils;
37  import math.geom2d.curve.CurveSet2D;
38  import math.geom2d.curve.PolyCurve2D;
39  
40  /**
41   * A set of Bezier curves, making a continuous curve.
42   * 
43   * @author dlegland
44   */
45  public class PolyCubicBezierCurve2D extends PolyCurve2D<CubicBezierCurve2D> {
46  
47  	// ===================================================================
48      // Constructors
49  
50      public PolyCubicBezierCurve2D() {
51          super();
52      }
53  
54      public PolyCubicBezierCurve2D(int n) {
55          super(n);
56      }
57  
58      public PolyCubicBezierCurve2D(CubicBezierCurve2D[] curves) {
59          super(curves);
60      }
61  
62      public PolyCubicBezierCurve2D(Collection<CubicBezierCurve2D> curves) {
63          super(curves);
64      }
65      
66      // ===================================================================
67      // Static methods
68  
69      /**
70       * Creates a series a cubic bezier curves, by grouping 4 adjacent points.
71       * Two consecutive curves share one point, N curves will require 3*n+1
72       * points.
73       */
74      public final static PolyCubicBezierCurve2D create(Point2D[] points){
75      	// number of points
76      	int np = points.length;
77      	
78      	// compute number of curves
79      	int nc = (np-1)/3;
80      	
81      	// create array of curves
82      	PolyCubicBezierCurve2D polyBezier = new PolyCubicBezierCurve2D(nc);
83      	
84      	// build each curve
85      	for(int i=0; i<nc; i++)
86      		polyBezier.addCurve(new CubicBezierCurve2D(
87      				points[i*3], points[i*3+1], points[i*3+2], points[i*3+3]));
88      	
89      	// return the curve
90      	return polyBezier;
91      }
92  
93      
94      /**
95       * Creates a series a cubic bezier curves, by grouping consecutive couples
96       * of points and vectors. A polycurve composed of N Bezier curves requires
97       * N+1 points and N+1 vectors. 
98       */
99      public final static PolyCubicBezierCurve2D create(
100     		Point2D[] points, Vector2D[] vectors){
101     	// number of points
102     	int np = Math.min(points.length, vectors.length);
103     	
104     	// compute number of curves
105     	int nc = (np-1)/2;
106     	
107     	// create array of curves
108     	PolyCubicBezierCurve2D polyBezier = new PolyCubicBezierCurve2D(nc);
109     	
110     	// build each curve
111     	for(int i=0; i<nc; i++)
112     		polyBezier.addCurve(new CubicBezierCurve2D(
113     				points[i*2], vectors[i*2], points[i*2+1], vectors[i*2+1]));
114     	
115     	// return the curve
116     	return polyBezier;
117     }
118 
119     
120     // ===================================================================
121     // Methods specific to PolyCubicBezierCurve2D
122 
123     /**
124      * Returns a new PolyCubicBezierCurve2D.
125      */
126     @Override
127     public PolyCubicBezierCurve2D clip(Box2D box) {
128         // Clip the curve
129         CurveSet2D<? extends Curve2D> set = Curve2DUtils.clipCurve(this, box);
130 
131         // Stores the result in appropriate structure
132         PolyCubicBezierCurve2D result = new PolyCubicBezierCurve2D(
133         		set.getCurveNumber());
134 
135         // convert the result
136         for (Curve2D curve : set.getCurves()) {
137             if (curve instanceof CubicBezierCurve2D)
138                 result.addCurve((CubicBezierCurve2D) curve);
139         }
140         return result;
141     }
142 
143     @Override
144     public PolyCubicBezierCurve2D transform(AffineTransform2D trans) {
145         PolyCubicBezierCurve2D result = new PolyCubicBezierCurve2D(this.curves.size());
146         for (CubicBezierCurve2D curve : curves)
147             result.addCurve(curve.transform(trans));
148         return result;
149     }
150 
151 }