-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
executable file
·373 lines (303 loc) · 9.94 KB
/
main.c
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
/* ************************************************************
*
* MODULE: r.refine
*
* Authors: Jon Todd <[email protected]>, Laura Toma <[email protected]>
* Bowdoin College, USA
*
* Purpose: convert grid data to TIN
*
* COPYRIGHT:
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*
************************************************************ */
/******************************************************************************
*
* main.c contains the main function for r.refine, controls the high
* level working of the program, and parses arguments from the user.
*
* AUTHOR(S): Jonathan Todd - <[email protected]>
*
* UPDATED: jt 2005-08-11
*
* COMMENTS:
*
* r.refine can either be compiled for a GRASS or for standalone
* modes. In order to compile for grass the __GRASS__ flag must be
* defined in the makefile.
*
*****************************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <pthread.h>
#include <strings.h>
#include "rtimer.h"
#include "refine_tin.h"
#include "render_tin.h"
#ifdef __GRASS__
#include "grass.h"
#endif
// global tin needed for rendering in OpenGL
extern TIN *tinGlobal;
// parse arguments from the user
void parse_args(int argc, char *argv[],double *err,double *mem,
int *useNoData, int *delaunay, int *render,
char **outputFile,char **inputFile,
char **outputSites, char** outputVect);
int main(int argc, char** argv) {
Rtimer refineTime;
Rtimer totalTime;
Rtimer outputTime;
char buf1[1000];
char buf2[1000];
char buf3[1000];
tinGlobal = NULL;
TILED_GRID *gridFile = NULL;
BOOL doRefine = 0; // Should we refine? Default: No
// default refinement parameters
double err = 1; // Default error = 1%
double errAmt = 0.01; // Default error = 1%
double mem = 250; // Default memory size 250mb
int useNoData = 0; // Default to not use nodata points
int delaunay = 1; // Default to use delaunay
int doRender = 0; // Default to not render the tin
char *outputFile = NULL; // File to save tin as
char *inputFile = NULL; // Input grid file
char *outputSites = NULL; // Output filename for sites
char *outputVect = NULL; // Output filename for vectors
//
// GRASS only initializations
//
#ifdef __GRASS__
// initialize GIS library
G_gisinit(argv[0]);
struct GModule *module;
module = G_define_module();
module->description ="r.refine: scalable raster-to-TIN simplification.";
// get the current region and dimensions
struct Cell_head * region;
region = (struct Cell_head*)malloc(sizeof(struct Cell_head));
assert(region);
if (G_get_set_window(region) == -1) {
G_fatal_error("r.refine: error getting current region");
}
int nr = G_window_rows();
int nc = G_window_cols();
fprintf(stderr, "region size is %d x %d\n", nr, nc);
#endif
// get parameters from user arguments
parse_args(argc,argv,&err,&mem,&useNoData,&delaunay,&doRender,&outputFile,
&inputFile,&outputSites, &outputVect);
// import from grid if we have an inputFile
if(inputFile != NULL){
#ifdef __GRASS__
gridFile = raster2tiledGrid(inputFile,nr,nc,getTileLength(mem));
#else
gridFile = readGrid2Tile(inputFile,getTileLength(mem));
#endif
}
// start total timer
rt_start(totalTime);
// initialize a tin if we have a grid
if(gridFile != NULL){
doRefine = 1;
tinGlobal = initTin(gridFile, err, mem,useNoData,outputFile);
}
// if we just initialized the tin then we will refine it
if(doRefine){
// start refine timer
rt_start(refineTime);
// make err a percentage of the max and min elevations
errAmt = ((double)(tinGlobal->max - tinGlobal->min)) * (err/100.0);
// refine the tin
refineTin(errAmt,delaunay,tinGlobal,outputFile,outputSites,outputVect,useNoData);
// stop timers and print their values to a buffer for output
rt_stop(refineTime);
rt_stop(totalTime);
rt_sprint(buf1,totalTime);
rt_sprint(buf2,refineTime);
rt_sprint(buf3,outputTime);
// report the stats
printf(".......DONE........\n");
//printf("%s\n", argv[1]);
printf("err=%.2f%c absErr=%.2f mem=%.2fMB numTiles=%d\n",
err,'%', errAmt, mem, tinGlobal->numTiles);
printf("raster: %ld points\n",(long)tinGlobal->nrows*tinGlobal->ncols);
printf("TIN: triangles=%ld points=%ld\n",
(long)tinGlobal->numTris,
(long)tinGlobal->numPoints);
printf("total time: %s\n", buf1);
}
// if user wants to render then we pass control to OpenGL. Once
// control is passed there is no way to execute further code so this
// should always be done last unless using threads
if(doRender){
tinGlobal = readTinFile(outputFile);
OGL_init(&argc,argv);
OGL_render(argc, argv);
}
return 0;
}
//
// parse_args code for the GRASS and stand alone versions
//
#ifdef __GRASS__
void parse_args(int argc, char *argv[],double *err,double *mem,
int *useNoData, int *delaunay, int *render,
char **outputFile,char **inputFile,
char **outputSites, char** outputVect) {
// input grid
struct Option *input_grid;
input_grid = G_define_option() ;
input_grid->key = "grid";
input_grid->type = TYPE_STRING;
input_grid->required = YES;
input_grid->gisprompt = "old,cell,raster" ;
input_grid->description= "Input raster" ;
// error threshold value, in percentage
struct Option *epsilon;
epsilon = G_define_option();
epsilon->key = "epsilon";
epsilon->type = TYPE_DOUBLE;
epsilon->required = NO;
epsilon->answer = "1.0"; // default value
epsilon->description = "Error threshold, in percentage of max elevation";
// output tin
struct Option *output_file;
output_file = G_define_option() ;
output_file->key = "tin";
output_file->type = TYPE_STRING;
output_file->required = NO;
output_file->answer = "output.tin";
output_file->description= "Output TIN file" ;
// output sites
struct Option *output_sites;
output_sites = G_define_option() ;
output_sites->key = "output_sites";
output_sites->type = TYPE_STRING;
output_sites->required = NO;
output_sites->answer = "NULL";
output_sites->gisprompt = "new,site_lists,sites";
output_sites->description= "Name of output sites file." ;
// output sites
struct Option *output_vect;
output_vect = G_define_option() ;
output_vect->key = "output_vect";
output_vect->type = TYPE_STRING;
output_vect->required = NO;
output_vect->answer = "NULL";
output_vect->gisprompt = "new,vector,vector";
output_vect->description= "Name of output vector file." ;
// main memory
struct Option *memory;
memory = G_define_option() ;
memory->key = "memory";
memory->type = TYPE_INTEGER;
memory->required = NO;
memory->answer = "500"; // 300MB default value
memory->description = "Main memory size (in MB)";
// Use Delaunay ?
struct Flag *del;
del = G_define_flag() ;
del->key = 'd';
del->description = "Do NOT use Delaunay triangulation";
// Use nodata points?
struct Flag *no_data;
no_data = G_define_flag() ;
no_data->key = 'n';
no_data->description = "Include nodata points (in simplification)";
// render tin ?
struct Flag *render_tin;
render_tin = G_define_flag() ;
render_tin->key = 'r';
render_tin->description= "Render TIN in OpenGL" ;
if (G_parser(argc, argv)) {
exit (-1);
}
// print out
*err = strtod(epsilon->answer, NULL);
*mem = strtol(memory->answer,NULL,10);
*inputFile = input_grid->answer;
*outputFile = output_file->answer;
if (strcmp("NULL", output_sites->answer) == 0)
*outputSites = NULL;
else *outputSites = output_sites->answer;
if (strcmp("NULL", output_vect->answer) == 0)
*outputVect = NULL;
else *outputVect = output_vect->answer;
//default is 0
if (no_data->answer) *useNoData = 1;
//default is 1
if (del->answer) *delaunay = 0;
//default is 0
if (render_tin->answer) *render = 1;
printf("%s grid=%s output=%s output-sites=%s outputVect=%s "
"error=%.2f mem=%.2f delaunay=%d no_data=%d render=%d\n",
argv[0], *inputFile, *outputFile, *outputSites, *outputVect,
*err, *mem, *delaunay, *useNoData, *render);
}
#else
void parse_args(int argc, char *argv[],double *err,double *mem,
int *useNoData, int *delaunay, int *render,
char **outputFile, char **inputFile,
char **outputSites, char **outputVect){
// check for an import... if so we just display tin
if (argc >= 3 && strcmp(argv[2],"import")==0){
// since we render the outputFile, we will set the outputfile to
// be the name of the tin we are importing
*outputFile = argv[1];
if(argc == 4){
if(strcmp(argv[3],"render")==0)
*render = 1;
else
*render = 0;
}
}
// validate the number of arguments
else if (argc < 4){
printf("usage: r.refine <intput-grid> <output-tin> <error> [memory in MB]"
"[delaunay] [nodata] [render]\n");
printf(" tin <input-tin> import [render]\n");
exit(1);
}
// since we are not importing the TIN lets read in the gird
else{
*inputFile = argv[1];
*outputFile = argv[2];
// convert error value from char to float
sscanf(argv[3],"%lf",err);
// set memory
if(argc >= 5)
sscanf(argv[4],"%lf",mem);
else {
printf("Assuming 250 MB of memory.\n");
}
// set delaunay
if(argc >= 6)
sscanf(argv[5],"%d",delaunay);
else {
printf("Assuming Delaunay is on.\n");
}
// set nodata handeling
if(argc >= 7)
sscanf(argv[6],"%d",useNoData);
else {
printf("Assuming no to handle nodata points\n");
}
// decide whether or not to render
if(argc == 8){
if(strcmp(argv[7],"render")==0){
*render = 1;
}
else
*render = 0;
}
}
}
#endif