1 /* splittris.c -- reassemble the pieces produced by splittris
3 * Written by Curtis Olson, started January 1998.
5 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * (Log is kept at end of this file) */
27 #include <stdlib.h> /* for atoi() */
29 #include <sys/stat.h> /* for stat() */
30 #include <unistd.h> /* for stat() */
32 #include "assemtris.h"
34 #include "../../Src/Include/constants.h"
35 #include "../../Src/Include/types.h"
36 #include "../../Src/Math/fg_geodesy.h"
37 #include "../../Src/Math/mat3.h"
38 #include "../../Src/Math/polar.h"
39 #include "../../Src/Scenery/tileutils.h"
44 float nodes[MAX_NODES][3];
47 struct bucket my_index;
48 struct bucket ne_index, nw_index, sw_index, se_index;
49 struct bucket north_index, south_index, east_index, west_index;
52 /* return the file base name ( foo/bar/file.ext = file.ext ) */
53 void extract_file(char *in, char *base) {
59 while ( (i >= 0) && (in[i] != '/') ) {
68 /* return the file path name ( foo/bar/file.ext = foo/bar ) */
69 void extract_path(char *in, char *base) {
76 while ( (i >= 0) && (in[i] != '/') ) {
84 /* check if a file exists */
85 int file_exists(char *file) {
89 printf("checking %s ... ", file);
91 result = stat(file, &stat_buf);
94 /* stat failed, no file */
95 printf("not found.\n");
98 /* stat succeeded, file exists */
105 /* check to see if a shared object exists */
106 int shared_object_exists(char *basepath, char *ext, char *file) {
107 char scene_path[256];
110 if ( strcmp(ext, ".sw") == 0 ) {
111 gen_base_path(&my_index, scene_path);
112 index = gen_index(&my_index);
113 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
114 if ( file_exists(file) ) {
117 gen_base_path(&west_index, scene_path);
118 index = gen_index(&west_index);
119 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
120 if ( file_exists(file) ) {
123 gen_base_path(&sw_index, scene_path);
124 index = gen_index(&sw_index);
125 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
126 if ( file_exists(file) ) {
129 gen_base_path(&south_index, scene_path);
130 index = gen_index(&south_index);
131 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
132 if ( file_exists(file) ) {
137 if ( strcmp(ext, ".se") == 0 ) {
138 gen_base_path(&my_index, scene_path);
139 index = gen_index(&my_index);
140 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
141 if ( file_exists(file) ) {
144 gen_base_path(&east_index, scene_path);
145 index = gen_index(&east_index);
146 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
147 if ( file_exists(file) ) {
150 gen_base_path(&se_index, scene_path);
151 index = gen_index(&se_index);
152 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
153 if ( file_exists(file) ) {
156 gen_base_path(&south_index, scene_path);
157 index = gen_index(&south_index);
158 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
159 if ( file_exists(file) ) {
164 if ( strcmp(ext, ".ne") == 0 ) {
165 gen_base_path(&my_index, scene_path);
166 index = gen_index(&my_index);
167 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
168 if ( file_exists(file) ) {
171 gen_base_path(&east_index, scene_path);
172 index = gen_index(&east_index);
173 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
174 if ( file_exists(file) ) {
177 gen_base_path(&ne_index, scene_path);
178 index = gen_index(&ne_index);
179 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
180 if ( file_exists(file) ) {
183 gen_base_path(&north_index, scene_path);
184 index = gen_index(&north_index);
185 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
186 if ( file_exists(file) ) {
191 if ( strcmp(ext, ".nw") == 0 ) {
192 gen_base_path(&my_index, scene_path);
193 index = gen_index(&my_index);
194 sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
195 if ( file_exists(file) ) {
198 gen_base_path(&west_index, scene_path);
199 index = gen_index(&west_index);
200 sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
201 if ( file_exists(file) ) {
204 gen_base_path(&nw_index, scene_path);
205 index = gen_index(&nw_index);
206 sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
207 if ( file_exists(file) ) {
210 gen_base_path(&north_index, scene_path);
211 index = gen_index(&north_index);
212 sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
213 if ( file_exists(file) ) {
218 if ( strcmp(ext, ".south") == 0 ) {
219 gen_base_path(&my_index, scene_path);
220 index = gen_index(&my_index);
221 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
222 if ( file_exists(file) ) {
225 gen_base_path(&south_index, scene_path);
226 index = gen_index(&south_index);
227 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
228 if ( file_exists(file) ) {
233 if ( strcmp(ext, ".north") == 0 ) {
234 gen_base_path(&my_index, scene_path);
235 index = gen_index(&my_index);
236 sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
237 if ( file_exists(file) ) {
240 gen_base_path(&north_index, scene_path);
241 index = gen_index(&north_index);
242 sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
243 if ( file_exists(file) ) {
248 if ( strcmp(ext, ".west") == 0 ) {
249 gen_base_path(&my_index, scene_path);
250 index = gen_index(&my_index);
251 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
252 if ( file_exists(file) ) {
255 gen_base_path(&west_index, scene_path);
256 index = gen_index(&west_index);
257 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
258 if ( file_exists(file) ) {
263 if ( strcmp(ext, ".east") == 0 ) {
264 gen_base_path(&my_index, scene_path);
265 index = gen_index(&my_index);
266 sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
267 if ( file_exists(file) ) {
270 gen_base_path(&east_index, scene_path);
271 index = gen_index(&east_index);
272 sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
273 if ( file_exists(file) ) {
278 if ( strcmp(ext, ".body") == 0 ) {
279 gen_base_path(&my_index, scene_path);
280 index = gen_index(&my_index);
281 sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
282 if ( file_exists(file) ) {
291 /* my custom file opening routine ... don't open if a shared edge or
292 * vertex alread exists */
293 FILE *my_open(char *basename, char *basepath, char *ext) {
297 /* check if a shared object already exists */
298 if ( shared_object_exists(basepath, ext, filename) ) {
299 /* not an actual file open error, but we've already got the
300 * shared edge, so we don't want to create another one */
301 fp = fopen(filename, "r");
302 printf("Opening %s\n", filename);
306 printf("not opening\n");
312 /* given a file pointer, read all the gdn (geodetic nodes from it) */
313 void read_nodes(FILE *fp) {
316 while ( fgets(line, 250, fp) != NULL ) {
317 if ( strncmp(line, "gdn ", 4) == 0 ) {
318 sscanf(line, "gdn %f %f %f\n", &nodes[nodecount][0],
319 &nodes[nodecount][1], &nodes[nodecount][2]);
321 printf("read_nodes(%d) %.2f %.2f %.2f %s", nodecount,
322 nodes[nodecount][0], nodes[nodecount][1],
323 nodes[nodecount][2], line);
331 /* load in nodes from the various split and shared pieces to
332 * reconstruct a tile */
333 void build_node_list(char *basename, char *basepath) {
334 FILE *ne, *nw, *se, *sw, *north, *south, *east, *west, *body;
336 ne = my_open(basename, basepath, ".ne");
340 nw = my_open(basename, basepath, ".nw");
344 se = my_open(basename, basepath, ".se");
348 sw = my_open(basename, basepath, ".sw");
352 north = my_open(basename, basepath, ".north");
356 south = my_open(basename, basepath, ".south");
360 east = my_open(basename, basepath, ".east");
364 west = my_open(basename, basepath, ".west");
368 body = my_open(basename, basepath, ".body");
374 /* dump in WaveFront .obj format */
375 void dump_nodes(char *basename) {
380 /* generate output file name */
381 strcpy(file, basename);
384 strcat(file, ".node");
387 printf("Creating node file: %s\n", file);
388 printf(" writing vertices in .node format.\n");
389 fp = fopen(file, "w");
391 fprintf(fp, "%d 2 1 0\n", nodecount);
393 /* now write out actual node data */
394 for ( i = 0; i < nodecount; i++ ) {
395 fprintf(fp, "%d %.2f %.2f %.2f 0\n", i + 1,
396 nodes[i][0], nodes[i][1], nodes[i][2]);
403 int main(int argc, char **argv) {
404 char basename[256], basepath[256], temp[256];
408 strcpy(basename, argv[1]);
410 /* find the base path of the file */
411 extract_path(basename, basepath);
412 extract_path(basepath, basepath);
413 extract_path(basepath, basepath);
414 printf("%s\n", basepath);
416 /* find the index of the current file */
417 extract_file(basename, temp);
422 tmp_index = atoi(temp);
423 printf("%ld\n", tmp_index);
424 parse_index(tmp_index, &my_index);
426 printf("bucket = %d %d %d %d\n",
427 my_index.lon, my_index.lat, my_index.x, my_index.y);
428 /* generate the indexes of the neighbors */
429 offset_bucket(&my_index, &ne_index, 1, 1);
430 offset_bucket(&my_index, &nw_index, -1, 1);
431 offset_bucket(&my_index, &se_index, 1, -1);
432 offset_bucket(&my_index, &sw_index, -1, -1);
434 offset_bucket(&my_index, &north_index, 0, 1);
435 offset_bucket(&my_index, &south_index, 0, -1);
436 offset_bucket(&my_index, &east_index, 1, 0);
437 offset_bucket(&my_index, &west_index, -1, 0);
440 printf("Corner indexes = %ld %ld %ld %ld\n",
441 ne_index, nw_index, sw_index, se_index);
442 printf("Edge indexes = %ld %ld %ld %ld\n",
443 north_index, south_index, east_index, west_index);
446 /* load the input data files */
447 build_node_list(basename, basepath);
449 /* dump in WaveFront .obj format */
450 dump_nodes(basename);
457 /* Revision 1.2 1998/01/15 21:33:36 curt
458 /* Assembling triangles and building a new .node file with the proper shared
459 /* vertices now works. Now we just have to use the shared normals and we'll
462 * Revision 1.1 1998/01/15 02:45:26 curt