13#include <igl/boundary_facets.h>
23 const std::vector<json> &obstacle_displacements,
24 const std::vector<json> &dirichlet_conditions,
25 const bool non_conforming)
const
32 geometry, obstacle_displacements, dirichlet_conditions,
33 result.
fem->dimension(), non_conforming);
39 const json &geometry_selection)
const
41 if (geometry_selection.is_object() && geometry_selection.contains(
"same_as_volume"))
43 if (!geometry_selection[
"same_as_volume"].get<bool>())
46 std::vector<int> geometry_ids(mesh.
n_elements());
57 Eigen::MatrixXd barycenters;
59 std::vector<int> geometry_ids(mesh.
n_elements(), 0);
63 for (
const auto &selection : selections)
65 if (selection->inside(e, vertices, barycenters.row(e)))
67 geometry_ids[e] = selection->id(e, vertices, barycenters.row(e));
77 const bool non_conforming)
const
82 if (j_mesh[
"extract"].get<std::string>() !=
"volume")
89 if (j_mesh[
"advanced"][
"normalize_mesh"])
95 mesh->bounding_box(bbox[0], bbox[1]);
97 const std::string unit = j_mesh[
"unit"];
98 double unit_scale = 1;
107 j_mesh[
"transformation"],
108 (bbox[1] - bbox[0]).cwiseAbs().transpose(),
110 mesh->apply_affine_transformation(A, b);
113 mesh->bounding_box(bbox[0], bbox[1]);
116 std::vector<std::shared_ptr<Selection>> surface_selections =
121 const int n_refs = j_mesh[
"n_refs"];
122 const double refinement_location = j_mesh[
"advanced"][
"refinement_location"];
133 if (mesh->has_boundary_ids() && surface_selections.empty())
134 log_and_throw_error(
"Unable to refine a mesh with stored surface selections; provide an explicit surface_selection to recompute them after refinement.");
137 assert(mesh->n_elements() > 0);
138 const int uniform_value = mesh->get_body_id(0);
139 for (
int i = 1; i < mesh->n_elements(); ++i)
140 if (mesh->get_body_id(i) != uniform_value)
141 log_and_throw_error(
"Unable to apply stored nonuniform volume_selection because n_refs={} > 0!", n_refs);
143 logger().info(
"Performing global h-refinement with {} refinements", n_refs);
144 mesh->refine(n_refs, refinement_location);
145 mesh->set_body_ids(std::vector<int>(mesh->n_elements(), uniform_value));
150 if (j_mesh[
"advanced"][
"min_component"].get<int>() != -1)
182 if (j_mesh[
"advanced"][
"force_linear_geometry"].get<bool>())
196 const std::vector<std::shared_ptr<Selection>> node_selections =
199 if (!node_selections.empty())
201 bool boundary_only =
true;
202 for (
const auto &selection : node_selections)
204 if (!selection->boundary_only())
206 boundary_only =
false;
211 mesh->compute_node_ids([&](
const size_t n_id,
const RowVectorNd &p,
bool is_boundary) {
212 if (boundary_only && !is_boundary)
215 const std::vector<int> tmp = {int(n_id)};
216 for (
const auto &selection : node_selections)
218 if (selection->boundary_only() && !is_boundary)
221 if (selection->inside(n_id, tmp, p))
222 return selection->id(n_id, tmp, p);
224 return std::numeric_limits<int>::max();
228 if (!j_mesh[
"curve_selection"].is_null())
233 if (!surface_selections.empty())
235 bool boundary_only =
true;
236 for (
const auto &selection : surface_selections)
238 if (!selection->boundary_only())
240 boundary_only =
false;
245 mesh->compute_boundary_ids([&](
const size_t p_id,
const std::vector<int> &vs,
const RowVectorNd &p,
bool is_boundary) {
246 if (boundary_only && !is_boundary)
249 for (
const auto &selection : surface_selections)
251 if (selection->boundary_only() && !is_boundary)
254 if (selection->inside(p_id, vs, p))
255 return selection->id(p_id, vs, p);
257 return std::numeric_limits<int>::max();
264 const json volume_selection = j_mesh[
"volume_selection"];
265 if (volume_selection.is_object()
266 && volume_selection.size() == 1
267 && volume_selection.contains(
"id_offset"))
269 const int id_offset = volume_selection[
"id_offset"].get<
int>();
272 const int n_body_ids = mesh->n_elements();
273 std::vector<int> body_ids(n_body_ids);
274 for (
int i = 0; i < n_body_ids; ++i)
275 body_ids[i] = mesh->get_body_id(i) + id_offset;
276 mesh->set_body_ids(body_ids);
282 std::vector<std::shared_ptr<Selection>> volume_selections =
286 if (mesh->has_body_ids())
287 volume_selections.push_back(std::make_shared<SpecifiedSelection>(mesh->get_body_ids()));
289 mesh->compute_body_ids([&](
const size_t cell_id,
const std::vector<int> &vs,
const RowVectorNd &p) ->
int {
290 for (
const auto &selection : volume_selections)
293 if (selection->inside(cell_id, vs, p))
294 return selection->id(cell_id, vs, p);
313 const json &geometry,
314 const bool non_conforming)
const
318 if (geometry.empty())
325 std::unique_ptr<Mesh> mesh =
nullptr;
327 for (
const json &geometry : geometries)
329 if (!geometry[
"enabled"].get<bool>() || geometry[
"is_obstacle"].get<
bool>())
332 if (geometry[
"type"] !=
"mesh" && geometry[
"type"] !=
"mesh_array")
335 const std::unique_ptr<Mesh> tmp_mesh =
load_fem_entry(geometry, non_conforming);
338 mesh = tmp_mesh->copy();
340 mesh->append(tmp_mesh);
342 if (geometry[
"type"] ==
"mesh_array")
345 tmp_mesh->bounding_box(bbox[0], bbox[1]);
347 const long dim = tmp_mesh->dimension();
348 const bool is_offset_relative = geometry[
"array"][
"relative"];
349 const double offset = geometry[
"array"][
"offset"];
350 const VectorNd dimensions = (bbox[1] - bbox[0]);
351 const VectorNi size = geometry[
"array"][
"size"];
353 for (
int i = 0; i < size[0]; ++i)
355 for (
int j = 0; j < size[1]; ++j)
357 for (
int k = 0; k < (size.size() > 2 ? size[2] : 1); ++k)
359 if (i == 0 && j == 0 && k == 0)
362 RowVectorNd translation = offset * Eigen::RowVector3d(i, j, k).head(dim);
363 if (is_offset_relative)
364 translation.array() *= dimensions.array();
366 const std::unique_ptr<Mesh> copy_mesh = tmp_mesh->copy();
367 copy_mesh->apply_affine_transformation(MatrixNd::Identity(dim, dim), translation);
368 mesh->append(copy_mesh);
391 auto &codim_vertices = result.
points;
392 auto &codim_edges = result.
edges;
395 const int prev_dim = vertices.cols();
396 vertices.conservativeResize(vertices.rows(), dim);
398 vertices.rightCols(dim - prev_dim).setZero();
403 const std::string unit = j_mesh[
"unit"];
404 double unit_scale = 1;
408 const VectorNd mesh_dimensions = (vertices.colwise().maxCoeff() - vertices.colwise().minCoeff()).cwiseAbs();
412 vertices = vertices * A.transpose();
413 vertices.rowwise() += b.transpose();
416 std::string extract = j_mesh[
"extract"];
418 if (extract ==
"volume")
421 if (extract ==
"points")
424 codim_edges.resize(0, 0);
426 codim_vertices.resize(vertices.rows());
427 for (
int i = 0; i < codim_vertices.size(); ++i)
428 codim_vertices[i] = i;
430 else if (extract ==
"edges" &&
faces.size() != 0)
433 Eigen::MatrixXi edges;
434 igl::edges(
faces, edges);
436 codim_edges.conservativeResize(codim_edges.rows() + edges.rows(), 2);
437 codim_edges.bottomRows(edges.rows()) = edges;
439 else if (extract ==
"surface" && dim == 2 &&
faces.size() != 0)
442 Eigen::MatrixXi boundary_edges;
443 igl::boundary_facets(
faces, boundary_edges);
444 codim_edges.conservativeResize(codim_edges.rows() + boundary_edges.rows(), 2);
445 codim_edges.bottomRows(boundary_edges.rows()) = boundary_edges;
450 else if (extract ==
"volume")
456 if (j_mesh[
"n_refs"].get<int>() != 0)
458 if (
faces.size() != 0)
461 const int n_refs = j_mesh[
"n_refs"];
462 const double refinement_location = j_mesh[
"advanced"][
"refinement_location"];
463 for (
int i = 0; i < n_refs; i++)
465 const size_t n_vertices = vertices.rows();
466 const size_t n_edges = codim_edges.rows();
467 vertices.conservativeResize(n_vertices + n_edges, vertices.cols());
468 codim_edges.conservativeResize(2 * n_edges, codim_edges.cols());
469 for (
size_t ei = 0; ei < n_edges; ei++)
471 const int v0i = codim_edges(ei, 0);
472 const int v1i = codim_edges(ei, 1);
473 const int v2i = n_vertices + ei;
474 vertices.row(v2i) = (vertices.row(v1i) - vertices.row(v0i)) * refinement_location + vertices.row(v0i);
475 codim_edges.row(ei) << v0i, v2i;
476 codim_edges.row(n_edges + ei) << v2i, v1i;
486 const json &geometry,
487 const std::vector<json> &displacements,
488 const std::vector<json> &dirichlets,
494 if (geometry.empty())
499 for (
const json &geometry : geometries)
502 if (!geometry[
"is_obstacle"].get<bool>())
505 if (!geometry[
"enabled"].get<bool>())
508 if (geometry[
"type"] ==
"mesh" || geometry[
"type"] ==
"mesh_array")
511 Eigen::MatrixXd &vertices = loaded.
vertices;
512 Eigen::VectorXi &codim_vertices = loaded.
points;
513 Eigen::MatrixXi &codim_edges = loaded.
edges;
516 if (geometry[
"type"] ==
"mesh_array")
518 const Selection::BBox bbox{{vertices.colwise().minCoeff(), vertices.colwise().maxCoeff()}};
520 const bool is_offset_relative = geometry[
"array"][
"relative"];
521 const double offset = geometry[
"array"][
"offset"];
522 const VectorNd dimensions = (bbox[1] - bbox[0]);
523 const VectorNi size = geometry[
"array"][
"size"];
525 const int N = size.head(dim).prod();
526 const int nV = vertices.rows(), nCV = codim_vertices.rows(), nCE = codim_edges.rows(), nF =
faces.rows();
528 vertices.conservativeResize(N * nV, Eigen::NoChange);
529 codim_vertices.conservativeResize(N * nCV, Eigen::NoChange);
530 codim_edges.conservativeResize(N * nCE, Eigen::NoChange);
531 faces.conservativeResize(N * nF, Eigen::NoChange);
533 for (
int i = 0; i < size[0]; ++i)
535 for (
int j = 0; j < size[1]; ++j)
537 for (
int k = 0; k < (size.size() > 2 ? size[2] : 1); ++k)
539 RowVectorNd translation = offset * Eigen::RowVector3d(i, j, k).head(vertices.cols());
540 if (is_offset_relative)
541 translation.array() *= dimensions.array();
543 int n = i * size[1] + j;
549 vertices.middleRows(n * nV, nV) = vertices.topRows(nV).rowwise() + translation;
551 codim_vertices.segment(n * nV, nV) = codim_vertices.head(nV).array() + n * nV;
553 codim_edges.middleRows(n * nCE, nCE) = codim_edges.topRows(nCE).array() + n * nV;
555 faces.middleRows(n * nF, nF) =
faces.topRows(nF).array() + n * nV;
561 json displacement =
"{\"value\":[0, 0, 0]}"_json;
564 if (!geometry[
"surface_selection"].is_number())
567 const int id = geometry[
"surface_selection"];
568 for (
const json &disp : dirichlets)
570 if ((disp[
"id"].is_string() && disp[
"id"].get<std::string>() ==
"all")
571 || (disp[
"id"].is_number_integer() && disp[
"id"].get<
int>() ==
id))
576 else if (disp[
"id"].is_array())
578 for (
const json &disp_id : disp[
"id"])
580 assert(disp_id.is_number_integer());
581 if (disp_id.get<
int>() ==
id)
589 for (
const json &disp : displacements)
591 if ((disp[
"id"].is_string() && disp[
"id"].get<std::string>() ==
"all")
592 || (disp[
"id"].is_number_integer() && disp[
"id"].get<
int>() ==
id))
597 else if (disp[
"id"].is_array())
599 for (
const json &disp_id : disp[
"id"])
601 assert(disp_id.is_number_integer());
602 if (disp_id.get<
int>() ==
id)
613 vertices, codim_vertices, codim_edges,
faces, displacement,
resources_);
615 else if (geometry[
"type"] ==
"plane")
617 obstacle.
append_plane(geometry[
"point"], geometry[
"normal"]);
619 else if (geometry[
"type"] ==
"ground")
621 VectorNd gravity = VectorNd::Zero(dim);
623 const double height = geometry[
"height"];
624 assert(gravity.norm() != 0);
625 const VectorNd normal = -gravity.normalized();
629 else if (geometry[
"type"] ==
"mesh_sequence")
631 std::vector<std::string> mesh_files;
632 if (geometry[
"mesh_sequence"].is_array())
634 mesh_files = geometry[
"mesh_sequence"].get<std::vector<std::string>>();
638 assert(geometry[
"mesh_sequence"].is_string());
639 const std::string sequence = geometry[
"mesh_sequence"].get<std::string>();
642 std::sort(mesh_files.begin(), mesh_files.end(), [](
const std::string &p1,
const std::string &p2) {
643 return strnatcmp(p1.c_str(), p2.c_str()) < 0;
647 std::vector<Eigen::MatrixXd> vertices(mesh_files.size());
648 Eigen::VectorXi codim_vertices;
649 Eigen::MatrixXi codim_edges;
650 Eigen::MatrixXi
faces;
652 for (
int i = 0; i < mesh_files.size(); ++i)
654 json jmesh = geometry;
655 jmesh[
"mesh"] = mesh_files[i];
659 vertices[i] = std::move(loaded.
vertices);
660 Eigen::VectorXi &tmp_codim_vertices = loaded.
points;
661 Eigen::MatrixXi &tmp_codim_edges = loaded.
edges;
662 Eigen::MatrixXi &tmp_faces = loaded.
faces;
665 codim_vertices = tmp_codim_vertices;
666 codim_edges = tmp_codim_edges;
671 assert((codim_vertices.array() == tmp_codim_vertices.array()).all());
672 assert((codim_edges.array() == tmp_codim_edges.array()).all());
673 assert((
faces.array() == tmp_faces.array()).all());
678 vertices, codim_vertices, codim_edges,
faces, geometry[
"fps"]);
693 const double unit_scale,
694 const json &transform,
699 const int dim = mesh_dimensions.size();
706 if (transform[
"dimensions"].is_array())
709 (mesh_dimensions.array() == 0).select(1, mesh_dimensions);
711 scale = transform[
"dimensions"];
712 const int scale_size = scale.size();
713 scale.conservativeResize(dim);
714 if (scale_size < dim)
715 scale.tail(dim - scale_size).setZero();
717 scale.array() /= modified_dimensions.array();
719 else if (transform[
"scale"].is_number())
721 scale.setConstant(dim, transform[
"scale"].get<double>());
725 assert(transform[
"scale"].is_array());
726 scale = transform[
"scale"];
727 const int scale_size = scale.size();
728 scale.conservativeResize(dim);
729 if (scale_size < dim)
730 scale.tail(dim - scale_size).setZero();
736 A = (unit_scale * scale).asDiagonal();
744 MatrixNd R = MatrixNd::Identity(dim, dim);
745 if (!transform[
"rotation"].is_null())
749 if (transform[
"rotation"].is_number())
750 R = Eigen::Rotation2Dd(
deg2rad(transform[
"rotation"].get<double>()))
752 else if (!transform[
"rotation"].is_array() || !transform[
"rotation"].empty())
767 b = transform[
"translation"];
768 const int translation_size = b.size();
769 b.conservativeResize(dim);
770 if (translation_size < dim)
771 b.tail(dim - translation_size).setZero();
std::vector< Eigen::VectorXi > faces
static double convert(const json &val, const std::string &unit_type)
const std::string & length() const
virtual std::vector< std::string > list(const std::string &path) const =0
std::vector< std::string > glob(const std::string &pattern) const
Glob normalized logical paths using POSIX '*' and '?' wildcards.
virtual bool is_group(const std::string &path) const =0
std::unique_ptr< Mesh > load_fem(const json &geometry, bool non_conforming=false) const
Load and combine the enabled FEM entries in a geometry configuration.
Obstacle load_obstacles(const json &geometry, const std::vector< json > &obstacle_displacements, const std::vector< json > &dirichlet_conditions, int dimension, bool non_conforming=false) const
const io::ResourceIO & resources_
LoadedGeometry load(const json &geometry, const std::vector< json > &obstacle_displacements, const std::vector< json > &dirichlet_conditions, bool non_conforming=false) const
static void construct_affine_transformation(double unit_scale, const json &transform, const VectorNd &mesh_dimensions, MatrixNd &A, VectorNd &b)
Construct the affine map used by geometry and collision-proxy inputs.
void apply_geometry_selection(Mesh &mesh, const json &geometry_selection) const
Apply configured geometry IDs to an already constructed mesh.
SurfaceMesh load_surface(const json &geometry, int dimension) const
Load and prepare one configured surface/codimensional geometry entry.
std::unique_ptr< Mesh > load_fem_entry(const json &geometry, bool non_conforming=false) const
std::unique_ptr< Mesh > fem
Abstract mesh class to capture 2d/3d conforming and non-conforming meshes.
int n_elements() const
utitlity to return the number of elements, cells or faces in 3d and 2d
virtual int get_body_id(const int primitive) const
Get the volume selection of an element (cell in 3d, face in 2d)
virtual void compute_element_barycenters(Eigen::MatrixXd &barycenters) const =0
utility for 2d/3d.
virtual void bounding_box(RowVectorNd &min, RowVectorNd &max) const =0
computes the bbox of the mesh
void set_geometry_ids(const std::vector< int > &geometry_ids)
Set the geometry selection, one ID per element.
std::vector< int > element_vertices(const int el_id) const
list of vids of an element
The only non-legacy decoder from a logical resource path to mesh data.
std::unique_ptr< Mesh > load_fem(const std::string &path, bool non_conforming=false) const
SurfaceMesh load_surface(const std::string &path) const
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)
void append_plane(const VectorNd &point, const VectorNd &normal)
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)
void set_units(const Units &units)
static std::vector< std::shared_ptr< utils::Selection > > build_selections(const json &j_selections, const BBox &mesh_bbox, const io::ResourceIO &resources)
Build a vector of selection objects from a JSON selection(s).
std::array< RowVectorNd, 2 > BBox
Eigen::Matrix3d to_rotation_matrix(const json &jr, std::string mode)
std::vector< T > json_as_array(const json &j)
Return the value of a json object as an array.
bool is_param_valid(const json ¶ms, const std::string &key)
Determine if a key exists and is non-null in a json object.
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, Eigen::Dynamic, 1, 0, 3, 1 > VectorNd
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor, 3, 3 > MatrixNd
Eigen::Matrix< int, Eigen::Dynamic, 1, 0, 3, 1 > VectorNi
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)