PolyFEM
Loading...
Searching...
No Matches
Obstacle.cpp
Go to the documentation of this file.
2
7
8#include <igl/edges.h>
9#include <ipc/utils/eigen_ext.hpp>
10
11namespace polyfem
12{
13 using namespace utils;
14
15 namespace mesh
16 {
18 {
19 clear();
20 }
21
23 {
24 dim_ = 0;
25 v_.resize(0, 0);
26 codim_v_.resize(0);
27 f_.resize(0, 0);
28 e_.resize(0, 0);
29 tets_.resize(0, 0);
30
31 in_f_.resize(0, 0);
32 in_e_.resize(0, 0);
33 in_v_.resize(0);
34 in_tets_.resize(0, 0);
35
36 displacements_.clear();
37
38 endings_.clear();
39
40 planes_.clear();
41 }
42
44 const Eigen::MatrixXd &vertices,
45 const Eigen::VectorXi &codim_vertices,
46 const Eigen::MatrixXi &codim_edges,
47 const Eigen::MatrixXi &faces,
48 const Eigen::MatrixXi &tets)
49 {
50 if (vertices.size() == 0)
51 {
52 // Keep one (empty) entry per obstacle so that endings_ stays aligned
53 // with displacements_.
54 endings_.push_back(v_.rows());
55 return;
56 }
57
58 if (tets.size() && (tets.cols() == 3 || tets.cols() == 4))
59 {
60 const int tc = tets.cols();
61 tets_.conservativeResize(tets_.rows() + tets.rows(), tc);
62 tets_.bottomRows(tets.rows()) = tets.array() + v_.rows();
63
64 in_tets_.conservativeResize(in_tets_.rows() + tets.rows(), tc);
65 in_tets_.bottomRows(tets.rows()) = tets.array() + v_.rows();
66 }
67 else if (tets.size())
68 {
69 log_and_throw_error("Obstacle volumetric cells must be triangles (2D) or tetrahedra (3D)!");
70 }
71
72 if (dim_ == 0)
73 dim_ = vertices.cols();
74 assert(dim_ == vertices.cols());
75
76 if (codim_vertices.size())
77 {
78 codim_v_.conservativeResize(codim_v_.size() + codim_vertices.size());
79 codim_v_.tail(codim_vertices.size()) = codim_vertices.array() + v_.rows();
80
81 in_v_.conservativeResize(in_v_.size() + codim_vertices.size());
82 in_v_.tail(codim_vertices.size()) = codim_vertices.array() + v_.rows();
83 }
84
85 if (codim_edges.size())
86 {
87 e_.conservativeResize(e_.rows() + codim_edges.rows(), 2);
88 e_.bottomRows(codim_edges.rows()) = codim_edges.array() + v_.rows();
89
90 in_e_.conservativeResize(in_e_.rows() + codim_edges.rows(), 2);
91 in_e_.bottomRows(codim_edges.rows()) = codim_edges.array() + v_.rows();
92 }
93
94 if (faces.size() && faces.cols() == 3)
95 {
96 f_.conservativeResize(f_.rows() + faces.rows(), 3);
97 f_.bottomRows(faces.rows()) = faces.array() + v_.rows();
98
99 in_f_.conservativeResize(in_f_.rows() + faces.rows(), 3);
100 in_f_.bottomRows(faces.rows()) = faces.array() + v_.rows();
101
102 Eigen::MatrixXi edges;
103 igl::edges(faces, edges);
104
105 e_.conservativeResize(e_.rows() + edges.rows(), 2);
106 e_.bottomRows(edges.rows()) = edges.array() + v_.rows();
107 }
108 else if (faces.size())
109 {
110 log_and_throw_error("Obstacle supports only segments and triangles!");
111 }
112
113 v_.conservativeResize(v_.rows() + vertices.rows(), dim_);
114 v_.bottomRows(vertices.rows()) = vertices;
115
116 endings_.push_back(v_.rows());
117 }
118
120 const Eigen::MatrixXd &vertices,
121 const Eigen::VectorXi &codim_vertices,
122 const Eigen::MatrixXi &codim_edges,
123 const Eigen::MatrixXi &faces,
124 const json &displacement,
125 const io::ResourceIO &resources,
126 const Eigen::MatrixXi &tets)
127 {
128 append_mesh(vertices, codim_vertices, codim_edges, faces, tets);
129
130 const int dim = dim_ > 0 ? dim_ : vertices.cols();
131 if (!displacement["value"].is_array() || displacement["value"].size() < dim)
132 log_and_throw_error("Obstacle displacement value must have one entry per dimension ({}), got {}.", dim, displacement["value"].dump());
133
134 displacements_.emplace_back();
135 for (size_t d = 0; d < dim; ++d)
136 displacements_.back().value[d].init(displacement["value"][d], resources);
137
138 if (displacement.contains("interpolation"))
139 {
140 if (displacement["interpolation"].is_array())
141 {
142 for (int ii = 0; ii < displacement["interpolation"].size(); ++ii)
143 displacements_.back().interpolation.emplace_back(Interpolation::build(displacement["interpolation"][ii]));
144 }
145 else
146 displacements_.back().interpolation.emplace_back(Interpolation::build(displacement["interpolation"]));
147 }
148 }
149
151 const std::vector<Eigen::MatrixXd> &vertices,
152 const Eigen::VectorXi &codim_vertices,
153 const Eigen::MatrixXi &codim_edges,
154 const Eigen::MatrixXi &faces,
155 const int fps,
156 const Eigen::MatrixXi &tets)
157 {
158 if (vertices.size() == 0 || vertices[0].size() == 0)
159 return;
160 for (size_t i = 1; i < vertices.size(); ++i)
161 if (vertices[i].rows() != vertices[0].rows() || vertices[i].cols() != vertices[0].cols())
162 log_and_throw_error("Obstacle mesh sequence: frame {} has {}x{} vertices, the first frame has {}x{}.",
163 i, vertices[i].rows(), vertices[i].cols(), vertices[0].rows(), vertices[0].cols());
164
165 append_mesh(vertices[0], codim_vertices, codim_edges, faces, tets);
166
167 std::array<std::vector<Eigen::MatrixXd>, 3> displacements_xyz;
168 for (size_t i = 0; i < vertices.size(); ++i)
169 {
170 Eigen::MatrixXd displacement = vertices[i] - vertices[0];
171 for (size_t d = 0; d < dim_; ++d)
172 displacements_xyz[d].push_back(displacement.col(d));
173 }
174
175 displacements_.emplace_back();
176 for (size_t d = 0; d < dim_; ++d)
177 {
178 const std::vector<Eigen::MatrixXd> displacements = displacements_xyz[d];
179 displacements_.back().value[d].init(
180 [displacements, fps](double x, double y, double z, double t, int index) -> double {
181 const double frame = t * fps;
182 if (frame <= 0)
183 return displacements.front()(index);
184 const double interp = frame - floor(frame);
185 const int frame0 = (int)floor(frame);
186 const int frame1 = (int)ceil(frame);
187 if (frame1 >= displacements.size())
188 return displacements.back()(index);
189 const double u0 = displacements[frame0](index);
190 const double u1 = displacements[frame1](index);
191 return (u1 - u0) * interp + u0;
192 });
193 }
194 }
195
196 void Obstacle::append_plane(const VectorNd &origin, const VectorNd &normal)
197 {
198 if (dim_ == 0)
199 dim_ = origin.size();
200 assert(normal.size() >= dim_);
201 assert(origin.size() >= dim_);
202 planes_.emplace_back(origin.head(dim_), normal.head(dim_).normalized());
203 }
204
205 void Obstacle::change_displacement(const int oid, const Eigen::RowVector3d &val, const std::string &interp)
206 {
207 change_displacement(oid, val, interp.empty() ? std::make_shared<NoInterpolation>() : Interpolation::build(interp));
208 }
209 void Obstacle::change_displacement(const int oid, const std::function<Eigen::MatrixXd(double x, double y, double z, double t)> &func, const std::string &interp)
210 {
211 change_displacement(oid, func, interp.empty() ? std::make_shared<NoInterpolation>() : Interpolation::build(interp));
212 }
213 void Obstacle::change_displacement(const int oid, const json &val, const io::ResourceIO &resources, const std::string &interp)
214 {
215 change_displacement(oid, val, interp.empty() ? std::make_shared<NoInterpolation>() : Interpolation::build(interp), resources);
216 }
217
218 void Obstacle::change_displacement(const int oid, const Eigen::RowVector3d &val, const std::shared_ptr<Interpolation> &interp)
219 {
220 for (size_t k = 0; k < val.size(); ++k)
221 displacements_[oid].value[k].init(val[k]);
222
223 displacements_[oid].interpolation.clear();
224 displacements_[oid].interpolation.push_back(interp);
225 }
226
227 void Obstacle::change_displacement(const int oid, const std::function<Eigen::MatrixXd(double x, double y, double z, double t)> &func, const std::shared_ptr<Interpolation> &interp)
228 {
229 for (size_t k = 0; k < displacements_.back().value.size(); ++k)
230 displacements_[oid].value[k].init(func, k);
231
232 displacements_[oid].interpolation.clear();
233 displacements_[oid].interpolation.push_back(interp);
234 }
235
236 void Obstacle::change_displacement(const int oid, const json &val, const io::ResourceIO &resources, const std::shared_ptr<Interpolation> &interp)
237 {
238 change_displacement(oid, val, interp, resources);
239 }
240 void Obstacle::change_displacement(const int oid, const json &val, const std::shared_ptr<Interpolation> &interp, const io::ResourceIO &resources)
241 {
242 for (size_t k = 0; k < val.size(); ++k)
243 displacements_[oid].value[k].init(val[k], resources);
244
245 displacements_[oid].interpolation.clear();
246 displacements_[oid].interpolation.push_back(interp);
247 }
248
249 void Obstacle::update_displacement(const double t, Eigen::MatrixXd &sol) const
250 {
251 // NOTE: assumes obstacle displacements is stored at the bottom of sol
252 const int offset = sol.rows() - v_.rows() * (sol.cols() == 1 ? dim_ : 1);
253
254 int start = 0;
255
256 for (int k = 0; k < endings_.size(); ++k)
257 {
258 const int to = endings_[k];
259 const auto &disp = displacements_[k];
260
261 for (int i = start; i < to; ++i)
262 {
263 for (int d = 0; d < dim_; ++d)
264 {
265 const int sol_row = sol.cols() == 1 ? (offset + i * dim_ + d) : (offset + i);
266 const int sol_col = sol.cols() == 1 ? 0 : d;
267 sol(sol_row, sol_col) = disp.eval(v_.row(i), d, t, i - start);
268 }
269 }
270
271 start = to;
272 }
273 assert(endings_.empty() || (start * (sol.cols() == 1 ? dim_ : 1) + offset) == sol.rows());
274 }
275
276 void Obstacle::set_zero(Eigen::MatrixXd &sol) const
277 {
278 // NOTE: assumes obstacle displacements is stored at the bottom of sol
279 sol.bottomRows(v_.rows() * (sol.cols() == 1 ? dim_ : 1)).setZero();
280 }
281
282 void Obstacle::set_units(const Units &units)
283 {
284 for (auto &d : displacements_)
285 {
286 d.set_unit_type(units.length());
287 }
288 }
289
291 {
292 constexpr int size_x = 10;
293 constexpr int size_y = 10;
294
295 if (dim_ == 2)
296 {
297 Eigen::Vector2d tangent(normal().y(), -normal().x());
298
299 vis_v_.resize(size_x + 1, 2);
300 for (int x = 0; x <= size_x; ++x)
301 {
302 vis_v_.row(x) = (x - size_x / 2.0) * tangent;
303 }
304
305 vis_e_.resize(size_x, 2);
306 for (int x = 0; x < size_x; ++x)
307 {
308 vis_e_.row(x) << x, x + 1;
309 }
310 }
311 else
312 {
313 assert(dim_ == 3);
314
315 const Eigen::Vector3d normal = this->normal();
316
317 const Eigen::Vector3d cross_x = Eigen::Vector3d::UnitX().cross(normal);
318 const Eigen::Vector3d cross_y = Eigen::Vector3d::UnitY().cross(normal);
319
320 Eigen::Vector3d tangent_x, tangent_y;
321 if (cross_x.squaredNorm() > cross_y.squaredNorm())
322 {
323 tangent_x = cross_x.normalized();
324 tangent_y = normal.cross(cross_x).normalized();
325 }
326 else
327 {
328 tangent_x = cross_y.normalized();
329 tangent_y = normal.cross(cross_y).normalized();
330 }
331
332 vis_v_.resize((size_x + 1) * (size_y + 1), 3);
333 for (int x = 0; x <= size_x; ++x)
334 {
335 for (int y = 0; y <= size_y; ++y)
336 {
337 vis_v_.row(x * size_y + y) = (x - size_x / 2.0) * tangent_x + (y - size_y / 2.0) * tangent_y;
338 }
339 }
340
341 vis_f_.resize(2 * size_x * size_y, 3);
342 for (int x = 0; x < size_x; ++x)
343 {
344 for (int y = 0; y < size_y; ++y)
345 {
346 vis_f_.row(2 * (x * size_x + y)) << x * size_x + y, x * size_x + y + 1, (x + 1) * size_x + y;
347 vis_f_.row(2 * (x * size_x + y) + 1) << x * size_x + y + 1, (x + 1) * size_x + (y + 1), (x + 1) * size_x + y;
348 }
349 }
350
351 igl::edges(vis_f_, vis_e_);
352 }
353 }
354
355 } // namespace mesh
356} // namespace polyfem
double val
std::vector< Eigen::VectorXi > faces
int y
int z
int x
const std::string & length() const
Definition Units.hpp:19
Read-only interface to a hierarchical collection of simulation resources.
const VectorNd & normal() const
Definition Obstacle.hpp:119
std::vector< Plane > planes_
Definition Obstacle.hpp:103
void change_displacement(const int oid, const Eigen::RowVector3d &val, const std::shared_ptr< utils::Interpolation > &interp=std::make_shared< utils::NoInterpolation >())
Eigen::MatrixXi f_
Definition Obstacle.hpp:90
Eigen::MatrixXi e_
Definition Obstacle.hpp:91
void append_mesh(const Eigen::MatrixXd &vertices, const Eigen::VectorXi &codim_vertices, const Eigen::MatrixXi &codim_edges, const Eigen::MatrixXi &faces, const json &displacement, const io::ResourceIO &resources, const Eigen::MatrixXi &tets=Eigen::MatrixXi())
Definition Obstacle.cpp:119
void append_mesh_sequence(const std::vector< Eigen::MatrixXd > &vertices, const Eigen::VectorXi &codim_vertices, const Eigen::MatrixXi &codim_edges, const Eigen::MatrixXi &faces, const int fps, const Eigen::MatrixXi &tets=Eigen::MatrixXi())
Definition Obstacle.cpp:150
const Eigen::MatrixXi & tets() const
Definition Obstacle.hpp:49
void append_plane(const VectorNd &point, const VectorNd &normal)
Definition Obstacle.cpp:196
Eigen::MatrixXi in_f_
Definition Obstacle.hpp:95
Eigen::MatrixXi in_tets_
Definition Obstacle.hpp:97
Eigen::MatrixXd v_
Definition Obstacle.hpp:88
void set_zero(Eigen::MatrixXd &sol) const
Definition Obstacle.cpp:276
Eigen::VectorXi codim_v_
Definition Obstacle.hpp:89
void update_displacement(const double t, Eigen::MatrixXd &sol) const
Definition Obstacle.cpp:249
void set_units(const Units &units)
Definition Obstacle.cpp:282
Eigen::MatrixXi tets_
Definition Obstacle.hpp:92
Eigen::MatrixXi in_e_
Definition Obstacle.hpp:96
Eigen::VectorXi in_v_
Definition Obstacle.hpp:94
std::vector< assembler::TensorBCValue > displacements_
Definition Obstacle.hpp:99
std::vector< int > endings_
Definition Obstacle.hpp:101
static std::shared_ptr< Interpolation > build(const json &params)
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Definition Types.hpp:11
nlohmann::json json
Definition Common.hpp:9
void log_and_throw_error(const std::string &msg)
Definition Logger.cpp:73