-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathcudaPointCloud.cpp
387 lines (295 loc) · 7.88 KB
/
cudaPointCloud.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "cudaPointCloud.h"
#include "cudaMappedMemory.h"
#include "glUtility.h"
#include "glBuffer.h"
#include "glCamera.h"
#include "mat33.h"
#include "logging.h"
// constructor
cudaPointCloud::cudaPointCloud()
{
mPointsCPU = NULL;
mPointsGPU = NULL;
mBufferGL = NULL;
mCameraGL = NULL;
mDepthResize = NULL;
mDepthSize = 0;
mNumPoints = 0;
mMaxPoints = 0;
mHasRGB = false;
mHasNewPoints = false;
mHasCalibration = false;
}
// destructor
cudaPointCloud::~cudaPointCloud()
{
if( mDepthResize != NULL )
{
CUDA(cudaFree(mDepthResize));
mDepthResize = NULL;
}
if( mCameraGL != NULL )
{
delete mCameraGL;
mCameraGL = NULL;
}
Free();
}
// Free
void cudaPointCloud::Free()
{
if( mBufferGL != NULL )
{
delete mBufferGL;
mBufferGL = NULL;
}
if( mPointsCPU != NULL )
{
CUDA(cudaFreeHost(mPointsCPU));
mPointsCPU = NULL;
mPointsGPU = NULL;
mNumPoints = 0;
mMaxPoints = 0;
}
}
// Clear
void cudaPointCloud::Clear()
{
if( mPointsGPU != NULL )
CUDA(cudaMemset(mPointsGPU, 0, GetMaxSize()));
mNumPoints = 0;
}
// Create
cudaPointCloud* cudaPointCloud::Create()
{
return new cudaPointCloud();
}
// Reserve
bool cudaPointCloud::Reserve( uint32_t maxPoints )
{
if( maxPoints == 0 )
return false;
// check if enough memory is already allocated
if( maxPoints <= mMaxPoints && mPointsCPU != NULL )
return true;
// release old memory
Free();
// determine size of new memory
const size_t maxSize = maxPoints * sizeof(Vertex);
// allocate new memory
if( !cudaAllocMapped((void**)&mPointsCPU, (void**)&mPointsGPU, maxSize) )
{
LogError(LOG_CUDA "failed to allocate %zu bytes for point cloud\n", maxSize);
return false;
}
mMaxPoints = maxPoints;
return true;
}
// allocBufferGL
bool cudaPointCloud::allocBufferGL()
{
if( mBufferGL != NULL && mBufferGL->GetSize() == GetMaxSize() )
return true;
if( mBufferGL != NULL )
{
delete mBufferGL;
mBufferGL = NULL;
}
mBufferGL = glBuffer::Create(GL_VERTEX_BUFFER, GetMaxPoints(), sizeof(Vertex), NULL, GL_DYNAMIC_DRAW);
if( !mBufferGL )
return false;
return true;
}
// allocDepthResize
bool cudaPointCloud::allocDepthResize( size_t size )
{
if( size == 0 )
return false;
if( mDepthResize != NULL && mDepthSize == size )
return true;
if( mDepthResize != NULL )
{
CUDA(cudaFree(mDepthResize));
mDepthResize = NULL;
}
if( CUDA_FAILED(cudaMalloc(&mDepthResize, size)) )
return false;
mDepthSize = size;
return true;
}
// Render
bool cudaPointCloud::Render()
{
if( mNumPoints == 0 )
return false;
// make sure GL buffer is ready
if( !allocBufferGL() )
return false;
// make sure camera is ready
if( !mCameraGL )
{
mCameraGL = glCamera::Create(glCamera::YawPitchRoll);
if( !mCameraGL )
return false;
mCameraGL->SetEye(0.0f, 5.0f, 5.0f);
mCameraGL->StoreDefaults();
}
// copy to OpenGL if needed
if( mHasNewPoints )
{
void* ptr = mBufferGL->Map(GL_MAP_CUDA, GL_WRITE_DISCARD);
if( !ptr )
return false;
CUDA(cudaMemcpy(ptr, mPointsGPU, GetSize(), cudaMemcpyDeviceToDevice));
mBufferGL->Unmap();
mHasNewPoints = false;
}
// enable the camera and buffer
mCameraGL->Activate();
mBufferGL->Bind();
GL(glEnableClientState(GL_VERTEX_ARRAY));
GL(glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0));
GL(glEnableClientState(GL_COLOR_ARRAY));
GL(glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(Vertex), (void*)offsetof(Vertex, color)));
// draw the points
GL(glDrawArrays(GL_POINTS, 0, mNumPoints));
// disable the buffer and camera
GL(glDisableClientState(GL_COLOR_ARRAY));
GL(glDisableClientState(GL_VERTEX_ARRAY));
mBufferGL->Unbind();
mCameraGL->Deactivate();
return true;
}
// SetCalibration
void cudaPointCloud::SetCalibration( const float2& focalLength, const float2& principalPoint )
{
mFocalLength = focalLength;
mPrincipalPoint = principalPoint;
mHasCalibration = true;
}
// SetCalibration
void cudaPointCloud::SetCalibration( const float K[3][3] )
{
SetCalibration(make_float2(K[0][0], K[1][1]), make_float2(K[0][2], K[1][2]));
}
// SetCalibration
bool cudaPointCloud::SetCalibration( const char* filename )
{
if( !filename )
return false;
// open the camera calibration file
FILE* file = fopen(filename, "r");
if( !file )
{
LogError(LOG_CUDA "cudaPointCloud::Extract() -- failed to open calibration file %s\n", filename);
return false;
}
// parse the 3x3 calibration matrix
float K[3][3];
for( int n=0; n < 3; n++ )
{
char str[512];
if( !fgets(str, 512, file) )
{
LogError(LOG_CUDA "cudaPointCloud::Extract() -- failed to read line %i from calibration file %s\n", n+1, filename);
return false;
}
const int len = strlen(str);
if( len <= 0 )
{
LogError(LOG_CUDA "cudaPointCloud::Extract() -- invalid line %i from calibration file %s\n", n+1, filename);
return false;
}
if( str[len-1] == '\n' )
str[len-1] = 0;
if( sscanf(str, "%f %f %f", &K[n][0], &K[n][1], &K[n][2]) != 3 )
{
LogError(LOG_CUDA "cudaPointCloud::Extract() -- failed to parse line %i from calibration file %s\n", n+1, filename);
return false;
}
}
// close the file
fclose(file);
// dump the matrix
LogVerbose(LOG_CUDA "cudaPointCloud::Extract() -- loaded intrinsic camera calibration from %s\n", filename);
mat33_print(K, "K");
// save the calibration
SetCalibration(K);
return true;
}
// Save
bool cudaPointCloud::Save( const char* filename )
{
if( !filename || mNumPoints == 0 || !mPointsCPU )
return false;
// open the PCD file
FILE* file = fopen(filename, "w");
if( !file )
{
LogError(LOG_CUDA "cudaPointCloud::Save() -- failed to create %s\n", filename);
return false;
}
// write the PCD header
fprintf(file, "# .PCD v0.7 - Point Cloud Data file format\n");
fprintf(file, "VERSION 0.7\n");
if( mHasRGB )
{
fprintf(file, "FIELDS x y z rgb\n");
fprintf(file, "SIZE 4 4 4 4\n");
fprintf(file, "TYPE F F F U\n");
}
else
{
fprintf(file, "FIELDS x y z\n");
fprintf(file, "SIZE 4 4 4\n");
fprintf(file, "TYPE F F F\n");
}
fprintf(file, "COUNT 1 1 1 1\n");
fprintf(file, "WIDTH %u\n", mNumPoints);
fprintf(file, "HEIGHT 1\n");
fprintf(file, "VIEWPOINT 0 0 0 1 0 0 0\n");
fprintf(file, "POINTS %u\n", mNumPoints);
fprintf(file, "DATA ascii\n");
// wait for the GPU to finish any processing
CUDA(cudaDeviceSynchronize());
// write out points to the PCD file
for( size_t n=0; n < mNumPoints; n++ )
{
Vertex* point = GetData(n);
// output XYZ coordinates
fprintf(file, "%f %f %f", point->pos.x, point->pos.y, point->pos.z);
// output RGB color
if( mHasRGB )
{
// pack the color into 24 bits
const uint32_t rgb_packed = (uint32_t(point->color.x) << 16 |
uint32_t(point->color.y) << 8 |
uint32_t(point->color.z));
fprintf(file, " %u", rgb_packed);
}
fprintf(file, "\n");
}
fclose(file);
return true;
}