forked from KeckCAVES/LidarViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointSetSimilarity.cpp
141 lines (120 loc) · 3.92 KB
/
PointSetSimilarity.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
/***********************************************************************
PointSetSimilarity - Tool to calculate a similarity measure between two
(relatively small) subsets of the same original point set.
Copyright (c) 2009-2011 Oliver Kreylos
This file is part of the LiDAR processing and analysis package.
The LiDAR processing and analysis package is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
The LiDAR processing and analysis package is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with the LiDAR processing and analysis package; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <IO/OpenFile.h>
#include <IO/ValueSource.h>
#include <Math/Math.h>
#include <Geometry/ArrayKdTree.h>
#include "LidarTypes.h"
class MatchingPointFinder
{
/* Elements: */
private:
Point p; // Searched point
Scalar epsilon,epsilon2; // Maximum search distance
bool found; // Flag whether a matching point was found
/* Constructors and destructors: */
public:
MatchingPointFinder(const Point& sP,Scalar sEpsilon)
:p(sP),epsilon(sEpsilon),epsilon2(Math::sqr(epsilon)),
found(false)
{
}
/* Methods: */
const Point& getQueryPosition(void) const
{
return p;
}
bool operator()(const Point& node,int splitDimension)
{
/* Compare node's point to current closest point: */
if(Geometry::sqrDist(node,p)<epsilon2)
found=true;
/* Stop traversal if split plane is farther away than epsilon: */
return epsilon>Math::abs(node[splitDimension]-p[splitDimension]);
};
bool isFound(void) const
{
return found;
}
};
typedef Geometry::ArrayKdTree<Point> PointTree;
PointTree* loadPointSet(const char* pointFileName)
{
std::vector<Point> points;
{
std::cout<<"Loading points from "<<pointFileName<<"..."<<std::flush;
IO::ValueSource pointSource(IO::openFile(pointFileName));
pointSource.setWhitespace(',',true);
pointSource.setPunctuation('\n',true);
pointSource.skipWs();
while(!pointSource.eof())
{
/* Read the next point: */
Point p;
for(int i=0;i<3;++i)
p[i]=Scalar(pointSource.readNumber());
points.push_back(p);
/* Skip the rest of the line: */
pointSource.skipLine();
pointSource.skipWs();
}
std::cout<<" done"<<std::endl;
}
std::cout<<"Creating kd-tree of "<<points.size()<<" points..."<<std::flush;
PointTree* pointTree=new PointTree(points.size());
Point* ptps=pointTree->accessPoints();
for(size_t i=0;i<points.size();++i)
ptps[i]=points[i];
pointTree->releasePoints(4);
std::cout<<" done"<<std::endl;
return pointTree;
}
int main(int argc,char* argv[])
{
/* Load the two point sets: */
PointTree* trees[2];
for(int i=0;i<2;++i)
trees[i]=loadPointSet(argv[1+i]);
/* Get the epsilon value: */
Scalar epsilon=Scalar(atof(argv[3]));
/* Compare the point sets: */
int numMatches=0;
for(int tree=0;tree<2;++tree)
{
PointTree& tree1=*trees[tree];
PointTree& tree2=*trees[1-tree];
for(int i=0;i<tree1.getNumNodes();++i)
{
/* Find a match for the point in tree1 in tree2: */
MatchingPointFinder mpf(tree1.getNode(i),epsilon);
tree2.traverseTreeDirected(mpf);
if(mpf.isFound())
++numMatches;
}
}
/* Print the result: */
std::cout<<"Similarity percentage between the two point sets: "<<double(numMatches)*100.0/double(trees[0]->getNumNodes()+trees[1]->getNumNodes())<<"%"<<std::endl;
for(int i=0;i<2;++i)
delete trees[i];
return 0;
}