Examples for using a KdTree to analyze and process local neighbourhoods.
#include "interface.h"
#include "algorithm.h"
#include "geometry.h"
#include "examples.h"
#include "io.h"
int test_KdTree()
{
printf("\n***test_KdTree***\n");
size_t i=0,numNN=0,ptsVec=0;
ALG3D_KDTREE tree=0;
const char* fname ="./sample_data/testData.xyz";
const size_t maxPoints = 15;
double vDist[3]={5,3,1};
double x=156.47, y=- 14.24, z=-637.97;
printf("nearest neighbor: nothing found in range\n");
}
else{
printf(
"nearest neighbor: %lf, %lf %lf\n",nn.
vec[0],nn.
vec[1],nn.
vec[2]);
}
printf("ALG3D_findNearestNeighbor finished\n");
else{
printf("found %I64d neighbors:\n",numNN);
for(i=0;i<numNN;i++){
printf("[%3I64d]: %lf, %lf, %lf\n",i,vNN[i].vec[0],vNN[i].vec[1],vNN[i].vec[2]);
}
}
else{
printf("points inside box: %I64d\n",numNN);
}
else if (numNN != maxPoints){
printf("warning: number of points returned (%I64d) not equals number of points requested (%I64d)\n", numNN, maxPoints);
return 0;
}
else{
printf("points found: %I64d/%I64d\n", numNN, maxPoints);
}
printf("ALG3D_findKNearestNeighbors finished\n");
printf("--> passed successfully\n");
return 1;
}
int test_KdTreeWithNormals()
{
printf("\n***test_KdTreeWithNormals***\n");
size_t i = 0, numNN = 0, ptsVec = 0;
ALG3D_KDTREE tree = 0;
const char* fname = "./sample_data/schiller05.xyz";
const size_t maxPoints = 15;
for (i = 0; i < ptsVec; ++i)
{
toSensor.
vec[0] = sensorPos.
vec[0] - data[i].
vec[0];
toSensor.
vec[1] = sensorPos.
vec[1] - data[i].
vec[1];
toSensor.
vec[2] = sensorPos.
vec[2] - data[i].
vec[2];
{
normals[i].
vec[0] = -normals[i].
vec[0];
normals[i].
vec[1] = -normals[i].
vec[1];
normals[i].
vec[2] = -normals[i].
vec[2];
}
}
printf("nearest neighbor: nothing found in range\n");
}
else{
printf(
"nearest compatible neighbor: %lf %lf %lf\n", nn.
vec[0], nn.
vec[1], nn.
vec[2]);
}
printf("ALG3D_findNearestNormalCompatibleNeighbor finished\n");
return 0;
else{
printf("found %I64d neighbors:\n", numNN);
for (i = 0; i < numNN; i++){
printf("[%3I64d]: %lf %lf %lf\n", i, vNN[i].vec[0], vNN[i].vec[1], vNN[i].vec[2]);
}
}
printf("ALG3D_findNormalCompatibleNeighbors finished\n");
return 0;
else if (numNN != maxPoints){
printf("warning: number of points returned (%I64d) not equals number of points requested (%I64d)\n", numNN, maxPoints);
return 0;
}
else{
printf("points found: %I64d/%I64d\n", numNN, maxPoints);
for (i = 0; i < numNN; i++){
printf("[%3I64d]: %lf %lf %lf\n", i, vNN[i].vec[0], vNN[i].vec[1], vNN[i].vec[2]);
}
}
printf("ALG3D_findKNearestNormalCompatibleNeighbors finished\n");
free(normals);
printf("--> passed successfully\n");
return 1;
}
int test_ThinningTree()
{
size_t i=0,ptsVec=0, count=0;
ALG3D_KDTREE tree=0;
const char* fname ="./sample_data/testData.xyz";
printf("\n***test_ThinningTree***\n");
else{
count=0;
for (i=0; i < ptsVec; ++i){
if (data[i].vec[0] != 0 && data[i].vec[1] != 0 && data[i].vec[2] != 0)
count++;
}
printf("remaining points: %I64d\n",count);
}
printf("--> passed successfully\n");
return 1;
}
int test_RegistrationTree()
{
printf("\n***test_RegistrationTree***\n");
const char* fnameStat ="./sample_data/surface1.xyz";
const char* fnameDyn ="./sample_data/surface2.xyz";
const char* fnameOut ="./sample_data/icpOut2.xyz";
size_t ptsStat=0, ptsDyn=0;
ALG3D_KDTREE tree=0;
size_t i=0;
double R[9]={0,0,0,0,0,0,0,0,0},T[3]={0,0,0},S[1]={0};
printf("minDist: %lf, maxDist: %lf, meanDev: %lf, stdDev: %3.10lf, pairs: %I64d, iterations: %I64d\n",
printf("** R **");
for(i=0;i<9;++i){
if(i%3==0)printf("\n");
printf("%lf\t",R[i]);
}printf("\n");
printf("** T **\n");
for(i=0;i<3;++i){
printf("%lf\t",T[i]);
}printf("\n");
printf("** S **\n");
printf("%lf\n",S[0]);
printf("--> passed successfully\n");
return 1;
}
int test_RegistrationPlaneTree()
{
printf("\n***test_RegistrationPlaneTree***\n");
const char* fnameStat ="./sample_data/surface1.xyz";
const char* fnameDyn ="./sample_data/surface2.xyz";
const char* fnameOut ="./sample_data/icpOut2Plane.xyz";
size_t ptsStat=0, ptsDyn=0;
ALG3D_KDTREE tree=0;
size_t i=0;
double R[9]={0,0,0,0,0,0,0,0,0},T[3]={0,0,0},S[1]={0};
printf("minDist: %lf, maxDist: %lf, meanDev: %lf, stdDev: %3.10lf, pairs: %I64d, iterations: %I64d\n",
printf("** R **");
for(i=0;i<9;++i){
if(i%3==0)printf("\n");
printf("%lf\t",R[i]);
}printf("\n");
printf("** T **\n");
for(i=0;i<3;++i){
printf("%lf\t",T[i]);
}printf("\n");
printf("** S **\n");
printf("%lf\n",S[0]);
free(normalsStat);
printf("--> passed successfully\n");
return 1;
}
int test_SmoothingTree()
{
printf("\n***test_SmoothingTree***\n");
size_t i=0,ptsVec=0;
ALG3D_KDTREE tree=0;
const char* fname ="./sample_data/testData.xyz";
for(i=0;i<ptsVec;++i){
resdata[i].vec[0]=0;
resdata[i].vec[1]=0;
resdata[i].vec[2]=0;
}
else{
}
free(resdata);
printf("--> passed successfully\n");
return 1;
}
int test_RegionGrowTree()
{
printf("\n***test_RegionGrowTree***\n");
ALG3D_KDTREE tree=0;
const char* fname = "./sample_data/testData.xyz";
size_t ptsVec = 0;
size_t* vRegionIdx = 0;
size_t numIdx = 0;
vRegionIdx = (size_t*)malloc(ptsVec*sizeof(size_t));
printf("regions found: %I64d\n", numIdx);
free(vRegionIdx);
printf("--> passed successfully\n");
return 1;
}
int test_DistancesTree()
{
printf("\n***test_DistancesTree***\n");
ALG3D_KDTREE tree = 0;
double *distances = 0;
double minValue = 0, maxValue = 0, meanValue = 0;
const char* fnameRef = "./sample_data/cone.xyz";
const char* fnameTest = "./sample_data/cone_rot.xyz";
size_t i = 0, ptsRef = 0, ptsTest = 0;
distances = (double*)malloc(ptsTest*sizeof(double));
for (i = 0; i < ptsTest; ++i){
distances[i] = -1;
}
return 0;
}
meanValue = minValue = maxValue = distances[0];
for (i = 1; i < ptsTest; ++i){
const double d = distances[i];
if (d < minValue)minValue = d;
else if (d>maxValue)maxValue = d;
meanValue += d;
}
meanValue /= ptsTest;
printf("[distances]: min: %lf, max: %lf, mean: %lf\n", minValue, maxValue, meanValue);
free(distances);
printf("--> passed successfully\n");
return 1;
}
int test_SurfaceNormalsTree()
{
printf("\n***test_SurfaceNormalsTree***\n");
ALG3D_KDTREE tree=0;
const char* fname = "./sample_data/cylinder.xyz";
size_t ptsVec = 0;
const size_t K = 25;
const double radius = 1.0;
free(normals);
printf("--> passed successfully\n");
return 1;
}
int test_SurfaceNormalsTreeFast()
{
printf("\n***test_SurfaceNormalsTreeFast***\n");
ALG3D_KDTREE tree=0;
const char* fname = "./sample_data/cylinder.xyz";
size_t ptsVec = 0;
const size_t K = 25;
const double radius = 1.0;
free(normals);
printf("--> passed successfully\n");
return 1;
}
int test_SurfaceFeaturesTree()
{
printf("\n***test_SurfaceFeaturesTree***\n");
ALG3D_KDTREE tree=0;
const char* fname = "./sample_data/surface1.xyz";
size_t ptsVec = 0;
double *intrinsic_curvatures = 0;
double *extrinsic_curvatures = 0;
intrinsic_curvatures = (double*)malloc(ptsVec*sizeof(double));
extrinsic_curvatures = (double*)malloc(ptsVec*sizeof(double));
double mnIn, mxIn, mnEx, mxEx;
mnIn = mxIn = intrinsic_curvatures[0];
mnEx = mxEx = extrinsic_curvatures[0];
for (size_t i = 1; i < ptsVec; ++i)
{
if (intrinsic_curvatures[i] < mnIn) mnIn = intrinsic_curvatures[i];
else if (intrinsic_curvatures[i] > mxIn) mxIn = intrinsic_curvatures[i];
if (extrinsic_curvatures[i] < mnEx) mnEx = extrinsic_curvatures[i];
else if (extrinsic_curvatures[i] > mxEx) mxEx = extrinsic_curvatures[i];
}
printf("intrinsic curvature: min: %lf, max: %lf\n", mnIn, mxIn);
printf("extrinsic curvature: min: %lf, max: %lf\n", mnEx, mxEx);
free(intrinsic_curvatures);
free(extrinsic_curvatures);
printf("--> passed successfully\n");
return 1;
}
int test_SurfaceFeaturesTreeFast()
{
printf("\n***test_SurfaceFeaturesTreeFast***\n");
ALG3D_KDTREE tree=0;
const char* fname = "./sample_data/surface1.xyz";
size_t ptsVec = 0;
double *intrinsic_curvatures = 0;
double *extrinsic_curvatures = 0;
intrinsic_curvatures = (double*)malloc(ptsVec*sizeof(double));
extrinsic_curvatures = (double*)malloc(ptsVec*sizeof(double));
double mnIn, mxIn, mnEx, mxEx;
mnIn = mxIn = intrinsic_curvatures[0];
mnEx = mxEx = extrinsic_curvatures[0];
for (size_t i = 1; i < ptsVec; ++i)
{
if (intrinsic_curvatures[i] < mnIn) mnIn = intrinsic_curvatures[i];
else if (intrinsic_curvatures[i] > mxIn) mxIn = intrinsic_curvatures[i];
if (extrinsic_curvatures[i] < mnEx) mnEx = extrinsic_curvatures[i];
else if (extrinsic_curvatures[i] > mxEx) mxEx = extrinsic_curvatures[i];
}
printf("intrinsic curvature: min: %lf, max: %lf\n", mnIn, mxIn);
printf("extrinsic curvature: min: %lf, max: %lf\n", mnEx, mxEx);
free(intrinsic_curvatures);
free(extrinsic_curvatures);
printf("--> passed successfully\n");
return 1;
}
int test_LocalOutlierFactorTree()
{
printf("\n***test_LocalOutlierFactorTree***\n");
const char* fname = "./sample_data/testData.xyz";
size_t ptsVec = 0;
double *lof_values = 0;
ALG3D_KDTREE tree = 0;
lof_values = (double*)malloc(ptsVec * sizeof(double));
double mn, mx;
mn = mx = lof_values[0];
for (size_t i = 1; i < ptsVec; ++i)
{
if (lof_values[i] < mn) mn = lof_values[i];
else if (lof_values[i] > mx) mx = lof_values[i];
}
printf("local outlier factors: min: %lf, max: %lf\n", mn, mx);
free(lof_values);
printf("--> passed successfully\n");
return 1;
}
int test_RemoveOutliersTree()
{
printf("\n***test_RemoveOutliersTree***\n");
const char* fname = "./sample_data/testData.xyz";
size_t ptsVec = 0;
ALG3D_KDTREE tree = 0;
size_t count = 0;
for (int i = 0; i < ptsVec; ++i){
if (vec[i].vec[0] != 0 && vec[i].vec[1] != 0 && vec[i].vec[2] != 0)
count++;
}
printf("remaining points: %I64d\n", ptsVec - count);
printf("--> passed successfully\n");
return 1;
}