11#include <../Math/math.hpp>
13#ifndef _POLYTOPE2D_HPP
14#define _POLYTOPE2D_HPP
20 using VectorRd = Eigen::Matrix<double, DIMENSION, 1>;
21 using VectorZd = Eigen::Matrix<int, DIMENSION, 1>;
23 template <
size_t object_dim>
24 using Simplex = std::array<VectorRd, object_dim + 1>;
26 template <
size_t object_dim>
30 template <
size_t object_dim>
34 template <
size_t object_dim>
53 template <
size_t object_dim>
75 inline double diam()
const {
return _diameter; }
77 inline double measure()
const {
return _measure; }
83 inline std::vector<Polytope<0> *>
get_vertices()
const {
return _vertices; }
84 inline std::vector<Polytope<1> *>
get_edges()
const {
return _edges; }
86 inline std::vector<Polytope<DIMENSION> *>
get_cells()
const {
return _cells; }
88 inline size_t n_vertices()
const {
return _vertices.size(); }
89 inline size_t n_edges()
const {
return _edges.size(); }
90 inline size_t n_faces()
const {
return _edges.size(); }
91 inline size_t n_cells()
const {
return _cells.size(); }
123 void plot(std::ofstream *out)
const;
134 std::vector<int> _face_directions;
136 std::vector<Polytope<0> *> _vertices;
137 std::vector<Polytope<1> *> _edges;
139 std::vector<Polytope<DIMENSION> *> _cells;
160 template <
size_t object_dim>
163 VectorRd center_mass = VectorRd::Zero();
165 for (
auto &coord : simplex)
169 center_mass(
i) += coord(
i);
172 return center_mass / (object_dim + 1);
175 template <
size_t object_dim>
178 Eigen::MatrixXd CM_mat = Eigen::MatrixXd::Zero(object_dim + 2, object_dim + 2);
179 for (
size_t i = 0;
i < object_dim + 1; ++
i)
182 for (
size_t j =
i + 1;
j < object_dim + 1; ++
j)
185 double norm = (j_coords - i_coords).norm();
186 CM_mat(
i,
j) = norm * norm;
187 CM_mat(
j,
i) = CM_mat(
i,
j);
189 CM_mat(
i, object_dim + 1) = 1;
190 CM_mat(object_dim + 1,
i) = 1;
194 double det = CM_mat.determinant();
196 double scaling = std::pow(
Math::factorial(object_dim), 2) * std::pow(2, object_dim);
198 return std::sqrt(std::abs(det / scaling));
201 template <
size_t object_dim>
203 : _index(index), _simplices(simplices)
207 if (object_dim == 0 || object_dim == 1)
209 assert(simplices.size() == 1);
212 _is_boundary =
false;
215 _center_mass = VectorRd::Zero();
216 std::vector<VectorRd> vertex_coords;
217 for (
auto &simplex : simplices)
220 double measure_of_simplex = simplex_measure<object_dim>(simplex);
221 _measure += measure_of_simplex;
222 _center_mass += measure_of_simplex * simplex_center_mass<object_dim>(simplex);
223 for (
auto &coord : simplex)
225 if (std::find(vertex_coords.begin(), vertex_coords.end(), coord) == vertex_coords.end())
227 vertex_coords.push_back(coord);
231 _center_mass /= _measure;
234 for (
auto it = vertex_coords.begin();
it != vertex_coords.end(); ++
it)
236 for (
auto jt =
it; jt != vertex_coords.end(); ++jt)
238 _diameter = std::max(_diameter, (*
it - *jt).norm());
245 _normal =
VectorRd(-_tangent(1), _tangent(0));
249 template <
size_t object_dim>
255 template <
size_t object_dim>
259 assert(object_dim == 0);
262 template <
size_t object_dim>
265 template <
size_t object_dim>
268 template <
size_t object_dim>
271 assert(std::find(_vertices.begin(), _vertices.end(), vertex) == _vertices.end());
272 _vertices.push_back(vertex);
275 template <
size_t object_dim>
278 assert(std::find(_edges.begin(), _edges.end(), edge) == _edges.end());
279 _edges.push_back(edge);
282 template <
size_t object_dim>
285 assert(std::find(_edges.begin(), _edges.end(), face) == _edges.end());
286 _edges.push_back(face);
289 template <
size_t object_dim>
292 assert(std::find(_cells.begin(), _cells.end(), cell) == _cells.end());
293 _cells.push_back(cell);
296 template <
size_t object_dim>
299 assert(
i < _vertices.size());
303 template <
size_t object_dim>
306 assert(
i < _edges.size());
310 template <
size_t object_dim>
313 assert(
i < _edges.size());
317 template <
size_t object_dim>
320 assert(
i < _cells.size());
324 template <
size_t object_dim>
328 for (
size_t iF = 0; iF < _edges.size(); ++iF)
330 VectorRd normal = _edges[iF]->normal();
331 Simplex<object_dim - 1> face_simplex = _edges[iF]->get_simplices()[0];
334 for (
auto &cell_simplex : this->get_simplices())
337 for (
size_t i = 0; (
i < cell_simplex.size()) &&
count < 2; ++
i)
339 if (std::find(face_simplex.begin(), face_simplex.end(), cell_simplex[
i]) == face_simplex.end())
346 center = simplex_center_mass<object_dim>(cell_simplex);
351 _face_directions.push_back(
Math::sgn((_edges[iF]->center_mass() - center).dot(normal)));
352 assert(_face_directions[iF] != 0);
356 template <
size_t object_dim>
360 assert(face_index < _edges.size());
361 return _face_directions[face_index] * _edges[face_index]->normal();
364 template <
size_t object_dim>
367 return this->face_normal(edge_index);
370 template <
size_t object_dim>
373 assert(object_dim == 0);
374 return this->get_simplices()[0][0];
377 template <
size_t object_dim>
384 template <
size_t object_dim>
387 assert(object_dim == 1);
392 template <
size_t object_dim>
395 auto itr = std::find(_vertices.begin(), _vertices.end(), vertex);
396 if (itr != _vertices.end())
398 return itr - _vertices.begin();
402 throw "Vertex not found";
406 template <
size_t object_dim>
409 auto itr = std::find(_edges.begin(), _edges.end(), edge);
410 if (itr != _edges.end())
412 return itr - _edges.begin();
416 throw "Edge not found";
420 template <
size_t object_dim>
423 auto itr = std::find(_edges.begin(), _edges.end(), face);
424 if (itr != _edges.end())
426 return itr - _edges.begin();
430 throw "Face not found";
434 template <
size_t object_dim>
437 auto itr = std::find(_cells.begin(), _cells.end(), cell);
438 if (itr != _cells.end())
440 return itr - _cells.begin();
444 throw "Cell not found";
448 template <
size_t object_dim>
452 assert(face_index < _edges.size());
453 return _face_directions[face_index];
456 template <
size_t object_dim>
459 assert(object_dim == 2);
460 assert(edge_index < _edges.size());
461 return _face_directions[edge_index];
464 template <
size_t object_dim>
467 assert(object_dim == 1);
468 assert(vertex_index < _vertices.size());
469 return ( (_vertices[vertex_index]->coords()-_center_mass).dot(this->tangent()) > 0 ? 1 : -1);
472 template <
size_t object_dim>
475 assert(object_dim > 0);
476 for (
auto &simplex : _simplices)
478 for (
size_t i = 0;
i < object_dim + 1; ++
i)
480 size_t i_next = (
i + 1) % (object_dim + 1);
481 *out << simplex[
i](0) <<
" " << simplex[
i](1) << std::endl;
482 *out << simplex[i_next](0) <<
" " << simplex[i_next](1) << std::endl;
488 template <
size_t object_dim>
491 assert(object_dim > 0);
492 for (
size_t i = 0;
i < _vertices.size(); ++
i)
494 size_t i_next = (
i + 1) % _vertices.size();
495 *out << _vertices[
i]->coords()(0) <<
" " << _vertices[
i]->coords()(1) << std::endl;
496 *out << _vertices[i_next]->coords()(0) <<
" " << _vertices[i_next]->coords()(1) << std::endl;
A Vertex is a Polytope with object_dim = 0.
Definition Polytope2D.hpp:55
for i
Definition convergence_analysis.m:48
for j
Definition convergence_analysis.m:19
VectorRd normal() const
Return the normal of a Face.
Definition Polytope2D.hpp:378
double measure() const
Return the Lebesgue measure of the Polytope.
Definition Polytope2D.hpp:77
void construct_face_normals()
Set the directions of the face normals of a cell.
Definition Polytope2D.hpp:325
int index_cell(const Polytope< DIMENSION > *cell) const
Returns the local index of a cell.
Definition Polytope2D.hpp:435
int index_edge(const Polytope< 1 > *edge) const
Returns the local index of an edge.
Definition Polytope2D.hpp:407
void add_edge(Polytope< 1 > *edge)
Add an edge to the Polytope.
Definition Polytope2D.hpp:276
VectorRd center_mass() const
Return the center mass of the Polytope.
Definition Polytope2D.hpp:76
void plot(std::ofstream *out) const
Plot the polytope to out.
Definition Polytope2D.hpp:489
std::vector< Polytope< DIMENSION > * > get_cells() const
Return the cells of the Polytope.
Definition Polytope2D.hpp:86
VectorRd tangent() const
Return the tangent of a Edge.
Definition Polytope2D.hpp:385
void set_global_index(const size_t idx)
Set the global index.
Definition Polytope2D.hpp:79
size_t n_vertices() const
Return the number of vertices of the Polytope.
Definition Polytope2D.hpp:88
void plot_simplices(std::ofstream *out) const
Plot the simplices to out.
Definition Polytope2D.hpp:473
VectorRd face_normal(const size_t face_index) const
Return the outer normal of a Cell towards the Face located at face_index.
Definition Polytope2D.hpp:357
size_t n_edges() const
Return the number of edges of the Polytope.
Definition Polytope2D.hpp:89
int index_face(const Polytope< DIMENSION - 1 > *face) const
Returns the local index of a face.
Definition Polytope2D.hpp:421
void add_vertex(Polytope< 0 > *vertex)
Add a vertex to the Polytope.
Definition Polytope2D.hpp:269
void add_cell(Polytope< DIMENSION > *cell)
Add a cell to the Polytope.
Definition Polytope2D.hpp:290
int index_vertex(const Polytope< 0 > *vertex) const
Returns the local index of a vertex.
Definition Polytope2D.hpp:393
Polytope< DIMENSION > * cell(const size_t i) const
Return the i-th cell of the Polytope.
Definition Polytope2D.hpp:318
int vertex_orientation(const size_t vertex_index) const
Return the orientation of a Vertex.
Definition Polytope2D.hpp:465
std::vector< Polytope< DIMENSION - 1 > * > get_faces() const
Return the faces of the Polytope.
Definition Polytope2D.hpp:85
double diam() const
Return the diameter of the Polytope.
Definition Polytope2D.hpp:75
~Polytope()
Definition Polytope2D.hpp:266
Polytope< DIMENSION - 1 > * face(const size_t i) const
Return the i-th face of the Polytope.
Definition Polytope2D.hpp:311
void add_face(Polytope< DIMENSION - 1 > *face)
Add a face to the Polytope.
Definition Polytope2D.hpp:283
void set_boundary(bool val)
Set the boundary value of the Polytope.
Definition Polytope2D.hpp:81
size_t n_faces() const
Return the number of faces of the Polytope.
Definition Polytope2D.hpp:90
Polytope< 1 > * edge(const size_t i) const
Return the i-th edge of the Polytope.
Definition Polytope2D.hpp:304
std::vector< Polytope< 0 > * > get_vertices() const
Return the vertices of the Polytope.
Definition Polytope2D.hpp:83
bool is_boundary() const
Return true if Polytope is a boundary object, false otherwise.
Definition Polytope2D.hpp:80
VectorRd edge_normal(const size_t edge_index) const
Return the edge normal of a 2D object.
Definition Polytope2D.hpp:365
double simplex_measure(Simplex< object_dim > simplex)
Definition Polytope2D.hpp:176
size_t global_index() const
Return the global index of the Polytope.
Definition Polytope2D.hpp:74
std::vector< Polytope< 1 > * > get_edges() const
Return the edges of the Polytope.
Definition Polytope2D.hpp:84
int face_orientation(const size_t face_index) const
Return the orientation of a Face.
Definition Polytope2D.hpp:449
Polytope()
Destructor.
Definition Polytope2D.hpp:263
Simplices< object_dim > get_simplices() const
Return the simplices making up the Polytope.
Definition Polytope2D.hpp:78
int edge_orientation(const size_t edge_index) const
Return the orientation of a Edge.
Definition Polytope2D.hpp:457
Polytope< 0 > * vertex(const size_t i) const
Return the i-th vertex of the Polytope.
Definition Polytope2D.hpp:297
VectorRd simplex_center_mass(Simplex< object_dim > simplex)
Method to find the Lebesgue measure of an arbitrary simplex in arbitrary space.
Definition Polytope2D.hpp:161
VectorRd coords() const
Return the coordinates of a Vertex.
Definition Polytope2D.hpp:371
size_t n_cells() const
Return the number of cells of the Polytope.
Definition Polytope2D.hpp:91
end Read size then branch according to sparse or dense format count
Definition mmread.m:75
const int sgn(T val)
Definition math.hpp:42
const size_t factorial(size_t n)
Definition math.hpp:12
std::array< VectorRd, object_dim+1 > Simplex
Definition Polytope2D.hpp:24
constexpr int DIMENSION
Definition Polytope2D.hpp:18
std::vector< Simplex< object_dim > > Simplices
Method to find the center mass of an arbitrary simplex in arbitrary space.
Definition Polytope2D.hpp:29
Eigen::Matrix< int, DIMENSION, 1 > VectorZd
Definition Polytope2D.hpp:21
Eigen::Matrix< double, DIMENSION, 1 > VectorRd
Definition Polytope2D.hpp:20