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

points.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 POINTS_H
00024 #define POINTS_H
00025 
00026 #define GLUE_MLIB
00027 
00028 class Wquat;
00029 class Wtransf;
00030 class Wplane;
00031 class Wline;
00032 class Wpt;
00033 class Wvec;
00034 class ROOMpt;
00035 class ROOMvec;
00036 class XYline;
00037 class XYvec;
00038 class XYpt;
00039 class NDCvec;
00040 class NDCpt;
00041 class NDCZvec;
00042 class NDCZpt;
00043 class NDCZpt_list;
00044 class VEXEL;
00045 class PIXEL;
00046 typedef const class Wquat cWquat;
00047 typedef const class Wpt_list cWpt_list;
00048 typedef const class Wplane cWplane;
00049 typedef const class Wtransf cWtransf;
00050 typedef const class Wline cWline;
00051 typedef const class Wvec cWvec;
00052 typedef const class Wpt cWpt;
00053 typedef const class ROOMvec cROOMvec;
00054 typedef const class ROOMpt cROOMpt;
00055 typedef const class XYpt cXYpt;
00056 typedef const class XYvec cXYvec;
00057 typedef const class XYline cXYline;
00058 typedef const class XYpt_list cXYpt_list;
00059 typedef const class NDCvec cNDCvec;
00060 typedef const class NDCpt cNDCpt;
00061 typedef const class NDCZvec cNDCZvec;
00062 typedef const class NDCZpt cNDCZpt;
00063 typedef const class NDCZpt_list cNDCZpt_list;
00064 typedef const class VEXEL cVEXEL;
00065 typedef const class PIXEL cPIXEL;
00066 
00067 #include "std/strings.H"
00068 
00069 #include "mlib/point2d.H"
00070 #include "mlib/point3d.H"
00071 #include "mlib/plane.H"
00072 #include "mlib/transf3d.H"
00073 #include "mlib/quat.H"
00074 #include "mlib/nearest_pt.H"
00075 
00076 //---------------------------------------------------
00077 //   World Coordinates
00078 //---------------------------------------------------
00079 class DllImpExp Wvec : public _vec3d<Wvec> {
00080    public :
00081     static cWvec X;
00082     static cWvec Y;
00083     static cWvec Z;
00084     Wvec()        { }
00085     Wvec(cXYpt &);
00086     Wvec(Greal x, Greal y, Greal z):_vec3d<Wvec>(x,y,z) { }
00087     Wvec(cWpt &, cVEXEL &);
00088     Wvec(cROOMvec &);
00089 };
00090 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00091 // Should be able to define this once for _vec3d, but that doesn't work
00092 // (then again, we have to do this because of template problems in the first
00093 // place)
00094 inline Wvec operator *(Greal s, const Wvec& p) { return p * s;}
00095 #endif
00096 
00097 class DllImpExp Wpt : public _point3d<Wpt, Wvec> {
00098    public :
00099            Wpt() { }
00100            Wpt(Greal x, Greal y, Greal z):_point3d<Wpt,Wvec>(x,y,z) { }
00101            Wpt(cXYpt &);
00102            Wpt(cNDCZpt &);
00103            Wpt(cXYpt &, Greal d);
00104            Wpt(cXYpt &, cWpt &);
00105            Wpt(cROOMpt &);
00106     inline Wpt(cWtransf &);
00107     inline Wpt(cWline &, cWline &);
00108     inline Wpt(cWline &, cWpt   &);
00109     inline Wpt(cWplane&, cWline &);
00110 };
00111 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00112 inline Wpt operator *(Greal s, const Wpt& p) { return p * s;}
00113 #endif
00114 
00115 class Wline : public _line<Wline, Wpt, Wvec> 
00116 {
00117    public:
00118      Wline() { }
00119      Wline(cWpt &p,  cWvec &v) : _line<Wline,Wpt,Wvec>(p,v)  {}
00120      Wline(cWpt &p1, cWpt  &p2): _line<Wline,Wpt,Wvec>(p1,p2) {}
00121      Wline(cXYpt&x)            : _line<Wline,Wpt,Wvec>(Wpt(x),Wvec(x)) {}
00122 };
00123 
00124 #define TRANSF3D _transf3d<Wtransf,Wpt,Wvec,Wline,Wquat>
00125 class Wtransf : public TRANSF3D
00126 {
00127    public:
00128     Wtransf()   { }
00129     Wtransf(cWpt   &  origin, 
00130             cWvec  &    xDir, 
00131             cWvec  &    yDir, 
00132             cWvec  &    zDir) : TRANSF3D(origin,xDir,yDir,zDir) { }
00133     Wtransf(cWvec  &    col0, 
00134             cWvec  &    col1, 
00135             cWvec  &    col2) : TRANSF3D(col0,col1,col2) { }
00136     Wtransf(cWpt   &  origin, 
00137             cWvec  &    xDir, 
00138             cWvec  &    yDir) : TRANSF3D(origin,xDir,yDir) { }
00139     Wtransf(cWline &  axis)   : TRANSF3D(axis) { }
00140     Wtransf(cWpt   &  origin) : TRANSF3D(origin) { }
00141 };
00142 #undef TRANSF3D
00143 
00144 #define DPLANE _plane<Wplane,Wpt,Wvec,Wline>
00145 class Wplane : public DPLANE {
00146    public :
00147     Wplane()                     { }
00148     Wplane(cWvec  &n, Greal d)             : DPLANE(n,d) { }
00149     Wplane(cWpt&n, cWvec&v)                 : DPLANE(n,v) { }
00150     Wplane(cWpt&n, cWpt&p1, cWpt&p2)        : DPLANE(n,p1,p2) { }
00151     Wplane(cWpt&p, cWvec&v1, cWvec&v2)      : DPLANE(p,v1,v2) { }
00152     Wplane(cWpt plg[], int n)               : DPLANE(plg,n) { }
00153     Wplane(cWpt plg[], int n, cWvec& normal): DPLANE(plg,n,normal) { }
00154 };
00155 #undef DPLANE
00156 
00157 inline Wpt::Wpt(cWtransf &t)           { (*this) = t.origin(); }
00158 inline Wpt::Wpt(cWline  &a, cWline &b) { (*this) = a.intersect(b); }
00159 inline Wpt::Wpt(cWline  &a, cWpt   &b) { (*this) = a.project(b); }
00160 inline Wpt::Wpt(cWplane &a, cWline &b) { (*this) = a.intersect(b); }
00161 
00162 class Wpt_list : public _point3d_list<Wpt_list,Wtransf,Wpt, Wvec>
00163 {
00164   public:
00165    Wpt_list(int max=0):_point3d_list<Wpt_list,Wtransf,Wpt,Wvec>(max) { }
00166    Wpt_list(const _point3d_list<Wpt_list,Wtransf,Wpt,Wvec> &p)
00167                        :_point3d_list<Wpt_list,Wtransf,Wpt,Wvec>(p)   { }
00168    Wpt_list(const _point3d_list<Wpt_list,Wtransf,Wpt,Wvec> &p, cWtransf &t)
00169                        :_point3d_list<Wpt_list,Wtransf,Wpt,Wvec>(p,t) { }
00170 };
00171 
00172 inline Wpt_list 
00173 operator *(cWtransf &x, cWpt_list &pts) {
00174    Wpt_list res;
00175    for (int i = 0; i < pts.num(); i++)
00176       res += x * pts[i];
00177    return res;
00178 }
00179 
00180 #define QUAT _quat<Wquat, Wtransf, Wpt, Wvec, Wline>
00181 class Wquat : public QUAT
00182 {
00183 public:
00184    Wquat()                      {}
00185    Wquat(cWvec& v, Greal w)    : QUAT(v,w) {}
00186    Wquat(Greal s)              : QUAT(Wvec(),s) {}
00187    Wquat(cWtransf& t)           : QUAT(t) {}
00188    Wquat(cWvec& v1, cWvec& v2)  : QUAT(v1,v2) {}
00189 };
00190 #undef QUAT
00191    
00192 //---------------------------------------------------
00193 //   ROOM Coordinates
00194 // Physical coordinate mapping of the platform
00195 // coordinates in meters.
00196 //---------------------------------------------------
00197 class DllImpExp ROOMvec : public _vec3d<ROOMvec> {
00198    public :
00199     static cROOMvec X;
00200     static cROOMvec Y;
00201     static cROOMvec Z;
00202     ROOMvec()        { }
00203     ROOMvec(cWvec &);
00204     ROOMvec(Greal x, Greal y, Greal z):_vec3d<ROOMvec>(x,y,z) { }
00205 };
00206 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00207 inline ROOMvec operator *(Greal s, const ROOMvec& p) { return p * s;}
00208 #endif
00209 class DllImpExp ROOMpt : public _point3d<ROOMpt, ROOMvec> {
00210    public :
00211     ROOMpt() { }
00212     ROOMpt(Greal x, Greal y, Greal z):_point3d<ROOMpt,ROOMvec>(x,y,z) { }
00213     ROOMpt(cWpt &);
00214 };
00215 
00216 
00217 //---------------------------------------------------
00218 //   XY coordinates : -1 to 1 normalized coordinates
00219 //---------------------------------------------------
00220 class XYvec : public _vec2d<XYvec> {
00221    public :
00222     XYvec() { }
00223     XYvec(Greal x, Greal y):_vec2d<XYvec>(x,y) { }
00224     DllImpExp XYvec(cNDCvec &);
00225     DllImpExp XYvec(cVEXEL &);
00226 };
00227 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00228 inline XYvec operator *(Greal s, const XYvec& p) { return p * s;}
00229 #endif
00230 class DllImpExp XYpt : public _point2d<XYpt, XYvec> {
00231    public :
00232           XYpt() { }
00233           XYpt(Greal x, Greal y):_point2d<XYpt,XYvec>(x,y) { }
00234           XYpt(cNDCpt &);
00235           XYpt(cPIXEL &);
00236           XYpt(cWpt   &);
00237    inline XYpt(cXYline &, cXYline &);
00238    inline XYpt(cXYline &, cXYpt   &);
00239 };
00240 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00241 inline XYpt operator *(Greal s, const XYpt& p) { return p * s;}
00242 #endif
00243 class XYline : public _line<XYline, XYpt, XYvec> 
00244 {
00245    public:
00246      XYline() { }
00247      XYline(cXYpt &p,  cXYvec &v) : _line<XYline,XYpt,XYvec>(p,v)  {}
00248      XYline(cXYpt &p1, cXYpt  &p2): _line<XYline,XYpt,XYvec>(p1,p2) {}
00249 };
00250 class XYpt_list : public _point2d_list<XYpt_list,XYpt,XYvec> {
00251    public :
00252     XYpt_list(int m=0):_point2d_list<XYpt_list,XYpt,XYvec>(m) { }
00253     XYpt_list(const _point2d_list<XYpt_list,XYpt,XYvec> &p)
00254                        :_point2d_list<XYpt_list,XYpt,XYvec>(p) { }
00255 };
00256 
00257 inline XYpt::XYpt(cXYline &a, cXYline &b) {(*this) = a.intersect(b);}
00258 inline XYpt::XYpt(cXYline &a, cXYpt   &b) {(*this) = a.project(b);}
00259 
00260 //------------------------------------------------------
00261 //   NDCZ coordinates : 3D normalized aspect coordinates
00262 //------------------------------------------------------
00263 class DllImpExp NDCZvec : public _vec3d<NDCZvec> {
00264    public :
00265     NDCZvec() { }
00266     NDCZvec(Greal x, Greal y, Greal z):_vec3d<NDCZvec>(x,y,z) { }
00267     NDCZvec(cNDCvec &);
00268     NDCZvec(cXYvec &);
00269     NDCZvec(cWvec&v, cWtransf& obj_to_ndc_der);
00270     NDCZvec(cVEXEL &);   
00271 
00272     NDCZvec perpendicular() const { return NDCZvec(-_y, _x, 0); }
00273     Greal  planar_length() const { return sqrt(_x*_x+_y*_y); }
00274 };
00275 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00276 inline NDCZvec operator *(Greal s, const NDCZvec& p) { return p * s;}
00277 #endif
00278 
00279 class DllImpExp NDCZpt : public _point3d<NDCZpt, NDCZvec> {
00280    public :
00281      NDCZpt() { }
00282      NDCZpt(Greal x, Greal y, Greal z):_point3d<NDCZpt,NDCZvec>(x,y,z) { }
00283      NDCZpt(cWpt &w);
00284      NDCZpt(cWpt &w, cWtransf& PM);
00285      NDCZpt(cXYpt &);
00286      NDCZpt(cNDCpt &);
00287      NDCZpt(cPIXEL &);
00288      Greal planar_length(cNDCZpt& p) const {
00289         Greal dx = _x - p._x, dy = _y - p._y;
00290         return sqrt(dx*dx + dy*dy);
00291      }
00292 };
00293 
00294 class DllImpExp NDCZpt_list : public ARRAY<NDCZpt> {
00295  protected:
00296    ARRAY<Greal>    _partial_length;
00297 
00298  public :
00299    NDCZpt_list(int max=0)             :ARRAY<NDCZpt>(max) { }
00300    NDCZpt_list(const ARRAY<NDCZpt> &p) :ARRAY<NDCZpt>(p) { }
00301 
00302    // Parameterization / Lengths
00303    NDCZpt       interpolate(Greal s, NDCZvec *tan=0, 
00304                             int*segp=0, Greal*tp=0) const;
00305    void         interpolate_length(Greal s, int &seg, Greal &t) const;
00306 
00307    void   update_length      () { Greal len = 0;
00308                                   _partial_length.clear();
00309                                   if (_num > 0)
00310                                      _partial_length.realloc(_num);
00311                                   _partial_length += 0;
00312                                   for ( int i=1; i< _num; i++ ) {
00313                                      len += segment_length(i-1);
00314                                      _partial_length += len;
00315                                   }
00316                                }
00317    Greal partial_length     (int i) const { return i>=_partial_length.num() ? 
00318                                                    0 : _partial_length[i]; }
00319    Greal length             ()      const { return _partial_length.last(); }
00320    NDCZvec segment           (int i) const { return _array[i+1]-_array[i];}
00321    Greal segment_length     (int i) const { return segment(i).length(); }
00322 
00323    // misc
00324    NDCZvec  tan(int i)      const;
00325    NDCZvec  normal() const {
00326                NDCZvec ret(0, 1, 0);
00327                if (num() > 2) {
00328                   const int delta = num() / 3;
00329                   const NDCZpt a = _array[0];
00330                   const NDCZpt b = _array[delta];
00331                   const NDCZpt c = _array[2 * delta];
00332                   const NDCZpt d = _array[(3 * delta) % num()];
00333                   ret = NDCZvec((c[1]-a[1])*(d[2]-b[2]) + (c[2]-a[2])*(b[1]-d[1]),
00334                           (c[2]-a[2])*(d[0]-b[0]) + (c[0]-a[0])*(b[2]-d[2]),
00335                           (c[0]-a[0])*(d[1]-b[1]) + (c[1]-a[1])*(b[0]-d[0]));
00336                }
00337                return ret;
00338           }
00339 
00340    NDCZpt   average () const { NDCZpt sum; 
00341                                for(int i=0;i<num();i++) sum=sum%(*this)[i];
00342                                return sum/(num() ? num():1); 
00343                              }
00344 };
00345 
00346 DllImpExp NDCZvec normal_to_ndcz(cWpt& p, cWvec& world_normal);
00347 
00348 //---------------------------------------------------
00349 //   NDC coordinates : normalized aspect coordinates
00350 //---------------------------------------------------
00351 class DllImpExp NDCvec : public _vec2d<NDCvec> {
00352    public :
00353     NDCvec() { }
00354     NDCvec(Greal x, Greal y):_vec2d<NDCvec>(x,y) { }
00355     NDCvec(cXYvec &);
00356     NDCvec(cVEXEL &);
00357 };
00358 class DllImpExp NDCpt : public _point2d<NDCpt, NDCvec> {
00359    public :
00360      NDCpt() { }
00361      NDCpt(Greal x, Greal y):_point2d<NDCpt,NDCvec>(x,y) { }
00362      NDCpt(cNDCZpt& n):_point2d<NDCpt,NDCvec>(n[0],n[1]) { }
00363      NDCpt(cWpt &w);
00364      NDCpt(cXYpt &);
00365 };
00366 
00367 //---------------------------------------------------
00368 //   PIXEL coordinates
00369 //
00370 //      corresponds to GL-style pixels coords:
00371 //      lower left corner is (0,0)
00372 //      upper right is (width-1, height-1)
00373 //---------------------------------------------------
00374 class DllImpExp VEXEL : public _vec2d<VEXEL> {
00375  public :
00376    VEXEL() { }
00377    VEXEL(Greal x, Greal y):_vec2d<VEXEL>(x,y) { }
00378    VEXEL(cXYvec &);
00379    VEXEL(cNDCvec &);
00380    VEXEL(cWpt &, cWvec &);
00381 };
00382 #ifdef GLUE_NEEDS_DOUBLE_STAR_EXPLICIT
00383 inline VEXEL operator *(Greal s, const VEXEL& p) { return p * s;}
00384 #endif
00385 
00386 class DllImpExp PIXEL : public _point2d<PIXEL, VEXEL> {
00387  public :
00388    PIXEL() { }
00389    PIXEL(Greal x, Greal y):_point2d<PIXEL,VEXEL>(x,y) { }
00390    PIXEL(cXYpt &);
00391    // XXX - workaround for gcc 2.97
00392    PIXEL(cWpt &w) { cXYpt xy(w); cPIXEL pix = xy; *this = pix; }
00393 };
00394 
00395 class PIXELline : public _line<PIXELline, PIXEL, VEXEL> {
00396  public:
00397    PIXELline() : _line<PIXELline, PIXEL, VEXEL> () { }
00398    PIXELline(cPIXEL &p, cVEXEL &v) :
00399       _line<PIXELline, PIXEL, VEXEL>(p,v)          { }
00400    PIXELline(cPIXEL &p1, cPIXEL &p2) :
00401       _line<PIXELline, PIXEL, VEXEL>(p1,p2)        { }
00402 };
00403 
00404 class PIXEL_list : public _point2d_list<PIXEL_list,PIXEL,VEXEL> {
00405  public:
00406    PIXEL_list(int n=0) : _point2d_list<PIXEL_list,PIXEL,VEXEL>(n) { }
00407    PIXEL_list(const _point2d_list<PIXEL_list,PIXEL,VEXEL> &list) :
00408       _point2d_list<PIXEL_list,PIXEL,VEXEL>(list)                  { }
00409    PIXEL_list(const XYpt_list &list) :
00410       _point2d_list<PIXEL_list,PIXEL,VEXEL>(list.num())            {
00411       for(int i=0; i<list.num(); i++ ) {
00412          // must use operator+=() instead of (*this) += so it will 
00413          // compile under windows
00414          operator+=(PIXEL(list[i]));
00415       }
00416    }
00417 };
00418 
00419 
00420 extern DllImpExp cWtransf &Identity;
00421 extern DllImpExp cWpt     &Origin;
00422 
00423 inline double
00424 world_length(cWpt& p, double r)
00425 {
00426    // given point p in world space and pixel length r,
00427    // return the world space length of a segment passing
00428    // through p, parallel to the film plane, with pixel
00429    // length r;
00430    return 2*(p - Wpt(XYpt(PIXEL(p) + VEXEL(r/2,0)), p)).length();
00431 }
00432 
00433 inline double
00434 obj_length(cWpt& o, double r, cWtransf& M, cWtransf& I)
00435 {
00436    // same as above, but for object-space point o, with
00437    // transform M from object to world and its inverse I:
00438    Wpt w = M*o;
00439    return (o - I*Wpt(XYpt(PIXEL(w)+VEXEL(r,0)), w)).length();
00440 }
00441 
00442 // Returns true if the two line segments (p1,p2) and (q1,q2) intersect or 
00443 // overlap, false otherwise.
00444 //
00445 template <class  P, class L>
00446 inline bool 
00447 intersect_segs(const L& line1, const L& line2, P& inter)
00448 {
00449    P ap = line1.intersect(line2);
00450    P bp = line2.intersect(line1);
00451    if (ap.isEqual(bp)) {    // Lines intersect, do the line segments?
00452       inter = ap;
00453 
00454       // Is ap on line1 and line2?
00455       double len1 = line1.vector().length();
00456       double len2 = line2.vector().length();
00457       double dot1 = (ap - line1.point()) * line1.vector() / len1;
00458       if (dot1 <= len1  && dot1 >= 0) {
00459           double dot2 = (ap - line2.point()) * line2.vector() / len2;
00460       if (dot2 <= len2 && dot2 >= 0) 
00461              return 1;
00462       } 
00463    } 
00464    return 0;
00465 }
00466 
00467 
00468 #endif // POINTS_H
00469 
00470 /* end of file points.H */

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