From 6dac7e0916bdc725055764922ea45c865df1060d Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Sun, 11 Feb 2018 14:42:11 +0100 Subject: [PATCH] Fix issue with clipping methods --- src/mathematics/mathematics_functions.cpp | 137 ++++++++++------------ 1 file changed, 65 insertions(+), 72 deletions(-) diff --git a/src/mathematics/mathematics_functions.cpp b/src/mathematics/mathematics_functions.cpp index 52ac337b..99640c58 100755 --- a/src/mathematics/mathematics_functions.cpp +++ b/src/mathematics/mathematics_functions.cpp @@ -225,30 +225,26 @@ List reactphysics3d::clipSegmentWithPlanes(const Vector3& segA, const V const List& planesPoints, const List& planesNormals, MemoryAllocator& allocator) { - assert(planesPoints.size() == planesNormals.size()); - List list1(allocator, 2); - List list2(allocator, 2); + List inputVertices(allocator, 2); + List outputVertices(allocator, 2); - List* inputVertices = &list1; - List* outputVertices = &list2; - - inputVertices->add(segA); - inputVertices->add(segB); + inputVertices.add(segA); + inputVertices.add(segB); // For each clipping plane for (uint p=0; psize() == 0) return *inputVertices; + if (inputVertices.size() == 0) return inputVertices; - assert(inputVertices->size() == 2); + assert(inputVertices.size() == 2); - outputVertices->clear(); + outputVertices.clear(); - Vector3& v1 = (*inputVertices)[0]; - Vector3& v2 = (*inputVertices)[1]; + Vector3& v1 = inputVertices[0]; + Vector3& v2 = inputVertices[1]; decimal v1DotN = (v1 - planesPoints[p]).dot(planesNormals[p]); decimal v2DotN = (v2 - planesPoints[p]).dot(planesNormals[p]); @@ -263,40 +259,39 @@ List reactphysics3d::clipSegmentWithPlanes(const Vector3& segA, const V decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]); if (t >= decimal(0) && t <= decimal(1.0)) { - outputVertices->add(v1 + t * (v2 - v1)); + outputVertices.add(v1 + t * (v2 - v1)); } else { - outputVertices->add(v2); + outputVertices.add(v2); } } else { - outputVertices->add(v1); + outputVertices.add(v1); } // Add the second vertex - outputVertices->add(v2); + outputVertices.add(v2); } else { // If the second vertex is behind the clipping plane // If the first vertex is in front of the clippling plane if (v1DotN >= decimal(0.0)) { - outputVertices->add(v1); + outputVertices.add(v1); // The first point we keep is the intersection between the segment v1, v2 and the clipping plane decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]); if (t >= decimal(0.0) && t <= decimal(1.0)) { - outputVertices->add(v1 + t * (v2 - v1)); + outputVertices.add(v1 + t * (v2 - v1)); } } } inputVertices = outputVertices; - outputVertices = p % 2 == 0 ? &list1 : &list2; } - return *outputVertices; + return outputVertices; } // Clip a polygon against multiple planes and return the clipped polygon vertices @@ -306,75 +301,73 @@ List reactphysics3d::clipPolygonWithPlanes(const List& polygon assert(planesPoints.size() == planesNormals.size()); - uint nbMaxElements = polygonVertices.size() + planesPoints.size(); - List list1(allocator, nbMaxElements); - List list2(allocator, nbMaxElements); + uint nbMaxElements = polygonVertices.size() + planesPoints.size(); + List inputVertices(allocator, nbMaxElements); + List outputVertices(allocator, nbMaxElements); - const List* inputVertices = &polygonVertices; - List* outputVertices = &list2; + inputVertices.addRange(polygonVertices); - // For each clipping plane - for (uint p=0; pclear(); + outputVertices.clear(); - uint nbInputVertices = inputVertices->size(); - uint vStart = nbInputVertices - 1; + uint nbInputVertices = inputVertices.size(); + uint vStart = nbInputVertices - 1; - // For each edge of the polygon - for (uint vEnd = 0; vEnd= decimal(0.0)) { + // If the second vertex is in front of the clippling plane + if (v2DotN >= decimal(0.0)) { - // If the first vertex is not in front of the clippling plane - if (v1DotN < decimal(0.0)) { + // If the first vertex is not in front of the clippling plane + if (v1DotN < decimal(0.0)) { - // The second point we keep is the intersection between the segment v1, v2 and the clipping plane - decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]); + // The second point we keep is the intersection between the segment v1, v2 and the clipping plane + decimal t = computePlaneSegmentIntersection(v1, v2, planesNormals[p].dot(planesPoints[p]), planesNormals[p]); - if (t >= decimal(0) && t <= decimal(1.0)) { - outputVertices->add(v1 + t * (v2 - v1)); + if (t >= decimal(0) && t <= decimal(1.0)) { + outputVertices.add(v1 + t * (v2 - v1)); + } + else { + outputVertices.add(v2); + } + } + + // Add the second vertex + outputVertices.add(v2); + } + else { // If the second vertex is behind the clipping plane + + // If the first vertex is in front of the clippling plane + if (v1DotN >= decimal(0.0)) { + + // The first point we keep is the intersection between the segment v1, v2 and the clipping plane + decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]); + + if (t >= decimal(0.0) && t <= decimal(1.0)) { + outputVertices.add(v1 + t * (v2 - v1)); + } + else { + outputVertices.add(v1); + } } - else { - outputVertices->add(v2); - } } - // Add the second vertex - outputVertices->add(v2); - } - else { // If the second vertex is behind the clipping plane - - // If the first vertex is in front of the clippling plane - if (v1DotN >= decimal(0.0)) { - - // The first point we keep is the intersection between the segment v1, v2 and the clipping plane - decimal t = computePlaneSegmentIntersection(v1, v2, -planesNormals[p].dot(planesPoints[p]), -planesNormals[p]); - - if (t >= decimal(0.0) && t <= decimal(1.0)) { - outputVertices->add(v1 + t * (v2 - v1)); - } - else { - outputVertices->add(v1); - } - } + vStart = vEnd; } - vStart = vEnd; + inputVertices = outputVertices; } - inputVertices = outputVertices; - outputVertices = p % 2 == 0 ? &list1 : &list2; - } - - return *outputVertices; + return outputVertices; } // Project a point onto a plane that is given by a point and its unit length normal