ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvOrientedBBox.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 "ecvOrientedBBox.h"
9 
10 #include "ecvDisplayTools.h"
11 #include "ecvGLMatrix.h"
12 #include "ecvMesh.h"
13 #include "ecvPointCloud.h"
14 #include "ecvQhull.h"
15 
16 // CV_CORE_LIB
18 
19 // EIGEN
20 #include <Eigen/Eigenvalues>
21 
22 // SYSTEM
23 #include <numeric>
24 
26  // Use default color from context
27  draw(context, context.bbDefaultCol);
28 }
29 
32  return;
33  }
34 
35  context.viewID = QString("BBox-") + context.viewID;
38 }
39 
40 Eigen::Vector3d ecvOrientedBBox::GetMinBound() const {
41  auto points = GetBoxPoints();
42  return ComputeMinBound(points);
43 }
44 
45 Eigen::Vector3d ecvOrientedBBox::GetMaxBound() const {
46  auto points = GetBoxPoints();
47  return ComputeMaxBound(points);
48 }
49 
50 Eigen::Vector3d ecvOrientedBBox::GetCenter() const { return center_; }
51 
52 ccBBox ecvOrientedBBox::getOwnBB(bool withGLFeatures) {
54 }
55 
58 }
59 
61  return *this;
62 }
63 
65  const Eigen::Matrix4d& transformation) {
66  const Eigen::Matrix3d rotation = transformation.block<3, 3>(0, 0);
67  const Eigen::Vector3d translation = transformation.block<3, 1>(0, 3);
68  this->Rotate(rotation, Eigen::Vector3d(0.0, 0.0, 0.0));
69  this->Translate(translation, true);
70  return *this;
71 }
72 
73 ecvOrientedBBox& ecvOrientedBBox::Translate(const Eigen::Vector3d& translation,
74  bool relative) {
75  if (relative) {
76  center_ += translation;
77  } else {
78  center_ = translation;
79  }
80  return *this;
81 }
82 
84  const Eigen::Vector3d& center) {
85  extent_ *= scale;
86  center_ = scale * (center_ - center) + center;
87  return *this;
88 }
89 
90 ecvOrientedBBox& ecvOrientedBBox::Rotate(const Eigen::Matrix3d& R,
91  const Eigen::Vector3d& center) {
92  R_ = R * R_;
93  center_ = R * (center_ - center) + center;
94  return *this;
95 }
96 
98  ecvOrientedBBox rotatedBox(*this);
99  rotatedBox.Rotate(ccGLMatrixd::ToEigenMatrix3(mat),
100  Eigen::Vector3d(0.0, 0.0, 0.0));
102  true);
103  return rotatedBox;
104 }
105 
107  ecvOrientedBBox rotatedBox(*this);
108  rotatedBox.Rotate(ccGLMatrixd::ToEigenMatrix3(mat),
109  Eigen::Vector3d(0.0, 0.0, 0.0));
111  true);
112  return rotatedBox;
113 }
114 
116  const std::vector<Eigen::Vector3d>& points) {
118 }
119 
121  const std::vector<CCVector3>& points) {
122  auto mesh = std::get<0>(
124  ccGenericPointCloud* hull_pcd = mesh->getAssociatedCloud();
125  assert(hull_pcd);
126 
127  Eigen::Vector3d mean;
128  Eigen::Matrix3d cov;
129  std::tie(mean, cov) = hull_pcd->computeMeanAndCovariance();
130 
131  Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> es(cov);
132  Eigen::Vector3d evals = es.eigenvalues();
133  Eigen::Matrix3d R = es.eigenvectors();
134  R.col(0) /= R.col(0).norm();
135  R.col(1) /= R.col(1).norm();
136  R.col(2) /= R.col(2).norm();
137 
138  if (evals(1) > evals(0)) {
139  std::swap(evals(1), evals(0));
140  Eigen::Vector3d tmp = R.col(1);
141  R.col(1) = R.col(0);
142  R.col(0) = tmp;
143  }
144  if (evals(2) > evals(0)) {
145  std::swap(evals(2), evals(0));
146  Eigen::Vector3d tmp = R.col(2);
147  R.col(2) = R.col(0);
148  R.col(0) = tmp;
149  }
150  if (evals(2) > evals(1)) {
151  std::swap(evals(2), evals(1));
152  Eigen::Vector3d tmp = R.col(2);
153  R.col(2) = R.col(1);
154  R.col(1) = tmp;
155  }
156 
157  hull_pcd->placeIteratorAtBeginning();
158  CCVector3* pt = nullptr;
159  while ((pt = const_cast<CCVector3*>(hull_pcd->getNextPoint()))) {
160  Eigen::Vector3d vc = CCVector3d::fromArray(*pt - mean);
161  Eigen::Vector3d data = R.transpose() * vc;
162  *pt = data;
163  }
164 
165  const auto aabox = hull_pcd->GetAxisAlignedBoundingBox();
166 
167  ecvOrientedBBox obox;
168  obox.center_ = R * aabox.GetCenter() + mean;
169  obox.R_ = R;
170  obox.extent_ = aabox.GetExtent();
171 
172  return obox;
173 }
174 
176  const ccBBox& aabox) {
177  ecvOrientedBBox obox;
178  obox.center_ = aabox.GetCenter();
179  obox.extent_ = aabox.GetExtent();
180  obox.R_ = Eigen::Matrix3d::Identity();
181  return obox;
182 }
int points
Eigen::Matrix3d rotation
Definition: VoxelGridIO.cpp:27
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
static std::vector< Eigen::Matrix< double, 3, 1 > > fromArrayContainer(const std::vector< Vector3Tpl< PointCoordinateType >> &container)
Definition: CVGeom.h:301
Bounding box structure.
Definition: ecvBBox.h:25
static ccBBox CreateFromPoints(const std::vector< CCVector3 > &points)
Definition: ecvBBox.cpp:82
Eigen::Vector3d GetExtent() const
Get the extent/length of the bounding box in x, y, and z dimension.
Definition: ecvBBox.h:147
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
Definition: ecvBBox.h:87
Vector3Tpl< T > getTranslationAsVec3D() const
Returns a copy of the translation as a CCVector3.
static Eigen::Matrix< double, 3, 3 > ToEigenMatrix3(const ccGLMatrixTpl< float > &mat)
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Double version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:56
A 3D cloud interface with associated features (color, normals, octree, etc.)
std::tuple< Eigen::Vector3d, Eigen::Matrix3d > computeMeanAndCovariance() const
static Eigen::Vector3d ComputeMinBound(const std::vector< Eigen::Vector3d > &points)
Compute min bound of a list points.
Definition: ecvHObject.cpp:272
virtual ccBBox GetAxisAlignedBoundingBox() const
Returns an axis-aligned bounding box of the geometry.
Definition: ecvHObject.cpp:447
static Eigen::Vector3d ComputeMaxBound(const std::vector< Eigen::Vector3d > &points)
Compute max bound of a list points.
Definition: ecvHObject.cpp:284
virtual void placeIteratorAtBeginning()=0
Sets the cloud iterator at the beginning.
virtual const CCVector3 * getNextPoint()=0
Returns the next point (relatively to the global iterator position)
Eigen::Vector3d extent_
The extent of the bounding box in its frame of reference.
std::vector< Eigen::Vector3d > GetBoxPoints() const
Eigen::Vector3d center_
The center point of the bounding box.
void SetColor(const Eigen::Vector3d &color)
Sets the bounding box color.
static std::tuple< std::shared_ptr< ccMesh >, std::vector< size_t > > ComputeConvexHull(const std::vector< Eigen::Vector3d > &points)
Definition: ecvQhull.cpp:34
RGB color structure.
Definition: ecvColorTypes.h:49
static Eigen::Vector3d ToEigen(const Type col[3])
Definition: ecvColorTypes.h:72
static void DrawOrientedBBox(const CC_DRAW_CONTEXT &context, const ecvOrientedBBox *obb)
static QMainWindow * GetMainWindow()
virtual ecvOrientedBBox & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &center) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ,...
static ecvOrientedBBox CreateFromAxisAlignedBoundingBox(const ccBBox &aabox)
virtual ecvOrientedBBox & Scale(const double scale, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
virtual ecvOrientedBBox & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
virtual ecvOrientedBBox & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
const ecvOrientedBBox operator*(const ccGLMatrix &mat)
Applies transformation to the bounding box.
void draw(CC_DRAW_CONTEXT &context) override
Draws entity and its children.
static ecvOrientedBBox CreateFromPoints(const std::vector< Eigen::Vector3d > &points)
virtual Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
virtual ccBBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
GraphType data
Definition: graph_cut.cc:138
ImGuiContext * context
Definition: Window.cpp:76
void swap(cloudViewer::core::SmallVectorImpl< T > &LHS, cloudViewer::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370
Display context.