src/tools/lvr2_asciiconverter/Main.cpp
Go to the documentation of this file.
1 
28 #include "Options.hpp"
29 
30 #include "lvr2/io/Timestamp.hpp"
31 #include "lvr2/io/AsciiIO.hpp"
32 #include "lvr2/io/DataStruct.hpp"
33 #include "lvr2/io/ModelFactory.hpp"
34 #include "lvr2/io/Progress.hpp"
35 
36 #include <iostream>
37 #include <fstream>
38 
39 using namespace lvr2;
40 
41 
45 int main(int argc, char** argv)
46 {
47  // Parse command line arguments
48  ascii_convert::Options options(argc, argv);
49 
50  // Exit if options had to generate a usage message
51  // (this means required parameters are missing)
52  if (options.printUsage()) return 0;
53 
54  std::cout << options << std::endl;
55 
56  // Get input and putput files
57  string inputFile = options.inputFile();
58  string outputFile = options.outputFile();
59 
60  // Count entries
61  int numEntries = AsciiIO::getEntriesInLine(inputFile);
62  int numLines = AsciiIO::countLines(inputFile);
63  int numPoints = numLines - 1;
64 
65  if(numPoints <= 0)
66  {
67  std::cout << timestamp << "File contains no points. Exiting." << std::endl;
68  }
69 
70  // Check color and intensity options
71  bool readColor = true;
72  if( (options.r() < 0) || (options.g() < 0) || (options.b() < 0) )
73  {
74  readColor = false;
75  }
76 
77  bool readIntensity = options.i() >= 0;
78  bool convert = options.convertRemission();
79 
80  // Print stats
81  std::cout << timestamp << "Read colors\t\t: " << readColor << std::endl;
82  std::cout << timestamp << "Read intensities\t\t: " << readIntensity << std::endl;
83  std::cout << timestamp << "Convert intensities\t: " << convert << std::endl;
84 
85  // Alloc buffers
86  floatArr points(new float[3 * numPoints]);
88  floatArr intensities;
89 
90  if(readColor || (readIntensity && convert))
91  {
92  colors = ucharArr(new unsigned char[3 * numPoints]);
93  }
94 
95  if(readIntensity)
96  {
97  intensities = floatArr(new float[numPoints]);
98  }
99 
100  // Open input file and skip first line
101 
102  string comment = timestamp.getElapsedTime() + "Reading file " + inputFile;
103  ProgressBar progress(numPoints, comment);
104 
105  std::ifstream in(inputFile.c_str());
106  char buffer[2048];
107  if(in.good())
108  {
109  // Data to store the input data
110  float* data = new float[numEntries];
111  in.getline(buffer, 2048);
112  int c = 0;
113  while(in.good() && c <= numPoints)
114  {
115  // Read a new line
116  for(int i = 0; i < numEntries; i++)
117  {
118  in >> data[i];
119  }
120 
121  // Fill data arrays
122  int posPtr = 3 * c;
123 
124  points[posPtr ] = data[options.x()];
125  points[posPtr + 1] = data[options.y()];
126  points[posPtr + 2] = data[options.z()];
127 
128  points[posPtr ] *= options.sx();
129  points[posPtr + 1] *= options.sy();
130  points[posPtr + 2] *= options.sz();
131 
132  if(convert)
133  {
134  colors[posPtr ] = (unsigned char)data[options.i()];
135  colors[posPtr + 1] = (unsigned char)data[options.i()];
136  colors[posPtr + 2] = (unsigned char)data[options.i()];
137  }
138  else if (readColor)
139  {
140  colors[posPtr ] = (unsigned char)data[options.r()];
141  colors[posPtr + 1] = (unsigned char)data[options.g()];
142  colors[posPtr + 2] = (unsigned char)data[options.b()];
143  }
144 
145  if(readIntensity)
146  {
147  intensities[c] = data[options.i()];
148  }
149  c++;
150  ++progress;
151  }
152  delete[] data;
153 
154  // Create model and save data
155  PointBufferPtr pointBuffer(new PointBuffer );
156  pointBuffer->setPointArray(points, numPoints);
157  pointBuffer->setColorArray(colors, numPoints);
158  pointBuffer->addFloatChannel(intensities, "intensities", numPoints, 1);
159 
160  ModelPtr model( new Model(pointBuffer));
161  ModelFactory::saveModel(model, outputFile);
162  }
163  else
164  {
165  std::cout << "Unable to open file for output: " << inputFile << std::endl;
166  }
167 
168  std::cout << std::endl;
169 
170  return 0;
171 }
172 
A class to handle point information with an arbitrarily large number of attribute channels...
Definition: PointBuffer.hpp:51
float sy()
Returns the scaling factor for the y coordinates.
int y()
Returns the position of the x coordinate in the data.
Datastructures for holding loaded data.
const kaboom::Options * options
bool printUsage() const
Prints a usage message.
int r()
Returns the position of the x coordinate in the data.
static int getEntriesInLine(string filename)
Helper method. Returns the number of columns in the given file.
Definition: AsciiIO.cpp:404
A class to parse the program options for the reconstruction executable.
void convert(COORD_SYSTEM from, COORD_SYSTEM to, float *point)
static Timestamp timestamp
A global time stamp object for program runtime measurement.
Definition: Timestamp.hpp:116
boost::shared_array< unsigned char > ucharArr
Definition: DataStruct.hpp:137
int main(int argc, char **argv)
Main entry point for the LSSR surface executable.
int i()
Returns the position of the x coordinate in the data.
std::shared_ptr< PointBuffer > PointBufferPtr
Read and write pointclouds from .pts and .3d files.
float sz()
Returns the scaling factor for the z coordinates.
int z()
Returns the position of the x coordinate in the data.
static size_t countLines(string filename)
TODO: Coordinate mapping for ascii files.
Definition: AsciiIO.cpp:386
boost::shared_array< float > floatArr
Definition: DataStruct.hpp:133
bool convertRemission() const
If true, intinesites will be converted to colors.
int g()
Returns the position of the x coordinate in the data.
std::shared_ptr< Model > ModelPtr
Definition: Model.hpp:80
string outputFile() const
Retuns the input file.
string inputFile() const
Retuns the input file.
float sx()
Returns the scaling factor for the x coordinates.
string getElapsedTime() const
Returns a string representation of the current timer value.
Definition: Timestamp.cpp:136
int b()
Returns the position of the x coordinate in the data.
static void saveModel(ModelPtr m, std::string file)
int x()
Returns the position of the x coordinate in the data.
char ** argv


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 Mon Feb 28 2022 22:46:08