Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_TRIMESH_MESH_H
00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_TRIMESH_MESH_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/trimesh/trimesh_vertex.h>
00013 #include <OpenTissue/core/containers/mesh/trimesh/trimesh_face.h>
00014 #include <OpenTissue/core/containers/mesh/trimesh/trimesh_core_access.h>
00015
00016 #include <algorithm>
00017
00018 namespace OpenTissue
00019 {
00020 namespace trimesh
00021 {
00022 namespace detail
00023 {
00024
00034 template<
00035 typename M
00036 , typename V
00037 , typename F
00038 , template <typename, typename> class K
00039 >
00040 class TMesh
00041 : public K<
00042 TriMeshVertex< TMesh< M, V, F, K> >
00043 , TriMeshFace< TMesh< M, V, F, K> >
00044 >
00045 {
00046 public:
00047
00048 typedef M math_types;
00049 typedef V vertex_traits;
00050 typedef F face_traits;
00051 typedef TMesh< M, V, F, K > mesh_type;
00052 typedef TriMeshVertex<mesh_type> vertex_type;
00053 typedef TriMeshFace<mesh_type> face_type;
00054 typedef K< vertex_type, face_type > kernel_type;
00055 typedef typename kernel_type::vertex_handle vertex_handle;
00056 typedef typename kernel_type::face_handle face_handle;
00057 typedef typename kernel_type::vertex_iterator vertex_iterator;
00058 typedef typename kernel_type::face_iterator face_iterator;
00059 typedef typename kernel_type::const_vertex_iterator const_vertex_iterator;
00060 typedef typename kernel_type::const_face_iterator const_face_iterator;
00061
00062 protected:
00063
00078 template<typename value_type>
00079 class TriMeshFaceVertexCirculator
00080 {
00081 private:
00082
00083 unsigned int m_idx;
00084 face_type * m_face;
00085 bool m_active;
00086
00087 public:
00088 TriMeshFaceVertexCirculator()
00089 : m_idx(0)
00090 , m_face(0)
00091 , m_active(false)
00092 {}
00093
00094 explicit TriMeshFaceVertexCirculator( face_type const & f)
00095 : m_idx(0)
00096 , m_face(const_cast<face_type*>(&f))
00097 , m_active(false)
00098 {}
00099
00100 template <class OtherValue>
00101 TriMeshFaceVertexCirculator(
00102 TriMeshFaceVertexCirculator<OtherValue> const& other
00103 )
00104 : m_idx(other.m_idx)
00105 , m_face(other.m_face)
00106 , m_active(other.m_active)
00107 {}
00108
00109 public:
00110
00111 template <class OtherValue>
00112 bool operator==(TriMeshFaceVertexCirculator<OtherValue> const& ) const
00113 {
00114 return (m_active && m_idx == 0);
00115 }
00116
00117 template <class OtherValue>
00118 bool operator!=(TriMeshFaceVertexCirculator<OtherValue> const& other) const
00119 {
00120 return !( *this == other);
00121 }
00122
00123 TriMeshFaceVertexCirculator & operator++()
00124 {
00125 m_active = true;
00126 m_idx = (m_idx + 1)%3;
00127 return *this;
00128 }
00129
00130 TriMeshFaceVertexCirculator & operator--()
00131 {
00132 m_active = true;
00133 m_idx = (m_idx - 1)%3;
00134 return *this;
00135 }
00136
00137 public:
00138
00139 value_type & operator*() const
00140 {
00141 if(m_idx==0)
00142 return *(m_face->get_vertex0_iterator());
00143 if(m_idx==1)
00144 return *(m_face->get_vertex1_iterator());
00145
00146 return *(m_face->get_vertex2_iterator());
00147 }
00148
00149 value_type * operator->() const
00150 {
00151 if(m_idx==0)
00152 return &(*(m_face->get_vertex0_iterator()));
00153 if(m_idx==1)
00154 return &(*(m_face->get_vertex1_iterator()));
00155
00156 return &(*(m_face->get_vertex2_iterator()));
00157 }
00158
00159 };
00160
00161 public:
00162
00163 typedef TriMeshFaceVertexCirculator<vertex_type> face_vertex_circulator;
00164 typedef TriMeshFaceVertexCirculator<vertex_type const> const_face_vertex_circulator;
00165
00166 private:
00167
00168 struct assign_owner
00169 {
00170 assign_owner(mesh_type * new_owner)
00171 : m_new_owner(new_owner)
00172 {};
00173
00174 template <typename feature_type>
00175 void operator() (feature_type & f)
00176 {
00177 trimesh_core_access::set_owner( (&f), m_new_owner);
00178 }
00179
00180 mesh_type * m_new_owner;
00181 };
00182
00183 public:
00184
00185 TMesh(){}
00186
00187 ~TMesh() { this->clear(); }
00188
00189 explicit TMesh(TMesh const & m) { (*this) = m; }
00190
00191 TMesh & operator=(TMesh const & mesh)
00192 {
00193 kernel_type::operator=(mesh);
00194
00195
00196 std::for_each( this->vertex_begin(), this->vertex_end(), assign_owner(this) );
00197 std::for_each( this->face_begin(), this->face_end(), assign_owner(this) );
00198
00199 return *this;
00200 }
00201
00202 public:
00203
00204 vertex_handle add_vertex()
00205 {
00206 vertex_handle v = this->create_vertex();
00207 vertex_iterator vit = get_vertex_iterator(v);
00208 trimesh_core_access::set_owner(vit,this);
00209 return v;
00210 };
00211
00212 template<typename vector3_type>
00213 vertex_handle add_vertex(vector3_type const & coord)
00214 {
00215 vertex_handle v = add_vertex();
00216 get_vertex_iterator(v)->m_coord = coord;
00217 return v;
00218 };
00219
00220 face_handle add_face(vertex_handle const & v0,vertex_handle const & v1,vertex_handle const & v2)
00221 {
00222 if(! is_valid_vertex_handle(v0) )
00223 return this->null_face_handle();
00224 if(! is_valid_vertex_handle(v1) )
00225 return this->null_face_handle();
00226 if(! is_valid_vertex_handle(v2) )
00227 return this->null_face_handle();
00228 if(v0==v1)
00229 return this->null_face_handle();
00230 if(v0==v2)
00231 return this->null_face_handle();
00232 if(v1==v2)
00233 return this->null_face_handle();
00234
00235
00236 face_handle f = this->create_face();
00237 face_iterator fit = get_face_iterator(f);
00238 trimesh_core_access::set_owner(fit,this);
00239
00240 trimesh_core_access::set_vertex0_handle(fit,v0);
00241 trimesh_core_access::set_vertex1_handle(fit,v1);
00242 trimesh_core_access::set_vertex2_handle(fit,v2);
00243
00244 trimesh_core_access::increment_face_counter(fit->get_vertex0_iterator());
00245 trimesh_core_access::increment_face_counter(fit->get_vertex1_iterator());
00246 trimesh_core_access::increment_face_counter(fit->get_vertex2_iterator());
00247
00248 return f;
00249 };
00250
00251 template<typename vertex_handle_iterator>
00252 face_handle add_face(vertex_handle_iterator begin,vertex_handle_iterator end)
00253 {
00254 face_handle face;
00255 vertex_handle_iterator v0 = begin;
00256 vertex_handle_iterator v1 = v0; ++v1;
00257 vertex_handle_iterator v2 = v1; ++v2;
00258
00259
00260
00261
00262 while(v2!=end)
00263 {
00264 face = add_face( *v0, *v1, *v2 );
00265 v1 = v2;
00266 ++v2;
00267 }
00268
00269
00270 return face;
00271 }
00272
00273 bool remove_vertex(vertex_handle const & v)
00274 {
00275 if(!is_valid_vertex_handle(v))
00276 {
00277 assert(!"TMesh::remove_vertex(...): Invalid vertex handle");
00278 return false;
00279 }
00280 return remove_vertex( get_vertex_iterator(v) );
00281 };
00282
00283 bool remove_vertex(vertex_iterator v)
00284 {
00285 if(v->get_face_count()>0)
00286 {
00287 assert(!"TMesh::remove_vertex(...): Could not remove vertex because it is used by a face");
00288 return false;
00289 }
00290 erase_vertex(v->get_handle());
00291 return true;
00292 };
00293
00294 bool remove_face(face_handle const & f)
00295 {
00296 if(!is_valid_face_handle(f))
00297 return false;
00298 return remove_face( get_face_iterator(f) );
00299 };
00300
00301 bool remove_face(face_iterator f)
00302 {
00303
00304 trimesh_core_access::decrement_face_counter(f->get_vertex0_iterator());
00305 trimesh_core_access::decrement_face_counter(f->get_vertex1_iterator());
00306 trimesh_core_access::decrement_face_counter(f->get_vertex2_iterator());
00307
00308 erase_face(f->get_handle());
00309 return true;
00310 };
00311
00312 };
00313
00314 }
00315 }
00316 }
00317
00318
00319 #endif