| 1 | 
greg | 
1.1 | 
/* Copyright (c) 1986 Regents of the University of California */ | 
| 2 | 
  | 
  | 
 | 
| 3 | 
  | 
  | 
#ifndef lint | 
| 4 | 
  | 
  | 
static char SCCSid[] = "$SunId$ LBL"; | 
| 5 | 
  | 
  | 
#endif | 
| 6 | 
  | 
  | 
 | 
| 7 | 
  | 
  | 
/* | 
| 8 | 
  | 
  | 
 *  oconv.c - main program for object to octree conversion. | 
| 9 | 
  | 
  | 
 * | 
| 10 | 
  | 
  | 
 *     7/29/85 | 
| 11 | 
  | 
  | 
 */ | 
| 12 | 
  | 
  | 
 | 
| 13 | 
  | 
  | 
#include  "standard.h" | 
| 14 | 
  | 
  | 
 | 
| 15 | 
  | 
  | 
#include  "octree.h" | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
#include  "object.h" | 
| 18 | 
  | 
  | 
 | 
| 19 | 
  | 
  | 
#include  "otypes.h" | 
| 20 | 
  | 
  | 
 | 
| 21 | 
greg | 
1.2 | 
#define  OMARGIN        (10*FTINY)      /* margin around global cube */ | 
| 22 | 
  | 
  | 
 | 
| 23 | 
greg | 
1.1 | 
#define  MAXOBJFIL      63              /* maximum number of scene files */ | 
| 24 | 
  | 
  | 
 | 
| 25 | 
  | 
  | 
char  *progname;                        /* argv[0] */ | 
| 26 | 
  | 
  | 
 | 
| 27 | 
  | 
  | 
char  *libpath;                         /* library search path */ | 
| 28 | 
  | 
  | 
 | 
| 29 | 
  | 
  | 
int  nowarn = 0;                        /* supress warnings? */ | 
| 30 | 
  | 
  | 
 | 
| 31 | 
  | 
  | 
int  objlim = 5;                        /* # of objects before split */ | 
| 32 | 
  | 
  | 
 | 
| 33 | 
  | 
  | 
int  resolu = 1024;                     /* octree resolution limit */ | 
| 34 | 
  | 
  | 
 | 
| 35 | 
  | 
  | 
CUBE  thescene = {EMPTY, {0.0, 0.0, 0.0}, 0.0};         /* our scene */ | 
| 36 | 
  | 
  | 
 | 
| 37 | 
  | 
  | 
char  *ofname[MAXOBJFIL+1];             /* object file names */ | 
| 38 | 
  | 
  | 
int  nfiles = 0;                        /* number of object files */ | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
double  mincusize;                      /* minimum cube size from resolu */ | 
| 41 | 
  | 
  | 
 | 
| 42 | 
  | 
  | 
 | 
| 43 | 
  | 
  | 
main(argc, argv)                /* convert object files to an octree */ | 
| 44 | 
  | 
  | 
int  argc; | 
| 45 | 
  | 
  | 
