From 3521239d96988194722bc8f18a87d3d84718d9f8 Mon Sep 17 00:00:00 2001 From: Marcel Admiraal Date: Thu, 13 Aug 2020 16:07:59 +0100 Subject: [PATCH] Don't attempt to insert points into degenerate triangles. Use a simpler degenerate triangle check that calculates area. --- modules/csg/csg.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp index b270e04c639..47982d519a8 100644 --- a/modules/csg/csg.cpp +++ b/modules/csg/csg.cpp @@ -154,6 +154,14 @@ inline bool is_point_in_triangle(const Vector3 &p_point, const Vector3 p_vertice return true; } +inline static bool is_triangle_degenerate(const Vector2 p_vertices[3], real_t p_vertex_snap2) { + real_t det = p_vertices[0].x * p_vertices[1].y - p_vertices[0].x * p_vertices[2].y + + p_vertices[0].y * p_vertices[2].x - p_vertices[0].y * p_vertices[1].x + + p_vertices[1].x * p_vertices[2].y - p_vertices[1].y * p_vertices[2].x; + + return det < p_vertex_snap2; +} + inline static bool are_segements_parallel(const Vector2 p_segment1_points[2], const Vector2 p_segment2_points[2], float p_vertex_snap2) { Vector2 segment1 = p_segment1_points[1] - p_segment1_points[0]; Vector2 segment2 = p_segment2_points[1] - p_segment2_points[0]; @@ -1117,6 +1125,11 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { face_vertices[2].uv }; + // Skip degenerate triangles. + if (is_triangle_degenerate(points, vertex_snap2)) { + continue; + } + // Check if point is existing face vertex. for (int i = 0; i < 3; ++i) { if ((p_point - face_vertices[i].point).length_squared() < vertex_snap2) { @@ -1198,11 +1211,8 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { // The new vertex is the last vertex. for (int i = 0; i < 3; ++i) { // Don't create degenerate triangles. - Vector2 edge[2] = { points[i], points[(i + 1) % 3] }; - Vector2 new_edge1[2] = { vertices[new_vertex_idx].point, points[i] }; - Vector2 new_edge2[2] = { vertices[new_vertex_idx].point, points[(i + 1) % 3] }; - if (are_segements_parallel(edge, new_edge1, vertex_snap2) && - are_segements_parallel(edge, new_edge2, vertex_snap2)) { + Vector2 new_points[3] = { points[i], points[(i + 1) % 3], vertices[new_vertex_idx].point }; + if (is_triangle_degenerate(new_points, vertex_snap2)) { continue; }