2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 #ifndef __BSP_TMESH_H__
29 #define __BSP_TMESH_H__
31 #include "MT_Point3.h"
32 #include "MT_Vector3.h"
33 #include "MT_Transform.h"
35 #include "MEM_SmartPtr.h"
39 #include "CSG_BooleanOps.h"
42 * A very basic test mesh.
58 std::vector<BSP_TVertex> m_verts;
59 std::vector<BSP_TFace> m_faces;
61 MT_Vector3 m_min,m_max;
63 std::vector<BSP_TVertex> &
69 std::vector<BSP_TFace> &
81 for (i= 2; i <num_verts; i++) {
83 f.m_verts[0] = verts[0];
84 f.m_verts[1] = verts[i-1];
85 f.m_verts[2] = verts[i];
89 BuildNormal(m_faces.back());
98 m_verts[f.m_verts[1]].m_pos -
99 m_verts[f.m_verts[0]].m_pos;
101 m_verts[f.m_verts[2]].m_pos -
102 m_verts[f.m_verts[1]].m_pos;
104 MT_Vector3 normal = l1.cross(l2);
106 f.m_normal = normal.safe_normalized();
114 * some iterator functions to describe the mesh to the BSP module.
118 * This class defines 2 C style iterators over a CSG mesh, one for
119 * vertices and 1 for faces. They conform to the iterator interface
120 * defined in CSG_BooleanOps.h
133 CSG_VertexIteratorDescriptor * iterator
135 delete ((VertexIt *)(iterator->it));
146 // assume CSG_IteratorPtr is of the correct type.
147 VertexIt * vertex_it = (VertexIt *)it;
149 if (vertex_it->pos < vertex_it->mesh->VertexSet().end()) return 0;
159 // assume CSG_IteratorPtr is of the correct type.
160 VertexIt * vertex_it = (VertexIt *)it;
162 MT_Point3 p = vertex_it->pos->m_pos;
163 p = vertex_it->trans * p;
165 p.getValue(vert->position);
173 // assume CSG_IteratorPtr is of the correct type.
174 VertexIt * vertex_it = (VertexIt *)it;
184 // assume CSG_IteratorPtr is of the correct type.
185 VertexIt * vertex_it = (VertexIt *)it;
187 vertex_it->pos = vertex_it->mesh->VertexSet().begin();
191 CSG_VertexIteratorDescriptor *
196 // user should have insured mesh is not equal to NULL.
198 CSG_VertexIteratorDescriptor * output = new CSG_VertexIteratorDescriptor;
199 if (output == NULL) return NULL;
200 output->Done = VertexIt_Done;
201 output->Fill = VertexIt_Fill;
202 output->Step = VertexIt_Step;
203 output->Reset = VertexIt_Reset;
204 output->num_elements = mesh->VertexSet().size();
206 VertexIt * v_it = new VertexIt;
208 v_it->pos = mesh->VertexSet().begin();
228 CSG_FaceIteratorDescriptor * iterator
230 delete ((FaceIt *)(iterator->it));
241 // assume CSG_IteratorPtr is of the correct type.
242 FaceIt * face_it = (FaceIt *)it;
244 if (face_it->pos < face_it->mesh->FaceSet().end()) {
256 // assume CSG_IteratorPtr is of the correct type.
257 FaceIt * face_it = (FaceIt *)it;
258 // essentially iterating through a triangle fan here.
260 face->vertex_index[0] = int(face_it->pos->m_verts[0]);
261 face->vertex_index[1] = int(face_it->pos->m_verts[1]);
262 face->vertex_index[2] = int(face_it->pos->m_verts[2]);
264 face->vertex_number = 3;
272 // assume CSG_IteratorPtr is of the correct type.
273 FaceIt * face_it = (FaceIt *)it;
283 // assume CSG_IteratorPtr is of the correct type.
284 FaceIt * face_it = (FaceIt *)it;
286 face_it->pos = face_it->mesh->FaceSet().begin();
290 CSG_FaceIteratorDescriptor *
294 CSG_FaceIteratorDescriptor * output = new CSG_FaceIteratorDescriptor;
295 if (output == NULL) return NULL;
297 output->Done = FaceIt_Done;
298 output->Fill = FaceIt_Fill;
299 output->Step = FaceIt_Step;
300 output->Reset = FaceIt_Reset;
302 output->num_elements = mesh->FaceSet().size();
304 FaceIt * f_it = new FaceIt;
306 f_it->pos = mesh->FaceSet().begin();
314 * Some Build functions.
318 MEM_SmartPtr<BSP_TMesh>
320 CSG_MeshPropertyDescriptor &props,
321 CSG_FaceIteratorDescriptor &face_it,
322 CSG_VertexIteratorDescriptor &vertex_it
324 MEM_SmartPtr<BSP_TMesh> mesh = new BSP_TMesh();
328 while (!vertex_it.Done(vertex_it.it)) {
330 vertex_it.Fill(vertex_it.it,&vert);
333 v.m_pos = MT_Point3(vert.position);
334 mesh->VertexSet().push_back(v);
336 vertex_it.Step(vertex_it.it);
342 while (!face_it.Done(face_it.it)) {
343 face_it.Fill(face_it.it,&face);
347 f.m_verts[0] = face.vertex_index[0],
348 f.m_verts[1] = face.vertex_index[1],
349 f.m_verts[2] = face.vertex_index[2],
351 mesh->BuildNormal(f);
353 mesh->FaceSet().push_back(f);
355 face_it.Step(face_it.it);