| 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 | // An octree storing triangles. Can be intersected with a sphere or a line segment. |
| 30 | // Jernej Barbic, CMU |
| 31 | |
| 32 | #include "openGL-headers.h" |
| 33 | #include <iostream> |
| 34 | #include "octree.h" |
| 35 | using namespace std; |
| 36 | |
| 37 | template<class TriangleClass> |
| 38 | Octree<TriangleClass>::Octree(int maxDepth_, int depth_): maxDepth(maxDepth_), depth(depth_) |
| 39 | { |
| 40 | for(int i=0; i<8; i++) |
| 41 | childrenNodes[i] = NULL; |
| 42 | } |
| 43 | |
| 44 | template<class TriangleClass> |
| 45 | bool Octree<TriangleClass>::build(std::vector<TriangleClass> & triangleList, int maxNumTriangles) |
| 46 | { |
| 47 | BoundingBox parentCube(triangleList); |
| 48 | parentCube.expand(1.05); |
| 49 | return build(triangleList,parentCube,maxNumTriangles); |
| 50 | } |
| 51 | |
| 52 | template<class TriangleClass> |
| 53 | int Octree<TriangleClass>::buildPrintInfo = 0; |
| 54 | |
| 55 | template<class TriangleClass> |
| 56 | int Octree<TriangleClass>::numMaxDepthExceededCases = 0; |
| 57 | |
| 58 | template<class TriangleClass> |
| 59 | int Octree<TriangleClass>::numMaxTriInDepthExceededCases = 0; |
| 60 | |
| 61 | template<class TriangleClass> |
| 62 | bool Octree<TriangleClass>::build(std::vector<TriangleClass> & triangleList, BoundingBox & parentCube, int maxNumTriangles) |
| 63 | { |
| 64 | // set the bounding box |
| 65 | boundingBox = parentCube; |
| 66 | |
| 67 | // total number of triangles |
| 68 | int numTriangles = (int)triangleList.size(); |
| 69 | |
| 70 | // if there are fewer triangles than the threshold value, or if max depth has been reached, this node becomes a leaf which stores the triangles |
| 71 | // max depth checking is necessary, since otherwise vertices with high valence will always be contained in some box, and that box will be split forever |
| 72 | if ((numTriangles <= maxNumTriangles) || (depth >= maxDepth)) |
| 73 | { |
| 74 | // cout << "L" << numTriangles << " " << depth << " " << endl; |
| 75 | for(int i=0; i<numTriangles; i++) |
| 76 | triangles.push_back(triangleList[i]); |
| 77 | |
| 78 | if ((depth >= maxDepth) && (numTriangles >= maxNumTriangles)) |
| 79 | { |
| 80 | if (buildPrintInfo == 2) |
| 81 | printf("Warning: cube max depth (%d) reached with more triangles per cell (%d) than requested (%d)\n",maxDepth,numTriangles,maxNumTriangles); |
| 82 | |
| 83 | if (buildPrintInfo == 1) |
| 84 | { |
| 85 | numMaxDepthExceededCases++; |
| 86 | |
| 87 | if (numMaxDepthExceededCases <= 5) |
| 88 | printf("Warning: cube max depth (%d) reached with more triangles per cell (%d) than requested (%d)\n",maxDepth,numTriangles,maxNumTriangles); |
| 89 | |
| 90 | if (numMaxDepthExceededCases == 5) |
| 91 | { |
| 92 | printf("(suppressing further max depth warning messages)\n"); |
| 93 | } |
| 94 | |
| 95 | if (numTriangles > numMaxTriInDepthExceededCases) |
| 96 | numMaxTriInDepthExceededCases = numTriangles; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | //cout << "N" << numTriangles << " "; |
| 104 | |
| 105 | // create the child cubes; |
| 106 | BoundingBox childCubeBoxes[8]; |
| 107 | createChildCubes(childCubeBoxes); |
| 108 | |
| 109 | std::vector<TriangleClass> childTriangles[8]; |
| 110 | |
| 111 | // for each child, generate a list of triangles that intersect the child bounding box |
| 112 | for(int i=0; i<numTriangles; i++) |
| 113 | for(int j=0; j<8; j++) |
| 114 | if (triangleList[i].doesIntersectBox(childCubeBoxes[j])) |
| 115 | childTriangles[j].push_back(triangleList[i]); |
| 116 | |
| 117 | // for (int i=0; i<8; i++) |
| 118 | // cout << "S" << i << ":" << childTriangles[i].size() << " "; |
| 119 | // cout << endl; |
| 120 | |
| 121 | // for any child with intersecting triangles, create and recursively build the subtree |
| 122 | for(int i=0; i<8; i++) |
| 123 | { |
| 124 | if (childTriangles[i].size() > 0) |
| 125 | { |
| 126 | childrenNodes[i] = new Octree<TriangleClass>(maxDepth,depth+1); |
| 127 | |
| 128 | if(childrenNodes[i] == NULL) |
| 129 | return false; |
| 130 | |
| 131 | if(!childrenNodes[i]->build(childTriangles[i], childCubeBoxes[i], maxNumTriangles)) |
| 132 | return false; |
| 133 | } |
| 134 | else |
| 135 | childrenNodes[i] = NULL; |
| 136 | } |
| 137 | |
| 138 | //cout << "C "; |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | template<class TriangleClass> |
| 143 | int Octree<TriangleClass>::getDepth() |
| 144 | { |
| 145 | if(triangles.size() > 0) |
| 146 | return 0; // leaf |
| 147 | else |
| 148 | { |
| 149 | int maxChildDepth=0; |
| 150 | for(int i=0; i<8; i++) |
| 151 | { |
| 152 | if(childrenNodes[i] != NULL) |
| 153 | { |
| 154 | int childDepth = childrenNodes[i]->getDepth(); |
| 155 | if (childDepth > maxChildDepth) |
| 156 | maxChildDepth = childDepth; |
| 157 | } |
| 158 | } |
| 159 | return maxChildDepth + 1; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | template<class TriangleClass> |
| 164 | void Octree<TriangleClass>::buildCollisionList(std::vector<TriangleClass*> & triangleList, const SimpleSphere & simpleSphere) |
| 165 | { |
| 166 | // if the bounding box does not intersect the sphere, there can be no collision |
| 167 | if(!simpleSphere.doesBoundingBoxIntersect(boundingBox)) |
| 168 | return; |
| 169 | |
| 170 | // if this node is a leaf, add its triangles to the potential collision list, otherwise recurse |
| 171 | if(triangles.size() > 0) |
| 172 | { |
| 173 | int tsize = (int)triangles.size(); |
| 174 | for(int i=0; i<tsize; i++) |
| 175 | triangleList.push_back(&(triangles[i])); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | for(int i=0; i<8; i++) |
| 180 | if(childrenNodes[i] != NULL) |
| 181 | childrenNodes[i]->buildCollisionList(triangleList, simpleSphere); |
| 182 | } |
| 183 | |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | template<class TriangleClass> |
| 188 | void Octree<TriangleClass>::buildCollisionList(std::vector<TriangleClass*> & triangleList, const Vec3d & segmentStartPoint, const Vec3d & segmentEndPoint, vector<Vec3d> * intersectionList) |
| 189 | { |
| 190 | // if the bounding box does not intersect the line segment, there can be no collision |
| 191 | Vec3d intersectionPoint; |
| 192 | if(!boundingBox.lineSegmentIntersection(segmentStartPoint, segmentEndPoint, &intersectionPoint)) |
| 193 | return; |
| 194 | |
| 195 | // if this node is a leaf, add its triangles to the potential collision list, otherwise recurse |
| 196 | if(triangles.size() > 0) |
| 197 | { |
| 198 | int tsize = (int)triangles.size(); |
| 199 | for(int i=0; i<tsize; i++) |
| 200 | { |
| 201 | Vec3d intersectionPoint; |
| 202 | int collisionStatus = triangles[i].lineSegmentIntersection(segmentStartPoint, segmentEndPoint, &intersectionPoint); |
| 203 | if (collisionStatus == 1) |
| 204 | { |
| 205 | triangleList.push_back(&(triangles[i])); |
| 206 | if(intersectionList) |
| 207 | intersectionList->push_back(intersectionPoint); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | for(int i=0; i<8; i++) |
| 214 | if(childrenNodes[i] != NULL) |
| 215 | childrenNodes[i]->buildCollisionList(triangleList, segmentStartPoint, segmentEndPoint, intersectionList); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | template<class TriangleClass> |
| 220 | void Octree<TriangleClass>::render() |
| 221 | { |
| 222 | for(int i=0; i<8; i++) |
| 223 | if(childrenNodes[i] != NULL) |
| 224 | childrenNodes[i]->render(); |
| 225 | |
| 226 | // render only leaf nodes that are not empty |
| 227 | if(triangles.size() > 0) |
| 228 | boundingBox.render(); |
| 229 | } |
| 230 | |
| 231 | template<class TriangleClass> |
| 232 | void Octree<TriangleClass>::render(int level) |
| 233 | { |
| 234 | for(int i=0; i<8; i++) |
| 235 | if(childrenNodes[i] != NULL) |
| 236 | childrenNodes[i]->render(level); |
| 237 | |
| 238 | // render only non-empty leaf nodes at the requested depth |
| 239 | if ((triangles.size() > 0) && (level == depth)) |
| 240 | boundingBox.render(); |
| 241 | } |
| 242 | |
| 243 | template<class TriangleClass> |
| 244 | void Octree<TriangleClass>::render(int level, int boxIndex) |
| 245 | { |
| 246 | renderCounter = 0; |
| 247 | renderHelper(level,boxIndex); |
| 248 | } |
| 249 | |
| 250 | template<class TriangleClass> |
| 251 | int Octree<TriangleClass>::renderCounter; |
| 252 | |
| 253 | template<class TriangleClass> |
| 254 | int Octree<TriangleClass>::printRenderInfo = 0; |
| 255 | |
| 256 | template<class TriangleClass> |
| 257 | void Octree<TriangleClass>::renderHelper(int level, int boxIndex) |
| 258 | { |
| 259 | unsigned int j; |
| 260 | for(int i=0; i<8; i++) |
| 261 | if(childrenNodes[i] != NULL) |
| 262 | childrenNodes[i]->renderHelper(level,boxIndex); |
| 263 | |
| 264 | // render only non-empty leaf nodes at the requested depth |
| 265 | if ((triangles.size() > 0) && (level == depth)) |
| 266 | { |
| 267 | if (renderCounter == boxIndex) |
| 268 | { |
| 269 | glColor3f(0,1,0); |
| 270 | boundingBox.render(); |
| 271 | glColor3f(1,0,0); |
| 272 | for(j=0; j<triangles.size(); j++) |
| 273 | triangles[j].render(); |
| 274 | glColor3f(0,0,0); |
| 275 | for(j=0; j<triangles.size(); j++) |
| 276 | triangles[j].renderEdges(); |
| 277 | if (printRenderInfo == 1) |
| 278 | { |
| 279 | boundingBox.print(); |
| 280 | printf("Num triangles: %d.\n", (int)triangles.size()); |
| 281 | printRenderInfo = 0; |
| 282 | } |
| 283 | } |
| 284 | renderCounter++; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | template<class TriangleClass> |
| 289 | void Octree<TriangleClass>::deallocate() |
| 290 | { |
| 291 | for(int i=0; i<8; i++) |
| 292 | { |
| 293 | if(childrenNodes[i] != NULL) |
| 294 | { |
| 295 | childrenNodes[i]->deallocate(); |
| 296 | |
| 297 | delete(childrenNodes[i]); |
| 298 | childrenNodes[i] = NULL; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | triangles.clear(); |
| 303 | } |
| 304 | |
| 305 | template<class TriangleClass> |
| 306 | void Octree<TriangleClass>::createChildCubes(BoundingBox * childCubeBoxes) |
| 307 | { |
| 308 | Vec3d center = boundingBox.center(); |
| 309 | |
| 310 | // top left & near |
| 311 | childCubeBoxes[0].setbmin(boundingBox.bmin()[0], center[1], boundingBox.bmin()[2]); |
| 312 | childCubeBoxes[0].setbmax(center[0], boundingBox.bmax()[1], center[2]); |
| 313 | |
| 314 | // top right & near |
| 315 | childCubeBoxes[1].setbmin(center[0], center[1], boundingBox.bmin()[2]); |
| 316 | childCubeBoxes[1].setbmax(boundingBox.bmax()[0], boundingBox.bmax()[1], center[2]); |
| 317 | |
| 318 | // bottom left & near |
| 319 | childCubeBoxes[2].setbmin(boundingBox.bmin()[0], boundingBox.bmin()[1], boundingBox.bmin()[2]); |
| 320 | childCubeBoxes[2].setbmax(center[0], center[1], center[2]); |
| 321 | |
| 322 | // bottom right & near |
| 323 | childCubeBoxes[3].setbmin(center[0], boundingBox.bmin()[1], boundingBox.bmin()[2]); |
| 324 | childCubeBoxes[3].setbmax(boundingBox.bmax()[0], center[1], center[2]); |
| 325 | |
| 326 | // top left & far |
| 327 | childCubeBoxes[4].setbmin(boundingBox.bmin()[0], center[1], center[2]); |
| 328 | childCubeBoxes[4].setbmax(center[0], boundingBox.bmax()[1], boundingBox.bmax()[2]); |
| 329 | |
| 330 | // top right & far |
| 331 | childCubeBoxes[5].setbmin(center[0], center[1], center[2]); |
| 332 | childCubeBoxes[5].setbmax(boundingBox.bmax()[0], boundingBox.bmax()[1], boundingBox.bmax()[2]); |
| 333 | |
| 334 | // bottom left & far |
| 335 | childCubeBoxes[6].setbmin(boundingBox.bmin()[0], boundingBox.bmin()[1], center[2]); |
| 336 | childCubeBoxes[6].setbmax(center[0], center[1], boundingBox.bmax()[2]); |
| 337 | |
| 338 | // bottom right & far |
| 339 | childCubeBoxes[7].setbmin(center[0], boundingBox.bmin()[1], center[2]); |
| 340 | childCubeBoxes[7].setbmax(boundingBox.bmax()[0], center[1], boundingBox.bmax()[2]); |
| 341 | |
| 342 | // sanity check |
| 343 | for (int i=0; i<7; i++) |
| 344 | childCubeBoxes[i].verifyBox(); |
| 345 | } |
| 346 | |
| 347 | template Octree<TriangleBasic>::Octree(int maxDepth_g, int depth_g); |
| 348 | template bool Octree<TriangleBasic>::build(std::vector<TriangleBasic> &triangleList, BoundingBox &parentCube, int maxNumTriangles); |
| 349 | template bool Octree<TriangleBasic>::build(std::vector<TriangleBasic> &triangleList, int maxNumTriangles); |
| 350 | template void Octree<TriangleBasic>::setBuildPrintInfo(int info); |
| 351 | template void Octree<TriangleBasic>::getBuildInfo(int * numMaxDepthExceededCases, int * numMaxTriInDepthExceededCases); |
| 352 | template int Octree<TriangleBasic>::getDepth(); |
| 353 | template void Octree<TriangleBasic>::buildCollisionList(std::vector<TriangleBasic*> &triangleList, const SimpleSphere &simpleSphere); |
| 354 | template void Octree<TriangleBasic>::buildCollisionList(std::vector<TriangleBasic*> &triangleList, const Vec3d & segmentStartPoint, const Vec3d & segmentEndPoint, vector<Vec3d> * intersectionList); |
| 355 | template void Octree<TriangleBasic>::render(); |
| 356 | template void Octree<TriangleBasic>::render(int level); |
| 357 | template void Octree<TriangleBasic>::render(int level, int boxIndex); |
| 358 | template void Octree<TriangleBasic>::renderHelper(int level, int boxIndex); |
| 359 | |
| 360 | template void Octree<TriangleBasic>::deallocate(); |
| 361 | template void Octree<TriangleBasic>::createChildCubes(BoundingBox * childCubeBoxes); |
| 362 | |
| 363 | template Octree<TriangleWithCollisionInfo>::Octree(int maxDepth_g, int depth_g); |
| 364 | template bool Octree<TriangleWithCollisionInfo>::build(std::vector<TriangleWithCollisionInfo> &triangleList, BoundingBox &parentCube, int maxNumTriangles); |
| 365 | template bool Octree<TriangleWithCollisionInfo>::build(std::vector<TriangleWithCollisionInfo> &triangleList, int maxNumTriangles); |
| 366 | template void Octree<TriangleWithCollisionInfo>::setBuildPrintInfo(int info); |
| 367 | template void Octree<TriangleWithCollisionInfo>::getBuildInfo(int * numMaxDepthExceededCases, int * numMaxTriInDepthExceededCases); |
| 368 | template void Octree<TriangleWithCollisionInfo>::buildCollisionList(std::vector<TriangleWithCollisionInfo*> &triangleList, const SimpleSphere &simpleSphere); |
| 369 | template void Octree<TriangleWithCollisionInfo>::buildCollisionList(std::vector<TriangleWithCollisionInfo*> &triangleList, const Vec3d & segmentStartPoint, const Vec3d & segmentEndPoint, std::vector<Vec3d> * intersectionList); |
| 370 | template int Octree<TriangleWithCollisionInfo>::getDepth(); |
| 371 | template void Octree<TriangleWithCollisionInfo>::render(); |
| 372 | template void Octree<TriangleWithCollisionInfo>::render(int level); |
| 373 | template void Octree<TriangleWithCollisionInfo>::render(int level, int boxIndex); |
| 374 | template void Octree<TriangleWithCollisionInfo>::renderHelper(int level, int boxIndex); |
| 375 | template void Octree<TriangleWithCollisionInfo>::deallocate(); |
| 376 | template void Octree<TriangleWithCollisionInfo>::createChildCubes(BoundingBox * childCubeBoxes); |
| 377 | |
| 378 | template Octree<TriangleWithCollisionInfoAndPseudoNormals>::Octree(int maxDepth_g, int depth_g); |
| 379 | template bool Octree<TriangleWithCollisionInfoAndPseudoNormals>::build(std::vector<TriangleWithCollisionInfoAndPseudoNormals> &triangleList, BoundingBox &parentCube, int maxNumTriangles); |
| 380 | template bool Octree<TriangleWithCollisionInfoAndPseudoNormals>::build(std::vector<TriangleWithCollisionInfoAndPseudoNormals> &triangleList, int maxNumTriangles); |
| 381 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::setBuildPrintInfo(int info); |
| 382 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::getBuildInfo(int * numMaxDepthExceededCases, int * numMaxTriInDepthExceededCases); |
| 383 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::buildCollisionList(std::vector<TriangleWithCollisionInfoAndPseudoNormals*> &triangleList, const SimpleSphere &simpleSphere); |
| 384 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::buildCollisionList(std::vector<TriangleWithCollisionInfoAndPseudoNormals*> &triangleList, const Vec3d & segmentStartPoint, const Vec3d & segmentEndPoint, vector<Vec3d> * intersectionList); |
| 385 | template int Octree<TriangleWithCollisionInfoAndPseudoNormals>::getDepth(); |
| 386 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::render(); |
| 387 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::render(int level); |
| 388 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::render(int level, int boxIndex); |
| 389 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::renderHelper(int level, int boxIndex); |
| 390 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::deallocate(); |
| 391 | template void Octree<TriangleWithCollisionInfoAndPseudoNormals>::createChildCubes(BoundingBox * childCubeBoxes); |
| 392 |