1 /* === S I N F G =========================================================== */
3 ** \brief Various discreet type definitions
5 ** $Id: vector.h,v 1.2 2005/01/23 04:03:21 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __SINFG_VECTOR_H
25 #define __SINFG_VECTOR_H
27 /* === H E A D E R S ======================================================= */
32 /* === M A C R O S ========================================================= */
39 extern "C" { int _isnan(double x); }
45 #define isnan __isnanf
50 /* === T Y P E D E F S ===================================================== */
52 /* === C L A S S E S & S T R U C T S ======================================= */
62 typedef Real value_type;
69 Vector(const value_type &x, const value_type &y):_x(x),_y(y) { };
71 bool is_valid()const { return !(isnan(_x) || isnan(_y)); }
74 operator[](const int& i)
78 operator[](const int& i) const
82 operator+=(const Vector &rhs)
90 operator-=(const Vector &rhs)
98 operator*=(const value_type &rhs)
106 operator/=(const value_type &rhs)
108 value_type tmp=1.0/rhs;
115 operator+(const Vector &rhs)const
116 { return Vector(*this)+=rhs; }
119 operator-(const Vector &rhs)const
120 { return Vector(*this)-=rhs; }
123 operator*(const value_type &rhs)const
124 { return Vector(*this)*=rhs; }
127 operator/(const value_type &rhs)const
128 { return Vector(*this)/=rhs; }
132 { return Vector(-_x,-_y); }
135 operator*(const Vector &rhs)const
136 { return _x*rhs._x+_y*rhs._y; }
139 operator==(const Vector &rhs)const
140 { return _x==rhs._x && _y==rhs._y; }
143 operator!=(const Vector &rhs)const
144 { return _y!=rhs._y || _x!=rhs._x; }
146 //! Returns the squared magnitude of the vector
147 value_type mag_squared()const
148 { return _x*_x+_y*_y; }
150 //! Returns the magnitude of the vector
151 value_type mag()const
152 { return sqrt(mag_squared()); }
154 //! Returns the reciprocal of the magnitude of the vector
155 value_type inv_mag()const
156 { return 1.0/sqrt(mag_squared()); }
158 //! Returns a normalized version of the vector
160 { return (*this)*inv_mag(); }
162 //! Returns a perpendicular version of the vector
164 { return Vector(_y,-_x); }
166 bool is_equal_to(const Vector& rhs)const
168 static const value_type epsilon(0.0000000000001);
169 // return (_x>rhs._x)?_x-rhs._x<=epsilon:rhs._x-_x<=epsilon && (_y>rhs._y)?_y-rhs._y<=epsilon:rhs._y-_y<=epsilon;
170 return (*this-rhs).mag_squared()<=epsilon;
173 static const Vector zero() { return Vector(0,0); }
179 typedef Vector Point;
183 }; // END of namespace sinfg
187 inline sinfg::Vector::value_type
188 abs(const sinfg::Vector &rhs)
189 { return rhs.mag(); }
191 }; // END of namespace std
193 #include <ETL/bezier>
198 class bezier_base<sinfg::Vector,float> : public std::unary_function<float,sinfg::Vector>
201 typedef sinfg::Vector value_type;
202 typedef float time_type;
205 bezier_base<sinfg::Vector::value_type,time_type> bezier_x,bezier_y;
210 affine_combo<value_type,time_type> affine_func;
215 const value_type &a, const value_type &b, const value_type &c, const value_type &d,
216 const time_type &r=0.0, const time_type &s=1.0):
217 a(a),b(b),c(c),d(d) { set_rs(r,s); sync(); }
221 bezier_x[0]=a[0],bezier_y[0]=a[1];
222 bezier_x[1]=b[0],bezier_y[1]=b[1];
223 bezier_x[2]=c[0],bezier_y[2]=c[1];
224 bezier_x[3]=d[0],bezier_y[3]=d[1];
230 operator()(time_type t)const
232 return sinfg::Vector(bezier_x(t),bezier_y(t));
235 void evaluate(time_type t, value_type &f, value_type &df) const
237 t=(t-get_r())/get_dt();
239 const value_type p1 = affine_func(
243 const value_type p2 = affine_func(
248 f = affine_func(p1,p2,t);
252 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); }
253 void set_r(time_type new_r) { bezier_x.set_r(new_r); bezier_y.set_r(new_r); }
254 void set_s(time_type new_s) { bezier_x.set_s(new_s); bezier_y.set_s(new_s); }
255 const time_type &get_r()const { return bezier_x.get_r(); }
256 const time_type &get_s()const { return bezier_x.get_s(); }
257 time_type get_dt()const { return bezier_x.get_dt(); }
264 operator[](int i) const
267 //! Bezier curve intersection function
268 time_type intersect(const bezier_base<value_type,time_type> &x, time_type near=0.0)const
277 /* === E N D =============================================================== */