Wildmeshing Toolkit
winding_number.cpp
Go to the documentation of this file.
1 #include "winding_number.hpp"
2 
3 #include <wmtk/EdgeMesh.hpp>
4 #include <wmtk/TetMesh.hpp>
5 #include <wmtk/TriMesh.hpp>
7 #include <wmtk/utils/Logger.hpp>
9 
10 #include <wmtk/utils/Rational.hpp>
11 
14 
15 namespace wmtk::components {
16 
17 std::shared_ptr<Mesh> winding_number(
18  const std::shared_ptr<Mesh>& query_mesh_ptr,
19  const std::shared_ptr<Mesh>& surface_ptr)
20 {
21  using namespace internal;
22 
23  TriMesh& surface_mesh = static_cast<TriMesh&>(*surface_ptr);
24 
25  // compute winding number
26  auto winding_numbers = winding_number(*query_mesh_ptr, surface_mesh);
27 
28  wmtk::logger().info("winding number computed!");
29 
30  // register winding number to tets
31  auto winding_number_handle = query_mesh_ptr->register_attribute<double>(
32  "winding_number",
33  query_mesh_ptr->top_simplex_type(),
34  1);
35  auto winding_number_accessor = query_mesh_ptr->create_accessor<double>(winding_number_handle);
36 
37  const auto& top_simplex_tuples = query_mesh_ptr->get_all(query_mesh_ptr->top_simplex_type());
38 
39  for (int64_t i = 0; i < top_simplex_tuples.size(); ++i) {
40  winding_number_accessor.scalar_attribute(top_simplex_tuples[i]) = winding_numbers[i];
41  }
42 
43  return query_mesh_ptr;
44 
45  // get the matrix
46  // wmtk::utils::EigenMatrixWriter writer;
47  // mesh->serialize(writer);
48  // Eigen::MatrixX<Rational> mesh_pos_rational;
49  // Eigen::MatrixXd mesh_pos;
50  // MatrixX<int64_t> mesh_FV;
51 
52  // writer.get_position_matrix(mesh_pos_rational);
53  // mesh_pos.resize(mesh_pos_rational.rows(), mesh_pos_rational.cols());
54 
55  // mesh_pos = mesh_pos_rational.cast<double>();
56 
57  // switch (mesh->top_simplex_type()) {
58  // case (PrimitiveType::Tetrahedron): {
59  // writer.get_TV_matrix(mesh_FV);
60  // break;
61  // }
62  // case (PrimitiveType::Triangle): {
63  // writer.get_FV_matrix(mesh_FV);
64  // break;
65  // }
66  // case (PrimitiveType::Edge): {
67  // writer.get_EV_matrix(mesh_FV);
68  // break;
69  // }
70  // default: throw std::runtime_error("Unsupported Mesh Type");
71  // }
72 
73  // // construct the inside and outside mesh
74  // std::map<int64_t, int64_t> v_map_inside, v_map_outside;
75  // std::vector<Eigen::Vector3d> v_inside, v_outside;
76  // std::vector<std::vector<int64_t>> fv_inside, fv_outside;
77 
78  // std::cout << "here" << std::endl;
79 
80  // for (int64_t i = 0; i < mesh_FV.rows(); ++i) {
81  // if (winding_numbers[i] > options.threshold) {
82  // // outside
83  // std::vector<int64_t> row;
84  // for (int64_t j = 0; j < mesh_FV.cols(); ++j) {
85  // if (v_map_outside.find(mesh_FV(i, j)) == v_map_outside.end()) {
86  // v_map_outside[mesh_FV(i, j)] = v_outside.size();
87  // Eigen::Vector3d p = mesh_pos.row(mesh_FV(i, j));
88  // v_outside.emplace_back(p); // map v to new vids
89  // }
90  // row.emplace_back(v_map_outside[mesh_FV(i, j)]); // update the entry of FV
91  // }
92  // fv_outside.push_back(row);
93  // } else {
94  // // inside
95  // std::vector<int64_t> row;
96  // for (int64_t j = 0; j < mesh_FV.cols(); ++j) {
97  // if (v_map_inside.find(mesh_FV(i, j)) == v_map_inside.end()) {
98  // v_map_inside[mesh_FV(i, j)] = v_inside.size();
99  // Eigen::Vector3d p = mesh_pos.row(mesh_FV(i, j));
100  // v_inside.emplace_back(p); // map v to new vids
101  // }
102  // row.emplace_back(v_map_inside[mesh_FV(i, j)]); // update the entry of FV
103  // }
104  // fv_inside.push_back(row);
105  // }
106  // }
107 
108  // RowVectors3d V_outside(v_outside.size(), 3), V_inside(v_inside.size(), 3);
109  // MatrixX<int64_t> FV_outside(fv_outside.size(), mesh_FV.cols()),
110  // FV_inside(fv_inside.size(), mesh_FV.cols());
111 
112  // for (int64_t i = 0; i < v_inside.size(); ++i) {
113  // V_inside.row(i) = v_inside[i];
114  // }
115  // for (int64_t i = 0; i < v_outside.size(); ++i) {
116  // V_outside.row(i) = v_outside[i];
117  // }
118  // for (int64_t i = 0; i < fv_inside.size(); ++i) {
119  // for (int64_t j = 0; j < mesh_FV.cols(); ++j) {
120  // FV_inside(i, j) = fv_inside[i][j];
121  // }
122  // }
123  // for (int64_t i = 0; i < fv_outside.size(); ++i) {
124  // for (int64_t j = 0; j < mesh_FV.cols(); ++j) {
125  // FV_outside(i, j) = fv_outside[i][j];
126  // }
127  // }
128 
129  // wmtk::logger().info("inside and outside mesh constructed");
130 
131  // switch (mesh->top_simplex_type()) {
132  // case (PrimitiveType::Tetrahedron): {
133  // std::shared_ptr<TetMesh> m_out = std::make_shared<wmtk::TetMesh>();
134  // std::shared_ptr<TetMesh> m_in = std::make_shared<wmtk::TetMesh>();
135  // m_out->initialize(FV_outside);
136  // m_in->initialize(FV_inside);
137  // mesh_utils::set_matrix_attribute(V_outside, "vertices", PrimitiveType::Vertex, *m_out);
138  // mesh_utils::set_matrix_attribute(V_inside, "vertices", PrimitiveType::Vertex, *m_in);
139  // cache.write_mesh(*m_in, options.output + "_inside");
140  // cache.write_mesh(*m_out, options.output + "_outside");
141  // break;
142  // }
143  // case (PrimitiveType::Triangle): {
144  // std::shared_ptr<TriMesh> m_out = std::make_shared<wmtk::TriMesh>();
145  // std::shared_ptr<TriMesh> m_in = std::make_shared<wmtk::TriMesh>();
146  // m_out->initialize(FV_outside);
147  // m_in->initialize(FV_inside);
148  // mesh_utils::set_matrix_attribute(V_outside, "vertices", PrimitiveType::Vertex, *m_out);
149  // mesh_utils::set_matrix_attribute(V_inside, "vertices", PrimitiveType::Vertex, *m_in);
150  // cache.write_mesh(*m_in, options.output + "_inside");
151  // cache.write_mesh(*m_out, options.output + "_outside");
152  // break;
153  // }
154  // case (PrimitiveType::Edge): {
155  // std::shared_ptr<EdgeMesh> m_out = std::make_shared<wmtk::EdgeMesh>();
156  // std::shared_ptr<EdgeMesh> m_in = std::make_shared<wmtk::EdgeMesh>();
157  // m_out->initialize(FV_outside);
158  // m_in->initialize(FV_inside);
159  // mesh_utils::set_matrix_attribute(V_outside, "vertices", PrimitiveType::Vertex, *m_out);
160  // mesh_utils::set_matrix_attribute(V_inside, "vertices", PrimitiveType::Vertex, *m_in);
161  // cache.write_mesh(*m_in, options.output + "_inside");
162  // cache.write_mesh(*m_out, options.output + "_outside");
163  // break;
164  // }
165  // default: throw std::runtime_error("Unsupported Mesh Type");
166  // }
167 
168  // wmtk::logger().info("winding number filtering finished");
169 }
170 
171 } // namespace wmtk::components
Eigen::VectorXd winding_number(const Mesh &m, const TriMesh &surface)
std::shared_ptr< Mesh > winding_number(const std::shared_ptr< Mesh > &query_mesh_ptr, const std::shared_ptr< Mesh > &surface_ptr)
spdlog::logger & logger()
Retrieves the current logger.
Definition: Logger.cpp:58