PolyFEM
Loading...
Searching...
No Matches
Mesh2D.cpp
Go to the documentation of this file.
2
4
6
7#include <igl/barycentric_coordinates.h>
8
9#include <cassert>
10#include <array>
11
12namespace polyfem
13{
14 namespace mesh
15 {
16 void Mesh2D::get_edges(Eigen::MatrixXd &p0, Eigen::MatrixXd &p1) const
17 {
18 p0.resize(n_edges(), 2);
19 p1.resize(p0.rows(), p0.cols());
20
21 for (int e = 0; e < n_edges(); ++e)
22 {
23 const int v0 = edge_vertex(e, 0);
24 const int v1 = edge_vertex(e, 1);
25
26 p0.row(e) = point(v0);
27 p1.row(e) = point(v1);
28 }
29 }
30
31 void Mesh2D::get_edges(Eigen::MatrixXd &p0, Eigen::MatrixXd &p1, const std::vector<bool> &valid_elements) const
32 {
33 int count = 0;
34 for (size_t i = 0; i < valid_elements.size(); ++i)
35 {
36 if (valid_elements[i])
37 {
38 count += n_face_vertices(i);
39 }
40 }
41
42 p0.resize(count, 2);
43 p1.resize(count, 2);
44
45 count = 0;
46
47 for (size_t i = 0; i < valid_elements.size(); ++i)
48 {
49 if (!valid_elements[i])
50 continue;
51
52 auto index = get_index_from_face(i);
53 for (int j = 0; j < n_face_vertices(i); ++j)
54 {
55 p0.row(count) = point(index.vertex);
56 p1.row(count) = point(switch_vertex(index).vertex);
57
58 index = next_around_face(index);
59 ++count;
60 }
61 }
62 }
63
64 RowVectorNd Mesh2D::face_barycenter(const int face_index) const
65 {
66 RowVectorNd bary(2);
67 bary.setZero();
68
69 const int n_vertices = n_face_vertices(face_index);
70 Navigation::Index index = get_index_from_face(face_index);
71
72 for (int lv = 0; lv < n_vertices; ++lv)
73 {
74 bary += point(index.vertex);
75 index = next_around_face(index);
76 }
77 return bary / n_vertices;
78 }
79
80 void Mesh2D::barycentric_coords(const RowVectorNd &p, const int el_id, Eigen::MatrixXd &coords) const
81 {
82 assert(is_simplex(el_id));
83
84 auto index = get_index_from_face(el_id);
85 const auto A = point(index.vertex);
86
87 index = next_around_face(index);
88 const auto B = point(index.vertex);
89
90 index = next_around_face(index);
91 const auto C = point(index.vertex);
92
93 igl::barycentric_coordinates(p, A, B, C, coords);
94 }
95
96 void Mesh2D::compute_face_jacobian(const int el_id, const Eigen::MatrixXd &reference_map, Eigen::MatrixXd &jacobian) const
97 {
98 assert(is_simplex(el_id));
99
100 auto index = get_index_from_face(el_id);
101 const auto A = point(index.vertex);
102 index = next_around_face(index);
103 const auto B = point(index.vertex);
104 index = next_around_face(index);
105 const auto C = point(index.vertex);
106
107 Eigen::MatrixXd coords(3, 3);
108 coords << A, 1, B, 1, C, 1;
109 coords.transposeInPlace();
110
111 jacobian = coords * reference_map;
112
113 assert(jacobian.determinant() > 0);
114 }
115
116 void Mesh2D::elements_boxes(std::vector<std::array<Eigen::Vector3d, 2>> &boxes) const
117 {
118 boxes.resize(n_elements());
119
120 for (int i = 0; i < n_elements(); ++i)
121 {
122 auto &box = boxes[i];
123 box[0] << std::numeric_limits<double>::max(), std::numeric_limits<double>::max(), 0;
124 box[1] << std::numeric_limits<double>::lowest(), std::numeric_limits<double>::lowest(), 0;
125
126 auto index = get_index_from_face(i);
127
128 for (int j = 0; j < n_face_vertices(i); ++j)
129 {
130 for (int d = 0; d < 2; ++d)
131 {
132 box[0][d] = std::min(box[0][d], point(index.vertex)[d]);
133 box[1][d] = std::max(box[1][d], point(index.vertex)[d]);
134 }
135 index = next_around_face(index);
136 }
137 }
138 }
139 } // namespace mesh
140} // namespace polyfem
Eigen::RowVectorXd point
void elements_boxes(std::vector< std::array< Eigen::Vector3d, 2 > > &boxes) const override
constructs a box around every element (3d cell, 2d face)
Definition Mesh2D.cpp:116
RowVectorNd face_barycenter(const int index) const override
face barycenter
Definition Mesh2D.cpp:64
Navigation::Index next_around_face(Navigation::Index idx) const
Definition Mesh2D.hpp:63
void barycentric_coords(const RowVectorNd &p, const int el_id, Eigen::MatrixXd &coord) const override
constructs barycentric coodiantes for a point p.
Definition Mesh2D.cpp:80
void get_edges(Eigen::MatrixXd &p0, Eigen::MatrixXd &p1) const override
Get all the edges.
Definition Mesh2D.cpp:16
virtual Navigation::Index switch_vertex(Navigation::Index idx) const =0
virtual Navigation::Index get_index_from_face(int f, int lv=0) const =0
void compute_face_jacobian(const int el_id, const Eigen::MatrixXd &reference_map, Eigen::MatrixXd &jacobian) const
Definition Mesh2D.cpp:96
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
Definition Mesh.hpp:181
virtual int n_vertices() const =0
number of vertices
bool is_simplex(const int el_id) const
checks if element is simplex
Definition Mesh.cpp:392
virtual int edge_vertex(const int e_id, const int lv_id) const =0
id of the edge vertex
virtual int n_edges() const =0
number of edges
virtual int n_face_vertices(const int f_id) const =0
number of vertices of a face
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
Definition Types.hpp:13