diff options
Diffstat (limited to 'modules/csg/csg.cpp')
-rw-r--r-- | modules/csg/csg.cpp | 69 |
1 files changed, 0 insertions, 69 deletions
diff --git a/modules/csg/csg.cpp b/modules/csg/csg.cpp index a6951a9320..ccd1b89fb5 100644 --- a/modules/csg/csg.cpp +++ b/modules/csg/csg.cpp @@ -37,12 +37,10 @@ // Static helper functions. inline static bool is_snapable(const Vector3 &p_point1, const Vector3 &p_point2, real_t p_distance) { - return (p_point1 - p_point2).length_squared() < p_distance * p_distance; } inline static Vector2 interpolate_segment_uv(const Vector2 p_segement_points[2], const Vector2 p_uvs[2], const Vector2 &p_interpolation_point) { - float segment_length = (p_segement_points[1] - p_segement_points[0]).length(); if (segment_length < CMP_EPSILON) return p_uvs[0]; @@ -54,7 +52,6 @@ inline static Vector2 interpolate_segment_uv(const Vector2 p_segement_points[2], } inline static Vector2 interpolate_triangle_uv(const Vector2 p_vertices[3], const Vector2 p_uvs[3], const Vector2 &p_interpolation_point) { - if (p_interpolation_point.distance_squared_to(p_vertices[0]) < CMP_EPSILON2) return p_uvs[0]; if (p_interpolation_point.distance_squared_to(p_vertices[1]) < CMP_EPSILON2) @@ -83,7 +80,6 @@ inline static Vector2 interpolate_triangle_uv(const Vector2 p_vertices[3], const } static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 &p_dir, const Vector3 p_vertices[3], float p_tolerance, Vector3 &r_intersection_point) { - Vector3 edge1 = p_vertices[1] - p_vertices[0]; Vector3 edge2 = p_vertices[2] - p_vertices[0]; Vector3 h = p_dir.cross(edge2); @@ -115,7 +111,6 @@ static inline bool ray_intersects_triangle(const Vector3 &p_from, const Vector3 } inline bool is_point_in_triangle(const Vector3 &p_point, const Vector3 p_vertices[3], int p_shifted = 0) { - real_t det = p_vertices[0].dot(p_vertices[1].cross(p_vertices[2])); // If determinant is, zero try shift the triangle and the point. @@ -149,7 +144,6 @@ inline bool is_point_in_triangle(const Vector3 &p_point, const Vector3 p_vertice } 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]; real_t segment1_length2 = segment1.dot(segment1); @@ -172,7 +166,6 @@ inline static bool are_segements_parallel(const Vector2 p_segment1_points[2], co // CSGBrush void CSGBrush::_regen_face_aabbs() { - for (int i = 0; i < faces.size(); i++) { faces.write[i].aabb = AABB(); faces.write[i].aabb.position = faces[i].vertices[0]; @@ -182,7 +175,6 @@ void CSGBrush::_regen_face_aabbs() { } void CSGBrush::build_from_faces(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs, const Vector<bool> &p_smooth, const Vector<Ref<Material>> &p_materials, const Vector<bool> &p_invert_faces) { - faces.clear(); int vc = p_vertices.size(); @@ -204,7 +196,6 @@ void CSGBrush::build_from_faces(const Vector<Vector3> &p_vertices, const Vector< faces.resize(p_vertices.size() / 3); for (int i = 0; i < faces.size(); i++) { - Face &f = faces.write[i]; f.vertices[0] = rv[i * 3 + 0]; f.vertices[1] = rv[i * 3 + 1]; @@ -227,10 +218,8 @@ void CSGBrush::build_from_faces(const Vector<Vector3> &p_vertices, const Vector< f.invert = false; if (mc == vc / 3) { - Ref<Material> mat = rm[i]; if (mat.is_valid()) { - const Map<Ref<Material>, int>::Element *E = material_map.find(mat); if (E) { @@ -255,7 +244,6 @@ void CSGBrush::build_from_faces(const Vector<Vector3> &p_vertices, const Vector< } void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform &p_xform) { - faces = p_brush.faces; materials = p_brush.materials; @@ -271,7 +259,6 @@ void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform &p_xform) { // CSGBrushOperation void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_brush_a, const CSGBrush &p_brush_b, CSGBrush &r_merged_brush, float p_vertex_snap) { - // Check for face collisions and add necessary faces. Build2DFaceCollection build2DFaceCollection; for (int i = 0; i < p_brush_a.faces.size(); i++) { @@ -287,7 +274,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b mesh_merge.vertex_snap = p_vertex_snap; for (int i = 0; i < p_brush_a.faces.size(); i++) { - Ref<Material> material; if (p_brush_a.faces[i].material != -1) { material = p_brush_a.materials[p_brush_a.faces[i].material]; @@ -307,7 +293,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b } for (int i = 0; i < p_brush_b.faces.size(); i++) { - Ref<Material> material; if (p_brush_b.faces[i].material != -1) { material = p_brush_b.materials[p_brush_b.faces[i].material]; @@ -333,9 +318,7 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b r_merged_brush.faces.clear(); switch (p_operation) { - case OPERATION_UNION: { - int outside_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { @@ -349,7 +332,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b outside_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { - if (mesh_merge.faces[i].inside) continue; @@ -369,7 +351,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b } break; case OPERATION_INTERSECTION: { - int inside_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { @@ -383,7 +364,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b inside_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { - if (!mesh_merge.faces[i].inside) continue; @@ -403,7 +383,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b } break; case OPERATION_SUBSTRACTION: { - int face_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { @@ -419,7 +398,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b face_count = 0; for (int i = 0; i < mesh_merge.faces.size(); i++) { - if (mesh_merge.faces[i].from_b && !mesh_merge.faces[i].inside) continue; if (!mesh_merge.faces[i].from_b && mesh_merge.faces[i].inside) @@ -460,7 +438,6 @@ void CSGBrushOperation::merge_brushes(Operation p_operation, const CSGBrush &p_b #define BVH_LIMIT 8 int CSGBrushOperation::MeshMerge::_create_bvh(FaceBVH *facebvhptr, FaceBVH **facebvhptrptr, int p_from, int p_size, int p_depth, int &r_max_depth, int &r_max_alloc) { - if (p_depth > r_max_depth) { r_max_depth = p_depth; } @@ -485,7 +462,6 @@ int CSGBrushOperation::MeshMerge::_create_bvh(FaceBVH *facebvhptr, FaceBVH **fac int li = aabb.get_longest_axis_index(); switch (li) { - case Vector3::AXIS_X: { SortArray<FaceBVH *, FaceBVHCmpX> sort_x; sort_x.nth_element(0, p_size, p_size / 2, &facebvhptrptr[p_from]); @@ -521,7 +497,6 @@ int CSGBrushOperation::MeshMerge::_create_bvh(FaceBVH *facebvhptr, FaceBVH **fac } void CSGBrushOperation::MeshMerge::_add_distance(List<real_t> &r_intersectionsA, List<real_t> &r_intersectionsB, bool p_from_B, real_t p_distance) const { - List<real_t> &intersections = p_from_B ? r_intersectionsB : r_intersectionsA; // Check if distance exists. @@ -533,7 +508,6 @@ void CSGBrushOperation::MeshMerge::_add_distance(List<real_t> &r_intersectionsA, } bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_depth, int p_bvh_first, int p_face_idx) const { - Face face = faces[p_face_idx]; Vector3 face_points[3] = { points[face.points[0]], @@ -563,22 +537,16 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de stack[0] = pos; while (true) { - uint32_t node = stack[level] & NODE_IDX_MASK; const FaceBVH *current_facebvhptr = &(facebvhptr[node]); bool done = false; switch (stack[level] >> VISITED_BIT_SHIFT) { - case TEST_AABB_BIT: { - if (current_facebvhptr->face >= 0) { - while (current_facebvhptr) { - if (p_face_idx != current_facebvhptr->face && current_facebvhptr->aabb.intersects_ray(face_center, face_normal)) { - const Face ¤t_face = faces[current_facebvhptr->face]; Vector3 current_points[3] = { points[current_face.points[0]], @@ -610,7 +578,6 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; } else { - bool valid = current_facebvhptr->aabb.intersects_ray(face_center, face_normal); if (!valid) { @@ -623,7 +590,6 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de } case VISIT_LEFT_BIT: { - stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = current_facebvhptr->left | TEST_AABB_BIT; level++; @@ -631,7 +597,6 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de } case VISIT_RIGHT_BIT: { - stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node; stack[level + 1] = current_facebvhptr->right | TEST_AABB_BIT; level++; @@ -639,7 +604,6 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de } case VISIT_DONE_BIT: { - if (level == 0) { done = true; break; @@ -658,7 +622,6 @@ bool CSGBrushOperation::MeshMerge::_bvh_inside(FaceBVH *facebvhptr, int p_max_de } void CSGBrushOperation::MeshMerge::mark_inside_faces() { - // Mark faces that are inside. This helps later do the boolean ops when merging. // This approach is very brute force with a bunch of optimizations, // such as BVH and pre AABB intersection test. @@ -719,7 +682,6 @@ void CSGBrushOperation::MeshMerge::mark_inside_faces() { _create_bvh(facebvh, bvhptr, 0, faces.size(), 1, max_depth, max_alloc); for (int i = 0; i < faces.size(); i++) { - // Check if face AABB intersects the intersection AABB. if (!intersection_aabb.intersects_inclusive(facebvh[i].aabb)) continue; @@ -730,10 +692,8 @@ void CSGBrushOperation::MeshMerge::mark_inside_faces() { } void CSGBrushOperation::MeshMerge::add_face(const Vector3 p_points[], const Vector2 p_uvs[], bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b) { - int indices[3]; for (int i = 0; i < 3; i++) { - VertexKey vk; vk.x = int((double(p_points[i].x) + double(vertex_snap) * 0.31234) / double(vertex_snap)); vk.y = int((double(p_points[i].y) + double(vertex_snap) * 0.31234) / double(vertex_snap)); @@ -781,7 +741,6 @@ void CSGBrushOperation::MeshMerge::add_face(const Vector3 p_points[], const Vect // CSGBrushOperation::Build2DFaces int CSGBrushOperation::Build2DFaces::_get_point_idx(const Vector2 &p_point) { - for (int vertex_idx = 0; vertex_idx < vertices.size(); ++vertex_idx) { if ((p_point - vertices[vertex_idx].point).length_squared() < vertex_snap2) return vertex_idx; @@ -790,7 +749,6 @@ int CSGBrushOperation::Build2DFaces::_get_point_idx(const Vector2 &p_point) { } int CSGBrushOperation::Build2DFaces::_add_vertex(const Vertex2D &p_vertex) { - // Check if vertex exists. int vertex_id = _get_point_idx(p_vertex.point); if (vertex_id != -1) @@ -801,7 +759,6 @@ int CSGBrushOperation::Build2DFaces::_add_vertex(const Vertex2D &p_vertex) { } void CSGBrushOperation::Build2DFaces::_add_vertex_idx_sorted(Vector<int> &r_vertex_indices, int p_new_vertex_index) { - if (p_new_vertex_index >= 0 && r_vertex_indices.find(p_new_vertex_index) == -1) { ERR_FAIL_COND_MSG(p_new_vertex_index >= vertices.size(), "Invalid vertex index."); @@ -814,7 +771,6 @@ void CSGBrushOperation::Build2DFaces::_add_vertex_idx_sorted(Vector<int> &r_vert // The second vertex. if (r_vertex_indices.size() == 1) { - Vector2 first_point = vertices[r_vertex_indices[0]].point; Vector2 new_point = vertices[p_new_vertex_index].point; @@ -857,14 +813,12 @@ void CSGBrushOperation::Build2DFaces::_add_vertex_idx_sorted(Vector<int> &r_vert } void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_indices) { - int segments = p_segment_indices.size() - 1; if (segments < 2) return; // Faces around an inner vertex are merged by moving the inner vertex to the first vertex. for (int sorted_idx = 1; sorted_idx < segments; ++sorted_idx) { - int closest_idx = 0; int inner_idx = p_segment_indices[sorted_idx]; @@ -893,7 +847,6 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ // Create the new faces. for (int merge_idx = 0; merge_idx < merge_faces.size(); ++merge_idx) { - int outer_edge_idx[2]; outer_edge_idx[0] = merge_faces[merge_idx].vertex_idx[(merge_faces_inner_vertex_idx[merge_idx] + 1) % 3]; outer_edge_idx[1] = merge_faces[merge_idx].vertex_idx[(merge_faces_inner_vertex_idx[merge_idx] + 2) % 3]; @@ -937,7 +890,6 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ // Split faces using degenerate points. for (int face_idx = 0; face_idx < faces.size(); ++face_idx) { - Face2D face = faces[face_idx]; Vertex2D face_vertices[3] = { vertices[face.vertex_idx[0]], @@ -951,7 +903,6 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ }; for (int point_idx = 0; point_idx < degenerate_points.size(); ++point_idx) { - int degenerate_idx = degenerate_points[point_idx]; Vector2 point_2D = vertices[degenerate_idx].point; @@ -968,7 +919,6 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ // Check if point is on an each edge. for (int face_edge_idx = 0; face_edge_idx < 3; ++face_edge_idx) { - Vector2 edge_points[2] = { face_points[face_edge_idx], face_points[(face_edge_idx + 1) % 3] @@ -976,7 +926,6 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ Vector2 closest_point = Geometry::get_closest_point_to_segment_2d(point_2D, edge_points); if ((closest_point - point_2D).length_squared() < vertex_snap2) { - int opposite_vertex_idx = face.vertex_idx[(face_edge_idx + 2) % 3]; // If new vertex snaps to degenerate vertex, just delete this face. @@ -1014,10 +963,8 @@ void CSGBrushOperation::Build2DFaces::_merge_faces(const Vector<int> &p_segment_ } void CSGBrushOperation::Build2DFaces::_find_edge_intersections(const Vector2 p_segment_points[2], Vector<int> &r_segment_indices) { - // For each face. for (int face_idx = 0; face_idx < faces.size(); ++face_idx) { - Face2D face = faces[face_idx]; Vertex2D face_vertices[3] = { vertices[face.vertex_idx[0]], @@ -1027,7 +974,6 @@ void CSGBrushOperation::Build2DFaces::_find_edge_intersections(const Vector2 p_s // Check each edge. for (int face_edge_idx = 0; face_edge_idx < 3; ++face_edge_idx) { - Vector2 edge_points[2] = { face_vertices[face_edge_idx].point, face_vertices[(face_edge_idx + 1) % 3].point @@ -1050,7 +996,6 @@ void CSGBrushOperation::Build2DFaces::_find_edge_intersections(const Vector2 p_s // Else check if the segment intersects the edge. if (on_edge || Geometry::segment_intersects_segment_2d(p_segment_points[0], p_segment_points[1], edge_points[0], edge_points[1], &intersection_point)) { - // Check if intersection point is an edge point. if ((intersection_point - edge_points[0]).length_squared() < vertex_snap2 || (intersection_point - edge_points[1]).length_squared() < vertex_snap2) @@ -1113,11 +1058,9 @@ void CSGBrushOperation::Build2DFaces::_find_edge_intersections(const Vector2 p_s } int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { - int new_vertex_idx = -1; for (int face_idx = 0; face_idx < faces.size(); ++face_idx) { - Face2D face = faces[face_idx]; Vertex2D face_vertices[3] = { vertices[face.vertex_idx[0]], @@ -1144,7 +1087,6 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { // Check if point is on an each edge. bool on_edge = false; for (int face_edge_idx = 0; face_edge_idx < 3; ++face_edge_idx) { - Vector2 edge_points[2] = { points[face_edge_idx], points[(face_edge_idx + 1) % 3] @@ -1206,7 +1148,6 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { // If not on an edge, check if the point is inside the face. if (!on_edge && Geometry::is_point_in_triangle(p_point, face_vertices[0].point, face_vertices[1].point, face_vertices[2].point)) { - // Add the point as a new vertex. Vertex2D new_vertex; new_vertex.point = p_point; @@ -1216,7 +1157,6 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { // Create three new faces around this point and remove this face. // 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] }; @@ -1243,7 +1183,6 @@ int CSGBrushOperation::Build2DFaces::_insert_point(const Vector2 &p_point) { } void CSGBrushOperation::Build2DFaces::insert(const CSGBrush &p_brush, int p_face_idx) { - // Find edge points that cross the plane and face points that are in the plane. // Map those points to 2D. // Create new faces from those points. @@ -1252,7 +1191,6 @@ void CSGBrushOperation::Build2DFaces::insert(const CSGBrush &p_brush, int p_face int points_count = 0; for (int i = 0; i < 3; i++) { - Vector3 point_3D = p_brush.faces[p_face_idx].vertices[i]; if (plane.has_point(point_3D)) { @@ -1262,7 +1200,6 @@ void CSGBrushOperation::Build2DFaces::insert(const CSGBrush &p_brush, int p_face points_2D[points_count++] = Vector2(point_2D.x, point_2D.y); } else { - Vector3 next_point_3D = p_brush.faces[p_face_idx].vertices[(i + 1) % 3]; if (plane.has_point(next_point_3D)) @@ -1315,7 +1252,6 @@ void CSGBrushOperation::Build2DFaces::insert(const CSGBrush &p_brush, int p_face } void CSGBrushOperation::Build2DFaces::addFacesToMesh(MeshMerge &r_mesh_merge, bool p_smooth, bool p_invert, const Ref<Material> &p_material, bool p_from_b) { - for (int face_idx = 0; face_idx < faces.size(); ++face_idx) { Face2D face = faces[face_idx]; Vertex2D fv[3] = { @@ -1339,7 +1275,6 @@ void CSGBrushOperation::Build2DFaces::addFacesToMesh(MeshMerge &r_mesh_merge, bo CSGBrushOperation::Build2DFaces::Build2DFaces(const CSGBrush &p_brush, int p_face_idx, float p_vertex_snap2) : vertex_snap2(p_vertex_snap2 * p_vertex_snap2) { - // Convert 3D vertex points to 2D. Vector3 points_3D[3] = { p_brush.faces[p_face_idx].vertices[0], @@ -1368,7 +1303,6 @@ CSGBrushOperation::Build2DFaces::Build2DFaces(const CSGBrush &p_brush, int p_fac } void CSGBrushOperation::update_faces(const CSGBrush &p_brush_a, const int p_face_idx_a, const CSGBrush &p_brush_b, const int p_face_idx_b, Build2DFaceCollection &p_collection, float p_vertex_snap) { - Vector3 vertices_a[3] = { p_brush_a.faces[p_face_idx_a].vertices[0], p_brush_a.faces[p_face_idx_a].vertices[1], @@ -1437,14 +1371,11 @@ void CSGBrushOperation::update_faces(const CSGBrush &p_brush_a, const int p_face // Check for intersection using the SAT theorem. { - // Edge pair cross product combinations. for (int i = 0; i < 3; i++) { - Vector3 axis_a = (vertices_a[i] - vertices_a[(i + 1) % 3]).normalized(); for (int j = 0; j < 3; j++) { - Vector3 axis_b = (vertices_b[j] - vertices_b[(j + 1) % 3]).normalized(); Vector3 sep_axis = axis_a.cross(axis_b); |