-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-histograms.cpp
188 lines (166 loc) · 6.09 KB
/
create-histograms.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
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include "boost/filesystem.hpp"
#include <cstring>
#include "project/Importer.h"
#include "project/Exporter.h"
#include "project/PointCloud.h"
#include <cmath>
#include "project/KdTree.h"
#include "project/SPFH.h"
/**
* Implementation of histogram computation following these steps:
* 1.) read point cloud from input file
* 2.) build kd-tree from point cloud
* 3.) compute normals for all points in point cloud
* 4.) compute SPFH for every point in point cloud
* 5.) compute FPFH for every point in point cloud
* 6.) export histogram representation to feature file
*
* \author: mack
*/
void computeHistogramsForPath(std::string path, double rN, double rH,
double minsize, double maxsize, double binsize, Exporter exp) {
Importer import(path);
PointCloud cloud;
std::cout << "Importing data from file " << path << std::endl;
//read point cloud from file
import.readFile(cloud);
//build kd-tree from cloud
KdTreeNode root;
root.buildTree(cloud.getPoints(), 0);
//compute normals for all points with radius rN
int count = 0;
std::cout << "computing normals..." << std::endl;
for (unsigned int i = 0; i < cloud.getSize(); i++) {
count++;
if (count % 1000 == 0) {
std::cout << count << "..." << std::endl;
}
std::vector<Point*> neighbors;
root.fixedRadiusSearch(cloud.getPoints()[i], rN, neighbors);
cloud.getPoints()[i]->computeNormal(neighbors);
}
//compute SPFHs for all points with radius rH
count = 0;
std::cout << "computing SPFHs..." << std::endl;
for (unsigned int i = 0; i < cloud.getSize(); i++) {
count++;
if (count % 1000 == 0) {
std::cout << count << "..." << std::endl;
}
std::vector<Point*> neighbors;
root.fixedRadiusSearch(cloud.getPoints()[i], rH, neighbors);
SPFH* s = new SPFH(minsize, maxsize, binsize);
for (unsigned int j = 0; j < neighbors.size(); j++) {
if (cloud.getPoints()[i] == neighbors[j]) {
continue;
}
Features* f = new Features(cloud.getPoints()[i], neighbors[j]);
s->addToHistogram(f);
delete f;
}
cloud.getPoints()[i]->setSimplePointFeatureHistogram(s);
}
//compute SPFHs for all points with radius rH
count = 0;
std::cout << "computing FPFHs..." << std::endl;
for (unsigned int i = 0; i < cloud.getSize(); i++) {
count++;
if (count % 1000 == 0) {
std::cout << count << "..." << std::endl;
}
std::vector<Point*> neighbors;
root.fixedRadiusSearch(cloud.getPoints()[i], rH, neighbors);
FPFH* f = new FPFH(minsize, maxsize, binsize);
f->addToHistogram(
cloud.getPoints()[i]->getSimplePointFeatureHistogram(), 1, 1);
for (unsigned int j = 0; j < neighbors.size(); j++) {
if (cloud.getPoints()[i] == neighbors[j]) {
continue;
}
f->addToHistogram(neighbors[j]->getSimplePointFeatureHistogram(),
neighbors.size(),
cloud.getPoints()[i]->euclideanDistance(neighbors[j]));
}
cloud.getPoints()[i]->setFastPointFeatureHistogram(f);
}
//write histograms to file
std::cout << "Writing histograms in file..." << std::endl;
exp.writeFile(cloud);
}
int main(int argc, char **argv) {
std::string sourceDirTrain = "data/train";
std::string sourceDirTest = "data/test";
std::string destinationTrain = "data/histograms/train/histograms.dat";
std::string destinationTest = "data/histograms/test/histograms.dat";
double rN = 5;
double rH = 2.5;
double binsize = 0.1;
double minsize = -M_PI;
double maxsize = M_PI;
//read in parameters, use standard parameters if nothing given
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-help") == 0) {
std::cout << "The following parameters are available: \n"
<< "-rN: The radius rN for the nearest neighbor search during normal computation \n"
<< "-rH: The radius rH for the nearest neighbor search during histogram computation \n"
<< "-iTr: The path to the source directory for training data (default: data/train) \n"
<< "-iTe: The path to the source directory for test data (default: data/test) \n"
<< "-oTr: The path to the target file for training data (default: data/histograms/train/histograms.dat) \n"
<< "-oTe: The path to the target file for test data (default: data/histograms/test/histograms.dat) \n"
<< "-bin: The binsize of the histograms (default: 0.1) "
<< std::endl;
return 1;
}
if (strcmp(argv[i], "-rN") == 0) {
rN = atof(argv[++i]);
} else if (strcmp(argv[i], "-rH") == 0) {
rH = atof(argv[++i]);
} else if (strcmp(argv[i], "-iTr") == 0) {
sourceDirTrain = argv[++i];
} else if (strcmp(argv[i], "-iTe") == 0) {
sourceDirTest = argv[++i];
} else if (strcmp(argv[i], "-oTr") == 0) {
destinationTrain = argv[++i];
} else if (strcmp(argv[i], "-oTe") == 0) {
destinationTest = argv[++i];
} else if (strcmp(argv[i], "-bin") == 0) {
binsize = atof(argv[++i]);
}
}
//compute histograms for training files
Exporter expTrain(destinationTrain);
expTrain.deleteOldFile();
if (!boost::filesystem::exists(sourceDirTrain))
std::cout << " The filepath " << sourceDirTrain
<< " does not exist. You can specify another directory using input parameter -i."
<< std::endl;
boost::filesystem::directory_iterator end_itr_train;
for (boost::filesystem::directory_iterator itr(sourceDirTrain);
itr != end_itr_train; ++itr) {
if (itr->path().leaf().string().find(".xyz") != std::string::npos) {
computeHistogramsForPath(itr->path().string(), rN, rH, minsize,
maxsize, binsize, expTrain);
}
}
//compute histograms for test files
Exporter expTest(destinationTest);
expTest.deleteOldFile();
if (!boost::filesystem::exists(sourceDirTest))
std::cout << " The filepath " << sourceDirTest
<< " does not exist. You can specify another directory using input parameter -i."
<< std::endl;
boost::filesystem::directory_iterator end_itr_test; // default construction yields past-the-end
for (boost::filesystem::directory_iterator itr(sourceDirTest);
itr != end_itr_test; ++itr) {
if (itr->path().leaf().string().find(".xyz") != std::string::npos) {
computeHistogramsForPath(itr->path().string(), rN, rH, minsize,
maxsize, binsize, expTest);
}
}
std::cout << "done" << std::endl;
return 1;
}