[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 <stdio.h> |
---|
| 24 | |
---|
| 25 | #include "OccGrid.h" |
---|
| 26 | //#include "levden.h" |
---|
| 27 | |
---|
| 28 | OccGrid::OccGrid() |
---|
| 29 | { |
---|
| 30 | this->xdim = this->ydim = this->zdim = 0; |
---|
| 31 | this->elems = NULL; |
---|
| 32 | this->axis = Z_AXIS; |
---|
| 33 | this->flip = FALSE; |
---|
| 34 | this->origin[0] = 0; |
---|
| 35 | this->origin[1] = 0; |
---|
| 36 | this->origin[2] = 0; |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | OccGrid::OccGrid(int xd, int yd, int zd) |
---|
| 41 | { |
---|
| 42 | int size1, size2, size3, sliceSize; |
---|
| 43 | |
---|
| 44 | this->xdim = xd; |
---|
| 45 | this->ydim = yd; |
---|
| 46 | this->zdim = zd; |
---|
| 47 | this->elems = new OccElement[xd*yd*zd]; |
---|
| 48 | if (this->elems == NULL) { |
---|
| 49 | this->xdim = 0; |
---|
| 50 | this->ydim = 0; |
---|
| 51 | this->zdim = 0; |
---|
| 52 | printf("OccGrid::new - insufficient memory.\n"); |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | int maxdim = MAX(this->xdim, MAX(this->ydim, this->zdim)); |
---|
| 56 | this->sliceOrigins = new vec3f[maxdim]; |
---|
| 57 | |
---|
| 58 | this->axis = Z_AXIS; |
---|
| 59 | this->flip = FALSE; |
---|
| 60 | this->origin[0] = 0; |
---|
| 61 | this->origin[1] = 0; |
---|
| 62 | this->origin[2] = 0; |
---|
| 63 | |
---|
| 64 | size1 = xd*yd; |
---|
| 65 | size2 = xd*zd; |
---|
| 66 | size3 = yd*zd; |
---|
| 67 | |
---|
| 68 | sliceSize = MAX(MAX(size1, size2), size3); |
---|
| 69 | this->slice = new OccElement[sliceSize]; |
---|
| 70 | if (this->elems == NULL) { |
---|
| 71 | printf("OccGrid::new - insufficient memory.\n"); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | OccGrid::~OccGrid() |
---|
| 77 | { |
---|
| 78 | if (this->elems != NULL) { |
---|
| 79 | delete [] this->elems; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | int |
---|
| 85 | OccGrid::write(const char *) |
---|
| 86 | { |
---|
| 87 | return TRUE; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | int |
---|
| 92 | OccGrid::writeDen(const char *filename) |
---|
| 93 | { |
---|
| 94 | /* |
---|
| 95 | |
---|
| 96 | orig_min[0] = extr_min[0] = map_min[0] = 0; |
---|
| 97 | orig_min[1] = extr_min[1] = map_min[1] = 0; |
---|
| 98 | orig_min[2] = extr_min[2] = map_min[2] = 0; |
---|
| 99 | |
---|
| 100 | orig_max[0] = extr_max[0] = map_max[0] = this->xdim - 1; |
---|
| 101 | orig_max[1] = extr_max[1] = map_max[1] = this->ydim - 1; |
---|
| 102 | orig_max[2] = extr_max[2] = map_max[2] = this->zdim - 1; |
---|
| 103 | |
---|
| 104 | orig_len[0] = extr_len[0] = map_len[0] = this->xdim; |
---|
| 105 | orig_len[1] = extr_len[1] = map_len[1] = this->ydim; |
---|
| 106 | orig_len[2] = extr_len[2] = map_len[2] = this->zdim; |
---|
| 107 | |
---|
| 108 | map_warps = 0; |
---|
| 109 | |
---|
| 110 | map_length = (long)map_len[X] * (long)map_len[Y] * (long)map_len[Z]; |
---|
| 111 | |
---|
| 112 | map_address = (uchar *)(this->elems); |
---|
| 113 | |
---|
| 114 | Store_Indexed_DEN_File(filename, sizeof(OccElement)); |
---|
| 115 | |
---|
| 116 | */ |
---|
| 117 | |
---|
| 118 | // So much for error checking... |
---|
| 119 | return TRUE; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | int |
---|
| 124 | OccGrid::read(const char *) |
---|
| 125 | { |
---|
| 126 | return TRUE; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | int |
---|
| 131 | OccGrid::transposeXZ() |
---|
| 132 | { |
---|
| 133 | int dim, bufInc; |
---|
| 134 | OccElement *buf1, *buf2; |
---|
| 135 | int xx,yy,zz; |
---|
| 136 | |
---|
| 137 | // Assumes that all dimensions are equal |
---|
| 138 | if (this->xdim != this->ydim || this->ydim != this->zdim) { |
---|
| 139 | printf("OccGrid::transposeXZ - need equal dimensions.\n"); |
---|
| 140 | return FALSE; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | dim = this->xdim; |
---|
| 144 | bufInc = dim*dim; |
---|
| 145 | |
---|
| 146 | for (zz = 0; zz < dim; zz++) { |
---|
| 147 | for (yy = 0; yy < dim; yy++) { |
---|
| 148 | buf1 = this->elems + yy*dim + zz*dim*dim; |
---|
| 149 | buf2 = this->elems + zz + yy*dim; |
---|
| 150 | for (xx = 0; xx < zz; xx++, buf1++, buf2+=bufInc) { |
---|
| 151 | SWAP_USHORT(buf1->value, buf2->value); |
---|
| 152 | SWAP_USHORT(buf1->totalWeight, buf2->totalWeight); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | SWAP_FLOAT(this->origin[0], this->origin[2]); |
---|
| 158 | |
---|
| 159 | return TRUE; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | int |
---|
| 164 | OccGrid::transposeYZ() |
---|
| 165 | { |
---|
| 166 | int dim; |
---|
| 167 | OccElement *buf1, *buf2; |
---|
| 168 | int xx,yy,zz; |
---|
| 169 | |
---|
| 170 | // Assumes that all dimensions are equal |
---|
| 171 | if (this->xdim != this->ydim || this->ydim != this->zdim) { |
---|
| 172 | printf("OccGrid::transposeYZ - need equal dimensions.\n"); |
---|
| 173 | return FALSE; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | dim = this->xdim; |
---|
| 177 | |
---|
| 178 | for (zz = 0; zz < dim; zz++) { |
---|
| 179 | for (yy = 0; yy < zz; yy++) { |
---|
| 180 | buf1 = this->elems + yy*dim + zz*dim*dim; |
---|
| 181 | buf2 = this->elems + zz*dim + yy*dim*dim; |
---|
| 182 | for (xx = 0; xx < dim; xx++, buf1++, buf2++) { |
---|
| 183 | SWAP_USHORT(buf1->value, buf2->value); |
---|
| 184 | SWAP_USHORT(buf1->totalWeight, buf2->totalWeight); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | SWAP_FLOAT(this->origin[1], this->origin[2]); |
---|
| 190 | |
---|
| 191 | return TRUE; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | int |
---|
| 195 | OccGrid::copy(OccGrid *src) |
---|
| 196 | { |
---|
| 197 | OccElement *el = this->elems; |
---|
| 198 | OccElement *srcel = src->elems; |
---|
| 199 | |
---|
| 200 | if (this->xdim != src->xdim || this->ydim != src->ydim || |
---|
| 201 | this->zdim != src->zdim) { |
---|
| 202 | fprintf(stderr, "Error: OccGrid::Copy -- grids must be equal size\n"); |
---|
| 203 | return FALSE; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | int nels = this->xdim * this->ydim * this->zdim; |
---|
| 207 | // For every element, copy value & weight |
---|
| 208 | for (int i=0; i < nels; i++) { |
---|
| 209 | el->value = srcel->value; |
---|
| 210 | el->totalWeight = srcel->totalWeight; |
---|
| 211 | el++; |
---|
| 212 | srcel++; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return TRUE; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | OccElement * |
---|
| 220 | OccGrid::getSlice(const char *axis, int sliceNum, int *pxdim, int *pydim) |
---|
| 221 | { |
---|
| 222 | OccElement *buf1, *buf2; |
---|
| 223 | int xx, yy, zz, bufInc; |
---|
| 224 | |
---|
| 225 | buf1 = slice; |
---|
| 226 | if (EQSTR(axis, "x")) { |
---|
| 227 | if (sliceNum >= this->xdim) |
---|
| 228 | return this->slice; |
---|
| 229 | bufInc = -this->xdim*this->ydim; |
---|
| 230 | for (yy = 0; yy < this->ydim; yy++) { |
---|
| 231 | buf2 = this->address(sliceNum, yy, this->zdim-1); |
---|
| 232 | for (zz = this->zdim-1; zz >= 0 ; zz--, buf1++, buf2+=bufInc) { |
---|
| 233 | buf1->value = buf2->value; |
---|
| 234 | buf1->totalWeight = buf2->totalWeight; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | *pxdim = this->ydim; |
---|
| 238 | *pydim = this->zdim; |
---|
| 239 | } |
---|
| 240 | else if (EQSTR(axis, "y")) { |
---|
| 241 | if (sliceNum >= this->ydim) |
---|
| 242 | return this->slice; |
---|
| 243 | bufInc = 1; |
---|
| 244 | for (zz = this->zdim-1; zz >= 0; zz--) { |
---|
| 245 | buf2 = this->address(0, sliceNum, zz); |
---|
| 246 | for (xx = 0; xx < this->xdim; xx++, buf1++, buf2+=bufInc) { |
---|
| 247 | buf1->value = buf2->value; |
---|
| 248 | buf1->totalWeight = buf2->totalWeight; |
---|
| 249 | } |
---|
| 250 | } |
---|
| 251 | *pxdim = this->xdim; |
---|
| 252 | *pydim = this->zdim; |
---|
| 253 | } |
---|
| 254 | else if (EQSTR(axis, "z")) { |
---|
| 255 | if (sliceNum >= this->ydim) |
---|
| 256 | return this->slice; |
---|
| 257 | bufInc = 1; |
---|
| 258 | for (yy = 0; yy < this->ydim; yy++) { |
---|
| 259 | buf2 = this->address(0, yy, sliceNum); |
---|
| 260 | for (xx = 0; xx < this->xdim; xx++, buf1++, buf2+=bufInc) { |
---|
| 261 | buf1->value = buf2->value; |
---|
| 262 | buf1->totalWeight = buf2->totalWeight; |
---|
| 263 | } |
---|
| 264 | } |
---|
| 265 | *pxdim = this->xdim; |
---|
| 266 | *pydim = this->ydim; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | return this->slice; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | void |
---|
| 274 | OccGrid::clear() |
---|
| 275 | { |
---|
| 276 | OccElement *buf = this->elems; |
---|
| 277 | for (int i = 0; i < this->xdim*this->ydim*this->zdim; i++, buf++) { |
---|
| 278 | buf->value = 0; |
---|
| 279 | buf->totalWeight = 0; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|