OpenMesh
OpenMesh/Tools/Subdivider/Adaptive/Composite/CompositeT.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 CompositeT
00049 //
00050 //=============================================================================
00051 
00052 #ifndef OPENMESH_SUBDIVIDER_ADAPTIVE_COMPOSITET_HH
00053 #define OPENMESH_SUBDIVIDER_ADAPTIVE_COMPOSITET_HH
00054 
00055 
00056 //== INCLUDES =================================================================
00057 
00058 #include <OpenMesh/Core/System/config.hh>
00059 #include <OpenMesh/Tools/Subdivider/Adaptive/Composite/CompositeTraits.hh>
00060 // --------------------
00061 #include <vector>
00062 #include <memory>
00063 #include <string>
00064 
00065 
00066 //== NAMESPACE ================================================================
00067 
00068 namespace OpenMesh   { // BEGIN_NS_OPENMESH
00069 namespace Subdivider { // BEGIN_NS_SUBDIVIDER
00070 namespace Adaptive   { // BEGIN_NS_ADAPTIVE
00071 
00072 
00073 //== CLASS DEFINITION =========================================================
00074 
00075 
00076 template <typename R> struct RuleHandleT;
00077 template <typename M> class  RuleInterfaceT;
00078 
00079 
00080 //== CLASS DEFINITION =========================================================
00081 
00131 template <typename M> class CompositeT
00132 {
00133 public:
00134 
00135   typedef RuleInterfaceT<M>  Rule;
00136   typedef M                  Mesh;
00137   typedef std::vector<Rule*> RuleSequence;
00138 
00139   typedef typename M::VertexHandle   VH;
00140   typedef typename M::FaceHandle     FH;
00141   typedef typename M::EdgeHandle     EH;
00142   typedef typename M::HalfedgeHandle HH;
00143 
00144 public:
00145 
00147   CompositeT(Mesh& _mesh) 
00148     : subdiv_type_(0), 
00149       subdiv_rule_(NULL), /*first_rule_(NULL), last_rule_(NULL),*/ mesh_(_mesh)
00150   { }
00151 
00153   virtual ~CompositeT() 
00154   { cleanup(); }
00155 
00156 
00159   void cleanup(void)
00160   {
00161     subdiv_type_ = 0;
00162     subdiv_rule_ = NULL;
00163 
00164     std::for_each(rule_sequence_.begin(), 
00165                   rule_sequence_.end(), DeleteRule() );
00166     rule_sequence_.clear();
00167   }
00168 
00169 
00171   bool initialize(void);
00172 
00173   
00175   void refine(typename Mesh::FaceHandle& _fh);
00176 
00177 
00179   void refine(typename Mesh::VertexHandle& _vh);
00180 
00181 
00183   int subdiv_type() { return subdiv_type_; }
00184 
00185 
00186   // Return subdivision rule.
00187   const Rule& subdiv_rule() const { return *subdiv_rule_; }
00188 
00189 public:
00190 
00192   //*@
00193 
00198   template < typename R >
00199   RuleHandleT<R> add()
00200   {
00201     size_t idx = rule_sequence_.size();
00202     rule_sequence_.push_back( new R( mesh_ ) );
00203     return RuleHandleT<R>( (idx < rule_sequence_.size()) ? idx : -1 );
00204   }
00205 
00210   template < typename R >
00211   RuleHandleT<R>& add( RuleHandleT<R>& _rh )
00212   {
00213     return _rh = add< R >();
00214   }
00215 
00221   template < typename R >
00222   typename RuleHandleT<R>::Rule& rule( const RuleHandleT<R>& _rh )
00223   {
00224     typedef typename RuleHandleT<R>::Rule rule_t;
00225     assert( _rh.is_valid() );
00226     return *dynamic_cast<rule_t*>(rule_sequence_[ _rh.idx() ]);
00227   }
00228 
00229 
00235   RuleInterfaceT<M>& rule( size_t _idx )
00236   {
00237     assert( _idx < n_rules() );
00238     return *rule_sequence_[ _idx ];
00239   }
00240 
00242   size_t n_rules() const { return rule_sequence_.size(); }
00243 
00245   std::string rules_as_string(const std::string& _sep= " * ") const;
00246 
00248 
00249 protected:
00250 
00252   const RuleSequence& rules() const { return rule_sequence_; }
00253 
00254 protected: // helper
00255 
00256   // get current generation from state
00257   state_t generation(state_t _s) { return _s-(_s % n_rules()); }
00258   state_t generation( VH _vh ) { return generation(mesh_.data(_vh).state()); }
00259   state_t generation( EH _eh ) { return generation(mesh_.data(_eh).state()); }
00260   state_t generation( FH _fh ) { return generation(mesh_.data(_fh).state()); }
00261 
00262 private:
00263 
00264   // short cuts
00265   Rule* t_rule() { return subdiv_rule_;           }
00266   Rule* f_rule() { return rule_sequence_.front(); }
00267   Rule* l_rule() { return rule_sequence_.back();  }
00268 
00269 private:
00270 
00271   // 
00272   RuleSequence rule_sequence_;
00273 
00274   // Split type
00275   int   subdiv_type_;
00276 
00277   Rule  *subdiv_rule_;
00278 //   Rule  *first_rule_; 
00279 //   Rule  *last_rule_;
00280 
00281   //
00282   Mesh  &mesh_;
00283 
00284 private: // helper
00285 
00286 #ifndef DOXY_IGNORE_THIS
00287   struct DeleteRule { void operator()( Rule* _r ) { delete _r; } };
00288 #endif
00289 
00290 private:
00291 
00292   CompositeT( const CompositeT& );
00293   CompositeT& operator = ( const CompositeT );
00294 
00295 };
00296 
00297    
00298 //=============================================================================
00299 } // END_NS_ADAPTIVE
00300 } // END_NS_SUBDIVIDER
00301 } // END_NS_OPENMESH
00302 //=============================================================================
00303 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_SUBDIVIDER_ADAPTIVE_COMPOSITET_CC)
00304 #  define OPENMESH_SUBDIVIDER_TEMPLATES
00305 #  include "CompositeT.cc"
00306 #endif
00307 //=============================================================================
00308 #endif // OPENMESH_SUBDIVIDER_ADAPTIVE_COMPOSITET_HH defined
00309 //=============================================================================