10#include <../Math/math.hpp>
12#ifndef _POLYTOPE2D_HPP
13#define _POLYTOPE2D_HPP
19 using VectorRd = Eigen::Matrix<double, DIMENSION, 1>;
20 using VectorZd = Eigen::Matrix<int, DIMENSION, 1>;
22 template <
size_t object_dim>
23 using Simplex = std::array<VectorRd, object_dim + 1>;
25 template <
size_t object_dim>
29 template <
size_t object_dim>
33 template <
size_t object_dim>
52 template <
size_t object_dim>
74 inline double diam()
const {
return _diameter; }
76 inline double measure()
const {
return _measure; }
82 inline std::vector<Polytope<0> *>
get_vertices()
const {
return _vertices; }
83 inline std::vector<Polytope<1> *>
get_edges()
const {
return _edges; }
85 inline std::vector<Polytope<DIMENSION> *>
get_cells()
const {
return _cells; }
87 inline size_t n_vertices()
const {
return _vertices.size(); }
88 inline size_t n_edges()
const {
return _edges.size(); }
89 inline size_t n_faces()
const {
return _edges.size(); }
90 inline size_t n_cells()
const {
return _cells.size(); }
122 void plot(std::ofstream *out)
const;
133 std::vector<int> _face_directions;
135 std::vector<Polytope<0> *> _vertices;
136 std::vector<Polytope<1> *> _edges;
138 std::vector<Polytope<DIMENSION> *> _cells;
159 template <
size_t object_dim>
162 VectorRd center_mass = VectorRd::Zero();
164 for (
auto &coord : simplex)
168 center_mass(
i) += coord(
i);
171 return center_mass / (object_dim + 1);
174 template <
size_t object_dim>
177 Eigen::MatrixXd CM_mat = Eigen::MatrixXd::Zero(object_dim + 2, object_dim + 2);
178 for (
size_t i = 0;
i < object_dim + 1; ++
i)
181 for (
size_t j =
i + 1;
j < object_dim + 1; ++
j)
184 double norm = (j_coords - i_coords).norm();
185 CM_mat(
i,
j) = norm * norm;
186 CM_mat(
j,
i) = CM_mat(
i,
j);
188 CM_mat(
i, object_dim + 1) = 1;
189 CM_mat(object_dim + 1,
i) = 1;
193 double det = CM_mat.determinant();
195 double scaling = std::pow(
Math::factorial(object_dim), 2) * std::pow(2, object_dim);
197 return std::sqrt(std::abs(det / scaling));
200 template <
size_t object_dim>
202 : _index(index), _simplices(simplices)
206 if (object_dim == 0 || object_dim == 1)
208 assert(simplices.size() == 1);
211 _is_boundary =
false;
214 _center_mass = VectorRd::Zero();
215 std::vector<VectorRd> vertex_coords;
216 for (
auto &simplex : simplices)
219 double measure_of_simplex = simplex_measure<object_dim>(simplex);
220 _measure += measure_of_simplex;
221 _center_mass += measure_of_simplex * simplex_center_mass<object_dim>(simplex);
222 for (
auto &coord : simplex)
224 if (std::find(vertex_coords.begin(), vertex_coords.end(), coord) == vertex_coords.end())
226 vertex_coords.push_back(coord);
230 _center_mass /= _measure;
233 for (
auto it = vertex_coords.begin(); it != vertex_coords.end(); ++it)
235 for (
auto jt = it; jt != vertex_coords.end(); ++jt)
237 _diameter = std::max(_diameter, (*it - *jt).norm());
244 _normal =
VectorRd(-_tangent(1), _tangent(0));
248 template <
size_t object_dim>
254 template <
size_t object_dim>
258 assert(object_dim == 0);
261 template <
size_t object_dim>
264 template <
size_t object_dim>
267 template <
size_t object_dim>
270 assert(std::find(_vertices.begin(), _vertices.end(), vertex) == _vertices.end());
271 _vertices.push_back(vertex);
274 template <
size_t object_dim>
277 assert(std::find(_edges.begin(), _edges.end(), edge) == _edges.end());
278 _edges.push_back(edge);
281 template <
size_t object_dim>
284 assert(std::find(_edges.begin(), _edges.end(), face) == _edges.end());
285 _edges.push_back(face);
288 template <
size_t object_dim>
291 assert(std::find(_cells.begin(), _cells.end(), cell) == _cells.end());
292 _cells.push_back(cell);
295 template <
size_t object_dim>
298 assert(
i < _vertices.size());
302 template <
size_t object_dim>
305 assert(
i < _edges.size());
309 template <
size_t object_dim>
312 assert(
i < _edges.size());
316 template <
size_t object_dim>
319 assert(
i < _cells.size());
323 template <
size_t object_dim>
327 for (
size_t iF = 0; iF < _edges.size(); ++iF)
329 VectorRd normal = _edges[iF]->normal();
330 Simplex<object_dim - 1> face_simplex = _edges[iF]->get_simplices()[0];
333 for (
auto &cell_simplex : this->get_simplices())
336 for (
size_t i = 0; (
i < cell_simplex.size()) &&
count < 2; ++
i)
338 if (std::find(face_simplex.begin(), face_simplex.end(), cell_simplex[
i]) == face_simplex.end())
345 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:54
Compute max and min eigenvalues of all matrices for i
Definition compute_eigs.m:5
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:76
void construct_face_normals()
Set the directions of the face normals of a cell.
Definition Polytope2D.hpp:324
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:275
VectorRd center_mass() const
Return the center mass of the Polytope.
Definition Polytope2D.hpp:75
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:85
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:78
size_t n_vertices() const
Return the number of vertices of the Polytope.
Definition Polytope2D.hpp:87
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:88
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:268
void add_cell(Polytope< DIMENSION > *cell)
Add a cell to the Polytope.
Definition Polytope2D.hpp:289
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:317
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:84
double diam() const
Return the diameter of the Polytope.
Definition Polytope2D.hpp:74
~Polytope()
Definition Polytope2D.hpp:265
Polytope< DIMENSION - 1 > * face(const size_t i) const
Return the i-th face of the Polytope.
Definition Polytope2D.hpp:310
void add_face(Polytope< DIMENSION - 1 > *face)
Add a face to the Polytope.
Definition Polytope2D.hpp:282
void set_boundary(bool val)
Set the boundary value of the Polytope.
Definition Polytope2D.hpp:80
size_t n_faces() const
Return the number of faces of the Polytope.
Definition Polytope2D.hpp:89
Polytope< 1 > * edge(const size_t i) const
Return the i-th edge of the Polytope.
Definition Polytope2D.hpp:303
std::vector< Polytope< 0 > * > get_vertices() const
Return the vertices of the Polytope.
Definition Polytope2D.hpp:82
bool is_boundary() const
Return true if Polytope is a boundary object, false otherwise.
Definition Polytope2D.hpp:79
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:175
size_t global_index() const
Return the global index of the Polytope.
Definition Polytope2D.hpp:73
std::vector< Polytope< 1 > * > get_edges() const
Return the edges of the Polytope.
Definition Polytope2D.hpp:83
int face_orientation(const size_t face_index) const
Return the orientation of a Face.
Definition Polytope2D.hpp:449
Polytope()
Destructor.
Definition Polytope2D.hpp:262
Simplices< object_dim > get_simplices() const
Return the simplices making up the Polytope.
Definition Polytope2D.hpp:77
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:296
VectorRd simplex_center_mass(Simplex< object_dim > simplex)
Method to find the Lebesgue measure of an arbitrary simplex in arbitrary space.
Definition Polytope2D.hpp:160
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:90
end Read size then branch according to sparse or dense format count
Definition mmread.m:75
for j
Definition mmread.m:174
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:23
constexpr int DIMENSION
Definition Polytope2D.hpp:17
std::vector< Simplex< object_dim > > Simplices
Method to find the center mass of an arbitrary simplex in arbitrary space.
Definition Polytope2D.hpp:28
Eigen::Matrix< int, DIMENSION, 1 > VectorZd
Definition Polytope2D.hpp:20
Eigen::Matrix< double, DIMENSION, 1 > VectorRd
Definition Polytope2D.hpp:19