00001 #ifndef OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_KERNELS_TRIMESH_ARRAY_KERNEL_H 00002 #define OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_KERNELS_TRIMESH_ARRAY_KERNEL_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 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 00015 #include <list> 00016 #include <vector> 00017 #include <cassert> 00018 00019 namespace OpenTissue 00020 { 00021 namespace trimesh 00022 { 00023 00027 template< typename vertex_type_, typename face_type_ > 00028 class TriMeshArrayKernel 00029 { 00030 public: 00031 00032 typedef vertex_type_ vertex_type; 00033 typedef face_type_ face_type; 00034 typedef TriMeshArrayKernel<vertex_type, face_type> kernel_type; 00035 00036 public: 00037 00038 typedef std::size_t index_type; 00039 typedef typename std::vector<vertex_type>::size_type size_type; 00040 typedef typename std::vector<vertex_type>::iterator vertex_iterator; 00041 typedef typename std::vector<face_type>::iterator face_iterator; 00042 typedef typename std::vector<vertex_type>::const_iterator const_vertex_iterator; 00043 typedef typename std::vector<face_type>::const_iterator const_face_iterator; 00044 00045 private: 00046 00053 class Handle 00054 { 00055 protected: 00056 00057 index_type m_idx; 00058 00059 public: 00060 00061 Handle() 00062 : m_idx(~0u) 00063 {} 00064 00065 explicit Handle(index_type idx) 00066 : m_idx(idx) 00067 {} 00068 00069 Handle(Handle const & h) 00070 : m_idx(h.m_idx) 00071 {} 00072 00073 Handle & operator=(Handle const & h){ m_idx = h.m_idx; return *this; } 00074 bool operator<(Handle const & h)const { return m_idx < h.m_idx;} 00075 bool operator==(Handle const & h) const {return (h.m_idx==m_idx);} 00076 bool operator!=(Handle const & h) const {return !((*this)==h);} 00077 index_type get_idx(void) const { return m_idx; } 00078 bool is_null() const { return (m_idx == ~0u); } 00079 }; 00080 00081 public: 00082 00083 class vertex_handle : public Handle 00084 { 00085 public: 00086 vertex_handle():Handle(){} 00087 vertex_handle(index_type idx):Handle(idx){} 00088 vertex_handle(vertex_handle const & v):Handle(v){} 00089 }; 00090 00091 class face_handle : public Handle 00092 { 00093 public: 00094 face_handle():Handle(){} 00095 face_handle(index_type idx):Handle(idx){} 00096 face_handle(face_handle const & f):Handle(f){} 00097 }; 00098 00099 public: 00100 00101 static vertex_handle const & null_vertex_handle() 00102 { 00103 static vertex_handle h; 00104 return h; 00105 } 00106 static face_handle const & null_face_handle() 00107 { 00108 static face_handle h; 00109 return h; 00110 } 00111 00112 private: 00113 00114 std::vector<vertex_type> m_vertices; 00115 std::vector<face_type> m_faces; 00116 std::vector<bool> m_used_vertex; 00117 std::vector<bool> m_used_face; 00118 00119 public: 00120 00121 vertex_iterator vertex_begin() { return m_vertices.begin(); } 00122 vertex_iterator vertex_end() { return m_vertices.end(); } 00123 face_iterator face_begin() { return m_faces.begin(); } 00124 face_iterator face_end() { return m_faces.end(); } 00125 00126 const_vertex_iterator vertex_begin() const { return m_vertices.begin(); } 00127 const_vertex_iterator vertex_end() const { return m_vertices.end(); } 00128 const_face_iterator face_begin() const { return m_faces.begin(); } 00129 const_face_iterator face_end() const { return m_faces.end(); } 00130 00131 size_type size_faces() const { return m_faces.size(); } 00132 size_type size_vertices() const { return m_vertices.size(); } 00133 00134 public: 00135 00136 TriMeshArrayKernel() 00137 {} 00138 00139 explicit TriMeshArrayKernel(TriMeshArrayKernel const & other_kernel) 00140 { *this = other_kernel; } 00141 00142 public: 00143 00144 TriMeshArrayKernel & operator=(TriMeshArrayKernel const & rhs) 00145 { 00146 m_vertices = rhs.m_vertices; 00147 m_faces = rhs.m_faces; 00148 m_used_vertex = rhs.m_used_vertex; 00149 m_used_face = rhs.m_used_face; 00150 return (*this); 00151 } 00152 00153 protected: 00154 00155 vertex_handle create_vertex() 00156 { 00157 m_vertices.push_back(vertex_type()); 00158 vertex_iterator last = m_vertices.end(); 00159 --last; 00160 index_type new_idx = m_used_vertex.size(); 00161 m_used_vertex.push_back(true); 00162 vertex_handle h(new_idx); 00163 trimesh_core_access::set_self_handle( last, h); 00164 return h; 00165 } 00166 00167 face_handle create_face() 00168 { 00169 m_faces.push_back(face_type()); 00170 face_iterator last = m_faces.end(); 00171 --last; 00172 index_type new_idx = m_used_face.size(); 00173 m_used_face.push_back(true); 00174 face_handle h(new_idx); 00175 trimesh_core_access::set_self_handle( last, h); 00176 return h; 00177 } 00178 00179 void erase_vertex(vertex_handle const & v) 00180 { 00181 if(is_valid_vertex_handle(v)) 00182 m_used_vertex[v.get_idx()] = false; 00183 } 00184 00185 void erase_face(face_handle const & f) 00186 { 00187 if(is_valid_face_handle(f)) 00188 m_used_face[f.get_idx()] = false; 00189 } 00190 00191 public: 00192 00193 vertex_handle get_vertex_handle(index_type idx) const 00194 { 00195 assert(idx>=0); 00196 assert(idx<m_used_vertex.size()); 00197 if(m_used_vertex[idx]) 00198 return vertex_handle(idx); 00199 return null_vertex_handle(); 00200 }; 00201 00202 face_handle get_face_handle(index_type idx) const 00203 { 00204 assert(idx>=0); 00205 assert(idx<m_used_face.size()); 00206 if(m_used_face[idx]) 00207 return face_handle(idx); 00208 return null_face_handle(); 00209 } 00210 00211 00212 vertex_iterator get_vertex_iterator(vertex_handle const & v) 00213 { 00214 if( ! is_valid_vertex_handle(v) ) 00215 return vertex_end(); 00216 return (m_vertices.begin() + v.get_idx()); 00217 } 00218 00219 face_iterator get_face_iterator(face_handle const & f) 00220 { 00221 if( ! is_valid_face_handle(f) ) 00222 return face_end(); 00223 return m_faces.begin() + f.get_idx(); 00224 }; 00225 00226 const_vertex_iterator get_vertex_iterator(vertex_handle const & v) const 00227 { 00228 if( ! is_valid_vertex_handle(v) ) 00229 return vertex_end(); 00230 return (m_vertices.begin() + v.get_idx()); 00231 } 00232 00233 const_face_iterator get_face_iterator(face_handle const & f) const 00234 { 00235 if( ! is_valid_face_handle(f) ) 00236 return face_end(); 00237 return m_faces.begin() + f.get_idx(); 00238 } 00239 00240 void clear() 00241 { 00242 m_vertices.clear(); 00243 m_faces.clear(); 00244 m_used_vertex.clear(); 00245 m_used_face.clear(); 00246 } 00247 00248 public: 00249 00250 bool is_valid_vertex_handle(vertex_handle const & v) const 00251 { 00252 if(v.is_null()) 00253 return false; 00254 assert(v.get_idx()>=0); 00255 assert(v.get_idx()<m_used_vertex.size()); 00256 return m_used_vertex[v.get_idx()]; 00257 } 00258 00259 bool is_valid_face_handle(face_handle const & f) const 00260 { 00261 if(f.is_null()) 00262 return false; 00263 assert(f.get_idx()>=0); 00264 assert(f.get_idx()<m_used_face.size()); 00265 return m_used_face[f.get_idx()]; 00266 } 00267 00268 public: 00269 00270 void pack() 00271 { 00272 assert(false || !"Sorry not implemented yet"); 00273 } 00274 00275 }; 00276 00277 } // namespace trimesh 00278 } // namespace OpenTissue 00279 00280 //OPENTISSUE_CORE_CONTAINERS_MESH_TRIMESH_KERNELS_TRIMESH_ARRAY_KERNEL2_H 00281 #endif