LASWriter.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2024, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <rtabmap/core/LASWriter.h>
29 #include <rtabmap/utilite/UFile.h>
31 #include <rtabmap/utilite/UStl.h>
33 
34 #include <fstream>
35 #include <liblas/liblas.hpp>
36 
37 namespace rtabmap {
38 
39 int saveLASFile(const std::string & filePath,
40  const pcl::PointCloud<pcl::PointXYZ> & cloud,
41  const std::vector<int> & cameraIds)
42 {
43  UASSERT_MSG(cameraIds.empty() || cameraIds.size() == cloud.size(),
44  uFormat("cameraIds=%d cloud=%d", (int)cameraIds.size(), (int)cloud.size()).c_str());
45 
46  std::ofstream ofs;
47  ofs.open(filePath, std::ios::out | std::ios::binary);
48 
49  liblas::Header header;
50  header.SetScale(0.001, 0.001, 0.001);
51 
52  header.SetCompressed(uToLowerCase(UFile::getExtension(filePath)) == "laz");
53 
54  try {
55  liblas::Writer writer(ofs, header);
56 
57  for(size_t i=0; i<cloud.size(); ++i)
58  {
59  liblas::Point point(&header);
60  point.SetCoordinates(cloud.at(i).x, cloud.at(i).y, cloud.at(i).z);
61  if(!cameraIds.empty())
62  {
63  point.SetPointSourceID(cameraIds.at(i));
64  }
65 
66  writer.WritePoint(point);
67  }
68  }
69  catch(liblas::configuration_error & e)
70  {
71  UERROR("\"laz\" format not available, use \"las\" instead: %s", e.what());
72  return 1;
73  }
74 
75  return 0; //success
76 }
77 
78 int saveLASFile(const std::string & filePath,
79  const pcl::PointCloud<pcl::PointXYZRGB> & cloud,
80  const std::vector<int> & cameraIds,
81  const std::vector<float> & intensities)
82 {
83  UASSERT_MSG(cameraIds.empty() || cameraIds.size() == cloud.size(),
84  uFormat("cameraIds=%d cloud=%d", (int)cameraIds.size(), (int)cloud.size()).c_str());
85  UASSERT_MSG(intensities.empty() || intensities.size() == cloud.size(),
86  uFormat("intensities=%d cloud=%d", (int)intensities.size(), (int)cloud.size()).c_str());
87 
88  UASSERT_MSG(cameraIds.empty() || cameraIds.size() == cloud.size(),
89  uFormat("cameraIds=%d cloud=%d", (int)cameraIds.size(), (int)cloud.size()).c_str());
90 
91  std::ofstream ofs;
92  ofs.open(filePath, std::ios::out | std::ios::binary);
93 
94  liblas::Header header;
95 
96  header.SetScale(0.001, 0.001, 0.001);
97  header.SetCompressed(uToLowerCase(UFile::getExtension(filePath)) == "laz");
98 
99  try {
100  liblas::Writer writer(ofs, header);
101 
102  for(size_t i=0; i<cloud.size(); ++i)
103  {
104  liblas::Point point(&header);
105  point.SetCoordinates(cloud.at(i).x, cloud.at(i).y, cloud.at(i).z);
106  liblas::Color color(cloud.at(i).r*65535/255, cloud.at(i).g*65535/255, cloud.at(i).b*65535/255);
107  point.SetColor(color);
108  if(!cameraIds.empty())
109  {
110  point.SetPointSourceID(cameraIds.at(i));
111  }
112  if(!intensities.empty())
113  {
114  point.SetIntensity(intensities.at(i));
115  }
116 
117  writer.WritePoint(point);
118  }
119  }
120  catch(liblas::configuration_error & e)
121  {
122  UERROR("\"laz\" format not available, use \"las\" instead: %s", e.what());
123  return 1;
124  }
125 
126  return 0; //success
127 }
128 
129 int saveLASFile(const std::string & filePath,
130  const pcl::PointCloud<pcl::PointXYZI> & cloud,
131  const std::vector<int> & cameraIds)
132 {
133  UASSERT_MSG(cameraIds.empty() || cameraIds.size() == cloud.size(),
134  uFormat("cameraIds=%d cloud=%d", (int)cameraIds.size(), (int)cloud.size()).c_str());
135 
136  std::ofstream ofs;
137  ofs.open(filePath, std::ios::out | std::ios::binary);
138 
139  liblas::Header header;
140  header.SetScale(0.001, 0.001, 0.001);
141 
142  header.SetCompressed(uToLowerCase(UFile::getExtension(filePath)) == "laz");
143 
144  try {
145  liblas::Writer writer(ofs, header);
146 
147  for(size_t i=0; i<cloud.size(); ++i)
148  {
149  liblas::Point point(&header);
150  point.SetCoordinates(cloud.at(i).x, cloud.at(i).y, cloud.at(i).z);
151  if(!cameraIds.empty())
152  {
153  point.SetPointSourceID(cameraIds.at(i));
154  }
155 
156  writer.WritePoint(point);
157  }
158  }
159  catch(liblas::configuration_error & e)
160  {
161  UERROR("\"laz\" format not available, use \"las\" instead: %s", e.what());
162  return 1;
163  }
164 
165  return 0; //success
166 }
167 
168 }
169 
uToLowerCase
std::string UTILITE_EXPORT uToLowerCase(const std::string &str)
Definition: UConversion.cpp:77
LASWriter.h
point
point
UConversion.h
Some conversion functions.
UFile::getExtension
std::string getExtension()
Definition: UFile.h:140
rtabmap::saveLASFile
int saveLASFile(const std::string &filePath, const pcl::PointCloud< pcl::PointXYZ > &cloud, const std::vector< int > &cameraIds=std::vector< int >())
Definition: LASWriter.cpp:39
UASSERT_MSG
#define UASSERT_MSG(condition, msg_str)
Definition: ULogger.h:67
uFormat
std::string UTILITE_EXPORT uFormat(const char *fmt,...)
Definition: UConversion.cpp:365
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
ULogger.h
ULogger class and convenient macros.
header
std_msgs::Header const * header(const M &m)
UStl.h
Wrappers of STL for convenient functions.
UFile.h
rtabmap
Definition: CameraARCore.cpp:35
UERROR
#define UERROR(...)
i
int i


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sun Dec 1 2024 03:42:47