14#include <lagrange/Mesh.h>
15#include <lagrange/internal/bfs_orient.h>
16#include <lagrange/legacy/inline.h>
17#include <lagrange/utils/assert.h>
18#include <lagrange/utils/safe_cast.h>
35template <
typename VertexArray,
typename FacetArray>
38 using Scalar =
typename VertexArray::Scalar;
39 using Index =
typename FacetArray::Scalar;
40 using RowVector3r = Eigen::Matrix<Scalar, 1, 3>;
42 if (mesh.get_num_facets() == 0) {
47 auto tetra_signed_volume = [](
const RowVector3r& p1,
48 const RowVector3r& p2,
49 const RowVector3r& p3,
50 const RowVector3r& p4) {
51 return (p2 - p1).dot((p3 - p1).cross(p4 - p1)) / 6.0;
54 auto component_signed_volumes = [&](
const auto& vertices,
56 const auto& components,
57 auto& signed_volumes) {
60 std::array<RowVector3r, 4> t;
61 t[3] = RowVector3r::Zero(vertices.cols());
62 signed_volumes.resize(components.maxCoeff() + 1);
63 signed_volumes.setZero();
64 for (Eigen::Index f = 0; f < facets.rows(); ++f) {
65 for (Eigen::Index lv = 0; lv < facets.cols(); ++lv) {
66 t[lv] = vertices.row(facets(f, lv));
68 double vol = tetra_signed_volume(t[0], t[1], t[2], t[3]);
69 signed_volumes(components(f)) -= vol;
73 const auto& vertices = mesh.get_vertices();
75 mesh.export_facets(facets);
78 Eigen::Matrix<Index, Eigen::Dynamic, 1> components;
79 lagrange::internal::bfs_orient(facets, facets, components);
82 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> signed_volumes;
83 component_signed_volumes(vertices, facets, components, signed_volumes);
86 for (Eigen::Index f = 0; f < facets.rows(); ++f) {
87 if ((positive ? 1 : -1) * signed_volumes(components(f)) < 0) {
88 facets.row(f) = facets.row(f).reverse().eval();
92 mesh.import_facets(facets);
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30
void orient_outward(lagrange::SurfaceMesh< Scalar, Index > &mesh, const OrientOptions &options={})
Orient the facets of a mesh so that the signed volume of each connected component is positive or nega...
Definition: orient_outward.cpp:126