forked from LAStools/LAStools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlasexample.cpp
202 lines (165 loc) · 5.26 KB
/
lasexample.cpp
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
/*
===============================================================================
FILE: lasexample.cpp
CONTENTS:
This source code serves as an example how you can easily use LASlib to
write your own processing tools or how to import from and export to the
LAS format or - its compressed, but identical twin - the LAZ format.
PROGRAMMERS:
[email protected] - https://rapidlasso.de
COPYRIGHT:
(c) 2007-2014, rapidlasso GmbH - fast tools to catch reality
This is free software; you can redistribute and/or modify it under the
terms of the GNU Lesser General Licence as published by the Free Software
Foundation. See the LICENSE.txt file for more information.
This software is distributed WITHOUT ANY WARRANTY and without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CHANGE HISTORY:
3 January 2011 -- created while too homesick to go to Salzburg with Silke
===============================================================================
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lasreader.hpp"
#include "laswriter.hpp"
void usage(bool wait=false)
{
fprintf(stderr,"usage:\n");
fprintf(stderr,"lasexample in.las out.las\n");
fprintf(stderr,"lasexample -i in.las -o out.las -verbose\n");
fprintf(stderr,"lasexample -ilas -olas < in.las > out.las\n");
fprintf(stderr,"lasexample -h\n");
if (wait)
{
fprintf(stderr,"<press ENTER>\n");
getc(stdin);
}
exit(1);
}
static void byebye(bool error=false, bool wait=false)
{
if (wait)
{
fprintf(stderr,"<press ENTER>\n");
getc(stdin);
}
exit(error);
}
static double taketime()
{
return (double)(clock())/CLOCKS_PER_SEC;
}
int main(int argc, char *argv[])
{
int i;
bool verbose = false;
double start_time = 0.0;
LASreadOpener lasreadopener;
LASwriteOpener laswriteopener;
if (argc == 1)
{
fprintf(stderr,"%s is better run in the command line\n", argv[0]);
char file_name[256];
fprintf(stderr,"enter input file: "); fgets(file_name, 256, stdin);
file_name[strlen(file_name)-1] = '\0';
lasreadopener.set_file_name(file_name);
fprintf(stderr,"enter output file: "); fgets(file_name, 256, stdin);
file_name[strlen(file_name)-1] = '\0';
laswriteopener.set_file_name(file_name);
}
else
{
lasreadopener.parse(argc, argv);
laswriteopener.parse(argc, argv);
}
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '\0')
{
continue;
}
else if (strcmp(argv[i],"-h") == 0 || strcmp(argv[i],"-help") == 0)
{
usage();
}
else if (strcmp(argv[i],"-v") == 0 || strcmp(argv[i],"-verbose") == 0)
{
verbose = true;
}
else if (i == argc - 2 && !lasreadopener.active() && !laswriteopener.active())
{
lasreadopener.set_file_name(argv[i]);
}
else if (i == argc - 1 && !lasreadopener.active() && !laswriteopener.active())
{
lasreadopener.set_file_name(argv[i]);
}
else if (i == argc - 1 && lasreadopener.active() && !laswriteopener.active())
{
laswriteopener.set_file_name(argv[i]);
}
else
{
fprintf(stderr, "ERROR: cannot understand argument '%s'\n", argv[i]);
usage();
}
}
if (verbose) start_time = taketime();
// check input & output
if (!lasreadopener.active())
{
fprintf(stderr,"ERROR: no input specified\n");
usage(argc == 1);
}
if (!laswriteopener.active())
{
fprintf(stderr,"ERROR: no output specified\n");
usage(argc == 1);
}
// open lasreader
LASreader* lasreader = lasreadopener.open();
if (lasreader == 0)
{
fprintf(stderr, "ERROR: could not open lasreader\n");
byebye(argc==1);
}
// open laswriter
LASwriter* laswriter = laswriteopener.open(&lasreader->header);
if (laswriter == 0)
{
fprintf(stderr, "ERROR: could not open laswriter\n");
byebye(argc==1);
}
#ifdef _WIN32
if (verbose) fprintf(stderr, "reading %I64d points from '%s' and writing them modified to '%s'.\n", lasreader->npoints, lasreadopener.get_file_name(), laswriteopener.get_file_name());
#else
if (verbose) fprintf(stderr, "reading %lld points from '%s' and writing them modified to '%s'.\n", lasreader->npoints, lasreadopener.get_file_name(), laswriteopener.get_file_name());
#endif
// loop over points and modify them
// where there is a point to read
while (lasreader->read_point())
{
// modify the point
lasreader->point.set_point_source_ID(1020);
lasreader->point.set_user_data(42);
if (lasreader->point.get_classification() == 12) lasreader->point.set_classification(1);
lasreader->point.set_Z(lasreader->point.get_Z() + 10);
// write the modified point
laswriter->write_point(&lasreader->point);
// add it to the inventory
laswriter->update_inventory(&lasreader->point);
}
laswriter->update_header(&lasreader->header, TRUE);
I64 total_bytes = laswriter->close();
delete laswriter;
#ifdef _WIN32
if (verbose) fprintf(stderr,"total time: %g sec %I64d bytes for %I64d points\n", taketime()-start_time, total_bytes, lasreader->p_count);
#else
if (verbose) fprintf(stderr,"total time: %g sec %lld bytes for %lld points\n", taketime()-start_time, total_bytes, lasreader->p_count);
#endif
lasreader->close();
delete lasreader;
return 0;
}