1 /* === S Y N F I G ========================================================= */
3 ** \brief Various discreet type definitions
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === S T A R T =========================================================== */
26 #ifndef __SYNFIG_VECTOR_H
27 #define __SYNFIG_VECTOR_H
29 /* === H E A D E R S ======================================================= */
35 /* === M A C R O S ========================================================= */
41 extern "C" { int _isnan(double x); }
46 // For some reason isnan() isn't working on macosx any more.
47 // This is a quick fix.
48 #if defined(__APPLE__) && !defined(SYNFIG_ISNAN_FIX)
52 inline bool isnan(double x) { return x != x; }
53 inline bool isnan(float x) { return x != x; }
54 #define SYNFIG_ISNAN_FIX 1
58 /* === T Y P E D E F S ===================================================== */
60 /* === C L A S S E S & S T R U C T S ======================================= */
70 typedef Real value_type;
76 Vector(): _x(0.0), _y(0.0) { };
77 Vector(const value_type &x, const value_type &y):_x(x),_y(y) { };
79 bool is_valid()const { return !(isnan(_x) || isnan(_y)); }
82 operator[](const int& i)
86 operator[](const int& i) const
90 operator+=(const Vector &rhs)
98 operator-=(const Vector &rhs)
106 operator*=(const value_type &rhs)
114 operator/=(const value_type &rhs)
116 value_type tmp=1.0/rhs;
123 operator+(const Vector &rhs)const
124 { return Vector(*this)+=rhs; }
127 operator-(const Vector &rhs)const
128 { return Vector(*this)-=rhs; }
131 operator*(const value_type &rhs)const
132 { return Vector(*this)*=rhs; }
135 operator/(const value_type &rhs)const
136 { return Vector(*this)/=rhs; }
140 { return Vector(-_x,-_y); }
143 operator*(const Vector &rhs)const
144 { return _x*rhs._x+_y*rhs._y; }
147 operator==(const Vector &rhs)const
148 { return _x==rhs._x && _y==rhs._y; }
151 operator!=(const Vector &rhs)const
152 { return _y!=rhs._y || _x!=rhs._x; }
154 //! Returns the squared magnitude of the vector
155 value_type mag_squared()const
156 { return _x*_x+_y*_y; }
158 //! Returns the magnitude of the vector
159 value_type mag()const
160 { return sqrt(mag_squared()); }
162 //! Returns the reciprocal of the magnitude of the vector
163 value_type inv_mag()const
164 { return 1.0/sqrt(mag_squared()); }
166 //! Returns a normalized version of the vector
168 { return (*this)*inv_mag(); }
170 //! Returns a perpendicular version of the vector
172 { return Vector(_y,-_x); }
175 { return Angle::rad(atan2(_y, _x)); }
177 bool is_equal_to(const Vector& rhs)const
179 static const value_type epsilon(0.0000000000001);
180 // return (_x>rhs._x)?_x-rhs._x<=epsilon:rhs._x-_x<=epsilon && (_y>rhs._y)?_y-rhs._y<=epsilon:rhs._y-_y<=epsilon;
181 return (*this-rhs).mag_squared()<=epsilon;
184 static const Vector zero() { return Vector(0,0); }
190 typedef Vector Point;
194 }; // END of namespace synfig
198 inline synfig::Vector::value_type
199 abs(const synfig::Vector &rhs)
200 { return rhs.mag(); }
202 }; // END of namespace std
204 #include <ETL/bezier>
209 class bezier_base<synfig::Vector,float> : public std::unary_function<float,synfig::Vector>
212 typedef synfig::Vector value_type;
213 typedef float time_type;
216 bezier_base<synfig::Vector::value_type,time_type> bezier_x,bezier_y;
221 affine_combo<value_type,time_type> affine_func;
226 const value_type &a, const value_type &b, const value_type &c, const value_type &d,
227 const time_type &r=0.0, const time_type &s=1.0):
228 a(a),b(b),c(c),d(d) { set_rs(r,s); sync(); }
232 bezier_x[0]=a[0],bezier_y[0]=a[1];
233 bezier_x[1]=b[0],bezier_y[1]=b[1];
234 bezier_x[2]=c[0],bezier_y[2]=c[1];
235 bezier_x[3]=d[0],bezier_y[3]=d[1];
241 operator()(time_type t)const
243 return synfig::Vector(bezier_x(t),bezier_y(t));
246 void evaluate(time_type t, value_type &f, value_type &df) const
248 t=(t-get_r())/get_dt();
250 const value_type p1 = affine_func(
254 const value_type p2 = affine_func(
259 f = affine_func(p1,p2,t);
263 void set_rs(time_type new_r, time_type new_s) { bezier_x.set_rs(new_r,new_s); bezier_y.set_rs(new_r,new_s); }
264 void set_r(time_type new_r) { bezier_x.set_r(new_r); bezier_y.set_r(new_r); }
265 void set_s(time_type new_s) { bezier_x.set_s(new_s); bezier_y.set_s(new_s); }
266 const time_type &get_r()const { return bezier_x.get_r(); }
267 const time_type &get_s()const { return bezier_x.get_s(); }
268 time_type get_dt()const { return bezier_x.get_dt(); }
275 operator[](int i) const
278 //! Bezier curve intersection function
279 time_type intersect(const bezier_base<value_type,time_type> &/*x*/, time_type /*near*/=0.0)const
288 /* === E N D =============================================================== */