Polyhedron AABB::ToPolyhedron() const { // Note to maintainer: This function is an exact copy of OBB:ToPolyhedron() and Frustum::ToPolyhedron(). Polyhedron p; // Populate the corners of this AABB. // The will be in the order 0: ---, 1: --+, 2: -+-, 3: -++, 4: +--, 5: +-+, 6: ++-, 7: +++. for(int i = 0; i < 8; ++i) p.v.push_back(CornerPoint(i)); // Generate the 6 faces of this AABB. const int faces[6][4] = { { 0, 1, 3, 2 }, // X- { 4, 6, 7, 5 }, // X+ { 0, 4, 5, 1 }, // Y- { 7, 6, 2, 3 }, // Y+ { 0, 2, 6, 4 }, // Z- { 1, 5, 7, 3 }, // Z+ }; for(int f = 0; f < 6; ++f) { Polyhedron::Face face; for(int v = 0; v < 4; ++v) face.v.push_back(faces[f][v]); p.f.push_back(face); } assume(p.IsClosed()); assume(p.IsConvex()); assume(p.EulerFormulaHolds()); assume(p.FaceIndicesValid()); assume(p.FacesAreNondegeneratePlanar()); assume(p.Contains(this->CenterPoint())); return p; }
Polyhedron Polyhedron::ConvexHull(const float3 *pointArray, int numPoints) { ///\todo Check input ptr and size! std::set<int> extremes; const float3 dirs[] = { float3(1,0,0), float3(0,1,0), float3(0,0,1), float3(1,1,0), float3(1,0,1), float3(0,1,1), float3(1,1,1) }; for(size_t i = 0; i < ARRAY_LENGTH(dirs); ++i) { int idx1, idx2; OBB::ExtremePointsAlongDirection(dirs[i], pointArray, numPoints, idx1, idx2); extremes.insert(idx1); extremes.insert(idx2); } Polyhedron p; assume(extremes.size() >= 4); ///\todo Fix this case! int i = 0; std::set<int>::iterator iter = extremes.begin(); for(; iter != extremes.end() && i < 4; ++iter, ++i) p.v.push_back(pointArray[*iter]); Face f; f.v.resize(3); f.v[0] = 0; f.v[1] = 1; f.v[2] = 2; p.f.push_back(f); f.v[0] = 0; f.v[1] = 1; f.v[2] = 3; p.f.push_back(f); f.v[0] = 0; f.v[1] = 2; f.v[2] = 3; p.f.push_back(f); f.v[0] = 1; f.v[1] = 2; f.v[2] = 3; p.f.push_back(f); p.OrientNormalsOutsideConvex(); // Ensure that the winding order of the generated tetrahedron is correct for each face. // assert(p.IsClosed()); //assert(p.IsConvex()); assert(p.FaceIndicesValid()); assert(p.EulerFormulaHolds()); // assert(p.FacesAreNondegeneratePlanar()); CHullHelp hull; for(int j = 0; j < (int)p.f.size(); ++j) hull.livePlanes.push_back(j); // For better performance, merge the remaining extreme points first. for(; iter != extremes.end(); ++iter) { p.MergeConvex(pointArray[*iter]); mathassert(p.FaceIndicesValid()); // mathassert(p.IsClosed()); // mathassert(p.FacesAreNondegeneratePlanar()); // mathassert(p.IsConvex()); } // Merge all the rest of the points. for(int j = 0; j < numPoints; ++j) { if (p.f.size() > 5000 && (j & 255) == 0) printf("Mergeconvex %d/%d, #vertices %d, #faces %d\n", j, numPoints, (int)p.v.size(), (int)p.f.size()); p.MergeConvex(pointArray[i]); mathassert(p.FaceIndicesValid()); // mathassert(p.IsClosed()); // mathassert(p.FacesAreNondegeneratePlanar()); //mathassert(p.IsConvex()); // if (p.f.size() > 5000) // break; } return p; }