forked from pyomeca/ezc3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cpp
More file actions
152 lines (129 loc) · 4.51 KB
/
Data.cpp
File metadata and controls
152 lines (129 loc) · 4.51 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
#define EZC3D_API_EXPORTS
///
/// \file Data.cpp
/// \brief Implementation of Data class
/// \author Pariterre
/// \version 1.0
/// \date October 17th, 2018
///
#include "ezc3d/Data.h"
#include "ezc3d/AnalogsInfo.h"
#include "ezc3d/DataStartInfo.h"
#include "ezc3d/Header.h"
#include "ezc3d/Parameters.h"
#include "ezc3d/PointsInfo.h"
#include "ezc3d/RotationsInfo.h"
#include "ezc3d/ezc3d.h"
#include <iostream>
#include <stdexcept>
ezc3d::DataNS::Data::Data() {}
ezc3d::DataNS::Data::Data(ezc3d::c3d &c3d, std::fstream &file) {
// Firstly move the pointer to the data start position
file.seekg(static_cast<int>(c3d.header().dataStart() - 1) * 512,
std::ios::beg);
// Read the data
ezc3d::DataNS::Points3dNS::Info pointsInfo(c3d);
ezc3d::DataNS::AnalogsNS::Info analogsInfo(c3d);
ezc3d::DataNS::RotationNS::Info rotationsInfo(c3d);
size_t nbFrames = c3d.header().nbFrames();
if (nbFrames == 0xFFFF && !c3d.header().hasRotationalData()) {
// This is a special case to account for Vicon files which don't provide the
// actual number of frames in the header nor in the parameters when the
// number of frames is larger than 65535. We need to make sure the
// rotational data are not present in the file after the points and analogs
// data in order to use "all-of-file" (nbFrames = -1) reading
nbFrames = -1;
}
for (size_t j = 0; j < nbFrames; ++j) {
ezc3d::DataNS::Frame f;
// Read point 3d
f.add(ezc3d::DataNS::Points3dNS::Points(c3d, file, pointsInfo));
// Read analogs
f.add(ezc3d::DataNS::AnalogsNS::Analogs(c3d, file, analogsInfo));
// If we ran out of space, then leave. The reason we test here is because
// is set after failing, resulting in one extra frame added if this if
// is after the push_back
if (file.eof())
break;
_frames.push_back(f);
}
// Read the rotation data
if (c3d.header().hasRotationalData()) {
// Prepare the reading
// If the max length of the file is smaller than the data start, then there
// is no data
std::streampos fileSize = file.seekg(0, std::ios::end).tellg();
int targetPos(static_cast<int>(rotationsInfo.dataStart() - 1) * 512);
if (fileSize < targetPos) {
return;
}
file.seekg(targetPos, std::ios::beg);
for (size_t i = 0; i < c3d.header().nbFrames(); ++i) {
if (file.eof())
break;
_frames[i].add(
ezc3d::DataNS::RotationNS::Rotations(c3d, file, rotationsInfo));
}
}
}
ezc3d::DataNS::Data ezc3d::DataNS::Data::clone() const {
Data copy;
for (const auto &frame : _frames)
copy._frames.push_back(frame.clone());
return copy;
}
void ezc3d::DataNS::Data::print() const {
for (size_t i = 0; i < nbFrames(); ++i) {
std::cout << "Frame " << i << "\n";
frame(i).print();
std::cout << "\n";
}
}
void ezc3d::DataNS::Data::write(
const ezc3d::Header &header, std::fstream &f,
const ezc3d::DataNS::Points3dNS::Info &pointsInfo,
const ezc3d::DataNS::AnalogsNS::Info &analogsInfo,
ezc3d::DataStartInfo &dataStartInfoToFill) const {
dataStartInfoToFill.setPointDataStart(f.tellg());
for (size_t i = 0; i < nbFrames(); ++i)
frame(i).write(f, pointsInfo, analogsInfo, 0);
if (header.hasRotationalData()) {
ezc3d::c3d::moveCursorToANewBlock(f);
dataStartInfoToFill.setRotationsDataStart(f.tellg());
for (size_t i = 0; i < nbFrames(); ++i)
frame(i).write(f, pointsInfo, analogsInfo, 1);
}
}
size_t ezc3d::DataNS::Data::nbFrames() const { return _frames.size(); }
const ezc3d::DataNS::Frame &ezc3d::DataNS::Data::frame(size_t idx) const {
try {
return _frames.at(idx);
} catch (const std::out_of_range &) {
throw std::out_of_range(
"Data::frame method is trying to access the frame " +
std::to_string(idx) + " while the maximum number of frame is " +
std::to_string(nbFrames()) + ".");
}
}
ezc3d::DataNS::Frame &ezc3d::DataNS::Data::frame(size_t idx) {
try {
return _frames.at(idx);
} catch (const std::out_of_range &) {
throw std::out_of_range(
"Data::frame method is trying to access the frame " +
std::to_string(idx) + " while the maximum number of frames is " +
std::to_string(nbFrames()) + ".");
}
}
void ezc3d::DataNS::Data::frame(const ezc3d::DataNS::Frame &frame, size_t idx) {
if (idx == SIZE_MAX)
_frames.push_back(frame);
else {
if (idx >= _frames.size())
_frames.resize(idx + 1);
_frames[idx].add(frame);
}
}
const std::vector<ezc3d::DataNS::Frame> &ezc3d::DataNS::Data::frames() const {
return _frames;
}