Lagrange
orient_outward.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
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>
19
20#include <Eigen/Core>
21
22namespace lagrange {
23LAGRANGE_LEGACY_INLINE
24namespace legacy {
25
35template <typename VertexArray, typename FacetArray>
36void orient_outward(lagrange::Mesh<VertexArray, FacetArray>& mesh, bool positive = true)
37{
38 using Scalar = typename VertexArray::Scalar;
39 using Index = typename FacetArray::Scalar;
40 using RowVector3r = Eigen::Matrix<Scalar, 1, 3>;
41
42 if (mesh.get_num_facets() == 0) {
43 // TODO: Fix igl::bfs_orient to work on empty meshes.
44 return;
45 }
46
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;
52 };
53
54 auto component_signed_volumes = [&](const auto& vertices,
55 const auto& facets,
56 const auto& components,
57 auto& signed_volumes) {
58 la_runtime_assert(vertices.cols() == 3);
59 la_runtime_assert(facets.cols() == 3);
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));
67 }
68 double vol = tetra_signed_volume(t[0], t[1], t[2], t[3]);
69 signed_volumes(components(f)) -= vol;
70 }
71 };
72
73 const auto& vertices = mesh.get_vertices();
74 FacetArray facets;
75 mesh.export_facets(facets);
76
77 // Orient connected components on the surface
78 Eigen::Matrix<Index, Eigen::Dynamic, 1> components;
79 lagrange::internal::bfs_orient(facets, facets, components);
80
81 // Signed volumes per components
82 Eigen::Matrix<Scalar, Eigen::Dynamic, 1> signed_volumes;
83 component_signed_volumes(vertices, facets, components, signed_volumes);
84
85 // Flip facets in each compoenent with incorrect volume sign
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();
89 }
90 }
91
92 mesh.import_facets(facets);
93}
94
95} // namespace legacy
96} // namespace lagrange
Definition: Mesh.h:48
#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