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 
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 
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 }
ByteStreamOut * stream
BOOL IS_LITTLE_ENDIAN()
Definition: mydefs.hpp:144
virtual BOOL putBytes(const U8 *bytes, U32 num_bytes)=0
#define I32_QUANTIZE(n)
Definition: mydefs.hpp:111
int BOOL
Definition: mydefs.hpp:57
#define FALSE
Definition: mydefs.hpp:133
void ENDIAN_SWAP_32(U8 *field)
Definition: mydefs.hpp:167
unsigned int U32
Definition: mydefs.hpp:39
BOOL write_point(const LASpoint *point)
virtual I64 tell() const =0
F64 get_y() const
long long I64
Definition: mydefs.hpp:48
I32 scan_azimuth_array_offset
unsigned char U8
Definition: mydefs.hpp:41
F64 get_x() const
I64 close(BOOL update_npoints=true)
FILE * file
int I32
Definition: mydefs.hpp:35
BOOL refile(FILE *file)
BOOL update_header(const LASheader *header, BOOL use_inventory=TRUE, BOOL update_extra_bytes=FALSE)
I64 npoints
Definition: laswriter.hpp:51
virtual BOOL put32bitsLE(const U8 *bytes)=0
F64 get_z() const
#define LAS_TOOLS_VERSION
BOOL open(const char *file_name, const LASheader *header, I32 version=48, U32 io_buffer_size=65536)
#define TRUE
Definition: mydefs.hpp:137
I32 get_extra_attribute_array_offset(const char *name) const
I32 pulse_width_array_offset
void get_extra_attribute(I32 index, U8 *data) const
#define NULL
Definition: mydefs.hpp:141
I64 p_count
Definition: laswriter.hpp:52


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 Mon Feb 28 2022 22:46:07