Merge pull request #41229 from madmiraal/fix-csg

Don't attempt to insert points into degenerate triangles when creating CSG shapes.
This commit is contained in:
Rémi Verschelde 2020-08-13 23:19:49 +02:00 committed by GitHub
commit ef607b0137
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}