Open3D (C++ API)  0.19.0
BoundingVolume.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include "open3d/core/Tensor.h"
15 #include "open3d/utility/Logging.h"
16 
17 namespace open3d {
18 namespace t {
19 namespace geometry {
20 
21 class OrientedBoundingBox;
22 
47 public:
49  AxisAlignedBoundingBox(const core::Device &device = core::Device("CPU:0"));
50 
60  AxisAlignedBoundingBox(const core::Tensor &min_bound,
61  const core::Tensor &max_bound);
62 
63  virtual ~AxisAlignedBoundingBox() override {}
64 
66  core::Device GetDevice() const override { return device_; }
67 
69  core::Dtype GetDtype() const { return dtype_; }
70 
77  bool copy = false) const;
78 
81  return To(GetDevice(), /*copy=*/true);
82  }
83 
84  AxisAlignedBoundingBox &Clear() override;
85 
86  bool IsEmpty() const override { return Volume() == 0; }
87 
94  void SetMinBound(const core::Tensor &min_bound);
95 
102  void SetMaxBound(const core::Tensor &max_bound);
103 
110  void SetColor(const core::Tensor &color);
111 
112 public:
113  core::Tensor GetMinBound() const { return min_bound_; }
114 
115  core::Tensor GetMaxBound() const { return max_bound_; }
116 
117  core::Tensor GetColor() const { return color_; }
118 
119  core::Tensor GetCenter() const { return (min_bound_ + max_bound_) * 0.5; }
120 
130  AxisAlignedBoundingBox &Translate(const core::Tensor &translation,
131  bool relative = true);
132 
145  double scale,
146  const std::optional<core::Tensor> &center = std::nullopt);
147 
152 
155 
157  core::Tensor GetHalfExtent() const { return GetExtent() / 2; }
158 
161  double GetMaxExtent() const {
162  return GetExtent().Max({0}).To(core::Float64).Item<double>();
163  }
164 
167  double GetXPercentage(double x) const;
168 
171  double GetYPercentage(double y) const;
172 
175  double GetZPercentage(double z) const;
176 
178  double Volume() const {
179  return GetExtent().Prod({0}).To(core::Float64).Item<double>();
180  }
181 
185  core::Tensor GetBoxPoints() const;
186 
191  const core::Tensor &points) const;
192 
194  std::string ToString() const;
195 
198 
201 
211  const core::Dtype &dtype = core::Float32,
212  const core::Device &device = core::Device("CPU:0"));
213 
220 
221 protected:
232 };
233 
258 public:
260  OrientedBoundingBox(const core::Device &device = core::Device("CPU:0"));
261 
274  OrientedBoundingBox(const core::Tensor &center,
275  const core::Tensor &rotation,
276  const core::Tensor &extent);
277 
278  virtual ~OrientedBoundingBox() override {}
279 
281  core::Device GetDevice() const override { return device_; }
282 
284  core::Dtype GetDtype() const { return dtype_; }
285 
291  OrientedBoundingBox To(const core::Device &device, bool copy = false) const;
292 
294  OrientedBoundingBox Clone() const { return To(GetDevice(), /*copy=*/true); }
295 
296  OrientedBoundingBox &Clear() override;
297 
298  bool IsEmpty() const override { return Volume() == 0; }
299 
305  void SetCenter(const core::Tensor &center);
306 
312  void SetRotation(const core::Tensor &rotation);
313 
319  void SetExtent(const core::Tensor &extent);
320 
325  void SetColor(const core::Tensor &color);
326 
327 public:
328  core::Tensor GetMinBound() const;
329 
330  core::Tensor GetMaxBound() const;
331 
332  core::Tensor GetColor() const { return color_; }
333 
334  core::Tensor GetCenter() const { return center_; }
335 
336  core::Tensor GetRotation() const { return rotation_; }
337 
338  core::Tensor GetExtent() const { return extent_; }
339 
347  OrientedBoundingBox &Translate(const core::Tensor &translation,
348  bool relative = true);
349 
359  const core::Tensor &rotation,
360  const std::optional<core::Tensor> &center = std::nullopt);
361 
366  OrientedBoundingBox &Transform(const core::Tensor &transformation);
367 
380  double scale,
381  const std::optional<core::Tensor> &center = std::nullopt);
382 
384  double Volume() const {
385  return GetExtent().Prod({0}).To(core::Float64).Item<double>();
386  }
387 
411  core::Tensor GetBoxPoints() const;
412 
417  const core::Tensor &points) const;
418 
420  std::string ToString() const;
421 
424 
427 
434  const AxisAlignedBoundingBox &aabb);
435 
444  const core::Dtype &dtype = core::Float32,
445  const core::Device &device = core::Device("CPU:0"));
446 
472  const core::Tensor &points,
473  bool robust = false,
475 
476 protected:
483 };
484 
513 public:
517  const core::Device &device = core::Device("CPU:0"));
518 
532  const core::Tensor &rotation,
533  const core::Tensor &radii);
534 
535  virtual ~OrientedBoundingEllipsoid() override {}
536 
538  core::Device GetDevice() const override { return device_; }
539 
542  core::Dtype GetDtype() const { return dtype_; }
543 
551  const core::Dtype &dtype,
552  bool copy = false) const;
553 
556  return To(GetDevice(), GetDtype(), /*copy=*/true);
557  }
558 
559  OrientedBoundingEllipsoid &Clear() override;
560 
561  bool IsEmpty() const override { return Volume() == 0; }
562 
568  void SetCenter(const core::Tensor &center);
569 
575  void SetRotation(const core::Tensor &rotation);
576 
582  void SetRadii(const core::Tensor &radii);
583 
588  void SetColor(const core::Tensor &color);
589 
590 public:
591  core::Tensor GetMinBound() const;
592 
593  core::Tensor GetMaxBound() const;
594 
595  core::Tensor GetColor() const { return color_; }
596 
597  core::Tensor GetCenter() const { return center_; }
598 
599  core::Tensor GetRotation() const { return rotation_; }
600 
601  core::Tensor GetRadii() const { return radii_; }
602 
611  bool relative = true);
612 
622  const core::Tensor &rotation,
623  const std::optional<core::Tensor> &center = std::nullopt);
624 
632  double scale,
633  const std::optional<core::Tensor> &center = std::nullopt);
634 
636  double Volume() const;
637 
666 
671  const core::Tensor &points) const;
672 
674  std::string ToString() const;
675 
678 
681 
683  OrientedBoundingBox GetOrientedBoundingBox(bool robust = false) const;
684 
687  bool robust = false) const;
688 
698  const core::Dtype &dtype = core::Float32,
699  const core::Device &device = core::Device("CPU:0"));
700 
710  const core::Tensor &points, bool robust = false);
711 
712 protected:
719 };
720 
721 } // namespace geometry
722 } // namespace t
723 } // namespace open3d
math::float4 color
Definition: LineSetBuffers.cpp:45
double t
Definition: SurfaceReconstructionPoisson.cpp:172
bool copy
Definition: VtkUtils.cpp:74
Definition: Device.h:18
Definition: Dtype.h:20
Definition: Tensor.h:32
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1357
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1371
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:285
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:138
Definition: BoundingVolume.h:20
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:46
double GetZPercentage(double z) const
Definition: BoundingVolume.cpp:194
double Volume() const
Returns the volume of the bounding box.
Definition: BoundingVolume.h:178
core::Tensor GetMinBound() const
Definition: BoundingVolume.h:113
core::Dtype GetDtype() const
Returns the data type attribute of this AxisAlignedBoundingBox.
Definition: BoundingVolume.h:69
static AxisAlignedBoundingBox FromLegacy(const open3d::geometry::AxisAlignedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Definition: BoundingVolume.cpp:267
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:75
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
Definition: BoundingVolume.cpp:200
OrientedBoundingBox GetOrientedBoundingBox() const
Convert to an oriented box.
Definition: BoundingVolume.cpp:263
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:111
double GetYPercentage(double y) const
Definition: BoundingVolume.cpp:188
double GetMaxExtent() const
Returns the maximum extent, i.e. the maximum of X, Y and Z axis' extents.
Definition: BoundingVolume.h:161
core::Dtype dtype_
The data type of the bounding box.
Definition: BoundingVolume.h:225
virtual ~AxisAlignedBoundingBox() override
Definition: BoundingVolume.h:63
open3d::geometry::AxisAlignedBoundingBox ToLegacy() const
Convert to a legacy Open3D axis-aligned box.
Definition: BoundingVolume.cpp:250
AxisAlignedBoundingBox Clone() const
Returns copy of the AxisAlignedBoundingBox on the same device.
Definition: BoundingVolume.h:80
std::string ToString() const
Text description.
Definition: BoundingVolume.cpp:227
core::Tensor GetExtent() const
Get the extent/length of the bounding box in x, y, and z dimension.
Definition: BoundingVolume.h:154
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:163
AxisAlignedBoundingBox & Scale(double scale, const std::optional< core::Tensor > &center=std::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:145
core::Tensor max_bound_
The upper x, y, z bounds of the bounding box.
Definition: BoundingVolume.h:229
AxisAlignedBoundingBox & Clear() override
Clear all elements in the geometry.
Definition: BoundingVolume.cpp:68
core::Tensor GetColor() const
Definition: BoundingVolume.h:117
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:93
core::Tensor GetCenter() const
Definition: BoundingVolume.h:119
AxisAlignedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty AxisAlignedBoundingBox on the provided device.
Definition: BoundingVolume.cpp:20
core::Device device_
The device to use for the bounding box. The default is CPU:0.
Definition: BoundingVolume.h:223
core::Tensor GetMaxBound() const
Definition: BoundingVolume.h:115
AxisAlignedBoundingBox & Translate(const core::Tensor &translation, bool relative=true)
Translate the axis-aligned box by the given translation.
Definition: BoundingVolume.cpp:127
core::Tensor min_bound_
The lower x, y, z bounds of the bounding box.
Definition: BoundingVolume.h:227
core::Tensor color_
The color of the bounding box in RGB. The default is white.
Definition: BoundingVolume.h:231
core::Tensor GetHalfExtent() const
Returns the half extent of the bounding box.
Definition: BoundingVolume.h:157
core::Device GetDevice() const override
Returns the device attribute of this AxisAlignedBoundingBox.
Definition: BoundingVolume.h:66
double GetXPercentage(double x) const
Definition: BoundingVolume.cpp:182
AxisAlignedBoundingBox To(const core::Device &device, bool copy=false) const
Transfer the AxisAlignedBoundingBox to a specified device.
Definition: BoundingVolume.cpp:56
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
Definition: BoundingVolume.cpp:211
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: BoundingVolume.h:86
static AxisAlignedBoundingBox CreateFromPoints(const core::Tensor &points)
Definition: BoundingVolume.cpp:234
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:19
The base geometry class.
Definition: Geometry.h:23
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:257
static OrientedBoundingBox CreateFromAxisAlignedBoundingBox(const AxisAlignedBoundingBox &aabb)
Definition: BoundingVolume.cpp:534
core::Dtype dtype_
Definition: BoundingVolume.h:478
double Volume() const
Returns the volume of the bounding box.
Definition: BoundingVolume.h:384
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:377
core::Tensor center_
Definition: BoundingVolume.h:479
core::Dtype GetDtype() const
Returns the data type attribute of this OrientedBoundingBox.
Definition: BoundingVolume.h:284
static OrientedBoundingBox FromLegacy(const open3d::geometry::OrientedBoundingBox &box, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Definition: BoundingVolume.cpp:543
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: BoundingVolume.h:298
static OrientedBoundingBox CreateFromPoints(const core::Tensor &points, bool robust=false, MethodOBBCreate method=MethodOBBCreate::MINIMAL_APPROX)
Definition: BoundingVolume.cpp:570
core::Tensor extent_
Definition: BoundingVolume.h:481
core::Tensor GetColor() const
Definition: BoundingVolume.h:332
core::Tensor GetCenter() const
Definition: BoundingVolume.h:334
core::Tensor GetPointIndicesWithinBoundingBox(const core::Tensor &points) const
Indices to points that are within the bounding box.
Definition: BoundingVolume.cpp:495
core::Tensor rotation_
Definition: BoundingVolume.h:480
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:355
OrientedBoundingBox & Clear() override
Clear all elements in the geometry.
Definition: BoundingVolume.cpp:347
core::Tensor GetRotation() const
Definition: BoundingVolume.h:336
OrientedBoundingBox & Transform(const core::Tensor &transformation)
Transform the oriented box by the given transformation matrix.
Definition: BoundingVolume.cpp:464
core::Tensor GetMaxBound() const
Definition: BoundingVolume.cpp:410
core::Device device_
Definition: BoundingVolume.h:477
void SetColor(const core::Tensor &color)
Set the color of the box.
Definition: BoundingVolume.cpp:391
std::string ToString() const
Text description.
Definition: BoundingVolume.cpp:512
open3d::geometry::OrientedBoundingBox ToLegacy() const
Convert to a legacy Open3D oriented box.
Definition: BoundingVolume.cpp:517
virtual ~OrientedBoundingBox() override
Definition: BoundingVolume.h:278
OrientedBoundingBox(const core::Device &device=core::Device("CPU:0"))
Construct an empty OrientedBoundingBox on the provided device.
Definition: BoundingVolume.cpp:292
core::Device GetDevice() const override
Returns the device attribute of this OrientedBoundingBox.
Definition: BoundingVolume.h:281
OrientedBoundingBox To(const core::Device &device, bool copy=false) const
Definition: BoundingVolume.cpp:334
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Convert to an axis-aligned box.
Definition: BoundingVolume.cpp:530
OrientedBoundingBox & Scale(double scale, const std::optional< core::Tensor > &center=std::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:480
core::Tensor color_
Definition: BoundingVolume.h:482
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:363
core::Tensor GetMinBound() const
Definition: BoundingVolume.cpp:406
core::Tensor GetBoxPoints() const
Returns the eight points that define the bounding box.
Definition: BoundingVolume.cpp:414
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:420
OrientedBoundingBox & Rotate(const core::Tensor &rotation, const std::optional< core::Tensor > &center=std::nullopt)
Rotate the oriented box by the given rotation matrix. If the rotation matrix is not orthogonal,...
Definition: BoundingVolume.cpp:435
core::Tensor GetExtent() const
Definition: BoundingVolume.h:338
OrientedBoundingBox Clone() const
Returns copy of the OrientedBoundingBox on the same device.
Definition: BoundingVolume.h:294
A bounding ellipsoid oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:512
core::Tensor radii_
Definition: BoundingVolume.h:717
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Convert to an axis-aligned box.
Definition: BoundingVolume.cpp:855
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: BoundingVolume.h:561
OrientedBoundingEllipsoid Clone() const
Returns copy of the OrientedBoundingEllipsoid on the same device.
Definition: BoundingVolume.h:555
core::Tensor GetRotation() const
Definition: BoundingVolume.h:599
OrientedBoundingEllipsoid & Clear() override
Clear all elements in the geometry.
Definition: BoundingVolume.cpp:655
OrientedBoundingEllipsoid To(const core::Device &device, const core::Dtype &dtype, bool copy=false) const
Definition: BoundingVolume.cpp:642
OrientedBoundingEllipsoid(const core::Device &device=core::Device("CPU:0"))
Construct an empty OrientedBoundingEllipsoid on the provided device.
Definition: BoundingVolume.cpp:599
core::Device device_
Definition: BoundingVolume.h:713
core::Tensor GetEllipsoidPoints() const
Returns the six critical points of the bounding ellipsoid.
Definition: BoundingVolume.cpp:727
static OrientedBoundingEllipsoid FromLegacy(const open3d::geometry::OrientedBoundingEllipsoid &ellipsoid, const core::Dtype &dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Definition: BoundingVolume.cpp:871
core::Tensor GetColor() const
Definition: BoundingVolume.h:595
double Volume() const
Returns the volume of the bounding ellipsoid.
Definition: BoundingVolume.cpp:722
OrientedBoundingBox GetOrientedBoundingBox(bool robust=false) const
Returns an oriented bounding box around the ellipsoid.
Definition: BoundingVolume.cpp:860
static OrientedBoundingEllipsoid CreateFromPoints(const core::Tensor &points, bool robust=false)
Definition: BoundingVolume.cpp:899
core::Tensor GetMinBound() const
Definition: BoundingVolume.cpp:714
core::Tensor GetRadii() const
Definition: BoundingVolume.h:601
core::Tensor center_
Definition: BoundingVolume.h:715
OrientedBoundingBox GetMinimalOrientedBoundingBox(bool robust=false) const
Returns the minimal oriented bounding box around the ellipsoid.
Definition: BoundingVolume.cpp:865
open3d::geometry::OrientedBoundingEllipsoid ToLegacy() const
Convert to a legacy Open3D oriented bounding ellipsoid.
Definition: BoundingVolume.cpp:838
core::Dtype GetDtype() const
Returns the data type attribute of this OrientedBoundingEllipsoid.
Definition: BoundingVolume.h:542
void SetCenter(const core::Tensor &center)
Set the center of the ellipsoid. If the data type of the given tensor differs from the data type of t...
Definition: BoundingVolume.cpp:663
core::Device GetDevice() const override
Returns the device attribute of this OrientedBoundingEllipsoid.
Definition: BoundingVolume.h:538
core::Tensor GetPointIndicesWithinBoundingEllipsoid(const core::Tensor &points) const
Indices to points that are within the bounding ellipsoid.
Definition: BoundingVolume.cpp:813
core::Dtype dtype_
Definition: BoundingVolume.h:714
OrientedBoundingEllipsoid & Scale(double scale, const std::optional< core::Tensor > &center=std::nullopt)
Scale the oriented ellipsoid. The scaling center will be the ellipsoid center if it is not specified.
Definition: BoundingVolume.cpp:798
core::Tensor GetCenter() const
Definition: BoundingVolume.h:597
core::Tensor GetMaxBound() const
Definition: BoundingVolume.cpp:718
OrientedBoundingEllipsoid & Rotate(const core::Tensor &rotation, const std::optional< core::Tensor > &center=std::nullopt)
Rotate the oriented ellipsoid by the given rotation matrix. If the rotation matrix is not orthogonal,...
Definition: BoundingVolume.cpp:769
OrientedBoundingEllipsoid & Translate(const core::Tensor &translation, bool relative=true)
Translate the oriented ellipsoid by the given translation. If relative is true, the translation is ad...
Definition: BoundingVolume.cpp:754
core::Tensor color_
Definition: BoundingVolume.h:718
core::Tensor rotation_
Definition: BoundingVolume.h:716
std::string ToString() const
Text description.
Definition: BoundingVolume.cpp:832
virtual ~OrientedBoundingEllipsoid() override
Definition: BoundingVolume.h:535
void SetColor(const core::Tensor &color)
Set the color of the ellipsoid.
Definition: BoundingVolume.cpp:699
void SetRotation(const core::Tensor &rotation)
Set the rotation matrix of the ellipsoid. If the data type of the given tensor differs from the data ...
Definition: BoundingVolume.cpp:685
void SetRadii(const core::Tensor &radii)
Set the radii of the ellipsoid. If the data type of the given tensor differs from the data type of th...
Definition: BoundingVolume.cpp:671
int points
Definition: FilePCD.cpp:54
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype Float32
Definition: Dtype.cpp:42
MethodOBBCreate
Definition: Geometry.h:120
@ MINIMAL_APPROX
Minimal OBB approximation.
Definition: PinholeCameraIntrinsic.cpp:16