Medusa  1.1
Coordinate Free Mehless Method implementation
STLShape.hpp
Go to the documentation of this file.
1 #ifndef MEDUSA_BITS_DOMAINS_STLSHAPE_HPP_
2 #define MEDUSA_BITS_DOMAINS_STLSHAPE_HPP_
3 
4 #include <medusa/Config.hpp>
5 #include "STLShape_fwd.hpp"
9 
10 #include <map>
11 
17 namespace mm {
18 
19 template <typename vec_t>
20 STLShape<vec_t>::STLShape(const std::vector<STL::Triangle>& stl) {
21  assert_msg(!stl.empty(), "Triangle list should not be empty.");
22  int num_triangles = stl.size();
23  std::map<vec_t, int> index;
24  int num_points = 0;
25  faces_.resize(num_triangles);
26  normals_.resize(num_triangles);
27  bbox_.first = bbox_.second = v(stl[0].p1);
28  for (int i = 0; i < num_triangles; ++i) {
29  const auto& t = stl[i];
30 
31  auto p1 = v(t.p1);
32  if (index.count(p1) == 0) index[p1] = num_points++;
33  faces_[i][0] = index[p1];
34  bbox_.first = bbox_.first.cwiseMin(p1);
35  bbox_.second = bbox_.second.cwiseMax(p1);
36 
37  auto p2 = v(t.p2);
38  if (index.count(p2) == 0) index[p2] = num_points++;
39  faces_[i][1] = index[p2];
40  bbox_.first = bbox_.first.cwiseMin(p2);
41  bbox_.second = bbox_.second.cwiseMax(p2);
42 
43  auto p3 = v(t.p3);
44  if (index.count(p3) == 0) index[p3] = num_points++;
45  faces_[i][2] = index[p3];
46  bbox_.first = bbox_.first.cwiseMin(p3);
47  bbox_.second = bbox_.second.cwiseMax(p3);
48 
49  normals_[i] = v(t.normal).normalized();
50  }
51 
52  vertices_.resize(num_points);
53  for (const auto& p : index) {
54  vertices_[p.second] = p.first;
55  }
56 }
57 
58 template <typename vec_t>
61  const std::function<scalar_t(vec_t)>& dx, int type) const {
62  if (type == 0) type = -1;
64 
66  int num_faces = faces_.size();
67  for (int i = 0; i < num_faces; ++i) {
68  auto p1 = vertices_[faces_[i][0]];
69  auto p2 = vertices_[faces_[i][1]];
70  auto p3 = vertices_[faces_[i][2]];
71  auto n = normals_[i];
72 
74  p1, p2, p3, n, dx, false);
75 
76  for (const auto& p : points) {
77  if (tree.existsPointInSphere(p, dx(p))) continue;
78  tree.insert(p);
79  d.addBoundaryNode(p, type, n);
80  }
81  }
82 
83  return d;
84 }
85 
86 } // namespace mm
87 
88 #endif // MEDUSA_BITS_DOMAINS_STLSHAPE_HPP_
mm::KDTreeMutable::insert
void insert(const vec_t &point)
Insert a point into the tree.
Definition: KDTreeMutable.hpp:24
mm
Root namespace for the whole library.
Definition: Gaussian.hpp:14
scalar_t
Scalar scalar_t
Type of the elements, alias of Scalar.
Definition: MatrixBaseAddons.hpp:16
mm::DomainDiscretization
Class representing domain discretization along with an associated shape.
Definition: DomainDiscretization_fwd.hpp:46
mm::STLShape::discretizeBoundaryWithDensity
DomainDiscretization< vec_t > discretizeBoundaryWithDensity(const std::function< scalar_t(vec_t)> &, int) const override
Discretizes boundary with given density and fill engine.
Definition: STLShape.hpp:60
mm::KDTreeMutable
A k-d tree data structure that supports dynamic insertions and lazy-removal.
Definition: HalfLinksRefine_fwd.hpp:18
mm::discretization_helpers::discretizeTriangleWithDensity
Range< vec_t > discretizeTriangleWithDensity(const vec_t &p1, const vec_t &p2, const vec_t &p3, const vec_t &normal, const func_t &h, bool only_interior=true)
Discretize a triangle in 3D space with given density.
Definition: discretization_helpers_advanced.hpp:45
assert_msg
#define assert_msg(cond,...)
Assert with better error reporting.
Definition: assert.hpp:75
Config.hpp
KDTreeMutable.hpp
STLShape_fwd.hpp
mm::KDTreeMutable::existsPointInSphere
bool existsPointInSphere(const vec_t &p, scalar_t r)
Check if any point exists in sphere centered at p with radius r.
Definition: KDTreeMutable_fwd.hpp:88
mm::STLShape::STLShape
STLShape(const std::vector< STL::Triangle > &stl)
Create STLShape from triangles read from STL file.
Definition: STLShape.hpp:20
discretization_helpers_advanced.hpp
DomainDiscretization_fwd.hpp
mm::KDTreeMutable::size
int size() const
Returns number of points in the tree.
Definition: KDTreeMutable_fwd.hpp:103
mm::Range< vec_t >
mm::DomainDiscretization::addBoundaryNode
int addBoundaryNode(const vec_t &point, int type, const vec_t &normal)
Adds a boundary node with given type and normal to the domain.
Definition: DomainDiscretization.hpp:56