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
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::t::geometry::AxisAlignedBoundingBox::Volume
double Volume() const
Returns the volume of the bounding box.
Definition: BoundingVolume.h:179
cloudViewer::t::geometry::OrientedBoundingBox::GetPointIndicesWithinBoundingBox
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
Definition: BoundingVolume.cpp:509
cloudViewer::t::geometry::AxisAlignedBoundingBox::color_
core::Tensor color_
The color of the bounding box in RGB. The default is white.
Definition: BoundingVolume.h:232
cloudViewer::t::geometry::OrientedBoundingBox::GetMinBound
core::Tensor GetMinBound() const
Definition: BoundingVolume.cpp:420
x
normal_z x
Definition: my_point_types.h:165
cloudViewer::t::geometry::AxisAlignedBoundingBox::max_bound_
core::Tensor max_bound_
The upper x, y, z bounds of the bounding box.
Definition: BoundingVolume.h:230
cloudViewer::t::geometry::AxisAlignedBoundingBox::Scale
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...
Definition: BoundingVolume.cpp:144
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetBoxPoints
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
Definition: BoundingVolume.cpp:199
cloudViewer::t::geometry::OrientedBoundingBox::OrientedBoundingBox
OrientedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty OrientedBoundingBox on the provided device.
Definition: BoundingVolume.cpp:306
core::Tensor
cloudViewer::t::geometry::MethodOBBCreate::MINIMAL_APPROX
@ MINIMAL_APPROX
Minimal OBB approximation.
cloudViewer::t::geometry::OrientedBoundingBox::Rotate
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,...
Definition: BoundingVolume.cpp:449
copy
bool copy
Definition: VtkUtils.cpp:74
cloudViewer::t::geometry::AxisAlignedBoundingBox
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:47
cloudViewer::t::geometry::OrientedBoundingBox::IsEmpty
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: BoundingVolume.h:299
cloudViewer::t::geometry::OrientedBoundingBox::extent_
core::Tensor extent_
Definition: BoundingVolume.h:482
cloudViewer::t::geometry::OrientedBoundingBox::SetRotation
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...
Definition: BoundingVolume.cpp:391
cloudViewer::t::geometry::OrientedBoundingBox::Clear
OrientedBoundingBox & Clear() override
Clear all elements in the geometry.
Definition: BoundingVolume.cpp:361
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetDevice
core::Device GetDevice() const override
Returns the device attribute of this AxisAlignedBoundingBox.
Definition: BoundingVolume.h:67
ccBBox
Bounding box structure.
Definition: ecvBBox.h:25
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetXPercentage
double GetXPercentage(double x) const
Definition: BoundingVolume.cpp:181
cloudViewer::t::geometry::MethodOBBCreate
MethodOBBCreate
Definition: Geometry.h:118
DrawableGeometry.h
cloudViewer::t::geometry::AxisAlignedBoundingBox::SetMaxBound
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 ...
Definition: BoundingVolume.cpp:92
cloudViewer::t::geometry::AxisAlignedBoundingBox::SetColor
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,...
Definition: BoundingVolume.cpp:110
cloudViewer::t::geometry::AxisAlignedBoundingBox::min_bound_
core::Tensor min_bound_
The lower x, y, z bounds of the bounding box.
Definition: BoundingVolume.h:228
cloudViewer::t::geometry::OrientedBoundingBox::SetCenter
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...
Definition: BoundingVolume.cpp:369
cloudViewer::t::geometry::OrientedBoundingBox::FromLegacy
static OrientedBoundingBox FromLegacy(const cloudViewer::geometry::OrientedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Definition: BoundingVolume.cpp:557
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetDtype
core::Dtype GetDtype() const
Returns the data type attribute of this AxisAlignedBoundingBox.
Definition: BoundingVolume.h:70
Geometry.h
cloudViewer::t::geometry::OrientedBoundingBox::color_
core::Tensor color_
Definition: BoundingVolume.h:483
cloudViewer::t::geometry::AxisAlignedBoundingBox::IsEmpty
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: BoundingVolume.h:87
cloudViewer::t::geometry::AxisAlignedBoundingBox::To
AxisAlignedBoundingBox To(const core::Device &device, bool copy=false) const
Transfer the AxisAlignedBoundingBox to a specified device.
Definition: BoundingVolume.cpp:55
color
math::float4 color
Definition: LineSetBuffers.cpp:46
cloudViewer::t::geometry::OrientedBoundingBox::Clone
OrientedBoundingBox Clone() const
Returns copy of the OrientedBoundingBox on the same device.
Definition: BoundingVolume.h:295
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetYPercentage
double GetYPercentage(double y) const
Definition: BoundingVolume.cpp:187
cloudViewer::t::geometry::AxisAlignedBoundingBox::~AxisAlignedBoundingBox
virtual ~AxisAlignedBoundingBox() override
Definition: BoundingVolume.h:64
cloudViewer::t::geometry::OrientedBoundingBox::SetExtent
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...
Definition: BoundingVolume.cpp:377
cloudViewer::t::geometry::OrientedBoundingBox
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:258
cloudViewer::core::Dtype
Definition: Dtype.h:21
cloudViewer::t::geometry::AxisAlignedBoundingBox::dtype_
core::Dtype dtype_
The data type of the bounding box.
Definition: BoundingVolume.h:226
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetOrientedBoundingBox
OrientedBoundingBox GetOrientedBoundingBox() const
Convert to an oriented box.
Definition: BoundingVolume.cpp:275
cloudViewer::OrientedBoundingBox
A bounding box oriented along an arbitrary frame of reference.
Definition: OrientedBoundingBox.h:22
cloudViewer::core::Tensor::Max
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1275
cloudViewer::t::geometry::OrientedBoundingBox::~OrientedBoundingBox
virtual ~OrientedBoundingBox() override
Definition: BoundingVolume.h:279
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetExtent
core::Tensor GetExtent() const
Get the extent/length of the bounding box in x, y, and z dimension.
Definition: BoundingVolume.h:155
cloudViewer::t::geometry::AxisAlignedBoundingBox::FromLegacy
static AxisAlignedBoundingBox FromLegacy(const cloudViewer::geometry::AxisAlignedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Definition: BoundingVolume.cpp:279
cloudViewer::t::geometry::OrientedBoundingBox::GetMaxBound
core::Tensor GetMaxBound() const
Definition: BoundingVolume.cpp:424
cloudViewer::t::geometry::OrientedBoundingBox::Transform
OrientedBoundingBox & Transform(const core::Tensor &transformation)
Transform the oriented box by the given transformation matrix.
Definition: BoundingVolume.cpp:478
cloudViewer::t::geometry::OrientedBoundingBox::center_
core::Tensor center_
Definition: BoundingVolume.h:480
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetMaxBound
core::Tensor GetMaxBound() const
Definition: BoundingVolume.h:116
cloudViewer::t::geometry::OrientedBoundingBox::Scale
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...
Definition: BoundingVolume.cpp:494
y
normal_z y
Definition: my_point_types.h:168
cloudViewer::t::geometry::OrientedBoundingBox::device_
core::Device device_
Definition: BoundingVolume.h:478
cloudViewer::t::geometry::AxisAlignedBoundingBox::ToLegacy
cloudViewer::geometry::AxisAlignedBoundingBox ToLegacy() const
Convert to a legacy CloudViewer axis-aligned box.
Definition: BoundingVolume.cpp:249
cloudViewer::t::geometry::OrientedBoundingBox::GetAxisAlignedBoundingBox
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Convert to an axis-aligned box.
Definition: BoundingVolume.cpp:544
cloudViewer::t::geometry::OrientedBoundingBox::GetCenter
core::Tensor GetCenter() const
Definition: BoundingVolume.h:335
cloudViewer::t::geometry::AxisAlignedBoundingBox::operator+=
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...
Definition: BoundingVolume.cpp:162
cloudViewer::t::geometry::AxisAlignedBoundingBox::Clear
AxisAlignedBoundingBox & Clear() override
Clear all elements in the geometry.
Definition: BoundingVolume.cpp:67
cloudViewer::t::geometry::DrawableGeometry
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:19
cloudViewer::t::geometry::OrientedBoundingBox::SetColor
void SetColor(const core::Tensor &color)
Set the color of the box.
Definition: BoundingVolume.cpp:405
cloudViewer::t::geometry::OrientedBoundingBox::GetColor
core::Tensor GetColor() const
Definition: BoundingVolume.h:333
cloudViewer::t::geometry::OrientedBoundingBox::GetBoxPoints
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
Definition: BoundingVolume.cpp:428
cloudViewer::t::geometry::AxisAlignedBoundingBox::ToString
std::string ToString() const
Text description.
Definition: BoundingVolume.cpp:226
cloudViewer::core::Device
Definition: Device.h:18
cloudViewer::t::geometry::OrientedBoundingBox::Volume
double Volume() const
Returns the volume of the bounding box.
Definition: BoundingVolume.h:385
TensorCheck.h
cloudViewer::t::geometry::OrientedBoundingBox::dtype_
core::Dtype dtype_
Definition: BoundingVolume.h:479
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetMaxExtent
double GetMaxExtent() const
Returns the maximum extent, i.e. the maximum of X, Y and Z axis' extents.
Definition: BoundingVolume.h:162
cloudViewer::t::geometry::AxisAlignedBoundingBox::Translate
AxisAlignedBoundingBox & Translate(const core::Tensor &translation, bool relative=true)
Translate the axis-aligned box by the given translation.
Definition: BoundingVolume.cpp:126
cloudViewer::core::Float64
const Dtype Float64
Definition: Dtype.cpp:43
cloudViewer::t::geometry::OrientedBoundingBox::GetDevice
core::Device GetDevice() const override
Returns the device attribute of this OrientedBoundingBox.
Definition: BoundingVolume.h:282
points
int points
Definition: FileIOFactory.cpp:144
cloudViewer::core::Float32
const Dtype Float32
Definition: Dtype.cpp:42
cloudViewer::t::geometry::OrientedBoundingBox::CreateFromPoints
static OrientedBoundingBox CreateFromPoints(const core::Tensor &points, bool robust=false, MethodOBBCreate method=MethodOBBCreate::MINIMAL_APPROX)
Definition: BoundingVolume.cpp:584
cloudViewer::utility::optional
Definition: Optional.h:44
cloudViewer::t::geometry::AxisAlignedBoundingBox::Clone
AxisAlignedBoundingBox Clone() const
Returns copy of the AxisAlignedBoundingBox on the same device.
Definition: BoundingVolume.h:81
cloudViewer::t::geometry::AxisAlignedBoundingBox::AxisAlignedBoundingBox
AxisAlignedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty AxisAlignedBoundingBox on the provided device.
Definition: BoundingVolume.cpp:19
cloudViewer::t::geometry::AxisAlignedBoundingBox::device_
core::Device device_
The device to use for the bounding box. The default is CPU:0.
Definition: BoundingVolume.h:224
cloudViewer::t::geometry::AxisAlignedBoundingBox::CreateFromPoints
static AxisAlignedBoundingBox CreateFromPoints(const core::Tensor &points)
Definition: BoundingVolume.cpp:233
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetMinBound
core::Tensor GetMinBound() const
Definition: BoundingVolume.h:114
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetColor
core::Tensor GetColor() const
Definition: BoundingVolume.h:118
cloudViewer::t::geometry::OrientedBoundingBox::GetExtent
core::Tensor GetExtent() const
Definition: BoundingVolume.h:339
BoundingVolume.h
cloudViewer::t::geometry::AxisAlignedBoundingBox::SetMinBound
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 ...
Definition: BoundingVolume.cpp:74
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetZPercentage
double GetZPercentage(double z) const
Definition: BoundingVolume.cpp:193
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetPointIndicesWithinBoundingBox
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
Definition: BoundingVolume.cpp:210
cloudViewer::t::geometry::OrientedBoundingBox::GetDtype
core::Dtype GetDtype() const
Returns the data type attribute of this OrientedBoundingBox.
Definition: BoundingVolume.h:285
cloudViewer::utility::nullopt
constexpr nullopt_t nullopt
Definition: Optional.h:136
Tensor.h
cloudViewer::t::geometry::OrientedBoundingBox::ToLegacy
cloudViewer::geometry::OrientedBoundingBox ToLegacy() const
Convert to a legacy CloudViewer oriented box.
Definition: BoundingVolume.cpp:531
rotation
Eigen::Matrix3d rotation
Definition: VoxelGridIO.cpp:27
cloudViewer::geometry::OrientedBoundingBox
::ecvOrientedBBox OrientedBoundingBox
Definition: BoundingVolume.h:22
z
normal_z z
Definition: my_point_types.h:165
cloudViewer::t::geometry::OrientedBoundingBox::rotation_
core::Tensor rotation_
Definition: BoundingVolume.h:481
cloudViewer::t::geometry::OrientedBoundingBox::Translate
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...
Definition: BoundingVolume.cpp:434
cloudViewer::t::geometry::OrientedBoundingBox::ToString
std::string ToString() const
Text description.
Definition: BoundingVolume.cpp:526
cloudViewer::t::geometry::OrientedBoundingBox::CreateFromAxisAlignedBoundingBox
static OrientedBoundingBox CreateFromAxisAlignedBoundingBox(const AxisAlignedBoundingBox &aabb)
Definition: BoundingVolume.cpp:548
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetHalfExtent
core::Tensor GetHalfExtent() const
Returns the half extent of the bounding box.
Definition: BoundingVolume.h:158
cloudViewer::t::geometry::AxisAlignedBoundingBox::GetCenter
core::Tensor GetCenter() const
Definition: BoundingVolume.h:120
cloudViewer::t::geometry::Geometry
The base geometry class.
Definition: Geometry.h:23
cloudViewer::t::geometry::OrientedBoundingBox::GetRotation
core::Tensor GetRotation() const
Definition: BoundingVolume.h:337
cloudViewer::t::geometry::OrientedBoundingBox::To
OrientedBoundingBox To(const core::Device &device, bool copy=false) const
Definition: BoundingVolume.cpp:348
cloudViewer::core::Tensor::Prod
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1261