Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

point2d.H

Go to the documentation of this file.
00001 /*
00002  * Copyright 2000, Brown University, Providence, RI.
00003  * 
00004  *                         All Rights Reserved
00005  * 
00006  * Permission to use, copy, modify, and distribute this software and its
00007  * documentation for any purpose other than its incorporation into a
00008  * commercial product is hereby granted without fee, provided that the
00009  * above copyright notice appear in all copies and that both that
00010  * copyright notice and this permission notice appear in supporting
00011  * documentation, and that the name of Brown University not be used in
00012  * advertising or publicity pertaining to distribution of the software
00013  * without specific, written prior permission.
00014  * 
00015  * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
00016  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
00017  * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
00018  * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00019  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00020  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
00021  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00022  */
00023 #ifndef AMOD_MATH_LIB_INC_POINT2d_H
00024 #define AMOD_MATH_LIB_INC_POINT2d_H
00025 
00026 #include <math.h>
00027 #include "std/config.H"
00028 #include "vector2d.H"
00029 
00030 
00031 template <class P, class V>
00032 class _point2d {
00033 protected:
00034     Greal _x, _y;
00035 
00036 public:
00037 
00038     _point2d()                     : _x(0),    _y(0)    {}
00039     _point2d(Greal xx, Greal yy) : _x(xx),   _y(yy)   {}
00040 
00041     const Greal *data()               const { return &_x; }
00042     P        operator  %(const P &p)   const { return P(_x+p[0],_y+p[1]);}
00043     P        operator  +(const V &v)   const { return P(_x+v[0],_y+v[1]);}
00044     V        operator  -(const P &p)   const { return V(_x-p[0],_y-p[1]);}
00045     P        operator  -(const V &v)   const { return P(_x-v[0],_y-v[1]);}
00046     P        operator  -()             const { return P(-_x,   -_y);     }
00047 
00048     P        operator  *(Greal    s)  const { return P(_x*s, _y*s); }
00049     P        operator  /(Greal    s)  const { return P(_x/s, _y/s); }
00050     
00051     Greal   operator [](int index)    const { return (&_x)[index];     }
00052     Greal&  operator [](int index)          { return (&_x)[index];     }
00053 
00054     Greal   length     ()             const { return sqrt(_x*_x+_y*_y); }
00055     Greal   lengthSqrd ()             const { return _x*_x+_y*_y;       }
00056 
00057     Greal   distSqrd   (const P &p)   const { return (_x-p._x) * (_x-p._x) + 
00058                                                       (_y-p._y) * (_y-p._y);  }
00059     Greal   dist       (const P &p)   const { return sqrt(distSqrd(p)); }
00060 
00061     bool     isEqual    (const P &p, Greal epsSqrd = epsAbsSqrdMath()) const 
00062                                              { return distSqrd(p) <= epsSqrd; }
00063 
00064     void     operator +=(const P &p)         { _x += p[0]; _y += p[1];     }
00065     void     operator +=(const V &v)         { _x += v[0]; _y += v[1];     }
00066     void     operator -=(const P &p)         { _x -= p[0]; _y -= p[1];     }
00067     void     operator -=(const V &v)         { _x -= v[0]; _y -= v[1];     }
00068     void     operator *=(Greal scalar)      { _x *= scalar; _y *= scalar; }
00069     void     operator /=(Greal scalar)      { _x /= scalar; _y /= scalar; }
00070 
00071     bool     operator ==(const P &p)   const { return _x == p._x && _y == p._y;}
00072     bool     operator !=(const P &p)   const { return _x != p._x || _y != p._y;}
00073 
00074 }; // class _point2d
00075 
00076 
00077 /* ---------- inlined global functions using _point2d template ------ */
00078 
00079 
00080 template <class P, class V>
00081 inline ostream &
00082 operator <<(ostream &os, const _point2d<P,V> &p) 
00083 { 
00084    return os << "< " << p[0] << " " << p[1] << " >"; 
00085 }
00086 
00087 template <class P, class V>
00088 inline istream &
00089 operator>>(istream &is, _point2d<P,V> &p) 
00090 { 
00091    char dummy;
00092    return is >> dummy >> p[0] >> p[1] >> dummy;
00093 }
00094 
00095 template <class P, class V>
00096 inline Greal det(const _point2d<P,V> &a, const _point2d<P,V> &b) {
00097                    return (a[0] * b[1] - a[1] * b[0]); }
00098 
00099 
00100 #define TPLIST _point2d_list<L,P,V>
00101 
00102 template <class L,class P,class V>
00103 class _point2d_list : public ARRAY<P> {
00104    protected :
00105     ARRAY<Greal>    _partial_length;
00106     Greal           _length;
00107 
00108    public :
00109     _point2d_list(int m=16)         :ARRAY<P>(m), _partial_length(0) {_length=0;}
00110     _point2d_list(const ARRAY<P> &p):ARRAY<P>(p), _partial_length(0) {_length=0;}
00111 
00112     // Intersection
00113     int     contains(const TPLIST &list)                          const;
00114     int     contains(const P &p)                                  const;
00115     bool    ray_intersect(const P &p,const V &d,P &hit,int loop=0)const;
00116     P       ray_intersect(const P &p,const V &d,int k0,int k1)    const;
00117 
00118     // Distance computations
00119     Greal  dist_to_seg     (const P &p, int k)                   const;
00120     Greal  avg_dist_to_seg (const P &p, int k)                   const;
00121     int     nearest_point   (const P &p)                          const;
00122     P       average ()                                            const;
00123     void    closest (const P &p, P &, Greal &, int &)            const;
00124     Greal  closest (const P &p, P &, int &)                      const;
00125     P       closest (const P &p)                                  const;
00126 
00127     // Parameterization / Length 
00128     void    update_length      ();
00129     Greal  partial_length     (int i) const {
00130        return (i>=_partial_length.num())? 0 : _partial_length[i]; }
00131     Greal  length             ()      const { return _length; }
00132     Greal  segment_length     (int i) const {
00133        return (_array[i+1]-_array[i]).length(); }
00134 
00135     V       get_tangent(Greal s) const;
00136     
00137     // Interpolation
00138     P       interpolate        (Greal s, V *tan=0,
00139                                 int *segp=0, Greal *tp=0)      const;
00140     void    interpolate_length (Greal s, int &seg, Greal &t ) const;
00141     Greal  invert             (const P &p)                     const;
00142     Greal  invert             (const P &p, int seg)            const;
00143 
00144     // Editing
00145     L      *clone_piece     (int k1, int k2) const;
00146     void    append          (TPLIST *poly);
00147     void    prepend         (TPLIST *poly);
00148    
00149 };
00150 
00151 #ifdef GLUE_NEEDS_TEMPLATES_IN_H_FILE
00152 #include "point2d.C"
00153 #endif
00154 #endif // AMOD_MATH_LIB_INC_POINT2d_H
00155 
00156 /* end of file point2d.H */

Generated on Mon Sep 15 16:25:57 2003 for gluebase by doxygen1.2.18