ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvBBox.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 
10 // LOCAL
11 #include <Logging.h>
12 
13 #include "ecvDisplayTools.h"
14 #include "ecvOrientedBBox.h"
15 
16 using namespace cloudViewer;
17 
20 }
21 
22 ccBBox& ccBBox::Transform(const Eigen::Matrix4d& transformation) {
24  "A general transform of a ccBBox would not be axis "
25  "aligned anymore, convert it to a OrientedBoundingBox first");
26  return *this;
27 }
28 
29 ccBBox& ccBBox::Translate(const Eigen::Vector3d& translation, bool relative) {
30  if (relative) {
31  m_bbMin += translation;
32  m_bbMax += translation;
33  } else {
34  const Eigen::Vector3d half_extent = GetHalfExtent();
35  m_bbMin = CCVector3::fromArray(translation - half_extent);
36  m_bbMax = CCVector3::fromArray(translation + half_extent);
37  }
38  return *this;
39 }
40 
41 ccBBox& ccBBox::Scale(const double s, const Eigen::Vector3d& center) {
42  m_bbMin = static_cast<PointCoordinateType>(s) * (m_bbMin - center) + center;
43  m_bbMax = static_cast<PointCoordinateType>(s) * (m_bbMax - center) + center;
44  return *this;
45 }
46 
47 ccBBox& ccBBox::Rotate(const Eigen::Matrix3d& R,
48  const Eigen::Vector3d& center) {
50  "A rotation of a ccBBox would not be axis aligned "
51  "anymore, convert it to an ecvOrientedBBox first");
52  return *this;
53 }
54 
55 const ccBBox& ccBBox::operator+=(const ccBBox& other) {
56  if (IsEmpty()) {
57  this->m_bbMin = other.minCorner();
58  this->m_bbMax = other.maxCorner();
59  this->setValidity(true);
60  } else if (!other.IsEmpty()) {
61  this->add(other.minCorner());
62  this->add(other.maxCorner());
63  this->setValidity(true);
64  }
65  return *this;
66 }
67 
69  // Use default color from context
70  draw(context, context.bbDefaultCol);
71 }
72 
75  return;
76  }
77  context.bbDefaultCol = col;
78  context.viewID = QString("BBox-") + context.viewID;
80 }
81 
82 ccBBox ccBBox::CreateFromPoints(const std::vector<CCVector3>& points) {
83  ccBBox box;
84  if (points.empty()) {
85  box.minCorner() = CCVector3(0.0f, 0.0f, 0.0f);
86  box.maxCorner() = CCVector3(0.0f, 0.0f, 0.0f);
87  box.setValidity(false);
88  } else {
89  for (auto& pt : points) {
90  box.add(pt);
91  }
92  }
93  box.setValidity(box.GetMaxExtent() > 0);
94  return box;
95 }
96 
97 ccBBox ccBBox::CreateFromPoints(const std::vector<Eigen::Vector3d>& points) {
98  ccBBox box;
99  if (points.empty()) {
100  box.minCorner() = CCVector3(0.0f, 0.0f, 0.0f);
101  box.maxCorner() = CCVector3(0.0f, 0.0f, 0.0f);
102  } else {
103  box.minCorner() = std::accumulate(
104  points.begin(), points.end(), points[0],
105  [](const Eigen::Vector3d& a, const Eigen::Vector3d& b) {
106  return a.array().min(b.array()).matrix();
107  });
108  box.maxCorner() = std::accumulate(
109  points.begin(), points.end(), points[0],
110  [](const Eigen::Vector3d& a, const Eigen::Vector3d& b) {
111  return a.array().max(b.array()).matrix();
112  });
113  }
114 
115  box.setValidity(!box.IsEmpty());
116  return box;
117 }
118 
119 std::vector<Eigen::Vector3d> ccBBox::GetBoxPoints() const {
120  std::vector<Eigen::Vector3d> points(8);
121  Eigen::Vector3d extent = GetExtent();
122  Eigen::Vector3d min_bound = CCVector3d::fromArray(m_bbMin);
123  Eigen::Vector3d max_bound = CCVector3d::fromArray(m_bbMax);
124  points[0] = min_bound;
125  points[1] = min_bound + Eigen::Vector3d(extent(0), 0, 0);
126  points[2] = min_bound + Eigen::Vector3d(0, extent(1), 0);
127  points[3] = min_bound + Eigen::Vector3d(0, 0, extent(2));
128  points[4] = max_bound;
129  points[5] = max_bound - Eigen::Vector3d(extent(0), 0, 0);
130  points[6] = max_bound - Eigen::Vector3d(0, extent(1), 0);
131  points[7] = max_bound - Eigen::Vector3d(0, 0, extent(2));
132  return points;
133 }
134 
135 std::string ccBBox::GetPrintInfo() const {
136  return fmt::format("[({:.4f}, {:.4f}, {:.4f}) - ({:.4f}, {:.4f}, {:.4f})]",
137  m_bbMin(0), m_bbMin(1), m_bbMin(2), m_bbMax(0),
138  m_bbMax(1), m_bbMax(2));
139 }
140 
142  ccBBox rotatedBox;
143 
144  if (m_valid) {
145  rotatedBox.add(mat * m_bbMin);
146  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMin.y, m_bbMax.z));
147  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMax.y, m_bbMin.z));
148  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMin.y, m_bbMin.z));
149  rotatedBox.add(mat * m_bbMax);
150  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMax.y, m_bbMax.z));
151  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMax.y, m_bbMin.z));
152  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMin.y, m_bbMax.z));
153  }
154 
155  return rotatedBox;
156 }
157 
159  ccBBox rotatedBox;
160 
161  if (m_valid) {
162  rotatedBox.add(mat * m_bbMin);
163  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMin.y, m_bbMax.z));
164  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMax.y, m_bbMin.z));
165  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMin.y, m_bbMin.z));
166  rotatedBox.add(mat * m_bbMax);
167  rotatedBox.add(mat * CCVector3(m_bbMin.x, m_bbMax.y, m_bbMax.z));
168  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMax.y, m_bbMin.z));
169  rotatedBox.add(mat * CCVector3(m_bbMax.x, m_bbMin.y, m_bbMax.z));
170  }
171 
172  return rotatedBox;
173 }
174 
176  if (m_valid) {
177  m_bbMin += V;
178  m_bbMax += V;
179  }
180  return *this;
181 }
183  if (m_valid) {
184  m_bbMin -= V;
185  m_bbMax -= V;
186  }
187 
188  return *this;
189 }
190 const ccBBox& ccBBox::operator*=(float scaleFactor) {
191  if (m_valid) {
192  m_bbMin *= static_cast<PointCoordinateType>(scaleFactor);
193  m_bbMax *= static_cast<PointCoordinateType>(scaleFactor);
194  }
195 
196  return *this;
197 }
199  if (m_valid) {
200  CCVector3 boxCorners[8];
201 
202  boxCorners[0] = m_bbMin;
203  boxCorners[1] = CCVector3(m_bbMin.x, m_bbMin.y, m_bbMax.z);
204  boxCorners[2] = CCVector3(m_bbMin.x, m_bbMax.y, m_bbMin.z);
205  boxCorners[3] = CCVector3(m_bbMax.x, m_bbMin.y, m_bbMin.z);
206  boxCorners[4] = m_bbMax;
207  boxCorners[5] = CCVector3(m_bbMin.x, m_bbMax.y, m_bbMax.z);
208  boxCorners[6] = CCVector3(m_bbMax.x, m_bbMax.y, m_bbMin.z);
209  boxCorners[7] = CCVector3(m_bbMax.x, m_bbMin.y, m_bbMax.z);
210 
211  clear();
212 
213  for (int i = 0; i < 8; ++i) {
214  add(mat * boxCorners[i]);
215  }
216  }
217 
218  return *this;
219 }
220 
221 const ccBBox& ccBBox::operator+=(const Eigen::Vector3d& V) {
222  if (m_valid) {
223  m_bbMin += V;
224  m_bbMax += V;
225  }
226  return *this;
227 }
228 
229 const ccBBox& ccBBox::operator-=(const Eigen::Vector3d& V) {
230  if (m_valid) {
231  m_bbMin -= V;
232  m_bbMax -= V;
233  }
234 
235  return *this;
236 }
237 
238 const ccBBox& ccBBox::operator*=(double scaleFactor) {
239  if (m_valid) {
240  m_bbMin *= static_cast<PointCoordinateType>(scaleFactor);
241  m_bbMax *= static_cast<PointCoordinateType>(scaleFactor);
242  }
243 
244  return *this;
245 }
246 
247 const ccBBox& ccBBox::operator*=(const Eigen::Matrix3d& mat) {
248  if (m_valid) {
249  CCVector3 boxCorners[8];
250 
251  boxCorners[0] = m_bbMin;
252  boxCorners[1] = CCVector3(m_bbMin.x, m_bbMin.y, m_bbMax.z);
253  boxCorners[2] = CCVector3(m_bbMin.x, m_bbMax.y, m_bbMin.z);
254  boxCorners[3] = CCVector3(m_bbMax.x, m_bbMin.y, m_bbMin.z);
255  boxCorners[4] = m_bbMax;
256  boxCorners[5] = CCVector3(m_bbMin.x, m_bbMax.y, m_bbMax.z);
257  boxCorners[6] = CCVector3(m_bbMax.x, m_bbMax.y, m_bbMin.z);
258  boxCorners[7] = CCVector3(m_bbMax.x, m_bbMin.y, m_bbMax.z);
259 
260  clear();
261 
262  ccGLMatrix tempMat = ccGLMatrix(mat);
263 
264  for (int i = 0; i < 8; ++i) {
265  add(tempMat * boxCorners[i]);
266  }
267  }
268 
269  return *this;
270 }
Vector3Tpl< PointCoordinateType > CCVector3
Default 3D Vector.
Definition: CVGeom.h:798
float PointCoordinateType
Type of the coordinates of a (N-D) point.
Definition: CVTypes.h:16
filament::Texture::InternalFormat format
int points
static Vector3Tpl fromArray(const int a[3])
Constructor from an int array.
Definition: CVGeom.h:268
Bounding box structure.
Definition: ecvBBox.h:25
const ccBBox operator*(const ccGLMatrix &mat)
Applies transformation to the bounding box.
Definition: ecvBBox.cpp:141
void draw(CC_DRAW_CONTEXT &context) override
Draws entity and its children.
Definition: ecvBBox.cpp:68
PointCoordinateType GetMaxExtent() const
Definition: ecvBBox.h:156
virtual ccBBox & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &center) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ,...
Definition: ecvBBox.cpp:47
static ccBBox CreateFromPoints(const std::vector< CCVector3 > &points)
Definition: ecvBBox.cpp:82
const ccBBox & operator+=(const ccBBox &other)
Definition: ecvBBox.cpp:55
std::string GetPrintInfo() const
Returns the 3D dimensions of the bounding box in string format.
Definition: ecvBBox.cpp:135
const ccBBox & operator-=(const CCVector3 &V) override
Definition: ecvBBox.cpp:182
virtual ccBBox & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Definition: ecvBBox.cpp:29
virtual ccBBox & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: ecvBBox.cpp:22
virtual ccBBox & Scale(const double s, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
Definition: ecvBBox.cpp:41
std::vector< Eigen::Vector3d > GetBoxPoints() const
Returns the eight points that define the bounding box.
Definition: ecvBBox.cpp:119
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
Definition: ecvBBox.cpp:18
const ccBBox & operator*=(float scaleFactor) override
Scales the bounding box.
Definition: ecvBBox.cpp:190
virtual bool IsEmpty() const override
Definition: ecvBBox.h:72
Float version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:19
Double version of ccGLMatrixTpl.
Definition: ecvGLMatrix.h:56
const Vector3Tpl< T > & maxCorner() const
Returns max corner (const)
Definition: BoundingBox.h:156
void setValidity(bool state)
Sets bonding box validity.
Definition: BoundingBox.h:200
const Vector3Tpl< T > & minCorner() const
Returns min corner (const)
Definition: BoundingBox.h:154
void add(const Vector3Tpl< T > &P)
'Enlarges' the bounding box with a point
Definition: BoundingBox.h:131
RGB color structure.
Definition: ecvColorTypes.h:49
static QMainWindow * GetMainWindow()
static void DrawBBox(const CC_DRAW_CONTEXT &context, const ccBBox *bbox)
static ecvOrientedBBox CreateFromAxisAlignedBoundingBox(const ccBBox &aabox)
#define LogError(...)
Definition: Logging.h:60
a[190]
ImGuiContext * context
Definition: Window.cpp:76
Generic file read and write utility for python interface.
Display context.