File: LAS.h

package info (click to toggle)
cgal 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (414 lines) | stat: -rw-r--r-- 17,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) 2016 GeometryFactory (France).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1.1/Point_set_3/include/CGAL/Point_set_3/IO/LAS.h $
// $Id: include/CGAL/Point_set_3/IO/LAS.h 08b27d3db14 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Simon Giraudot

#ifndef CGAL_POINT_SET_IO_LAS_H
#define CGAL_POINT_SET_IO_LAS_H

#include <CGAL/license/Point_set_3.h>

#ifdef CGAL_LINKED_WITH_LASLIB
#include <CGAL/IO/read_las_points.h>
#include <CGAL/IO/write_las_points.h>
#endif // LAS

#include <fstream>
#include <string>

#if defined(CGAL_LINKED_WITH_LASLIB) || defined(DOXYGEN_RUNNING)

namespace CGAL {

template <typename Point, typename Vector>
class Point_set_3;

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Read

namespace IO {

namespace internal {

template <typename PointSet, typename PropertyMap>
void check_if_property_is_used(PointSet& point_set,
                               PropertyMap& map)
{
  for(typename PointSet::iterator it = point_set.begin(); it != point_set.end(); ++it)
    if(get(map, *it) != typename PropertyMap::value_type())
      return;

  point_set.remove_property_map(map);
}

} // namespace internal

/*!
  \ingroup PkgPointSet3IOLAS

  \brief reads the content of an input stream in the \ref IOStreamLAS into a point set.

  \attention To read a binary file, the flag `std::ios::binary` must be set during the creation of the `ifstream`.

  \param is the input stream
  \param point_set the point set

  \note All LAS properties are read as described in `read_LAS_with_properties()`.

  \return `true` if the reading was successful, `false` otherwise.
 */
template <typename Point, typename Vector>
bool read_LAS(std::istream& is,
              CGAL::Point_set_3<Point, Vector>& point_set)
{
  if(!is)
  {
    std::cerr << "Error: cannot open file" << std::endl;
    return false;
  }

  typedef CGAL::Point_set_3<Point, Vector> Point_set;
  typedef typename Point_set::template Property_map<float> Float_map;
  typedef typename Point_set::template Property_map<double> Double_map;
  typedef typename Point_set::template Property_map<unsigned short> Ushort_map;
  typedef typename Point_set::template Property_map<unsigned char> Uchar_map;
  typedef typename Point_set::template Property_map<unsigned int> Uint_map;

  Ushort_map intensity = point_set.template add_property_map<unsigned short>("intensity", 0).first;
  Uchar_map return_number = point_set.template add_property_map<unsigned char>("return_number", 0).first;
  Uchar_map number_of_returns = point_set.template add_property_map<unsigned char>("number_of_returns", 0).first;
  Uchar_map scan_direction_flag = point_set.template add_property_map<unsigned char>("scan_direction_flag", 0).first;
  Uchar_map edge_of_flight_line = point_set.template add_property_map<unsigned char>("edge_of_flight_line", 0).first;
  Uchar_map classification = point_set.template add_property_map<unsigned char>("classification", 0).first;
  Uchar_map synthetic_flag = point_set.template add_property_map<unsigned char>("synthetic_flag", 0).first;
  Uchar_map keypoint_flag = point_set.template add_property_map<unsigned char>("keypoint_flag", 0).first;
  Uchar_map withheld_flag = point_set.template add_property_map<unsigned char>("withheld_flag", 0).first;
  Float_map scan_angle = point_set.template add_property_map<float>("scan_angle", 0.).first;
  Uchar_map user_data = point_set.template add_property_map<unsigned char>("user_data", 0).first;
  Ushort_map point_source_ID = point_set.template add_property_map<unsigned short>("point_source_ID", 0).first;
  Uint_map deleted_flag = point_set.template add_property_map<unsigned int>("deleted_flag", 0).first;
  Double_map gps_time = point_set.template add_property_map<double>("gps_time", 0).first;
  Ushort_map R = point_set.template add_property_map<unsigned short>("R", 0).first;
  Ushort_map G = point_set.template add_property_map<unsigned short>("G", 0).first;
  Ushort_map B = point_set.template add_property_map<unsigned short>("B", 0).first;
  Ushort_map I = point_set.template add_property_map<unsigned short>("I", 0).first;

  bool okay
      = read_LAS_with_properties
      (is, point_set.index_back_inserter(),
       make_las_point_reader(point_set.point_push_map()),
       std::make_pair(point_set.push_property_map(intensity), LAS_property::Intensity()),
       std::make_pair(point_set.push_property_map(return_number), LAS_property::Return_number()),
       std::make_pair(point_set.push_property_map(number_of_returns), LAS_property::Number_of_returns()),
       std::make_pair(point_set.push_property_map(scan_direction_flag), LAS_property::Scan_direction_flag()),
       std::make_pair(point_set.push_property_map(edge_of_flight_line), LAS_property::Edge_of_flight_line()),
       std::make_pair(point_set.push_property_map(classification), LAS_property::Classification()),
       std::make_pair(point_set.push_property_map(synthetic_flag), LAS_property::Synthetic_flag()),
       std::make_pair(point_set.push_property_map(keypoint_flag), LAS_property::Keypoint_flag()),
       std::make_pair(point_set.push_property_map(withheld_flag), LAS_property::Withheld_flag()),
       std::make_pair(point_set.push_property_map(scan_angle), LAS_property::Scan_angle()),
       std::make_pair(point_set.push_property_map(user_data), LAS_property::User_data()),
       std::make_pair(point_set.push_property_map(point_source_ID), LAS_property::Point_source_ID()),
       std::make_pair(point_set.push_property_map(deleted_flag), LAS_property::Deleted_flag()),
       std::make_pair(point_set.push_property_map(gps_time), LAS_property::GPS_time()),
       std::make_pair(point_set.push_property_map(R), LAS_property::R()),
       std::make_pair(point_set.push_property_map(G), LAS_property::G()),
       std::make_pair(point_set.push_property_map(B), LAS_property::B()),
       std::make_pair(point_set.push_property_map(I), LAS_property::I()));

  internal::check_if_property_is_used(point_set, intensity);
  internal::check_if_property_is_used(point_set, return_number);
  internal::check_if_property_is_used(point_set, number_of_returns);
  internal::check_if_property_is_used(point_set, scan_direction_flag);
  internal::check_if_property_is_used(point_set, edge_of_flight_line);
  internal::check_if_property_is_used(point_set, classification);
  internal::check_if_property_is_used(point_set, synthetic_flag);
  internal::check_if_property_is_used(point_set, keypoint_flag);
  internal::check_if_property_is_used(point_set, withheld_flag);
  internal::check_if_property_is_used(point_set, scan_angle);
  internal::check_if_property_is_used(point_set, user_data);
  internal::check_if_property_is_used(point_set, point_source_ID);
  internal::check_if_property_is_used(point_set, deleted_flag);
  internal::check_if_property_is_used(point_set, gps_time);
  internal::check_if_property_is_used(point_set, R);
  internal::check_if_property_is_used(point_set, G);
  internal::check_if_property_is_used(point_set, B);
  internal::check_if_property_is_used(point_set, I);

  return okay;
}

/*!
  \ingroup PkgPointSet3IOLAS

  \brief reads the content of an input file in the \ref IOStreamLAS into a point set.

  \param fname the path to the input file
  \param point_set the point set

  \note All LAS properties are read as described in `read_LAS_with_properties()`.

  \return `true` if the reading was successful, `false` otherwise.
*/
template <typename Point, typename Vector>
bool read_LAS(const std::string& fname, CGAL::Point_set_3<Point, Vector>& point_set)
{
  std::ifstream is(fname, std::ios::binary);
  CGAL::IO::set_mode(is, CGAL::IO::BINARY);
  return read_LAS(is, point_set);
}

} // namespace IO

#ifndef CGAL_NO_DEPRECATED_CODE

template <typename Point, typename Vector>
CGAL_DEPRECATED bool read_las_point_set(std::istream& is, ///< input stream.
                                        CGAL::Point_set_3<Point, Vector>& point_set) ///< point set
{
  return IO::read_LAS(is, point_set);
}

#endif // CGAL_NO_DEPRECATED_CODE

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Write

namespace IO {

/*!
  \ingroup PkgPointSet3IOLAS

  \brief writes the content of a point set into an output stream in the \ref IOStreamLAS.

  \attention To write to a binary file, the flag `std::ios::binary` must be set during the creation of the `ofstream`.

  \tparam Point the point type of the `Point_set_3`
  \tparam Vector the vector type of the `Point_set_3`

  \param os the output stream
  \param point_set the point set

  \note All LAS properties are written as described in `read_LAS_with_properties()`.

  \return `true` if the writing was successful, `false` otherwise.
 */
template <typename Point, typename Vector>
bool write_LAS(std::ostream& os,
               CGAL::Point_set_3<Point, Vector>& point_set)
{
  if(!os)
  {
    std::cerr << "Error: cannot open file" << std::endl;
    return false;
  }

  typedef CGAL::Point_set_3<Point, Vector> Point_set;
  typedef typename Point_set::template Property_map<float> Float_map;
  typedef typename Point_set::template Property_map<double> Double_map;
  typedef typename Point_set::template Property_map<unsigned short> Ushort_map;
  typedef typename Point_set::template Property_map<unsigned char> Uchar_map;
  typedef typename Point_set::template Property_map<unsigned int> Uint_map;

  Ushort_map intensity;
  bool remove_intensity;
  std::tie(intensity, remove_intensity)
      = point_set.template add_property_map<unsigned short>("intensity", 0);

  Uchar_map return_number;
  bool remove_return_number;
  std::tie(return_number, remove_return_number)
      = point_set.template add_property_map<unsigned char>("return_number", 0);

  Uchar_map number_of_returns;
  bool remove_number_of_returns;
  std::tie(number_of_returns, remove_number_of_returns)
      = point_set.template add_property_map<unsigned char>("number_of_returns", 0);

  Uchar_map scan_direction_flag;
  bool remove_scan_direction_flag;
  std::tie(scan_direction_flag, remove_scan_direction_flag)
      = point_set.template add_property_map<unsigned char>("scan_direction_flag", 0);

  Uchar_map edge_of_flight_line;
  bool remove_edge_of_flight_line;
  std::tie(edge_of_flight_line, remove_edge_of_flight_line)
      = point_set.template add_property_map<unsigned char>("edge_of_flight_line", 0);

  Uchar_map classification;
  bool remove_classification;
  std::tie(classification, remove_classification)
      = point_set.template add_property_map<unsigned char>("classification", 0);

  Uchar_map synthetic_flag;
  bool remove_synthetic_flag;
  std::tie(synthetic_flag, remove_synthetic_flag)
      = point_set.template add_property_map<unsigned char>("synthetic_flag", 0);

  Uchar_map keypoint_flag;
  bool remove_keypoint_flag;
  std::tie(keypoint_flag, remove_keypoint_flag)
      = point_set.template add_property_map<unsigned char>("keypoint_flag", 0);

  Uchar_map withheld_flag;
  bool remove_withheld_flag;
  std::tie(withheld_flag, remove_withheld_flag)
      = point_set.template add_property_map<unsigned char>("withheld_flag", 0);

  Float_map scan_angle;
  bool remove_scan_angle;
  std::tie(scan_angle, remove_scan_angle)
      = point_set.template add_property_map<float>("scan_angle", 0.);

  Uchar_map user_data;
  bool remove_user_data;
  std::tie(user_data, remove_user_data)
      = point_set.template add_property_map<unsigned char>("user_data", 0);

  Ushort_map point_source_ID;
  bool remove_point_source_ID;
  std::tie(point_source_ID, remove_point_source_ID)
      = point_set.template add_property_map<unsigned short>("point_source_ID", 0);

  Uint_map deleted_flag;
  bool remove_deleted_flag;
  std::tie(deleted_flag, remove_deleted_flag)
      = point_set.template add_property_map<unsigned int>("deleted_flag", 0);

  Double_map gps_time;
  bool remove_gps_time;
  std::tie(gps_time, remove_gps_time)
      = point_set.template add_property_map<double>("gps_time", 0);

  Ushort_map R;
  bool remove_R;
  std::tie(R, remove_R) = point_set.template add_property_map<unsigned short>("R", 0);
  Ushort_map G;
  bool remove_G;
  std::tie(G, remove_G) = point_set.template add_property_map<unsigned short>("G", 0);
  Ushort_map B;
  bool remove_B;
  std::tie(B, remove_B) = point_set.template add_property_map<unsigned short>("B", 0);
  Ushort_map I;
  bool remove_I;
  std::tie(I, remove_I) = point_set.template add_property_map<unsigned short>("I", 0);

  if(remove_R)
  {
    std::optional<Uchar_map> charR, charG, charB;

    charR = point_set.template property_map<unsigned char>("r");
    if(!charR.has_value())
      charR = point_set.template property_map<unsigned char>("red");
    charG = point_set.template property_map<unsigned char>("g");
    if(!charG.has_value())
      charG = point_set.template property_map<unsigned char>("green");
    charB = point_set.template property_map<unsigned char>("b");
    if(!charB.has_value())
      charB = point_set.template property_map<unsigned char>("blue");

    if(charR.has_value() && charG.has_value() && charB.has_value())
    {
      for(typename Point_set::iterator it = point_set.begin(); it != point_set.end(); ++it)
      {
        put(R, *it, (unsigned short)(get(charR.value(), *it)));
        put(G, *it, (unsigned short)(get(charG.value(), *it)));
        put(B, *it, (unsigned short)(get(charB.value(), *it)));
      }
    }
  }

  bool okay
      = write_LAS_with_properties
      (os, point_set,
       make_las_point_writer(point_set.point_map()),
       std::make_pair(intensity, LAS_property::Intensity()),
       std::make_pair(return_number, LAS_property::Return_number()),
       std::make_pair(number_of_returns, LAS_property::Number_of_returns()),
       std::make_pair(scan_direction_flag, LAS_property::Scan_direction_flag()),
       std::make_pair(edge_of_flight_line, LAS_property::Edge_of_flight_line()),
       std::make_pair(classification, LAS_property::Classification()),
       std::make_pair(synthetic_flag, LAS_property::Synthetic_flag()),
       std::make_pair(keypoint_flag, LAS_property::Keypoint_flag()),
       std::make_pair(withheld_flag, LAS_property::Withheld_flag()),
       std::make_pair(scan_angle, LAS_property::Scan_angle()),
       std::make_pair(user_data, LAS_property::User_data()),
       std::make_pair(point_source_ID, LAS_property::Point_source_ID()),
       std::make_pair(deleted_flag, LAS_property::Deleted_flag()),
       std::make_pair(gps_time, LAS_property::GPS_time()),
       std::make_pair(R, LAS_property::R()),
       std::make_pair(G, LAS_property::G()),
       std::make_pair(B, LAS_property::B()),
       std::make_pair(I, LAS_property::I()));

  if(remove_intensity) point_set.remove_property_map(intensity);
  if(remove_return_number) point_set.remove_property_map(return_number);
  if(remove_number_of_returns) point_set.remove_property_map(number_of_returns);
  if(remove_scan_direction_flag) point_set.remove_property_map(scan_direction_flag);
  if(remove_edge_of_flight_line) point_set.remove_property_map(edge_of_flight_line);
  if(remove_classification) point_set.remove_property_map(classification);
  if(remove_synthetic_flag) point_set.remove_property_map(synthetic_flag);
  if(remove_keypoint_flag) point_set.remove_property_map(keypoint_flag);
  if(remove_withheld_flag) point_set.remove_property_map(withheld_flag);
  if(remove_scan_angle) point_set.remove_property_map(scan_angle);
  if(remove_user_data) point_set.remove_property_map(user_data);
  if(remove_point_source_ID) point_set.remove_property_map(point_source_ID);
  if(remove_deleted_flag) point_set.remove_property_map(deleted_flag);
  if(remove_gps_time) point_set.remove_property_map(gps_time);
  if(remove_R) point_set.remove_property_map(R);
  if(remove_G) point_set.remove_property_map(G);
  if(remove_B) point_set.remove_property_map(B);
  if(remove_I) point_set.remove_property_map(I);

  return okay;
}

/*!
  \ingroup PkgPointSet3IOLAS

  \brief writes the content of a point set into an output file in the \ref IOStreamLAS.

  \tparam Point the point type of the `Point_set_3`
  \tparam Vector the vector type of the `Point_set_3`

  \param fname the path to the output file
  \param point_set the point set

  \note All LAS properties are written as described in `read_LAS_with_properties()`.

  \return `true` if the writing was successful, `false` otherwise.
 */
template <typename Point, typename Vector>
bool write_LAS(const std::string& fname,
               CGAL::Point_set_3<Point, Vector>& point_set)
{
  std::ofstream os(fname, std::ios::binary);
  CGAL::IO::set_mode(os, CGAL::IO::BINARY);
  return write_LAS(os, point_set);
}

} // namespace IO

#ifndef CGAL_NO_DEPRECATED_CODE

template <typename Point, typename Vector>
CGAL_DEPRECATED bool write_las_point_set(std::ostream& os, ///< output stream.
                                         CGAL::Point_set_3<Point, Vector>& point_set)  ///< point set
{
  return IO::write_LAS(os, point_set);
}

#endif // CGAL_NO_DEPRECATED_CODE

} // namespace CGAL

#endif // LAS

#endif // CGAL_POINT_SET_IO_LAS_H