char  **argv; | 
| 46 | 
  | 
  | 
{ | 
| 47 | 
  | 
  | 
        char  *getenv(); | 
| 48 | 
  | 
  | 
        double  atof(); | 
| 49 | 
  | 
  | 
        FVECT  bbmin, bbmax; | 
| 50 | 
  | 
  | 
        char  *infile = NULL; | 
| 51 | 
  | 
  | 
        int  outflags = IO_ALL; | 
| 52 | 
  | 
  | 
        OBJECT  startobj; | 
| 53 | 
  | 
  | 
        int  i; | 
| 54 | 
  | 
  | 
 | 
| 55 | 
  | 
  | 
        progname = argv[0]; | 
| 56 | 
  | 
  | 
 | 
| 57 | 
  | 
  | 
        if ((libpath = getenv("RAYPATH")) == NULL) | 
| 58 | 
  | 
  | 
                libpath = ":/usr/local/lib/ray"; | 
| 59 | 
  | 
  | 
 | 
| 60 | 
  | 
  | 
        for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 61 | 
  | 
  | 
                switch (argv[i][1]) { | 
| 62 | 
  | 
  | 
                case 'i':                               /* input octree */ | 
| 63 | 
  | 
  | 
                        infile = argv[++i]; | 
| 64 | 
  | 
  | 
                        break; | 
| 65 | 
  | 
  | 
                case 'b':                               /* bounding cube */ | 
| 66 | 
greg | 
1.2 | 
                        thescene.cuorg[0] = atof(argv[++i]) - OMARGIN; | 
| 67 | 
  | 
  | 
                        thescene.cuorg[1] = atof(argv[++i]) - OMARGIN; | 
| 68 | 
  | 
  | 
                        thescene.cuorg[2] = atof(argv[++i]) - OMARGIN; | 
| 69 | 
  | 
  | 
                        thescene.cusize = atof(argv[++i]) + 2*OMARGIN; | 
| 70 | 
greg | 
1.1 | 
                        break; | 
| 71 | 
  | 
  | 
                case 'n':                               /* set limit */ | 
| 72 | 
  | 
  | 
                        objlim = atoi(argv[++i]); | 
| 73 | 
  | 
  | 
                        break; | 
| 74 | 
  | 
  | 
                case 'r':                               /* resolution limit */ | 
| 75 | 
  | 
  | 
                        resolu = atoi(argv[++i]); | 
| 76 | 
  | 
  | 
                        break; | 
| 77 | 
  | 
  | 
                case 'f':                               /* freeze octree */ | 
| 78 | 
  | 
  | 
                        outflags &= ~IO_FILES; | 
| 79 | 
  | 
  | 
                        break; | 
| 80 | 
  | 
  | 
                case 'w':                               /* supress warnings */ | 
| 81 | 
  | 
  | 
                        nowarn = 1; | 
| 82 | 
  | 
  | 
                        break; | 
| 83 | 
  | 
  | 
                default: | 
| 84 | 
  | 
  | 
                        sprintf(errmsg, "unknown option: '%s'", argv[i]); | 
| 85 | 
  | 
  | 
                        error(USER, errmsg); | 
| 86 | 
  | 
  | 
                        break; | 
| 87 | 
  | 
  | 
                } | 
| 88 | 
  | 
  | 
         | 
| 89 | 
  | 
  | 
        if (infile != NULL) {           /* get old octree & objects */ | 
| 90 | 
  | 
  | 
                if (thescene.cusize > FTINY) | 
| 91 | 
  | 
  | 
                        error(USER, "only one of '-b' or '-i'"); | 
| 92 | 
  | 
  | 
                nfiles = readoct(infile, IO_ALL, &thescene, ofname); | 
| 93 | 
  | 
  | 
                if (nfiles == 0 && outflags & IO_FILES) { | 
| 94 | 
  | 
  | 
                        error(WARNING, "frozen octree"); | 
| 95 | 
  | 
  | 
                        outflags &= ~IO_FILES; | 
| 96 | 
  | 
  | 
                } | 
| 97 | 
  | 
  | 
        } | 
| 98 | 
  | 
  | 
 | 
| 99 | 
  | 
  | 
        printargs(argc, argv, stdout);  /* info. header */ | 
| 100 | 
  | 
  | 
        printf("\n"); | 
| 101 | 
  | 
  | 
 | 
| 102 | 
  | 
  | 
        startobj = nobjects;            /* previous objects already converted */ | 
| 103 | 
  | 
  | 
         | 
| 104 | 
  | 
  | 
        for ( ; i < argc; i++) {                /* read new files */ | 
| 105 | 
  | 
  | 
                if (nfiles >= MAXOBJFIL) | 
| 106 | 
  | 
  | 
                        error(INTERNAL, "too many scene files"); | 
| 107 | 
  | 
  | 
                readobj(ofname[nfiles++] = argv[i]); | 
| 108 | 
  | 
  | 
        } | 
| 109 | 
  | 
  | 
        ofname[nfiles] = NULL; | 
| 110 | 
  | 
  | 
                                                /* find bounding box */ | 
| 111 | 
  | 
  | 
        bbmin[0] = bbmin[1] = bbmin[2] = FHUGE; | 
| 112 | 
  | 
  | 
        bbmax[0] = bbmax[1] = bbmax[2] = -FHUGE; | 
| 113 | 
  | 
  | 
        for (i = startobj; i < nobjects; i++) | 
| 114 | 
  | 
  | 
                add2bbox(objptr(i), bbmin, bbmax); | 
| 115 | 
  | 
  | 
                                                /* set/check cube */ | 
| 116 | 
  | 
  | 
        if (thescene.cusize == 0.0) { | 
| 117 | 
  | 
  | 
                if (bbmin[0] <= bbmax[0]) { | 
| 118 | 
  | 
  | 
                        for (i = 0; i < 3; i++) { | 
| 119 | 
greg | 
1.2 | 
                                bbmin[i] -= OMARGIN; | 
| 120 | 
  | 
  | 
                                bbmax[i] += OMARGIN; | 
| 121 | 
greg | 
1.1 | 
                        } | 
| 122 | 
  | 
  | 
                        for (i = 0; i < 3; i++) | 
| 123 | 
  | 
  | 
                                if (bbmax[i] - bbmin[i] > thescene.cusize) | 
| 124 | 
  | 
  | 
                                        thescene.cusize = bbmax[i] - bbmin[i]; | 
| 125 | 
greg | 
1.3 | 
                        for (i = 0; i < 3; i++) | 
| 126 | 
  | 
  | 
                                thescene.cuorg[i] = | 
| 127 | 
  | 
  | 
                                        (bbmax[i]+bbmin[i]-thescene.cusize)*.5; | 
| 128 | 
greg | 
1.1 | 
                } | 
| 129 | 
  | 
  | 
        } else { | 
| 130 | 
  | 
  | 
                for (i = 0; i < 3; i++) | 
| 131 | 
  | 
  | 
                        if (bbmin[i] < thescene.cuorg[i] || | 
| 132 | 
  | 
  | 
                                bbmax[i] > thescene.cuorg[i] + thescene.cusize) | 
| 133 | 
  | 
  | 
                                error(USER, "boundary does not encompass scene"); | 
| 134 | 
  | 
  | 
        } | 
| 135 | 
  | 
  | 
 | 
| 136 | 
  | 
  | 
        mincusize = thescene.cusize / resolu - FTINY; | 
| 137 | 
  | 
  | 
                 | 
| 138 | 
  | 
  | 
        for (i = startobj; i < nobjects; i++)           /* add new objects */ | 
| 139 | 
  | 
  | 
                addobject(&thescene, i); | 
| 140 | 
  | 
  | 
         | 
| 141 | 
  | 
  | 
        thescene.cutree = combine(thescene.cutree);     /* optimize */ | 
| 142 | 
  | 
  | 
 | 
| 143 | 
  | 
  | 
        writeoct(outflags, &thescene, ofname);  /* write structures to stdout */ | 
| 144 | 
  | 
  | 
 | 
| 145 | 
  | 
  | 
        quit(0); | 
| 146 | 
  | 
  | 
} | 
| 147 | 
  | 
  | 
 | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
