laswriter_bin.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: laswriter_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 "laswriter_bin.hpp"
32 
33 #include "bytestreamout_file.hpp"
34 
35 #ifdef _WIN32
36 #include <fcntl.h>
37 #include <io.h>
38 #endif
39 
40 #include <stdlib.h>
41 #include <string.h>
42 
43 struct TSrow
44 {
45  U8 code;
46  U8 line;
48  I32 x;
49  I32 y;
50  I32 z;
51 };
52 
53 struct TSpoint
54 {
55  I32 x;
56  I32 y;
57  I32 z;
58  U8 code;
59  U8 echo;
60  U8 flag;
61  U8 mark;
62  U16 line;
63  U16 intensity;
64 };
65 
66 struct TSheader
67 {
68  I32 size;
69  I32 version;
70  I32 recog_val;
71  I8 recog_str[4];
72  I32 npoints;
73  I32 units;
74  F64 origin_x;
75  F64 origin_y;
76  F64 origin_z;
77  I32 time;
78  I32 rgb;
79 };
80 
82 {
83  if (stream == 0) return FALSE;
84  if (this->file) this->file = file;
85  return ((ByteStreamOutFile*)stream)->refile(file);
86 }
87 
88 BOOL LASwriterBIN::open(const char* file_name, const LASheader* header, const char* version, U32 io_buffer_size)
89 {
90  if (file_name == 0)
91  {
92  fprintf(stderr,"ERROR: file name pointer is zero\n");
93  return FALSE;
94  }
95 
96  file = fopen(file_name, "wb");
97 
98  if (file == 0)
99  {
100  fprintf(stderr, "ERROR: cannot open file '%s'\n", file_name);
101  return FALSE;
102  }
103 
104  if (setvbuf(file, NULL, _IOFBF, io_buffer_size) != 0)
105  {
106  fprintf(stderr, "WARNING: setvbuf() failed with buffer size %u\n", io_buffer_size);
107  }
108 
109  ByteStreamOut* out;
110  if (IS_LITTLE_ENDIAN())
111  out = new ByteStreamOutFileLE(file);
112  else
113  out = new ByteStreamOutFileBE(file);
114 
115  return open(out, header, version);
116 }
117 
118 BOOL LASwriterBIN::open(FILE* file, const LASheader* header, const char* version)
119 {
120  if (file == 0)
121  {
122  fprintf(stderr,"ERROR: file pointer is zero\n");
123  return FALSE;
124  }
125 
126 #ifdef _WIN32
127  if (file == stdout)
128  {
129  if(_setmode( _fileno( stdout ), _O_BINARY ) == -1 )
130  {
131  fprintf(stderr, "ERROR: cannot set stdout to binary (untranslated) mode\n");
132  }
133  }
134 #endif
135 
136  ByteStreamOut* out;
137  if (IS_LITTLE_ENDIAN())
138  out = new ByteStreamOutFileLE(file);
139  else
140  out = new ByteStreamOutFileBE(file);
141 
142  return open(out, header, version);
143 }
144 
145 BOOL LASwriterBIN::open(ByteStreamOut* stream, const LASheader* header, const char* version)
146 {
147  if (stream == 0)
148  {
149  fprintf(stderr,"ERROR: ByteStreamOut pointer is zero\n");
150  return FALSE;
151  }
152  this->stream = stream;
153 
154  if (header == 0)
155  {
156  fprintf(stderr,"ERROR: LASheader pointer is zero\n");
157  return FALSE;
158  }
159 
160  if (strstr(version, "ts16"))
161  {
162  this->version = 20020715;
163  }
164  else
165  {
166  this->version = 20010712;
167  }
168 
169  TSheader tsheader;
170  tsheader.size = sizeof(TSheader);
171  tsheader.version = this->version;
172  tsheader.recog_val = 970401;
173  strncpy(tsheader.recog_str, "CXYZ", 4);
174  tsheader.npoints = header->number_of_point_records;
175  double scale = header->x_scale_factor;
176  if (header->y_scale_factor < scale) scale = header->y_scale_factor;
177  if (header->z_scale_factor < scale) scale = header->z_scale_factor;
178  units = tsheader.units = (I32)(1.0 / scale);
179  origin_x = tsheader.origin_x = -header->x_offset/scale;
180  origin_y = tsheader.origin_y = -header->y_offset/scale;
181  origin_z = tsheader.origin_z = -header->z_offset/scale;
182  tsheader.time = (header->point_data_format == 1) || (header->point_data_format == 3) || (header->point_data_format == 4) || (header->point_data_format == 5);
183  tsheader.rgb = (header->point_data_format == 2) || (header->point_data_format == 3) || (header->point_data_format == 5);
184 
185  return stream->putBytes((U8*)&tsheader, sizeof(TSheader));
186 }
187 
189 {
190  U16 echo;
191 
192  if (point->number_of_returns_of_given_pulse <= 1)
193  echo = 0;
194  else if (point->return_number == 1)
195  echo = 1;
196  else if (point->return_number >= point->number_of_returns_of_given_pulse)
197  echo = 3;
198  else
199  echo = 2;
200 
201  if (version == 20020715)
202  {
203  TSpoint tspoint;
204  tspoint.x = I32_QUANTIZE(point->get_x()*units+origin_x);
205  tspoint.y = I32_QUANTIZE(point->get_y()*units+origin_y);
206  tspoint.z = I32_QUANTIZE(point->get_z()*units+origin_z);
207  tspoint.code = point->classification;
208  tspoint.echo = (U8)echo;
209  tspoint.flag = 0;
210  tspoint.mark = 0;
211  tspoint.line = point->point_source_ID;
212  tspoint.intensity = point->intensity;
213  if (!stream->putBytes((U8*)&tspoint, sizeof(TSpoint))) return FALSE;
214  }
215  else
216  {
217  TSrow tsrow;
218  tsrow.code = point->classification;
219  tsrow.line = (U8)(point->point_source_ID);
220  tsrow.echo_intensity = (echo << 14) | (point->intensity & 0x3FFF);
221  tsrow.x = I32_QUANTIZE(point->get_x()*units+origin_x);
222  tsrow.y = I32_QUANTIZE(point->get_y()*units+origin_y);
223  tsrow.z = I32_QUANTIZE(point->get_z()*units+origin_z);
224  if (!stream->putBytes((U8*)&tsrow, sizeof(TSrow))) return FALSE;
225  }
226 
227  if (point->have_gps_time)
228  {
229  U32 time = (U32)(point->gps_time/0.0002+0.5);
230  if (!stream->putBytes((U8*)&time, sizeof(U32))) return FALSE;
231  }
232  if (point->have_rgb)
233  {
234  U8 rgba[4];
235  rgba[0] = point->rgb[0]/256;
236  rgba[1] = point->rgb[1]/256;
237  rgba[2] = point->rgb[2]/256;
238  rgba[3] = 0;
239  if (!stream->putBytes((U8*)&rgba, sizeof(U32))) return FALSE;
240  }
241  p_count++;
242  return TRUE;
243 }
244 
245 BOOL LASwriterBIN::update_header(const LASheader* header, BOOL use_inventory, BOOL update_extra_bytes)
246 {
247  return TRUE;
248 }
249 
251 {
252  I64 bytes = 0;
253 
254  if (stream)
255  {
256  if (update_header && p_count != npoints)
257  {
258  if (!stream->isSeekable())
259  {
260 #ifdef _WIN32
261  fprintf(stderr, "ERROR: stream not seekable. cannot update header from %I64d to %I64d points.\n", npoints, p_count);
262 #else
263  fprintf(stderr, "ERROR: stream not seekable. cannot update header from %lld to %lld points.\n", npoints, p_count);
264 #endif
265  }
266  else
267  {
268  stream->seek(16);
270  stream->seekEnd();
271  }
272  }
273  bytes = stream->tell();
274  delete stream;
275  stream = 0;
276  }
277 
278  if (file)
279  {
280  fclose(file);
281  file = 0;
282  }
283 
284  npoints = p_count;
285  p_count = 0;
286 
287  return bytes;
288 }
289 
291 {
292  stream = 0;
293  file = 0;
294 }
295 
297 {
298  if (file) close();
299 }
LASquantizer::y_offset
F64 y_offset
Definition: lasdefinitions.hpp:96
TSpoint::echo
U8 echo
Definition: lasreader_bin.cpp:59
LASheader
Definition: lasdefinitions.hpp:952
LASwriterBIN::version
U32 version
Definition: laswriter_bin.hpp:63
LASwriterBIN::~LASwriterBIN
~LASwriterBIN()
Definition: laswriter_bin.cpp:296
TSpoint::line
U16 line
Definition: lasreader_bin.cpp:62
LASwriterBIN::origin_y
F64 origin_y
Definition: laswriter_bin.hpp:66
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
TSpoint::intensity
U16 intensity
Definition: lasreader_bin.cpp:63
LASpoint
Definition: lasdefinitions.hpp:472
LASpoint::point_source_ID
U16 point_source_ID
Definition: lasdefinitions.hpp:489
TSpoint::y
I32 y
Definition: lasreader_bin.cpp:56
LASwriterBIN::LASwriterBIN
LASwriterBIN()
Definition: laswriter_bin.cpp:290
LASwriterBIN::close
I64 close(BOOL update_npoints=true)
Definition: laswriter_bin.cpp:250
I8
char I8
Definition: mydefs.hpp:37
LASwriterBIN::units
I32 units
Definition: laswriter_bin.hpp:64
LASwriterBIN::origin_z
F64 origin_z
Definition: laswriter_bin.hpp:67
I64
long long I64
Definition: mydefs.hpp:48
TSheader::time
I32 time
Definition: lasreader_bin.cpp:77
F64
double F64
Definition: mydefs.hpp:52
LASwriter::p_count
I64 p_count
Definition: laswriter.hpp:52
LASwriterBIN::update_header
BOOL update_header(const LASheader *header, BOOL use_inventory=TRUE, BOOL update_extra_bytes=FALSE)
Definition: laswriter_bin.cpp:245
LASquantizer::x_scale_factor
F64 x_scale_factor
Definition: lasdefinitions.hpp:92
I32
int I32
Definition: mydefs.hpp:35
LASwriterBIN::stream
ByteStreamOut * stream
Definition: laswriter_bin.hpp:61
LASpoint::have_rgb
BOOL have_rgb
Definition: lasdefinitions.hpp:518
TSheader
Definition: lasreader_bin.cpp:66
TRUE
#define TRUE
Definition: mydefs.hpp:137
LASwriterBIN::write_point
BOOL write_point(const LASpoint *point)
Definition: laswriter_bin.cpp:188
TSheader::recog_val
I32 recog_val
Definition: lasreader_bin.cpp:70
ByteStreamOutFile
Definition: bytestreamout_file.hpp:43
LASquantizer::x_offset
F64 x_offset
Definition: lasdefinitions.hpp:95
LASheader::number_of_point_records
U32 number_of_point_records
Definition: lasdefinitions.hpp:973
NULL
#define NULL
Definition: mydefs.hpp:141
TSrow
Definition: lasreader_bin.cpp:43
ByteStreamOut::put32bitsLE
virtual BOOL put32bitsLE(const U8 *bytes)=0
TSheader::npoints
I32 npoints
Definition: lasreader_bin.cpp:72
ByteStreamOut::tell
virtual I64 tell() const =0
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
ByteStreamOut
Definition: bytestreamout.hpp:36
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
I32_QUANTIZE
#define I32_QUANTIZE(n)
Definition: mydefs.hpp:111
TSrow::y
I32 y
Definition: lasreader_bin.cpp:49
LASpoint::get_y
F64 get_y() const
Definition: lasdefinitions.hpp:812
TSheader::units
I32 units
Definition: lasreader_bin.cpp:73
U16
unsigned short U16
Definition: mydefs.hpp:40
TSheader::origin_y
F64 origin_y
Definition: lasreader_bin.cpp:75
LASquantizer::z_offset
F64 z_offset
Definition: lasdefinitions.hpp:97
laswriter_bin.hpp
LASpoint::have_gps_time
BOOL have_gps_time
Definition: lasdefinitions.hpp:517
TSheader::size
I32 size
Definition: lasreader_bin.cpp:68
ByteStreamOut::seek
virtual BOOL seek(const I64 position)=0
LASwriterBIN::origin_x
F64 origin_x
Definition: laswriter_bin.hpp:65
U8
unsigned char U8
Definition: mydefs.hpp:41
BOOL
int BOOL
Definition: mydefs.hpp:57
LASwriterBIN::open
BOOL open(const char *file_name, const LASheader *header, const char *version, U32 io_buffer_size=65536)
Definition: laswriter_bin.cpp:88
ByteStreamOut::isSeekable
virtual BOOL isSeekable() const =0
bytestreamout_file.hpp
TSrow::code
U8 code
Definition: lasreader_bin.cpp:45
FALSE
#define FALSE
Definition: mydefs.hpp:133
file
FILE * file
Definition: arithmeticencoder.cpp:77
ByteStreamOutFileBE
Definition: bytestreamout_file.hpp:87
LASwriterBIN::file
FILE * file
Definition: laswriter_bin.hpp:62
TSrow::echo_intensity
U16 echo_intensity
Definition: lasreader_bin.cpp:47
LASwriter::npoints
I64 npoints
Definition: laswriter.hpp:51
TSpoint::code
U8 code
Definition: lasreader_bin.cpp:58
LASwriterBIN::refile
BOOL refile(FILE *file)
Definition: laswriter_bin.cpp:81
LASpoint::number_of_returns_of_given_pulse
U8 number_of_returns_of_given_pulse
Definition: lasdefinitions.hpp:483
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
LASpoint::get_z
F64 get_z() const
Definition: lasdefinitions.hpp:813
LASpoint::get_x
F64 get_x() const
Definition: lasdefinitions.hpp:811
TSpoint::flag
U8 flag
Definition: lasreader_bin.cpp:60
TSheader::version
I32 version
Definition: lasreader_bin.cpp:69
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
ByteStreamOut::seekEnd
virtual BOOL seekEnd()=0
ByteStreamOut::putBytes
virtual BOOL putBytes(const U8 *bytes, U32 num_bytes)=0
TSrow::z
I32 z
Definition: lasreader_bin.cpp:50
LASquantizer::z_scale_factor
F64 z_scale_factor
Definition: lasdefinitions.hpp:94
LASpoint::intensity
U16 intensity
Definition: lasdefinitions.hpp:481
TSpoint::mark
U8 mark
Definition: lasreader_bin.cpp:61
ByteStreamOutFileLE
Definition: bytestreamout_file.hpp:67


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