summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-08-13 23:19:49 +0200
committerGitHub <noreply@github.com>2020-08-13 23:19:49 +0200
commitef607b01377b26b228a949a70fa786f1e005a850 (patch)
tree5b46db8fa65b6dc9a6cf7703c1560c5ac0101c52 /modules
parentd84b28b94f26db0ea115baf92662b09e6d6db67b (diff)
parent3521239d96988194722bc8f18a87d3d84718d9f8 (diff)
Merge pull request #41229 from madmiraal/fix-csg
Don't attempt to insert points into degenerate triangles when creating CSG shapes.
Diffstat (limited to 'modules')
-rw-r--r--modules/csg/csg.cpp20
1 files changed, 15 insertions, 5 deletions
diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp
index b270e04c63..47982d519a 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;
}