ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
boundingvolume.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <ecvBBox.h>
9 #include <ecvHObject.h>
10 #include <ecvOrientedBBox.h>
11 
12 #include <sstream>
13 
14 #include "pybind/docstring.h"
17 
18 namespace cloudViewer {
19 namespace geometry {
20 
21 void pybind_boundingvolume(py::module &m) {
24  std::shared_ptr<cloudViewer::OrientedBoundingBox>>
25  oriented_bounding_box_base(m, "OrientedBoundingBox",
26  "The base OrientedBoundingBox class.");
27  py::detail::bind_default_constructor<cloudViewer::OrientedBoundingBox>(
28  oriented_bounding_box_base);
29  py::detail::bind_copy_functions<cloudViewer::OrientedBoundingBox>(
30  oriented_bounding_box_base);
31  oriented_bounding_box_base
32  .def(py::init<const Eigen::Vector3d &, const Eigen::Matrix3d &,
33  const Eigen::Vector3d &>(),
34  "Create OrientedBoundingBox from center, rotation R and "
35  "extent "
36  "in x, y and z direction",
37  "center"_a, "R"_a, "extent"_a)
38  .def("__repr__",
39  [](const cloudViewer::OrientedBoundingBox &box) {
40  return std::string("OrientedBoundingBox");
41  })
43  "Returns the volume of the bounding box.")
45  "Get the extent/length of the bounding box in x, y, and z "
46  "dimension "
47  "in its frame of reference")
48  .def("get_half_extent",
50  "Returns the half extent of the bounding box in its frame of "
51  "reference.")
52  .def("get_max_extent",
54  "Returns the max extent of the bounding box in its frame of "
55  "reference")
57  "``float64`` array of shape ``(3, )``", "color"_a)
59  "``float64`` array of shape ``(3, )``")
61  "``float64`` array of shape ``(3, )``")
62  .def("get_box_points",
64  "Returns the eight points that define the bounding box.")
66  "Returns the eight points that define the bounding box.")
68  "Clear all elements in the geometry..")
69  .def("get_point_indices_within_bounding_box",
70  py::overload_cast<const std::vector<Eigen::Vector3d> &>(
72  GetPointIndicesWithinBoundingBox,
73  py::const_),
74  "Return indices to points that are within the bounding box.",
75  "points"_a)
76  .def_static(
77  "create_from_axis_aligned_bounding_box",
79  CreateFromAxisAlignedBoundingBox,
80  "Returns an oriented bounding box from the BoundingBox.",
81  "aabox"_a)
82  .def_readwrite("center", &cloudViewer::OrientedBoundingBox::center_,
83  "``float64`` array of shape ``(3, )``")
84  .def_readwrite("R", &cloudViewer::OrientedBoundingBox::R_,
85  "``float64`` array of shape ``(3,3 )``")
86  .def_readwrite("extent", &cloudViewer::OrientedBoundingBox::extent_,
87  "``float64`` array of shape ``(3, )``")
88  .def_readwrite("color", &cloudViewer::OrientedBoundingBox::color_,
89  "``float64`` array of shape ``(3, )``");
90 
91  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "clear");
92  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "volume");
93  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "get_extent");
94  docstring::ClassMethodDocInject(m, "OrientedBoundingBox",
95  "get_half_extent");
96  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "get_max_extent");
97  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "set_color");
98  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "get_color");
99  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "color");
100  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "get_box_points");
101  docstring::ClassMethodDocInject(m, "OrientedBoundingBox", "box_points");
102  docstring::ClassMethodDocInject(m, "OrientedBoundingBox",
103  "get_point_indices_within_bounding_box",
104  {{"points", "A list of points."}});
106  m, "OrientedBoundingBox", "create_from_axis_aligned_bounding_box",
107  {{"aabox",
108  "BoundingBox object from which OrientedBoundingBox is "
109  "created."}});
110 
111  py::class_<ecvOrientedBBox, PyGeometry<ecvOrientedBBox>,
112  std::shared_ptr<ecvOrientedBBox>, ccHObject,
114  oriented_bounding_box(m, "ecvOrientedBBox",
115  "Class that defines an oriented box that can "
116  "be computed from 3D geometries.");
117  py::detail::bind_default_constructor<ecvOrientedBBox>(
118  oriented_bounding_box);
119  py::detail::bind_copy_functions<ecvOrientedBBox>(oriented_bounding_box);
120  oriented_bounding_box
121  .def(py::init<const Eigen::Vector3d &, const Eigen::Matrix3d &,
122  const Eigen::Vector3d &, const std::string &>(),
123  "Create ecvOrientedBBox from center, rotation R and extent "
124  "in x, y and z "
125  "direction",
126  "center"_a, "R"_a, "extent"_a, "name"_a = "ecvOrientedBBox")
127  .def("__repr__",
128  [](const ecvOrientedBBox &box) {
129  std::stringstream s;
130  auto c = box.center_;
131  auto e = box.extent_;
132  s << "ecvOrientedBBox: center: (" << c.x() << ", " << c.y()
133  << ", " << c.z() << "), extent: " << e.x() << ", "
134  << e.y() << ", " << e.z() << ")";
135  return s.str();
136  })
137  .def_static(
138  "create_from_points",
139  py::overload_cast<const std::vector<Eigen::Vector3d> &>(
141  "Creates the bounding box that encloses the set of points.",
142  "points"_a)
143  .def_static("create_from_axis_aligned_bounding_box",
145  "Returns an oriented bounding box from the ccBBox.",
146  "aabox"_a);
147 
148  docstring::ClassMethodDocInject(m, "ecvOrientedBBox", "create_from_points",
149  {{"points", "A list of points."}});
151  m, "ecvOrientedBBox", "create_from_axis_aligned_bounding_box",
152  {{"aabox",
153  "BoundingBox object from which OrientedBoundingBox is "
154  "created."}});
155 
156  py::class_<cloudViewer::BoundingBox,
157  std::shared_ptr<cloudViewer::BoundingBox>>
158  axis_bounding_box_base(m, "BoundingBox",
159  "Class that defines an axis_aligned box "
160  "that can be computed from 3D "
161  "geometries, The axis aligned bounding "
162  "box uses the coordinate axes for "
163  "bounding box generation.");
164  py::detail::bind_default_constructor<cloudViewer::BoundingBox>(
165  axis_bounding_box_base);
166  py::detail::bind_copy_functions<cloudViewer::BoundingBox>(
167  axis_bounding_box_base);
168  axis_bounding_box_base
169  .def(py::init<const Eigen::Vector3d &, const Eigen::Vector3d &>(),
170  "Create an BoundingBox from min bounds and max "
171  "bounds in x, y and z",
172  "bbMinCorner"_a, "bbMaxCorner"_a)
173  .def("__repr__",
174  [](const cloudViewer::BoundingBox &box) {
175  return std::string("BoundingBox");
176  })
177  .def("clear", &cloudViewer::BoundingBox::clear,
178  "Resets the bounding box.")
179  .def("set_color", &cloudViewer::BoundingBox::SetColor,
180  "``float64`` array of shape ``(3, )``", "color"_a)
181  .def("get_color", &cloudViewer::BoundingBox::GetColor,
182  "``float64`` array of shape ``(3, )``")
183  .def("is_valid", &cloudViewer::BoundingBox::isValid,
184  "Returns whether bounding box is valid or not")
185  .def("set_validity", &cloudViewer::BoundingBox::setValidity,
186  "Sets bonding box validity.", "state"_a)
187  .def("volume", &cloudViewer::BoundingBox::volume,
188  "Returns the bounding box volume.")
190  "'Enlarges' the bounding box with a point.", "point"_a)
191  .def("get_x_percentage", &cloudViewer::BoundingBox::getXPercentage,
192  "Returns x Percentage.", "x"_a)
193  .def("get_y_percentage", &cloudViewer::BoundingBox::getYPercentage,
194  "Returns y Percentage.", "y"_a)
195  .def("get_z_percentage", &cloudViewer::BoundingBox::getZPercentage,
196  "Returns z Percentage.", "z"_a)
197  .def("get_diag_norm", &cloudViewer::BoundingBox::getDiagNormd,
198  "Returns diagonal length (double precision)")
199  .def("get_min_box_dim", &cloudViewer::BoundingBox::getMinBoxDim,
200  "Returns minimal box dimension")
201  .def("get_max_box_dim", &cloudViewer::BoundingBox::getMaxBoxDim,
202  "Returns maximal box dimension")
203  .def("compute_volume", &cloudViewer::BoundingBox::computeVolume,
204  "Returns the bounding-box volume")
205  .def("get_bounds", &cloudViewer::BoundingBox::getBounds,
206  "Returns the bounding-box bounds", "bounds"_a)
207  .def("min_distance_to", &cloudViewer::BoundingBox::minDistTo,
208  "Computes min gap (absolute distance) between this "
209  "bounding-box and another one; return min gap (>=0) "
210  "or -1 if at least one of the box is not valid",
211  "box"_a)
212  .def("contains", &cloudViewer::BoundingBox::containsEigen,
213  "Returns whether a points is inside the box or not", "point"_a)
214  .def("get_point_indices_within_boundingbox",
215  py::overload_cast<const std::vector<Eigen::Vector3d> &>(
217  GetPointIndicesWithinBoundingBox,
218  py::const_),
219  "Returns point indices Within bounding box.", "points"_a);
220 
221  docstring::ClassMethodDocInject(m, "BoundingBox", "clear");
222  docstring::ClassMethodDocInject(m, "BoundingBox", "is_valid");
223  docstring::ClassMethodDocInject(m, "BoundingBox", "set_color");
224  docstring::ClassMethodDocInject(m, "BoundingBox", "get_color");
225  docstring::ClassMethodDocInject(m, "BoundingBox", "set_validity");
226  docstring::ClassMethodDocInject(m, "BoundingBox", "volume");
227  docstring::ClassMethodDocInject(m, "BoundingBox", "add");
228  docstring::ClassMethodDocInject(m, "BoundingBox", "get_x_percentage");
229  docstring::ClassMethodDocInject(m, "BoundingBox", "get_y_percentage");
230  docstring::ClassMethodDocInject(m, "BoundingBox", "get_z_percentage");
231  docstring::ClassMethodDocInject(m, "BoundingBox", "get_diag_norm");
232  docstring::ClassMethodDocInject(m, "BoundingBox", "get_min_box_dim");
233  docstring::ClassMethodDocInject(m, "BoundingBox", "get_max_box_dim");
234  docstring::ClassMethodDocInject(m, "BoundingBox", "compute_volume");
235  docstring::ClassMethodDocInject(m, "BoundingBox", "min_distance_to");
236  docstring::ClassMethodDocInject(m, "BoundingBox", "contains");
237  docstring::ClassMethodDocInject(m, "BoundingBox",
238  "get_point_indices_within_boundingbox",
239  {{"points", "A list of points."}});
241  m, "BoundingBox", "get_bounds",
242  {{"bounds", "the output bounds with six double values."}});
243 
244  py::class_<ccBBox, PyGeometry<ccBBox>, std::shared_ptr<ccBBox>, ccHObject,
246  axis_aligned_bounding_box(m, "ccBBox",
247  "Class that defines an axis_aligned box "
248  "that can be computed from 3D "
249  "geometries, The axis aligned bounding "
250  "box uses the coordinate axes for "
251  "bounding box generation.");
252  py::detail::bind_default_constructor<ccBBox>(axis_aligned_bounding_box);
253  py::detail::bind_copy_functions<ccBBox>(axis_aligned_bounding_box);
254  axis_aligned_bounding_box
255  .def(py::init<const Eigen::Vector3d &, const Eigen::Vector3d &,
256  const std::string &>(),
257  "Create an ccBBox from min bounds and max "
258  "bounds in x, y and z",
259  "min_bound"_a, "max_bound"_a, "name"_a = "ccBBox")
260  .def("__repr__",
261  [](const ccBBox &box) {
262  std::stringstream s;
263  auto mn = box.GetMinBound();
264  auto mx = box.GetMaxBound();
265  s << "ccBBox: min: (" << mn.x() << ", " << mn.y() << ", "
266  << mn.z() << "), max: (" << mx.x() << ", " << mx.y()
267  << ", " << mx.z() << ")";
268  return s.str();
269  })
270  .def(py::self += py::self)
271  .def(py::self += Eigen::Vector3d())
272  .def(py::self -= Eigen::Vector3d())
273  .def(py::self *= double())
274  .def(py::self *= float())
275  .def(py::self *= Eigen::Matrix3d())
276  .def("get_box_points", &ccBBox::GetBoxPoints,
277  "Returns the eight points that define the bounding box.")
278  .def("get_extent", &ccBBox::GetExtent,
279  "Get the extent/length of the bounding box in x, y, and z "
280  "dimension.")
281  .def("get_half_extent", &ccBBox::GetHalfExtent,
282  "Returns the half extent of the bounding box.")
283  .def("get_max_extent", &ccBBox::GetMaxExtent,
284  "Returns the maximum extent, i.e. the maximum of X, Y and Z "
285  "axis")
286  .def("set_min_bound", &ccBBox::SetMinBounds,
287  "``float64`` array of shape ``(3, )``", "minBound"_a)
288  .def("set_max_bound", &ccBBox::SetMaxBounds,
289  "``float64`` array of shape ``(3, )``", "maxBound"_a)
290  .def("get_print_info", &ccBBox::GetPrintInfo,
291  "Returns the 3D dimensions of the bounding box in string "
292  "format.")
293  .def_static(
294  "create_from_points",
295  py::overload_cast<const std::vector<Eigen::Vector3d> &>(
297  "Creates the bounding box that encloses the set of points.",
298  "points"_a);
299  docstring::ClassMethodDocInject(m, "ccBBox", "get_box_points");
300  docstring::ClassMethodDocInject(m, "ccBBox", "get_extent");
301  docstring::ClassMethodDocInject(m, "ccBBox", "get_half_extent");
302  docstring::ClassMethodDocInject(m, "ccBBox", "get_max_extent");
303  docstring::ClassMethodDocInject(m, "ccBBox", "get_print_info");
305  m, "ccBBox", "set_min_bound",
306  {{"minBound", "The minimum corner coordinate."}});
308  m, "ccBBox", "set_max_bound",
309  {{"maxBound", "The maximum corner coordinate."}});
310  docstring::ClassMethodDocInject(m, "ccBBox", "create_from_points",
311  {{"points", "A list of points."}});
312 }
313 
314 } // namespace geometry
315 } // namespace cloudViewer
Bounding box structure.
Definition: ecvBBox.h:25
PointCoordinateType GetMaxExtent() const
Definition: ecvBBox.h:156
void SetMinBounds(const Eigen::Vector3d &minBound)
Definition: ecvBBox.h:132
Eigen::Vector3d GetExtent() const
Get the extent/length of the bounding box in x, y, and z dimension.
Definition: ecvBBox.h:147
std::string GetPrintInfo() const
Returns the 3D dimensions of the bounding box in string format.
Eigen::Vector3d GetHalfExtent() const
Returns the half extent of the bounding box.
Definition: ecvBBox.h:152
static ccBBox CreateFromPoints(const std::vector< CCVector3 > &points)
void SetMaxBounds(const Eigen::Vector3d &maxBound)
Definition: ecvBBox.h:135
std::vector< Eigen::Vector3d > GetBoxPoints() const
Returns the eight points that define the bounding box.
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
double getDiagNormd() const
Returns diagonal length (double precision)
Definition: BoundingBox.h:175
Eigen::Vector3d GetColor() const
Gets the bounding box color.
Definition: BoundingBox.h:267
PointCoordinateType minDistTo(const BoundingBoxTpl< PointCoordinateType > &bbox) const
Definition: BoundingBox.h:209
PointCoordinateType getMinBoxDim() const
Returns minimal box dimension.
Definition: BoundingBox.h:178
void setValidity(bool state)
Sets bonding box validity.
Definition: BoundingBox.h:200
double computeVolume() const
Returns the bounding-box volume.
Definition: BoundingBox.h:192
PointCoordinateType getMaxBoxDim() const
Returns maximal box dimension.
Definition: BoundingBox.h:185
bool containsEigen(const Eigen::Vector3d &point) const
Definition: BoundingBox.h:236
void addEigen(const Eigen::Vector3d &point)
Definition: BoundingBox.h:260
void SetColor(const Eigen::Vector3d &color)
Sets the bounding box color.
Definition: BoundingBox.h:265
bool isValid() const
Returns whether bounding box is valid or not.
Definition: BoundingBox.h:203
A bounding box oriented along an arbitrary frame of reference.
Eigen::Vector3d color_
The color of the bounding box in RGB.
Eigen::Vector3d GetHalfExtent() const
Returns the half extent of the bounding box in its frame of reference.
Eigen::Vector3d extent_
The extent of the bounding box in its frame of reference.
const Eigen::Vector3d & GetExtent() const
std::vector< Eigen::Vector3d > GetBoxPoints() const
double volume() const
Returns the volume of the bounding box.
Eigen::Vector3d center_
The center point of the bounding box.
const Eigen::Vector3d & GetColor() const
Gets the bounding box color.
double GetMaxExtent() const
Returns the max extent of the bounding box in its frame of reference.
void SetColor(const Eigen::Vector3d &color)
Sets the bounding box color.
static ecvOrientedBBox CreateFromPoints(const std::vector< Eigen::Vector3d > &points)
static ecvOrientedBBox CreateFromAxisAlignedBoundingBox(const ccBBox &aabox)
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
::ecvOrientedBBox OrientedBoundingBox
void pybind_boundingvolume(py::module &m)
Generic file read and write utility for python interface.
BoundingBoxTpl< PointCoordinateType > BoundingBox
Default bounding-box type.
Definition: BoundingBox.h:304