View Javadoc

1   /* File BezierCurve2D.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  
24  package math.geom2d.spline;
25  
26  import math.geom2d.AffineTransform2D;
27  import math.geom2d.Box2D;
28  import math.geom2d.Shape2D;
29  import math.geom2d.Vector2D;
30  import math.geom2d.curve.Curve2D;
31  import math.geom2d.curve.Curve2DUtils;
32  import math.geom2d.curve.CurveSet2D;
33  import math.geom2d.curve.SmoothCurve2D;
34  
35  /**
36   * An extension of the Bezier curve provided in java.awt.geom, with support for
37   * SmoothCurve2D and OrientedCurve2D.
38   * 
39   * @deprecated replaced by CubicBezierCurve2D (0.7.1)
40   * @author Legland
41   */
42  @Deprecated
43  public class BezierCurve2D extends CubicBezierCurve2D implements Cloneable {
44  //TODO: transform as interface for cubic and quad bezier curves ?
45      /**
46  	 * 
47  	 */
48  	private static final long	serialVersionUID	= 1L;
49  
50    
51      // ===================================================================
52      // constructors
53  
54  	public BezierCurve2D() {
55          this(0, 0, 0, 0, 0, 0, 0, 0);
56      }
57  
58      /**
59       * Build a new Bezier curve from its array of coefficients. The array must
60       * have size 2*4.
61       * 
62       * @param coefs the coefficients of the BezierCurve2D.
63       */
64      public BezierCurve2D(double[][] coefs) {
65      	this(coefs[0][0], coefs[1][0], 
66      			coefs[0][0]+coefs[0][1]/3.0, 
67      			coefs[1][0]+coefs[1][1]/3.0,
68      			coefs[0][0]+2*coefs[0][1]/3.0+coefs[0][2]/3.0, 
69      			coefs[1][0]+2*coefs[1][1]/3.0+coefs[1][2]/3.0, 
70      			coefs[0][0]+coefs[0][1]+coefs[0][2]+coefs[0][3], 
71      			coefs[1][0]+coefs[1][1]+coefs[1][2]+coefs[1][3]);
72      }
73  
74      /**
75       * Build a new Bezier curve of degree 3 by specifying position of extreme
76       * points and position of 2 control points. The resulting curve is totally
77       * contained in the convex polygon formed by the 4 control points.
78       * 
79       * @param p1 first point
80       * @param ctrl1 first control point
81       * @param ctrl2 second control point
82       * @param p2 last point
83       */
84      public BezierCurve2D(java.awt.geom.Point2D p1, java.awt.geom.Point2D ctrl1,
85              java.awt.geom.Point2D ctrl2, java.awt.geom.Point2D p2) {
86          this(p1.getX(), p1.getY(), ctrl1.getX(), ctrl1.getY(), ctrl2.getX(),
87                  ctrl2.getY(), p2.getX(), p2.getY());
88      }
89  
90      /**
91       * Build a new Bezier curve of degree 3 by specifying position and tangent
92       * of first and last points.
93       * 
94       * @param p1 first point
95       * @param v1 first tangent vector
96       * @param p2 position of last point
97       * @param v2 last tangent vector
98       */
99      public BezierCurve2D(
100     		java.awt.geom.Point2D p1, Vector2D v1,
101             java.awt.geom.Point2D p2, Vector2D v2) {
102     	this(p1.getX(), p1.getY(), p1.getX()+v1.getX()/3,
103     			p1.getY()+v1.getY()/3, p2.getX()-v2.getX()/3, 
104     			p2.getY()-v2.getY()/3, p2.getX(), p2.getY());
105     }
106 
107     /**
108      * Build a new Bezier curve of degree 3 by specifying position of extreme
109      * points and position of 2 control points. The resulting curve is totally
110      * containe in the convex polygon formed by the 4 control points.
111      */
112     public BezierCurve2D(double x1, double y1, double xctrl1, double yctrl1,
113             double xctrl2, double yctrl2, double x2, double y2) {
114         super(x1, y1, xctrl1, yctrl1, xctrl2, yctrl2, x2, y2);
115     }
116 
117     // ===================================================================
118     // methods specific to BezierCurve2D
119 
120     /**
121      * Returns the Bezier curve given by control points taken in reverse
122      * order.
123      */
124     @Override
125 	public BezierCurve2D getReverseCurve() {
126         return new BezierCurve2D(
127                 this.getP2(), this.getCtrlP2(),
128                 this.getCtrlP1(), this.getP1());
129     }
130 
131     /**
132      * Computes portion of BezierCurve. If t1<t0, returns null.
133      */
134     @Override
135 	public BezierCurve2D getSubCurve(double t0, double t1) {
136         t0 = Math.max(t0, 0);
137         t1 = Math.min(t1, 1);
138         if (t0>t1)
139             return null;
140 
141         double dt = t1-t0;
142         Vector2D v0 = getTangent(t0).times(dt);
143         Vector2D v1 = getTangent(t1).times(dt);
144         return new BezierCurve2D(getPoint(t0), v0, getPoint(t1), v1);
145     }
146 
147     /**
148      * Clip the Bezier curve by a box. REturn a set of BezierCurve2D.
149      */
150     @Override
151 	public CurveSet2D<? extends BezierCurve2D> clip(Box2D box) {
152         // Clip the curve
153         CurveSet2D<SmoothCurve2D> set = 
154             Curve2DUtils.clipSmoothCurve(this, box);
155 
156         // Stores the result in appropriate structure
157         CurveSet2D<BezierCurve2D> result = new CurveSet2D<BezierCurve2D>();
158 
159         // convert the result
160         for (Curve2D curve : set.getCurves()) {
161             if (curve instanceof BezierCurve2D)
162                 result.addCurve((BezierCurve2D) curve);
163         }
164         return result;
165     }
166 
167     /**
168      * Returns the Bezier Curve transformed by the given AffineTransform2D. This
169      * is simply done by transforming control points of the curve.
170      */
171     @Override
172 	public BezierCurve2D transform(AffineTransform2D trans) {
173         return new BezierCurve2D(
174                 trans.transform(this.getP1()), 
175                 trans.transform(this.getCtrlP1()),
176                 trans.transform(this.getCtrlP2()),
177                 trans.transform(this.getP2()));
178     }
179 
180     @Override
181     public boolean equals(Object obj) {
182         if(!(obj instanceof java.awt.geom.CubicCurve2D.Double))
183             return false;
184         
185         java.awt.geom.CubicCurve2D.Double bezier = 
186             (java.awt.geom.CubicCurve2D.Double) obj;
187         if(Math.abs(this.x1-bezier.x1)>Shape2D.ACCURACY) return false;
188         if(Math.abs(this.y1-bezier.y1)>Shape2D.ACCURACY) return false;
189         if(Math.abs(this.ctrlx1-bezier.ctrlx1)>Shape2D.ACCURACY) return false;
190         if(Math.abs(this.ctrly1-bezier.ctrly1)>Shape2D.ACCURACY) return false;
191         if(Math.abs(this.ctrlx2-bezier.ctrlx2)>Shape2D.ACCURACY) return false;
192         if(Math.abs(this.ctrly2-bezier.ctrly2)>Shape2D.ACCURACY) return false;
193         if(Math.abs(this.x2-bezier.x2)>Shape2D.ACCURACY) return false;
194         if(Math.abs(this.y2-bezier.y2)>Shape2D.ACCURACY) return false;
195         
196         return true;
197     }
198     
199     @Override
200     public BezierCurve2D clone() {
201         return new BezierCurve2D(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2);
202     }
203 }