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

plane.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_INCPLANE_H
00024 
00026 #define AMOD_MATH_LIB_INCPLANE_H
00027 // DESCRIPTION:
00028 //
00029 // Declaration of a class plane, keeping a definition of an oriented plane in 
00030 // 3D. The plane is defined by its (unit) normal vector and parameter 'd', 
00031 // which is the signed distance of the plane from the origin of the coordinate 
00032 // system.
00033 //
00034 // For all points on the plane holds:
00035 //
00036 //   normal * point + d = 0
00037 //
00038 // The plane parameters are accessed via public data members 'normal' and 'd'.
00039 //
00040 // If the plane is invalid (cannot be constructed), the plane normal is set to 
00041 // a null vector.
00042 //
00044 
00045 #include "point2d.H"
00046 #include "point3d.H"
00047 #include "line3d.H"
00048 
00049 template <class PLANE, class P, class V, class L>
00050 class _plane
00051 {
00052 protected:
00053     V       _normal;
00054     Greal  _d;
00055 
00056 public:
00057 
00058     _plane()                     :                        _d(0) {}
00059     _plane(const V &n, Greal d) : _normal(n.normalize()),_d(d) {}
00060     _plane(const P &p, const V&n): _normal(n.normalize()),_d((P()-p)*_normal) {}
00061     _plane(const P &p1,  const P&p2, const P&p3)  
00062            { *this = _plane<PLANE,P,V,L>(p1, cross(p3-p1, p2-p1)); }
00063     _plane(const P &,  const V&, const V&);
00064 
00065     // Create plane from a polygon of vertices (n >=3). All polygon vertices
00066     // are used to calculate the plane coefficients to make the formula
00067     // symetrical.
00068     //
00069     _plane(const P plg[], int n);
00070 
00071     // The plane normal is given, just calculate the 'd' parameter from all 
00072     // polygon vertices.
00073     //
00074     _plane(const P plg[], int n, const V& normal);
00075 
00076     PLANE    operator -()          const { return PLANE(-_normal, -_d); }
00077     Greal   dist      (const P &p)const { return (p-P())*_normal+_d; }
00078     P        project   (const P & )const;
00079     V        project   (const V & )const;
00080     L        project   (const L & )const;
00081 
00082     P        operator* (const L & )const;
00083 
00084           V &normal    ()                { return _normal; }
00085     const V &normal    ()          const { return _normal; }
00086     Greal  &d         ()                { return _d; }
00087     Greal   d         ()          const { return _d; }
00088     P        origin    ()          const { return P()-_normal*_d; }
00089 
00090     bool     isParallel(const PLANE &p) const { return _normal.isEqual(p._normal)||
00091                                                        _normal.isEqual(-p._normal); }
00092     bool     isEqual   (const PLANE &p) const { return (_d*_normal).isEqual(
00093                                                         p._d*p._normal);  }
00094     bool     isValid   ()          const { return fabs(_normal.length() - 1) <
00095                                    epsNorMath(); }
00096 
00097     void     orthoplane(int& i1, int& i2)          const;
00098     void     orthoplane(int& i1, int& i2, int& i3) const;
00099 
00100     P        intersect(const L &l) const { return plane_intersect(l.point(), 
00101                                             l.vector(), origin(), _normal); }
00102 
00103 }; // class _plane
00104 
00105 template <class P, class V> 
00106 inline
00107 P        
00108 plane_intersect(
00109    const P &pt, 
00110    const V &D, 
00111    const P &O, 
00112    const V &N)
00113 {
00114    Greal t = (fabs(D*N) > 0.000001) ? ((O-pt) * N) / (D*N) : -1; 
00115 
00116    return pt + t * D; 
00117 }
00118 
00119 template <class P, class V> 
00120 inline
00121 Greal
00122 axis_ang( 
00123    const P &p1, 
00124    const P &p2, 
00125    const P &axispt, 
00126    const V &axis)
00127 {
00128    V v1 = (p1 - axispt).normalize();
00129    V v2 = (p2 - axispt).normalize();
00130    const Greal negone = -1.0;
00131    const Greal one    =  1.0;
00132    Greal ang = acos(clamp(v1*v2, negone, one));
00133    if (cross(v1,v2) * axis < 0)
00134       ang = -ang;
00135    return ang;
00136 }
00137 
00138 #ifdef GLUE_NEEDS_TEMPLATES_IN_H_FILE
00139 #include "plane.C"
00140 #endif
00141 #endif

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