Don't attempt to insert points into degenerate triangles.

Use a simpler degenerate triangle check that calculates area.

(cherry picked from commit 3521239d96)
This commit is contained in:
Marcel Admiraal 2020-08-13 16:07:59 +01:00 committed by Rémi Verschelde
parent aa0221f0fb
commit 808865af97
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 15 additions and 5 deletions

View File

@ -146,6 +146,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];
@ -1116,6 +1124,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)
@ -1199,11 +1212,8 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) {
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;
}