+/* return the file base name ( foo/bar/file.ext = file.ext ) */
+void extract_file(char *in, char *base) {
+ int len, i;
+
+ len = strlen(in);
+
+ i = len - 1;
+ while ( (i >= 0) && (in[i] != '/') ) {
+ i--;
+ }
+
+ in += (i + 1);
+ strcpy(base, in);
+}
+
+
+/* return the file path name ( foo/bar/file.ext = foo/bar ) */
+void extract_path(char *in, char *base) {
+ int len, i;
+
+ len = strlen(in);
+ strcpy(base, in);
+
+ i = len - 1;
+ while ( (i >= 0) && (in[i] != '/') ) {
+ i--;
+ }
+
+ base[i] = '\0';
+}
+
+
+/* check if a file exists */
+int file_exists(char *file) {
+ struct stat stat_buf;
+ int result;
+
+ printf("checking %s ... ", file);
+
+ result = stat(file, &stat_buf);
+
+ if ( result != 0 ) {
+ /* stat failed, no file */
+ printf("not found.\n");
+ return(0);
+ } else {
+ /* stat succeeded, file exists */
+ printf("exists.\n");
+ return(1);
+ }
+}
+
+
+/* check to see if a shared object exists */
+int shared_object_exists(char *basepath, char *ext, char *file) {
+ char scene_path[256];
+ long int index;
+
+ if ( strcmp(ext, ".sw") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&west_index, scene_path);
+ index = gen_index(&west_index);
+ sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&sw_index, scene_path);
+ index = gen_index(&sw_index);
+ sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&south_index, scene_path);
+ index = gen_index(&south_index);
+ sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".se") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&east_index, scene_path);
+ index = gen_index(&east_index);
+ sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&se_index, scene_path);
+ index = gen_index(&se_index);
+ sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&south_index, scene_path);
+ index = gen_index(&south_index);
+ sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".ne") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&east_index, scene_path);
+ index = gen_index(&east_index);
+ sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&ne_index, scene_path);
+ index = gen_index(&ne_index);
+ sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&north_index, scene_path);
+ index = gen_index(&north_index);
+ sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".nw") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.nw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&west_index, scene_path);
+ index = gen_index(&west_index);
+ sprintf(file, "%s/%s/%ld.1.ne", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&nw_index, scene_path);
+ index = gen_index(&nw_index);
+ sprintf(file, "%s/%s/%ld.1.se", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&north_index, scene_path);
+ index = gen_index(&north_index);
+ sprintf(file, "%s/%s/%ld.1.sw", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".south") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&south_index, scene_path);
+ index = gen_index(&south_index);
+ sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".north") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.north", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&north_index, scene_path);
+ index = gen_index(&north_index);
+ sprintf(file, "%s/%s/%ld.1.south", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".west") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&west_index, scene_path);
+ index = gen_index(&west_index);
+ sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".east") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.east", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ gen_base_path(&east_index, scene_path);
+ index = gen_index(&east_index);
+ sprintf(file, "%s/%s/%ld.1.west", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ if ( strcmp(ext, ".body") == 0 ) {
+ gen_base_path(&my_index, scene_path);
+ index = gen_index(&my_index);
+ sprintf(file, "%s/%s/%ld.1.body", basepath, scene_path, index);
+ if ( file_exists(file) ) {
+ return(1);
+ }
+ }
+
+ return(0);
+}
+
+
+/* given a file pointer, read all the vn (normals from it) */
+void read_normals(FILE *fp) {
+ char line[256];
+
+ while ( fgets(line, 250, fp) != NULL ) {
+ if ( strncmp(line, "vn ", 3) == 0 ) {
+ sscanf(line, "vn %f %f %f\n", &normals[normalcount][0],
+ &normals[normalcount][1], &normals[normalcount][2]);
+ /*
+ printf("read_normals(%d) %.2f %.2f %.2f %s", normalcount,
+ normals[normalcount][0], normals[normalcount][1],
+ normals[normalcount][2], line);
+ */
+ normalcount++;
+ }
+ }
+}
+
+
+/* my custom file opening routine ... don't open if a shared edge or
+ * vertex alread exists */
+FILE *my_open(char *basename, char *basepath, char *ext) {
+ FILE *fp;
+ char filename[256];
+
+ /* check if a shared object already exists */
+ if ( shared_object_exists(basepath, ext, filename) ) {
+ /* not an actual file open error, but we've already got the
+ * shared edge, so we don't want to create another one */
+ fp = fopen(filename, "r");
+ printf("Opening %s\n", filename);
+ return(fp);
+ } else {
+ /* open the file */
+ printf("not opening\n");
+ return(NULL);
+ }
+}
+
+