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