OpenMesh
OpenMesh/Apps/Decimating/DecimaterViewerWidget.hh
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 
00042 
00043 #ifndef OPENMESHAPPS_DECIMATERVIEWERWIDGET_HH
00044 #define OPENMESHAPPS_DECIMATERVIEWERWIDGET_HH
00045 
00046 
00047 //== INCLUDES =================================================================
00048 
00049 #if !defined(OM_USE_OSG)
00050 #  define OM_USE_OSG 0
00051 #endif
00052 
00053 
00054 #include <qtimer.h>
00055 #include <string>
00056 #include <memory>
00057 
00058 //--------------------
00059 #include <OpenMesh/Core/IO/MeshIO.hh>
00060 //--------------------
00061 #if OM_USE_OSG
00062 #  include <OpenMesh/Tools/Kernel_OSG/TriMesh_OSGArrayKernelT.hh>
00063 #  define DEFAULT_TRAITS Kernel_OSG::Traits
00064 #  define TRIMESH_KERNEL Kernel_OSG::TriMesh_OSGArrayKernelT
00065 #else
00066 //--------------------
00067 #  include <OpenMesh/Core/Mesh/Traits.hh>
00068 #  include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
00069 #  define DEFAULT_TRAITS DefaultTraits
00070 #  define TRIMESH_KERNEL TriMesh_ArrayKernelT
00071 #endif
00072 
00073 #include <OpenMesh/Apps/QtViewer/MeshViewerWidgetT.hh>
00074 
00075 #include <OpenMesh/Tools/Decimater/DecimaterT.hh>
00076 #include <OpenMesh/Tools/Decimater/ModNormalFlippingT.hh>
00077 #include <OpenMesh/Tools/Decimater/ModQuadricT.hh>
00078 
00079 
00080 //== CLASS DEFINITION =========================================================
00081 
00082 
00083 using namespace OpenMesh;
00084 
00085 struct MyTraits : public DEFAULT_TRAITS
00086 {
00087   VertexAttributes  ( Attributes::Normal       );
00088   FaceAttributes    ( Attributes::Normal       );  
00089 };
00090 
00091 typedef TRIMESH_KERNEL<MyTraits>                     mesh_t;
00092 typedef MeshViewerWidgetT<mesh_t>                    MeshViewerWidget;
00093 
00094 //== CLASS DEFINITION =========================================================
00095 
00096 
00097 class DecimaterViewerWidget : public MeshViewerWidget
00098 {
00099   Q_OBJECT
00100    
00101 public:
00102   
00103   typedef MeshViewerWidget inherited_t;
00104 
00105   typedef Decimater::DecimaterT<mesh_t>                decimater_t;  
00106   typedef Decimater::ModQuadricT< decimater_t >        mod_quadric_t;
00107   typedef Decimater::ModNormalFlippingT< decimater_t > mod_nf_t;
00108 
00109   // object types
00110   typedef std::auto_ptr< decimater_t   >   decimater_o;
00111   typedef std::auto_ptr< mod_quadric_t >   mod_quadric_o;
00112   typedef std::auto_ptr< mod_nf_t      >   mod_nf_o;
00113 
00115   DecimaterViewerWidget(QWidget* _parent=0)
00116     : MeshViewerWidget(_parent), steps_(1)
00117   {
00118     timer_ = new QTimer(this);
00119     
00120     connect( timer_, SIGNAL(timeout()), SLOT(animate()) );
00121   }
00122   
00124   ~DecimaterViewerWidget()
00125   {
00126     delete timer_;
00127   }
00128 
00129 public: // inherited
00130 
00131   bool open_mesh(const char* _filename, OpenMesh::IO::Options _opt)
00132   {
00133     bool rc;
00134 
00135     if ( (rc = inherited_t::open_mesh( _filename, _opt )) )
00136     {
00137       std::cout << "prepare decimater" << std::endl;
00138 
00139       // to be compatible with gcc 2.95.3
00140       {
00141         decimater_o  tmp( new decimater_t  ( mesh() ) );
00142         decimater_ = tmp;
00143       }
00144       {
00145         mod_quadric_o  tmp( new mod_quadric_t( *decimater_ ) );
00146         mod_quadric_ = tmp;
00147       }
00148       {
00149         mod_nf_o       tmp( new mod_nf_t     ( *decimater_ ) );
00150         mod_nf_      = tmp;
00151       }
00152 
00153       decimater_->initialize();
00154     }
00155     return rc;
00156   }
00157   
00158 protected slots:
00159 
00160   void animate( void );
00161 
00162 protected:   
00163 
00164   virtual void keyPressEvent(QKeyEvent* _event);  
00165 
00166 
00167 private:
00168   
00169   bool              animate_;
00170   QTimer           *timer_;
00171 
00172   decimater_o       decimater_;  
00173   mod_quadric_o     mod_quadric_;
00174   mod_nf_o          mod_nf_;
00175 
00176   size_t            steps_;
00177 };
00178 
00179 
00180 //=============================================================================
00181 #endif // OPENMESHAPPS_DECIMATERVIEWERWIDGET_HH defined
00182 //=============================================================================
00183