From 0bfc588c393ad9aeb72bf91bc0918ece98162565 Mon Sep 17 00:00:00 2001 From: Aaron Date: Tue, 15 Aug 2023 18:01:16 +0800 Subject: [PATCH] Update Triangulate Process [more another case] --- code/Common/PolyTools.h | 19 ++++++++++++++----- code/PostProcessing/TriangulateProcess.cpp | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/code/Common/PolyTools.h b/code/Common/PolyTools.h index a5bd1090b..b53de8579 100644 --- a/code/Common/PolyTools.h +++ b/code/Common/PolyTools.h @@ -64,8 +64,14 @@ inline double GetArea2D(const T& v1, const T& v2, const T& v3) { * The function accepts an unconstrained template parameter for use with * both aiVector3D and aiVector2D, but generally ignores the third coordinate.*/ template -inline bool OnLeftSideOfLine2D(const T& p0, const T& p1,const T& p2) { - return GetArea2D(p0,p2,p1) > 0; +inline int OnLeftSideOfLine2D(const T& p0, const T& p1,const T& p2) { + double area = GetArea2D(p0,p2,p1); + if(std::abs(area) < ai_epsilon) + return 0; + else if(area > 0) + return 1; + else + return -1; } // ------------------------------------------------------------------------------- @@ -75,7 +81,10 @@ inline bool OnLeftSideOfLine2D(const T& p0, const T& p1,const T& p2) { template inline bool PointInTriangle2D(const T& p0, const T& p1,const T& p2, const T& pp) { // pp should be left side of the three triangle side, by ccw arrow - return OnLeftSideOfLine2D(p0, p1, pp) && OnLeftSideOfLine2D(p1, p2, pp) && OnLeftSideOfLine2D(p2, p0, pp); + int c1 = OnLeftSideOfLine2D(p0, p1, pp); + int c2 = OnLeftSideOfLine2D(p1, p2, pp); + int c3 = OnLeftSideOfLine2D(p2, p0, pp); + return (c1 >= 0) && (c2 >= 0) && (c3 >= 0); } @@ -110,7 +119,7 @@ inline bool IsCCW(T* in, size_t npoints) { c = std::sqrt(cc); theta = std::acos((bb + cc - aa) / (2 * b * c)); - if (OnLeftSideOfLine2D(in[i],in[i+2],in[i+1])) { + if (OnLeftSideOfLine2D(in[i],in[i+2],in[i+1]) == 1) { // if (convex(in[i].x, in[i].y, // in[i+1].x, in[i+1].y, // in[i+2].x, in[i+2].y)) { @@ -140,7 +149,7 @@ inline bool IsCCW(T* in, size_t npoints) { //if (convex(in[npoints-2].x, in[npoints-2].y, // in[0].x, in[0].y, // in[1].x, in[1].y)) { - if (OnLeftSideOfLine2D(in[npoints-2],in[1],in[0])) { + if (OnLeftSideOfLine2D(in[npoints-2],in[1],in[0]) == 1) { convex_turn = AI_MATH_PI_F - theta; convex_sum += convex_turn; } else { diff --git a/code/PostProcessing/TriangulateProcess.cpp b/code/PostProcessing/TriangulateProcess.cpp index b4c66b41e..f16de2196 100644 --- a/code/PostProcessing/TriangulateProcess.cpp +++ b/code/PostProcessing/TriangulateProcess.cpp @@ -451,7 +451,7 @@ bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh) { *pnt2 = &temp_verts[next]; // Must be a convex point. Assuming ccw winding, it must be on the right of the line between p-1 and p+1. - if (OnLeftSideOfLine2D(*pnt0,*pnt2,*pnt1)) { + if (OnLeftSideOfLine2D(*pnt0,*pnt2,*pnt1) == 1) { continue; }