-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointCloud.cpp
156 lines (122 loc) · 2.89 KB
/
PointCloud.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
146
147
148
149
150
151
152
153
154
155
156
#include "PointCloud.h"
#include <QDebug>
PointCloud::PointCloud() : m_needsExtents(false)
{
}
PointCloud::PointCloud(const QVector<QVector3D> &points,
const QVector<QColor> &colors) : m_points(points),
m_colors(colors),
m_needsExtents(true)
{
}
PointCloud::~PointCloud()
{
}
const QVector3D & PointCloud::point(int index) const
{
return m_points.at(index);
}
void PointCloud::setPoint(int index, const QVector3D &point)
{
m_points.replace(index, point);
m_needsExtents = true;
}
int PointCloud::count() const
{
return m_points.count();
}
bool PointCloud::hasColor() const
{
return !m_colors.isEmpty();
}
const QVector3D & PointCloud::boundingBoxMinimum() const
{
if(m_needsExtents) calculateExtents();
return m_min;
}
const QVector3D & PointCloud::boundingBoxMaximum() const
{
if(m_needsExtents) calculateExtents();
return m_max;
}
const QVector3D & PointCloud::boundingBoxCenter() const
{
if(m_needsExtents) calculateExtents();
return m_center;
}
QVector<float> PointCloud::pointData() const
{
QVector<float> interleaved;
foreach(QVector3D point, m_points)
{
interleaved.push_back(point.x());
interleaved.push_back(point.y());
interleaved.push_back(point.z());
}
return interleaved;
}
QVector<unsigned char> PointCloud::colorData() const
{
QVector<unsigned char> result;
foreach(QColor c, m_colors)
{
result.push_back(c.red());
result.push_back(c.green());
result.push_back(c.blue());
}
return result;
}
QVector<float> PointCloud::colorDataF() const
{
QVector<float> result;
foreach(QColor c, m_colors)
{
result.push_back(c.redF());
result.push_back(c.greenF());
result.push_back(c.blueF());
}
return result;
}
void PointCloud::shuffle()
{
for(int i = m_points.count() - 1; i > 0; i--)
{
// Get random index between 0 and i
int j = (double)qrand()/RAND_MAX * i;
QVector3D tmp = m_points.at(i);
m_points.replace(i, m_points.at(j));
m_points.replace(j, tmp);
if(hasColor())
{
QColor tmpColor = m_colors.at(i);
m_colors.replace(i, m_colors.at(j));
m_colors.replace(j, tmpColor);
}
}
}
PointCloud PointCloud::shuffled() const
{
PointCloud result(*this);
result.shuffle();
return result;
}
void PointCloud::calculateExtents() const
{
// Initialize min and max
m_min = m_points.first();
m_max = m_points.first();
// Find min and max of all points
foreach(QVector3D point, m_points)
{
if(point.x() < m_min.x()) m_min.setX(point.x());
else if(point.x() > m_max.x()) m_max.setX(point.x());
if(point.y() < m_min.y()) m_min.setY(point.y());
else if(point.y() > m_max.y()) m_max.setY(point.y());
if(point.z() < m_min.z()) m_min.setZ(point.z());
else if(point.z() > m_max.z()) m_max.setZ(point.z());
}
// Calculate Center
m_center = (m_max - m_min)/2.0;
// Mark extents as calculated
m_needsExtents = false;
}