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"]);
46 const json &j_selections,
48 const std::string &root_path)
50 std::vector<std::shared_ptr<Selection>> selections;
51 if (j_selections.is_number_integer())
53 selections.push_back(std::make_shared<UniformSelection>(j_selections.get<
int>()));
55 else if (j_selections.is_string())
57 selections.push_back(std::make_shared<FileSelection>(
resolve_path(j_selections, root_path)));
59 else if (j_selections.is_object())
61 selections.push_back(
build(j_selections, mesh_bbox));
63 else if (j_selections.is_array())
65 for (
const json &s : j_selections.get<std::vector<json>>())
67 selections.push_back(
build(s, mesh_bbox));
70 else if (!j_selections.is_null())
80 const json &selection,
84 auto bboxj = selection[
"box"];
86 const int dim = bboxj[0].size();
87 assert(bboxj[1].size() == dim);
92 if (selection.value(
"relative",
false))
94 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
95 bbox_[0] = mesh_width.cwiseProduct(
bbox_[0]) + mesh_bbox[0];
96 bbox_[1] = mesh_width.cwiseProduct(
bbox_[1]) + mesh_bbox[0];
102 assert(
bbox_[0].size() == p.size());
103 assert(
bbox_[1].size() == p.size());
106 for (
int d = 0; d < p.size(); ++d)
121 const json &selection,
127 tolerance_ = mesh_bbox.size() == 3 ? 1e-2 : 1e-7;
133 assert(p.size() == 2 || p.size() == 3);
136 const auto &[min_corner, max_corner] =
mesh_bbox_;
138 if (std::abs(p(0) - min_corner(0)) <
tolerance_)
140 else if (std::abs(p(1) - min_corner(1)) <
tolerance_)
142 else if (std::abs(p(0) - max_corner(0)) <
tolerance_)
144 else if (std::abs(p(1) - max_corner(1)) <
tolerance_)
146 else if (p.size() == 3 && std::abs(p(2) - min_corner(2)) <
tolerance_)
148 else if (p.size() == 3 && std::abs(p(2) - max_corner(2)) <
tolerance_)
157 const json &selection,
164 if (selection.value(
"relative",
false))
166 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
173 id_ = selection[
"id"];
178 assert(
center_.size() == p.size());
186 const json &selection,
194 if (selection.value(
"relative",
false))
196 RowVectorNd mesh_width = mesh_bbox[1] - mesh_bbox[0];
197 point_ = mesh_width.cwiseProduct(
point_) + mesh_bbox[0];
198 p2 = mesh_width.cwiseProduct(p2) + mesh_bbox[0];
206 id_ = selection[
"id"];
211 assert(
point_.size() == p.size());
214 const double proj =
axis_.dot(v);
227 const json &selection,
233 if (selection[
"axis"].is_string())
235 std::string axis = selection[
"axis"];
236 int sign = axis[0] ==
'-' ? -1 : 1;
237 int dim = std::tolower(axis.back()) -
'x' + 1;
238 assert(dim >= 1 && dim <= 3);
243 assert(selection[
"axis"].is_number_integer());
244 axis_ = selection[
"axis"];
245 assert(std::abs(
axis_) >= 1 && std::abs(
axis_) <= 3);
248 if (selection.value(
"relative",
false))
250 int dim = std::abs(
axis_) - 1;
257 const double v = p[std::abs(
axis_) - 1];
268 const json &selection,
274 if (selection.contains(
"point"))
276 point_ = selection[
"point"];
277 if (selection.value(
"relative",
false))
278 point_ = (mesh_bbox[1] - mesh_bbox[0]).cwiseProduct(
point_) + mesh_bbox[0];
286 assert(p.size() ==
normal_.size());
294 const std::vector<int> &ids)
302 return ids_.at(element_id);
308 const std::string &file_path,
315 logger().error(
"Unable to open selection file \"{}\"!", file_path);
321 for (
int k = 0; k < mat.size(); ++k)
322 this->
ids_.push_back(mat(k) + id_offset);
326 data_.resize(mat.rows());
328 for (
int i = 0; i < mat.rows(); ++i)
330 data_[i].first = mat(i, 0) + id_offset;
332 for (
int j = 1; j < mat.cols(); ++j)
334 data_[i].second.push_back(mat(i, j));
337 std::sort(
data_[i].second.begin(),
data_[i].second.end());
347 std::vector<int> tmp;
348 for (
const auto &t :
data_)
361 std::vector<int> tmp;
362 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
std::array< RowVectorNd, 2 > BBox
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.
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)