17 const json &selection,
19 const std::string &root_path)
21 std::shared_ptr<Selection> res =
nullptr;
22 if (selection.contains(
"box"))
23 res = std::make_shared<BoxSelection>(selection, mesh_bbox);
24 else if (selection.contains(
"threshold"))
25 res = std::make_shared<BoxSideSelection>(selection, mesh_bbox);
26 else if (selection.contains(
"center"))
27 res = std::make_shared<SphereSelection>(selection, mesh_bbox);
28 else if (selection.contains(
"radius"))
29 res = std::make_shared<CylinderSelection>(selection, mesh_bbox);
30 else if (selection.contains(
"axis"))
31 res = std::make_shared<AxisPlaneSelection>(selection, mesh_bbox);
32 else if (selection.contains(
"normal"))
33 res = std::make_shared<PlaneSelection>(selection, mesh_bbox);
34 else if (selection[
"id"].is_string())
35 res = std::make_shared<FileSelection>(
36 resolve_path(selection[
"id"], root_path), selection.value(
"id_offset", 0));
37 else if (selection[
"id"].is_number_integer())
38 res = std::make_shared<UniformSelection>(selection[
"id"]);
39 else if (selection.contains(
"file"))
40 res = std::make_shared<FileSelection>(
resolve_path(selection[
"file"], root_path));
44 if (selection.contains(
"boundary_only"))
45 res->boundary_only_ = selection[
"boundary_only"];
50 const json &j_selections,
52 const std::string &root_path)
54 std::vector<std::shared_ptr<Selection>> selections;
55 if (j_selections.is_number_integer())
57 selections.push_back(std::make_shared<UniformSelection>(j_selections.get<
int>()));
59 else if (j_selections.is_string())
61 selections.push_back(std::make_shared<FileSelection>(
resolve_path(j_selections, root_path)));
63 else if (j_selections.is_object())
65 selections.push_back(
build(j_selections, mesh_bbox, root_path));
67 else if (j_selections.is_array())
69 for (
const json &s : j_selections.get<std::vector<json>>())
71 selections.push_back(
build(s, mesh_bbox, root_path));
74 else if (!j_selections.is_null())
85 const json &selection,
89 auto bboxj = selection[
"box"];
91 const int dim = bboxj[0].size();
92 assert(bboxj[1].size() == dim);
97 if (selection.value(
"relative",
false))
99 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
100 bbox_[0] = mesh_width.cwiseProduct(
bbox_[0]) + mesh_bbox[0];
101 bbox_[1] = mesh_width.cwiseProduct(
bbox_[1]) + mesh_bbox[0];
107 assert(
bbox_[0].size() == p.size());
108 assert(
bbox_[1].size() == p.size());
111 for (
int d = 0; d < p.size(); ++d)
126 const json &selection,
132 tolerance_ = mesh_bbox.size() == 3 ? 1e-2 : 1e-7;
138 assert(p.size() == 2 || p.size() == 3);
141 const auto &[min_corner, max_corner] =
mesh_bbox_;
143 if (std::abs(p(0) - min_corner(0)) <
tolerance_)
145 else if (std::abs(p(1) - min_corner(1)) <
tolerance_)
147 else if (std::abs(p(0) - max_corner(0)) <
tolerance_)
149 else if (std::abs(p(1) - max_corner(1)) <
tolerance_)
151 else if (p.size() == 3 && std::abs(p(2) - min_corner(2)) <
tolerance_)
153 else if (p.size() == 3 && std::abs(p(2) - max_corner(2)) <
tolerance_)
162 const json &selection,
169 if (selection.value(
"relative",
false))
171 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
178 id_ = selection[
"id"];
183 assert(
center_.size() == p.size());
191 const json &selection,
199 if (selection.value(
"relative",
false))
201 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
202 point_ = mesh_width.cwiseProduct(
point_) + mesh_bbox[0];
203 p2 = mesh_width.cwiseProduct(p2) + mesh_bbox[0];
211 id_ = selection[
"id"];
216 assert(
point_.size() == p.size());
219 const double proj =
axis_.dot(v);
232 const json &selection,
238 if (selection[
"axis"].is_string())
240 std::string axis = selection[
"axis"];
241 int sign = axis[0] ==
'-' ? -1 : 1;
242 int dim = std::tolower(axis.back()) -
'x' + 1;
243 assert(dim >= 1 && dim <= 3);
248 assert(selection[
"axis"].is_number_integer());
249 axis_ = selection[
"axis"];
250 assert(std::abs(
axis_) >= 1 && std::abs(
axis_) <= 3);
253 if (selection.value(
"relative",
false))
255 int dim = std::abs(
axis_) - 1;
262 const double v = p[std::abs(
axis_) - 1];
273 const json &selection,
279 if (selection.contains(
"point"))
281 point_ = selection[
"point"];
282 if (selection.value(
"relative",
false))
283 point_ = (mesh_bbox[1] - mesh_bbox[0]).cwiseProduct(
point_) + mesh_bbox[0];
291 assert(p.size() ==
normal_.size());
299 const std::vector<int> &ids)
307 return ids_.at(element_id);
313 const std::string &file_path,
320 logger().error(
"Unable to open selection file \"{}\"!", file_path);
326 for (
int k = 0; k < mat.size(); ++k)
327 this->
ids_.push_back(mat(k) + id_offset);
331 data_.resize(mat.rows());
333 for (
int i = 0; i < mat.rows(); ++i)
335 data_[i].first = mat(i, 0) + id_offset;
337 for (
int j = 1; j < mat.cols(); ++j)
339 data_[i].second.push_back(mat(i, j));
342 std::sort(
data_[i].second.begin(),
data_[i].second.end());
352 std::vector<int> tmp;
353 for (
const auto &t :
data_)
366 std::vector<int> tmp;
367 for (
const auto &t :
data_)
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 std::string &file_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::shared_ptr< Selection > build(const json &j_selections, const BBox &mesh_bbox, const std::string &root_path)
Build a selection objects from a JSON selection.
std::array< RowVectorNd, 2 > BBox
static std::vector< std::shared_ptr< utils::Selection > > build_selections(const json &j_selections, const BBox &mesh_bbox, const std::string &root_path)
Build a vector of selection objects from a JSON selection(s).
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)
bool read_matrix(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
Reads a matrix from a file. Determines the file format based on the path's extension.
std::string resolve_path(const std::string &path, const std::string &input_file_path, const bool only_if_exists=false)
spdlog::logger & logger()
Retrieves the current logger.
Eigen::Matrix< double, 1, Eigen::Dynamic, Eigen::RowMajor, 1, 3 > RowVectorNd
void log_and_throw_error(const std::string &msg)