-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDataTools.cpp
More file actions
474 lines (366 loc) · 13.1 KB
/
DataTools.cpp
File metadata and controls
474 lines (366 loc) · 13.1 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "DataTools.h"
#include <boost/tokenizer.hpp>
DataTools::DataTools()
{}
DataTools::~DataTools()
{
}
std::string DataTools::computeElapsedTime(const clock_t &_startTimer, const clock_t &_stopTimer)
{
int _iHours =(int)floor((float)(_stopTimer - _startTimer)/(CLOCKS_PER_SEC*60*60));
int _iMinutes = (int)floor((float)(_stopTimer - _startTimer))/(CLOCKS_PER_SEC*60)-_iHours*60;
int _iSeconds = ((float)(_stopTimer - _startTimer))/CLOCKS_PER_SEC - _iMinutes*60 - _iHours*60*60;
char _cHours[3], _cMinutes[3], _cSeconds[3];
sprintf(_cHours,"%d",_iHours);
if ( strlen(_cHours) < 2)
sprintf(_cHours,"0%d",_iHours);
sprintf(_cMinutes,"%d",_iMinutes);
if ( strlen(_cMinutes) < 2)
sprintf(_cMinutes,"0%d",_iMinutes);
sprintf(_cSeconds,"%d",_iSeconds);
if ( strlen(_cSeconds) < 2)
sprintf(_cSeconds,"0%d",_iSeconds);
std::stringstream _ss;
_ss << _cHours << ":" << _cMinutes << ":" << _cSeconds;
return _ss.str();
}
bool DataTools::getTrackingDataFromFile(std::string filename, std::string target, std::vector<TrackingData> &trackingVector)
{
std::fstream _file;
std::string _header;
std::streamoff _headerOffset;
int _fileLength;
_file.open(filename.c_str() , std::ios::in);
_file.seekg (0, std::ios::end);
_fileLength = _file.tellg();
_file.seekg (0, std::ios::beg);
// parse the header
bool isHeader = true;
bool lastStar = false;
while (_file.good() && isHeader)
{
char ch;
ch = (char) _file.get();
if (ch == '*')
{
lastStar = true;
} else
if (ch == '/' && lastStar)
{
isHeader = false;
} else
{
lastStar = false;
}
_header.append(1,ch);
}
// after header is succesfully parsed calculate header offset
_headerOffset = _file.tellg();
while(! ( static_cast<int>(_file.tellg()) == -1 || static_cast<int>(_file.tellg()) >= _fileLength ))
{
float tmpTimeStamp;
int nrTargets, nrPoints;
_file >> tmpTimeStamp;
//m_allTimeStamps.push_back(tmpTimeStamp);
_file >> nrTargets;
_file >> nrPoints;
/*
std::cout << "Time Stamp: " << tmpTimeStamp << std::endl;
std::cout << "Number of Targets: " << nrTargets << std::endl;
std::cout << "Number of Points: " << nrPoints << std::endl;
*/
for ( int i= 0; i < nrTargets; i++)
{
TrackingData td;
//m_file.flags( std::ios_base::scientific);
_file >> td.targetID
>> td.translation[0] >> td.translation[1] >> td.translation[2]
>> td.quaternion[0] >> td.quaternion[1] >> td.quaternion[2] >> td.quaternion[3]
>> td.scale[0] >> td.scale[1] >> td.scale[2]
>> td.error >> td.isValid;
td.timestamp = tmpTimeStamp;
if ( td.targetID.compare( target) == 0 && td.isValid && !_file.fail()) // avoid the empty line in file problem
{
trackingVector.push_back(td);
}
}
}
_file.clear();
return true;
}
bool DataTools::getTargetNamesFromFile(std::string filename, std::vector<std::string> &targetNameVector)
{
std::fstream _file;
std::string _header;
std::streamoff _headerOffset;
int _fileLength;
_file.open(filename.c_str() , std::ios::in);
_file.seekg (0, std::ios::end);
_fileLength = _file.tellg();
_file.seekg (0, std::ios::beg);
// parse the header
bool isHeader = true;
bool lastStar = false;
while (_file.good() && isHeader)
{
char ch;
ch = (char) _file.get();
if (ch == '*')
{
lastStar = true;
} else
if (ch == '/' && lastStar)
{
isHeader = false;
} else
{
lastStar = false;
}
_header.append(1,ch);
}
// after header is succesfully parsed calculate header offset
_headerOffset = _file.tellg();
// only load up to _maxCOUNT tracking data to check for the target IDs
int _counter = 0;
int _maxCOUNT = 100;
while(! ( static_cast<int>(_file.tellg()) == -1 || static_cast<int>(_file.tellg()) >= _fileLength ) && _counter <= _maxCOUNT)
{
_counter++;
float tmpTimeStamp;
int nrTargets, nrPoints;
_file >> tmpTimeStamp;
//m_allTimeStamps.push_back(tmpTimeStamp);
_file >> nrTargets;
_file >> nrPoints;
/*
std::cout << "Time Stamp: " << tmpTimeStamp << std::endl;
std::cout << "Number of Targets: " << nrTargets << std::endl;
std::cout << "Number of Points: " << nrPoints << std::endl;
*/
for ( int i= 0; i < nrTargets; i++)
{
TrackingData td;
//m_file.flags( std::ios_base::scientific);
_file >> td.targetID
>> td.translation[0] >> td.translation[1] >> td.translation[2]
>> td.quaternion[0] >> td.quaternion[1] >> td.quaternion[2] >> td.quaternion[3]
>> td.scale[0] >> td.scale[1] >> td.scale[2]
>> td.error >> td.isValid;
std::vector<std::string>::iterator it = std::find(targetNameVector.begin(), targetNameVector.end(), td.targetID);
// not found, so we can add it to the target names
if (it == targetNameVector.end())
{
targetNameVector.push_back(td.targetID);
}
}
}
_file.clear();
return true;
}
bool DataTools::writeTrackingDataToFile(std::vector<Eigen::Transform3f, Eigen::aligned_allocator<Eigen::Transform3f>> &_data, std::string targetName, std::string filename)
{
std::ofstream _trackingFile;
_trackingFile.open(filename);
// write mandatory header
_trackingFile << " Tracking data: timestamp <NumOfTargets> <NumOfPoints> <TargetID> x z z qw qx qy qz sx sy sz error isValid */" << std::endl;
for (int i=0; i < _data.size();i++)
{
_trackingFile << i << "\t1 \t0" << std::endl;
Eigen::Transform3f _trans = _data[i];
Eigen::Quaternionf _quat(_trans.rotation());
_trackingFile << targetName << "\t" << _trans.translation().x() << "\t" << _trans.translation().y() << "\t" << _trans.translation().z() << "\t" << _quat.w() << "\t" << _quat.x() << "\t" << _quat.y() << "\t" << _quat.z() << "\t1.0\t1.0\t1.0\t0\t1" << std::endl;
}
_trackingFile.close();
return true;
}
bool DataTools::getTrackingDataFromFileRelative(std::string filename, std::string target, std::string referenceTarget, std::vector<TrackingData> &trackingVector)
{
std::fstream _file;
std::string _header;
std::streamoff _headerOffset;
int _fileLength;
_file.open(filename.c_str() , std::ios::in);
_file.seekg (0, std::ios::end);
_fileLength = _file.tellg();
_file.seekg (0, std::ios::beg);
// parse the header
bool isHeader = true;
bool lastStar = false;
while (_file.good() && isHeader)
{
char ch;
ch = (char) _file.get();
if (ch == '*')
{
lastStar = true;
} else
if (ch == '/' && lastStar)
{
isHeader = false;
} else
{
lastStar = false;
}
_header.append(1,ch);
}
// after header is succesfully parsed calculate header offset
_headerOffset = _file.tellg();
int counter = 0;
while(! ( static_cast<int>(_file.tellg()) == -1 || static_cast<int>(_file.tellg()) >= _fileLength ))
{
float tmpTimeStamp;
int nrTargets, nrPoints;
_file >> tmpTimeStamp;
//m_allTimeStamps.push_back(tmpTimeStamp);
_file >> nrTargets;
_file >> nrPoints;
/*
std::cout << "Time Stamp: " << tmpTimeStamp << std::endl;
std::cout << "Number of Targets: " << nrTargets << std::endl;
std::cout << "Number of Points: " << nrPoints << std::endl;
*/
TrackingData _referenceTarget;
TrackingData _toolTarget;
bool _validReferenceTarget = false;
bool _validToolTarget = false;
for ( int i= 0; i < nrTargets; i++)
{
TrackingData td;
//m_file.flags( std::ios_base::scientific);
_file >> td.targetID
>> td.translation[0] >> td.translation[1] >> td.translation[2]
>> td.quaternion[0] >> td.quaternion[1] >> td.quaternion[2] >> td.quaternion[3]
>> td.scale[0] >> td.scale[1] >> td.scale[2]
>> td.error >> td.isValid;
td.timestamp = tmpTimeStamp;
// get the tracking data of the tool target
if ( td.targetID.compare( target) == 0 && !_file.fail() && td.isValid) // avoid the empty line in file problem
{
_validToolTarget = true;
_toolTarget = td;
//trackingVector.push_back(td);
} // get the tracking data of the reference target
else if ( td.targetID.compare( referenceTarget) == 0 && !_file.fail() && td.isValid) // avoid the empty line in file problem
{
_validReferenceTarget = true;
_referenceTarget = td;
//trackingVector.push_back(td);
}
}
// if we have acquired both, the tool and the reference position, we got enough data
// to provide the tool position in the reference target coordinate system
if ( _validReferenceTarget && _validToolTarget )
{
Eigen::Quaternionf _toolQuat( _toolTarget.quaternion[0], _toolTarget.quaternion[1], _toolTarget.quaternion[2], _toolTarget.quaternion[3]);
Eigen::Vector3f _transTool;
_transTool << _toolTarget.translation[0], _toolTarget.translation[1], _toolTarget.translation[2];
Eigen::Quaternionf _referenceQuat( _referenceTarget.quaternion[0], _referenceTarget.quaternion[1], _referenceTarget.quaternion[2], _referenceTarget.quaternion[3]);
Eigen::Vector3f _transReference;
_transReference << _referenceTarget.translation[0], _referenceTarget.translation[1], _referenceTarget.translation[2];
// build the 4x4 transformation matrix from the quaternion and 3-vector (tool target)
Eigen::Transform3f _toolTrackingMatrix;
_toolTrackingMatrix.setIdentity();
_toolTrackingMatrix.rotate( _toolQuat.toRotationMatrix());
_toolTrackingMatrix.translation() = _transTool;
// build the 4x4 transformation matrix from the quaterion and 3-vector (reference target)
Eigen::Transform3f _referenceTrackingMatrix;
_referenceTrackingMatrix.setIdentity();
_referenceTrackingMatrix.rotate( _referenceQuat.toRotationMatrix());
_referenceTrackingMatrix.translation() = _transReference;
Eigen::Transform3f _newToolCoordinates = _referenceTrackingMatrix.inverse() * _toolTrackingMatrix;
Eigen::Quaternionf _newToolQuat(_newToolCoordinates.rotation());
Eigen::Vector3f _newToolTrans(_newToolCoordinates.translation());
/*if ( counter == 889 || counter == 890 )
{
std::cout << "Reference: " << std::endl;
std::cout << _referenceTrackingMatrix.matrix() << std::endl << std::endl;
std::cout << _referenceQuat.w() << ", " << _referenceQuat.x() << ", " << _referenceQuat.y() << ", " << _referenceQuat.z() << std::endl;
std::cout << "USProbe: " << std::endl;
std::cout << _toolTrackingMatrix.matrix() << std::endl << std::endl;
std::cout << _toolQuat.w() << ", " << _toolQuat.x() << ", " << _toolQuat.y() << ", " << _toolQuat.z() << std::endl;
std::cout << "Trans: " << std::endl;
std::cout << _newToolCoordinates.matrix() << std::endl;
std::cout << std::endl;
std::cout << "New Quat: "<< std::endl;
std::cout << _newToolQuat.w() << ", " << _newToolQuat.x() << ", " << _newToolQuat.y() << ", " << _newToolQuat.z() << std::endl;
std::cout << "Back to Rotation: "<< std::endl;
Eigen::Matrix3f _testMat = _newToolQuat.toRotationMatrix();
std::cout << _testMat << std::endl;
std::cout << std::endl;
}*/
TrackingData _td;
_td = _toolTarget;
_td.quaternion[0] = _newToolQuat.w();
_td.quaternion[1] = _newToolQuat.x();
_td.quaternion[2] = _newToolQuat.y();
_td.quaternion[3] = _newToolQuat.z();
_td.translation[0] = _newToolTrans.x();
_td.translation[1] = _newToolTrans.y();
_td.translation[2] = _newToolTrans.z();
trackingVector.push_back(_td);
}
counter++;
}
_file.clear();
return true;
}
bool DataTools::getVideoTimestampsFromFile(std::string filename, std::vector<float> ×tampVector)
{
std::fstream _file;
int _fileLength;
_file.open(filename.c_str() , std::ios::in);
_file.seekg (0, std::ios::end);
_fileLength = _file.tellg();
_file.seekg (0, std::ios::beg);
int counter=0;
while(! ( static_cast<int>(_file.tellg()) == -1 || static_cast<int>(_file.tellg()) >= _fileLength ))
{
counter++;
float tmpTimeStamp;
_file >> tmpTimeStamp;
if( !_file.fail() ) // avoid the empty line at end of file problem
timestampVector.push_back(tmpTimeStamp);
}
return true;
}
/* TJK_removed_CAMP:
void DataTools::eigen2CAMP(const Eigen::Matrix4f &eigMat, CAMP::Matrix4<float> &campMat)
{
for(int i=0;i<16;i++)
{
campMat.c_array()[i] = eigMat(i);
}
campMat = campMat.transpose();
}
void DataTools::eigen2CAMP(const Eigen::Matrix3f &eigMat, CAMP::Matrix3<float> &campMat)
{
for(int i=0;i<9;i++)
{
campMat.c_array()[i] = eigMat(i);
}
campMat = campMat.transpose();
}
*/
/*void DataTools::eigen2CAMP(const Eigen::Matrix4f &eigMat, CAMP::Matrix4<float> &campMat)
{
for(int i=0;i<16;i++)
{
campMat.c_array()[i] = eigMat(i);
}
campMat = campMat.transpose();
}
void DataTools::eigen2CAMP(const Eigen::Matrix3f &eigMat, CAMP::Matrix3<float> &campMat)
{
for(int i=0;i<9;i++)
{
campMat.c_array()[i] = eigMat(i);
}
campMat = campMat.transpose();
}*/
/*
void DataTools::eigen2CAMP(const Eigen::Matrix3f &eigMat, CAMP::Matrix3<float> &campMat)
{
}
void DataTools::eigen2CAMP(const Eigen::Matrix3f &eigMat, CAMP::Matrix3<float> &Mat)
{
}*/