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

transf3d.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_INCTRANSF3D_H
00024 #define AMOD_MATH_LIB_INCTRANSF3D_H
00025 
00027 // DESCRIPTION:
00028 //
00029 // Declaration of class transf3d representing a general affine or projective 
00030 // transformation in 3D space. The transform is represented by a 4x4 matrix,
00031 // the last row being the projective coefficients and are set to (0,0,0,1) for 
00032 // affine transformations.
00033 //
00034 // To transform a point or a vector, PREmultiply it by a transf3d matrix.
00035 //
00037 
00038 #include "point3d.H"
00039 #include "line3d.H"
00040 
00041 typedef Greal RAWmat[4][4];
00042 
00043 template <class T, class P, class V, class L, class Q>
00044 class _transf3d
00045 {
00046 protected:
00047     Greal _c[4][4];
00048     int    _perspective;
00049 
00050 public:
00051 
00052     // The constructor creates an identity transform.
00053     //
00054     inline _transf3d() {
00055        _c[0][1] = _c[0][2] = _c[0][3] = _c[1][0] = _c[1][2] = _c[1][3] = 
00056        _c[2][0] = _c[2][1] = _c[2][3] = _c[3][0] = _c[3][1] = _c[3][2] = 0.0;
00057        _c[0][0] = _c[1][1] = _c[2][2] = _c[3][3] = 1;
00058        // isError = false;
00059        _perspective = 0;
00060     }
00061 
00062     // The constructor creates a rigid motion transform from an origin and 
00063     // directions of x, y and z axes.
00064     //
00065     _transf3d(const P&  origin, 
00066               const V&    xDir, 
00067               const V&    yDir, 
00068               const V&    zDir);
00069 
00070     // The constructor creates a linear map (no translation)
00071     // whose columns are the given vectors
00072     //
00073     _transf3d(const V&    col0, 
00074               const V&    col1, 
00075               const V&    col2);
00076 
00077     // The constructor creates a rigid motion transform from an origin and 
00078     // direction of x and y axis. If yDir is not perpendicular to xDir, yDir
00079     // will be adjusted.
00080     //
00081     _transf3d(const P&  origin, 
00082               const V&    xDir, 
00083               const V&    yDir);
00084 
00085     // The constructor creates a rigid motion transform from an origin 
00086     // (axis.point) and the direction of the z axis (axis.vector). The 
00087     // directions of the x and y axes are determined according to the 
00088     // AutoCAD's arbitrary axis algorithm.
00089     //
00090     _transf3d(const L& axis);
00091 
00092     // The constructor creates a rigid motion transform from an origin. The
00093     // x, y and z axis are aligned with the world x, y and z axis.
00094     //
00095     _transf3d(const P& origin);
00096 
00097     Greal& operator ()(int i1, int i2)       { return _c[i2][i1]; }
00098     Greal  operator ()(int i1, int i2) const { return _c[i2][i1]; }
00099 
00100     void    set_perspective(int p)  { _perspective = p;   }
00101     int     perspective()     const { return _perspective;}
00102 
00103     V        get_scale()const{return V(X().length(),Y().length(),Z().length());}
00104     void     getCoordSystem(P& o, V& x, V& y, V& z) const
00105                              { x = X(); y = Y(); z = Z(); o = origin(); }
00106 
00107     V        X()      const  { return V(_c[0][0],_c[0][1], _c[0][2]); }
00108     V        Y()      const  { return V(_c[1][0],_c[1][1], _c[1][2]); }
00109     V        Z()      const  { return V(_c[2][0],_c[2][1], _c[2][2]); }
00110     P        origin() const  { return P(_c[3][0],_c[3][1], _c[3][2]); }
00111     Greal  *matrix() const  { return (Greal *)_c; }
00112     void     vals(RAWmat &d, bool row=true) const 
00113                              { for (int i=0; i<4; i++)
00114                                   for (int j=0; j<4; j++)
00115                                      if (row)
00116                                           d[i][j] = _c[i][j];
00117                                      else d[j][i] = _c[i][j]; }
00118 
00119     // returns the rotation part of the transform
00120     T        rotation() const { return T(P(0,0,0), X(), Y(), Z()); }
00121     
00122     // Create special transformations
00123     //
00124     static T rotation   (const  Q& quat);
00125     static T rotation   (const  L& axis,    Greal   angle);
00126     static T rotation   (const  V& axis,    Greal   angle);
00127     static T shear      (const  V& normal,  const V& shearVec);
00128     static T scaling    (const  P& fixedPt, Greal   factor);
00129     static T scaling    (const  P& fixedPt, const V& xyzFactors);
00130     static T scaling    (const  V& xyzFactors);
00131     static T scaling    (Greal    factor);
00132     static T stretching (const  L& axis);
00133     static T translation(const  V&);
00134 
00135     // The transformation maps points as follows:
00136     //
00137     // point  src1             maps to point  dst1
00138     // vec    (src1,src2)      maps to vector (dst1,dst2)
00139     // plane  (src1,src2,src3) maps to plane  (dst1,dst2,dst3)
00140     //
00141     static T align(const P& src1, const P& src2, const P& src3,
00142                    const P& dst1, const P& dst2, const P& dst3);
00143 
00144     // The transformation maps points and vectors as follows:
00145     //
00146     // point  src1             maps to point  dst1
00147     // vec src2             maps to vector dst2
00148     // plane  (src1,src2,src3) maps to plane  (dst1,dst2,dst3)
00149     //
00150     static T align(const P&  src1, const V& src2, const V& src3,
00151                    const P&  dst1, const V& dst2, const V& dst3);
00152 
00153     static T align(const P&  src1, const V&    src2,
00154                    const P&  dst1, const V&    dst2);
00155 
00156     static T alignAndScale(const P& o, const V& x, const V& y, const V& z);
00157 
00158     T        transpose      ()          const;
00159     T        unscaled       ()          const;
00160     T        normalize_basis()          const;
00161     T        orthogonalize  ()          const;
00162     T        invert         ()          const;
00163     T        derivative(const P&)       const;
00164 
00165     bool     isValid                 () const;
00166     bool     isIdentity              () const;
00167     bool     isOrthogonal            () const;  // No shear
00168     bool     isEqualScalingOrthogonal() const;  // No shear, no nonequal scaling
00169 
00170     int operator != (const T &m) const { return !(*this == m); }
00171     int operator == (const T &m) const {
00172        return origin() == m.origin() &&
00173               X()      == m.X() &&
00174               Y()      == m.Y() &&
00175               Z()      == m.Z();
00176     }
00177 
00178 }; // class transf3d
00179 
00180 //
00181 // ------------------- inline templated operators --------------
00182 //
00183 
00184 #define TPVLQ template <class T,class P,class V, class L, class Q>
00185 TPVLQ ostream &operator <<(ostream &os, const _transf3d<T,P,V,L,Q> &x); 
00186 TPVLQ istream &operator >>(istream &os,       _transf3d<T,P,V,L,Q> &x); 
00187 TPVLQ T operator *(const _transf3d<T,P,V,L,Q> &, const _transf3d<T,P,V,L,Q>&);
00188 TPVLQ P operator *(const _transf3d<T,P,V,L,Q> &, const _point3d<P,V> &);
00189 TPVLQ V operator *(const _transf3d<T,P,V,L,Q> &, const _vec3d<V> &);
00190 TPVLQ L operator *(const _transf3d<T,P,V,L,Q> &, const _line<L,P,V> &);
00191 #undef TPVLQ
00192 
00193 #ifdef GLUE_NEEDS_TEMPLATES_IN_H_FILE
00194 #include "transf3d.C"
00195 #endif
00196 
00197 #endif

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