[37] | 1 | /* |
---|
| 2 | |
---|
| 3 | Brian Curless |
---|
| 4 | |
---|
| 5 | Computer Graphics Laboratory |
---|
| 6 | Stanford University |
---|
| 7 | |
---|
| 8 | --------------------------------------------------------------------- |
---|
| 9 | |
---|
| 10 | Copyright (1997) The Board of Trustees of the Leland Stanford Junior |
---|
| 11 | University. Except for commercial resale, lease, license or other |
---|
| 12 | commercial transactions, permission is hereby given to use, copy, |
---|
| 13 | modify this software for academic purposes only. No part of this |
---|
| 14 | software or any derivatives thereof may be used in the production of |
---|
| 15 | computer models for resale or for use in a commercial |
---|
| 16 | product. STANFORD MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND |
---|
| 17 | CONCERNING THIS SOFTWARE. No support is implied or provided. |
---|
| 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | #include <limits.h> |
---|
| 23 | #include <stdlib.h> |
---|
| 24 | #include <math.h> |
---|
| 25 | #include <unistd.h> |
---|
| 26 | #include <sys/wait.h> |
---|
| 27 | |
---|
| 28 | #include "vrip.h" |
---|
| 29 | #include "vripGridCmds.h" |
---|
| 30 | #include "vripGlobals.h" |
---|
| 31 | #include "vripAux.h" |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | const int CONSERVATIVE_AVG_DOWN = 0; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | int |
---|
| 38 | Vrip_InfoRLECmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 39 | { |
---|
| 40 | char str[PATH_MAX]; |
---|
| 41 | |
---|
| 42 | sprintf(str, "xdim %d ydim %d zdim %d resolution %f axis %d", |
---|
| 43 | frontRLEGrid->xdim, frontRLEGrid->ydim, frontRLEGrid->zdim, |
---|
| 44 | frontRLEGrid->resolution, frontRLEGrid->axis); |
---|
| 45 | |
---|
| 46 | Tcl_SetResult(interp, str, TCL_VOLATILE); |
---|
| 47 | |
---|
| 48 | return TCL_OK; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | int |
---|
| 54 | Vrip_InfoNormRLECmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 55 | { |
---|
| 56 | char str[PATH_MAX]; |
---|
| 57 | |
---|
| 58 | sprintf(str, "xdim %d ydim %d zdim %d resolution %f axis %d", |
---|
| 59 | frontRLEGridNorm->xdim, frontRLEGridNorm->ydim, frontRLEGridNorm->zdim, |
---|
| 60 | frontRLEGridNorm->resolution, frontRLEGridNorm->axis); |
---|
| 61 | |
---|
| 62 | Tcl_SetResult(interp, str, TCL_VOLATILE); |
---|
| 63 | |
---|
| 64 | return TCL_OK; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | int |
---|
| 69 | Vrip_NewGridCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 70 | { |
---|
| 71 | int dim; |
---|
| 72 | float res; |
---|
| 73 | vec3f origin; |
---|
| 74 | |
---|
| 75 | if (argc != 3 && argc != 6) { |
---|
| 76 | interp->result = "Usage: vrip_newgrid <dim> <res> [<ox> <oy> <oz>]"; |
---|
| 77 | return TCL_ERROR; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | dim = atoi(argv[1]); |
---|
| 81 | res = atof(argv[2]); |
---|
| 82 | |
---|
| 83 | if (argc == 6) { |
---|
| 84 | origin[0] = atof(argv[3])-dim*res/2; |
---|
| 85 | origin[1] = atof(argv[4])-dim*res/2; |
---|
| 86 | origin[2] = atof(argv[5])-dim*res/2; |
---|
| 87 | } else { |
---|
| 88 | origin[0] = -dim*res/2; |
---|
| 89 | origin[1] = 0.15 - dim*res/2; |
---|
| 90 | origin[2] = -dim*res/2; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | if (theGrid != NULL) { |
---|
| 94 | delete theGrid; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | theGrid = new OccGrid(dim,dim,dim); |
---|
| 98 | theGrid->resolution = res; |
---|
| 99 | theGrid->origin[0] = origin[0]; |
---|
| 100 | theGrid->origin[1] = origin[1]; |
---|
| 101 | theGrid->origin[2] = origin[2]; |
---|
| 102 | |
---|
| 103 | theGrid->clear(); |
---|
| 104 | |
---|
| 105 | return TCL_OK; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | int |
---|
| 110 | Vrip_NewGridRLECmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 111 | { |
---|
| 112 | int xdim, ydim, zdim, maxDim; |
---|
| 113 | float res; |
---|
| 114 | vec3f origin; |
---|
| 115 | |
---|
| 116 | if (argc != 3 && argc != 5 && argc != 6 && argc != 8) { |
---|
| 117 | interp->result = "Usage: vrip_newgridrle <dim> [<dim> <dim>] <res> [<ox> <oy> <oz>]"; |
---|
| 118 | return TCL_ERROR; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | if (argc == 3 || argc == 6) { |
---|
| 122 | xdim = ydim = zdim = atoi(argv[1]); |
---|
| 123 | res = atof(argv[2]); |
---|
| 124 | } else { |
---|
| 125 | xdim = atoi(argv[1]); |
---|
| 126 | ydim = atoi(argv[2]); |
---|
| 127 | zdim = atoi(argv[3]); |
---|
| 128 | res = atof(argv[4]); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | if (argc == 3 || argc == 5) { |
---|
| 132 | origin[0] = -xdim*res/2; |
---|
| 133 | origin[1] = 0.15 - ydim*res/2; |
---|
| 134 | origin[2] = -zdim*res/2; |
---|
| 135 | } else if (argc == 6) { |
---|
| 136 | origin[0] = atof(argv[3])-xdim*res/2; |
---|
| 137 | origin[1] = atof(argv[4])-ydim*res/2; |
---|
| 138 | origin[2] = atof(argv[5])-zdim*res/2; |
---|
| 139 | } else if (argc == 8) { |
---|
| 140 | origin[0] = atof(argv[5])-xdim*res/2; |
---|
| 141 | origin[1] = atof(argv[6])-ydim*res/2; |
---|
| 142 | origin[2] = atof(argv[7])-zdim*res/2; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if (backRLEGrid != NULL) { |
---|
| 146 | delete backRLEGrid; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if (frontRLEGrid != NULL) { |
---|
| 150 | delete frontRLEGrid; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if (theDepthMap != NULL) { |
---|
| 154 | delete theDepthMap; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | backRLEGrid = new OccGridRLE(xdim, ydim, zdim, CHUNK_SIZE); |
---|
| 159 | backRLEGrid->resolution = res; |
---|
| 160 | backRLEGrid->origin[0] = origin[0]; |
---|
| 161 | backRLEGrid->origin[1] = origin[1]; |
---|
| 162 | backRLEGrid->origin[2] = origin[2]; |
---|
| 163 | |
---|
| 164 | backRLEGrid->clear(); |
---|
| 165 | |
---|
| 166 | frontRLEGrid = new OccGridRLE(xdim, ydim, zdim, CHUNK_SIZE); |
---|
| 167 | frontRLEGrid->resolution = res; |
---|
| 168 | frontRLEGrid->origin[0] = origin[0]; |
---|
| 169 | frontRLEGrid->origin[1] = origin[1]; |
---|
| 170 | frontRLEGrid->origin[2] = origin[2]; |
---|
| 171 | |
---|
| 172 | frontRLEGrid->clear(); |
---|
| 173 | |
---|
| 174 | |
---|
| 175 | if (UseTails) { |
---|
| 176 | Tcl_Eval(interp, "vrip_fillrle 1 0"); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // Could this be tighter? |
---|
| 180 | maxDim = MAX(xdim, MAX(ydim, zdim)); |
---|
| 181 | theDepthMap = new DepthMap(int(maxDim*2.2+20), int(maxDim*2.2+20), |
---|
| 182 | FALSE, DEPTH_TREE_GRANULARITY); |
---|
| 183 | |
---|
| 184 | return TCL_OK; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | int |
---|
| 189 | Vrip_NewGridNormRLECmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 190 | { |
---|
| 191 | int xdim, ydim, zdim, maxDim; |
---|
| 192 | float res; |
---|
| 193 | vec3f origin; |
---|
| 194 | |
---|
| 195 | if (argc != 3 && argc != 5 && argc != 6 && argc != 8) { |
---|
| 196 | interp->result = "Usage: vrip_newgridnormrle <dim> [<dim> <dim>] <res> [<ox> <oy> <oz>]"; |
---|
| 197 | return TCL_ERROR; |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | if (argc == 3 || argc == 6) { |
---|
| 201 | xdim = ydim = zdim = atoi(argv[1]); |
---|
| 202 | res = atof(argv[2]); |
---|
| 203 | } else { |
---|
| 204 | xdim = atoi(argv[1]); |
---|
| 205 | ydim = atoi(argv[2]); |
---|
| 206 | zdim = atoi(argv[3]); |
---|
| 207 | res = atof(argv[4]); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | if (argc == 3 || argc == 5) { |
---|
| 211 | origin[0] = -xdim*res/2; |
---|
| 212 | origin[1] = 0.15 - ydim*res/2; |
---|
| 213 | origin[2] = -zdim*res/2; |
---|
| 214 | } else if (argc == 6) { |
---|
| 215 | origin[0] = atof(argv[3])-xdim*res/2; |
---|
| 216 | origin[1] = atof(argv[4])-ydim*res/2; |
---|
| 217 | origin[2] = atof(argv[5])-zdim*res/2; |
---|
| 218 | } else if (argc == 8) { |
---|
| 219 | origin[0] = atof(argv[5])-xdim*res/2; |
---|
| 220 | origin[1] = atof(argv[6])-ydim*res/2; |
---|
| 221 | origin[2] = atof(argv[7])-zdim*res/2; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | if (backRLEGridNorm != NULL) { |
---|
| 225 | delete backRLEGridNorm; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | if (frontRLEGridNorm != NULL) { |
---|
| 229 | delete frontRLEGridNorm; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | if (theDepthMap != NULL) { |
---|
| 233 | delete theDepthMap; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | backRLEGridNorm = new OccGridNormRLE(xdim, ydim, zdim, CHUNK_SIZE); |
---|
| 238 | backRLEGridNorm->resolution = res; |
---|
| 239 | backRLEGridNorm->origin[0] = origin[0]; |
---|
| 240 | backRLEGridNorm->origin[1] = origin[1]; |
---|
| 241 | backRLEGridNorm->origin[2] = origin[2]; |
---|
| 242 | |
---|
| 243 | backRLEGridNorm->clear(); |
---|
| 244 | |
---|
| 245 | frontRLEGridNorm = new OccGridNormRLE(xdim, ydim, zdim, CHUNK_SIZE); |
---|
| 246 | frontRLEGridNorm->resolution = res; |
---|
| 247 | frontRLEGridNorm->origin[0] = origin[0]; |
---|
| 248 | frontRLEGridNorm->origin[1] = origin[1]; |
---|
| 249 | frontRLEGridNorm->origin[2] = origin[2]; |
---|
| 250 | |
---|
| 251 | frontRLEGridNorm->clear(); |
---|
| 252 | |
---|
| 253 | |
---|
| 254 | if (UseTails) { |
---|
| 255 | Tcl_Eval(interp, "vrip_fillrle 1 0"); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // Could this be tighter? |
---|
| 259 | maxDim = MAX(xdim, MAX(ydim, zdim)); |
---|
| 260 | theDepthMap = new DepthMap(int(maxDim*2.2+20), int(maxDim*2.2+20), |
---|
| 261 | TRUE, DEPTH_TREE_GRANULARITY); |
---|
| 262 | |
---|
| 263 | return TCL_OK; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | |
---|
| 267 | int |
---|
| 268 | Vrip_AvgDownCmd(ClientData, Tcl_Interp *interp, int argc, const char *[]) |
---|
| 269 | { |
---|
| 270 | int xx, yy, zz, newXDim, newYDim, newZDim ; |
---|
| 271 | int xok, yok, zok, xinc, zinc, yinc; |
---|
| 272 | double value, totalWeight; |
---|
| 273 | OccElement *newBuf, *theBuf; |
---|
| 274 | OccGrid *newGrid; |
---|
| 275 | |
---|
| 276 | if (argc != 1) { |
---|
| 277 | interp->result = "Usage: vrip_avgdown"; |
---|
| 278 | return TCL_ERROR; |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | if (theGrid == NULL) { |
---|
| 282 | interp->result = "Grid not allocated yet"; |
---|
| 283 | return TCL_ERROR; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | newXDim = int(ceil(theGrid->xdim/2.0)); |
---|
| 287 | newYDim = int(ceil(theGrid->ydim/2.0)); |
---|
| 288 | newZDim = int(ceil(theGrid->zdim/2.0)); |
---|
| 289 | newGrid = new OccGrid(newXDim, newYDim, newZDim); |
---|
| 290 | |
---|
| 291 | xinc = 1; |
---|
| 292 | yinc = theGrid->xdim; |
---|
| 293 | zinc = theGrid->xdim*theGrid->ydim; |
---|
| 294 | newBuf = newGrid->elems; |
---|
| 295 | for (zz = 0; zz < newGrid->zdim; zz++) { |
---|
| 296 | zok = (2*zz) < (theGrid->zdim - 1); |
---|
| 297 | for (yy = 0; yy < newGrid->ydim; yy++) { |
---|
| 298 | yok = (2*yy) < (theGrid->ydim - 1); |
---|
| 299 | theBuf = theGrid->address(0, 2*yy, 2*zz); |
---|
| 300 | for (xx = 0; xx < newGrid->xdim; xx++, newBuf++, theBuf+=2) { |
---|
| 301 | xok = (2*xx) < (theGrid->xdim - 1); |
---|
| 302 | value = theBuf->value; |
---|
| 303 | totalWeight = theBuf->totalWeight; |
---|
| 304 | if (xok) { |
---|
| 305 | value += (theBuf+xinc)->value; |
---|
| 306 | totalWeight += (theBuf+xinc)->totalWeight ; |
---|
| 307 | } |
---|
| 308 | if (yok) { |
---|
| 309 | value += (theBuf+yinc)->value; |
---|
| 310 | totalWeight += (theBuf+yinc)->totalWeight ; |
---|
| 311 | } |
---|
| 312 | if (xok&&yok) { |
---|
| 313 | value += (theBuf+xinc+yinc)->value; |
---|
| 314 | totalWeight += (theBuf+xinc+yinc)->totalWeight ; |
---|
| 315 | } |
---|
| 316 | if (zok) { |
---|
| 317 | value += (theBuf+zinc)->value; |
---|
| 318 | totalWeight += (theBuf+zinc)->totalWeight ; |
---|
| 319 | } |
---|
| 320 | if (xok&&zok) { |
---|
| 321 | value += (theBuf+xinc+zinc)->value; |
---|
| 322 | totalWeight += (theBuf+xinc+zinc)->totalWeight ; |
---|
| 323 | } |
---|
| 324 | if (yok&&zok) { |
---|
| 325 | value += (theBuf+yinc+zinc)->value; |
---|
| 326 | totalWeight += (theBuf+yinc+zinc)->totalWeight ; |
---|
| 327 | } |
---|
| 328 | if (xok&&yok&&zok) { |
---|
| 329 | value += (theBuf+xinc+yinc+zinc)->value; |
---|
| 330 | totalWeight += (theBuf+xinc+yinc+zinc)->totalWeight ; |
---|
| 331 | } |
---|
| 332 | newBuf->value = ushort(value/8); |
---|
| 333 | newBuf->totalWeight = ushort(totalWeight/8); |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | // Coordinate systems??? |
---|
| 340 | |
---|
| 341 | delete theGrid; |
---|
| 342 | |
---|
| 343 | theGrid = newGrid; |
---|
| 344 | |
---|
| 345 | return TCL_OK; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | |
---|
| 349 | int |
---|
| 350 | Vrip_AvgDownRLECmd(ClientData, Tcl_Interp *interp, int argc, const char *[]) |
---|
| 351 | { |
---|
| 352 | int xx, yy, zz, newXDim, newYDim, newZDim; |
---|
| 353 | int xok, yok, zok; |
---|
| 354 | double value, totalWeight, newWeight; |
---|
| 355 | int zeroWeight; |
---|
| 356 | OccElementDbl *newBuf; |
---|
| 357 | OccElement *oldScanline; |
---|
| 358 | float newRes; |
---|
| 359 | |
---|
| 360 | if (argc != 1) { |
---|
| 361 | interp->result = "Usage: vrip_avgdown"; |
---|
| 362 | return TCL_ERROR; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | newXDim = int(ceil(frontRLEGrid->xdim/2.0)); |
---|
| 366 | newYDim = int(ceil(frontRLEGrid->ydim/2.0)); |
---|
| 367 | newZDim = int(ceil(frontRLEGrid->zdim/2.0)); |
---|
| 368 | delete backRLEGrid; |
---|
| 369 | backRLEGrid = new OccGridRLE(newXDim, newYDim, newZDim, CHUNK_SIZE); |
---|
| 370 | newRes = backRLEGrid->resolution = frontRLEGrid->resolution * 2; |
---|
| 371 | |
---|
| 372 | backRLEGrid->reset(); |
---|
| 373 | |
---|
| 374 | OccElementDbl *newScanlineDbl = new OccElementDbl[newXDim]; |
---|
| 375 | OccElement *newScanline = new OccElement[newXDim]; |
---|
| 376 | int *counts = new int[newXDim]; |
---|
| 377 | int *countPtr; |
---|
| 378 | |
---|
| 379 | if (CONSERVATIVE_AVG_DOWN) |
---|
| 380 | printf("Using conservative averaging down...\n"); |
---|
| 381 | |
---|
| 382 | printf("\n"); |
---|
| 383 | for (zz = 0; zz < backRLEGrid->zdim; zz++) { |
---|
| 384 | printf("\rProcessing slice %d of %d...", zz+1, backRLEGrid->zdim); |
---|
| 385 | fflush(stdout); |
---|
| 386 | zok = (2*zz) < (frontRLEGrid->zdim - 1); |
---|
| 387 | for (yy = 0; yy < backRLEGrid->ydim; yy++) { |
---|
| 388 | yok = (2*yy) < (frontRLEGrid->ydim - 1); |
---|
| 389 | oldScanline = frontRLEGrid->getScanline(2*yy,2*zz); |
---|
| 390 | newBuf = newScanlineDbl; |
---|
| 391 | countPtr = counts; |
---|
| 392 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 393 | xx++, newBuf++, countPtr++, oldScanline+=2) { |
---|
| 394 | |
---|
| 395 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 396 | |
---|
| 397 | *countPtr = 1; |
---|
| 398 | newBuf->value = oldScanline->value; |
---|
| 399 | newBuf->totalWeight = oldScanline->totalWeight; |
---|
| 400 | if (xok) { |
---|
| 401 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 402 | if (newWeight > 0) { |
---|
| 403 | if (newBuf->totalWeight == 0) { |
---|
| 404 | newBuf->value = (oldScanline+1)->value; |
---|
| 405 | newBuf->totalWeight = newWeight; |
---|
| 406 | } else { |
---|
| 407 | newBuf->value += (oldScanline+1)->value; |
---|
| 408 | newBuf->totalWeight += newWeight; |
---|
| 409 | *countPtr = *countPtr + 1; |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | if (yok) { |
---|
| 416 | oldScanline = frontRLEGrid->getScanline(2*yy+1,2*zz); |
---|
| 417 | newBuf = newScanlineDbl; |
---|
| 418 | countPtr = counts; |
---|
| 419 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 420 | xx++, newBuf++, countPtr++, oldScanline+=2) { |
---|
| 421 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 422 | |
---|
| 423 | |
---|
| 424 | newWeight = oldScanline->totalWeight; |
---|
| 425 | if (newWeight > 0) { |
---|
| 426 | if (newBuf->totalWeight == 0) { |
---|
| 427 | newBuf->value = oldScanline->value; |
---|
| 428 | newBuf->totalWeight = newWeight; |
---|
| 429 | } else { |
---|
| 430 | newBuf->value += oldScanline->value; |
---|
| 431 | newBuf->totalWeight += newWeight; |
---|
| 432 | *countPtr = *countPtr + 1; |
---|
| 433 | } |
---|
| 434 | } |
---|
| 435 | if (xok) { |
---|
| 436 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 437 | if (newWeight > 0) { |
---|
| 438 | if (newBuf->totalWeight == 0) { |
---|
| 439 | newBuf->value = (oldScanline+1)->value; |
---|
| 440 | newBuf->totalWeight = newWeight; |
---|
| 441 | } else { |
---|
| 442 | newBuf->value += (oldScanline+1)->value; |
---|
| 443 | newBuf->totalWeight += newWeight; |
---|
| 444 | *countPtr = *countPtr + 1; |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | if (zok) { |
---|
| 452 | oldScanline = frontRLEGrid->getScanline(2*yy,2*zz+1); |
---|
| 453 | newBuf = newScanlineDbl; |
---|
| 454 | countPtr = counts; |
---|
| 455 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 456 | xx++, newBuf++, countPtr++, oldScanline+=2) { |
---|
| 457 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 458 | |
---|
| 459 | |
---|
| 460 | newWeight = oldScanline->totalWeight; |
---|
| 461 | if (newWeight > 0) { |
---|
| 462 | if (newBuf->totalWeight == 0) { |
---|
| 463 | newBuf->value = oldScanline->value; |
---|
| 464 | newBuf->totalWeight = newWeight; |
---|
| 465 | } else { |
---|
| 466 | newBuf->value += oldScanline->value; |
---|
| 467 | newBuf->totalWeight += newWeight; |
---|
| 468 | *countPtr = *countPtr + 1; |
---|
| 469 | } |
---|
| 470 | } |
---|
| 471 | if (xok) { |
---|
| 472 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 473 | if (newWeight > 0) { |
---|
| 474 | if (newBuf->totalWeight == 0) { |
---|
| 475 | newBuf->value = (oldScanline+1)->value; |
---|
| 476 | newBuf->totalWeight = newWeight; |
---|
| 477 | } else { |
---|
| 478 | newBuf->value += (oldScanline+1)->value; |
---|
| 479 | newBuf->totalWeight += newWeight; |
---|
| 480 | *countPtr = *countPtr + 1; |
---|
| 481 | } |
---|
| 482 | } |
---|
| 483 | } |
---|
| 484 | } |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | if (yok&&zok) { |
---|
| 488 | oldScanline = frontRLEGrid->getScanline(2*yy+1,2*zz+1); |
---|
| 489 | newBuf = newScanlineDbl; |
---|
| 490 | countPtr = counts; |
---|
| 491 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 492 | xx++, newBuf++, countPtr++, oldScanline+=2) { |
---|
| 493 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 494 | |
---|
| 495 | |
---|
| 496 | newWeight = oldScanline->totalWeight; |
---|
| 497 | if (newWeight > 0) { |
---|
| 498 | if (newBuf->totalWeight == 0) { |
---|
| 499 | newBuf->value = oldScanline->value; |
---|
| 500 | newBuf->totalWeight = newWeight; |
---|
| 501 | } else { |
---|
| 502 | newBuf->value += oldScanline->value; |
---|
| 503 | newBuf->totalWeight += newWeight; |
---|
| 504 | *countPtr = *countPtr + 1; |
---|
| 505 | } |
---|
| 506 | } |
---|
| 507 | if (xok) { |
---|
| 508 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 509 | if (newWeight > 0) { |
---|
| 510 | if (newBuf->totalWeight == 0) { |
---|
| 511 | newBuf->value = (oldScanline+1)->value; |
---|
| 512 | newBuf->totalWeight = newWeight; |
---|
| 513 | } else { |
---|
| 514 | newBuf->value += (oldScanline+1)->value; |
---|
| 515 | newBuf->totalWeight += newWeight; |
---|
| 516 | *countPtr = *countPtr + 1; |
---|
| 517 | } |
---|
| 518 | } |
---|
| 519 | } |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | for (xx = 0; xx < backRLEGrid->xdim; xx++) { |
---|
| 524 | newScanline[xx].value = ushort(newScanlineDbl[xx].value/ |
---|
| 525 | counts[xx]); |
---|
| 526 | newScanline[xx].totalWeight = |
---|
| 527 | ushort(newScanlineDbl[xx].totalWeight/counts[xx]); |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | backRLEGrid->putScanline(newScanline, yy, zz); |
---|
| 531 | } |
---|
| 532 | } |
---|
| 533 | printf("\n"); |
---|
| 534 | |
---|
| 535 | |
---|
| 536 | |
---|
| 537 | // Coordinate systems??? |
---|
| 538 | |
---|
| 539 | delete frontRLEGrid; |
---|
| 540 | frontRLEGrid = new OccGridRLE(newXDim, newYDim, newZDim, CHUNK_SIZE); |
---|
| 541 | frontRLEGrid->resolution = newRes; |
---|
| 542 | swapgrids(); |
---|
| 543 | |
---|
| 544 | return TCL_OK; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | int |
---|
| 548 | Vrip_AvgDownRLE_oldCmd(ClientData, Tcl_Interp *interp, int argc, const char *[]) |
---|
| 549 | { |
---|
| 550 | int xx, yy, zz, newXDim, newYDim, newZDim; |
---|
| 551 | int xok, yok, zok; |
---|
| 552 | double value, totalWeight, newWeight; |
---|
| 553 | int zeroWeight; |
---|
| 554 | OccElementDbl *newBuf; |
---|
| 555 | OccElement *oldScanline; |
---|
| 556 | float newRes; |
---|
| 557 | |
---|
| 558 | if (argc != 1) { |
---|
| 559 | interp->result = "Usage: vrip_avgdown"; |
---|
| 560 | return TCL_ERROR; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | newXDim = int(ceil(frontRLEGrid->xdim/2.0)); |
---|
| 564 | newYDim = int(ceil(frontRLEGrid->ydim/2.0)); |
---|
| 565 | newZDim = int(ceil(frontRLEGrid->zdim/2.0)); |
---|
| 566 | newRes = frontRLEGrid->resolution/2; |
---|
| 567 | delete backRLEGrid; |
---|
| 568 | backRLEGrid = new OccGridRLE(newXDim, newYDim, newZDim, CHUNK_SIZE); |
---|
| 569 | backRLEGrid->resolution = newRes; |
---|
| 570 | |
---|
| 571 | backRLEGrid->reset(); |
---|
| 572 | |
---|
| 573 | OccElementDbl *newScanlineDbl = new OccElementDbl[newXDim]; |
---|
| 574 | OccElement *newScanline = new OccElement[newXDim]; |
---|
| 575 | |
---|
| 576 | if (CONSERVATIVE_AVG_DOWN) |
---|
| 577 | printf("Using conservative averaging down...\n"); |
---|
| 578 | |
---|
| 579 | for (zz = 0; zz < backRLEGrid->zdim; zz++) { |
---|
| 580 | zok = (2*zz) < (frontRLEGrid->zdim - 1); |
---|
| 581 | for (yy = 0; yy < backRLEGrid->ydim; yy++) { |
---|
| 582 | yok = (2*yy) < (frontRLEGrid->ydim - 1); |
---|
| 583 | oldScanline = frontRLEGrid->getScanline(2*yy,2*zz); |
---|
| 584 | newBuf = newScanlineDbl; |
---|
| 585 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 586 | xx++, newBuf++, oldScanline+=2) { |
---|
| 587 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 588 | value = oldScanline->value; |
---|
| 589 | newWeight = oldScanline->totalWeight; |
---|
| 590 | zeroWeight = newWeight == 0; |
---|
| 591 | totalWeight = newWeight; |
---|
| 592 | if (xok) { |
---|
| 593 | value += (oldScanline+1)->value; |
---|
| 594 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 595 | zeroWeight = (newWeight == 0) || zeroWeight; |
---|
| 596 | totalWeight += newWeight; |
---|
| 597 | } |
---|
| 598 | newBuf->value = value; |
---|
| 599 | if (zeroWeight) |
---|
| 600 | newBuf->totalWeight = 0; |
---|
| 601 | else |
---|
| 602 | newBuf->totalWeight = totalWeight; |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | if (yok) { |
---|
| 606 | oldScanline = frontRLEGrid->getScanline(2*yy+1,2*zz); |
---|
| 607 | newBuf = newScanlineDbl; |
---|
| 608 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 609 | xx++, newBuf++, oldScanline+=2) { |
---|
| 610 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 611 | value = oldScanline->value; |
---|
| 612 | newWeight = oldScanline->totalWeight; |
---|
| 613 | zeroWeight = newWeight == 0; |
---|
| 614 | totalWeight = newWeight; |
---|
| 615 | if (xok) { |
---|
| 616 | value += (oldScanline+1)->value; |
---|
| 617 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 618 | zeroWeight = (newWeight == 0) || zeroWeight; |
---|
| 619 | totalWeight += newWeight; |
---|
| 620 | } |
---|
| 621 | newBuf->value += value; |
---|
| 622 | if (CONSERVATIVE_AVG_DOWN && |
---|
| 623 | (zeroWeight || newBuf->totalWeight == 0)) |
---|
| 624 | newBuf->totalWeight = 0; |
---|
| 625 | else |
---|
| 626 | newBuf->totalWeight += totalWeight; |
---|
| 627 | } |
---|
| 628 | } |
---|
| 629 | |
---|
| 630 | if (zok) { |
---|
| 631 | oldScanline = frontRLEGrid->getScanline(2*yy,2*zz+1); |
---|
| 632 | newBuf = newScanlineDbl; |
---|
| 633 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 634 | xx++, newBuf++, oldScanline+=2) { |
---|
| 635 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 636 | value = oldScanline->value; |
---|
| 637 | newWeight = oldScanline->totalWeight; |
---|
| 638 | zeroWeight = newWeight == 0; |
---|
| 639 | totalWeight = newWeight; |
---|
| 640 | if (xok) { |
---|
| 641 | value += (oldScanline+1)->value; |
---|
| 642 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 643 | zeroWeight = (newWeight == 0) || zeroWeight; |
---|
| 644 | totalWeight += newWeight; |
---|
| 645 | } |
---|
| 646 | newBuf->value += value; |
---|
| 647 | if (CONSERVATIVE_AVG_DOWN && |
---|
| 648 | (zeroWeight || newBuf->totalWeight == 0)) |
---|
| 649 | newBuf->totalWeight = 0; |
---|
| 650 | else |
---|
| 651 | newBuf->totalWeight += totalWeight; |
---|
| 652 | } |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | if (yok&&zok) { |
---|
| 656 | oldScanline = frontRLEGrid->getScanline(2*yy+1,2*zz+1); |
---|
| 657 | newBuf = newScanlineDbl; |
---|
| 658 | for (xx = 0; xx < backRLEGrid->xdim; |
---|
| 659 | xx++, newBuf++, oldScanline+=2) { |
---|
| 660 | xok = (2*xx) < (frontRLEGrid->xdim - 1); |
---|
| 661 | value = oldScanline->value; |
---|
| 662 | newWeight = oldScanline->totalWeight; |
---|
| 663 | zeroWeight = newWeight == 0; |
---|
| 664 | totalWeight = newWeight; |
---|
| 665 | if (xok) { |
---|
| 666 | value += (oldScanline+1)->value; |
---|
| 667 | newWeight = (oldScanline+1)->totalWeight; |
---|
| 668 | zeroWeight = (newWeight == 0) || zeroWeight; |
---|
| 669 | totalWeight += newWeight; |
---|
| 670 | } |
---|
| 671 | newBuf->value += value; |
---|
| 672 | if (CONSERVATIVE_AVG_DOWN && |
---|
| 673 | (zeroWeight || newBuf->totalWeight == 0)) |
---|
| 674 | newBuf->totalWeight = 0; |
---|
| 675 | else |
---|
| 676 | newBuf->totalWeight += totalWeight; |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | for (xx = 0; xx < backRLEGrid->xdim; xx++) { |
---|
| 681 | newScanline[xx].value = ushort(newScanlineDbl[xx].value/8); |
---|
| 682 | newScanline[xx].totalWeight = |
---|
| 683 | ushort(newScanlineDbl[xx].totalWeight/8); |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | backRLEGrid->putScanline(newScanline, yy, zz); |
---|
| 687 | } |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | |
---|
| 691 | // Coordinate systems??? |
---|
| 692 | |
---|
| 693 | delete frontRLEGrid; |
---|
| 694 | frontRLEGrid = new OccGridRLE(newXDim, newYDim, newZDim, CHUNK_SIZE); |
---|
| 695 | frontRLEGrid->resolution = newRes; |
---|
| 696 | swapgrids(); |
---|
| 697 | |
---|
| 698 | return TCL_OK; |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | |
---|
| 702 | |
---|
| 703 | int |
---|
| 704 | Vrip_TransposeXZCmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 705 | { |
---|
| 706 | if (argc != 1) { |
---|
| 707 | interp->result = "Usage: vrip_transpxz"; |
---|
| 708 | return TCL_ERROR; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | theGrid->transposeXZ(); |
---|
| 712 | |
---|
| 713 | return TCL_OK; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | |
---|
| 717 | int |
---|
| 718 | Vrip_TransposeYZCmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 719 | { |
---|
| 720 | if (argc != 1) { |
---|
| 721 | interp->result = "Usage: vrip_transpyz"; |
---|
| 722 | return TCL_ERROR; |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | theGrid->transposeYZ(); |
---|
| 726 | |
---|
| 727 | return TCL_OK; |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | |
---|
| 731 | int |
---|
| 732 | Vrip_ExpandVarToConstCmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 733 | { |
---|
| 734 | if (argc != 1) { |
---|
| 735 | interp->result = "Usage: vrip_varfromconst"; |
---|
| 736 | return TCL_ERROR; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | printf("Doing first expansion of varying regions...\n"); |
---|
| 740 | expandVaryingFromConstant(frontRLEGrid, backRLEGrid); |
---|
| 741 | swapgrids(); |
---|
| 742 | |
---|
| 743 | printf("Transposing X <-> Z...\n"); |
---|
| 744 | frontRLEGrid->transposeXZ(backRLEGrid); |
---|
| 745 | swapgrids(); |
---|
| 746 | |
---|
| 747 | printf("Doing second expansion of varying regions...\n"); |
---|
| 748 | expandVaryingFromConstant(frontRLEGrid, backRLEGrid); |
---|
| 749 | swapgrids(); |
---|
| 750 | |
---|
| 751 | printf("Transposing X <-> Z...\n"); |
---|
| 752 | frontRLEGrid->transposeXZ(backRLEGrid); |
---|
| 753 | swapgrids(); |
---|
| 754 | |
---|
| 755 | printf("Transposing X <-> Y...\n"); |
---|
| 756 | frontRLEGrid->transposeXY(backRLEGrid); |
---|
| 757 | swapgrids(); |
---|
| 758 | |
---|
| 759 | printf("Doing final expansion of varying regions...\n"); |
---|
| 760 | expandVaryingFromConstant(frontRLEGrid, backRLEGrid); |
---|
| 761 | swapgrids(); |
---|
| 762 | |
---|
| 763 | printf("Transposing X <-> Y...\n"); |
---|
| 764 | frontRLEGrid->transposeXY(backRLEGrid); |
---|
| 765 | swapgrids(); |
---|
| 766 | |
---|
| 767 | return TCL_OK; |
---|
| 768 | } |
---|
| 769 | |
---|
| 770 | |
---|
| 771 | int |
---|
| 772 | Vrip_TransposeXZRLECmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 773 | { |
---|
| 774 | if (argc != 1) { |
---|
| 775 | interp->result = "Usage: vrip_transpxz"; |
---|
| 776 | return TCL_ERROR; |
---|
| 777 | } |
---|
| 778 | |
---|
| 779 | frontRLEGrid->transposeXZ(backRLEGrid); |
---|
| 780 | swapgrids(); |
---|
| 781 | |
---|
| 782 | return TCL_OK; |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | |
---|
| 786 | int |
---|
| 787 | Vrip_TransposeXYRLECmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 788 | { |
---|
| 789 | if (argc != 1) { |
---|
| 790 | interp->result = "Usage: vrip_transpxy"; |
---|
| 791 | return TCL_ERROR; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | frontRLEGrid->transposeXY(backRLEGrid); |
---|
| 795 | swapgrids(); |
---|
| 796 | |
---|
| 797 | return TCL_OK; |
---|
| 798 | } |
---|
| 799 | |
---|
| 800 | |
---|
| 801 | int |
---|
| 802 | Vrip_TransposeYZRLECmd(ClientData, Tcl_Interp *interp, int argc, const char **) |
---|
| 803 | { |
---|
| 804 | if (argc != 1) { |
---|
| 805 | interp->result = "Usage: vrip_transpyz"; |
---|
| 806 | return TCL_ERROR; |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | frontRLEGrid->transposeYZ(backRLEGrid); |
---|
| 810 | swapgrids(); |
---|
| 811 | |
---|
| 812 | return TCL_OK; |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | |
---|
| 816 | int |
---|
| 817 | Vrip_ExtractCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 818 | { |
---|
| 819 | int xmin, xmax, ymin, ymax, zmin, zmax; |
---|
| 820 | int yy, zz; |
---|
| 821 | int newXdim, newYdim, newZdim, xdim, ydim; |
---|
| 822 | OccElement *slice, *scanline; |
---|
| 823 | |
---|
| 824 | if (argc != 7) { |
---|
| 825 | interp->result = |
---|
| 826 | "Usage: vrip_extract <xmin> <xmax> <ymin> <ymax> <zmin> <zmax>"; |
---|
| 827 | return TCL_ERROR; |
---|
| 828 | } |
---|
| 829 | |
---|
| 830 | xmin = atoi(argv[1]); |
---|
| 831 | xmax = atoi(argv[2]); |
---|
| 832 | ymin = atoi(argv[3]); |
---|
| 833 | ymax = atoi(argv[4]); |
---|
| 834 | zmin = atoi(argv[5]); |
---|
| 835 | zmax = atoi(argv[6]); |
---|
| 836 | |
---|
| 837 | if (xmin < 0 || xmax >= frontRLEGrid->xdim || |
---|
| 838 | ymin < 0 || ymax >= frontRLEGrid->ydim || |
---|
| 839 | zmin < 0 || zmax >= frontRLEGrid->zdim) { |
---|
| 840 | |
---|
| 841 | interp->result = "Out of range!"; |
---|
| 842 | return TCL_ERROR; |
---|
| 843 | } |
---|
| 844 | |
---|
| 845 | newXdim = xmax - xmin; |
---|
| 846 | newYdim = ymax - ymin; |
---|
| 847 | newZdim = zmax - zmin; |
---|
| 848 | |
---|
| 849 | backRLEGrid->init(newXdim, newYdim, newZdim, CHUNK_SIZE); |
---|
| 850 | |
---|
| 851 | for (zz = zmin; zz < zmax; zz++) { |
---|
| 852 | slice = frontRLEGrid->getSlice("z", zz, &xdim, &ydim); |
---|
| 853 | for (yy = ymin; yy < ymax; yy++) { |
---|
| 854 | scanline = slice + yy*xdim + xmin; |
---|
| 855 | backRLEGrid->putScanline(scanline, yy-ymin, zz-zmin); |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | swapgrids(); |
---|
| 860 | |
---|
| 861 | return TCL_OK; |
---|
| 862 | } |
---|
| 863 | |
---|
| 864 | |
---|
| 865 | int |
---|
| 866 | Vrip_BlurVisBordersCmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) |
---|
| 867 | { |
---|
| 868 | int xx, yy, zz, i, j, k; |
---|
| 869 | OccElement *scanline, *elem, *buf; |
---|
| 870 | ushort totalWeight; |
---|
| 871 | double value, weight, sumWeight; |
---|
| 872 | |
---|
| 873 | if (argc != 1) { |
---|
| 874 | interp->result = "Usage: vrip_blurvb"; |
---|
| 875 | return TCL_ERROR; |
---|
| 876 | } |
---|
| 877 | |
---|
| 878 | |
---|
| 879 | scanline = new OccElement[frontRLEGrid->xdim]; |
---|
| 880 | |
---|
| 881 | backRLEGrid->reset(); |
---|
| 882 | for (zz = 0; zz < frontRLEGrid->zdim; zz++) { |
---|
| 883 | for (yy = 0; yy < frontRLEGrid->ydim; yy++) { |
---|
| 884 | buf = scanline; |
---|
| 885 | for (xx = 0; xx < frontRLEGrid->xdim; xx++, buf++) { |
---|
| 886 | elem = frontRLEGrid->getElement(xx,yy,zz); |
---|
| 887 | if (xx == 0 || xx == frontRLEGrid->xdim - 1 || |
---|
| 888 | yy == 0 || yy == frontRLEGrid->ydim - 1 || |
---|
| 889 | zz == 0 || zz == frontRLEGrid->zdim - 1) { |
---|
| 890 | |
---|
| 891 | buf->value = elem->value; |
---|
| 892 | buf->totalWeight = elem->totalWeight; |
---|
| 893 | } |
---|
| 894 | else { |
---|
| 895 | totalWeight = elem->totalWeight; |
---|
| 896 | buf->totalWeight = totalWeight; |
---|
| 897 | value = 0; |
---|
| 898 | sumWeight = 0; |
---|
| 899 | for (i = -1; i <= 1; i++) { |
---|
| 900 | for (j = -1; j <= 1; j++) { |
---|
| 901 | for (k = -1; k <= 1; k++) { |
---|
| 902 | weight = exp(-(i*i + j*j + k*k)*totalWeight/10); |
---|
| 903 | value += |
---|
| 904 | weight*frontRLEGrid->getElement(xx+k,yy+j,zz+i)->value; |
---|
| 905 | sumWeight += weight; |
---|
| 906 | } |
---|
| 907 | } |
---|
| 908 | } |
---|
| 909 | value /= sumWeight; |
---|
| 910 | buf->value = ushort(value); |
---|
| 911 | } |
---|
| 912 | } |
---|
| 913 | backRLEGrid->putScanline(scanline, yy, zz); |
---|
| 914 | } |
---|
| 915 | } |
---|
| 916 | |
---|
| 917 | delete [] scanline; |
---|
| 918 | |
---|
| 919 | swapgrids(); |
---|
| 920 | |
---|
| 921 | return TCL_OK; |
---|
| 922 | } |
---|
| 923 | |
---|