-
Notifications
You must be signed in to change notification settings - Fork 39
/
PrintPrimitiveFile.cpp
235 lines (208 loc) · 6.39 KB
/
PrintPrimitiveFile.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <vector>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <Misc/ThrowStdErr.h>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <Geometry/Point.h>
#include <Geometry/Vector.h>
#include <Geometry/Endianness.h>
#include <Geometry/OutputOperators.h>
typedef Geometry::Point<double,3> Point;
typedef Geometry::Vector<double,3> Vector;
void processFile(const char* fileName,const std::vector<int> extractIndices,IO::FilePtr outputFile)
{
/* Open the primitive file: */
IO::FilePtr file(IO::openFile(fileName));
file->setEndianness(Misc::LittleEndian);
/* Read the file header: */
char header[40];
file->read<char>(header,sizeof(header));
if(strcmp(header,"LidarViewer primitive file v1.2 \n")!=0)
{
std::cerr<<"File "<<fileName<<" is not a valid version 1.2 primitive file"<<std::endl;
return;
}
std::cout<<"Primitive file "<<fileName<<":"<<std::endl;
/* Process all primitives in the file: */
int primitiveIndex=0;
while(!file->eof())
{
/* Check whether to save this primitive to the result file: */
bool save=outputFile!=0;
if(save)
{
save=false;
for(std::vector<int>::const_iterator eaIt=extractIndices.begin();!save&&eaIt!=extractIndices.end();++eaIt)
save=*eaIt==primitiveIndex;
}
/* Read the primitive type: */
int primitiveType=file->read<int>();
if(save)
outputFile->write<int>(primitiveType);
/* Read a primitive of the appropriate type: */
switch(primitiveType)
{
case 0: // Plane primitive
{
size_t numPoints=file->read<unsigned int>();
double rms=file->read<double>();
std::cout<<"Plane primitive fitting "<<numPoints<<" points with RMS "<<rms<<":"<<std::endl;
Vector normal=file->read<Vector>();
double offset=file->read<double>();
std::cout<<" Plane equation: "<<normal<<" * X = "<<offset<<std::endl;
std::cout<<" Corner points:"<<std::endl;
Point corners[4];
for(int i=0;i<4;++i)
{
corners[i]=file->read<Point>();
std::cout<<" "<<corners[i]<<std::endl;
}
int numSegments[2];
file->read(numSegments,2);
std::cout<<" Number of segments: "<<numSegments[0]<<" x "<<numSegments[1]<<std::endl;
if(save)
{
outputFile->write<unsigned int>(numPoints);
outputFile->write<double>(rms);
outputFile->write<Vector>(normal);
outputFile->write<double>(offset);
outputFile->write<Point>(corners,4);
outputFile->write<int>(numSegments,2);
}
break;
}
case 1: // Sphere primitive
{
size_t numPoints=file->read<unsigned int>();
double rms=file->read<double>();
std::cout<<"Sphere primitive fitting "<<numPoints<<" points with RMS "<<rms<<":"<<std::endl;
Point center=file->read<Point>();
double radius=file->read<double>();
std::cout<<" Center point: "<<center<<std::endl;
std::cout<<" Radius: "<<radius<<std::endl;
if(save)
{
outputFile->write<unsigned int>(numPoints);
outputFile->write<double>(rms);
outputFile->write<Point>(center);
outputFile->write<double>(radius);
}
break;
}
case 2: // Cylinder primitive
{
size_t numPoints=file->read<unsigned int>();
double rms=file->read<double>();
std::cout<<"Cylinder primitive fitting "<<numPoints<<" points with RMS "<<rms<<":"<<std::endl;
Point center=file->read<Point>();
Vector axis=file->read<Vector>();
double radius=file->read<double>();
double height=file->read<double>();
int numSegments[2];
file->read(numSegments,2);
std::cout<<" Center point: "<<center<<std::endl;
std::cout<<" Axis direction: "<<axis<<std::endl;
std::cout<<" Radius: "<<radius<<std::endl;
std::cout<<" Height: "<<height<<std::endl;
std::cout<<" Number of segments: "<<numSegments[0]<<" x "<<numSegments[1]<<std::endl;
if(save)
{
outputFile->write<unsigned int>(numPoints);
outputFile->write<double>(rms);
outputFile->write<Point>(center);
outputFile->write<Vector>(axis);
outputFile->write<double>(radius);
outputFile->write<double>(height);
outputFile->write<int>(numSegments,2);
}
break;
}
case 3: // Line primitive
{
size_t numPoints=file->read<unsigned int>();
double rms=file->read<double>();
Point center=file->read<Point>();
Vector axis=file->read<Vector>();
double length=file->read<double>();
std::cout<<"Line primitive fitting "<<numPoints<<" points with RMS "<<rms<<":"<<std::endl;
std::cout<<" Center point: "<<center<<std::endl;
std::cout<<" Axis direction: "<<axis<<std::endl;
std::cout<<" Length: "<<length<<std::endl;
if(save)
{
outputFile->write<unsigned int>(numPoints);
outputFile->write<double>(rms);
outputFile->write<Point>(center);
outputFile->write<Vector>(axis);
outputFile->write<double>(length);
}
break;
}
case 4: // Point primitive
{
Point point=file->read<Point>();
std::cout<<"Point primitive:"<<std::endl;
std::cout<<" Point: "<<point<<std::endl;
if(save)
{
outputFile->write<Point>(point);
}
break;
}
default:
std::cerr<<"Unknown primitive type "<<primitiveType<<" in primitive file "<<fileName<<std::endl;
return;
}
std::cout<<std::endl;
++primitiveIndex;
}
}
int main(int argc,char* argv[])
{
/* Process the command line: */
IO::FilePtr outputFile;
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"o")==0)
{
/* Close the previous output file: */
outputFile=0;
/* Open a new output file: */
++i;
try
{
outputFile=IO::openFile(argv[i],IO::File::WriteOnly);
outputFile->setEndianness(Misc::LittleEndian);
char header[40]="LidarViewer primitive file v1.2 \n";
outputFile->write<char>(header,sizeof(header));
}
catch(std::runtime_error err)
{
std::cerr<<"Could not create output file "<<argv[i]<<" due to exception "<<err.what()<<std::endl;
outputFile=0;
}
}
}
else
{
/* Check if the user wants to extract primitives: */
const char* fileName=argv[i];
std::vector<int> extractIndices;
while(i+1<argc&&isdigit(argv[i+1][0]))
{
++i;
extractIndices.push_back(atoi(argv[i]));
}
/* Process the file: */
processFile(fileName,extractIndices,outputFile);
}
}
return 0;
}