Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Polyhedron.cpp
Go to the documentation of this file.
1#include <MeshModule.h>
2#include <PolyhedralMesh.h>
3#include <ConvexHull.h>
4namespace model
5{
6 template <int dim, int order>
7 void
8 Polyhedron<dim, order>::buildSingleFace(const Simplex<dim, dim - 1> *newStart,
9 std::shared_ptr<Polyhedron<dim, dim - 1>> newFace,
10 std::map<std::vector<size_t>, const Simplex<dim, dim - 1> *> &allSimplices)
11 requires(order == dim)
12 {
13 allSimplices.erase(std::vector<size_t>(newStart->xID.begin(), newStart->xID.end()));
14 const auto boundaryNeighbors(newStart->boundaryNeighbors());
15 for (const auto &neighbor : boundaryNeighbors)
16 {
17 const auto neighborRgnIDs(neighbor->regionIDs());
18 if (neighborRgnIDs.find(this->regionID) != neighborRgnIDs.end() // neighbor in region
19 && allSimplices.find(std::vector<size_t>(neighbor->xID.begin(), neighbor->xID.end())) !=
20 allSimplices.end()) // neighbor found in allSimplices
21 {
22 if ((newStart->outNormal() - neighbor->outNormal()).norm() < FLT_EPSILON)
23 { // same plane
24 allSimplices.erase(std::vector<size_t>(neighbor->xID.begin(), neighbor->xID.end()));
25 newFace->insert(neighbor);
26 buildSingleFace(neighbor, newFace, allSimplices);
27 }
28 }
29 }
30 faces().emplace(newFace->sID, newFace);
31 }
32
33 /**********************************************************************/
34 template <int dim, int order>
35 void Polyhedron<dim, order>::buildFaces()
36 requires(order == dim)
37 {
40 const auto t0 = std::chrono::system_clock::now();
41 std::cout << "MeshRegion " << this->regionID << " buiding PlanarMeshFace's " << std::flush;
42
43 std::map<std::vector<size_t>, const Simplex<dim, dim - 1> *> allSimplices;
44 for (const auto &simplex : simplices())
45 { // collect each possible boundary/region_boundary simplices
46 for (const auto &child : simplex->children())
47 {
48 if (child->isBoundarySimplex())
49 {
50 allSimplices.emplace(std::vector<size_t>(child->xID.begin(), child->xID.end()), child.get());
51 }
52 }
53 }
54
55 while (allSimplices.size())
56 {
57 auto newFace = cellFactory.owner->createFace(allSimplices.begin()->second);
58 buildSingleFace(allSimplices.begin()->second, newFace, allSimplices);
59 }
60
61 for (auto &face : faces())
62 {
63 face.second->finalize();
64 }
65 std::cout << magentaColor << " ["
66 << (std::chrono::duration<double>(std::chrono::system_clock::now() - t0)).count() << " sec]"
67 << defaultColor << std::endl;
68 }
69
70 template <int dim, int order>
72 requires(order == dim)
73 {
74 buildFaces();
75 }
76 template <int dim, int order>
77 typename Polyhedron<dim, order>::VectorDim Polyhedron<dim, order>::outNormal(const std::set<size_t> &ids) const
78 requires(order == dim)
79 {
80 VectorDim result = VectorDim::Zero();
81 for (const auto id : ids)
82 {
83 result += faces().at(id)->outNormal();
84 }
85 const double norm = result.norm();
86 return norm > FLT_EPSILON ? (result / norm).eval() : VectorDim::Zero();
87 }
88
89 template <int dim, int order>
91 requires(order == dim - 1)
92 {
93 this->regionIDs = {*simplex->regionIDs().begin(), *simplex->regionIDs().rbegin()};
94 this->n = simplex->outNormal(this->regionIDs.first);
95 this->c = simplex->center();
96 this->insert(simplex);
97 }
98 template <int dim, int order>
99 const std::vector<const Simplex<dim, 0> *> &Polyhedron<dim, order>::convexHull() const
100 requires(order == dim - 1)
101 {
102 return this->hull;
103 }
104
105 /**********************************************************************/
106 template <int dim, int order>
108 requires(order == dim - 1)
109 {
113
114 // buildEdges();
115
116 // Recompute this->c and this->n to minimize floating point errors
117 this->n.setZero();
118 this->c.setZero();
119 for (const auto &simplex : *this)
120 {
121 this->n += simplex->outNormal(this->regionIDs.first) / this->simplices().size();
122 this->c += simplex->center() / this->simplices().size();
123 }
124 this->n.normalize();
125
126 for (const auto &simplex : *this)
127 { // Make sure all simplices belong to face
128 if (fabs((this->c - simplex->center()).dot(this->n)) > FLT_EPSILON)
129 {
130 throw std::runtime_error("simplex does not belong to PlanarMeshFace.");
131 }
132 }
133
134 // Compute ConvexHull of vertices
135 std::set<const Simplex<dim, 0> *> vertices;
136 for (const auto &simplex : *this)
137 { // Collect vertices on this face
138 for (const auto &v : simplex->vertices())
139 {
140 vertices.insert(v);
141 }
142 }
143
144 // PlanarMeshFaceHull<dim>::makeHull(vertices,outNormal(),center,this->hull);
145
146 if constexpr (dim < 3)
147 {
148 this->hull.assign(vertices.begin(), vertices.end());
149 if constexpr (dim == 2)
150 {
151 const VectorDim tangent = (this->hull.back()->P0 - this->hull.front()->P0).normalized();
152 std::sort(this->hull.begin(), this->hull.end(), [&](const auto *a, const auto *b)
153 {
154 return a->P0.dot(tangent) < b->P0.dot(tangent);
155 });
156 this->hull = {this->hull.front(), this->hull.back()};
157 }
158 this->c.setZero();
159 for (const auto *vertex : this->hull)
161 this->c += vertex->P0 / this->hull.size();
163 }
164 else if (vertices.size())
165 {
167 // assert(dim==3 && "ALGORITHM ONLY VALID IN dim=3");
169 const Plane<dim> plane(this->c, this->n);
171 for (const auto &v : vertices)
172 { // compute local position in the plane as insert in ConvexHull
173 const auto x(plane.localPosition(v->P0));
174 hull.emplace(std::array<double, 2>{x[0], x[1]}, v);
175 }
176
177 const auto hullPts = hull.getPoints();
178 this->hull.clear();
179 for (const auto &hp : hullPts)
180 {
181 this->hull.push_back(hp.t);
182 }
183 if (this->hull.size() < 3)
184 {
185 std::cout << "vertices.size()=" << vertices.size() << std::endl;
186 throw std::runtime_error("PlanarMeshFace hull has size " + std::to_string(this->hull.size()));
187 }
188
189 // Recompute center using Hull
190 this->c.setZero();
191 for (const auto &v : convexHull())
192 {
193 this->c += v->P0 / convexHull().size();
194 }
195 }
196 }
197
198 /**********************************************************************/
199 template <int dim, int order>
200 Eigen::Matrix<double, dim, 1> Polyhedron<dim, order>::outNormal() const
201 requires(order == dim - 1)
202 {
203 return isExternal() ? this->n : Eigen::Matrix<double, dim, 1>::Zero();
204 }
205
206 /**********************************************************************/
207 template <int dim, int order>
208 Eigen::Matrix<double, dim, 1> Polyhedron<dim, order>::outNormal(const int &k) const
209 requires(order == dim - 1)
210 {
211 return k == this->regionIDs.first
212 ? this->n
213 : (k == this->regionIDs.second ? (-this->n).eval() : Eigen::Matrix<double, dim, 1>::Zero());
214 }
215
216 /**********************************************************************/
217 template <int dim, int order>
218 const Eigen::Matrix<double, dim, 1> &Polyhedron<dim, order>::center() const
219 requires(order == dim - 1)
220 {
221 return this->c;
222 }
223
224 /**********************************************************************/
225 template <int dim, int order>
227 requires(order == dim - 1)
228 {
229 return Plane<dim>(center(), this->n);
230 }
231
232 template struct Polyhedron<1, 0>;
233 template struct Polyhedron<1, 1>;
234 template struct Polyhedron<2, 1>;
235 template struct Polyhedron<2, 2>;
236 template struct Polyhedron<3, 3>;
237 template struct Polyhedron<3, 2>;
238} // namespace model
c
Definition int_R.m:15
a
Definition int_R.m:14
b
Definition int_R.m:16
x
Definition int_R.m:12
size_t InstanceCount< Derived >::count
static std::string magentaColor
auto eval(const std::shared_ptr< T > &t)
static std::string defaultColor
Plane< dim > asPlane() const
const MeshFacesContainerType & faces() const
Definition Polyhedron.h:136
const SimplexContainerType & simplices() const
Definition Polyhedron.h:128
const VectorDim & center() const
VectorDim outNormal() const
bool isExternal() const
Definition Polyhedron.h:146
Simplex< dim, order > SimplexType
Definition Polyhedron.h:119
Eigen::Matrix< double, dim, 1 > VectorDim
Definition Polyhedron.h:118
void initialize(const SimplexType *simplex)
const std::vector< const Simplex< dim, 0 > * > & convexHull() const
struct otri neighbor
REAL t0
b order
int k
REAL * vertex
Definition triangle.cpp:542
result
void vertices
REAL v[4]