OpenMesh
OpenMesh/Tools/Decimater/ModRoundnessT.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 ModRoundnessT
00049 //
00050 //=============================================================================
00051 
00052 #ifndef OPENMESH_TOOLS_MODROUNDNESST_HH
00053 #define OPENMESH_TOOLS_MODROUNDNESST_HH
00054 
00055 
00056 //== INCLUDES =================================================================
00057 
00058 #include <OpenMesh/Tools/Decimater/ModBaseT.hh>
00059 #include <math.h>
00060 
00061 #if defined(OM_CC_MSVC)
00062 #  define OM_ENABLE_WARNINGS 4244
00063 #  pragma warning(disable : OM_ENABLE_WARNINGS )
00064 #endif
00065 
00066 //== NAMESPACE ================================================================
00067 
00068 namespace OpenMesh { // BEGIN_NS_OPENMESH
00069 namespace Decimater { // BEGIN_NS_DECIMATER
00070 
00071 
00072 //== CLASS DEFINITION =========================================================
00073 
00074 
00077 template <class DecimaterType>
00078 class ModRoundnessT : public ModBaseT<DecimaterType>
00079 {
00080 public:
00081   DECIMATING_MODULE( ModRoundnessT, DecimaterType, Roundness );
00082 
00083 public:
00084 
00085   // typedefs
00086   typedef typename Mesh::Point                      Point;
00087   typedef typename vector_traits<Point>::value_type value_type;
00088 
00089 public:
00090    
00092   ModRoundnessT( DecimaterType &_dec ) :
00093     Base(_dec, false), 
00094     min_r_(-1.0)
00095   { }
00096  
00098   ~ModRoundnessT() { }
00099 
00100 public: // inherited
00101    
00112   float collapse_priority(const CollapseInfo& _ci)  
00113   {    
00114 //     using namespace OpenMesh;
00115 
00116     typename Mesh::ConstVertexOHalfedgeIter voh_it(Base::mesh(), _ci.v0);
00117     double                                  r;
00118     double                                  priority = 0.0; //==LEGAL_COLLAPSE
00119     typename Mesh::FaceHandle               fhC, fhB;
00120     Vec3f                                   B,C;
00121     
00122     if ( min_r_ < 0.0 ) // continues mode
00123     {      
00124       C   = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(voh_it)));
00125       fhC = Base::mesh().face_handle( voh_it.handle() );
00126 
00127       for (++voh_it; voh_it; ++voh_it) 
00128       {
00129         B   = C;
00130         fhB = fhC;
00131         C   = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(voh_it)));
00132         fhC = Base::mesh().face_handle( voh_it.handle() );
00133 
00134         if ( fhB == _ci.fl || fhB == _ci.fr )
00135           continue;
00136       
00137         // simulate collapse using position of v1
00138         r = roundness( vector_cast<Vec3f>(_ci.p1), B, C );
00139       
00140         // return the maximum non-roundness
00141         priority = std::max( priority, (1.0-r) );
00142 
00143       }
00144     }
00145     else // binary mode
00146     {
00147       C   = vector_cast<Vec3f>(Base::mesh().point( Base::mesh().to_vertex_handle(voh_it)));
00148       fhC = Base::mesh().face_handle( voh_it.handle() );
00149 
00150       for (++voh_it; voh_it && (priority==Base::LEGAL_COLLAPSE); ++voh_it) 
00151       {
00152         B   = C;
00153         fhB = fhC;
00154         C   = vector_cast<Vec3f>(Base::mesh().point(Base::mesh().to_vertex_handle(voh_it)));
00155         fhC = Base::mesh().face_handle( voh_it.handle() );
00156 
00157         if ( fhB == _ci.fl || fhB == _ci.fr )
00158           continue;
00159 
00160         priority = ( (r=roundness( vector_cast<Vec3f>(_ci.p1), B, C )) < min_r_)
00161           ? Base::ILLEGAL_COLLAPSE 
00162           : Base::LEGAL_COLLAPSE;
00163       }
00164     }
00165 
00166     return (float) priority;
00167   }
00168   
00169    
00170 
00171 public: // specific methods
00172 
00173   void set_min_angle( float _angle, bool /* _binary=true */ )
00174   {
00175     assert( _angle > 0 && _angle < 60 );
00176 
00177     _angle = float(M_PI * _angle /180.0);
00178 
00179     Vec3f A,B,C;
00180 
00181     A = Vec3f(             0, 0,           0);
00182     B = Vec3f( 2*cos(_angle), 0,           0);
00183     C = Vec3f(   cos(_angle), sin(_angle), 0);
00184 
00185     double r1 = roundness(A,B,C);
00186 
00187     _angle = float(0.5 * ( M_PI - _angle ));
00188 
00189     A = Vec3f(             0, 0,           0);
00190     B = Vec3f( 2*cos(_angle), 0,           0);
00191     C = Vec3f(   cos(_angle), sin(_angle), 0);
00192 
00193     double r2 = roundness(A,B,C);
00194 
00195     set_min_roundness( value_type(std::min(r1,r2)), true ); 
00196   }
00197 
00205   void set_min_roundness( value_type _min_roundness, bool _binary=true )
00206   {
00207     assert( 0.0 <= _min_roundness && _min_roundness <= 1.0 );
00208     min_r_  = _min_roundness;
00209     Base::set_binary(_binary);
00210   }
00211 
00213   void unset_min_roundness()
00214   {
00215     min_r_  = -1.0;
00216     Base::set_binary(false);
00217   }
00218 
00219   // Compute a normalized roundness of a triangle ABC
00220   //
00221   // Having
00222   //   A,B,C corner points of triangle
00223   //   a,b,c the vectors BC,CA,AB
00224   //   Area  area of triangle
00225   //
00226   // then define
00227   //
00228   //      radius of circumference 
00229   // R := -----------------------
00230   //      length of shortest edge
00231   //
00232   //       ||a|| * ||b|| * ||c||    
00233   //       ---------------------
00234   //             4 * Area                 ||a|| * ||b|| * ||c||
00235   //    = ----------------------- = -----------------------------------
00236   //      min( ||a||,||b||,||c||)   4 * Area * min( ||a||,||b||,||c|| )
00237   //
00238   //                      ||a|| * ||b|| * ||c||
00239   //    = -------------------------------------------------------
00240   //      4 *  1/2 * ||cross(B-A,C-A)||  * min( ||a||,||b||,||c|| )
00241   //
00242   //                         a'a * b'b * c'c
00243   // R� = ----------------------------------------------------------
00244   //       4 * cross(B-A,C-A)'cross(B-A,C-A) * min( a'a, b'b, c'c )
00245   //
00246   //                      a'a * b'b * c'c
00247   // R = 1/2 * sqrt(---------------------------)
00248   //                 AA * min( a'a, b'b, c'c )
00249   //
00250   // At angle 60� R has it's minimum for all edge lengths = sqrt(1/3)
00251   //
00252   // Define normalized roundness 
00253   //
00254   // nR := sqrt(1/3) / R
00255   //
00256   //                         AA * min( a'a, b'b, c'c )
00257   //     = sqrt(4/3) * sqrt(---------------------------)
00258   //                              a'a * b'b * c'c
00259   //
00260   double roundness( const Vec3f& A, const Vec3f& B, const Vec3f &C )
00261   {
00262     const value_type epsilon = value_type(1e-15);
00263 
00264     static const value_type sqrt43 = value_type(sqrt(4.0/3.0)); // 60�,a=b=c, **)    
00265 
00266     Vec3f vecAC     = C-A;
00267     Vec3f vecAB     = B-A;
00268 
00269     // compute squared values to avoid sqrt-computations
00270     value_type aa = (B-C).sqrnorm();
00271     value_type bb = vecAC.sqrnorm();
00272     value_type cc = vecAB.sqrnorm();
00273     value_type AA = cross(vecAC,vecAB).sqrnorm(); // without factor 1/4   **)
00274 
00275     if ( AA < epsilon )
00276       return 0.0;
00277 
00278     double nom   = AA * std::min( std::min(aa,bb),cc );
00279     double denom = aa * bb * cc;    
00280     double nR    = sqrt43 * sqrt(nom/denom);
00281 
00282     return nR;
00283   }
00284 
00285 private:
00286   
00287   value_type min_r_;
00288 };
00289 
00290 
00291 //=============================================================================
00292 } // END_NS_DECIMATER
00293 } // END_NS_OPENMESH
00294 //=============================================================================
00295 #if defined(OM_CC_MSVC) && defined(OM_ENABLE_WARNINGS)
00296 #  pragma warning(default : OM_ENABLE_WARNINGS)
00297 #  undef OM_ENABLE_WARNINGS
00298 #endif
00299 //=============================================================================
00300 #endif // OPENMESH_TOOLS_PROGMESHT_HH defined
00301 //=============================================================================
00302