Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/objMeshOctree.cpp

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 // Jernej Barbic, CMU
30 // builds an octree on top of the geometry from a given obj file
31 // Can be intersected with a sphere or a line segment.
32
33 #include "triple.h"
34 #include "objMeshOctree.h"
35 using namespace std;
36
37 template<class TriangleClass>
38 const double ObjMeshOctree<TriangleClass>::bboxExpansionRatio = 1.05;
39
40 template<class TriangleClass>
41 ObjMeshOctree<TriangleClass>::ObjMeshOctree(const ObjMesh * objMeshIn, int maxNumTrianglesInLeafNode_, int maxTreeDepth_, int printInfo) :
42 maxNumTrianglesInLeafNode(maxNumTrianglesInLeafNode_), maxTreeDepth(maxTreeDepth_)
43 {
44 // copy mesh
45 ObjMesh * objMesh = new ObjMesh(*objMeshIn);
46
47 cout << "Checking if mesh is triangular... ";
48 if (!objMesh->isTriangularMesh())
49 {
50 cout << "mesh was not triangular: triangulating... ";
51 objMesh->triangulate();
52 cout << "done" << endl;
53 }
54 else
55 cout << "yes" << endl;
56
57 int triangleIndex = 0;
58 triangles.clear();
59 for(unsigned int i=0; i < objMesh->getNumGroups(); i++) // over all groups
60 for (unsigned int j=0; j < (objMesh->getGroupHandle(i))->getNumFaces(); j++) // over all faces
61 {
62 Vec3d p0 = objMesh->getPosition(objMesh->getVertexIndex(i,j,0));
63 Vec3d p1 = objMesh->getPosition(objMesh->getVertexIndex(i,j,1));
64 Vec3d p2 = objMesh->getPosition(objMesh->getVertexIndex(i,j,2));
65 TriangleClass triangle(p0,p1,p2);
66 triangle.setIndex(triangleIndex); // 0-indexed
67 triangleIndex++;
68 triangles.push_back(triangle);
69 }
70
71 cout << "Total number of triangles is: " << triangles.size() << endl;
72
73 // build the octree
74 cout << "Building the octree data structure... " << endl;
75 root = new Octree<TriangleClass>(maxTreeDepth);
76 root->setBuildPrintInfo(printInfo);
77 root->build(triangles, maxNumTrianglesInLeafNode);
78
79 if(printInfo == 1)
80 {
81 int numMaxDepthExceededCases;
82 int numMaxTriInDepthExceededCases;
83 root->getBuildInfo(&numMaxDepthExceededCases, &numMaxTriInDepthExceededCases);
84 printf("Total number of cells with more than %d triangles: %d. Max triangles in such cells: %d.\n", maxNumTrianglesInLeafNode, numMaxDepthExceededCases, numMaxTriInDepthExceededCases);
85 }
86
87 triangles.clear(); // release memory
88 delete(objMesh);
89 }
90
91 template<>
92 ObjMeshOctree<TriangleWithCollisionInfoAndPseudoNormals>::ObjMeshOctree(const ObjMesh * objMeshIn, int maxNumTrianglesInLeafNode_, int maxTreeDepth_, int printInfo) :
93 maxNumTrianglesInLeafNode(maxNumTrianglesInLeafNode_), maxTreeDepth(maxTreeDepth_)
94 {
95 // copy mesh
96 ObjMesh * objMesh = new ObjMesh(*objMeshIn);
97
98 cout << "Checking if mesh is triangular... ";
99 if (!objMesh->isTriangularMesh())
100 {
101 cout << "mesh was not triangular: triangulating... ";
102 objMesh->triangulate();
103 cout << "done" << endl;
104 }
105 else
106 cout << "yes" << endl;
107
108 ObjMesh * pseudoNormalObjMesh = objMesh;
109 int numZeroAreaFaces = pseudoNormalObjMesh->removeZeroAreaFaces();
110 cout << "Encountered and removed " << numZeroAreaFaces << " faces with zero surface area." << endl;
111
112 triangles.clear();
113
114 pseudoNormalObjMesh->computePseudoNormals();
115 pseudoNormalObjMesh->computeEdgePseudoNormals();
116
117 // compute face pseudonormals
118 Vec3d pseudoNormals[7];
119 for(unsigned int i=0; i < pseudoNormalObjMesh->getNumGroups(); i++) // over all groups
120 {
121 for (unsigned int j=0; j < (pseudoNormalObjMesh->getGroupHandle(i))->getNumFaces(); j++) // over all faces
122 {
123 // vertices
124 unsigned int index0 = pseudoNormalObjMesh->getVertexIndex(i, j, 0);
125 unsigned int index1 = pseudoNormalObjMesh->getVertexIndex(i, j, 1);
126 unsigned int index2 = pseudoNormalObjMesh->getVertexIndex(i, j, 2);
127
128 pseudoNormals[0] = pseudoNormalObjMesh->getPseudoNormal(index0);
129 pseudoNormals[1] = pseudoNormalObjMesh->getPseudoNormal(index1);
130 pseudoNormals[2] = pseudoNormalObjMesh->getPseudoNormal(index2);
131
132 // edge pseudo normal
133 if (pseudoNormalObjMesh->getEdgePseudoNormal(index0, index1, &pseudoNormals[3]) != 0)
134 {
135 cout << "Error: encountered an edge without a pseudonormal. Degenerate face? Vertices: " << index0 << " " << index1 << endl;
136 delete(objMesh);
137 throw(1);
138 }
139 if (pseudoNormalObjMesh->getEdgePseudoNormal(index1, index2, &pseudoNormals[4]) != 0)
140 {
141 cout << "Error: encountered an edge without a pseudonormal. Degenerate face? Vertices: " << index1 << " " << index2 << endl;
142 delete(objMesh);
143 throw(1);
144 }
145 if (pseudoNormalObjMesh->getEdgePseudoNormal(index2, index0, &pseudoNormals[5]) != 0)
146 {
147 cout << "Error: encountered an edge without a pseudonormal. Degenerate face? Vertices: " << index2 << " " << index0 << endl;
148 delete(objMesh);
149 throw(1);
150 }
151
152 // face pseudo normal
153 Vec3d p0 = pseudoNormalObjMesh->getPosition(index0);
154 Vec3d p1 = pseudoNormalObjMesh->getPosition(index1);
155 Vec3d p2 = pseudoNormalObjMesh->getPosition(index2);
156
157 pseudoNormals[6] = norm(cross(p1-p0,p2-p0));
158
159 for(int normali=0; normali < 7; normali++)
160 {
161 if (ObjMesh::isNaN(pseudoNormals[normali][0]) ||
162 ObjMesh::isNaN(pseudoNormals[normali][1]) ||
163 ObjMesh::isNaN(pseudoNormals[normali][2]))
164 {
165 cout << "Error: nan encountered: " << pseudoNormals[normali][0] << " " << pseudoNormals[normali][1] << " " << pseudoNormals[normali][2] << endl;
166 cout << "Group: " << i << " Triangle: " << j << " " << endl;
167 cout << " vtx0: " << index0 << " vtx1: " << index1 << " vtx2: " << index2 << endl;
168 cout << " " << p0 << endl;
169 cout << " " << p1 << endl;
170 cout << " " << p2 << endl;
171 cout << "Feature: " << normali << endl;
172 delete(objMesh);
173 throw(1);
174 }
175 }
176
177 TriangleWithCollisionInfoAndPseudoNormals triangle(p0,p1,p2,pseudoNormals);
178 triangles.push_back(triangle);
179 }
180 }
181
182 cout << "Total number of triangles is: " << triangles.size() << endl;
183
184 // build the octree
185 Vec3d bmin, bmax;
186 objMesh->getCubicBoundingBox(1.0, &bmin, &bmax);
187
188 cout << "The model tight-fitting cube-shaped bounding box is: " << endl;
189 cout << "xmin: " << bmin[0] << " xmax: " << bmax[0] << endl;
190 cout << "ymin: " << bmin[1] << " ymax: " << bmax[1] << endl;
191 cout << "zmin: " << bmin[2] << " zmax: " << bmax[2] << endl;
192
193 BoundingBox bboxOctree(bmin, bmax);
194 bboxOctree.expand(bboxExpansionRatio);
195
196 cout << "Starting the octree creation algorithm..." << endl;
197
198 root = new Octree<TriangleWithCollisionInfoAndPseudoNormals>(maxTreeDepth);
199 root->setBuildPrintInfo(printInfo);
200 root->build(triangles, bboxOctree, maxNumTrianglesInLeafNode);
201
202 if(printInfo == 1)
203 {
204 int numMaxDepthExceededCases;
205 int numMaxTriInDepthExceededCases;
206 root->getBuildInfo(&numMaxDepthExceededCases, &numMaxTriInDepthExceededCases);
207 printf("Total number of cells with more than %d triangles: %d. Max triangles in such cells: %d.\n", maxNumTrianglesInLeafNode, numMaxDepthExceededCases,
208 numMaxTriInDepthExceededCases);
209 }
210
211 triangles.clear(); // release memory
212 delete(objMesh);
213
214 cout << "Octree creation completed successfully." << endl;
215 }
216
217 template ObjMeshOctree<TriangleBasic>::ObjMeshOctree(const ObjMesh * objMesh, int maxNumTrianglesInLeafNode_, int maxTreeDepth_, int printInfo);
218 template ObjMeshOctree<TriangleWithCollisionInfo>::ObjMeshOctree(const ObjMesh * objMesh, int maxNumTrianglesInLeafNode_, int maxTreeDepth_, int printInfo);
219 #if defined(_WIN32) || defined(WIN32)
220 //template ObjMeshOctree<TriangleWithCollisionInfoAndPseudoNormals>::ObjMeshOctree( ObjMesh * objMesh, int maxNumTrianglesInLeafNode_, int maxTreeDepth_, int printInfo );
221 #endif
222