DatIO.cpp
Go to the documentation of this file.
1 
35 #include "lvr2/io/DatIO.hpp"
36 #include "lvr2/io/Timestamp.hpp"
37 #include "lvr2/io/Progress.hpp"
38 
39 #include <boost/filesystem.hpp>
40 #include <iostream>
41 #include <fstream>
42 #include <vector>
43 
44 using std::vector;
45 using std::ifstream;
46 using std::ofstream;
47 using std::cout;
48 using std::endl;
49 
50 namespace lvr2
51 {
52 
54 {
55 
56 }
57 
59 {
60 
61 }
62 
64 {
65  return read(filename, 4);
66 }
67 
68 ModelPtr DatIO::read(string filename, int n, int reduction)
69 {
70  ModelPtr model( new Model);
71  PointBufferPtr pointBuffer(new PointBuffer);
72 
73  // Allocate point buffer and read data from file
74  int c = 0;
75  ifstream in(filename.c_str(), std::ios::binary);
76 
77 
78  int numPoints = 0;
79 
80  in.read((char*)&numPoints, sizeof(int));
81 
82  // Determine modulo value suitable for desired reduction
83  int mod_filter = 1;
84  if(reduction != 0 && numPoints > reduction)
85  {
86  mod_filter = (int)(numPoints / reduction);
87  cout << timestamp << "Reduction mode. Reading every " << mod_filter << "th point." << endl;
88  numPoints = reduction;
89  }
90  else
91  {
92  reduction = numPoints; // needed for progress estimation
93  }
94 
95 
96 
97  float* pointArray = new float[3 * numPoints];
98  unsigned char* colorArray = new unsigned char[3 * numPoints];
99  floatArr intensityArray;
100 
101  if (n > 3)
102  {
103  intensityArray = floatArr( new float[numPoints] );
104  }
105 
106  float* buffer = new float[n-1];
107  int reflect;
108 
109  string msg = timestamp.getElapsedTime() + "Loading points";
110  ProgressBar progress(numPoints, msg);
111  int d = 0;
112  while(in.good())
113  {
114  memset(buffer, 0, (n - 1) * sizeof(float));
115 
116  in.read((char*)buffer, (n - 1) * sizeof(float));
117 
118  // should only be read if n > 3...
119  if (n > 3)
120  {
121  in.read((char*)&reflect, sizeof(int));
122  }
123 
124  if(c % mod_filter == 0 && d < numPoints)
125  {
126  // Copy point data to point array
127  int pos = 3 * d;
128  pointArray[pos] = buffer[0];
129  pointArray[pos + 1] = buffer[1];
130  pointArray[pos + 2] = buffer[2];
131 
132  if(n > 3)
133  {
134  intensityArray[d] = (float) reflect;
135 
136  colorArray[pos] = (unsigned char)reflect;
137  colorArray[pos + 1] = (unsigned char)reflect;
138  colorArray[pos + 2] = (unsigned char)reflect;
139  }
140  else
141  {
142  colorArray[pos] = (unsigned char)0;
143  colorArray[pos + 1] = (unsigned char)0;
144  colorArray[pos + 2] = (unsigned char)0;
145  }
146  d++;
147  ++progress;
148  }
149  c++;
150  }
151  delete[] buffer;
152  in.close();
153 
154  // Truncate arrays to actual size
155  pointArray = (float*)realloc(pointArray, 3 * d * sizeof(float));
156  colorArray = (unsigned char*)realloc(colorArray, 3 * d * sizeof(unsigned char));
157 
158  cout << timestamp << "Creating point buffer with " << d << "points." << endl;
159 
160  // Setup model pointer
161  floatArr parr(pointArray);
162  ucharArr carr(colorArray);
163  pointBuffer->setPointArray(parr, d);
164  pointBuffer->setColorArray(carr, d);
165  pointBuffer->addFloatChannel(intensityArray, "intensities", d, 1);
166 
167  model->m_pointCloud = pointBuffer;
168  return model;
169 
170 }
171 
172 void DatIO::save(ModelPtr ptr, string filename)
173 {
174  m_model = ptr;
175  save(filename);
176 }
177 
178 void DatIO::save(string filename)
179 {
180  PointBufferPtr pointBuffer = m_model->m_pointCloud;
181  float buffer[4];
182  if(pointBuffer)
183  {
184  ofstream out(filename.c_str(), std::ios::binary);
185  if(out.good())
186  {
187  size_t numPoints = pointBuffer->numPoints();
188  size_t numIntensities;
189  size_t w_intensities;
190  floatArr pointArray = pointBuffer->getPointArray();
191  floatArr intensityArray = pointBuffer->getFloatArray("intensities", numIntensities, w_intensities);
192  float buffer[4];
193  cout << timestamp << "Writing " << numPoints << " to " << filename << endl;
194  for(size_t i = 0; i < numPoints; i++)
195  {
196  memset(buffer, 0, 4 * sizeof(float));
197  size_t pos = i * 3;
198  buffer[0] = pointArray[pos];
199  buffer[1] = pointArray[pos + 1];
200  buffer[2] = pointArray[pos + 2];
201  if(intensityArray)
202  {
203  buffer[3] = intensityArray[i * w_intensities];
204  }
205  out.write((char*)buffer, 4 * sizeof(float));
206  }
207  out.close();
208  }
209  else
210  {
211  cout << timestamp << "DatIO: Unable to open file " << filename << " for writing." << endl;
212  }
213  }
214 }
215 
216 } // namespace lvr2
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::ProgressBar
Definition: Progress.hpp:68
lvr2::PointBufferPtr
std::shared_ptr< PointBuffer > PointBufferPtr
Definition: PointBuffer.hpp:130
lvr2::PointBuffer
A class to handle point information with an arbitrarily large number of attribute channels....
Definition: PointBuffer.hpp:51
lvr2::Timestamp::getElapsedTime
string getElapsedTime() const
Returns a string representation of the current timer value.
Definition: Timestamp.cpp:136
lvr2::DatIO::~DatIO
virtual ~DatIO()
Definition: DatIO.cpp:58
lvr2::Model
Definition: Model.hpp:51
lvr2::DatIO::DatIO
DatIO()
Definition: DatIO.cpp:53
scripts.normalize_multiple.filename
filename
Definition: normalize_multiple.py:60
lvr2::timestamp
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
Progress.hpp
DatIO.hpp
lvr2::DatIO::save
virtual void save(ModelPtr model, string filename)
Definition: DatIO.cpp:172
lvr2::DatIO::read
virtual ModelPtr read(string filename, int n, int reduction=0)
Definition: DatIO.cpp:68
lvr2::BaseIO::m_model
ModelPtr m_model
Definition: BaseIO.hpp:104
lvr2::ucharArr
boost::shared_array< unsigned char > ucharArr
Definition: DataStruct.hpp:137
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::ModelPtr
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
Timestamp.hpp


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23