-
Notifications
You must be signed in to change notification settings - Fork 97
/
jly_main.cpp
192 lines (163 loc) · 5.75 KB
/
jly_main.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
189
190
191
192
/********************************************************************
Main Function for point cloud registration with Go-ICP Algorithm
Last modified: Feb 13, 2014
"Go-ICP: Solving 3D Registration Efficiently and Globally Optimally"
Jiaolong Yang, Hongdong Li, Yunde Jia
International Conference on Computer Vision (ICCV), 2013
Copyright (C) 2013 Jiaolong Yang (BIT and ANU)
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include <time.h>
#include <iostream>
#include <fstream>
using namespace std;
#include "jly_goicp.h"
#include "ConfigMap.hpp"
#define DEFAULT_OUTPUT_FNAME "output.txt"
#define DEFAULT_CONFIG_FNAME "config.txt"
#define DEFAULT_MODEL_FNAME "model.txt"
#define DEFAULT_DATA_FNAME "data.txt"
void parseInput(int argc, char **argv, string & modelFName, string & dataFName, int & NdDownsampled, string & configFName, string & outputFName);
void readConfig(string FName, GoICP & goicp);
int loadPointCloud(string FName, int & N, POINT3D ** p);
int main(int argc, char** argv)
{
int Nm, Nd, NdDownsampled;
clock_t clockBegin, clockEnd;
string modelFName, dataFName, configFName, outputFname;
POINT3D * pModel, * pData;
GoICP goicp;
parseInput(argc, argv, modelFName, dataFName, NdDownsampled, configFName, outputFname);
readConfig(configFName, goicp);
// Load model and data point clouds
loadPointCloud(modelFName, Nm, &pModel);
loadPointCloud(dataFName, Nd, &pData);
goicp.pModel = pModel;
goicp.Nm = Nm;
goicp.pData = pData;
goicp.Nd = Nd;
// Build Distance Transform
cout << "Building Distance Transform..." << flush;
clockBegin = clock();
goicp.BuildDT();
clockEnd = clock();
cout << (double)(clockEnd - clockBegin)/CLOCKS_PER_SEC << "s (CPU)" << endl;
// Run GO-ICP
if(NdDownsampled > 0)
{
goicp.Nd = NdDownsampled; // Only use first NdDownsampled data points (assumes data points are randomly ordered)
}
cout << "Model ID: " << modelFName << " (" << goicp.Nm << "), Data ID: " << dataFName << " (" << goicp.Nd << ")" << endl;
cout << "Registering..." << endl;
clockBegin = clock();
goicp.Register();
clockEnd = clock();
double time = (double)(clockEnd - clockBegin)/CLOCKS_PER_SEC;
cout << "Optimal Rotation Matrix:" << endl;
cout << goicp.optR << endl;
cout << "Optimal Translation Vector:" << endl;
cout << goicp.optT << endl;
cout << "Finished in " << time << endl;
ofstream ofile;
ofile.open(outputFname.c_str(), ofstream::out);
ofile << time << endl;
ofile << goicp.optR << endl;
ofile << goicp.optT << endl;
ofile.close();
delete(pModel);
delete(pData);
return 0;
}
void parseInput(int argc, char **argv, string & modelFName, string & dataFName, int & NdDownsampled, string & configFName, string & outputFName)
{
// Set default values
modelFName = DEFAULT_MODEL_FNAME;
dataFName = DEFAULT_DATA_FNAME;
configFName = DEFAULT_CONFIG_FNAME;
outputFName = DEFAULT_OUTPUT_FNAME;
NdDownsampled = 0; // No downsampling
//cout << endl;
//cout << "USAGE:" << "./GOICP <MODEL FILENAME> <DATA FILENAME> <NUM DOWNSAMPLED DATA POINTS> <CONFIG FILENAME> <OUTPUT FILENAME>" << endl;
//cout << endl;
if(argc > 5)
{
outputFName = argv[5];
}
if(argc > 4)
{
configFName = argv[4];
}
if(argc > 3)
{
NdDownsampled = atoi(argv[3]);
}
if(argc > 2)
{
dataFName = argv[2];
}
if(argc > 1)
{
modelFName = argv[1];
}
cout << "INPUT:" << endl;
cout << "(modelFName)->(" << modelFName << ")" << endl;
cout << "(dataFName)->(" << dataFName << ")" << endl;
cout << "(NdDownsampled)->(" << NdDownsampled << ")" << endl;
cout << "(configFName)->(" << configFName << ")" << endl;
cout << "(outputFName)->(" << outputFName << ")" << endl;
cout << endl;
}
void readConfig(string FName, GoICP & goicp)
{
// Open and parse the associated config file
ConfigMap config(FName.c_str());
goicp.MSEThresh = config.getF("MSEThresh");
goicp.initNodeRot.a = config.getF("rotMinX");
goicp.initNodeRot.b = config.getF("rotMinY");
goicp.initNodeRot.c = config.getF("rotMinZ");
goicp.initNodeRot.w = config.getF("rotWidth");
goicp.initNodeTrans.x = config.getF("transMinX");
goicp.initNodeTrans.y = config.getF("transMinY");
goicp.initNodeTrans.z = config.getF("transMinZ");
goicp.initNodeTrans.w = config.getF("transWidth");
goicp.trimFraction = config.getF("trimFraction");
// If < 0.1% trimming specified, do no trimming
if(goicp.trimFraction < 0.001)
{
goicp.doTrim = false;
}
goicp.dt.SIZE = config.getI("distTransSize");
goicp.dt.expandFactor = config.getF("distTransExpandFactor");
cout << "CONFIG:" << endl;
config.print();
//cout << "(doTrim)->(" << goicp.doTrim << ")" << endl;
cout << endl;
}
int loadPointCloud(string FName, int & N, POINT3D ** p)
{
int i;
ifstream ifile;
ifile.open(FName.c_str(), ifstream::in);
if(!ifile.is_open())
{
cout << "Unable to open point file '" << FName << "'" << endl;
exit(-1);
}
ifile >> N; // First line has number of points to follow
*p = (POINT3D *)malloc(sizeof(POINT3D) * N);
for(i = 0; i < N; i++)
{
ifile >> (*p)[i].x >> (*p)[i].y >> (*p)[i].z;
}
ifile.close();
return 0;
}