15
15
#include " core/util.h"
16
16
#include " ply.h"
17
17
18
+ struct PointData
19
+ {
20
+ PointData () noexcept {}
21
+ float position[4 ];
22
+ float color[4 ];
23
+ };
24
+
18
25
PointCloud::PointCloud (bool useLinearColorsIn) :
26
+ numPoints(0 ),
27
+ pointSize(0 ),
19
28
useLinearColors(useLinearColorsIn)
20
29
{
21
30
;
@@ -59,55 +68,62 @@ bool PointCloud::ImportPly(const std::string& plyFilename)
59
68
Log::E (" Error parsing ply file \" %s\" , missing color property\n " , plyFilename.c_str ());
60
69
}
61
70
62
- pointDataVec.resize (ply.GetVertexCount ());
71
+ numPoints = ply.GetVertexCount ();
72
+ pointSize = sizeof (PointData);
73
+ PointData* pointDataPtr = new PointData[numPoints];
74
+ data.reset (pointDataPtr);
75
+
76
+ // GL_FLOAT = 0x1406
77
+ positionAttrib = {4 , 0x1406 , (int )pointSize, offsetof (PointData, position)};
78
+ colorAttrib = {4 , 0x1406 , (int )pointSize, offsetof (PointData, color)};
63
79
64
80
if (useDoubles)
65
81
{
66
82
int i = 0 ;
67
- ply.ForEachVertex ([this , &i, &props](const uint8_t * data, size_t size)
83
+ ply.ForEachVertex ([this , pointDataPtr, &i, &props](const uint8_t * data, size_t size)
68
84
{
69
85
if (useLinearColors)
70
86
{
71
- pointDataVec [i].position [0 ] = SRGBToLinear ((float )props.x .Get <double >(data));
72
- pointDataVec [i].position [1 ] = SRGBToLinear ((float )props.y .Get <double >(data));
73
- pointDataVec [i].position [2 ] = SRGBToLinear ((float )props.z .Get <double >(data));
87
+ pointDataPtr [i].position [0 ] = SRGBToLinear ((float )props.x .Get <double >(data));
88
+ pointDataPtr [i].position [1 ] = SRGBToLinear ((float )props.y .Get <double >(data));
89
+ pointDataPtr [i].position [2 ] = SRGBToLinear ((float )props.z .Get <double >(data));
74
90
}
75
91
else
76
92
{
77
- pointDataVec [i].position [0 ] = (float )props.x .Get <double >(data);
78
- pointDataVec [i].position [1 ] = (float )props.y .Get <double >(data);
79
- pointDataVec [i].position [2 ] = (float )props.z .Get <double >(data);
93
+ pointDataPtr [i].position [0 ] = (float )props.x .Get <double >(data);
94
+ pointDataPtr [i].position [1 ] = (float )props.y .Get <double >(data);
95
+ pointDataPtr [i].position [2 ] = (float )props.z .Get <double >(data);
80
96
}
81
- pointDataVec [i].position [3 ] = 1 .0f ;
82
- pointDataVec [i].color [0 ] = (float )props.red .Get <uint8_t >(data) / 255 .0f ;
83
- pointDataVec [i].color [1 ] = (float )props.green .Get <uint8_t >(data) / 255 .0f ;
84
- pointDataVec [i].color [2 ] = (float )props.blue .Get <uint8_t >(data) / 255 .0f ;
85
- pointDataVec [i].color [3 ] = 1 .0f ;
97
+ pointDataPtr [i].position [3 ] = 1 .0f ;
98
+ pointDataPtr [i].color [0 ] = (float )props.red .Get <uint8_t >(data) / 255 .0f ;
99
+ pointDataPtr [i].color [1 ] = (float )props.green .Get <uint8_t >(data) / 255 .0f ;
100
+ pointDataPtr [i].color [2 ] = (float )props.blue .Get <uint8_t >(data) / 255 .0f ;
101
+ pointDataPtr [i].color [3 ] = 1 .0f ;
86
102
i++;
87
103
});
88
104
}
89
105
else
90
106
{
91
107
int i = 0 ;
92
- ply.ForEachVertex ([this , &i, &props](const uint8_t * data, size_t size)
108
+ ply.ForEachVertex ([this , pointDataPtr, &i, &props](const uint8_t * data, size_t size)
93
109
{
94
110
if (useLinearColors)
95
111
{
96
- pointDataVec [i].position [0 ] = SRGBToLinear (props.x .Get <float >(data));
97
- pointDataVec [i].position [1 ] = SRGBToLinear (props.y .Get <float >(data));
98
- pointDataVec [i].position [2 ] = SRGBToLinear (props.z .Get <float >(data));
112
+ pointDataPtr [i].position [0 ] = SRGBToLinear (props.x .Get <float >(data));
113
+ pointDataPtr [i].position [1 ] = SRGBToLinear (props.y .Get <float >(data));
114
+ pointDataPtr [i].position [2 ] = SRGBToLinear (props.z .Get <float >(data));
99
115
}
100
116
else
101
117
{
102
- pointDataVec [i].position [0 ] = props.x .Get <float >(data);
103
- pointDataVec [i].position [1 ] = props.y .Get <float >(data);
104
- pointDataVec [i].position [2 ] = props.z .Get <float >(data);
118
+ pointDataPtr [i].position [0 ] = props.x .Get <float >(data);
119
+ pointDataPtr [i].position [1 ] = props.y .Get <float >(data);
120
+ pointDataPtr [i].position [2 ] = props.z .Get <float >(data);
105
121
}
106
- pointDataVec [i].position [3 ] = 1 .0f ;
107
- pointDataVec [i].color [0 ] = (float )props.red .Get <uint8_t >(data) / 255 .0f ;
108
- pointDataVec [i].color [1 ] = (float )props.green .Get <uint8_t >(data) / 255 .0f ;
109
- pointDataVec [i].color [2 ] = (float )props.blue .Get <uint8_t >(data) / 255 .0f ;
110
- pointDataVec [i].color [2 ] = 1 .0f ;
122
+ pointDataPtr [i].position [3 ] = 1 .0f ;
123
+ pointDataPtr [i].color [0 ] = (float )props.red .Get <uint8_t >(data) / 255 .0f ;
124
+ pointDataPtr [i].color [1 ] = (float )props.green .Get <uint8_t >(data) / 255 .0f ;
125
+ pointDataPtr [i].color [2 ] = (float )props.blue .Get <uint8_t >(data) / 255 .0f ;
126
+ pointDataPtr [i].color [3 ] = 1 .0f ;
111
127
i++;
112
128
});
113
129
}
@@ -130,7 +146,7 @@ bool PointCloud::ExportPly(const std::string& plyFilename) const
130
146
// ply files have unix line endings.
131
147
plyFile << " ply\n " ;
132
148
plyFile << " format binary_little_endian 1.0\n " ;
133
- plyFile << " element vertex " << pointDataVec. size () << " \n " ;
149
+ plyFile << " element vertex " << numPoints << " \n " ;
134
150
plyFile << " property float x\n " ;
135
151
plyFile << " property float y\n " ;
136
152
plyFile << " property float z\n " ;
@@ -155,19 +171,22 @@ bool PointCloud::ExportPly(const std::string& plyFilename) const
155
171
156
172
void PointCloud::InitDebugCloud ()
157
173
{
158
- pointDataVec.clear ();
174
+ const int NUM_POINTS = 5 ;
175
+
176
+ numPoints = NUM_POINTS * 3 ;
177
+ pointSize = sizeof (PointData);
178
+ PointData* pointDataPtr = new PointData[numPoints];
179
+ data.reset (pointDataPtr);
159
180
160
181
//
161
182
// make an debug pointVec, that contains three lines one for each axis.
162
183
//
163
184
const float AXIS_LENGTH = 1 .0f ;
164
- const int NUM_POINTS = 5 ;
165
185
const float DELTA = (AXIS_LENGTH / (float )NUM_POINTS);
166
- pointDataVec.resize (NUM_POINTS * 3 );
167
186
// x axis
168
187
for (int i = 0 ; i < NUM_POINTS; i++)
169
188
{
170
- PointCloud:: PointData& p = pointDataVec [i];
189
+ PointData& p = pointDataPtr [i];
171
190
p.position [0 ] = i * DELTA;
172
191
p.position [1 ] = 0 .0f ;
173
192
p.position [2 ] = 0 .0f ;
@@ -180,7 +199,7 @@ void PointCloud::InitDebugCloud()
180
199
// y axis
181
200
for (int i = 0 ; i < NUM_POINTS; i++)
182
201
{
183
- PointCloud:: PointData& p = pointDataVec [i + NUM_POINTS];
202
+ PointData& p = pointDataPtr [i + NUM_POINTS];
184
203
p.position [0 ] = 0 .0f ;
185
204
p.position [1 ] = i * DELTA;
186
205
p.position [2 ] = 0 .0f ;
@@ -193,14 +212,25 @@ void PointCloud::InitDebugCloud()
193
212
// z axis
194
213
for (int i = 0 ; i < NUM_POINTS; i++)
195
214
{
196
- PointCloud:: PointData& p = pointDataVec [i + 2 * NUM_POINTS];
215
+ PointData& p = pointDataPtr [i + 2 * NUM_POINTS];
197
216
p.position [0 ] = 0 .0f ;
198
217
p.position [1 ] = 0 .0f ;
199
218
p.position [2 ] = i * DELTA;
200
219
p.position [3 ] = 1 .0f ;
201
- p.color [0 ] = 0 ;
202
- p.color [1 ] = 0 ;
220
+ p.color [0 ] = 0 . 0f ;
221
+ p.color [1 ] = 0 . 0f ;
203
222
p.color [2 ] = 1 .0f ;
204
223
p.color [3 ] = 0 .0f ;
205
224
}
206
225
}
226
+
227
+ void PointCloud::ForEachAttrib (const AttribData& attribData, const AttribCallback& cb) const
228
+ {
229
+ const uint8_t * bytePtr = (uint8_t *)data.get ();
230
+ bytePtr += attribData.offset ;
231
+ for (size_t i = 0 ; i < GetNumPoints (); i++)
232
+ {
233
+ cb ((const void *)bytePtr);
234
+ bytePtr += attribData.stride ;
235
+ }
236
+ }
0 commit comments