OpenMesh
OpenMesh/Core/Geometry/QuadricT.hh
Go to the documentation of this file.
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh 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  *  OpenMesh 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 OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00046 //=============================================================================
00047 //
00048 //  CLASS QuadricT
00049 //
00050 //=============================================================================
00051 
00052 #ifndef OPENMESH_GEOMETRY_QUADRIC_HH
00053 #define OPENMESH_GEOMETRY_QUADRIC_HH
00054 
00055 
00056 //== INCLUDES =================================================================
00057 
00058 #include "Config.hh"
00059 #include <OpenMesh/Core/Geometry/VectorT.hh>
00060 #include <OpenMesh/Core/Utils/GenProg.hh>
00061 
00062 //== NAMESPACE ================================================================
00063 
00064 namespace OpenMesh { //BEGIN_NS_OPENMESH
00065 namespace Geometry { //BEGIN_NS_GEOMETRY
00066 
00067 
00068 //== CLASS DEFINITION =========================================================
00069 
00070 
00077 template <class Scalar>
00078 class QuadricT
00079 {
00080 public:
00081   typedef Scalar           value_type;
00082   typedef QuadricT<Scalar> type;
00083   typedef QuadricT<Scalar> Self;
00084   //   typedef VectorInterface<Scalar, VecStorage3<Scalar> > Vec3;
00085   //   typedef VectorInterface<Scalar, VecStorage4<Scalar> > Vec4;
00086   //typedef Vector3Elem      Vec3;
00087   //typedef Vector4Elem      Vec4;
00088 
00090   QuadricT(Scalar _a, Scalar _b, Scalar _c, Scalar _d,
00091                       Scalar _e, Scalar _f, Scalar _g,
00092                                  Scalar _h, Scalar _i,
00093                                             Scalar _j)
00094   : a_(_a), b_(_b), c_(_c), d_(_d),
00095             e_(_e), f_(_f), g_(_g),
00096                     h_(_h), i_(_i),
00097                             j_(_j)
00098   {
00099   }
00100 
00101 
00103   QuadricT( Scalar _a=0.0, Scalar _b=0.0, Scalar _c=0.0, Scalar _d=0.0 )
00104   : a_(_a*_a), b_(_a*_b),  c_(_a*_c),  d_(_a*_d),
00105                e_(_b*_b),  f_(_b*_c),  g_(_b*_d),
00106                            h_(_c*_c),  i_(_c*_d),
00107                                        j_(_d*_d)
00108   {}
00109 
00110   template <class _Point>
00111   QuadricT(const _Point& _pt)
00112   {
00113     set_distance_to_point(_pt);
00114   }
00115 
00116   template <class _Normal, class _Point>
00117   QuadricT(const _Normal& _n, const _Point& _p)
00118   {
00119     set_distance_to_plane(_n,_p);
00120   }
00121 
00122   //set operator
00123   void set(Scalar _a, Scalar _b, Scalar _c, Scalar _d,
00124                       Scalar _e, Scalar _f, Scalar _g,
00125                                  Scalar _h, Scalar _i,
00126                                             Scalar _j)
00127   {
00128     a_ = _a; b_ = _b; c_ = _c; d_ = _d;
00129              e_ = _e; f_ = _f; g_ = _g;
00130                       h_ = _h; i_ = _i;
00131                                j_ = _j;
00132   }
00133 
00134   //sets the quadric representing the squared distance to _pt
00135   template <class _Point>
00136   void set_distance_to_point(const _Point& _pt)
00137   {
00138     set(1, 0, 0, -_pt[0],
00139            1, 0, -_pt[1],
00140               1, -_pt[2],
00141                  dot(_pt,_pt));
00142   }
00143 
00144   //sets the quadric representing the squared distance to the plane [_a,_b,_c,_d]
00145   void set_distance_to_plane(Scalar _a, Scalar _b, Scalar _c, Scalar _d)
00146   {
00147     a_ = _a*_a; b_ = _a*_b; c_ = _a*_c;  d_ = _a*_d;
00148                 e_ = _b*_b; f_ = _b*_c;  g_ = _b*_d;
00149                             h_ = _c*_c;  i_ = _c*_d;
00150                                          j_ = _d*_d;
00151   }
00152 
00153   //sets the quadric representing the squared distance to the plane
00154   //determined by the normal _n and the point _p
00155   template <class _Normal, class _Point>
00156   void set_distance_to_plane(const _Normal&  _n, const _Point& _p)
00157   {
00158     set_distance_to_plane(_n[0], _n[1], _n[2], -dot(_n,_p));
00159   }
00160 
00162   void clear()  { a_ = b_ = c_ = d_ = e_ = f_ = g_ = h_ = i_ = j_ = 0.0; }
00163 
00165   QuadricT<Scalar>& operator+=( const QuadricT<Scalar>& _q )
00166   {
00167     a_ += _q.a_;  b_ += _q.b_;  c_ += _q.c_;  d_ += _q.d_;
00168                   e_ += _q.e_;  f_ += _q.f_;  g_ += _q.g_;
00169                                 h_ += _q.h_;  i_ += _q.i_;
00170                                               j_ += _q.j_;
00171     return *this;
00172   }
00173 
00174 
00176   QuadricT<Scalar>& operator*=( Scalar _s)
00177   {
00178     a_ *= _s;  b_ *= _s;  c_ *= _s;  d_ *= _s;
00179                e_ *= _s;  f_ *= _s;  g_ *= _s;
00180                           h_ *= _s;  i_ *= _s;
00181                                      j_ *= _s;
00182     return *this;
00183   }
00184 
00185 
00187   template <class _Vec4>
00188   _Vec4 operator*(const _Vec4& _v) const
00189   {
00190     Scalar x(_v[0]), y(_v[1]), z(_v[2]), w(_v[3]);
00191     return _Vec4(x*a_ + y*b_ + z*c_ + w*d_,
00192                  x*b_ + y*e_ + z*f_ + w*g_,
00193                  x*c_ + y*f_ + z*h_ + w*i_,
00194                  x*d_ + y*g_ + z*i_ + w*j_);
00195   }
00196 
00198   template <class _Vec>
00199   Scalar operator()(const _Vec& _v) const
00200   {
00201     return evaluate(_v, GenProg::Int2Type<_Vec::size_>());
00202   }
00203 
00204   Scalar a() const { return a_; }
00205   Scalar b() const { return b_; }
00206   Scalar c() const { return c_; }
00207   Scalar d() const { return d_; }
00208   Scalar e() const { return e_; }
00209   Scalar f() const { return f_; }
00210   Scalar g() const { return g_; }
00211   Scalar h() const { return h_; }
00212   Scalar i() const { return i_; }
00213   Scalar j() const { return j_; }
00214 
00215   Scalar xx() const { return a_; }
00216   Scalar xy() const { return b_; }
00217   Scalar xz() const { return c_; }
00218   Scalar xw() const { return d_; }
00219   Scalar yy() const { return e_; }
00220   Scalar yz() const { return f_; }
00221   Scalar yw() const { return g_; }
00222   Scalar zz() const { return h_; }
00223   Scalar zw() const { return i_; }
00224   Scalar ww() const { return j_; }
00225 
00226 protected:
00227 
00229   template <class _Vec3>
00230   Scalar evaluate(const _Vec3& _v, GenProg::Int2Type<3>/*_dimension*/) const
00231   {
00232     Scalar x(_v[0]), y(_v[1]), z(_v[2]);
00233     return a_*x*x + 2.0*b_*x*y + 2.0*c_*x*z + 2.0*d_*x
00234                   +     e_*y*y + 2.0*f_*y*z + 2.0*g_*y
00235                                +     h_*z*z + 2.0*i_*z
00236                                             +     j_;
00237   }
00238 
00240   template <class _Vec4>
00241   Scalar evaluate(const _Vec4& _v, GenProg::Int2Type<4>/*_dimension*/) const
00242   {
00243     Scalar x(_v[0]), y(_v[1]), z(_v[2]), w(_v[3]);
00244     return a_*x*x + 2.0*b_*x*y + 2.0*c_*x*z + 2.0*d_*x*w
00245                   +     e_*y*y + 2.0*f_*y*z + 2.0*g_*y*w
00246                                +     h_*z*z + 2.0*i_*z*w
00247                                             +     j_*w*w;
00248   }
00249 
00250 private:
00251 
00252   Scalar a_, b_, c_, d_,
00253              e_, f_, g_,
00254                  h_, i_,
00255                      j_;
00256 };
00257 
00258 
00260 typedef QuadricT<float> Quadricf;
00261 
00263 typedef QuadricT<double> Quadricd;
00264 
00265 
00266 //=============================================================================
00267 } // END_NS_GEOMETRY
00268 } // END_NS_OPENMESH
00269 //============================================================================
00270 #endif // OPENMESH_GEOMETRY_HH defined
00271 //=============================================================================