-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcompute_pdf.cu
120 lines (105 loc) · 4.79 KB
/
compute_pdf.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
/////////////////////////////////////////////////////////////////////////////
/// \file compute_pdf.cu
///
/// \brief Cuda implementation of the operation to approximate the
/// probability distribution function at each sample in the different
/// receptive fields.
///
/// \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 <iostream>
#include <fstream>
#include "cuda_kernel_utils.h"
#define NEIGHBOR_BLOCK_PDF_SIZE 256
////////////////////////////////////////////////////////////////////////////////// GPU
/**
* Method to compute the pdfs of each neighboring point.
* @param pWindow Window used to compute the pdfs.
* @param pNumPoints Number of points.
* @param pNumNeighbors Number of neighboring points.
* @param pRadius Radius of the convolution.
* @param pAABBMin Minimum point of the grid (3 componenets).
* @param pAABBMax Maximum point of the grid (3 componenets).
* @param pPoints List of points.
* @param pBatchIds List of batch ids.
* @param pPoints2 List of neighboring points.
* @param pStartIndexs List of the starting indices in the neighboring list.
* @param pNeigbors List neighbors of each point.
* @param pOutPDFs Output parameter with the pdfs.
*/
__global__ void computePDFs(
const bool pScaleInv,
const float pWindow,
const int numSamples,
const int pNumNeighbors,
const float pRadius,
const float* __restrict__ pAABBMin,
const float* __restrict__ pAABBMax,
const float* __restrict__ pPoints,
const int* __restrict__ pBatchIds,
const int* __restrict__ pStartIndexs,
const int* __restrict__ pNeigbors,
float* __restrict__ pOutPDFs)
{
int currentNeighborIndex = threadIdx.x + blockDim.x*(blockIdx.x + blockIdx.y*gridDim.x + blockIdx.z*gridDim.x*gridDim.y);
if(currentNeighborIndex < pNumNeighbors){
int neighborIndex = currentNeighborIndex * 2;
int currentPoint = pNeigbors[neighborIndex];
float currPointCoords[3] = {pPoints[currentPoint*3], pPoints[currentPoint*3+1], pPoints[currentPoint*3+2]};
int currBatchId = pBatchIds[currentPoint];
float maxAabbSize = max(max(
pAABBMax[currBatchId*3] - pAABBMin[currBatchId*3],
pAABBMax[currBatchId*3+1] - pAABBMin[currBatchId*3+1]),
pAABBMax[currBatchId*3+2] - pAABBMin[currBatchId*3+2]);
float scaledRadius = (pScaleInv)?pRadius*maxAabbSize:pRadius;
int centralPoint = pNeigbors[neighborIndex+1];
int initIter = pStartIndexs[centralPoint];
int endIter = (centralPoint < numSamples-1)?pStartIndexs[centralPoint+1]:pNumNeighbors;
const float h = pWindow;
const float invH = 1/h;
const float invRadH = 1.0/(scaledRadius*h);
float currPdf = 0.0;
int iter = initIter;
while(iter < endIter)
{
int iterPoint = pNeigbors[iter*2]*3;
float iterPointCoords[3] = {pPoints[iterPoint], pPoints[iterPoint+1], pPoints[iterPoint+2]};
float diff [3] = {
(iterPointCoords[0] - currPointCoords[0])*invRadH,
(iterPointCoords[1] - currPointCoords[1])*invRadH,
(iterPointCoords[2] - currPointCoords[2])*invRadH};
float gaussVal = invH*((0.39894228)*exp((-0.5)*diff[0]*diff[0]));
gaussVal = gaussVal*invH*((0.39894228)*exp((-0.5)*diff[1]*diff[1]));
gaussVal = gaussVal*invH*((0.39894228)*exp((-0.5)*diff[2]*diff[2]));
currPdf += gaussVal;
iter++;
}
pOutPDFs[currentNeighborIndex] = (currPdf)/((float)endIter-initIter);
}
}
////////////////////////////////////////////////////////////////////////////////// CPU
void computeDPFsCPU(
const bool scaleInv,
const float pWindow,
const int numSamples,
const int pNumNeighbors,
const float pRadius,
const float* pInPts,
const int* pInBatchIds,
const float* pAABBMin,
const float* pAABBMax,
const int* pStartIndexs,
const int* pPackedIndexs,
float* pPDFs)
{
//Compute the PDF.
dim3 gridDimension = computeBlockGrid(pNumNeighbors, NEIGHBOR_BLOCK_PDF_SIZE);
computePDFs<<<gridDimension, NEIGHBOR_BLOCK_PDF_SIZE>>>(scaleInv, pWindow, numSamples, pNumNeighbors,
pRadius, pAABBMin, pAABBMax, pInPts, pInBatchIds, pStartIndexs, pPackedIndexs, pPDFs);
gpuErrchk(cudaPeekAtLastError());
}