BoctreeIO.cpp
Go to the documentation of this file.
1 
35 #include <stdio.h>
36 
37 #include "lvr2/io/BoctreeIO.hpp"
38 #include "lvr2/io/Timestamp.hpp"
39 
40 #include <slam6d/Boctree.h>
41 
42 namespace lvr2
43 {
44 
46 {
47 
48 }
49 
51 {
52 
53 }
54 
55 ModelPtr BoctreeIO::read(string directory )
56 {
57  int numScans = 0;
58  int firstScan = -1;
59  int lastScan = -1;
60 
61  // First, look for .oct files
62  boost::filesystem::directory_iterator lastFile;
63  for(boost::filesystem::directory_iterator it(directory); it != lastFile; it++ )
64  {
65  boost::filesystem::path p = it->path();
66  if(string(p.extension().string().c_str()) == ".oct")
67  {
68  // Check for naming convention "scanxxx.3d"
69  int num = 0;
70  if(sscanf(p.filename().string().c_str(), "scan%3d", &num))
71  {
72  numScans++;
73  if(firstScan == -1) firstScan = num;
74  if(lastScan == -1) lastScan = num;
75 
76  if(num > lastScan) lastScan = num;
77  if(num < firstScan) firstScan = num;
78  }
79  }
80  }
81 
82 
83 
84 
85  list<Point> allPoints;
86  if(numScans)
87  {
88 
89  for(int i = 0; i < numScans; i++)
90  {
91  char scanfile[128];
92  sprintf(scanfile, "%sscan%03d.oct", directory.c_str(), firstScan + i);
93 
94  cout << timestamp << "Reading " << scanfile << endl;
95 
96  Matrix4<Vec> tf;
97  vector<Point> points;
98  BOctTree<float>::deserialize(scanfile, points);
99 
100  // Try to get transformation from .frames file
101  boost::filesystem::path frame_path(
102  boost::filesystem::path(directory) /
103  boost::filesystem::path( "scan" + to_string( firstScan + i, 3 ) + ".frames" ) );
104  string frameFileName = "/" + frame_path.relative_path().string();
105 
106  ifstream frame_in(frameFileName.c_str());
107  if(!frame_in.good())
108  {
109  // Try to parse .pose file
110  boost::filesystem::path pose_path(
111  boost::filesystem::path(directory) /
112  boost::filesystem::path( "scan" + to_string( firstScan + i, 3 ) + ".pose" ) );
113  string poseFileName = "/" + pose_path.relative_path().string();
114 
115  ifstream pose_in(poseFileName.c_str());
116  if(pose_in.good())
117  {
118  float euler[6];
119  for(int i = 0; i < 6; i++) pose_in >> euler[i];
120  Vec position(euler[0], euler[1], euler[2]);
121  Vec angle(euler[3], euler[4], euler[5]);
122  tf = Matrix4<Vec>(position, angle);
123  }
124  else
125  {
126  cout << timestamp << "UOS Reader: Warning: No position information found." << endl;
127  tf = Matrix4<Vec>();
128  }
129 
130  }
131  else
132  {
133  // Use transformation from .frame files
134  tf = parseFrameFile(frame_in);
135 
136  }
137 
138  // Ugly hack to transform scan data
139  for(int j = 0; j < points.size(); j++)
140  {
141  Vec v(points[j].x, points[j].y, points[j].z);
142  v = tf * v;
143  if(v.length() < 10000)
144  {
145  points[j].x = v[0];
146  points[j].y = v[1];
147  points[j].z = v[2];
148  allPoints.push_back(points[j]);
149  }
150 
151  }
152 
153 
154  }
155  }
156 
157  cout << timestamp << "Read " << allPoints.size() << " points." << endl;
158 
159  ModelPtr model( new Model );
160 
161  if(allPoints.size())
162  {
163  floatArr points(new float[3 * allPoints.size()]);
164  ucharArr colors(new unsigned char[3 * allPoints.size()]);
165  floatArr intensities( new float[allPoints.size()]);
166 
167  int i = 0;
168  bool found_color = false;
169  float min_r = 1e10;
170  float max_r = -1e10;
171  for(list<Point>::iterator it = allPoints.begin(); it != allPoints.end(); it++)
172  {
173  Point p = *it;
174 
175  points[3 * i ] = p.x;
176  points[3 * i + 1] = p.y;
177  points[3 * i + 2] = p.z;
178 
179  colors[3 * i ] = p.rgb[0];
180  colors[3 * i + 1] = p.rgb[1];
181  colors[3 * i + 2] = p.rgb[2];
182 
183  if(p.rgb[0] != 255 && p.rgb[1] != 255 && p.rgb[2] != 255)
184  {
185  found_color = true;
186  }
187 
188  intensities[i] = p.reflectance;
189  if(intensities[i] < min_r) min_r = intensities[i];
190  if(intensities[i] > max_r) max_r = intensities[i];
191  //cout << p.reflectance << " " << p.amplitude << " " << p.deviation << endl;
192  i++;
193  }
194 
195 // // Map reflectances to 0..255
196 // float r_diff = max_r - min_r;
197 // if(r_diff > 0)
198 // {
199 // size_t np = allPoints.size();
200 // float b_size = r_diff / 255.0;
201 // for(int a = 0; a < np; a++)
202 // {
203 // float value = intensities[a];
204 // value -= min_r;
205 // value /= b_size;
206 // //cout << value << endl;
207 // intensities[a] = value;
208 // }
209 // }
210 
211  model->m_pointCloud = PointBufferPtr( new PointBuffer );
212  model->m_pointCloud->setPointArray(points, allPoints.size());
213  model->m_pointCloud->setColorArray(colors, allPoints.size());
214  model->m_pointCloud->addFloatChannel(intensities, "intensities", allPoints.size(), 1);
215  }
216 
217  return model;
218 }
219 
220 void BoctreeIO::save( string filename )
221 {
222 
223 }
224 
226 {
227  float m[16], color;
228  while(frameFile.good())
229  {
230  for(int i = 0; i < 16; i++ && frameFile.good()) frameFile >> m[i];
231  frameFile >> color;
232  }
233 
234  return Matrix4<Vec>(m);
235 }
236 
237 } /* namespace lvr2 */
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::Matrix4
A 4x4 matrix class implementation for use with the provided vertex types.
Definition: Matrix4.hpp:64
lvr2::BaseVector< float >
lvr2::BoctreeIO::save
virtual void save(string filename)
Save the loaded elements to the given file.
Definition: BoctreeIO.cpp:220
lvr2::color
Definition: DataStruct.hpp:81
lvr2::PointBufferPtr
std::shared_ptr< PointBuffer > PointBufferPtr
Definition: PointBuffer.hpp:130
BoctreeIO.hpp
p
SharedPointer p
Definition: ConvertShared.hpp:42
lvr2::PointBuffer
A class to handle point information with an arbitrarily large number of attribute channels....
Definition: PointBuffer.hpp:51
lvr2::BaseVector::x
CoordT x
Definition: BaseVector.hpp:65
lvr2::Model
Definition: Model.hpp:51
lvr2::BoctreeIO::~BoctreeIO
virtual ~BoctreeIO()
Definition: BoctreeIO.cpp:50
Boctree.h
Efficient representation of an octree.
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
scripts.create_png.colors
colors
Definition: create_png.py:41
lvr2::BoctreeIO::BoctreeIO
BoctreeIO()
Definition: BoctreeIO.cpp:45
BOctTree::deserialize
void deserialize(std::string filename)
Definition: Boctree.h:433
lvr2::BoctreeIO::read
virtual ModelPtr read(string filename)
Parse the given file and load supported elements.
Definition: BoctreeIO.cpp:55
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
Point
Representation of a point in 3D space.
Definition: point.h:22
lvr2::BoctreeIO::parseFrameFile
Matrix4< Vec > parseFrameFile(ifstream &frameFile)
Definition: BoctreeIO.cpp:225


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:22