Main MRPT website > C++ reference for MRPT 1.9.9
CPointsMap_liblas.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef CPOINTSMAP_LIBLAS_H
10 #define CPOINTSMAP_LIBLAS_H
11 
12 /** \file Include this file in your user application only if you have libLAS
13  * installed in your system */
14 #include <liblas/liblas.hpp>
15 #include <liblas/reader.hpp>
16 #include <liblas/writer.hpp>
17 
18 #include <mrpt/maps/CPointsMap.h>
19 #include <string>
20 #include <iostream>
21 #include <fstream>
22 
23 namespace mrpt
24 {
25 /** \ingroup mrpt_maps_grp */
26 namespace maps
27 {
28 /** \addtogroup mrpt_maps_liblas_grp libLAS interface for CPointsMap (in
29  * #include <mrpt/maps/CPointsMaps_liblas.h>)
30  * \ingroup mrpt_maps_grp
31  * @{ */
32 
33 /** Optional settings for saveLASFile() */
35 {
36  // None.
37 };
38 
39 /** Optional settings for loadLASFile() */
41 {
42  // None.
43 };
44 
45 /** Extra information gathered from the LAS file header */
47 {
52  /** Proj.4 string describing the Spatial Reference System. */
54  /** Creation date (Year number) */
56  /** Creation day of year */
58 
60 };
61 
62 /** Save the point cloud as an ASPRS LAS binary file (requires MRPT built
63  * against liblas). Refer to http://www.liblas.org/
64  * \return false on any error */
65 template <class POINTSMAP>
67  const POINTSMAP& ptmap, const std::string& filename,
69 {
70  std::ofstream ofs;
71  ofs.open(filename.c_str(), std::ios::out | std::ios::binary);
72 
73  if (!ofs.is_open())
74  {
75  std::cerr << "[saveLASFile] Couldn't write to file: " << filename
76  << std::endl;
77  return false;
78  }
79 
80  // Fill-in header:
81  // ---------------------------------
82  liblas::Header header;
83  const size_t nPts = ptmap.size();
84 
85  header.SetPointRecordsCount(nPts);
86 
87  // Create writer:
88  // ---------------------------------
89  liblas::Writer writer(ofs, header);
90 
91  const bool has_color = ptmap.hasColorPoints();
92  const float col_fract = 255.0f;
93 
94  liblas::Point pt(&header);
95  liblas::Color col;
96  for (size_t i = 0; i < nPts; i++)
97  {
98  float x, y, z, R, G, B;
99  ptmap.getPoint(i, x, y, z, R, G, B);
100 
101  pt.SetX(x);
102  pt.SetY(y);
103  pt.SetZ(z);
104 
105  if (has_color)
106  {
107  col.SetRed(static_cast<uint16_t>(R * col_fract));
108  col.SetGreen(static_cast<uint16_t>(G * col_fract));
109  col.SetBlue(static_cast<uint16_t>(B * col_fract));
110  pt.SetColor(col);
111  }
112 
113  if (!writer.WritePoint(pt))
114  {
115  std::cerr << "[saveLASFile] liblas returned error writing point #"
116  << i << " to file.\n";
117  return false;
118  }
119  }
120  return true; // All ok.
121 }
122 
123 /** Load the point cloud from an ASPRS LAS binary file (requires MRPT built
124  * against liblas). Refer to http://www.liblas.org/
125  * \note Color (RGB) information will be taken into account if using the
126  * derived class mrpt::maps::CColouredPointsMap
127  * \return false on any error */
128 template <class POINTSMAP>
130  POINTSMAP& ptmap, const std::string& filename,
131  LAS_HeaderInfo& out_headerInfo,
133 {
134  using namespace std;
135  ptmap.clear();
136 
137  std::ifstream ifs;
138  ifs.open(filename.c_str(), std::ios::in | std::ios::binary);
139 
140  if (!ifs.is_open())
141  {
142  std::cerr << "[loadLASFile] Couldn't open file: " << filename
143  << std::endl;
144  return false;
145  }
146 
147  // Create LAS reader:
148  // ---------------------
149  liblas::Reader reader(ifs);
150 
151  // Parse header info:
152  // ---------------------
153  liblas::Header const& header = reader.GetHeader();
154  const size_t nPts = header.GetPointRecordsCount();
155  ptmap.reserve(nPts);
156 
157  out_headerInfo.FileSignature = header.GetFileSignature();
158  out_headerInfo.SystemIdentifier = header.GetSystemId();
159  out_headerInfo.SoftwareIdentifier = header.GetSoftwareId();
160 #if LIBLAS_VERSION_NUM < 1800
161  out_headerInfo.project_guid = header.GetProjectId().to_string();
162 #else
163  out_headerInfo.project_guid =
164  boost::lexical_cast<std::string>(header.GetProjectId());
165 #endif
166  out_headerInfo.spatial_reference_proj4 = header.GetSRS().GetProj4();
167  out_headerInfo.creation_year = header.GetCreationYear();
168  out_headerInfo.creation_DOY = header.GetCreationDOY();
169 
170  // Load points:
171  // ---------------------
172  const bool has_color = ptmap.hasColorPoints();
173  const float col_fract = 1.0f / 255.0f;
174  while (reader.ReadNextPoint())
175  {
176  liblas::Point const& p = reader.GetPoint();
177 
178  if (has_color)
179  {
180  liblas::Color const& col = p.GetColor();
181  ptmap.insertPoint(
182  p.GetX(), p.GetY(), p.GetZ(), col.GetRed() * col_fract,
183  col.GetGreen() * col_fract, col.GetBlue() * col_fract);
184  }
185  else
186  {
187  ptmap.insertPoint(p.GetX(), p.GetY(), p.GetZ());
188  }
189  }
190 
191  if (ptmap.size() != nPts)
192  cerr << "[loadLASFile] Header says point count is " << nPts
193  << " but only " << ptmap.size() << " were really parsed in.\n";
194 
195  ptmap.mark_as_modified();
196 
197  return true; // All ok.
198 }
199 /** @} */
200 }
201 } // End of namespaces
202 
203 #endif
GLdouble GLdouble z
Definition: glext.h:3872
unsigned __int16 uint16_t
Definition: rptypes.h:44
Optional settings for loadLASFile()
const double G
std::string spatial_reference_proj4
Proj.4 string describing the Spatial Reference System.
STL namespace.
uint16_t creation_year
Creation date (Year number)
bool saveLASFile(const POINTSMAP &ptmap, const std::string &filename, const LAS_WriteParams &params=LAS_WriteParams())
Save the point cloud as an ASPRS LAS binary file (requires MRPT built against liblas).
nv_oem6_header_t header
Novatel frame: NV_OEM6_BESTPOS.
bool loadLASFile(POINTSMAP &ptmap, const std::string &filename, LAS_HeaderInfo &out_headerInfo, const LAS_LoadParams &params=LAS_LoadParams())
Load the point cloud from an ASPRS LAS binary file (requires MRPT built against liblas).
GLsizei const GLchar ** string
Definition: glext.h:4101
Optional settings for saveLASFile()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const float R
GLuint in
Definition: glext.h:7274
GLenum GLint GLint y
Definition: glext.h:3538
GLenum GLint x
Definition: glext.h:3538
GLfloat GLfloat p
Definition: glext.h:6305
GLenum const GLfloat * params
Definition: glext.h:3534
Extra information gathered from the LAS file header.
uint16_t creation_DOY
Creation day of year.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019