ModelFactory.cpp
Go to the documentation of this file.
1 
35 #include "lvr2/io/AsciiIO.hpp"
36 #include "lvr2/io/PLYIO.hpp"
37 #include "lvr2/io/UosIO.hpp"
38 #include "lvr2/io/ObjIO.hpp"
39 #include "lvr2/io/LasIO.hpp"
40 #include "lvr2/io/HDF5IO.hpp"
41 #include "lvr2/io/BoctreeIO.hpp"
42 #include "lvr2/io/ModelFactory.hpp"
43 #include "lvr2/io/DatIO.hpp"
44 #include "lvr2/io/STLIO.hpp"
45 #include "lvr2/io/Timestamp.hpp"
46 #include "lvr2/io/Progress.hpp"
47 
48 // PCL related includes
49 #ifdef LVR2_USE_PCL
50 #include "lvr2/io/PCDIO.hpp"
51 #endif
52 
53 // RiVLib
54 #ifdef LVR2_USE_RIVLIB
55 #include "lvr2/io/RxpIO.hpp"
56 #endif
57 
58 #include <boost/filesystem.hpp>
59 
60 namespace lvr2
61 {
62 
63 CoordinateTransform<float> ModelFactory::m_transform;
64 
66 {
67 
68  ModelPtr m(new Model);
69 
70  // Check extension
71  boost::filesystem::path selectedFile( filename );
72  std::string extension = selectedFile.extension().string();
73 
74  // Try to parse given file
75  BaseIO* io = 0;
76  if(extension == ".ply")
77  {
78  io = new PLYIO;
79  }
80  else if(extension == ".pts" || extension == ".3d" || extension == ".xyz" || extension == ".txt")
81  {
82  io = new AsciiIO;
83  }
84 #ifdef LVR2_USE_RIVLIB
85  else if(extension == ".rxp")
86  {
87  io = new RxpIO;
88  }
89 #endif
90  else if (extension == ".obj")
91  {
92  io = new ObjIO;
93  }
94  else if (extension == ".las")
95  {
96  io = new LasIO;
97  }
98  else if (extension ==".dat")
99  {
100  io = new DatIO;
101  }
102  else if (extension ==".h5")
103  {
104  io = new HDF5IO;
105  }
106 #ifdef LVR2_USE_PCL
107  else if (extension == ".pcd")
108  {
109  io = new PCDIO;
110  }
111 #endif /* LVR2_USE_PCL */
112  else if (extension == "")
113  {
114  bool found_3d = false;
115  bool found_boctree = false;
116 
117  // Check for supported data in directory.
118  boost::filesystem::directory_iterator lastFile;
119 
120  for(boost::filesystem::directory_iterator it(filename); it != lastFile; it++ )
121  {
122  boost::filesystem::path p = it->path();
123 
124  // Check for 3d files
125  if(p.extension().string() == ".3d")
126  {
127  // Check for naming convention "scanxxx.3d"
128  int num = 0;
129  if(sscanf(p.filename().string().c_str(), "scan%3d", &num))
130  {
131  found_3d = true;
132  }
133  }
134 
135  // Check for .oct files
136  if(p.extension().string() == ".oct")
137  {
138  // Check for naming convention "scanxxx.3d"
139  int num = 0;
140  if(sscanf(p.filename().string().c_str(), "scan%3d", &num))
141  {
142  found_boctree = true;
143  }
144  }
145 
146 
147  }
148 
149  // Check and create io
150  if(!found_boctree && found_3d)
151  {
152  io = new UosIO;
153  }
154  else if(found_boctree && found_3d)
155  {
156  cout << timestamp << "Found 3d files and octrees. Loading octrees per default." << endl;
157  io = new BoctreeIO;
158  }
159  else if(found_boctree && !found_3d)
160  {
161  io = new BoctreeIO;
162  }
163  else
164  {
165  cout << timestamp << "Given directory does not contain " << endl;
166  }
167  }
168 
169  // Return data model
170  if( io )
171  {
172  m = io->read( filename );
173 
174  if( m_transform.transforms())
175  {
176  // Convert coordinates in model
177  PointBufferPtr points = m->m_pointCloud;
178  size_t n_points = points->numPoints();
179  size_t n_normals = 0;
180  size_t dummy;
181 
182  floatArr p = points->getPointArray();
183  floatArr n = points->getFloatArray("normals", n_normals, dummy);
184 
185  // If normals are present every point should habe one
186  if(n_normals)
187  {
188  assert(n_normals == n_points);
189  }
190 
191  // Convert coordinates
192  float point[3];
193  float normal[3];
194 
195  for(size_t i = 0; i < n_points; i++)
196  {
197  // Re-order and scale point coordinates
198  point[0] = p[3 * i + m_transform.x] * m_transform.sx;
199  point[1] = p[3 * i + m_transform.y] * m_transform.sy;
200  point[2] = p[3 * i + m_transform.z] * m_transform.sz;
201 
202  p[3 * i] = point[0];
203  p[3 * i + 1] = point[1];
204  p[3 * i + 2] = point[2];
205  if(n_normals)
206  {
207  normal[0] = n[3 * i + m_transform.x] * m_transform.sx;
208  normal[1] = n[3 * i + m_transform.y] * m_transform.sy;
209  normal[2] = n[3 * i + m_transform.z] * m_transform.sz;
210 
211  n[3 * i] = normal[0];
212  n[3 * i + 1] = normal[1];
213  n[3 * i + 2] = normal[2];
214  }
215  }
216  }
217 
218  delete io;
219  }
220 
221  return m;
222 
223 }
224 
226 {
227  // Get file exptension
228  boost::filesystem::path selectedFile(filename);
229  std::string extension = selectedFile.extension().string();
230 
231  BaseIO* io = 0;
232 
233  // Create suitable io
234  if(extension == ".ply")
235  {
236  io = new PLYIO;
237  }
238  else if (extension == ".pts" || extension == ".3d" || extension == ".xyz" || extension == ".txt")
239  {
240  io = new AsciiIO;
241  }
242  else if ( extension == ".obj" )
243  {
244  io = new ObjIO;
245  }
246  else if (extension == ".stl")
247  {
248  io = new STLIO;
249  }
250  else if (extension == ".h5")
251  {
252  io = new HDF5IO;
253  }
254 #ifdef LVR2_USE_PCL
255  else if (extension == ".pcd")
256  {
257  io = new PCDIO;
258  }
259 #endif
260 
261  // Save model
262  if(io)
263  {
264  io->save( m, filename );
265  delete io;
266  }
267  else
268  {
269  cout << timestamp << "File format " << extension
270  << " is currently not supported." << endl;
271  }
272 
273 
274 
275 }
276 
277 } // namespace lvr2
lvr2::floatArr
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
lvr2::ModelFactory::saveModel
static void saveModel(ModelPtr m, std::string file)
Definition: ModelFactory.cpp:225
lvr2::CoordinateTransform::z
unsigned char z
Position of the z coordinate in the target system.
Definition: CoordinateTransform.hpp:81
HDF5IO.hpp
HDF5IO
BaseHDF5IO::AddFeatures< lvr2::hdf5features::ScanProjectIO, lvr2::hdf5features::ArrayIO > HDF5IO
Definition: src/tools/lvr2_hdf5_builder_2/Main.cpp:18
AsciiIO.hpp
Read and write pointclouds from .pts and .3d files.
lvr2::PCDIO
A import / export class for point cloud data in the PointCloudLibrary file format.
Definition: PCDIO.hpp:54
ObjIO.hpp
lvr2::PointBufferPtr
std::shared_ptr< PointBuffer > PointBufferPtr
Definition: PointBuffer.hpp:130
RxpIO.hpp
lvr2::BaseIO::read
virtual ModelPtr read(std::string filename)=0
Parse the given file and load supported elements.
lvr2::CoordinateTransform::x
unsigned char x
Position of the x coordinate in the target system.
Definition: CoordinateTransform.hpp:75
BoctreeIO.hpp
p
SharedPointer p
Definition: ConvertShared.hpp:42
lvr2::Model
Definition: Model.hpp:51
STLIO.hpp
LasIO.hpp
lvr2::CoordinateTransform::sy
T sy
Definition: CoordinateTransform.hpp:89
lvr2::UosIO
An input class for laser scans in UOS 3d format.
Definition: UosIO.hpp:74
UosIO.hpp
PCDIO.hpp
Read and write point clouds from PCD files.
lvr2::RxpIO
Reads .rxp files.
Definition: RxpIO.hpp:52
lvr2::CoordinateTransform::y
unsigned char y
Position of the y coordinate in the target system.
Definition: CoordinateTransform.hpp:78
scripts.normalize_multiple.filename
filename
Definition: normalize_multiple.py:60
lvr2::ModelFactory::m_transform
static CoordinateTransform< float > m_transform
Definition: ModelFactory.hpp:66
lvr2::timestamp
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
lvr2::ObjIO
A basic implementation of the obj file format.
Definition: ObjIO.hpp:53
Progress.hpp
DatIO.hpp
lvr2::DatIO
IO class for binary laser scans.
Definition: DatIO.hpp:50
lvr2::CoordinateTransform::transforms
bool transforms()
Definition: CoordinateTransform.hpp:69
lvr2::BoctreeIO
IO-Class to import compressed octrees from slam6d.
Definition: BoctreeIO.hpp:56
lvr2::BaseIO::save
virtual void save(std::string filename)=0
Save the loaded elements to the given file.
lvr2::PLYIO
A class for input and output to ply files.
Definition: PLYIO.hpp:104
lvr2::CoordinateTransform::sx
T sx
Definition: CoordinateTransform.hpp:85
lvr2::AsciiIO
A import / export class for point cloud data in plain text formats. Currently the file extensions ....
Definition: AsciiIO.hpp:55
lvr2
Definition: BaseBufferManipulators.hpp:39
PLYIO.hpp
I/O support for PLY files.
lvr2::ModelPtr
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
Timestamp.hpp
lvr2::ModelFactory::readModel
static ModelPtr readModel(std::string filename)
Definition: ModelFactory.cpp:65
ModelFactory.hpp
lvr2::LasIO
Interface class to read laser scan data in .las-Format.
Definition: LasIO.hpp:46
lvr2::STLIO
Definition: STLIO.hpp:47
lvr2::CoordinateTransform::sz
T sz
Definition: CoordinateTransform.hpp:93
lvr2::BaseIO
Interface specification for low-level io. All read elements are stored in linear arrays.
Definition: BaseIO.hpp:57


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