lasreader_bin.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: lasreader_bin.cpp
5 
6  CONTENTS:
7 
8  see corresponding header file
9 
10  PROGRAMMERS:
11 
12  martin.isenburg@gmail.com
13 
14  COPYRIGHT:
15 
16  (c) 2007-2011, Martin Isenburg, LASSO - tools to catch reality
17 
18  This is free software; you can redistribute and/or modify it under the
19  terms of the GNU Lesser General Licence as published by the Free Software
20  Foundation. See the COPYING file for more information.
21 
22  This software is distributed WITHOUT ANY WARRANTY and without even the
23  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 
25  CHANGE HISTORY:
26 
27  see corresponding header file
28 
29 ===============================================================================
30 */
31 #include "lasreader_bin.hpp"
32 
33 #include "bytestreamin.hpp"
34 #include "bytestreamin_file.hpp"
35 
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #ifdef _WIN32
40 #include <windows.h>
41 #endif
42 
43 struct TSrow
44 {
48  I32 x;
49  I32 y;
50  I32 z;
51 };
52 
53 struct TSpoint
54 {
55  I32 x;
56  I32 y;
57  I32 z;
64 };
65 
66 struct TSheader
67 {
79 };
80 
81 BOOL LASreaderBIN::open(const char* file_name)
82 {
83  if (file_name == 0)
84  {
85  fprintf(stderr,"ERROR: fine name pointer is zero\n");
86  return FALSE;
87  }
88 
89  // open file
90 
91  file = fopen(file_name, "rb");
92  if (file == 0)
93  {
94  fprintf(stderr, "ERROR: cannot open file '%s'\n", file_name);
95  return FALSE;
96  }
97 
98  // create input stream
99 
100  ByteStreamIn* in;
101  if (IS_LITTLE_ENDIAN())
102  in = new ByteStreamInFileLE(file);
103  else
104  in = new ByteStreamInFileBE(file);
105 
106  // clean header
107 
108  header.clean();
109 
110  // maybe set creation date
111 
112 #ifdef _WIN32
113  WIN32_FILE_ATTRIBUTE_DATA attr;
114  SYSTEMTIME creation;
115  GetFileAttributesEx(file_name, GetFileExInfoStandard, &attr);
116  FileTimeToSystemTime(&attr.ftCreationTime, &creation);
117  int startday[13] = {-1, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
118  header.file_creation_day = startday[creation.wMonth] + creation.wDay;
119  header.file_creation_year = creation.wYear;
120  // leap year handling
121  if ((((creation.wYear)%4) == 0) && (creation.wMonth > 2)) header.file_creation_day++;
122 #else
124  header.file_creation_year = 2011;
125 #endif
126 
127  return open(in);
128 }
129 
131 {
132  int i;
133 
134  if (stream == 0)
135  {
136  fprintf(stderr,"ERROR: ByteStreamIn* pointer is zero\n");
137  return FALSE;
138  }
139 
140  this->stream = stream;
141 
142  // read the TS header
143 
144  TSheader tsheader;
145  try { stream->getBytes((U8*)&tsheader, sizeof(TSheader)); } catch(...)
146  {
147  fprintf(stderr,"ERROR: reading terrasolid header\n");
148  return FALSE;
149  }
150 
151  // check the TS header
152 
153  if (tsheader.size != sizeof(TSheader))
154  {
155  fprintf(stderr,"ERROR: corrupt terrasolid header. size != 56.\n");
156  return FALSE;
157  }
158 
159  if (tsheader.recog_val != 970401)
160  {
161  fprintf(stderr,"ERROR: corrupt terrasolid header. recog_val != 979401.\n");
162  return FALSE;
163  }
164 
165  if (strncmp(tsheader.recog_str, "CXYZ", 4) != 0)
166  {
167  fprintf(stderr,"ERROR: corrupt terrasolid header. recog_str != CXYZ.\n");
168  return FALSE;
169  }
170 
171  version = tsheader.version;
172 
173  // populate the header as much as possible
174 
175  for (i = 0; i < 32; i++)
176  {
177  header.system_identifier[i] = '\0';
178  header.generating_software[i] = '\0';
179  }
180  sprintf(header.system_identifier, "LAStools (c) by Martin Isenburg");
181  sprintf(header.generating_software, "via LASreaderBIN (%d)", LAS_TOOLS_VERSION);
182 
183  if (tsheader.time)
184  {
185  if (tsheader.rgb)
186  {
189  }
190  else
191  {
194  }
195  }
196  else
197  {
198  if (tsheader.rgb)
199  {
202  }
203  else
204  {
207  }
208  }
209 
211  npoints = tsheader.npoints;
212 
213  F64 scale = 1.0/(F64)tsheader.units;
214  header.x_scale_factor = scale;
215  header.y_scale_factor = scale;
216  header.z_scale_factor = scale;
217  header.x_offset = -tsheader.origin_x*scale;
218  header.y_offset = -tsheader.origin_y*scale;
219  header.z_offset = -tsheader.origin_z*scale;
220 
221  // initialize point
222 
224 
225  // set point count to zero
226 
227  p_count = 0;
228 
229  // approximate bounding box init
230 
231  if (!read_point()) return FALSE;
232 
236 
237  for (i = tsheader.npoints/10; i < tsheader.npoints; i += tsheader.npoints/10)
238  {
239  if (!seek(i)) return FALSE;
240  if (!read_point()) return FALSE;
241  }
242 
245 
246  return seek(0);
247 }
248 
250 {
251  if (p_index < npoints)
252  {
253  long pos = sizeof(TSheader);
254  if (version == 20020715)
255  pos += (sizeof(TSpoint)*(U32)p_index);
256  else
257  pos += (sizeof(TSrow)*(U32)p_index);
258  if (point.have_gps_time)
259  pos += (sizeof(U32)*(U32)p_index);
260  if (point.have_rgb)
261  pos += (sizeof(U32)*(U32)p_index);
262  p_count = p_index;
263  return stream->seek(pos);
264  }
265  return FALSE;
266 }
267 
269 {
270  if (p_count < npoints)
271  {
272  int echo;
273  if (version == 20020715)
274  {
275  TSpoint tspoint;
276  try { stream->getBytes((U8*)&tspoint, sizeof(TSpoint)); } catch(...)
277  {
278  fprintf(stderr,"ERROR: reading terrasolid point after %u of %u\n", (U32)p_count, (U32)npoints);
279  return FALSE;
280  }
281  point.x = tspoint.x;
282  point.y = tspoint.y;
283  point.z = tspoint.z;
284  point.intensity = tspoint.intensity;
285  point.classification = tspoint.code;
286  point.point_source_ID = tspoint.line;
287  echo = tspoint.echo;
288  }
289  else
290  {
291  TSrow tsrow;
292  try { stream->getBytes((U8*)&tsrow, sizeof(TSrow)); } catch(...)
293  {
294  fprintf(stderr,"ERROR: reading terrasolid row after %u of %u\n", (U32)p_count, (U32)npoints);
295  return FALSE;
296  }
297  point.x = tsrow.x;
298  point.y = tsrow.y;
299  point.z = tsrow.z;
300  point.intensity = tsrow.echo_intensity & 0x3FFF;
301  point.classification = tsrow.code;
302  point.point_source_ID = tsrow.line;
303  echo = tsrow.echo_intensity >> 14;
304  }
305 
306  if (header.min_x > point.get_x()) header.min_x = point.get_x();
307  else if (header.max_x < point.get_x()) header.max_x = point.get_x();
308  if (header.min_y > point.get_y()) header.min_y = point.get_y();
309  else if (header.max_y < point.get_y()) header.max_y = point.get_y();
310  if (header.min_z > point.get_z()) header.min_z = point.get_z();
311  else if (header.max_z < point.get_z()) header.max_z = point.get_z();
312 
313  if (echo == 0) // only echo
314  {
316  point.return_number = 1;
318  }
319  else if (echo == 1) // first (of many)
320  {
322  point.return_number = 1;
324  }
325  else if (echo == 3) // last (of many)
326  {
328  point.return_number = 2;
330  }
331  else // intermediate
332  {
334  point.return_number = 2;
336  }
337 
338  if (point.have_gps_time)
339  {
340  U32 time;
341  try { stream->getBytes((U8*)&time, sizeof(U32)); } catch(...)
342  {
343  fprintf(stderr,"ERROR: reading terrasolid time\n");
344  return FALSE;
345  }
346  point.gps_time = 0.0002*time;
347  }
348 
349  if (point.have_rgb)
350  {
351  U8 rgba[4];
352  try { stream->getBytes((U8*)rgba, sizeof(U8)*4); } catch(...)
353  {
354  fprintf(stderr,"ERROR: reading terrasolid color\n");
355  return FALSE;
356  }
357  point.rgb[0] = 256*rgba[0];
358  point.rgb[1] = 256*rgba[1];
359  point.rgb[2] = 256*rgba[2];
360  }
361  p_count++;
362  return TRUE;
363  }
364  return FALSE;
365 }
366 
368 {
369  return stream;
370 }
371 
372 void LASreaderBIN::close(BOOL close_stream)
373 {
374  if (close_stream)
375  {
376  if (stream)
377  {
378  delete stream;
379  stream = 0;
380  }
381  if (file)
382  {
383  fclose(file);
384  file = 0;
385  }
386  }
387 }
388 
390 {
391  file = 0;
392  stream = 0;
393 }
394 
396 {
397  if (stream) close();
398 }
399 
400 LASreaderBINrescale::LASreaderBINrescale(F64 x_scale_factor, F64 y_scale_factor, F64 z_scale_factor) : LASreaderBIN()
401 {
402  scale_factor[0] = x_scale_factor;
403  scale_factor[1] = y_scale_factor;
404  scale_factor[2] = z_scale_factor;
405 }
406 
408 {
409  if (!LASreaderBIN::open(stream)) return FALSE;
410  // do we need to change anything
412  {
414  }
416  {
418  }
420  {
422  }
423  return TRUE;
424 }
425 
427 {
428  this->offset[0] = x_offset;
429  this->offset[1] = y_offset;
430  this->offset[2] = z_offset;
431 }
432 
434 {
435  if (!LASreaderBIN::open(stream)) return FALSE;
436  // do we need to change anything
437  if (header.x_offset != offset[0])
438  {
439  header.x_offset = offset[0];
440  }
441  if (header.y_offset != offset[1])
442  {
443  header.y_offset = offset[1];
444  }
445  if (header.z_offset != offset[2])
446  {
447  header.z_offset = offset[2];
448  }
449  return TRUE;
450 }
451 
452 LASreaderBINrescalereoffset::LASreaderBINrescalereoffset(F64 x_scale_factor, F64 y_scale_factor, F64 z_scale_factor, F64 x_offset, F64 y_offset, F64 z_offset) : LASreaderBINrescale(x_scale_factor, y_scale_factor, z_scale_factor), LASreaderBINreoffset(x_offset, y_offset, z_offset)
453 {
454 }
455 
457 {
458  if (!LASreaderBIN::open(stream)) return FALSE;
459  // do we need to change anything
461  {
463  }
465  {
467  }
469  {
471  }
472  if (header.x_offset != offset[0])
473  {
474  header.x_offset = offset[0];
475  }
476  if (header.y_offset != offset[1])
477  {
478  header.y_offset = offset[1];
479  }
480  if (header.z_offset != offset[2])
481  {
482  header.z_offset = offset[2];
483  }
484  return TRUE;
485 }
LASquantizer::y_offset
F64 y_offset
Definition: lasdefinitions.hpp:96
TSpoint::echo
U8 echo
Definition: lasreader_bin.cpp:59
LASheader::system_identifier
I8 system_identifier[32]
Definition: lasdefinitions.hpp:964
TSpoint::line
U16 line
Definition: lasreader_bin.cpp:62
LASheader::clean
void clean()
Definition: lasdefinitions.hpp:1095
LASreaderBINrescale
Definition: lasreader_bin.hpp:67
LASheader::min_y
F64 min_y
Definition: lasdefinitions.hpp:978
LASheader::number_of_points_by_return
U32 number_of_points_by_return[5]
Definition: lasdefinitions.hpp:974
LASreaderBINreoffset::open
virtual BOOL open(ByteStreamIn *stream)
Definition: lasreader_bin.cpp:433
TSheader::origin_x
F64 origin_x
Definition: lasreader_bin.cpp:74
LASpoint::gps_time
F64 gps_time
Definition: lasdefinitions.hpp:497
TSheader::origin_z
F64 origin_z
Definition: lasreader_bin.cpp:76
LASreader::point
LASpoint point
Definition: lasreader.hpp:53
LASreaderBINreoffset::offset
F64 offset[3]
Definition: lasreader_bin.hpp:83
LASreaderBIN::open
BOOL open(const char *file_name)
Definition: lasreader_bin.cpp:81
TSpoint::intensity
U16 intensity
Definition: lasreader_bin.cpp:63
LASpoint::point_source_ID
U16 point_source_ID
Definition: lasdefinitions.hpp:489
LASreaderBIN::stream
ByteStreamIn * stream
Definition: lasreader_bin.hpp:63
TSpoint::y
I32 y
Definition: lasreader_bin.cpp:56
LASheader::generating_software
I8 generating_software[32]
Definition: lasdefinitions.hpp:965
I8
char I8
Definition: mydefs.hpp:37
I64
long long I64
Definition: mydefs.hpp:48
TSheader::time
I32 time
Definition: lasreader_bin.cpp:77
F64
double F64
Definition: mydefs.hpp:52
LASreaderBIN::close
void close(BOOL close_stream=TRUE)
Definition: lasreader_bin.cpp:372
LASreaderBIN::~LASreaderBIN
virtual ~LASreaderBIN()
Definition: lasreader_bin.cpp:395
LASquantizer::x_scale_factor
F64 x_scale_factor
Definition: lasdefinitions.hpp:92
I32
int I32
Definition: mydefs.hpp:35
LASpoint::have_rgb
BOOL have_rgb
Definition: lasdefinitions.hpp:518
TSheader
Definition: lasreader_bin.cpp:66
TRUE
#define TRUE
Definition: mydefs.hpp:137
LASreaderBINrescalereoffset::LASreaderBINrescalereoffset
LASreaderBINrescalereoffset(F64 x_scale_factor, F64 y_scale_factor, F64 z_scale_factor, F64 x_offset, F64 y_offset, F64 z_offset)
Definition: lasreader_bin.cpp:452
LASreader::p_count
I64 p_count
Definition: lasreader.hpp:56
LASreader::npoints
I64 npoints
Definition: lasreader.hpp:55
LASreaderBIN
Definition: lasreader_bin.hpp:40
TSheader::recog_val
I32 recog_val
Definition: lasreader_bin.cpp:70
LASreaderBINrescale::scale_factor
F64 scale_factor[3]
Definition: lasreader_bin.hpp:74
LASquantizer::x_offset
F64 x_offset
Definition: lasdefinitions.hpp:95
LASheader::number_of_point_records
U32 number_of_point_records
Definition: lasdefinitions.hpp:973
TSrow
Definition: lasreader_bin.cpp:43
TSheader::npoints
I32 npoints
Definition: lasreader_bin.cpp:72
LASreaderBINrescalereoffset::open
BOOL open(ByteStreamIn *stream)
Definition: lasreader_bin.cpp:456
LASpoint::y
I32 y
Definition: lasdefinitions.hpp:479
TSheader::recog_str
I8 recog_str[4]
Definition: lasreader_bin.cpp:71
LASpoint::classification
U8 classification
Definition: lasdefinitions.hpp:486
LASpoint::rgb
U16 rgb[4]
Definition: lasdefinitions.hpp:498
ByteStreamIn::seek
virtual BOOL seek(const I64 position)=0
LASpoint::return_number
U8 return_number
Definition: lasdefinitions.hpp:482
TSpoint::z
I32 z
Definition: lasreader_bin.cpp:57
LASheader::point_data_format
U8 point_data_format
Definition: lasdefinitions.hpp:971
TSpoint::x
I32 x
Definition: lasreader_bin.cpp:55
TSheader::rgb
I32 rgb
Definition: lasreader_bin.cpp:78
bytestreamin.hpp
LASheader::file_creation_year
U16 file_creation_year
Definition: lasdefinitions.hpp:967
TSrow::y
I32 y
Definition: lasreader_bin.cpp:49
LASpoint::get_y
F64 get_y() const
Definition: lasdefinitions.hpp:812
LASheader::max_x
F64 max_x
Definition: lasdefinitions.hpp:975
LASreaderBIN::get_stream
ByteStreamIn * get_stream() const
Definition: lasreader_bin.cpp:367
LASreaderBIN::file
FILE * file
Definition: lasreader_bin.hpp:62
TSheader::units
I32 units
Definition: lasreader_bin.cpp:73
ByteStreamIn
Definition: bytestreamin.hpp:36
U16
unsigned short U16
Definition: mydefs.hpp:40
LASreaderBIN::seek
BOOL seek(const I64 p_index)
Definition: lasreader_bin.cpp:249
TSheader::origin_y
F64 origin_y
Definition: lasreader_bin.cpp:75
LASquantizer::z_offset
F64 z_offset
Definition: lasdefinitions.hpp:97
LASpoint::have_gps_time
BOOL have_gps_time
Definition: lasdefinitions.hpp:517
lasreader_bin.hpp
TSheader::size
I32 size
Definition: lasreader_bin.cpp:68
LASreaderBIN::read_point_default
BOOL read_point_default()
Definition: lasreader_bin.cpp:268
U8
unsigned char U8
Definition: mydefs.hpp:41
BOOL
int BOOL
Definition: mydefs.hpp:57
LASpoint::z
I32 z
Definition: lasdefinitions.hpp:480
LASreaderBINreoffset
Definition: lasreader_bin.hpp:77
TSrow::code
U8 code
Definition: lasreader_bin.cpp:45
FALSE
#define FALSE
Definition: mydefs.hpp:133
ByteStreamInFileLE
Definition: bytestreamin_file.hpp:65
LASheader::max_y
F64 max_y
Definition: lasdefinitions.hpp:977
bytestreamin_file.hpp
TSrow::echo_intensity
U16 echo_intensity
Definition: lasreader_bin.cpp:47
LAS_TOOLS_VERSION
#define LAS_TOOLS_VERSION
Definition: lasdefinitions.hpp:47
LASheader::min_x
F64 min_x
Definition: lasdefinitions.hpp:976
TSpoint::code
U8 code
Definition: lasreader_bin.cpp:58
LASpoint::number_of_returns_of_given_pulse
U8 number_of_returns_of_given_pulse
Definition: lasdefinitions.hpp:483
LASpoint::init
BOOL init(const LASquantizer *quantizer, const U8 point_type, const U16 point_size, const LASattributer *attributer=0)
Definition: lasdefinitions.hpp:595
LASreaderBINreoffset::LASreaderBINreoffset
LASreaderBINreoffset(F64 x_offset, F64 y_offset, F64 z_offset)
Definition: lasreader_bin.cpp:426
LASreaderBIN::version
I32 version
Definition: lasreader_bin.hpp:64
LASreader::header
LASheader header
Definition: lasreader.hpp:52
IS_LITTLE_ENDIAN
BOOL IS_LITTLE_ENDIAN()
Definition: mydefs.hpp:144
U32
unsigned int U32
Definition: mydefs.hpp:39
TSrow::line
U8 line
Definition: lasreader_bin.cpp:46
LASheader::file_creation_day
U16 file_creation_day
Definition: lasdefinitions.hpp:966
LASpoint::x
I32 x
Definition: lasdefinitions.hpp:478
LASheader::max_z
F64 max_z
Definition: lasdefinitions.hpp:979
LASpoint::get_z
F64 get_z() const
Definition: lasdefinitions.hpp:813
LASpoint::get_x
F64 get_x() const
Definition: lasdefinitions.hpp:811
LASreaderBINrescale::open
virtual BOOL open(ByteStreamIn *stream)
Definition: lasreader_bin.cpp:407
LASreader::read_point
BOOL read_point()
Definition: lasreader.hpp:74
ByteStreamIn::getBytes
virtual void getBytes(U8 *bytes, const U32 num_bytes)=0
LASheader::point_data_record_length
U16 point_data_record_length
Definition: lasdefinitions.hpp:972
TSpoint::flag
U8 flag
Definition: lasreader_bin.cpp:60
TSheader::version
I32 version
Definition: lasreader_bin.cpp:69
LASreaderBIN::LASreaderBIN
LASreaderBIN()
Definition: lasreader_bin.cpp:389
TSrow::x
I32 x
Definition: lasreader_bin.cpp:48
TSpoint
Definition: lasreader_bin.cpp:53
LASquantizer::y_scale_factor
F64 y_scale_factor
Definition: lasdefinitions.hpp:93
LASreaderBINrescale::LASreaderBINrescale
LASreaderBINrescale(F64 x_scale_factor, F64 y_scale_factor, F64 z_scale_factor)
Definition: lasreader_bin.cpp:400
TSrow::z
I32 z
Definition: lasreader_bin.cpp:50
LASquantizer::z_scale_factor
F64 z_scale_factor
Definition: lasdefinitions.hpp:94
LASheader::min_z
F64 min_z
Definition: lasdefinitions.hpp:980
LASpoint::intensity
U16 intensity
Definition: lasdefinitions.hpp:481
TSpoint::mark
U8 mark
Definition: lasreader_bin.cpp:61
ByteStreamInFileBE
Definition: bytestreamin_file.hpp:85


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23