-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathpoisson_sampling.cu
274 lines (247 loc) · 11.9 KB
/
poisson_sampling.cu
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
/////////////////////////////////////////////////////////////////////////////
/// \file poisson_sampling.cu
///
/// \brief Cuda implementation of the operations to perform a poisson disk
/// sampling on a batch of point clouds (O(n)), to obtain the
/// associated features to the selected points, and to propagate the
/// feature gradients.
///
/// \copyright Copyright (c) 2018 Visual Computing group of Ulm University,
/// Germany. See the LICENSE file at the top-level directory of
/// this distribution.
///
/// \author pedro hermosilla ([email protected])
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <float.h>
#include "cuda_kernel_utils.h"
#define BLOCK_SIZE 4
#define PT_BLOCK_SIZE 128
////////////////////////////////////////////////////////////////////////////////// GPU
__constant__ int cellOffsetsPool[27][3];
/**
* Method to select a set of points from a point cloud in which all of them are at
* distance [pRadius*0.5, pRadius].
* @param scaleInv Scale invariant.
* @param pCurrBatch Current batch processed.
* @param pCurrentCell Integer with the current cell of the block.
* @param pNumPoints Number of points.
* @param pBatchSize Size of the batch.
* @param pNumCells Number of cells of the grid.
* @param pRadius Radius of the possion disk.
* @param pAABBMinPoint Minimum point of the grid (3 componenets).
* @param pAABBMaxPoint Maximum point of the grid (3 componenets).
* @param pPoints List of points.
* @param pBatchIds List of the batch identifies.
* @param pPDFs List of pdfs of each point.
* @param pCellIndexs Indexs of the grid cells.
* @param pAuxBoleanBuffer Input/Output parameter with the list of booleans indicating
* if a point was selected.
* @param pOutSampledPoints Output parameter with the list of sampled points.
* @param pOutSampleBatchIds Output parameter with the list of sampled batch ids.
* @param pOutSampleIndexs Output parameter with the list of indexs of the sampled points.
* @param pOutNumSelectedPoints Output parameter with the number of selected points.
*/
__global__ void selectSamples(
const bool scaleInv,
const int pCurrBatch,
const int pCurrentCell,
const int pNumPoints,
const int pBatchSize,
const int pNumCells,
const float pRadius,
const float* __restrict__ pAABBMinPoint,
const float* __restrict__ pAABBMaxPoint,
const float* __restrict__ pPoints,
const int* __restrict__ pBatchIds,
const int* __restrict__ pCellIndexs,
bool* __restrict__ pAuxBooleanBuffer,
float* __restrict__ pOutSampledPoints,
int* __restrict__ pOutSampleBatchIds,
int* __restrict__ pOutSampleIndexs,
int* __restrict__ pOutNumSelectedPoints)
{
int xCell = (threadIdx.x + blockIdx.x * blockDim.x)*3 + 1 + cellOffsetsPool[pCurrentCell][0];
int yCell = (threadIdx.y + blockIdx.y * blockDim.y)*3 + 1 + cellOffsetsPool[pCurrentCell][1];
int zCell = (threadIdx.z + blockIdx.z * blockDim.z)*3 + 1 + cellOffsetsPool[pCurrentCell][2];
if(xCell < pNumCells && yCell < pNumCells & zCell < pNumCells){
float maxAabbSize = max(max(
pAABBMaxPoint[pCurrBatch*3] - pAABBMinPoint[pCurrBatch*3],
pAABBMaxPoint[pCurrBatch*3 + 1] - pAABBMinPoint[pCurrBatch*3 + 1]),
pAABBMaxPoint[pCurrBatch*3 + 2] - pAABBMinPoint[pCurrBatch*3 + 2]);
float radius = (scaleInv)?pRadius*maxAabbSize:pRadius;
int cellIndex = pCurrBatch*pNumCells*pNumCells*pNumCells + xCell*pNumCells*pNumCells + yCell*pNumCells + zCell;
int initPoint = pCellIndexs[cellIndex*2];
int endPoint = pCellIndexs[cellIndex*2 +1];
for(int i = initPoint; i < endPoint; ++i)
{
float centralCoords[3] = {pPoints[i*3], pPoints[i*3+1], pPoints[i*3+2]};
bool collision = false;
for(int neighIter = 0; (neighIter < 27) && !collision; ++neighIter)
{
int currCellIndex[3] = {xCell+cellOffsetsPool[neighIter][0], yCell+cellOffsetsPool[neighIter][1], zCell+cellOffsetsPool[neighIter][2]};
if(currCellIndex[0] >= 0 && currCellIndex[0] < pNumCells &&
currCellIndex[1] >= 0 && currCellIndex[1] < pNumCells &&
currCellIndex[2] >= 0 && currCellIndex[2] < pNumCells)
{
int cellIndexFlat = pCurrBatch*pNumCells*pNumCells*pNumCells + currCellIndex[0]*pNumCells*pNumCells + currCellIndex[1]*pNumCells + currCellIndex[2];
int initNeighIndex = pCellIndexs[cellIndexFlat*2];
int endNeighIndex = pCellIndexs[cellIndexFlat*2 + 1];
for(int j = initNeighIndex; (j < endNeighIndex) && !collision; ++j)
{
int currPointIndex = j * 3;
float currentCoords[3] = {pPoints[currPointIndex], pPoints[currPointIndex+1], pPoints[currPointIndex+2]};
float diffVector[3] = {currentCoords[0] - centralCoords[0], currentCoords[1] - centralCoords[1], currentCoords[2] - centralCoords[2]};
float pointDist = sqrt(diffVector[0]*diffVector[0] + diffVector[1]*diffVector[1] + diffVector[2]*diffVector[2]);
if(pointDist < radius && pAuxBooleanBuffer[j]){
collision = true;
}
}
}
}
if(!collision){
pAuxBooleanBuffer[i] = true;
int finalPointIndex = atomicAdd(&pOutNumSelectedPoints[0], 1);
pOutSampledPoints[finalPointIndex*3] = centralCoords[0];
pOutSampledPoints[finalPointIndex*3+1] = centralCoords[1];
pOutSampledPoints[finalPointIndex*3+2] = centralCoords[2];
pOutSampleBatchIds[finalPointIndex] = pCurrBatch;
pOutSampleIndexs[finalPointIndex] = i;
}
}
}
}
/**
* Method to get the features of the sampled points.
* @param pNumSamples Number of samples.
* @param pNumFeatures Number of features.
* @param pSampledIndexs List of indexs of the sampled points.
* @param pFeatures List of input features.
* @param pOutSampledFeatures List of output sampled features.
*/
__global__ void selectFeatureSamples(
const int pNumSamples,
const int pNumFeatures,
const int* __restrict__ pSampledIndexs,
const float* __restrict__ pFeatures,
float* __restrict__ pOutSampledFeatures)
{
int currentIndex = threadIdx.x + blockIdx.x * blockDim.x;
int sampleIndex = currentIndex/pNumFeatures;
int featureIndex = currentIndex%pNumFeatures;
if(sampleIndex < pNumSamples){
pOutSampledFeatures[currentIndex] = pFeatures[pSampledIndexs[sampleIndex]*pNumFeatures + featureIndex];
}
}
/**
* Method to get the gradients of the features of the sampled points.
* @param pNumSamples Number of samples.
* @param pNumFeatures Number of features.
* @param pSampledIndexs List of indexs of the sampled points.
* @param pFeaturesGrads List of gradients of output features.
* @param pOutSampledFeaturesGrads List of output gradients of input features.
*/
__global__ void selectFeatureSamplesGrad(
const int pNumSamples,
const int pNumFeatures,
const int* __restrict__ pSampledIndexs,
const float* __restrict__ pFeaturesGrads,
float* __restrict__ pOutSampledFeaturesGrads)
{
int currentIndex = threadIdx.x + blockIdx.x * blockDim.x;
int sampleIndex = currentIndex/pNumFeatures;
int featureIndex = currentIndex%pNumFeatures;
if(sampleIndex < pNumSamples){
pOutSampledFeaturesGrads[pSampledIndexs[sampleIndex]*pNumFeatures + featureIndex] = pFeaturesGrads[currentIndex];
}
}
////////////////////////////////////////////////////////////////////////////////// CPU
int samplePointCloud(
const bool scaleInv,
const float pRadius,
const int pNumPoints,
const int pBatchSize,
const int pNumCells,
const float* pAABBMin,
const float* pAABBMax,
const float* pPoints,
const int* pBatchIds,
const int* pCellIndexs,
float* pSelectedPts,
int* pSelectedBatchIds,
int* pSelectedIndexs,
bool* pAuxBoolBuffer)
{
//Init device symbols.
int cellOffsetsPoolCPU[27][3] = {
{1, 1, -1}, {0, -1, 1}, {0, 1, 1}, {0, 1, 0}, {0, 0, 1}, {0, -1, 0}, {-1, 1, -1},
{0, -1, -1}, {1, 0, 0}, {1, -1, 1}, {1, 0, 1}, {-1, 1, 1}, {-1, 0, 0}, {1, -1, -1},
{0, 1, -1}, {-1, -1, 0}, {-1, 1, 0}, {0, 0, 0}, {0, 0, -1}, {1, 1, 0}, {1, 0, -1},
{1, -1, 0}, {-1, 0, 1}, {1, 1, 1}, {-1, 0, -1}, {-1, -1, -1}, {-1, -1, 1}};
cudaMemcpyToSymbol(cellOffsetsPool, cellOffsetsPoolCPU, 27*3*sizeof(int));
int numSelectedPointsCPU = 0;
gpuErrchk(cudaMemset(pAuxBoolBuffer, 0, sizeof(bool)*pNumPoints));
int* numSelectedPoints;
gpuErrchk(cudaMalloc(&numSelectedPoints, sizeof(int)));
gpuErrchk(cudaMemset(numSelectedPoints, 0, sizeof(int)));
int numPhaseGroups = pNumCells/3;
numPhaseGroups += (pNumCells%3!=0)?1:0;
int numBlocks = numPhaseGroups/BLOCK_SIZE;
numBlocks += (numPhaseGroups%BLOCK_SIZE!=0)?1:0;
for(int b = 0; b < pBatchSize; ++b){
for(int i = 0; i < 27; ++i){
selectSamples<<<dim3(numBlocks,numBlocks,numBlocks), dim3(BLOCK_SIZE,BLOCK_SIZE,BLOCK_SIZE)>>>
(scaleInv, b, i, pNumPoints, pBatchSize, pNumCells, pRadius, pAABBMin,
pAABBMax, pPoints, pBatchIds, pCellIndexs, pAuxBoolBuffer, pSelectedPts,
pSelectedBatchIds, pSelectedIndexs, numSelectedPoints);
gpuErrchk(cudaPeekAtLastError());
}
}
//Copy from GPU the number of selected samples.
gpuErrchk(cudaMemcpy(&numSelectedPointsCPU, numSelectedPoints, sizeof(int), cudaMemcpyDeviceToHost));
gpuErrchk(cudaFree(numSelectedPoints));
#ifdef PRINT_CONV_INFO
printf("Num Cells: %d | Input points: %d | Result pooling: %d\n", pNumCells, pNumPoints, numSelectedPointsCPU);
#endif
return numSelectedPointsCPU;
}
void copyPoints(
float* pSelectedPts,
int* pSelectedBatchIds,
int* pSelectedIndexs,
const int pNumPts,
float* pDestPts,
int* pDestBatchIds,
int* pDestIndexs)
{
gpuErrchk(cudaMemcpy(pDestPts, pSelectedPts, sizeof(float)*3*pNumPts, cudaMemcpyDeviceToDevice));
gpuErrchk(cudaMemcpy(pDestBatchIds, pSelectedBatchIds, sizeof(int)*pNumPts, cudaMemcpyDeviceToDevice));
gpuErrchk(cudaMemcpy(pDestIndexs, pSelectedIndexs, sizeof(int)*pNumPts, cudaMemcpyDeviceToDevice));
}
void getFeaturesSampledPoints(
int pNumPoints,
int pNumFeatures,
int pNumSampledPoints,
const int* pInPointsIndexs,
const float* pInFeature,
float* pOutSelFeatures)
{
int numBlocksPoints = pNumSampledPoints/PT_BLOCK_SIZE;
numBlocksPoints += (pNumSampledPoints%PT_BLOCK_SIZE != 0)?1:0;
selectFeatureSamples<<<pNumSampledPoints, PT_BLOCK_SIZE>>>(pNumSampledPoints, pNumFeatures, pInPointsIndexs, pInFeature, pOutSelFeatures);
gpuErrchk(cudaPeekAtLastError());
}
void getFeaturesSampledPointsGradients(
int pNumPoints,
int pNumFeatures,
int pNumSampledPoints,
const int* pInPointsIndexs,
const float* pInOutFeatureGrad,
float* pOutInFeaturesGradients)
{
gpuErrchk(cudaMemset(pOutInFeaturesGradients, 0, sizeof(int)*pNumFeatures*pNumPoints));
int numBlocksPoints = pNumSampledPoints/PT_BLOCK_SIZE;
numBlocksPoints += (pNumSampledPoints%PT_BLOCK_SIZE != 0)?1:0;
selectFeatureSamplesGrad<<<pNumSampledPoints, PT_BLOCK_SIZE>>>(pNumSampledPoints, pNumFeatures, pInPointsIndexs, pInOutFeatureGrad, pOutInFeaturesGradients);
gpuErrchk(cudaPeekAtLastError());
}