Compute the convex hull of a set of points and return the resulting convex mesh
| 42 | |
| 43 | // Compute the convex hull of a set of points and return the resulting convex mesh |
| 44 | bool QuickHull::computeConvexHull(const VertexArray& vertexArray, PolygonVertexArray& outPolygonVertexArray, |
| 45 | Array<float>& outVertices, Array<unsigned int>& outIndices, |
| 46 | Array<PolygonVertexArray::PolygonFace>& outFaces, MemoryAllocator& allocator, |
| 47 | std::vector<Message>& errors) { |
| 48 | |
| 49 | bool isValid = true; |
| 50 | |
| 51 | // Extract the points from the array |
| 52 | Array<Vector3> points(allocator); |
| 53 | extractPoints(vertexArray, points); |
| 54 | |
| 55 | // Remove the duplicated vertices from the points |
| 56 | removeDuplicatedVertices(points, allocator); |
| 57 | |
| 58 | // If there are less than four vertices in the vertex array |
| 59 | if (points.size() < 4) { |
| 60 | |
| 61 | errors.push_back(Message("The VertexArray must contain at least 4 vertices to create a convex mesh")); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | Array<uint32> orphanPointsIndices(allocator, points.size()); |
| 66 | decimal maxAbsX = 0; |
| 67 | decimal maxAbsY = 0; |
| 68 | decimal maxAbsZ = 0; |
| 69 | for (uint32 i=0 ; i < points.size(); i++) { |
| 70 | orphanPointsIndices.add(i); |
| 71 | |
| 72 | decimal absX = std::abs(points[i].x); |
| 73 | decimal absY = std::abs(points[i].y); |
| 74 | decimal absZ = std::abs(points[i].z); |
| 75 | |
| 76 | if (absX > maxAbsX) { |
| 77 | maxAbsX = absX; |
| 78 | } |
| 79 | if (absY > maxAbsY) { |
| 80 | maxAbsY = absY; |
| 81 | } |
| 82 | if (absZ > maxAbsZ) { |
| 83 | maxAbsZ = absZ; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Compute the 'epsilon' value for this set of points |
| 88 | const decimal epsilon = 3 * (maxAbsX + maxAbsY + maxAbsZ) * MACHINE_EPSILON; |
| 89 | |
| 90 | QHHalfEdgeStructure convexHull(allocator); |
| 91 | |
| 92 | Array<QHHalfEdgeStructure::Face*> initialFaces(allocator); |
| 93 | |
| 94 | // Compute the initial convex hull |
| 95 | isValid &= computeInitialHull(points, convexHull, initialFaces, orphanPointsIndices, allocator, errors); |
| 96 | if (!isValid) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | assert(convexHull.getNbVertices() == 4); |
| 101 | assert(convexHull.getNbFaces() == 4); |
nothing calls this directly
no test coverage detected