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.");
135 if (mesh->has_node_ids() && !
is_param_valid(j_mesh,
"point_selection"))
136 log_and_throw_error(
"Unable to refine a mesh with stored point selections; provide an explicit point_selection to recompute them after refinement.");
137 if (mesh->has_geometry_ids() && !
is_param_valid(j_mesh,
"geometry_selection"))
138 log_and_throw_error(
"Unable to refine a mesh with stored geometry selections; provide an explicit geometry_selection to recompute them after refinement.");
139 if (mesh->orders().size() > 0 && mesh->orders().maxCoeff() > 1)
140 logger().warn(
"Refinement drops the higher-order geometry of the mesh; the refined mesh is linear.");
143 assert(mesh->n_elements() > 0);
144 const int uniform_value = mesh->get_body_id(0);
145 for (
int i = 1; i < mesh->n_elements(); ++i)
146 if (mesh->get_body_id(i) != uniform_value)
147 log_and_throw_error(
"Unable to apply stored nonuniform volume_selection because n_refs={} > 0!", n_refs);
149 logger().info(
"Performing global h-refinement with {} refinements", n_refs);
150 mesh->refine(n_refs, refinement_location);
151 mesh->set_body_ids(std::vector<int>(mesh->n_elements(), uniform_value));
156 if (j_mesh[
"advanced"][
"min_component"].get<int>() != -1)
188 if (j_mesh[
"advanced"][
"force_linear_geometry"].get<bool>())
202 const std::vector<std::shared_ptr<Selection>> node_selections =
205 if (!node_selections.empty())
207 bool boundary_only =
true;
208 for (
const auto &selection : node_selections)
210 if (!selection->boundary_only())
212 boundary_only =
false;
217 mesh->compute_node_ids([&](
const size_t n_id,
const RowVectorNd &p,
bool is_boundary) {
218 if (boundary_only && !is_boundary)
221 const std::vector<int> tmp = {int(n_id)};
222 for (
const auto &selection : node_selections)
224 if (selection->boundary_only() && !is_boundary)
227 if (selection->inside(n_id, tmp, p))
228 return selection->id(n_id, tmp, p);
230 return std::numeric_limits<int>::max();
234 if (!j_mesh[
"curve_selection"].is_null())
239 if (!surface_selections.empty())
241 bool boundary_only =
true;
242 for (
const auto &selection : surface_selections)
244 if (!selection->boundary_only())
246 boundary_only =
false;
251 mesh->compute_boundary_ids([&](
const size_t p_id,
const std::vector<int> &vs,
const RowVectorNd &p,
bool is_boundary) {
252 if (boundary_only && !is_boundary)
255 for (
const auto &selection : surface_selections)
257 if (selection->boundary_only() && !is_boundary)
260 if (selection->inside(p_id, vs, p))
261 return selection->id(p_id, vs, p);
263 return std::numeric_limits<int>::max();
270 const json volume_selection = j_mesh[
"volume_selection"];
271 if (volume_selection.is_object()
272 && volume_selection.size() == 1
273 && volume_selection.contains(
"id_offset"))
275 const int id_offset = volume_selection[
"id_offset"].get<
int>();
278 const int n_body_ids = mesh->n_elements();
279 std::vector<int> body_ids(n_body_ids);
280 for (
int i = 0; i < n_body_ids; ++i)
281 body_ids[i] = mesh->get_body_id(i) + id_offset;
282 mesh->set_body_ids(body_ids);
288 std::vector<std::shared_ptr<Selection>> volume_selections =
292 if (mesh->has_body_ids())
293 volume_selections.push_back(std::make_shared<SpecifiedSelection>(mesh->get_body_ids()));
295 mesh->compute_body_ids([&](
const size_t cell_id,
const std::vector<int> &vs,
const RowVectorNd &p) ->
int {
296 for (
const auto &selection : volume_selections)
299 if (selection->inside(cell_id, vs, p))
300 return selection->id(cell_id, vs, p);
319 const json &geometry,
320 const bool non_conforming)
const
324 if (geometry.empty())
331 std::unique_ptr<Mesh> mesh =
nullptr;
333 for (
const json &geometry : geometries)
335 if (!geometry[
"enabled"].get<bool>() || geometry[
"is_obstacle"].get<
bool>())
338 if (geometry[
"type"] !=
"mesh" && geometry[
"type"] !=
"mesh_array")
341 const std::unique_ptr<Mesh> tmp_mesh =
load_fem_entry(geometry, non_conforming);
344 mesh = tmp_mesh->copy();
346 mesh->append(tmp_mesh);
348 if (geometry[
"type"] ==
"mesh_array")
351 tmp_mesh->bounding_box(bbox[0], bbox[1]);
353 const long dim = tmp_mesh->dimension();
354 const bool is_offset_relative = geometry[
"array"][
"relative"];
355 const double offset = geometry[
"array"][
"offset"];
356 const VectorNd dimensions = (bbox[1] - bbox[0]);
357 const VectorNi size = geometry[
"array"][
"size"];
358 if (size.size() != dim)
359 log_and_throw_error(
"mesh_array: \"array/size\" needs one entry per dimension ({}), got {}.", dim, size.size());
361 for (
int i = 0; i < size[0]; ++i)
363 for (
int j = 0; j < size[1]; ++j)
365 for (
int k = 0; k < (size.size() > 2 ? size[2] : 1); ++k)
367 if (i == 0 && j == 0 && k == 0)
370 RowVectorNd translation = offset * Eigen::RowVector3d(i, j, k).head(dim);
371 if (is_offset_relative)
372 translation.array() *= dimensions.array();
374 const std::unique_ptr<Mesh> copy_mesh = tmp_mesh->copy();
375 copy_mesh->apply_affine_transformation(MatrixNd::Identity(dim, dim), translation);
376 mesh->append(copy_mesh);
399 auto &codim_vertices = result.
points;
400 auto &codim_edges = result.
edges;
402 auto &tets = result.
tets;
404 const int prev_dim = vertices.cols();
405 vertices.conservativeResize(vertices.rows(), dim);
407 vertices.rightCols(dim - prev_dim).setZero();
412 const std::string unit = j_mesh[
"unit"];
413 double unit_scale = 1;
417 const VectorNd mesh_dimensions = (vertices.colwise().maxCoeff() - vertices.colwise().minCoeff()).cwiseAbs();
421 vertices = vertices * A.transpose();
422 vertices.rowwise() += b.transpose();
427 if (dim == 2 && tets.rows() == 0 &&
faces.rows() > 0 &&
faces.cols() == 3)
430 std::string extract = j_mesh[
"extract"];
432 if (extract ==
"volume")
435 if (extract ==
"points")
438 codim_edges.resize(0, 0);
440 codim_vertices.resize(vertices.rows());
441 for (
int i = 0; i < codim_vertices.size(); ++i)
442 codim_vertices[i] = i;
444 else if (extract ==
"edges" &&
faces.size() != 0)
447 Eigen::MatrixXi edges;
448 igl::edges(
faces, edges);
450 codim_edges.conservativeResize(codim_edges.rows() + edges.rows(), 2);
451 codim_edges.bottomRows(edges.rows()) = edges;
453 else if (extract ==
"surface" && dim == 2 &&
faces.size() != 0)
456 Eigen::MatrixXi boundary_edges;
457 igl::boundary_facets(
faces, boundary_edges);
458 codim_edges.conservativeResize(codim_edges.rows() + boundary_edges.rows(), 2);
459 codim_edges.bottomRows(boundary_edges.rows()) = boundary_edges;
464 else if (extract ==
"volume")
470 if (j_mesh[
"n_refs"].get<int>() != 0)
472 if (
faces.size() != 0)
475 const int n_refs = j_mesh[
"n_refs"];
476 const double refinement_location = j_mesh[
"advanced"][
"refinement_location"];
477 for (
int i = 0; i < n_refs; i++)
479 const size_t n_vertices = vertices.rows();
480 const size_t n_edges = codim_edges.rows();
481 vertices.conservativeResize(n_vertices + n_edges, vertices.cols());
482 codim_edges.conservativeResize(2 * n_edges, codim_edges.cols());
483 for (
size_t ei = 0; ei < n_edges; ei++)
485 const int v0i = codim_edges(ei, 0);
486 const int v1i = codim_edges(ei, 1);
487 const int v2i = n_vertices + ei;
488 vertices.row(v2i) = (vertices.row(v1i) - vertices.row(v0i)) * refinement_location + vertices.row(v0i);
489 codim_edges.row(ei) << v0i, v2i;
490 codim_edges.row(n_edges + ei) << v2i, v1i;
500 const json &geometry,
501 const std::vector<json> &displacements,
502 const std::vector<json> &dirichlets,
508 if (geometry.empty())
513 for (
const json &geometry : geometries)
516 if (!geometry[
"is_obstacle"].get<bool>())
519 if (!geometry[
"enabled"].get<bool>())
522 if (geometry[
"type"] ==
"mesh" || geometry[
"type"] ==
"mesh_array")
525 Eigen::MatrixXd &vertices = loaded.
vertices;
526 Eigen::VectorXi &codim_vertices = loaded.
points;
527 Eigen::MatrixXi &codim_edges = loaded.
edges;
529 Eigen::MatrixXi &tets = loaded.
tets;
531 if (geometry[
"type"] ==
"mesh_array")
533 const Selection::BBox bbox{{vertices.colwise().minCoeff(), vertices.colwise().maxCoeff()}};
535 const bool is_offset_relative = geometry[
"array"][
"relative"];
536 const double offset = geometry[
"array"][
"offset"];
537 const VectorNd dimensions = (bbox[1] - bbox[0]);
538 const VectorNi size = geometry[
"array"][
"size"];
539 if (size.size() != dim)
540 log_and_throw_error(
"mesh_array: \"array/size\" needs one entry per dimension ({}), got {}.", dim, size.size());
542 const int N = size.head(dim).prod();
543 const int nV = vertices.rows(), nCV = codim_vertices.rows(), nCE = codim_edges.rows(), nF =
faces.rows(), nT = tets.rows();
545 vertices.conservativeResize(N * nV, Eigen::NoChange);
546 codim_vertices.conservativeResize(N * nCV, Eigen::NoChange);
547 codim_edges.conservativeResize(N * nCE, Eigen::NoChange);
548 faces.conservativeResize(N * nF, Eigen::NoChange);
549 tets.conservativeResize(N * nT, tets.cols());
551 for (
int i = 0; i < size[0]; ++i)
553 for (
int j = 0; j < size[1]; ++j)
555 for (
int k = 0; k < (size.size() > 2 ? size[2] : 1); ++k)
557 RowVectorNd translation = offset * Eigen::RowVector3d(i, j, k).head(vertices.cols());
558 if (is_offset_relative)
559 translation.array() *= dimensions.array();
561 int n = i * size[1] + j;
567 vertices.middleRows(n * nV, nV) = vertices.topRows(nV).rowwise() + translation;
569 codim_vertices.segment(n * nCV, nCV) = codim_vertices.head(nCV).array() + n * nV;
571 codim_edges.middleRows(n * nCE, nCE) = codim_edges.topRows(nCE).array() + n * nV;
573 faces.middleRows(n * nF, nF) =
faces.topRows(nF).array() + n * nV;
575 tets.middleRows(n * nT, nT) = tets.topRows(nT).array() + n * nV;
581 json displacement =
"{\"value\":[0, 0, 0]}"_json;
584 if (!geometry[
"surface_selection"].is_number())
587 const int id = geometry[
"surface_selection"];
588 for (
const json &disp : dirichlets)
590 if ((disp[
"id"].is_string() && disp[
"id"].get<std::string>() ==
"all")
591 || (disp[
"id"].is_number_integer() && disp[
"id"].get<
int>() ==
id))
596 else if (disp[
"id"].is_array())
598 for (
const json &disp_id : disp[
"id"])
600 assert(disp_id.is_number_integer());
601 if (disp_id.get<
int>() ==
id)
609 for (
const json &disp : displacements)
611 if ((disp[
"id"].is_string() && disp[
"id"].get<std::string>() ==
"all")
612 || (disp[
"id"].is_number_integer() && disp[
"id"].get<
int>() ==
id))
617 else if (disp[
"id"].is_array())
619 for (
const json &disp_id : disp[
"id"])
621 assert(disp_id.is_number_integer());
622 if (disp_id.get<
int>() ==
id)
633 vertices, codim_vertices, codim_edges,
faces, displacement,
resources_, tets);
635 else if (geometry[
"type"] ==
"plane")
637 obstacle.
append_plane(geometry[
"point"], geometry[
"normal"]);
639 else if (geometry[
"type"] ==
"ground")
641 VectorNd gravity = VectorNd::Zero(dim);
643 const double height = geometry[
"height"];
644 assert(gravity.norm() != 0);
645 const VectorNd normal = -gravity.normalized();
649 else if (geometry[
"type"] ==
"mesh_sequence")
651 std::vector<std::string> mesh_files;
652 if (geometry[
"mesh_sequence"].is_array())
654 mesh_files = geometry[
"mesh_sequence"].get<std::vector<std::string>>();
658 assert(geometry[
"mesh_sequence"].is_string());
659 const std::string sequence = geometry[
"mesh_sequence"].get<std::string>();
662 std::sort(mesh_files.begin(), mesh_files.end(), [](
const std::string &p1,
const std::string &p2) {
663 return strnatcmp(p1.c_str(), p2.c_str()) < 0;
667 std::vector<Eigen::MatrixXd> vertices(mesh_files.size());
668 Eigen::VectorXi codim_vertices;
669 Eigen::MatrixXi codim_edges;
670 Eigen::MatrixXi
faces;
671 Eigen::MatrixXi tets;
673 for (
int i = 0; i < mesh_files.size(); ++i)
675 json jmesh = geometry;
676 jmesh[
"mesh"] = mesh_files[i];
680 vertices[i] = std::move(loaded.
vertices);
681 Eigen::VectorXi &tmp_codim_vertices = loaded.
points;
682 Eigen::MatrixXi &tmp_codim_edges = loaded.
edges;
683 Eigen::MatrixXi &tmp_faces = loaded.
faces;
684 Eigen::MatrixXi &tmp_tets = loaded.
tets;
687 codim_vertices = tmp_codim_vertices;
688 codim_edges = tmp_codim_edges;
694 assert((codim_vertices.array() == tmp_codim_vertices.array()).all());
695 assert((codim_edges.array() == tmp_codim_edges.array()).all());
696 assert((
faces.array() == tmp_faces.array()).all());
697 assert((tets.array() == tmp_tets.array()).all());
702 vertices, codim_vertices, codim_edges,
faces, geometry[
"fps"], tets);
717 const double unit_scale,
718 const json &transform,
723 const int dim = mesh_dimensions.size();
730 if (transform[
"dimensions"].is_array())
733 (mesh_dimensions.array() == 0).select(1, mesh_dimensions);
735 scale = transform[
"dimensions"];
736 const int scale_size = scale.size();
737 scale.conservativeResize(dim);
738 if (scale_size < dim)
739 scale.tail(dim - scale_size).setZero();
741 scale.array() /= modified_dimensions.array();
743 else if (transform[
"scale"].is_number())
745 scale.setConstant(dim, transform[
"scale"].get<double>());
749 assert(transform[
"scale"].is_array());
750 scale = transform[
"scale"];
751 const int scale_size = scale.size();
752 scale.conservativeResize(dim);
753 if (scale_size < dim)
754 scale.tail(dim - scale_size).setZero();
760 A = (unit_scale * scale).asDiagonal();
768 MatrixNd R = MatrixNd::Identity(dim, dim);
769 if (!transform[
"rotation"].is_null())
773 if (transform[
"rotation"].is_number())
774 R = Eigen::Rotation2Dd(
deg2rad(transform[
"rotation"].get<double>()))
776 else if (!transform[
"rotation"].is_array() || !transform[
"rotation"].empty())
791 b = transform[
"translation"];
792 const int translation_size = b.size();
793 b.conservativeResize(dim);
794 if (translation_size < dim)
795 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.
SurfaceMesh load_surface(const std::string &path, bool keep_volume=false) const
std::unique_ptr< Mesh > load_fem(const std::string &path, bool non_conforming=false) 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, const Eigen::MatrixXi &tets=Eigen::MatrixXi())
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())
void append_plane(const VectorNd &point, const VectorNd &normal)
void set_units(const Units &units)
Eigen::MatrixXi tets
Volumetric cells indexing vertices; only filled when a 3D tet mesh is read with keep_volume.
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)