quit(code)                              /* exit program */ | 
| 150 | 
  | 
  | 
int  code; | 
| 151 | 
  | 
  | 
{ | 
| 152 | 
  | 
  | 
        exit(code); | 
| 153 | 
  | 
  | 
} | 
| 154 | 
  | 
  | 
 | 
| 155 | 
  | 
  | 
 | 
| 156 | 
  | 
  | 
cputs()                                 /* interactive error */ | 
| 157 | 
  | 
  | 
{ | 
| 158 | 
  | 
  | 
        /* referenced, but not used */ | 
| 159 | 
  | 
  | 
} | 
| 160 | 
  | 
  | 
 | 
| 161 | 
  | 
  | 
 | 
| 162 | 
  | 
  | 
wputs(s)                                /* warning message */ | 
| 163 | 
  | 
  | 
char  *s; | 
| 164 | 
  | 
  | 
{ | 
| 165 | 
  | 
  | 
        if (!nowarn) | 
| 166 | 
  | 
  | 
                eputs(s); | 
| 167 | 
  | 
  | 
} | 
| 168 | 
  | 
  | 
 | 
| 169 | 
  | 
  | 
 | 
| 170 | 
  | 
  | 
eputs(s)                                /* put string to stderr */ | 
| 171 | 
  | 
  | 
register char  *s; | 
| 172 | 
  | 
  | 
{ | 
| 173 | 
  | 
  | 
        static int  inline = 0; | 
| 174 | 
  | 
  | 
 | 
| 175 | 
  | 
  | 
        if (!inline++) { | 
| 176 | 
  | 
  | 
                fputs(progname, stderr); | 
| 177 | 
  | 
  | 
                fputs(": ", stderr); | 
| 178 | 
  | 
  | 
        } | 
| 179 | 
  | 
  | 
        fputs(s, stderr); | 
| 180 | 
  | 
  | 
        if (*s && s[strlen(s)-1] == '\n') | 
| 181 | 
  | 
  | 
                inline = 0; | 
| 182 | 
  | 
  | 
} | 
| 183 | 
  | 
  | 
 | 
| 184 | 
  | 
  | 
 | 
| 185 | 
  | 
  | 
addobject(cu, obj)                      /* add an object to a cube */ | 
| 186 | 
  | 
  | 
register CUBE  *cu; | 
| 187 | 
  | 
  | 
