Blender V2.61 - r43446
|
00001 00028 #ifndef BSP_TMesh_h 00029 #define BSP_TMesh_h 00030 00031 #include "MT_Point3.h" 00032 #include "MT_Vector3.h" 00033 #include "MT_Transform.h" 00034 00035 #include "MEM_SmartPtr.h" 00036 00037 #include <vector> 00038 00039 #include "CSG_BooleanOps.h" 00040 00045 struct BSP_TVertex { 00046 MT_Point3 m_pos; 00047 }; 00048 00049 struct BSP_TFace { 00050 int m_verts[3]; 00051 MT_Vector3 m_normal; 00052 }; 00053 00054 00055 class BSP_TMesh { 00056 public : 00057 00058 std::vector<BSP_TVertex> m_verts; 00059 std::vector<BSP_TFace> m_faces; 00060 00061 MT_Vector3 m_min,m_max; 00062 00063 std::vector<BSP_TVertex> & 00064 VertexSet( 00065 ){ 00066 return m_verts; 00067 } 00068 00069 std::vector<BSP_TFace> & 00070 FaceSet( 00071 ) { 00072 return m_faces; 00073 } 00074 00075 void 00076 AddFace( 00077 int *verts, 00078 int num_verts 00079 ){ 00080 int i; 00081 for (i= 2; i <num_verts; i++) { 00082 BSP_TFace f; 00083 f.m_verts[0] = verts[0]; 00084 f.m_verts[1] = verts[i-1]; 00085 f.m_verts[2] = verts[i]; 00086 00087 m_faces.push_back(f); 00088 00089 BuildNormal(m_faces.back()); 00090 } 00091 } 00092 00093 void 00094 BuildNormal( 00095 BSP_TFace & f 00096 ) const { 00097 MT_Vector3 l1 = 00098 m_verts[f.m_verts[1]].m_pos - 00099 m_verts[f.m_verts[0]].m_pos; 00100 MT_Vector3 l2 = 00101 m_verts[f.m_verts[2]].m_pos - 00102 m_verts[f.m_verts[1]].m_pos; 00103 00104 MT_Vector3 normal = l1.cross(l2); 00105 00106 f.m_normal = normal.safe_normalized(); 00107 } 00108 00109 }; 00110 00111 00112 00123 struct VertexIt { 00124 BSP_TMesh * mesh; 00125 BSP_TVertex * pos; 00126 MT_Transform trans; 00127 }; 00128 00129 00130 static 00131 void 00132 VertexIt_Destruct( 00133 CSG_VertexIteratorDescriptor * iterator 00134 ) { 00135 delete ((VertexIt *)(iterator->it)); 00136 iterator->it = NULL; 00137 delete(iterator); 00138 }; 00139 00140 00141 static 00142 int 00143 VertexIt_Done( 00144 CSG_IteratorPtr it 00145 ) { 00146 // assume CSG_IteratorPtr is of the correct type. 00147 VertexIt * vertex_it = (VertexIt *)it; 00148 00149 if (vertex_it->pos < vertex_it->mesh->VertexSet().end()) return 0; 00150 return 1; 00151 }; 00152 00153 static 00154 void 00155 VertexIt_Fill( 00156 CSG_IteratorPtr it, 00157 CSG_IVertex *vert 00158 ) { 00159 // assume CSG_IteratorPtr is of the correct type. 00160 VertexIt * vertex_it = (VertexIt *)it; 00161 00162 MT_Point3 p = vertex_it->pos->m_pos; 00163 p = vertex_it->trans * p; 00164 00165 p.getValue(vert->position); 00166 }; 00167 00168 static 00169 void 00170 VertexIt_Step( 00171 CSG_IteratorPtr it 00172 ) { 00173 // assume CSG_IteratorPtr is of the correct type. 00174 VertexIt * vertex_it = (VertexIt *)it; 00175 00176 ++(vertex_it->pos); 00177 }; 00178 00179 static 00180 void 00181 VertexIt_Reset( 00182 CSG_IteratorPtr it 00183 ) { 00184 // assume CSG_IteratorPtr is of the correct type. 00185 VertexIt * vertex_it = (VertexIt *)it; 00186 00187 vertex_it->pos = vertex_it->mesh->VertexSet().begin(); 00188 }; 00189 00190 static 00191 CSG_VertexIteratorDescriptor * 00192 VertexIt_Construct( 00193 BSP_TMesh *mesh, 00194 MT_Transform trans 00195 ){ 00196 // user should have insured mesh is not equal to NULL. 00197 00198 CSG_VertexIteratorDescriptor * output = new CSG_VertexIteratorDescriptor; 00199 if (output == NULL) return NULL; 00200 output->Done = VertexIt_Done; 00201 output->Fill = VertexIt_Fill; 00202 output->Step = VertexIt_Step; 00203 output->Reset = VertexIt_Reset; 00204 output->num_elements = mesh->VertexSet().size(); 00205 00206 VertexIt * v_it = new VertexIt; 00207 v_it->mesh = mesh; 00208 v_it->pos = mesh->VertexSet().begin(); 00209 v_it->trans = trans; 00210 output->it = v_it; 00211 return output; 00212 }; 00213 00214 00219 struct FaceIt { 00220 BSP_TMesh * mesh; 00221 BSP_TFace *pos; 00222 }; 00223 00224 00225 static 00226 void 00227 FaceIt_Destruct( 00228 CSG_FaceIteratorDescriptor * iterator 00229 ) { 00230 delete ((FaceIt *)(iterator->it)); 00231 iterator->it = NULL; 00232 delete(iterator); 00233 }; 00234 00235 00236 static 00237 int 00238 FaceIt_Done( 00239 CSG_IteratorPtr it 00240 ) { 00241 // assume CSG_IteratorPtr is of the correct type. 00242 FaceIt * face_it = (FaceIt *)it; 00243 00244 if (face_it->pos < face_it->mesh->FaceSet().end()) { 00245 return 0; 00246 } 00247 return 1; 00248 }; 00249 00250 static 00251 void 00252 FaceIt_Fill( 00253 CSG_IteratorPtr it, 00254 CSG_IFace *face 00255 ){ 00256 // assume CSG_IteratorPtr is of the correct type. 00257 FaceIt * face_it = (FaceIt *)it; 00258 // essentially iterating through a triangle fan here. 00259 00260 face->vertex_index[0] = int(face_it->pos->m_verts[0]); 00261 face->vertex_index[1] = int(face_it->pos->m_verts[1]); 00262 face->vertex_index[2] = int(face_it->pos->m_verts[2]); 00263 00264 face->vertex_number = 3; 00265 }; 00266 00267 static 00268 void 00269 FaceIt_Step( 00270 CSG_IteratorPtr it 00271 ) { 00272 // assume CSG_IteratorPtr is of the correct type. 00273 FaceIt * face_it = (FaceIt *)it; 00274 00275 face_it->pos ++; 00276 }; 00277 00278 static 00279 void 00280 FaceIt_Reset( 00281 CSG_IteratorPtr it 00282 ) { 00283 // assume CSG_IteratorPtr is of the correct type. 00284 FaceIt * face_it = (FaceIt *)it; 00285 00286 face_it->pos = face_it->mesh->FaceSet().begin(); 00287 }; 00288 00289 static 00290 CSG_FaceIteratorDescriptor * 00291 FaceIt_Construct( 00292 BSP_TMesh * mesh 00293 ) { 00294 CSG_FaceIteratorDescriptor * output = new CSG_FaceIteratorDescriptor; 00295 if (output == NULL) return NULL; 00296 00297 output->Done = FaceIt_Done; 00298 output->Fill = FaceIt_Fill; 00299 output->Step = FaceIt_Step; 00300 output->Reset = FaceIt_Reset; 00301 00302 output->num_elements = mesh->FaceSet().size(); 00303 00304 FaceIt * f_it = new FaceIt; 00305 f_it->mesh = mesh; 00306 f_it->pos = mesh->FaceSet().begin(); 00307 00308 output->it = f_it; 00309 00310 return output; 00311 }; 00312 00317 static 00318 MEM_SmartPtr<BSP_TMesh> 00319 BuildMesh( 00320 CSG_MeshPropertyDescriptor &props, 00321 CSG_FaceIteratorDescriptor &face_it, 00322 CSG_VertexIteratorDescriptor &vertex_it 00323 ) { 00324 MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh(); 00325 00326 CSG_IVertex vert; 00327 00328 while (!vertex_it.Done(vertex_it.it)) { 00329 00330 vertex_it.Fill(vertex_it.it,&vert); 00331 00332 BSP_TVertex v; 00333 v.m_pos = MT_Point3(vert.position); 00334 mesh->VertexSet().push_back(v); 00335 00336 vertex_it.Step(vertex_it.it); 00337 } 00338 00339 00340 CSG_IFace face; 00341 00342 while (!face_it.Done(face_it.it)) { 00343 face_it.Fill(face_it.it,&face); 00344 00345 BSP_TFace f; 00346 00347 f.m_verts[0] = face.vertex_index[0], 00348 f.m_verts[1] = face.vertex_index[1], 00349 f.m_verts[2] = face.vertex_index[2], 00350 00351 mesh->BuildNormal(f); 00352 00353 mesh->FaceSet().push_back(f); 00354 00355 face_it.Step(face_it.it); 00356 } 00357 00358 return mesh; 00359 }; 00360 00361 00362 00363 00364 00365 00366 00367 00368 00369 00370 00371 00372 00373 00374 00375 00376 00377 00378 00379 00380 00381 00382 00383 00384 00385 00386 00387 00388 00389 00390 00391 00392 00393 00394 00395 00396 #endif 00397