PlaneT.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS PlaneT
00049 //
00050 //=============================================================================
00051 
00052 
00053 #ifndef ACG_PLANE_HH
00054 #define ACG_PLANE_HH
00055 
00056 
00057 //== INCLUDES =================================================================
00058 
00059 
00060 #include "../../Math/VectorT.hh"
00061 #include "../../Math/Matrix4x4T.hh"
00062 
00063 
00064 //== NAMESPACES ===============================================================
00065 
00066 
00067 namespace ACG {
00068 namespace Geometry {
00069 
00070 
00071 //== CLASS DEFINITION =========================================================
00072 
00073 
00080 template <typename Scalar>
00081 class PlaneT
00082 {
00083 public:
00084 
00086   typedef VectorT<Scalar, 3>  Vec3;
00087   typedef VectorT<Scalar, 4>  Vec4;
00088   typedef Matrix4x4T<Scalar>  Mat4x4;
00089 
00090 
00092   PlaneT( Scalar _a=0, Scalar _b=0, Scalar _c=0, Scalar _d=0 )
00093     : coeffs_(_a, _b, _c, _d)
00094   { HNF(); }
00095 
00096 
00098   PlaneT( const Vec3& _o, const Vec3& _n )
00099     : coeffs_(_n[0], _n[1], _n[2], -(_n|_o))
00100   { HNF(); }
00101 
00102 
00104   PlaneT( const Vec3& _v0, const Vec3& _v1, const Vec3& _v2 )
00105   {
00106     Vec3 n = (_v1-_v0) % (_v2-_v0);
00107     coeffs_ = Vec4(n[0], n[1], n[2], -(n|_v0));
00108     HNF();
00109   }
00110 
00111 
00113   Vec3 normal() const { return Vec3(coeffs_[0], coeffs_[1], coeffs_[2]); }
00114 
00115 
00117   const Vec4& coeffs() const { return coeffs_; }
00118 
00119 
00121   Scalar distance( const Vec3& _v ) const
00122   {
00123     return ( _v[0]*coeffs_[0] +
00124              _v[1]*coeffs_[1] +
00125              _v[2]*coeffs_[2] +
00126                    coeffs_[3] );
00127   }
00128 
00129 
00131   bool operator() ( const Vec3& _v ) const { return distance(_v) > 0; }
00132 
00133 
00134   // INTERSECTION
00135   enum IntersectionTarget { Line, LineSegment, Ray };
00136 
00137   // intersect with (infinite) line
00138   bool intersect_line( const Vec3& _v0, const Vec3& _v1,
00139                        Vec3& _v, Scalar& _t ) const
00140   { return intersect(_v0, _v1, _v, _t, Line); }
00141 
00142   // intersect with ray
00143   bool intersect_ray( const Vec3& _v0, const Vec3& _v1,
00144                       Vec3& _v, Scalar& _t ) const
00145   { return intersect(_v0, _v1, _v, _t, Ray); }
00146 
00147   // intersect with line segment
00148   bool intersect_linesegment( const Vec3& _v0, const Vec3& _v1,
00149                               Vec3& _v, Scalar& _t ) const
00150   { return intersect(_v0, _v1, _v, _t, LineSegment); }
00151 
00153   bool intersect( const Vec3& _v0,
00154                   const Vec3& _v1,
00155                   Vec3& _v,
00156                   Scalar& _t,
00157                   IntersectionTarget _target )  const
00158   {
00159 #define SGN(d) ((d>0.0) ? 1 : -1)
00160 
00161     Scalar  d0(distance(_v0)), d1(distance(_v1));
00162     Scalar  a0(fabs(d0)), a1(fabs(d1));
00163 
00164     // endpoint on plane
00165     if (a0 < FLT_MIN)  { _v = _v0; _t = 0.0; return true; }
00166     if (a1 < FLT_MIN)  { _v = _v1; _t = 1.0; return true; }
00167 
00168     // triv accept
00169     if (SGN(d0) != SGN(d1))
00170     {
00171       _t = (a0/(a0+a1));
00172       _v = _v0*(1.0-_t) + _v1*_t;
00173       return true;
00174     }
00175 
00176     // depends on target
00177     else
00178     {
00179       if (_target == LineSegment)  return false;
00180 
00181       if (fabs(d0-d1) < FLT_MIN) return false; // line parallel to plane
00182       else _t = d0/(d0-d1);
00183 
00184       if (_target == Ray && _t < 0.0)  return false;
00185 
00186       _v = _v0*(1.0-_t) + _v1*_t;
00187       return true;
00188     }
00189 #undef SGN
00190   }
00191 
00192 
00194   bool affine_transformation( const Mat4x4& _M )
00195   {
00196     Mat4x4 M(_M);
00197     if (!M.invert()) return false;
00198     M.transpone();
00199     affineTransformation_precomp(M);
00200     return true;
00201   }
00202 
00203 
00205   void affine_transformation_precomp( const Mat4x4& _M_inverseTransposed )
00206   {
00207     coeffs_ = _M_inverseTransposed*coeffs_;
00208     HNF();
00209   }
00210 
00211 
00212 private:
00213 
00214   void HNF() {
00215     Scalar n = normal().norm();
00216     if (n != 0.0) coeffs_ /= n;
00217   }
00218 
00219   Vec4 coeffs_;
00220 };
00221 
00222 
00223 
00224 typedef PlaneT<float>   Planef;
00225 typedef PlaneT<double>  Planed;
00226 
00227 
00228 //=============================================================================
00229 } // namespace Geometry
00230 } // namespace ACG
00231 //=============================================================================
00232 #endif // ACG_PLANE_HH defined
00233 //=============================================================================
00234 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .