-
Notifications
You must be signed in to change notification settings - Fork 36
/
Interpolation.cpp
435 lines (345 loc) · 12.2 KB
/
Interpolation.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
*
COPYRIGHT AND LICENSE
Copyright (c) 2011 The Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
3. All advertising materials mentioning features or use of this
software must display the following acknowledgement: This product
includes software developed by the San Diego Supercomputer Center.
4. Neither the names of the Centers nor the names of the contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*
*
* Based on the notes by Prof. Ramon Arrowsmith([email protected])
* Authors: Han S Kim ([email protected]), Sriram Krishnan ([email protected])
*
*/
#include "Interpolation.h"
#include "Global.h"
#include <string.h>
#include <math.h>
#include <float.h>
#include <stddef.h>
#include <stdlib.h>
#include <time.h>
#include <sys/times.h>
#include <stdio.h>
#include <string.h>
#include <fstream> // std::ifstream
#include <iostream> // std::cout
#ifdef USE_LIBLAS_17
//if binding against liblas 1.7+ API, use C API calls
//(backported from CRREL codebase)
extern "C" {
#include <liblas/capi/liblas.h> // the C API is stable across liblas versions
}
#else
//use liblas 1.2.1 C++ API by default
#include <liblas/laspoint.hpp>
#include <liblas/lasreader.hpp>
#endif
/////////////////////////////////////////////////////////////
// Public Methods
/////////////////////////////////////////////////////////////
//x_dist and y_dist represent length and height of each grid cell (resolution)
//radius represents search radius.
//window size represents fill window size
Interpolation::Interpolation(double x_dist, double y_dist, double radius, int _window_size) : GRID_DIST_X (x_dist), GRID_DIST_Y(y_dist)
{
data_count = 0;
radius_sqr = radius * radius;
window_size = _window_size;
min_x = DBL_MAX;
min_y = DBL_MAX;
max_x = -DBL_MAX;
max_y = -DBL_MAX;
}
Interpolation::~Interpolation()
{
delete interp;
}
int Interpolation::init(char *inputName, int inputFormat)
{
clock_t t0, t1;
//////////////////////////////////////////////////////////////////////
// MIN/MAX SEARCHING
// TODO: the size of data, min, max of each coordinate
// are required to implement streaming processing....
//
// This code can be eliminated if database can provide these values
//////////////////////////////////////////////////////////////////////
t0 = clock();
if(inputName == NULL)
{
cout << "Wrong Input File Name" << endl;
return -1;
}
printf("inputName: '%s'\n", inputName);
if (inputFormat == INPUT_ASCII) {
FILE *fp;
char line[1024];
double data_x, data_y;
//double data_z;
if((fp = fopen64(inputName, "r")) == NULL)
{
cout << "file open error" << endl;
return -1;
}
// throw the first line away - it contains the header
fgets(line, sizeof(line), fp);
// read the data points to find min and max values
while(fgets(line, sizeof(line), fp) != NULL)
{
data_x = atof(strtok(line, ",\n"));
if(min_x > data_x) min_x = data_x;
if(max_x < data_x) max_x = data_x;
data_y = atof(strtok(NULL, ",\n"));
if(min_y > data_y) min_y = data_y;
if(max_y < data_y) max_y = data_y;
data_count++;
}
fclose(fp);
} else { // las input
#ifdef USE_LIBLAS_17
LASReaderH lr = LASReader_Create(inputName);
if (!lr) {
LASError_Print("Could not open file to read.");
return -1;
}
LASHeaderH lh = LASReader_GetHeader(lr);
if (!lh) {
LASError_Print("error while reading LAS file: verify that the input is valid LAS file.");
LASReader_Destroy(lr);
return -1;
}
min_x = LASHeader_GetMinX(lh);
min_y = LASHeader_GetMinY(lh);
max_x = LASHeader_GetMaxX(lh);
max_y = LASHeader_GetMaxY(lh);
data_count = LASHeader_GetPointRecordsCount(lh);
LASReader_Destroy(lr);
#else
try {
std::ifstream ifs;
ifs.open(inputName, std::ios::in | std::ios::binary);
// create a las file reader
liblas::LASReader reader(ifs);
/// get header information
liblas::LASHeader const& header = reader.GetHeader();
min_x = header.GetMinX();
min_y = header.GetMinY();
max_x = header.GetMaxX();
max_y = header.GetMaxY();
data_count = header.GetPointRecordsCount();
ifs.close();
} catch (std::runtime_error &e) {
cout << "error while reading LAS file: verify that the input is valid LAS file" << endl;
return -1;
}
#endif
}
t1 = clock();
printf("Min/Max searching time: %10.2f\n", (double)(t1 - t0)/CLOCKS_PER_SEC);
//////////////////////////////////////////////////////////////////////
// Intialization Step excluding min/max searching
//////////////////////////////////////////////////////////////////////
/*
for(i = 0; i < data_count; i++)
{
arrX[i] -= min_x;
arrY[i] -= min_y;
//printf("%f,%f,%f\n", arrX[i] , arrY[i] ,arrZ[i]);
}
*/
#ifdef ALIGN_ORIGIN
cout.flags ( ios::fixed);
cout.width (10);
cout << "Bounding Box:\t(" << min_x << ", " << min_y << "), (" << max_x << ", " << max_y << ")\n";
//a bounding box/grid based on min/max x and y values is not likely to be aligned to origin.
//to align to origin, the origin must be a grid point, and all other grid points must be
//multiples of the grid resolution (GRID_DIST_X/Y).
//By default, p2g makes the lower-left corner of the bounding box of the points (min_x, min_y)
//the lower-left corner of the grid, and the upper-right corner of the grid will be some multiple
//of GRID_DISTX/Y that will encompass (max_x,max_y).
//We align the grid to origin by extending the lower-left corner of the bounding box south-west
//until it aligns with the closest grid-point. The new lower-left corner will become the new min_x, min_y.
//this works because min/max values are only used to determine the size of the grid; interp does not
//require a point to exist at min_x and/or min_y. Also, because interp iterates through the point-cloud,
//padding the grid does not impact DEM generation time.
min_x = floor(min_x/GRID_DIST_X) * GRID_DIST_X;
min_y = floor(min_y/GRID_DIST_Y) * GRID_DIST_Y;
//max_x and max_y record the true upper-right boundary of the point-cloud and are not similarly padded.
//The upper-right corner of the grid will be aligned to origin, being an multiple of GRID_DIST_X/Y.
GRID_SIZE_X = (int)((max_x - min_x)/GRID_DIST_X) + 1;
GRID_SIZE_Y = (int)((max_y - min_y)/GRID_DIST_Y) + 1;
double grid_max_x = GRID_SIZE_X * GRID_DIST_X + min_x;
double grid_max_y = GRID_SIZE_Y * GRID_DIST_Y + min_y;
cout << "Aligned Grid:\t(" << min_x << ", " << min_y << "), (" << grid_max_x << ", " << grid_max_y << ")\n";
#else
cout << "ORIGINAL\tmin_x: " << min_x << ", max_x: " << max_x << ", min_y: " << min_y << ", max_y: " << max_y << endl;
GRID_SIZE_X = (int)((max_x - min_x)/GRID_DIST_X) + 1;
GRID_SIZE_Y = (int)((max_y - min_y)/GRID_DIST_Y) + 1;
#endif
cout << "GRID_SIZE_X " << GRID_SIZE_X << endl;
cout << "GRID_SIZE_Y " << GRID_SIZE_Y << endl;
cout << "NUMBER OF GRID POINTS: " << GRID_SIZE_X * GRID_SIZE_Y << "\n";
#ifdef HARD_LIMIT
if((GRID_SIZE_X * GRID_SIZE_Y) > HARD_LIMIT)
{
cout << "size of grid exceeds hard limit. Aborting...\n";
return -1;
}
#endif
// if the size is too big to fit in memory,
// then construct out-of-core structure
if(GRID_SIZE_X * GRID_SIZE_Y > MEM_LIMIT)
{
core_mode = OUTCORE;
interp = new OutCoreInterp(GRID_DIST_X, GRID_DIST_Y, GRID_SIZE_X, GRID_SIZE_Y, radius_sqr, min_x, max_x, min_y, max_y, window_size);
if(interp == NULL)
{
cout << "OutCoreInterp construction error" << endl;
return -1;
}
cout << "Interpolation uses out-of-core algorithm" << endl;
// else
// do normal
} else {
core_mode = INCORE;
interp = new InCoreInterp(GRID_DIST_X, GRID_DIST_Y, GRID_SIZE_X, GRID_SIZE_Y, radius_sqr, min_x, max_x, min_y, max_y, window_size);
cout << "Interpolation uses in-core algorithm" << endl;
}
if(interp->init() < 0)
{
cout << "inter->init() error" << endl;
return -1;
}
cout << "Interpolation::init() done successfully" << endl;
return 0;
}
int Interpolation::interpolation(char *inputName,
char *outputName,
int inputFormat,
int outputFormat,
unsigned int outputType)
{
int rc;
//unsigned int i;
double data_x, data_y;
double data_z;
printf("Interpolation Starts\n");
if (inputFormat == INPUT_ASCII) {
FILE *fp;
char line[1024];
if((fp = fopen64(inputName, "r")) == NULL)
{
printf("file open error\n");
return -1;
}
// throw the first line away - it contains the header
fgets(line, sizeof(line), fp);
// read every point and generate DEM
while(fgets(line, sizeof(line), fp) != NULL)
{
data_x = atof(strtok(line, ",\n"));
data_y = atof(strtok(NULL, ",\n"));
data_z = atof(strtok(NULL, ",\n"));
data_x -= min_x;
data_y -= min_y;
//if((rc = interp->update(arrX[i], arrY[i], arrZ[i])) < 0)
if((rc = interp->update(data_x, data_y, data_z)) < 0)
{
cout << "interp->update() error while processing " << endl;
return -1;
}
}
fclose(fp);
} else { // input format is LAS
#ifdef USE_LIBLAS_17
LASReaderH lr = LASReader_Create(inputName);
if (!lr) {
LASError_Print("Could not open file to read.");
return -1;
}
LASPointH lp;
while ((lp = LASReader_GetNextPoint(lr))) {
data_x = LASPoint_GetX(lp);
data_y = LASPoint_GetY(lp);
data_z = LASPoint_GetZ(lp);
data_x -= min_x;
data_y -= min_y;
if ((rc = interp->update(data_x, data_y, data_z)) < 0) {
cout << "interp->update() error while processing " << endl;
return -1;
}
}
LASReader_Destroy(lr);
#else
// instantiate a reader for the LAS file
std::ifstream ifs;
ifs.open(inputName, std::ios::in | std::ios::binary);
liblas::LASReader reader(ifs);
// process every point in the LAS file, and generate DEM
while (reader.ReadNextPoint()) {
liblas::LASPoint const& p = reader.GetPoint();
data_x = p.GetX();
data_y = p.GetY();
data_z = p.GetZ();
data_x -= min_x;
data_y -= min_y;
//if((rc = interp->update(arrX[i], arrY[i], arrZ[i])) < 0)
if((rc = interp->update(data_x, data_y, data_z)) < 0)
{
cout << "interp->update() error while processing " << endl;
return -1;
}
}
ifs.close();
#endif
}
if((rc = interp->finish(outputName, outputFormat, outputType)) < 0)
{
cout << "interp->finish() error" << endl;
return -1;
}
cout << "Interpolation::interpolation() done successfully" << endl;
return 0;
}
void Interpolation::setRadius(double r)
{
radius_sqr = r * r;
}
unsigned int Interpolation::getDataCount()
{
return data_count;
}
unsigned int Interpolation::getGridSizeX()
{
return GRID_SIZE_X;
}
unsigned int Interpolation::getGridSizeY()
{
return GRID_SIZE_Y;
}