| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id: robjutil.c,v 2.2 2020/04/02 20:44:15 greg Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | * Utility program for fixing up Wavefront .OBJ files. | 
| 6 | * | 
| 7 | */ | 
| 8 |  | 
| 9 | #include <stdio.h> | 
| 10 | #include <stdlib.h> | 
| 11 | #include <string.h> | 
| 12 | #include "rtio.h" | 
| 13 | #include "objutil.h" | 
| 14 |  | 
| 15 | const char      *progname; | 
| 16 | int             verbose = 0; | 
| 17 |  | 
| 18 | int | 
| 19 | main(int argc, char *argv[]) | 
| 20 | { | 
| 21 | Scene           *myScene; | 
| 22 | int             radout = 0; | 
| 23 | const char      *grp[64]; | 
| 24 | int             ngrps = 0; | 
| 25 | int             save_grps = 1; | 
| 26 | const char      *mat[64]; | 
| 27 | int             nmats = 0; | 
| 28 | int             save_mats = 1; | 
| 29 | int             do_tex = 0; | 
| 30 | int             do_norm = 0; | 
| 31 | char            *xfm = NULL; | 
| 32 | char            cbuf[256]; | 
| 33 | double          verteps = -1.; | 
| 34 | int     i, n; | 
| 35 |  | 
| 36 | progname = argv[0]; | 
| 37 | /* process options */ | 
| 38 | for (i = 1; i < argc && (argv[i][0] == '-' || argv[i][0] == '+'); i++) | 
| 39 | switch (argv[i][1]) { | 
| 40 | case 'v':                       /* verbose mode */ | 
| 41 | verbose = (argv[i][0] == '+'); | 
| 42 | break; | 
| 43 | case 'g':                       /* save/delete group */ | 
| 44 | if (save_grps != (argv[i][0] == '+')) { | 
| 45 | if (ngrps) { | 
| 46 | fprintf(stderr, | 
| 47 | "%s: -g and +g are mutually exclusive\n", | 
| 48 | argv[0]); | 
| 49 | exit(1); | 
| 50 | } | 
| 51 | save_grps = !save_grps; | 
| 52 | } | 
| 53 | if (ngrps >= 64) { | 
| 54 | fprintf(stderr, "%s: too many groups\n", | 
| 55 | argv[0]); | 
| 56 | exit(1); | 
| 57 | } | 
| 58 | grp[ngrps++] = argv[++i]; | 
| 59 | break; | 
| 60 | case 'm':                       /* save/delete material */ | 
| 61 | if (save_mats != (argv[i][0] == '+')) { | 
| 62 | if (nmats) { | 
| 63 | fprintf(stderr, | 
| 64 | "%s: -m and +m are mutually exclusive\n", | 
| 65 | argv[0]); | 
| 66 | exit(1); | 
| 67 | } | 
| 68 | save_mats = !save_mats; | 
| 69 | } | 
| 70 | if (nmats >= 64) { | 
| 71 | fprintf(stderr, "%s: too many materials\n", | 
| 72 | argv[0]); | 
| 73 | exit(1); | 
| 74 | } | 
| 75 | mat[nmats++] = argv[++i]; | 
| 76 | break; | 
| 77 | case 't':                       /* do texture coord's */ | 
| 78 | do_tex = (argv[i][0] == '+') ? 1 : -1; | 
| 79 | break; | 
| 80 | case 'n':                       /* do normals */ | 
| 81 | do_norm = (argv[i][0] == '+') ? 1 : -1; | 
| 82 | break; | 
| 83 | case 'c':                       /* coalesce vertices */ | 
| 84 | verteps = atof(argv[++i]); | 
| 85 | break; | 
| 86 | case 'r':                       /* output to Radiance file? */ | 
| 87 | radout = (argv[i][0] == '+'); | 
| 88 | break; | 
| 89 | case 'x':                       /* apply a transform */ | 
| 90 | if (xfm != NULL) { | 
| 91 | fprintf(stderr, "%s: only one '-x' option allowed\n", | 
| 92 | argv[0]); | 
| 93 | exit(1); | 
| 94 | } | 
| 95 | xfm = argv[++i]; | 
| 96 | break; | 
| 97 | default: | 
| 98 | fprintf(stderr, "%s: unknown option: %s\n", | 
| 99 | argv[0], argv[i]); | 
| 100 | goto userr; | 
| 101 | } | 
| 102 | if (i == argc) { | 
| 103 | if (verbose) | 
| 104 | fputs("Loading scene from standard input...\n", | 
| 105 | stderr); | 
| 106 | myScene = loadOBJ(NULL, NULL); | 
| 107 | } else { | 
| 108 | myScene = newScene(); | 
| 109 | for ( ; i < argc; i++) { | 
| 110 | if (verbose) | 
| 111 | fprintf(stderr, | 
| 112 | "Loading scene from \"%s\"...\n", | 
| 113 | argv[i]); | 
| 114 | if (loadOBJ(myScene, argv[i]) == NULL) | 
| 115 | exit(1); | 
| 116 | } | 
| 117 | } | 
| 118 | if (ngrps) { | 
| 119 | if (verbose) | 
| 120 | fputs("Deleting unwanted groups...\n", stderr); | 
| 121 | clearSelection(myScene, 0); | 
| 122 | sprintf(cbuf, "%s the following groups:", | 
| 123 | save_grps ? "Extracting" : "Deleting"); | 
| 124 | addComment(myScene, cbuf); | 
| 125 | for (i = 0; i < ngrps; i++) { | 
| 126 | sprintf(cbuf, "\t%s", grp[i]); | 
| 127 | addComment(myScene, cbuf); | 
| 128 | selectGroup(myScene, grp[i], 0); | 
| 129 | } | 
| 130 | if (save_grps) | 
| 131 | n = deleteFaces(myScene, 0, FACE_SELECTED); | 
| 132 | else | 
| 133 | n = deleteFaces(myScene, FACE_SELECTED, 0); | 
| 134 | sprintf(cbuf, "\t(%d faces removed)", n); | 
| 135 | addComment(myScene, cbuf); | 
| 136 | } | 
| 137 | if (nmats) { | 
| 138 | if (verbose) | 
| 139 | fputs("Deleting unwanted materials...\n", stderr); | 
| 140 | clearSelection(myScene, 0); | 
| 141 | sprintf(cbuf, "%s the following materials:", | 
| 142 | save_mats ? "Extracting" : "Deleting"); | 
| 143 | addComment(myScene, cbuf); | 
| 144 | for (i = 0; i < nmats; i++) { | 
| 145 | sprintf(cbuf, "\t%s", mat[i]); | 
| 146 | addComment(myScene, cbuf); | 
| 147 | selectMaterial(myScene, mat[i], 0); | 
| 148 | } | 
| 149 | if (save_mats) | 
| 150 | n = deleteFaces(myScene, 0, FACE_SELECTED); | 
| 151 | else | 
| 152 | n = deleteFaces(myScene, FACE_SELECTED, 0); | 
| 153 | sprintf(cbuf, "\t(%d faces removed)", n); | 
| 154 | addComment(myScene, cbuf); | 
| 155 | } | 
| 156 | if (verbose && do_tex < 0) | 
| 157 | fputs("Removing texture coordinates...\n", stderr); | 
| 158 | if (do_tex < 0 && (n = removeTexture(myScene, 0, 0))) { | 
| 159 | sprintf(cbuf, "Removed texture coordinates from %d faces", n); | 
| 160 | addComment(myScene, cbuf); | 
| 161 | } | 
| 162 | if (verbose && do_norm < 0) | 
| 163 | fputs("Removing surface normals...\n", stderr); | 
| 164 | if (do_norm < 0 && (n = removeNormals(myScene, 0, 0))) { | 
| 165 | sprintf(cbuf, "Removed surface normals from %d faces", n); | 
| 166 | addComment(myScene, cbuf); | 
| 167 | } | 
| 168 | if (verbose && verteps >= 0) | 
| 169 | fputs("Coalescing vertices...\n", stderr); | 
| 170 | if (verteps >= 0 && (n = coalesceVertices(myScene, verteps))) { | 
| 171 | sprintf(cbuf, "Coalesced %d vertices with epsilon of %g", | 
| 172 | n, verteps); | 
| 173 | addComment(myScene, cbuf); | 
| 174 | } | 
| 175 | if (verbose) | 
| 176 | fputs("Deleting degenerate faces...\n", stderr); | 
| 177 | n = deleteFaces(myScene, FACE_DEGENERATE, 0); | 
| 178 | if (n) { | 
| 179 | sprintf(cbuf, "Removed %d degenerate faces", n); | 
| 180 | addComment(myScene, cbuf); | 
| 181 | } | 
| 182 | if (verbose) | 
| 183 | fputs("Checking for duplicate faces...\n", stderr); | 
| 184 | if (findDuplicateFaces(myScene)) | 
| 185 | n = deleteFaces(myScene, FACE_DUPLICATE, 0); | 
| 186 | if (n) { | 
| 187 | sprintf(cbuf, "Removed %d duplicate faces", n); | 
| 188 | addComment(myScene, cbuf); | 
| 189 | } | 
| 190 | if (xfm != NULL) { | 
| 191 | if (verbose) | 
| 192 | fputs("Applying transform...\n", stderr); | 
| 193 | if (!xfmScene(myScene, xfm)) { | 
| 194 | fprintf(stderr, "%s: transform error\n", argv[0]); | 
| 195 | exit(1); | 
| 196 | } | 
| 197 | } | 
| 198 | if (verbose) | 
| 199 | fputs("Writing out scene...\n", stderr); | 
| 200 |  | 
| 201 | fputs("# File processed by: ", stdout); | 
| 202 | printargs(argc, argv, stdout); | 
| 203 | if (radout) | 
| 204 | toRadiance(myScene, stdout, 0, 0); | 
| 205 | else | 
| 206 | toOBJ(myScene, stdout); | 
| 207 | /* freeScene(myScene); */ | 
| 208 | if (verbose) | 
| 209 | fputs("Done.                                    \n", stderr); | 
| 210 | return(0); | 
| 211 | userr: | 
| 212 | fprintf(stderr, "Usage: %s [options] [input.obj ..]\n", argv[0]); | 
| 213 | fprintf(stderr, "Available options:\n"); | 
| 214 | fprintf(stderr, "\t+/-r\t\t\t# Radiance scene output on/off\n"); | 
| 215 | fprintf(stderr, "\t+/-v\t\t\t# on/off verbosity (progress reports)\n"); | 
| 216 | fprintf(stderr, "\t+/-g name\t\t# save/delete group\n"); | 
| 217 | fprintf(stderr, "\t+/-m name\t\t# save/delete faces with material\n"); | 
| 218 | fprintf(stderr, "\t+/-t\t\t\t# keep/remove texture coordinates\n"); | 
| 219 | fprintf(stderr, "\t+/-n\t\t\t# keep/remove vertex normals\n"); | 
| 220 | fprintf(stderr, "\t-c epsilon\t\t# coalesce vertices within epsilon\n"); | 
| 221 | fprintf(stderr, "\t-x 'xf spec'\t# apply the quoted transform\n"); | 
| 222 | return(1); | 
| 223 | } | 
| 224 |  | 
| 225 | void | 
| 226 | eputs(char *s)                          /* put string to stderr */ | 
| 227 | { | 
| 228 | static int  midline = 0; | 
| 229 |  | 
| 230 | if (!*s) | 
| 231 | return; | 
| 232 | if (!midline++) { | 
| 233 | fputs(progname, stderr); | 
| 234 | fputs(": ", stderr); | 
| 235 | } | 
| 236 | fputs(s, stderr); | 
| 237 | if (s[strlen(s)-1] == '\n') | 
| 238 | midline = 0; | 
| 239 | } |