laswriter_qfit.cpp
Go to the documentation of this file.
1 /*
2 ===============================================================================
3 
4  FILE: laswriter_qfit.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_qfit.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 
44 {
45  if (stream == 0) return FALSE;
46  if (this->file) this->file = file;
47  return ((ByteStreamOutFile*)stream)->refile(file);
48 }
49 
50 BOOL LASwriterQFIT::open(const char* file_name, const LASheader* header, I32 version, U32 io_buffer_size)
51 {
52  if (file_name == 0)
53  {
54  fprintf(stderr,"ERROR: file name pointer is zero\n");
55  return FALSE;
56  }
57 
58  file = fopen(file_name, "wb");
59 
60  if (file == 0)
61  {
62  fprintf(stderr, "ERROR: cannot open file '%s'\n", file_name);
63  return FALSE;
64  }
65 
66  if (setvbuf(file, NULL, _IOFBF, io_buffer_size) != 0)
67  {
68  fprintf(stderr, "WARNING: setvbuf() failed with buffer size %u\n", io_buffer_size);
69  }
70 
71  return open(file, header, version);
72 }
73 
74 BOOL LASwriterQFIT::open(FILE* file, const LASheader* header, I32 version)
75 {
76  if (file == 0)
77  {
78  fprintf(stderr,"ERROR: file pointer is zero\n");
79  return FALSE;
80  }
81 
82 #ifdef _WIN32
83  if (file == stdout)
84  {
85  if(_setmode( _fileno( stdout ), _O_BINARY ) == -1 )
86  {
87  fprintf(stderr, "ERROR: cannot set stdout to binary (untranslated) mode\n");
88  }
89  }
90 #endif
91 
92  ByteStreamOut* out;
93  if (IS_LITTLE_ENDIAN())
94  {
95  out = new ByteStreamOutFileLE(file);
97  }
98  else
99  {
100  out = new ByteStreamOutFileBE(file);
101  endian_swap = TRUE;
102  }
103 
104  return open(out, header, version);
105 }
106 
107 BOOL LASwriterQFIT::open(ByteStreamOut* stream, const LASheader* header, I32 version)
108 {
109  if (stream == 0)
110  {
111  fprintf(stderr,"ERROR: ByteStreamOut pointer is zero\n");
112  return FALSE;
113  }
114  this->stream = stream;
115 
116  if (header == 0)
117  {
118  fprintf(stderr,"ERROR: LASheader pointer is zero\n");
119  return FALSE;
120  }
121 
122  // check whether input is in longitude / latitude
123 
124  if (((-361 < header->min_x) && (-361 < header->min_y) && (header->max_x < 361) && (header->max_y < 361)) == FALSE)
125  {
126  fprintf(stderr,"ERROR: bounding box (%g %g / %g %g) exceeds longitude / latitude\n", header->min_x, header->min_y, header->max_x, header->max_y);
127  return FALSE;
128  }
129 
130  // check whether we need to rescale or reoffset
131 
133 
134  if (header->x_scale_factor != 0.000001 || header->y_scale_factor != 0.000001 || header->z_scale_factor != 0.001)
135  {
137  }
138 
139  if (header->x_offset != 0.0 || header->y_offset != 0.0 || header->z_offset != 0.0)
140  {
142  }
143 
144  // check whether these attributes are available
145 
150 
151  if (version == 0)
152  {
153  if (pulse_width_array_offset != -1)
154  {
155  version = 48;
156  }
157  else
158  {
159  version = 40;
160  }
161  }
162 
163  if (version == 48 || version == 40 || version == 56)
164  {
165  this->version = version;
166  }
167  else
168  {
169  fprintf(stderr,"WARNING: version %d of QFIT unknown ... using 48\n", version);
170  this->version = 48;
171  }
172 
173  // write version into first header record
174 
175  if (!stream->put32bitsLE((U8*)&version))
176  {
177  fprintf(stderr,"ERROR: while writing version of QFIT header\n");
178  return FALSE;
179  }
180 
181  // populate some ASCII into the remainder of first header record
182 
183  memset(buffer, 0, 48);
184  sprintf((char*)buffer, "via LASwriterQFIT (version %d)", LAS_TOOLS_VERSION);
185 
186  if (!stream->putBytes((U8*)buffer, version-4))
187  {
188  fprintf(stderr,"ERROR: writing first header record of QFIT header\n");
189  return FALSE;
190  }
191 
192  // write continuation and offset into second header record
193 
194  buffer[0] = -9000000;
195  buffer[1] = 2*version;
196 
197  if (!stream->put32bitsLE((U8*)&buffer[0]))
198  {
199  fprintf(stderr,"ERROR: while writing -9000000 into QFIT header\n");
200  return FALSE;
201  }
202  if (!stream->put32bitsLE((U8*)&buffer[1]))
203  {
204  fprintf(stderr,"ERROR: while writing offset into QFIT header\n");
205  return FALSE;
206  }
207 
208  // populate some ASCII into the remainder of second header record
209 
210  memset(buffer, 0, 48);
211  sprintf((char*)buffer, "LAStools by Martin Isenburg");
212  if (!stream->putBytes((U8*)buffer, version-8))
213  {
214  fprintf(stderr,"ERROR: writing second header record of QFIT header\n");
215  return FALSE;
216  }
217 
218  memset(buffer, 0, 48);
219 
220  return TRUE;
221 }
222 
224 {
225  buffer[0] = I32_QUANTIZE(point->gps_time / 0.001);
226  if (buffer[0] < 0) buffer[0] *= -1;
227  if (rescale_reoffset)
228  {
229  buffer[2] = I32_QUANTIZE(point->get_x()/0.000001);
230  buffer[1] = I32_QUANTIZE(point->get_y()/0.000001);
231  buffer[3] = I32_QUANTIZE(point->get_z()/0.001);
232  }
233  else
234  {
235  buffer[2] = point->x;
236  buffer[1] = point->y;
237  buffer[3] = point->z;
238  }
239  if (buffer[2] < 0) buffer[2] += 360000000; // convert negative longitude to LARGE east
240  buffer[5] = point->intensity;
241  buffer[6] = I32_QUANTIZE(point->scan_angle_rank/0.001);
242 
246  if (pulse_width_array_offset > -1) { U8 pulse_width; point->get_extra_attribute(pulse_width_array_offset, pulse_width); buffer[10] = pulse_width; };
247 
248  if (endian_swap)
249  {
250  ENDIAN_SWAP_32((U8*)&buffer[0]);
251  ENDIAN_SWAP_32((U8*)&buffer[1]);
252  ENDIAN_SWAP_32((U8*)&buffer[2]);
253  ENDIAN_SWAP_32((U8*)&buffer[3]);
254  ENDIAN_SWAP_32((U8*)&buffer[5]);
255  ENDIAN_SWAP_32((U8*)&buffer[6]);
256  ENDIAN_SWAP_32((U8*)&buffer[7]);
257  ENDIAN_SWAP_32((U8*)&buffer[8]);
258  ENDIAN_SWAP_32((U8*)&buffer[10]);
259  }
260 
261  if (!stream->putBytes((U8*)buffer, version)) return FALSE;
262 
263  p_count++;
264  return TRUE;
265 }
266 
267 BOOL LASwriterQFIT::update_header(const LASheader* header, BOOL use_inventory, BOOL update_extra_bytes)
268 {
269  return TRUE;
270 }
271 
273 {
274  I64 bytes = 0;
275 
276  if (stream)
277  {
278  bytes = stream->tell();
279  delete stream;
280  stream = 0;
281  }
282 
283  if (file)
284  {
285  fclose(file);
286  file = 0;
287  }
288 
289  npoints = p_count;
290  p_count = 0;
291 
292  return bytes;
293 }
294 
296 {
297  stream = 0;
298  file = 0;
299  version = 0;
300  endian_swap = FALSE;
303  pitch_array_offset = -1;
304  roll_array_offset = -1;
306 }
307 
309 {
310  if (file) close();
311 }
LASwriterQFIT::LASwriterQFIT
LASwriterQFIT()
Definition: laswriter_qfit.cpp:295
LASquantizer::y_offset
F64 y_offset
Definition: lasdefinitions.hpp:96
LASheader
Definition: lasdefinitions.hpp:952
LASheader::min_y
F64 min_y
Definition: lasdefinitions.hpp:978
LASpoint::gps_time
F64 gps_time
Definition: lasdefinitions.hpp:497
LASpoint::scan_angle_rank
I8 scan_angle_rank
Definition: lasdefinitions.hpp:487
LASpoint
Definition: lasdefinitions.hpp:472
LASwriterQFIT::stream
ByteStreamOut * stream
Definition: laswriter_qfit.hpp:60
LASwriterQFIT::write_point
BOOL write_point(const LASpoint *point)
Definition: laswriter_qfit.cpp:223
I64
long long I64
Definition: mydefs.hpp:48
LASattributer::get_extra_attribute_array_offset
I32 get_extra_attribute_array_offset(const char *name) const
Definition: lasdefinitions.hpp:403
LASwriterQFIT::endian_swap
BOOL endian_swap
Definition: laswriter_qfit.hpp:63
LASwriter::p_count
I64 p_count
Definition: laswriter.hpp:52
LASwriterQFIT::pitch_array_offset
I32 pitch_array_offset
Definition: laswriter_qfit.hpp:67
LASquantizer::x_scale_factor
F64 x_scale_factor
Definition: lasdefinitions.hpp:92
I32
int I32
Definition: mydefs.hpp:35
TRUE
#define TRUE
Definition: mydefs.hpp:137
ByteStreamOutFile
Definition: bytestreamout_file.hpp:43
LASquantizer::x_offset
F64 x_offset
Definition: lasdefinitions.hpp:95
NULL
#define NULL
Definition: mydefs.hpp:141
ByteStreamOut::put32bitsLE
virtual BOOL put32bitsLE(const U8 *bytes)=0
LASwriterQFIT::open
BOOL open(const char *file_name, const LASheader *header, I32 version=48, U32 io_buffer_size=65536)
Definition: laswriter_qfit.cpp:50
ByteStreamOut::tell
virtual I64 tell() const =0
LASpoint::y
I32 y
Definition: lasdefinitions.hpp:479
LASwriterQFIT::pulse_width_array_offset
I32 pulse_width_array_offset
Definition: laswriter_qfit.hpp:69
LASwriterQFIT::roll_array_offset
I32 roll_array_offset
Definition: laswriter_qfit.hpp:68
ByteStreamOut
Definition: bytestreamout.hpp:36
I32_QUANTIZE
#define I32_QUANTIZE(n)
Definition: mydefs.hpp:111
LASpoint::get_extra_attribute
void get_extra_attribute(I32 index, U8 *data) const
Definition: lasdefinitions.hpp:842
LASwriterQFIT::rescale_reoffset
BOOL rescale_reoffset
Definition: laswriter_qfit.hpp:64
LASpoint::get_y
F64 get_y() const
Definition: lasdefinitions.hpp:812
LASheader::max_x
F64 max_x
Definition: lasdefinitions.hpp:975
LASquantizer::z_offset
F64 z_offset
Definition: lasdefinitions.hpp:97
laswriter_qfit.hpp
ENDIAN_SWAP_32
void ENDIAN_SWAP_32(U8 *field)
Definition: mydefs.hpp:167
U8
unsigned char U8
Definition: mydefs.hpp:41
BOOL
int BOOL
Definition: mydefs.hpp:57
LASpoint::z
I32 z
Definition: lasdefinitions.hpp:480
bytestreamout_file.hpp
LASwriterQFIT::~LASwriterQFIT
~LASwriterQFIT()
Definition: laswriter_qfit.cpp:308
FALSE
#define FALSE
Definition: mydefs.hpp:133
file
FILE * file
Definition: arithmeticencoder.cpp:77
ByteStreamOutFileBE
Definition: bytestreamout_file.hpp:87
LASwriterQFIT::file
FILE * file
Definition: laswriter_qfit.hpp:61
LASheader::max_y
F64 max_y
Definition: lasdefinitions.hpp:977
LASwriterQFIT::close
I64 close(BOOL update_npoints=true)
Definition: laswriter_qfit.cpp:272
LAS_TOOLS_VERSION
#define LAS_TOOLS_VERSION
Definition: lasdefinitions.hpp:47
LASwriter::npoints
I64 npoints
Definition: laswriter.hpp:51
LASheader::min_x
F64 min_x
Definition: lasdefinitions.hpp:976
LASwriterQFIT::version
I32 version
Definition: laswriter_qfit.hpp:62
IS_LITTLE_ENDIAN
BOOL IS_LITTLE_ENDIAN()
Definition: mydefs.hpp:144
U32
unsigned int U32
Definition: mydefs.hpp:39
LASwriterQFIT::buffer
I32 buffer[14]
Definition: laswriter_qfit.hpp:65
LASpoint::x
I32 x
Definition: lasdefinitions.hpp:478
LASpoint::get_z
F64 get_z() const
Definition: lasdefinitions.hpp:813
LASpoint::get_x
F64 get_x() const
Definition: lasdefinitions.hpp:811
LASwriterQFIT::scan_azimuth_array_offset
I32 scan_azimuth_array_offset
Definition: laswriter_qfit.hpp:66
LASquantizer::y_scale_factor
F64 y_scale_factor
Definition: lasdefinitions.hpp:93
ByteStreamOut::putBytes
virtual BOOL putBytes(const U8 *bytes, U32 num_bytes)=0
LASwriterQFIT::refile
BOOL refile(FILE *file)
Definition: laswriter_qfit.cpp:43
LASquantizer::z_scale_factor
F64 z_scale_factor
Definition: lasdefinitions.hpp:94
LASpoint::intensity
U16 intensity
Definition: lasdefinitions.hpp:481
ByteStreamOutFileLE
Definition: bytestreamout_file.hpp:67
LASwriterQFIT::update_header
BOOL update_header(const LASheader *header, BOOL use_inventory=TRUE, BOOL update_extra_bytes=FALSE)
Definition: laswriter_qfit.cpp:267


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:24