-
Notifications
You must be signed in to change notification settings - Fork 17
/
qpointcloudgeometry.cpp
303 lines (276 loc) · 10.7 KB
/
qpointcloudgeometry.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "qpointcloudgeometry.h"
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/qbufferdatagenerator.h>
#include <QHash>
#include <iomanip>
#include <pcl/PCLPointCloud2.h>
#include <QSharedPointer>
#include <pcl/point_types.h>
//QByteArray createPointcloudVertexData(pcl::PCLPointCloud2 *pointcloud)
//{
// if(pointcloud == NULL || pointcloud->data.size() == 0)
// return QByteArray();
// //QByteArray arr;
// //arr.append(reinterpret_cast<char*>(&pointcloud->data[0]), pointcloud->data.size());
// return QByteArray(reinterpret_cast<char*>(&pointcloud->data[0]), pointcloud->data.size());//arr;
//}
class PointcloudVertexDataGenerator : public Qt3DRender::QBufferDataGenerator /*Qt3DRender::QBufferFunctor*/
{
public:
PointcloudVertexDataGenerator(QPointcloud *pointcloud)
: m_bytes(pointcloud->data())
{
}
QByteArray operator ()() Q_DECL_OVERRIDE
{
return m_bytes;
}
bool operator ==(const Qt3DRender::QBufferDataGenerator &other) const Q_DECL_OVERRIDE
{
const PointcloudVertexDataGenerator *otherFunctor = Qt3DRender::functor_cast<PointcloudVertexDataGenerator>(&other);
if (otherFunctor != NULL)
return otherFunctor->m_bytes == m_bytes;
return false;
}
QT3D_FUNCTOR(PointcloudVertexDataGenerator)
private:
QByteArray m_bytes;
};
class PointcloudColorVertexDataGenerator : public Qt3DRender::QBufferDataGenerator /*Qt3DRender::QBufferFunctor*/
{
public:
PointcloudColorVertexDataGenerator(QPointcloud *pointcloud, bool isUintRgba, int offset)
: m_pointcloud(new QPointcloud(pointcloud))
, m_isUintRgba(isUintRgba)
, m_colorOffset(offset)
{
}
QByteArray operator ()() Q_DECL_OVERRIDE
{
int colorChannels = 3;
int size = m_pointcloud->width() * m_pointcloud->height();
int pointStep = m_pointcloud->point_step();
QByteArray data = m_pointcloud->data();
QByteArray colorData;
colorData.resize(sizeof(float)*size*colorChannels);
float *colorDataFloats = reinterpret_cast<float*>(colorData.data());
for(int i=m_colorOffset ; i<size ; i++) {
pcl::RGB rgb;
memcpy (&rgb, &(data.data()[m_colorOffset + i * pointStep]), sizeof (float));
colorDataFloats[(i*colorChannels) ] = rgb.r/255.f;
colorDataFloats[(i*colorChannels)+1] = rgb.g/255.f;
colorDataFloats[(i*colorChannels)+2] = rgb.b/255.f;
//colorDataFloats[(i*colorChannels+3)] = rgb.a/255.f;
}
return colorData;
}
bool operator ==(const Qt3DRender::QBufferDataGenerator &other) const Q_DECL_OVERRIDE
{
const PointcloudColorVertexDataGenerator *otherFunctor = Qt3DRender::functor_cast<PointcloudColorVertexDataGenerator>(&other);
if (otherFunctor != NULL)
return otherFunctor->m_pointcloud == m_pointcloud;
return false;
}
QT3D_FUNCTOR(PointcloudVertexDataGenerator)
private:
QSharedPointer<QPointcloud> m_pointcloud;
bool m_isUintRgba;
int m_colorOffset;
};
class QPointcloudGeometryPrivate
{
public:
QPointcloudGeometryPrivate()
:m_vertexBuffer(NULL)
,m_colorBuffer(NULL)
,m_pointcloud(NULL)
,m_colorOffset(0)
,m_colorFormatUintRgba(false)
,m_colorAvailable(false)
{}
Qt3DRender::QBuffer *m_vertexBuffer;
Qt3DRender::QBuffer *m_colorBuffer;
QPointcloud *m_pointcloud;
int m_colorOffset;
bool m_colorFormatUintRgba;
bool m_colorAvailable;
};
QPointcloudGeometry::QPointcloudGeometry(Qt3DCore::QNode *parent)
:m_p(new QPointcloudGeometryPrivate)
{
m_p->m_vertexBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, this);
m_p->m_colorBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, this);
}
QPointcloudGeometry::~QPointcloudGeometry()
{
delete m_p;
}
Qt3DRender::QAttribute::VertexBaseType pclTypeToAttributeType(const QPointfield::PointFieldTypes &inp)
{
switch(inp)
{
case QPointfield::INT8:
return Qt3DRender::QAttribute::Byte;
case QPointfield::INT16:
return Qt3DRender::QAttribute::Short;
case QPointfield::INT32:
return Qt3DRender::QAttribute::Int;
case QPointfield::UINT8:
return Qt3DRender::QAttribute::UnsignedByte;
case QPointfield::UINT16:
return Qt3DRender::QAttribute::UnsignedShort;
case QPointfield::UINT32:
return Qt3DRender::QAttribute::UnsignedInt;
case QPointfield::FLOAT32:
return Qt3DRender::QAttribute::Float;
case QPointfield::FLOAT64:
return Qt3DRender::QAttribute::Double;
default:
Q_ASSERT(false);
return Qt3DRender::QAttribute::Float;
}
}
void QPointcloudGeometry::updateVertices()
{
if(
#if WITH_PCL
( m_p->m_pointcloud == NULL
|| m_p->m_pointcloud->pointcloud() == NULL
|| m_p->m_pointcloud->pointcloud()->data.size() == 0)
&&
#endif
( m_p->m_pointcloud->data().length() == 0))
return;
// if(m_p->m_vertexBuffer)
// {
// m_p->m_vertexBuffer->deleteLater();
// }
//m_p->m_vertexBuffer->setBufferFunctor(Qt3DRender::QBufferFunctorPtr(new PointcloudVertexDataFunctor(m_p->m_pointcloud->pointcloud())));
updateAttributes();
//QMetaObject::invokeMethod(this, "updateAttributes", Qt::QueuedConnection);
m_p->m_vertexBuffer->setDataGenerator(Qt3DRender::QBufferDataGeneratorPtr(new PointcloudVertexDataGenerator(this->m_p->m_pointcloud)));
if(m_p->m_colorAvailable) {
m_p->m_colorBuffer->setDataGenerator(Qt3DRender::QBufferDataGeneratorPtr(new PointcloudColorVertexDataGenerator(this->m_p->m_pointcloud, m_p->m_colorFormatUintRgba, m_p->m_colorOffset)));
}
}
QPointcloud *QPointcloudGeometry::pointcloud() const
{
return m_p->m_pointcloud;
}
void QPointcloudGeometry::updateAttributes()
{
// completely rebuild attribute list and remove all previous attributes
QVector<Qt3DRender::QAttribute *> atts = attributes();
Q_FOREACH(Qt3DRender::QAttribute *attr, atts)
{
if(attr->attributeType() == Qt3DRender::QAttribute::VertexAttribute)
{
removeAttribute(attr);
attr->deleteLater();
}
else
{
qDebug() << "skipped index";
}
}
// Prepare hash table to query attribute names easily
QHash<QString, QPointfield* > pfs;
m_p->m_pointcloud->updateAttributes();
const QList<QPointfield *> &fieldList = m_p->m_pointcloud->getFields();
for(auto fieldIter = fieldList.cbegin(); fieldIter != fieldList.cend() ; ++fieldIter)
{
pfs.insert((*fieldIter)->name(), *fieldIter);
}
m_p->m_colorAvailable = false;
// parse point fields and make reasonable attributes out of them
QHash<QString, QPointfield* >::const_iterator pf(pfs.find("x"));
if(pf != pfs.cend())
{
int num = 1 + (pfs.contains("y")?1:0) + (pfs.contains("z")?1:0) + (pfs.contains("w")?1:0);
Qt3DRender::QAttribute* attrib = new Qt3DRender::QAttribute(nullptr);
attrib->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
attrib->setDataType(pclTypeToAttributeType((*pf)->datatype()));
attrib->setDataSize(num);
attrib->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
attrib->setBuffer(m_p->m_vertexBuffer);
attrib->setByteStride(m_p->m_pointcloud->point_step());
attrib->setByteOffset((*pf)->offset());
attrib->setCount(m_p->m_pointcloud->width() * m_p->m_pointcloud->height());
addAttribute(attrib);
setBoundingVolumePositionAttribute(attrib);
}
pf = pfs.find("rgb");
if(pf != pfs.cend())
{
int num = 3;
Qt3DRender::QAttribute* attrib = new Qt3DRender::QAttribute(nullptr);
attrib->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
attrib->setDataType(Qt3DRender::QAttribute::Float);
attrib->setDataSize(num);
attrib->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
attrib->setBuffer(m_p->m_colorBuffer);
attrib->setByteStride(num * sizeof(float));
attrib->setByteOffset(0);
attrib->setCount(m_p->m_pointcloud->width() * m_p->m_pointcloud->height());
addAttribute(attrib);
m_p->m_colorOffset = (*pf)->offset();
m_p->m_colorFormatUintRgba = false;
m_p->m_colorAvailable = true;
}
pf = pfs.find("rgba");
if(pf != pfs.cend())
{
int num = 3;
Qt3DRender::QAttribute* attrib = new Qt3DRender::QAttribute(nullptr);
attrib->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
attrib->setDataType(Qt3DRender::QAttribute::Float);
attrib->setDataSize(num);
attrib->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
attrib->setBuffer(m_p->m_colorBuffer);
attrib->setByteStride(num * sizeof(float));
attrib->setByteOffset(0);
attrib->setCount(m_p->m_pointcloud->width() * m_p->m_pointcloud->height());
addAttribute(attrib);
m_p->m_colorOffset = (*pf)->offset();
m_p->m_colorFormatUintRgba = true;
m_p->m_colorAvailable = true;
}
pf = pfs.find("normal_x");
if(pf != pfs.cend())
{
int num = 1 + (pfs.contains("normal_y")?1:0) + (pfs.contains("normal_z")?1:0) + (pfs.contains("curvature")?1:0);
Qt3DRender::QAttribute* attrib = new Qt3DRender::QAttribute(nullptr);
attrib->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
attrib->setDataType(pclTypeToAttributeType((*pf)->datatype()));
attrib->setDataSize(num);
attrib->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
attrib->setBuffer(m_p->m_vertexBuffer);
attrib->setByteStride(m_p->m_pointcloud->point_step());
attrib->setByteOffset((*pf)->offset());
attrib->setCount(m_p->m_pointcloud->width() * m_p->m_pointcloud->height());
addAttribute(attrib);
}
pf = pfs.find("intensity");
if(pf != pfs.cend())
{
Qt3DRender::QAttribute* attrib = new Qt3DRender::QAttribute(nullptr);
attrib->setName("intensity");
attrib->setDataType(pclTypeToAttributeType((*pf)->datatype()));
attrib->setDataSize(1);
attrib->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
attrib->setBuffer(m_p->m_vertexBuffer);
attrib->setByteStride(m_p->m_pointcloud->point_step());
attrib->setByteOffset((*pf)->offset());
attrib->setCount(m_p->m_pointcloud->width() * m_p->m_pointcloud->height());
addAttribute(attrib);
}
}
void QPointcloudGeometry::setPointcloud(QPointcloud *pointcloud)
{
if (m_p->m_pointcloud == pointcloud)
return;
m_p->m_pointcloud = pointcloud;
updateVertices();
Q_EMIT pointcloudChanged(pointcloud);
}