| 1 |
/************************************************************************* |
| 2 |
* * |
| 3 |
* Vega FEM Simulation Library Version 3.1 * |
| 4 |
* * |
| 5 |
* "volumetricMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC * |
| 6 |
* All rights reserved. * |
| 7 |
* * |
| 8 |
* Code authors: Jernej Barbic, Yijing Li * |
| 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 |
#include <float.h> |
| 30 |
#include "objMesh.h" |
| 31 |
#include "generateSurfaceMesh.h" |
| 32 |
#include "cubicMesh.h" |
| 33 |
using namespace std; |
| 34 |
|
| 35 |
// classes to disambiguate and sort faces |
| 36 |
|
| 37 |
class TopologicalFaceI |
| 38 |
{ |
| 39 |
public: |
| 40 |
inline TopologicalFaceI(int p1, int p2, int p3, int p4) |
| 41 |
{ |
| 42 |
vertices[0] = p1; |
| 43 |
vertices[1] = p2; |
| 44 |
vertices[2] = p3; |
| 45 |
vertices[3] = p4; |
| 46 |
size = 4; |
| 47 |
sortVertices(); |
| 48 |
} |
| 49 |
|
| 50 |
inline TopologicalFaceI(int p1, int p2, int p3) |
| 51 |
{ |
| 52 |
vertices[0] = p1; |
| 53 |
vertices[1] = p2; |
| 54 |
vertices[2] = p3; |
| 55 |
size = 3; |
| 56 |
sortVertices(); |
| 57 |
} |
| 58 |
|
| 59 |
//accessor |
| 60 |
int vertex(int i) const { return vertices[i];} |
| 61 |
int sortedVertex(int i) const { return sortedVertices[i]; } |
| 62 |
int faceDegree() const { return size; } |
| 63 |
const int * index() const { return vertices; } |
| 64 |
|
| 65 |
TopologicalFaceI(const TopologicalFaceI & face) |
| 66 |
{ |
| 67 |
size = face.size; |
| 68 |
memcpy(vertices, face.vertices, sizeof(int) * size); |
| 69 |
memcpy(sortedVertices, face.sortedVertices, sizeof(int) * size); |
| 70 |
} |
| 71 |
|
| 72 |
TopologicalFaceI & operator = (const TopologicalFaceI & face) |
| 73 |
{ |
| 74 |
if (&face == this) |
| 75 |
return *this; |
| 76 |
size = face.size; |
| 77 |
memcpy(vertices, face.vertices, sizeof(int) * size); |
| 78 |
memcpy(sortedVertices, face.sortedVertices, sizeof(int) * size); |
| 79 |
return *this; |
| 80 |
} |
| 81 |
|
| 82 |
protected: |
| 83 |
|
| 84 |
inline void sortVertices() |
| 85 |
{ |
| 86 |
memcpy(sortedVertices, vertices, sizeof(int) * size); |
| 87 |
sort(sortedVertices, sortedVertices + size); |
| 88 |
} |
| 89 |
|
| 90 |
int size; |
| 91 |
int vertices[4]; |
| 92 |
int sortedVertices[4]; |
| 93 |
//std::vector<int> vertices_; |
| 94 |
}; |
| 95 |
|
| 96 |
class FaceOrder |
| 97 |
{ |
| 98 |
public: |
| 99 |
bool operator()(const TopologicalFaceI & x, const TopologicalFaceI & y) const; |
| 100 |
}; |
| 101 |
|
| 102 |
bool FaceOrder::operator()(const TopologicalFaceI & x, const TopologicalFaceI & y) const |
| 103 |
{ |
| 104 |
int degx = x.faceDegree(); |
| 105 |
int degy = y.faceDegree(); |
| 106 |
int mindeg = (degx < degy) ? degx : degy; |
| 107 |
|
| 108 |
for (int i = 0; i < mindeg; i++) |
| 109 |
{ |
| 110 |
int x1 = x.sortedVertex(i); |
| 111 |
int y1 = y.sortedVertex(i); |
| 112 |
|
| 113 |
if (x1 < y1) |
| 114 |
return true; |
| 115 |
if (y1 < x1) |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
// the main routine |
| 123 |
ObjMesh * GenerateSurfaceMesh::ComputeMesh(const VolumetricMesh * mesh, bool triangulateOutputMesh) |
| 124 |
{ |
| 125 |
// create an empty surface mesh |
| 126 |
ObjMesh * objMesh = new ObjMesh(); |
| 127 |
|
| 128 |
// create default group |
| 129 |
objMesh->addGroup("Default"); |
| 130 |
|
| 131 |
// add all vertices |
| 132 |
for(int i=0; i<mesh->getNumVertices(); i++) |
| 133 |
{ |
| 134 |
const Vec3d & pos = mesh->getVertex(i); |
| 135 |
objMesh->addVertexPosition(pos); |
| 136 |
} |
| 137 |
|
| 138 |
set<TopologicalFaceI,FaceOrder> surfaceFaces; |
| 139 |
//set<TopologicalFaceI,FaceOrder> interiorFaces; // not needed |
| 140 |
|
| 141 |
//interiorFaces.clear(); |
| 142 |
|
| 143 |
int numElementVertices = mesh->getNumElementVertices(); |
| 144 |
int faceDegree = 0; |
| 145 |
|
| 146 |
if (numElementVertices == 4) |
| 147 |
{ |
| 148 |
faceDegree = 3; |
| 149 |
triangulateOutputMesh = false; |
| 150 |
} |
| 151 |
|
| 152 |
if (numElementVertices == 8) |
| 153 |
faceDegree = 4; |
| 154 |
|
| 155 |
if (faceDegree == 0) |
| 156 |
{ |
| 157 |
printf("Error: unsupported mesh type encountered.\n"); |
| 158 |
return NULL; |
| 159 |
} |
| 160 |
|
| 161 |
// build unique list of all surface faces |
| 162 |
|
| 163 |
if (numElementVertices == 4) // tet mesh |
| 164 |
{ |
| 165 |
for (int i=0; i<mesh->getNumElements(); i++) |
| 166 |
{ |
| 167 |
// compute determinant to establish orientation |
| 168 |
double det = dot(mesh->getVertex(i, 1) - mesh->getVertex(i, 0), cross(mesh->getVertex(i, 2) - mesh->getVertex(i, 0), mesh->getVertex(i, 3) - mesh->getVertex(i, 0))); |
| 169 |
|
| 170 |
//surfaceFaces.erase(*face); |
| 171 |
//interiorFaces.insert(*face); |
| 172 |
|
| 173 |
#define PROCESS_FACE3(q0,q1,q2) \ |
| 174 |
{ \ |
| 175 |
TopologicalFaceI face(mesh->getVertexIndex(i,q0),mesh->getVertexIndex(i,q1),mesh->getVertexIndex(i,q2)); \ |
| 176 |
if (surfaceFaces.find(face) != surfaceFaces.end()) \ |
| 177 |
surfaceFaces.erase(face); \ |
| 178 |
else \ |
| 179 |
surfaceFaces.insert(face); \ |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
if (det >= 0) |
| 184 |
{ |
| 185 |
PROCESS_FACE3(1,2,3) |
| 186 |
PROCESS_FACE3(2,0,3) |
| 187 |
PROCESS_FACE3(3,0,1) |
| 188 |
PROCESS_FACE3(1,0,2) |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
PROCESS_FACE3(3,2,1) |
| 193 |
PROCESS_FACE3(3,0,2) |
| 194 |
PROCESS_FACE3(1,0,3) |
| 195 |
PROCESS_FACE3(2,0,1) |
| 196 |
} |
| 197 |
|
| 198 |
#undef PROCESS_FACE3 |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if (numElementVertices == 8) // cubic mesh |
| 203 |
{ |
| 204 |
for (int i = 0; i < mesh->getNumElements(); i++) |
| 205 |
{ |
| 206 |
|
| 207 |
#define PROCESS_FACE4(q0,q1,q2,q3) \ |
| 208 |
{ \ |
| 209 |
TopologicalFaceI face(mesh->getVertexIndex(i,q0),mesh->getVertexIndex(i,q1),mesh->getVertexIndex(i,q2),mesh->getVertexIndex(i,q3)); \ |
| 210 |
if (surfaceFaces.find(face) != surfaceFaces.end()) \ |
| 211 |
surfaceFaces.erase(face); \ |
| 212 |
else \ |
| 213 |
surfaceFaces.insert(face); \ |
| 214 |
} |
| 215 |
|
| 216 |
PROCESS_FACE4(0,3,2,1) |
| 217 |
PROCESS_FACE4(4,5,6,7) |
| 218 |
PROCESS_FACE4(0,1,5,4) |
| 219 |
PROCESS_FACE4(3,7,6,2) |
| 220 |
PROCESS_FACE4(1,2,6,5) |
| 221 |
PROCESS_FACE4(0,4,7,3) |
| 222 |
|
| 223 |
#undef PROCESS_FACE4 |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// now, surfaceFaces contains a unique list of all surface faces |
| 228 |
// add all faces to the surface mesh |
| 229 |
//int * index = (int*) malloc (sizeof(int) * faceDegree); |
| 230 |
set<TopologicalFaceI,FaceOrder>::iterator face; |
| 231 |
for (face = surfaceFaces.begin(); face != surfaceFaces.end(); ++face) // all surface faces |
| 232 |
{ |
| 233 |
//for (int i = 0; i < faceDegree; i++) |
| 234 |
// index[i] = face->vertex(i); |
| 235 |
const int * index = face->index(); |
| 236 |
|
| 237 |
std::pair< bool, unsigned int > texPos(false,0); // no textures |
| 238 |
std::pair< bool, unsigned int > normal(false,0); // no normals |
| 239 |
|
| 240 |
if (triangulateOutputMesh) |
| 241 |
{ |
| 242 |
// triangulate the face into two triangles |
| 243 |
ObjMesh::Face newFace1; |
| 244 |
newFace1.addVertex( ObjMesh::Vertex( index[0], texPos, normal ) ); |
| 245 |
newFace1.addVertex( ObjMesh::Vertex( index[1], texPos, normal ) ); |
| 246 |
newFace1.addVertex( ObjMesh::Vertex( index[2], texPos, normal ) ); |
| 247 |
|
| 248 |
ObjMesh::Face newFace2; |
| 249 |
newFace2.addVertex( ObjMesh::Vertex( index[2], texPos, normal ) ); |
| 250 |
newFace2.addVertex( ObjMesh::Vertex( index[3], texPos, normal ) ); |
| 251 |
newFace2.addVertex( ObjMesh::Vertex( index[0], texPos, normal ) ); |
| 252 |
|
| 253 |
objMesh->addFaceToGroup(newFace1,0); |
| 254 |
objMesh->addFaceToGroup(newFace2,0); |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
ObjMesh::Face newFace1; |
| 259 |
for(int i=0; i<faceDegree; i++) |
| 260 |
newFace1.addVertex( ObjMesh::Vertex( index[i], texPos, normal ) ); |
| 261 |
objMesh->addFaceToGroup(newFace1, 0); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
//free(index); |
| 266 |
|
| 267 |
if (mesh->getElementType() == CubicMesh::elementType()) |
| 268 |
{ |
| 269 |
// cubic mesh |
| 270 |
objMesh->setNormalsToFaceNormals(); |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
// other types of meshes (e.g., tet) |
| 275 |
objMesh->computePseudoNormals(); |
| 276 |
objMesh->setNormalsToPseudoNormals(); |
| 277 |
} |
| 278 |
|
| 279 |
objMesh->setSingleMaterial(ObjMesh::Material()); |
| 280 |
|
| 281 |
return objMesh; |
| 282 |
} |
| 283 |
|
| 284 |
// advanced routine, not used very often |
| 285 |
ObjMesh * GenerateSurfaceMesh::ComputeMesh(const VolumetricMesh * mesh, const ObjMesh * superMesh, bool triangulateOutputMesh) |
| 286 |
{ |
| 287 |
// for each volumetric mesh vertex, find the nearest obj file vertex |
| 288 |
int * closestObjVertex = (int*) malloc (sizeof(int) * mesh->getNumVertices()); |
| 289 |
for(int i=0; i<mesh->getNumVertices(); i++) |
| 290 |
{ |
| 291 |
const Vec3d & pos = mesh->getVertex(i); |
| 292 |
double dist; |
| 293 |
closestObjVertex[i] = superMesh->getClosestVertex(pos, &dist); |
| 294 |
} |
| 295 |
|
| 296 |
// build the list of triangles |
| 297 |
set<TopologicalFaceI,FaceOrder> superMeshFaces; |
| 298 |
for(unsigned int i=0; i < superMesh->getNumGroups(); i++) |
| 299 |
{ |
| 300 |
const ObjMesh::Group * groupHandle = superMesh->getGroupHandle(i); |
| 301 |
for(unsigned int iFace = 0; iFace < groupHandle->getNumFaces(); iFace++) |
| 302 |
{ |
| 303 |
const ObjMesh::Face * faceHandle = groupHandle->getFaceHandle(iFace); |
| 304 |
if (faceHandle->getNumVertices() != 3) |
| 305 |
{ |
| 306 |
printf("Error: input superMesh is not triangulated.\n"); |
| 307 |
free(closestObjVertex); |
| 308 |
return NULL; |
| 309 |
} |
| 310 |
|
| 311 |
superMeshFaces.insert(TopologicalFaceI(faceHandle->getVertexHandle(0)->getPositionIndex(), faceHandle->getVertexHandle(1)->getPositionIndex(), faceHandle->getVertexHandle(2)->getPositionIndex())); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
// create empty surface mesh |
| 316 |
ObjMesh * objMesh = new ObjMesh(); |
| 317 |
|
| 318 |
// create default group |
| 319 |
objMesh->addGroup("Default"); |
| 320 |
|
| 321 |
// add all vertices |
| 322 |
for(int i=0; i<mesh->getNumVertices(); i++) |
| 323 |
{ |
| 324 |
const Vec3d & posm = mesh->getVertex(i); |
| 325 |
Vec3d pos; |
| 326 |
pos[0] = posm[0]; |
| 327 |
pos[1] = posm[1]; |
| 328 |
pos[2] = posm[2]; |
| 329 |
objMesh->addVertexPosition(pos); |
| 330 |
} |
| 331 |
|
| 332 |
set<TopologicalFaceI,FaceOrder> surfaceFaces; |
| 333 |
//set<TopologicalFaceI,FaceOrder> interiorFaces; |
| 334 |
|
| 335 |
// create a unique list of faces |
| 336 |
surfaceFaces.clear(); |
| 337 |
//interiorFaces.clear(); |
| 338 |
|
| 339 |
int numElementVertices = mesh->getNumElementVertices(); |
| 340 |
int faceDegree = 0; |
| 341 |
|
| 342 |
if (numElementVertices == 4) |
| 343 |
{ |
| 344 |
faceDegree = 3; |
| 345 |
triangulateOutputMesh = false; |
| 346 |
} |
| 347 |
|
| 348 |
if (numElementVertices == 8) |
| 349 |
faceDegree = 4; |
| 350 |
|
| 351 |
if (faceDegree == 0) |
| 352 |
{ |
| 353 |
printf("Error: unsupported mesh type encountered.\n"); |
| 354 |
free(closestObjVertex); |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
|
| 358 |
// build unique list of all surface faces |
| 359 |
|
| 360 |
if (numElementVertices == 4) |
| 361 |
{ |
| 362 |
for (int i=0; i<mesh->getNumElements(); i++) |
| 363 |
{ |
| 364 |
// compute determinant to establish orientation |
| 365 |
double det = dot(mesh->getVertex(i, 1) - mesh->getVertex(i, 0), cross(mesh->getVertex(i, 2) - mesh->getVertex(i, 0), mesh->getVertex(i, 3) - mesh->getVertex(i, 0))); |
| 366 |
|
| 367 |
//surfaceFaces.erase(*face); |
| 368 |
//interiorFaces.insert(*face); |
| 369 |
|
| 370 |
#define PROCESS_FACE3(q0,q1,q2) \ |
| 371 |
{ \ |
| 372 |
TopologicalFaceI face(mesh->getVertexIndex(i,q0),mesh->getVertexIndex(i,q1),mesh->getVertexIndex(i,q2)); \ |
| 373 |
if (surfaceFaces.find(face) != surfaceFaces.end()) \ |
| 374 |
surfaceFaces.erase(face);\ |
| 375 |
else \ |
| 376 |
surfaceFaces.insert(face); \ |
| 377 |
} |
| 378 |
|
| 379 |
if (det >= 0) |
| 380 |
{ |
| 381 |
PROCESS_FACE3(1,2,3) |
| 382 |
PROCESS_FACE3(2,0,3) |
| 383 |
PROCESS_FACE3(3,0,1) |
| 384 |
PROCESS_FACE3(1,0,2) |
| 385 |
} |
| 386 |
else |
| 387 |
{ |
| 388 |
PROCESS_FACE3(3,2,1) |
| 389 |
PROCESS_FACE3(3,0,2) |
| 390 |
PROCESS_FACE3(1,0,3) |
| 391 |
PROCESS_FACE3(2,0,1) |
| 392 |
} |
| 393 |
|
| 394 |
#undef PROCESS_FACE3 |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if (numElementVertices == 8) |
| 399 |
{ |
| 400 |
for (int i=0; i<mesh->getNumElements(); i++) |
| 401 |
{ |
| 402 |
//surfaceFaces.erase(*face); |
| 403 |
//interiorFaces.insert(*face); |
| 404 |
|
| 405 |
#define PROCESS_FACE4(q0,q1,q2,q3) \ |
| 406 |
{ \ |
| 407 |
TopologicalFaceI face(mesh->getVertexIndex(i,q0),mesh->getVertexIndex(i,q1),mesh->getVertexIndex(i,q2),mesh->getVertexIndex(i,q3)); \ |
| 408 |
if (surfaceFaces.find(face) != surfaceFaces.end()) \ |
| 409 |
surfaceFaces.erase(face); \ |
| 410 |
else \ |
| 411 |
surfaceFaces.insert(face); \ |
| 412 |
} |
| 413 |
|
| 414 |
PROCESS_FACE4(0,3,2,1) |
| 415 |
PROCESS_FACE4(4,5,6,7) |
| 416 |
PROCESS_FACE4(0,1,5,4) |
| 417 |
PROCESS_FACE4(3,7,6,2) |
| 418 |
PROCESS_FACE4(1,2,6,5) |
| 419 |
PROCESS_FACE4(0,4,7,3) |
| 420 |
|
| 421 |
#undef PROCESS_FACE4 |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
// now, surfaceFaces contains a unique list of all surface faces |
| 426 |
// erase any faces that are not also superMesh faces |
| 427 |
set<TopologicalFaceI,FaceOrder> trueSurfaceFaces; |
| 428 |
for(set<TopologicalFaceI,FaceOrder> :: iterator iter = surfaceFaces.begin(); iter != surfaceFaces.end(); iter++) |
| 429 |
{ |
| 430 |
int vtx0 = closestObjVertex[iter->vertex(0)]; |
| 431 |
int vtx1 = closestObjVertex[iter->vertex(1)]; |
| 432 |
int vtx2 = closestObjVertex[iter->vertex(2)]; |
| 433 |
if (superMeshFaces.find(TopologicalFaceI(vtx0, vtx1, vtx2)) != superMeshFaces.end()) |
| 434 |
trueSurfaceFaces.insert(*iter); |
| 435 |
} |
| 436 |
|
| 437 |
surfaceFaces = trueSurfaceFaces; |
| 438 |
|
| 439 |
// add all faces to the surface obj mesh of the voxel mesh |
| 440 |
int * index = (int*) malloc (sizeof(int) * faceDegree); |
| 441 |
set<TopologicalFaceI,FaceOrder>::iterator face; |
| 442 |
for (face = surfaceFaces.begin(); face != surfaceFaces.end(); ++face) // all surface faces |
| 443 |
{ |
| 444 |
for (int i=0; i<faceDegree; i++) |
| 445 |
index[i] = face->vertex(i); |
| 446 |
|
| 447 |
std::pair< bool, unsigned int > texPos(false,0); // no textures |
| 448 |
std::pair< bool, unsigned int > normal(false,0); // no normals |
| 449 |
|
| 450 |
if (triangulateOutputMesh) |
| 451 |
{ |
| 452 |
// triangulate the face into two triangles |
| 453 |
ObjMesh::Face newFace1; |
| 454 |
newFace1.addVertex( ObjMesh::Vertex( index[0], texPos, normal ) ); |
| 455 |
newFace1.addVertex( ObjMesh::Vertex( index[1], texPos, normal ) ); |
| 456 |
newFace1.addVertex( ObjMesh::Vertex( index[2], texPos, normal ) ); |
| 457 |
|
| 458 |
ObjMesh::Face newFace2; |
| 459 |
newFace2.addVertex( ObjMesh::Vertex( index[2], texPos, normal ) ); |
| 460 |
newFace2.addVertex( ObjMesh::Vertex( index[3], texPos, normal ) ); |
| 461 |
newFace2.addVertex( ObjMesh::Vertex( index[0], texPos, normal ) ); |
| 462 |
|
| 463 |
objMesh->addFaceToGroup(newFace1,0); |
| 464 |
objMesh->addFaceToGroup(newFace2,0); |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
ObjMesh::Face newFace1; |
| 469 |
for(int i=0; i<faceDegree; i++) |
| 470 |
newFace1.addVertex( ObjMesh::Vertex( index[i], texPos, normal ) ); |
| 471 |
objMesh->addFaceToGroup(newFace1, 0); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
free(index); |
| 476 |
|
| 477 |
if (mesh->getElementType() == CubicMesh::elementType()) |
| 478 |
{ |
| 479 |
// cubic mesh |
| 480 |
objMesh->setNormalsToFaceNormals(); |
| 481 |
} |
| 482 |
else |
| 483 |
{ |
| 484 |
// other types of meshes (e.g., tet) |
| 485 |
objMesh->computePseudoNormals(); |
| 486 |
objMesh->setNormalsToPseudoNormals(); |
| 487 |
} |
| 488 |
|
| 489 |
free(closestObjVertex); |
| 490 |
|
| 491 |
objMesh->setSingleMaterial(ObjMesh::Material()); |
| 492 |
|
| 493 |
return objMesh; |
| 494 |
} |
| 495 |
|