PolyFEM
Loading...
Searching...
No Matches
Mesh3DStorage.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <Eigen/Dense>
4
5#include <array>
6#include <cassert>
7#include <cstdint>
8#include <initializer_list>
9#include <iterator>
10#include <limits>
11#include <string>
12#include <type_traits>
13#include <vector>
14
15namespace polyfem
16{
17 namespace mesh
18 {
20 template <typename T>
22 {
23 public:
25
26 T *begin() const { return begin_; }
27 T *end() const { return end_; }
28 size_t size() const { return end_ - begin_; }
29 bool empty() const { return begin_ == end_; }
30 T &operator[](const size_t i) const
31 {
32 assert(i < size());
33 return begin_[i];
34 }
35 T &front() const { return *begin_; }
36 T &back() const { return *(end_ - 1); }
37
39 std::vector<std::remove_const_t<T>> to_vector() const { return {begin_, end_}; }
40
41 private:
43 T *end_;
44 };
45
48 template <typename T>
50 {
51 public:
53 int size() const { return offsets_.empty() ? 0 : int(offsets_.size()) - 1; }
54 bool empty() const { return size() == 0; }
56 size_t n_entries() const { return data_.size(); }
57
59 {
60 assert(i >= 0 && i < size());
61 return {data_.data() + offsets_[i], data_.data() + offsets_[i + 1]};
62 }
63 ArrayRow<const T> operator[](const int i) const
64 {
65 assert(i >= 0 && i < size());
66 return {data_.data() + offsets_[i], data_.data() + offsets_[i + 1]};
67 }
68 int row_size(const int i) const { return int(offsets_[i + 1] - offsets_[i]); }
69
70 void clear()
71 {
72 offsets_.assign(1, 0);
73 data_.clear();
74 }
75 void reserve(const size_t n_rows, const size_t n_entries)
76 {
77 offsets_.reserve(n_rows + 1);
78 data_.reserve(n_entries);
79 }
80
82 template <typename It>
83 void push_back(It begin, It end)
84 {
85 data_.insert(data_.end(), begin, end);
87 }
88 template <typename Range>
89 void push_back(const Range &row) { push_back(std::begin(row), std::end(row)); }
90 void push_back(std::initializer_list<T> row) { push_back(row.begin(), row.end()); }
92 void push_back_filled(const size_t n, const T &value)
93 {
94 data_.resize(data_.size() + n, value);
96 }
97
99 void assign_sizes(const std::vector<uint32_t> &sizes, const T &value = T())
100 {
101 offsets_.resize(sizes.size() + 1);
102 offsets_[0] = 0;
103 for (size_t i = 0; i < sizes.size(); ++i)
104 offsets_[i + 1] = offsets_[i] + sizes[i];
105 data_.assign(offsets_.back(), value);
106 }
107
109 void append(const JaggedArray &other, const T shift)
110 {
111 const uint32_t n = data_.size();
112 if (offsets_.empty())
113 offsets_.push_back(0);
114 offsets_.reserve(offsets_.size() + other.size());
115 for (int i = 0; i < other.size(); ++i)
116 offsets_.push_back(n + other.offsets_[i + 1]);
117 data_.reserve(n + other.data_.size());
118 for (const T &v : other.data_)
119 data_.push_back(v + shift);
120 }
121
122 size_t memory() const { return offsets_.capacity() * sizeof(uint32_t) + data_.capacity() * sizeof(T); }
123
124 private:
126 {
127 assert(data_.size() <= std::numeric_limits<uint32_t>::max());
128 if (offsets_.empty()) // moved from: the new row is the first one
129 offsets_.push_back(0);
130 offsets_.push_back(data_.size());
131 }
132
133 std::vector<uint32_t> offsets_ = {0};
134 std::vector<T> data_;
135 };
136
137 enum class MeshType
138 {
139 TRI = 0,
140 QUA,
141 H_SUR,
142 TET,
143 HYB,
144 HEX
145 };
146
164 {
165 public:
168 Eigen::MatrixXd points;
169
170 // Vertices: incident vertices, edges, faces and cells.
172 std::vector<uint8_t> vertex_boundary;
173
174 // Edges: endpoints (min, max), incident faces and cells.
175 std::vector<std::array<uint32_t, 2>> edge_vs;
177 std::vector<uint8_t> edge_boundary;
178
179 // Faces: vertices in order, edges aligned with them, incident cells and
180 // the local index of the face in each of these cells.
183 std::vector<uint8_t> face_boundary;
184
185 // Cells.
190 std::vector<uint8_t> cell_hex;
192 std::vector<std::array<double, 3>> cell_kernel;
193
194 int n_vertices() const { return points.cols(); }
195 int n_edges() const { return edge_vs.size(); }
196 int n_faces() const { return face_vs.size(); }
197 int n_cells() const { return cell_fs.size(); }
198
199 // Fixed-size accessors used by the tetrahedral navigation.
200 int EV(const int k, const int e) const { return edge_vs[e][k]; }
201 int FV(const int k, const int f) const { return face_vs[f][k]; }
202 int FE(const int k, const int f) const { return face_es[f][k]; }
203 int HF(const int k, const int c) const { return cell_fs[c][k]; }
204 int FH(const int k, const int f) const { return k < face_hs.row_size(f) ? int(face_hs[f][k]) : -1; }
205 int FHi(const int k, const int f) const { return k < face_hs.row_size(f) ? int(face_hs_slot[f][k]) : -1; }
206
209 void append(const Mesh3DStorage &other);
210
212 size_t memory() const;
213 };
214
216 {
217 std::string Name;
221 Eigen::VectorXd V_Js;
222 Eigen::VectorXd H_Js;
223 Eigen::VectorXd Num_Js;
224 int32_t V_num, H_num;
225 };
226 } // namespace mesh
227} // namespace polyfem
Contiguous view of one row of a JaggedArray (like std::span).
std::vector< std::remove_const_t< T > > to_vector() const
Copy of the row.
T & operator[](const size_t i) const
ArrayRow(T *begin, T *end)
Rows of different lengths stored contiguously (compressed rows): row i is data[offsets[i],...
ArrayRow< T > operator[](const int i)
std::vector< uint32_t > offsets_
int size() const
Number of rows (a moved-from array has none).
void push_back_filled(const size_t n, const T &value)
Appends a row of n copies of value.
int row_size(const int i) const
void push_back(It begin, It end)
Appends a row.
void append(const JaggedArray &other, const T shift)
Appends the rows of other, adding shift to every entry.
void reserve(const size_t n_rows, const size_t n_entries)
ArrayRow< const T > operator[](const int i) const
void assign_sizes(const std::vector< uint32_t > &sizes, const T &value=T())
Replaces the content with rows of the given sizes, filled with value.
size_t n_entries() const
Total number of entries.
void push_back(std::initializer_list< T > row)
void push_back(const Range &row)
Topology of a conforming 3D mesh (used by CMesh3D), stored as flat arrays indexed by entity id.
JaggedArray< uint8_t > cell_fs_flag
JaggedArray< uint32_t > cell_input_vs
input order (rows may be empty)
std::vector< uint8_t > vertex_boundary
Eigen::MatrixXd points
Vertex positions, 3 x #vertices.
size_t memory() const
Bytes allocated by the storage.
std::vector< uint8_t > cell_hex
JaggedArray< uint32_t > face_vs
int HF(const int k, const int c) const
int FE(const int k, const int f) const
JaggedArray< uint32_t > vertex_fs
JaggedArray< uint32_t > face_es
JaggedArray< uint32_t > vertex_es
JaggedArray< uint32_t > face_hs
JaggedArray< uint32_t > edge_fs
std::vector< uint8_t > face_boundary
JaggedArray< uint32_t > cell_vs
navigation order (see above)
JaggedArray< uint32_t > cell_es
int FH(const int k, const int f) const
int EV(const int k, const int e) const
std::vector< std::array< double, 3 > > cell_kernel
A point from which the cell is star-shaped.
JaggedArray< uint32_t > vertex_hs
JaggedArray< uint32_t > vertex_vs
int FV(const int k, const int f) const
JaggedArray< uint32_t > edge_hs
void append(const Mesh3DStorage &other)
Appends the input topology of other (shifting its ids).
JaggedArray< uint16_t > face_hs_slot
std::vector< std::array< uint32_t, 2 > > edge_vs
int FHi(const int k, const int f) const
JaggedArray< uint32_t > cell_fs
std::vector< uint8_t > edge_boundary