OBJECT  obj; | 
| 188 | 
  | 
  | 
{ | 
| 189 | 
  | 
  | 
        CUBE  cukid; | 
| 190 | 
  | 
  | 
        OCTREE  ot; | 
| 191 | 
  | 
  | 
        OBJECT  oset[MAXSET+1]; | 
| 192 | 
  | 
  | 
        int  in; | 
| 193 | 
  | 
  | 
        register int  i, j; | 
| 194 | 
  | 
  | 
 | 
| 195 | 
  | 
  | 
        in = (*ofun[objptr(obj)->otype].funp)(objptr(obj), cu); | 
| 196 | 
  | 
  | 
 | 
| 197 | 
  | 
  | 
        if (!in) | 
| 198 | 
  | 
  | 
                return;                         /* no intersection */ | 
| 199 | 
  | 
  | 
         | 
| 200 | 
  | 
  | 
        if (istree(cu->cutree)) { | 
| 201 | 
  | 
  | 
                                                /* do children */ | 
| 202 | 
  | 
  | 
                cukid.cusize = cu->cusize * 0.5; | 
| 203 | 
  | 
  | 
                for (i = 0; i < 8; i++) { | 
| 204 | 
  | 
  | 
                        cukid.cutree = octkid(cu->cutree, i); | 
| 205 | 
  | 
  | 
                        for (j = 0; j < 3; j++) { | 
| 206 | 
  | 
  | 
                                cukid.cuorg[j] = cu->cuorg[j]; | 
| 207 | 
  | 
  | 
                                if ((1<<j) & i) | 
| 208 | 
  | 
  | 
                                        cukid.cuorg[j] += cukid.cusize; | 
| 209 | 
  | 
  | 
                        } | 
| 210 | 
  | 
  | 
                        addobject(&cukid, obj); | 
| 211 | 
  | 
  | 
                        octkid(cu->cutree, i) = cukid.cutree; | 
| 212 | 
  | 
  | 
                } | 
| 213 | 
  | 
  | 
                 | 
| 214 | 
  | 
  | 
        } else if (isempty(cu->cutree)) { | 
| 215 | 
  | 
  | 
                                                /* singular set */ | 
| 216 | 
  | 
  | 
                oset[0] = 1; oset[1] = obj; | 
| 217 | 
  | 
  | 
                cu->cutree = fullnode(oset); | 
| 218 | 
  | 
  | 
                 | 
| 219 | 
  | 
  | 
        } else { | 
| 220 | 
  | 
  | 
                                                /* add to full node */ | 
| 221 | 
  | 
  | 
                objset(oset, cu->cutree); | 
| 222 | 
  | 
  | 
                cukid.cusize = cu->cusize * 0.5; | 
| 223 | 
  | 
  | 
                 | 
| 224 | 
  | 
  | 
                if (in == 2 || oset[0] < objlim || cukid.cusize < mincusize) { | 
| 225 | 
  | 
  | 
                                                        /* add to set */ | 
| 226 | 
  | 
  | 
                        if (oset[0] >= MAXSET) { | 
| 227 | 
  | 
  | 
                                sprintf(errmsg, | 
| 228 | 
  | 
  | 
                                        "set overflow in addobject (%s)", | 
| 229 | 
  | 
  | 
                                                objptr(obj)->oname); | 
| 230 | 
  | 
  | 
                                error(INTERNAL, errmsg); | 
| 231 | 
  | 
  | 
                        } | 
| 232 | 
  | 
  | 
                        insertelem(oset, obj); | 
| 233 | 
  | 
  | 
                        cu->cutree = fullnode(oset); | 
| 234 | 
  | 
  | 
 | 
| 235 | 
  | 
  | 
                } else { | 
| 236 | 
  | 
  | 
                                                        /* subdivide cube */ | 
| 237 | 
  | 
  | 
                        if ((ot = octalloc()) == EMPTY) | 
| 238 | 
  | 
  | 
                                error(SYSTEM, "out of octree space"); | 
| 239 | 
  | 
  | 
                        for (i = 0; i < 8; i++) { | 
| 240 | 
  | 
  | 
                                cukid.cutree = EMPTY; | 
| 241 | 
  | 
  | 
                                for (j = 0; j < 3; j++) { | 
| 242 | 
  | 
  | 
                                        cukid.cuorg[j] = cu->cuorg[j]; | 
| 243 | 
  | 
  | 
                                        if ((1<<j) & i) | 
| 244 | 
  | 
  | 
                                                cukid.cuorg[j] += cukid.cusize; | 
| 245 | 
  | 
  | 
                                } | 
| 246 | 
  | 
  | 
                                for (j = 1; j <= oset[0]; j++) | 
| 247 | 
  | 
  | 
                                        addobject(&cukid, oset[j]); | 
| 248 | 
  | 
  | 
                                addobject(&cukid, obj); | 
| 249 | 
  | 
  | 
                                octkid(ot, i) = cukid.cutree; | 
| 250 | 
  | 
  | 
                        } | 
| 251 | 
  | 
  | 
                        cu->cutree = ot; | 
| 252 | 
  | 
  | 
                } | 
| 253 | 
  | 
  | 
        } | 
| 254 | 
  | 
  | 
} |