15 const json &selection,
19 std::shared_ptr<Selection> res =
nullptr;
20 if (selection.contains(
"box"))
21 res = std::make_shared<BoxSelection>(selection, mesh_bbox);
22 else if (selection.contains(
"threshold"))
23 res = std::make_shared<BoxSideSelection>(selection, mesh_bbox);
24 else if (selection.contains(
"center"))
25 res = std::make_shared<SphereSelection>(selection, mesh_bbox);
26 else if (selection.contains(
"radius"))
27 res = std::make_shared<CylinderSelection>(selection, mesh_bbox);
28 else if (selection.contains(
"axis"))
29 res = std::make_shared<AxisPlaneSelection>(selection, mesh_bbox);
30 else if (selection.contains(
"normal"))
31 res = std::make_shared<PlaneSelection>(selection, mesh_bbox);
32 else if (selection[
"id"].is_string())
33 res = std::make_shared<FileSelection>(
34 resources, selection[
"id"].get<std::string>(), selection.value(
"id_offset", 0));
35 else if (selection[
"id"].is_number_integer())
36 res = std::make_shared<UniformSelection>(selection[
"id"]);
37 else if (selection.contains(
"file"))
38 res = std::make_shared<FileSelection>(resources, selection[
"file"].get<std::string>());
42 if (selection.contains(
"boundary_only"))
43 res->boundary_only_ = selection[
"boundary_only"];
48 const json &j_selections,
52 std::vector<std::shared_ptr<Selection>> selections;
53 if (j_selections.is_number_integer())
55 selections.push_back(std::make_shared<UniformSelection>(j_selections.get<
int>()));
57 else if (j_selections.is_string())
59 selections.push_back(std::make_shared<FileSelection>(resources, j_selections.get<std::string>()));
61 else if (j_selections.is_object())
63 selections.push_back(
build(j_selections, mesh_bbox, resources));
65 else if (j_selections.is_array())
67 for (
const json &s : j_selections.get<std::vector<json>>())
69 selections.push_back(
build(s, mesh_bbox, resources));
72 else if (!j_selections.is_null())
83 const json &selection,
87 auto bboxj = selection[
"box"];
89 const int dim = bboxj[0].size();
90 assert(bboxj[1].size() == dim);
95 if (selection.value(
"relative",
false))
97 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
98 bbox_[0] = mesh_width.cwiseProduct(
bbox_[0]) + mesh_bbox[0];
99 bbox_[1] = mesh_width.cwiseProduct(
bbox_[1]) + mesh_bbox[0];
105 assert(
bbox_[0].size() == p.size());
106 assert(
bbox_[1].size() == p.size());
109 for (
int d = 0; d < p.size(); ++d)
124 const json &selection,
130 tolerance_ = mesh_bbox.size() == 3 ? 1e-2 : 1e-7;
136 assert(p.size() == 2 || p.size() == 3);
139 const auto &[min_corner, max_corner] =
mesh_bbox_;
141 if (std::abs(p(0) - min_corner(0)) <
tolerance_)
143 else if (std::abs(p(1) - min_corner(1)) <
tolerance_)
145 else if (std::abs(p(0) - max_corner(0)) <
tolerance_)
147 else if (std::abs(p(1) - max_corner(1)) <
tolerance_)
149 else if (p.size() == 3 && std::abs(p(2) - min_corner(2)) <
tolerance_)
151 else if (p.size() == 3 && std::abs(p(2) - max_corner(2)) <
tolerance_)
160 const json &selection,
167 if (selection.value(
"relative",
false))
169 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
176 id_ = selection[
"id"];
181 assert(
center_.size() == p.size());
189 const json &selection,
197 if (selection.value(
"relative",
false))
199 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
200 point_ = mesh_width.cwiseProduct(
point_) + mesh_bbox[0];
201 p2 = mesh_width.cwiseProduct(p2) + mesh_bbox[0];
209 id_ = selection[
"id"];
214 assert(
point_.size() == p.size());
217 const double proj =
axis_.dot(v);
230 const json &selection,
236 if (selection[
"axis"].is_string())
238 std::string axis = selection[
"axis"];
239 int sign = axis[0] ==
'-' ? -1 : 1;
240 int dim = std::tolower(axis.back()) -
'x' + 1;
241 assert(dim >= 1 && dim <= 3);
246 assert(selection[
"axis"].is_number_integer());
247 axis_ = selection[
"axis"];
248 assert(std::abs(
axis_) >= 1 && std::abs(
axis_) <= 3);
251 if (selection.value(
"relative",
false))
253 int dim = std::abs(
axis_) - 1;
260 const double v = p[std::abs(
axis_) - 1];
271 const json &selection,
277 if (selection.contains(
"point"))
279 point_ = selection[
"point"];
280 if (selection.value(
"relative",
false))
281 point_ = (mesh_bbox[1] - mesh_bbox[0]).cwiseProduct(
point_) + mesh_bbox[0];
289 assert(p.size() ==
normal_.size());
297 const std::vector<int> &ids)
305 return ids_.at(element_id);
312 const std::string &path,
320 catch (
const std::exception &)
327 for (
int k = 0; k < mat.size(); ++k)
328 this->
ids_.push_back(mat(k) + id_offset);
332 data_.resize(mat.rows());
334 for (
int i = 0; i < mat.rows(); ++i)
336 data_[i].first = mat(i, 0) + id_offset;
338 for (
int j = 1; j < mat.cols(); ++j)
340 data_[i].second.push_back(mat(i, j));
343 std::sort(
data_[i].second.begin(),
data_[i].second.end());
353 std::vector<int> tmp;
354 for (
const auto &t :
data_)
367 std::vector<int> tmp;
368 for (
const auto &t :
data_)
Read-only interface to a hierarchical collection of simulation resources.
virtual Eigen::MatrixXi read_int_matrix(const std::string &path) const =0
virtual std::string describe(const std::string &path) const =0
AxisPlaneSelection(const json &selection, const BBox &mesh_bbox)
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
BoxSelection(const json &selection, const BBox &mesh_bbox)
int id(const size_t element_id, const std::vector< int > &vs, const RowVectorNd &p) const override
BoxSideSelection(const json &selection, const BBox &mesh_bbox)
CylinderSelection(const json &selection, const BBox &mesh_bbox)
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
int id(const size_t element_id, const std::vector< int > &vs, const RowVectorNd &p) const override
FileSelection(const io::ResourceIO &resources, const std::string &path, const int id_offset=0)
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
std::vector< std::pair< int, std::vector< int > > > data_
PlaneSelection(const json &selection, const BBox &mesh_bbox)
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
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
static std::shared_ptr< Selection > build(const json &j_selections, const BBox &mesh_bbox, const io::ResourceIO &resources)
Build a selection objects from a JSON selection.
virtual bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
virtual int id(const size_t element_id, const std::vector< int > &vs, const RowVectorNd &p) const override
bool inside(const size_t p_id, const std::vector< int > &vs, const RowVectorNd &p) const override
SphereSelection(const json &selection, const BBox &mesh_bbox)
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)