ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
BoundingVolume.h
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 #pragma once
9 
10 #include <Logging.h>
11 
17 
18 namespace cloudViewer {
19 namespace t {
20 namespace geometry {
21 
23 
48 public:
50  AxisAlignedBoundingBox(const core::Device &device = core::Device("CPU:0"));
51 
61  AxisAlignedBoundingBox(const core::Tensor &min_bound,
62  const core::Tensor &max_bound);
63 
64  virtual ~AxisAlignedBoundingBox() override {}
65 
67  core::Device GetDevice() const override { return device_; }
68 
70  core::Dtype GetDtype() const { return dtype_; }
71 
78  bool copy = false) const;
79 
82  return To(GetDevice(), /*copy=*/true);
83  }
84 
85  AxisAlignedBoundingBox &Clear() override;
86 
87  bool IsEmpty() const override { return Volume() == 0; }
88 
95  void SetMinBound(const core::Tensor &min_bound);
96 
103  void SetMaxBound(const core::Tensor &max_bound);
104 
111  void SetColor(const core::Tensor &color);
112 
113 public:
114  core::Tensor GetMinBound() const { return min_bound_; }
115 
116  core::Tensor GetMaxBound() const { return max_bound_; }
117 
118  core::Tensor GetColor() const { return color_; }
119 
120  core::Tensor GetCenter() const { return (min_bound_ + max_bound_) * 0.5; }
121 
131  AxisAlignedBoundingBox &Translate(const core::Tensor &translation,
132  bool relative = true);
133 
146  double scale,
148 
153 
156 
158  core::Tensor GetHalfExtent() const { return GetExtent() / 2; }
159 
162  double GetMaxExtent() const {
163  return GetExtent().Max({0}).To(core::Float64).Item<double>();
164  }
165 
168  double GetXPercentage(double x) const;
169 
172  double GetYPercentage(double y) const;
173 
176  double GetZPercentage(double z) const;
177 
179  double Volume() const {
180  return GetExtent().Prod({0}).To(core::Float64).Item<double>();
181  }
182 
186  core::Tensor GetBoxPoints() const;
187 
192  const core::Tensor &points) const;
193 
195  std::string ToString() const;
196 
199 
202 
212  const core::Dtype &dtype = core::Float32,
213  const core::Device &device = core::Device("CPU:0"));
214 
221 
222 protected:
233 };
234 
259 public:
261  OrientedBoundingBox(const core::Device &device = core::Device("CPU:0"));
262 
275  OrientedBoundingBox(const core::Tensor &center,
276  const core::Tensor &rotation,
277  const core::Tensor &extent);
278 
279  virtual ~OrientedBoundingBox() override {}
280 
282  core::Device GetDevice() const override { return device_; }
283 
285  core::Dtype GetDtype() const { return dtype_; }
286 
292  OrientedBoundingBox To(const core::Device &device, bool copy = false) const;
293 
295  OrientedBoundingBox Clone() const { return To(GetDevice(), /*copy=*/true); }
296 
297  OrientedBoundingBox &Clear() override;
298 
299  bool IsEmpty() const override { return Volume() == 0; }
300 
306  void SetCenter(const core::Tensor &center);
307 
313  void SetRotation(const core::Tensor &rotation);
314 
320  void SetExtent(const core::Tensor &extent);
321 
326  void SetColor(const core::Tensor &color);
327 
328 public:
329  core::Tensor GetMinBound() const;
330 
331  core::Tensor GetMaxBound() const;
332 
333  core::Tensor GetColor() const { return color_; }
334 
335  core::Tensor GetCenter() const { return center_; }
336 
337  core::Tensor GetRotation() const { return rotation_; }
338 
339  core::Tensor GetExtent() const { return extent_; }
340 
348  OrientedBoundingBox &Translate(const core::Tensor &translation,
349  bool relative = true);
350 
360  const core::Tensor &rotation,
362 
367  OrientedBoundingBox &Transform(const core::Tensor &transformation);
368 
381  double scale,
383 
385  double Volume() const {
386  return GetExtent().Prod({0}).To(core::Float64).Item<double>();
387  }
388 
412  core::Tensor GetBoxPoints() const;
413 
418  const core::Tensor &points) const;
419 
421  std::string ToString() const;
422 
425 
428 
435  const AxisAlignedBoundingBox &aabb);
436 
445  const core::Dtype &dtype = core::Float32,
446  const core::Device &device = core::Device("CPU:0"));
447 
473  const core::Tensor &points,
474  bool robust = false,
476 
477 protected:
484 };
485 
486 } // namespace geometry
487 } // namespace t
488 } // namespace cloudViewer
int points
math::float4 color
bool copy
Definition: VtkUtils.cpp:74
Bounding box structure.
Definition: ecvBBox.h:25
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1261
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1275
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
core::Tensor color_
The color of the bounding box in RGB. The default is white.
cloudViewer::geometry::AxisAlignedBoundingBox ToLegacy() const
Convert to a legacy CloudViewer axis-aligned box.
core::Tensor max_bound_
The upper x, y, z bounds of the bounding box.
core::Tensor GetExtent() const
Get the extent/length of the bounding box in x, y, and z dimension.
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
static AxisAlignedBoundingBox FromLegacy(const cloudViewer::geometry::AxisAlignedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
void SetColor(const core::Tensor &color)
Set the color of the box. If the data type of the given tensor differs from the data type of the box,...
static AxisAlignedBoundingBox CreateFromPoints(const core::Tensor &points)
core::Tensor min_bound_
The lower x, y, z bounds of the bounding box.
core::Device device_
The device to use for the bounding box. The default is CPU:0.
core::Device GetDevice() const override
Returns the device attribute of this AxisAlignedBoundingBox.
void SetMinBound(const core::Tensor &min_bound)
Set the min bound of the box. If the data type of the given tensor differs from the data type of the ...
AxisAlignedBoundingBox To(const core::Device &device, bool copy=false) const
Transfer the AxisAlignedBoundingBox to a specified device.
AxisAlignedBoundingBox & operator+=(const AxisAlignedBoundingBox &other)
Add operation for axis-aligned bounding box. The device of other box must be the same as the device o...
AxisAlignedBoundingBox Clone() const
Returns copy of the AxisAlignedBoundingBox on the same device.
AxisAlignedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty AxisAlignedBoundingBox on the provided device.
bool IsEmpty() const override
Returns true iff the geometry is empty.
double GetMaxExtent() const
Returns the maximum extent, i.e. the maximum of X, Y and Z axis' extents.
AxisAlignedBoundingBox & Clear() override
Clear all elements in the geometry.
core::Dtype dtype_
The data type of the bounding box.
OrientedBoundingBox GetOrientedBoundingBox() const
Convert to an oriented box.
core::Tensor GetHalfExtent() const
Returns the half extent of the bounding box.
void SetMaxBound(const core::Tensor &max_bound)
Set the max bound of the box. If the data type of the given tensor differs from the data type of the ...
AxisAlignedBoundingBox & Scale(double scale, const utility::optional< core::Tensor > &center=utility::nullopt)
Scale the axis-aligned box. If is the min_bound and is the max_bound of the axis aligned bounding b...
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
std::string ToString() const
Text description.
AxisAlignedBoundingBox & Translate(const core::Tensor &translation, bool relative=true)
Translate the axis-aligned box by the given translation.
core::Dtype GetDtype() const
Returns the data type attribute of this AxisAlignedBoundingBox.
double Volume() const
Returns the volume of the bounding box.
Mix-in class for geometry types that can be visualized.
The base geometry class.
Definition: Geometry.h:23
A bounding box oriented along an arbitrary frame of reference.
OrientedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty OrientedBoundingBox on the provided device.
OrientedBoundingBox & Rotate(const core::Tensor &rotation, const utility::optional< core::Tensor > &center=utility::nullopt)
Rotate the oriented box by the given rotation matrix. If the rotation matrix is not orthogonal,...
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Convert to an axis-aligned box.
OrientedBoundingBox To(const core::Device &device, bool copy=false) const
void SetExtent(const core::Tensor &extent)
Set the extent of the box. If the data type of the given tensor differs from the data type of the box...
OrientedBoundingBox Clone() const
Returns copy of the OrientedBoundingBox on the same device.
cloudViewer::geometry::OrientedBoundingBox ToLegacy() const
Convert to a legacy CloudViewer oriented box.
core::Dtype GetDtype() const
Returns the data type attribute of this OrientedBoundingBox.
OrientedBoundingBox & Scale(double scale, const utility::optional< core::Tensor > &center=utility::nullopt)
Scale the axis-aligned box. If is the min_bound and is the max_bound of the axis aligned bounding b...
OrientedBoundingBox & Clear() override
Clear all elements in the geometry.
bool IsEmpty() const override
Returns true iff the geometry is empty.
static OrientedBoundingBox CreateFromPoints(const core::Tensor &points, bool robust=false, MethodOBBCreate method=MethodOBBCreate::MINIMAL_APPROX)
OrientedBoundingBox & Transform(const core::Tensor &transformation)
Transform the oriented box by the given transformation matrix.
OrientedBoundingBox & Translate(const core::Tensor &translation, bool relative=true)
Translate the oriented box by the given translation. If relative is true, the translation is added to...
static OrientedBoundingBox CreateFromAxisAlignedBoundingBox(const AxisAlignedBoundingBox &aabb)
void SetCenter(const core::Tensor &center)
Set the center of the box. If the data type of the given tensor differs from the data type of the box...
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
core::Device GetDevice() const override
Returns the device attribute of this OrientedBoundingBox.
static OrientedBoundingBox FromLegacy(const cloudViewer::geometry::OrientedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
std::string ToString() const
Text description.
void SetRotation(const core::Tensor &rotation)
Set the rotation matrix of the box. If the data type of the given tensor differs from the data type o...
double Volume() const
Returns the volume of the bounding box.
void SetColor(const core::Tensor &color)
Set the color of the box.
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype Float32
Definition: Dtype.cpp:42
::ecvOrientedBBox OrientedBoundingBox
@ MINIMAL_APPROX
Minimal OBB approximation.
constexpr nullopt_t nullopt
Definition: Optional.h:136
Generic file read and write utility for python interface.