Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
add normalize normals method
Browse files Browse the repository at this point in the history
  • Loading branch information
dwastberg committed Sep 17, 2024
1 parent 28a0ca7 commit 57f9c0a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/cpp/include/MeshBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ class MeshBuilder
// multimesh.push_back(surface_mesh);
// }
auto mesh = MeshProcessor::merge_meshes(multimesh, weld);
mesh.normalize_normal_direction();
return mesh;
}

Expand Down
6 changes: 4 additions & 2 deletions src/cpp/include/Triangulate.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ static void call_triangle(Mesh &mesh,
if (surface.vertices.size() == 3 && !has_holes)
{
mesh.vertices = surface.vertices;
mesh.faces.push_back(Simplex2D(0, 1, 2));
mesh.faces.push_back(Simplex2D(0, 1, 2, false));
mesh.sort_vertices();
return;
}

Expand All @@ -143,8 +144,9 @@ static void call_triangle(Mesh &mesh,
mesh.vertices = surface.vertices;
for (size_t i = 1; i < surface.vertices.size() - 1; i++)
{
mesh.faces.push_back(Simplex2D(0, i, i + 1));
mesh.faces.push_back(Simplex2D(0, i, i + 1, false));
}
mesh.sort_vertices();
}
else
{
Expand Down
59 changes: 59 additions & 0 deletions src/cpp/include/model/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,71 @@ class Mesh : public Printable
return c;
}

Vector3D centroid() const
{
Vector3D c{};
for (auto &v : vertices)
{
c += Vector3D(v);
}
c /= vertices.size();
return c;
}

/// sort vertices of each face
void sort_vertices(bool ccw = true)
{
for (auto &face : faces)
{
// Get the vertices of the face
auto v0 = vertices[face.v0];
auto v1 = vertices[face.v1];
auto v2 = vertices[face.v2];
auto normal = (v1 - v0).cross(v2 - v0);
// Check if the normal vector is pointing in the desired direction
bool swap = ccw ? normal.z < 0 : normal.z > 0;
if (swap) // Assuming z-axis is up
{
// Swap v1 and v2 to change the orientation
std::swap(face.v1, face.v2);
}
}
}

void normalize_normal_direction(bool outwards = true)
{
for (auto &face : faces)
{
auto centroid = this->centroid();
auto v0 = vertices[face.v0];
auto v1 = vertices[face.v1];
auto v2 = vertices[face.v2];
auto normal = (v1 - v0).cross(v2 - v0);
// Calculate the vector from the centroid to the face
auto to_face = (v0 + v1 + v2) / 3.0 - centroid;
bool swap = outwards ? normal.dot(to_face) < 0 : normal.dot(to_face) > 0;
if (swap)
{
// Swap v1 and v2 to change the orientation
std::swap(face.v1, face.v2);
}
}
}







/// Pretty-print
std::string __str__() const override
{
return "Mesh with " + str(vertices.size()) + " vertices and " +
str(faces.size()) + " faces";
}


};

} // namespace DTCC_BUILDER
Expand Down

0 comments on commit 57f9c0a

Please sign in to comment.