Main MRPT website > C++ reference for MRPT 1.5.7
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-2017, 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 installed in your system */
13 #include <liblas/liblas.hpp>
14 #include <liblas/reader.hpp>
15 #include <liblas/writer.hpp>
16 
17 #include <mrpt/maps/CPointsMap.h>
18 #include <string>
19 #include <iostream>
20 #include <fstream>
21 
22 namespace mrpt
23 {
24 /** \ingroup mrpt_maps_grp */
25 namespace maps
26 {
27  /** \addtogroup mrpt_maps_liblas_grp libLAS interface for CPointsMap (in #include <mrpt/maps/CPointsMaps_liblas.h>)
28  * \ingroup mrpt_maps_grp
29  * @{ */
30 
31  /** Optional settings for saveLASFile() */
33  {
34  // None.
35  };
36 
37  /** Optional settings for loadLASFile() */
39  {
40  // None.
41  };
42 
43  /** Extra information gathered from the LAS file header */
45  {
50  std::string spatial_reference_proj4; //!< Proj.4 string describing the Spatial Reference System.
51  uint16_t creation_year;//!< Creation date (Year number)
52  uint16_t creation_DOY; //!< Creation day of year
53 
55  {}
56  };
57 
58  /** Save the point cloud as an ASPRS LAS binary file (requires MRPT built against liblas). Refer to http://www.liblas.org/
59  * \return false on any error */
60  template <class POINTSMAP>
62  const POINTSMAP &ptmap,
63  const std::string &filename,
65  {
66  std::ofstream ofs;
67  ofs.open(filename.c_str(), std::ios::out | std::ios::binary);
68 
69  if (!ofs.is_open())
70  {
71  std::cerr << "[saveLASFile] Couldn't write to file: " << filename << std::endl;
72  return false;
73  }
74 
75  // Fill-in header:
76  // ---------------------------------
77  liblas::Header header;
78  const size_t nPts = ptmap.size();
79 
80  header.SetPointRecordsCount(nPts);
81 
82  // Create writer:
83  // ---------------------------------
84  liblas::Writer writer(ofs,header);
85 
86  const bool has_color = ptmap.hasColorPoints();
87  const float col_fract = 255.0f;
88 
89  liblas::Point pt(&header);
90  liblas::Color col;
91  for (size_t i=0;i<nPts;i++)
92  {
93  float x,y,z,R,G,B;
94  ptmap.getPoint(i, x,y,z, R,G,B);
95 
96  pt.SetX(x);
97  pt.SetY(y);
98  pt.SetZ(z);
99 
100  if (has_color)
101  {
102  col.SetRed( static_cast<uint16_t>(R*col_fract) );
103  col.SetGreen( static_cast<uint16_t>(G*col_fract) );
104  col.SetBlue( static_cast<uint16_t>(B*col_fract) );
105  pt.SetColor(col);
106  }
107 
108  if (!writer.WritePoint(pt))
109  {
110  std::cerr << "[saveLASFile] liblas returned error writing point #" << i << " to file.\n";
111  return false;
112  }
113  }
114  return true; // All ok.
115  }
116 
117  /** Load the point cloud from an ASPRS LAS binary file (requires MRPT built against liblas). Refer to http://www.liblas.org/
118  * \note Color (RGB) information will be taken into account if using the derived class mrpt::maps::CColouredPointsMap
119  * \return false on any error */
120  template <class POINTSMAP>
122  POINTSMAP &ptmap,
123  const std::string &filename,
124  LAS_HeaderInfo &out_headerInfo,
126  {
127  using namespace std;
128  ptmap.clear();
129 
130  std::ifstream ifs;
131  ifs.open(filename.c_str(), std::ios::in | std::ios::binary);
132 
133  if (!ifs.is_open())
134  {
135  std::cerr << "[loadLASFile] Couldn't open file: " << filename << std::endl;
136  return false;
137  }
138 
139  // Create LAS reader:
140  // ---------------------
141  liblas::Reader reader(ifs);
142 
143  // Parse header info:
144  // ---------------------
145  liblas::Header const& header = reader.GetHeader();
146  const size_t nPts = header.GetPointRecordsCount();
147  ptmap.reserve(nPts);
148 
149  out_headerInfo.FileSignature = header.GetFileSignature();
150  out_headerInfo.SystemIdentifier = header.GetSystemId();
151  out_headerInfo.SoftwareIdentifier = header.GetSoftwareId();
152 #if LIBLAS_VERSION_NUM < 1800
153  out_headerInfo.project_guid = header.GetProjectId().to_string();
154 #else
155  out_headerInfo.project_guid = boost::lexical_cast<std::string>(header.GetProjectId());
156 #endif
157  out_headerInfo.spatial_reference_proj4 = header.GetSRS().GetProj4();
158  out_headerInfo.creation_year = header.GetCreationYear();
159  out_headerInfo.creation_DOY = header.GetCreationDOY();
160 
161  // Load points:
162  // ---------------------
163  const bool has_color = ptmap.hasColorPoints();
164  const float col_fract = 1.0f/255.0f;
165  while (reader.ReadNextPoint())
166  {
167  liblas::Point const& p = reader.GetPoint();
168 
169  if (has_color)
170  {
171  liblas::Color const& col = p.GetColor();
172  ptmap.insertPoint( p.GetX(),p.GetY(),p.GetZ(), col.GetRed()*col_fract,col.GetGreen()*col_fract,col.GetBlue()*col_fract );
173  }
174  else
175  {
176  ptmap.insertPoint( p.GetX(),p.GetY(),p.GetZ() );
177  }
178  }
179 
180  if (ptmap.size()!=nPts)
181  cerr << "[loadLASFile] Header says point count is " << nPts << " but only " << ptmap.size() << " were really parsed in.\n";
182 
183  ptmap.mark_as_modified();
184 
185  return true; // All ok.
186  }
187  /** @} */
188 }
189 } // End of namespaces
190 
191 #endif
GLdouble GLdouble z
Definition: glext.h:3734
unsigned __int16 uint16_t
Definition: rptypes.h:46
Optional settings for loadLASFile()
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:3919
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:6301
GLenum GLint GLint y
Definition: glext.h:3516
GLenum GLint x
Definition: glext.h:3516
GLfloat GLfloat p
Definition: glext.h:5587
GLenum const GLfloat * params
Definition: glext.h:3514
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.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019