-
Notifications
You must be signed in to change notification settings - Fork 8
/
BoundBox.cpp
145 lines (111 loc) · 3.39 KB
/
BoundBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//-----------------------------------------------------------
// Copyright (C) 2019 Piotr (Peter) Beben <[email protected]>
// See LICENSE included with this distribution.
#include "BoundBox.h"
#include "Cloud.h"
#include "MessageLogger.h"
#include "constants.h"
#include <qopengl.h>
#include <Eigen/Dense>
#include <array>
using Vector3f = Eigen::Vector3f;
constexpr const std::array<GLuint, 24> BoundBox::m_elemGL;
//---------------------------------------------------------
BoundBox::BoundBox(const float minBBox[3], const float maxBBox[3],
MessageLogger* msgLogger)
{
m_msgLogger = msgLogger;
set(minBBox, maxBBox);
}
//---------------------------------------------------------
BoundBox::BoundBox(const Cloud& cloud, MessageLogger* msgLogger)
{
m_msgLogger = msgLogger;
set(cloud);
}
//---------------------------------------------------------
void BoundBox::set(const Cloud& cloud)
{
float minBBox[3] = {float_infinity, float_infinity, float_infinity};
float maxBBox[3] = {-float_infinity, -float_infinity, -float_infinity};
size_t numPoints = cloud.pointCount();
for(size_t i = 0; i < numPoints; ++i){
Vector3f v = cloud.point(i);
for(int j=0; j<3; ++j) {
minBBox[j] = std::min(v[j], minBBox[j]);
maxBBox[j] = std::max(v[j], maxBBox[j]);
}
}
set(minBBox, maxBBox);
}
//---------------------------------------------------------
void BoundBox::set(const float minBBox[3], const float maxBBox[3])
{
for(int i=0; i<3; ++i) {
m_minBBox[i] = minBBox[i];
m_maxBBox[i] = maxBBox[i];
}
m_vertCount = 8;
if( m_cloud != nullptr ) m_cloud->invalidateCT();
}
//---------------------------------------------------------
void BoundBox::pad(float padX, float padY, float padZ)
{
float padding[3] = {padX, padY, padZ};
for(int j=0; j<3; ++j) {
m_minBBox[j] -= padding[j];
m_maxBBox[j] += padding[j];
}
if( m_cloud != nullptr ) m_cloud->invalidateCT();
}
//---------------------------------------------------------
void BoundBox::rescale(float frac)
{
for(int j=0; j<3; ++j) {
float padding = frac*(m_maxBBox[j] - m_minBBox[j]);
m_minBBox[j] -= padding;
m_maxBBox[j] += padding;
}
if( m_cloud != nullptr ) m_cloud->invalidateCT();
}
//---------------------------------------------------------
void BoundBox::setParentCloud(Cloud *cloud)
{
m_cloud = cloud;
if( m_cloud != nullptr ) m_cloud->invalidateCT();
}
//---------------------------------------------------------
const GLfloat *BoundBox::vertGLData()
{
for(int i = 0, j = 0; i<m_vertCount; ++i){
for(int k = 0; k < 3; ++k){
m_vertGL[j+k] =
(((i>>k)&1) == 0) ? m_minBBox[k] : m_maxBBox[k];
}
m_vertGL[j+3] = 1.0f;
m_vertGL[j+4] = 0.0f;
m_vertGL[j+5] = 0.0f;
j += 6;
}
return static_cast<const GLfloat*>(m_vertGL.data());
}
//---------------------------------------------------------
void BoundBox::logMessageBBox() const
{
if(m_msgLogger != nullptr) {
m_msgLogger->logMessage(
QString("Bounding box minimum extent:\n") +
QString("(") +
QString::number(m_minBBox[0]) +
QString(", ") + QString::number(m_minBBox[1]) +
QString(", ") + QString::number(m_minBBox[2]) +
QString(")\n") +
QString("Bounding box maximum extent:\n") +
QString("(") +
QString::number(m_maxBBox[0]) +
QString(", ") + QString::number(m_maxBBox[1]) +
QString(", ") + QString::number(m_maxBBox[2]) +
QString(")\n"));
}
}
//---------------------------------------------------------