Wildmeshing Toolkit
delaunay.cpp
Go to the documentation of this file.
1 #include "delaunay.hpp"
2 
3 #include <wmtk/PointMesh.hpp>
4 #include <wmtk/TetMesh.hpp>
5 #include <wmtk/TriMesh.hpp>
6 #include <wmtk/utils/Logger.hpp>
8 
10 #include "internal/delaunay_3d.hpp"
11 
12 namespace wmtk::components {
13 
14 template <int D>
16  const PointMesh& point_cloud,
17  const attribute::MeshAttributeHandle& pts_attr)
18 {
19  auto pts_acc = point_cloud.create_const_accessor<double>(pts_attr);
20 
21  const auto vertices = point_cloud.get_all(PrimitiveType::Vertex);
22 
23  RowVectors<double, D> vec(vertices.size(), D);
24  size_t i = 0;
25  for (const Tuple& t : vertices) {
26  const auto p = pts_acc.vector_attribute(t);
27  vec.row(i) = p.transpose();
28  ++i;
29  }
30 
31  return vec;
32 }
33 
34 template <int D, typename MeshT>
35 std::shared_ptr<MeshT> delaunay_exec(
36  const PointMesh& point_cloud,
37  const attribute::MeshAttributeHandle& pts_attr,
38  const std::string& output_pos_attr_name)
39 {
40  // 2d --> TriMesh
41  // 3d --> TetMesh
42  static_assert(
43  (D == 2 && std::is_same<MeshT, TriMesh>()) || (D == 3 && std::is_same<MeshT, TetMesh>()));
44 
45  if constexpr (D == 2) {
46  throw std::runtime_error("not tested for 2d");
47  }
48 
49  std::shared_ptr<MeshT> meshptr = std::make_shared<MeshT>();
50  MeshT& mesh = *meshptr;
51  Eigen::MatrixXd vertices;
52  Eigen::MatrixXi faces;
53  const auto pts_vec = points_to_rowvectors<D>(point_cloud, pts_attr);
54  if constexpr (D == 2) {
55  std::tie(vertices, faces) = internal::delaunay_2d(pts_vec);
56  } else if constexpr (D == 3) {
57  std::tie(vertices, faces) = internal::delaunay_3d(pts_vec);
58  } else {
59  throw std::runtime_error("unsupported cell dimension in delaunay component");
60  }
61 
62  mesh.initialize(faces.cast<int64_t>());
64 
65  return meshptr;
66 }
67 
68 std::shared_ptr<Mesh> delaunay(
69  const PointMesh& point_cloud,
70  const attribute::MeshAttributeHandle& pts_attr,
71  const std::string& output_pos_attr_name)
72 {
73  using namespace internal;
74 
75  auto pts_acc = point_cloud.create_const_accessor<double>(pts_attr);
76  const auto cell_dimension = pts_acc.dimension();
77 
78 
79  // delaunay
80  switch (cell_dimension) {
81  case 2: {
82  return delaunay_exec<2, TriMesh>(point_cloud, pts_attr, output_pos_attr_name);
83  break;
84  }
85  case 3: {
86  return delaunay_exec<3, TetMesh>(point_cloud, pts_attr, output_pos_attr_name);
87  break;
88  }
89  default: {
90  throw std::runtime_error("unsupported cell dimension in delaunay component");
91  }
92  }
93 }
94 
95 } // namespace wmtk::components
const attribute::Accessor< T, Derived, Dim > create_const_accessor(const attribute::TypedAttributeHandle< T > &handle) const
constructs a const accessor that is aware of the derived mesh's type
Definition: MeshCRTP.hpp:74
std::vector< Tuple > get_all(PrimitiveType type) const
Generate a vector of Tuples from global vertex/edge/triangle/tetrahedron index.
Definition: Mesh.cpp:18
std::tuple< Eigen::MatrixXd, Eigen::MatrixXi > delaunay_3d(Eigen::Ref< const RowVectors3d > points)
Definition: delaunay_3d.cpp:7
std::tuple< Eigen::MatrixXd, Eigen::MatrixXi > delaunay_2d(Eigen::Ref< const RowVectors2d > points)
Definition: delaunay_2d.cpp:8
std::shared_ptr< Mesh > delaunay(const PointMesh &point_cloud, const attribute::MeshAttributeHandle &pts_attr, const std::string &output_pos_attr_name)
Definition: delaunay.cpp:68
RowVectors< double, D > points_to_rowvectors(const PointMesh &point_cloud, const attribute::MeshAttributeHandle &pts_attr)
Definition: delaunay.cpp:15
std::shared_ptr< MeshT > delaunay_exec(const PointMesh &point_cloud, const attribute::MeshAttributeHandle &pts_attr, const std::string &output_pos_attr_name)
Definition: delaunay.cpp:35
attribute::MeshAttributeHandle set_matrix_attribute(const Mat &data, const std::string &name, const PrimitiveType &type, Mesh &mesh)
Definition: mesh_utils.hpp:9
std::vector< Tuple > vertices(const Mesh &m, const Simplex &simplex)
SimplexCollection faces(const Mesh &mesh, const Simplex &simplex, const bool sort_and_clean)
Returns all faces of a simplex.
Definition: faces.cpp:10
Eigen::Matrix< T, Eigen::Dynamic, C > RowVectors
Definition: Types.hpp:8