EZC3D
Data.cpp
Go to the documentation of this file.
1 #define EZC3D_API_EXPORTS
2 
10 #include "Data.h"
11 #include "Header.h"
12 #include "Parameters.h"
13 
15 {
16 
17 }
18 
20 {
21  // Firstly move the pointer to the data start position
22  file.seekg( static_cast<int>(c3d.header().dataStart()-1)*512, std::ios::beg);
23 
24  // Get names of the data
25  std::vector<std::string> pointNames;
26  if (c3d.header().nb3dPoints() > 0)
27  pointNames = c3d.parameters().group("POINT").parameter("LABELS").valuesAsString();
28  std::vector<std::string> analogNames;
29  if (c3d.header().nbAnalogs() > 0)
30  analogNames = c3d.parameters().group("ANALOG").parameter("LABELS").valuesAsString();
31 
32  // Read the data
33  PROCESSOR_TYPE processorType(c3d.parameters().processorType());
34  float pointScaleFactor(c3d.parameters().group("POINT").parameter("SCALE").valuesAsFloat()[0]);
35  std::vector<float> analogScaleFactors(c3d.parameters().group("ANALOG").parameter("SCALE").valuesAsFloat());
36  float analogGeneralFactor(c3d.parameters().group("ANALOG").parameter("GEN_SCALE").valuesAsFloat()[0]);
37  std::vector<int> analogZeroOffset(c3d.parameters().group("ANALOG").parameter("OFFSET").valuesAsInt());
38  for (size_t j = 0; j < c3d.header().nbFrames(); ++j){
39  if (file.eof())
40  break;
41 
43  // Read point 3d
45  for (size_t i = 0; i < c3d.header().nb3dPoints(); ++i){
47  if (c3d.header().scaleFactor() < 0){ // if it is float
48  pt.x(c3d.readFloat(processorType, file));
49  pt.y(c3d.readFloat(processorType, file));
50  pt.z(c3d.readFloat(processorType, file));
51  pt.residual(c3d.readFloat(processorType, file));
52  } else {
53  pt.x(static_cast<float>(c3d.readInt(processorType, file, ezc3d::DATA_TYPE::WORD)) * pointScaleFactor);
54  pt.y(static_cast<float>(c3d.readInt(processorType, file, ezc3d::DATA_TYPE::WORD)) * pointScaleFactor);
55  pt.z(static_cast<float>(c3d.readInt(processorType, file, ezc3d::DATA_TYPE::WORD)) * pointScaleFactor);
56  pt.residual(static_cast<float>(c3d.readInt(processorType, file, ezc3d::DATA_TYPE::WORD)) * pointScaleFactor);
57  }
58  ptsAtAFrame.point(pt, i);
59  }
60  f.add(ptsAtAFrame); // modified by pts_tp which is an nonconst ref to internal points
61 
62  // Read analogs
65  for (size_t k = 0; k < c3d.header().nbAnalogByFrame(); ++k){
67  sub.nbChannels(c3d.header().nbAnalogs());
68  for (size_t i = 0; i < c3d.header().nbAnalogs(); ++i){
70  if (c3d.header().scaleFactor() < 0) // if it is float
71  c.data( (c3d.readFloat(processorType, file) - analogZeroOffset[i]) * analogScaleFactors[i] * analogGeneralFactor );
72  else
73  c.data( (static_cast<float>(c3d.readInt(processorType, file, ezc3d::DATA_TYPE::WORD)) - analogZeroOffset[i]) * analogScaleFactors[i] * analogGeneralFactor ); // * scaleFactor);
74  sub.channel(c, i);
75  }
76  analog.subframe(sub, k);
77  }
78  f.add(analog);
79  _frames.push_back(f);
80  }
81 
82  // remove the trailing empty frames if they exist
83  size_t nFrames(_frames.size());
84  if (nFrames > 0)
85  for (size_t i=0; i<nFrames-1; i--){ // -1 so we at least keep one frame if frames are empty
86  if (_frames.back().isempty())
87  _frames.pop_back();
88  else
89  break;
90  }
91 }
92 
94 {
95  for (size_t i = 0; i < nbFrames(); ++i){
96  std::cout << "Frame " << i << std::endl;
97  frame(i).print();
98  std::cout << std::endl;
99  }
100 }
101 
102 void ezc3d::DataNS::Data::write(std::fstream &f) const
103 {
104  for (size_t i = 0; i < nbFrames(); ++i)
105  frame(i).write(f);
106 }
107 
109 {
110  return _frames.size();
111 }
112 
114 {
115  try {
116  return _frames.at(idx);
117  } catch(std::out_of_range) {
118  throw std::out_of_range("Data::frame method is trying to access the frame "
119  + std::to_string(idx) +
120  " while the maximum number of frame is "
121  + std::to_string(nbFrames()) + ".");
122  }
123 }
124 
126 {
127  try {
128  return _frames.at(idx);
129  } catch(std::out_of_range) {
130  throw std::out_of_range("Data::frame method is trying to access the frame "
131  + std::to_string(idx) +
132  " while the maximum number of frames is "
133  + std::to_string(nbFrames()) + ".");
134  }
135 }
136 
137 void ezc3d::DataNS::Data::frame(const ezc3d::DataNS::Frame &frame, size_t idx)
138 {
139  if (idx == SIZE_MAX)
140  _frames.push_back(frame);
141  else {
142  if (idx >= _frames.size())
143  _frames.resize(idx+1);
144  _frames[idx].add(frame);
145  }
146 }
147 
148 const std::vector<ezc3d::DataNS::Frame> &ezc3d::DataNS::Data::frames() const
149 {
150  return _frames;
151 }
152 
153 
ezc3d::DataNS::Frame & frame_nonConst(size_t idx)
Get the frame of index idx in order to be modified by the caller.
Definition: Data.cpp:125
Analog holder for C3D analogous data.
Definition: Analogs.h:16
const std::vector< int > & valuesAsInt() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:343
float data() const
Get the value of the analog data.
Definition: Channel.cpp:33
size_t nbFrames() const
Get the number of frames.
Definition: Header.cpp:274
Points holder for C3D data 3D points data.
Definition: Points.h:16
void print() const
Print the data.
Definition: Data.cpp:93
const std::vector< ezc3d::DataNS::Frame > & frames() const
Get all the frames from the data set.
Definition: Data.cpp:148
PROCESSOR_TYPE
The type of processor used to store the data.
Definition: ezc3d.h:85
Declaration of Header class.
void add(const ezc3d::DataNS::Frame &frame)
Add a frame by copying a sent frame.
Definition: Frame.cpp:50
Data()
Create a ready to fill Data class.
Definition: Data.cpp:14
Declaration of data class.
float y() const
Get the Y component of the 3D point.
Definition: Point.cpp:59
const ezc3d::DataNS::AnalogsNS::Channel & channel(size_t idx) const
Get a particular analog channel of index idx from the analogous data.
Definition: Subframe.cpp:41
PROCESSOR_TYPE processorType() const
Get the processor type the file was writen on.
Definition: Parameters.cpp:294
const ezc3d::ParametersNS::GroupNS::Group & group(size_t idx) const
Get a particular group of index idx from the group holder.
Definition: Parameters.cpp:312
float scaleFactor() const
Get the scaling factor to convert the 3D point.
Definition: Header.cpp:307
size_t nbFrames() const
Get the number of frames in the data structure.
Definition: Data.cpp:108
size_t nbChannels() const
Get the number of analog channels.
Definition: Subframe.cpp:31
const ezc3d::ParametersNS::GroupNS::Parameter & parameter(size_t idx) const
Get a particular parameter of index idx from the group.
Definition: Group.cpp:143
Subframe for the analogous data.
Definition: Subframe.h:16
const ezc3d::DataNS::AnalogsNS::SubFrame & subframe(size_t idx) const
Get a particular subframe of index idx from the analogous data set.
Definition: Analogs.cpp:43
float residual() const
Get the residual component of the 3D point.
Definition: Point.cpp:80
Channel of an analogous data.
Definition: Channel.h:16
float z() const
Get the Z component of the 3D point.
Definition: Point.cpp:69
const ezc3d::Header & header() const
The header of the C3D.
Definition: ezc3d.cpp:340
const ezc3d::ParametersNS::Parameters & parameters() const
The parameters of the C3D.
Definition: ezc3d.cpp:345
Main class for C3D holder.
Definition: ezc3d.h:154
size_t nbAnalogByFrame() const
Get the number of analog by frame.
Definition: Header.cpp:317
const std::vector< std::string > & valuesAsString() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:357
size_t dataStart() const
Get the number of 256-byte blocks to get to the points and analogous data in the file.
Definition: Header.cpp:312
Frame holder for C3D data.
Definition: Frame.h:17
int readInt(PROCESSOR_TYPE processorType, std::fstream &file, unsigned int nByteToRead, int nByteFromPrevious=0, const std::ios_base::seekdir &pos=std::ios::cur)
Read an integer of nByteToRead bytes at the position current + nByteFromPrevious from a file...
Definition: ezc3d.cpp:179
const std::vector< float > & valuesAsFloat() const
Return the vector of values of the parameter.
Definition: Parameter.cpp:350
const ezc3d::DataNS::Frame & frame(size_t idx) const
Get the frame of index idx.
Definition: Data.cpp:113
size_t nbSubframes() const
Get the number of subframes.
Definition: Analogs.cpp:33
float x() const
Get the X component of the 3D point.
Definition: Point.cpp:49
size_t nb3dPoints() const
Get the number 3D points.
Definition: Header.cpp:246
void write(std::fstream &f) const
Write all the data to an opened file.
Definition: Data.cpp:102
size_t nbAnalogs() const
Get the number of analogs.
Definition: Header.cpp:256
Declaration of Parameters class.
float readFloat(PROCESSOR_TYPE processorType, std::fstream &file, int nByteFromPrevious=0, const std::ios_base::seekdir &pos=std::ios::cur)
Read a float at the position current + nByteFromPrevious from a file.
Definition: ezc3d.cpp:228