[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 | #ifndef _OCC_GRID_RLE_ |
---|
| 23 | #define _OCC_GRID_RLE_ |
---|
| 24 | |
---|
| 25 | #include "vrip.h" |
---|
| 26 | #include "ChunkAllocator.h" |
---|
| 27 | #include <limits.h> |
---|
| 28 | #include "OccGrid.h" |
---|
| 29 | |
---|
| 30 | typedef ushort RunLength; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | class OccScanlineRLE { |
---|
| 34 | |
---|
| 35 | public: |
---|
| 36 | |
---|
| 37 | RunLength *allocatedLengths; |
---|
| 38 | OccElement *allocatedElements; |
---|
| 39 | RunLength *lengths; |
---|
| 40 | OccElement *elements; |
---|
| 41 | RunLength *currentLength; |
---|
| 42 | OccElement *currentElem; |
---|
| 43 | |
---|
| 44 | OccScanlineRLE(); |
---|
| 45 | OccScanlineRLE(int dim); |
---|
| 46 | ~OccScanlineRLE(); |
---|
| 47 | |
---|
| 48 | void reset(); |
---|
| 49 | OccElement *getNextElement(); |
---|
| 50 | RunLength getNextLength(); |
---|
| 51 | void putNextElement(OccElement *element); |
---|
| 52 | void putNextLength(RunLength length); |
---|
| 53 | }; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | class OccGridRLE { |
---|
| 58 | |
---|
| 59 | public: |
---|
| 60 | enum {CONSTANT_DATA, VARYING_DATA, END_OF_RUN=USHRT_MAX}; |
---|
| 61 | enum {FALSE_DATA_BIT=0x8000}; |
---|
| 62 | static const ushort HIGHEST_BIT = 0x8000; |
---|
| 63 | |
---|
| 64 | int xdim, ydim, zdim; |
---|
| 65 | float resolution; |
---|
| 66 | vec3f origin; |
---|
| 67 | vec3f *sliceOrigins; |
---|
| 68 | int axis; |
---|
| 69 | int flip; |
---|
| 70 | |
---|
| 71 | RunLength **lengthAddr; |
---|
| 72 | ChunkAllocator *lengthChunker; |
---|
| 73 | RunLength *currentLength; |
---|
| 74 | |
---|
| 75 | OccElement **elementAddr; |
---|
| 76 | ChunkAllocator *elementChunker; |
---|
| 77 | OccElement *currentElem; |
---|
| 78 | |
---|
| 79 | OccScanlineRLE *rleScanline; |
---|
| 80 | OccElement *slice; |
---|
| 81 | OccElement *otherSlice; |
---|
| 82 | OccElement *scanline; |
---|
| 83 | OccElement *defaultElement; |
---|
| 84 | int transpXZbytesPerScanline; |
---|
| 85 | uchar *transpXZslice; |
---|
| 86 | |
---|
| 87 | OccElement emptyNoWeight; |
---|
| 88 | OccElement fullNoWeight; |
---|
| 89 | |
---|
| 90 | OccGridRLE(); |
---|
| 91 | OccGridRLE(int,int,int,int); |
---|
| 92 | |
---|
| 93 | void init(int,int,int,int); |
---|
| 94 | void freeSpace(); |
---|
| 95 | |
---|
| 96 | void copy(OccGridRLE *other); |
---|
| 97 | void copyParams(OccGridRLE *other); |
---|
| 98 | |
---|
| 99 | int transposeXZ(OccGridRLE *other); |
---|
| 100 | int transposeXY(OccGridRLE *other); |
---|
| 101 | int transposeYZ(OccGridRLE *other); |
---|
| 102 | |
---|
| 103 | void clear(); |
---|
| 104 | void reset(); |
---|
| 105 | |
---|
| 106 | OccElement voxRead (int x, int y, int z); |
---|
| 107 | void voxWrite(int x, int y, int z, OccElement &el); |
---|
| 108 | |
---|
| 109 | OccGrid *voxExpand(); |
---|
| 110 | void voxRLE(OccGrid *og); |
---|
| 111 | |
---|
| 112 | OccElement *getSlice(const char *axis, int sliceNum, int *pxdim, int *pydim); |
---|
| 113 | OccElement *getScanline(int y, int z); |
---|
| 114 | void putScanline(OccElement *line, int y, int z); |
---|
| 115 | void copyScanline(OccScanlineRLE *rleScanline, int y, int z); |
---|
| 116 | OccElement *transposeSlice(int xdim,int ydim); |
---|
| 117 | OccElement *getElement(int xx, int yy, int zz); |
---|
| 118 | void runStats(int yy, int zz, int *numRuns, int *numElems); |
---|
| 119 | int getRunType(RunLength *length); |
---|
| 120 | void setRunType(RunLength *length, int runType); |
---|
| 121 | void putNextElement(OccElement *element); |
---|
| 122 | void putNextLength(RunLength length); |
---|
| 123 | OccElement *getNextElement(); |
---|
| 124 | RunLength getNextLength(); |
---|
| 125 | void allocNewRun(int y, int z); |
---|
| 126 | void setScanline(int y, int z); |
---|
| 127 | int writeDen(const char *); |
---|
| 128 | int write(const char *); |
---|
| 129 | int read(const char *); |
---|
| 130 | int decideType(OccElement *); |
---|
| 131 | OccScanlineRLE *getRLEScanline(int yy, int zz); |
---|
| 132 | ~OccGridRLE(); |
---|
| 133 | }; |
---|
| 134 | |
---|
| 135 | |
---|
| 136 | inline void |
---|
| 137 | OccScanlineRLE::reset() |
---|
| 138 | { |
---|
| 139 | currentElem = elements; |
---|
| 140 | currentLength = lengths; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | inline OccElement * |
---|
| 144 | OccScanlineRLE::getNextElement() |
---|
| 145 | { |
---|
| 146 | return currentElem++; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | |
---|
| 150 | inline RunLength |
---|
| 151 | OccScanlineRLE::getNextLength() |
---|
| 152 | { |
---|
| 153 | return *currentLength++; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | inline void |
---|
| 158 | OccScanlineRLE::putNextElement(OccElement *element) |
---|
| 159 | { |
---|
| 160 | currentElem->value = element->value; |
---|
| 161 | currentElem->totalWeight = element->totalWeight; |
---|
| 162 | currentElem++; |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | inline void |
---|
| 167 | OccScanlineRLE::putNextLength(RunLength length) |
---|
| 168 | { |
---|
| 169 | *currentLength = length; |
---|
| 170 | currentLength++; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | /* |
---|
| 175 | void |
---|
| 176 | OccScanlineRLE::copyScanline(OccScanlineRLE *rleScanline) |
---|
| 177 | { |
---|
| 178 | int i; |
---|
| 179 | RunLength length; |
---|
| 180 | int runType; |
---|
| 181 | OccElement *element; |
---|
| 182 | |
---|
| 183 | this->reset(); |
---|
| 184 | rleScanline->reset(); |
---|
| 185 | |
---|
| 186 | while (TRUE) { |
---|
| 187 | length = rleScanline->getNextLength(); |
---|
| 188 | this->putNextLength(length); |
---|
| 189 | |
---|
| 190 | runType = getRunType(&length); |
---|
| 191 | |
---|
| 192 | if (runType == OccGridRLE::END_OF_RUN) |
---|
| 193 | break; |
---|
| 194 | |
---|
| 195 | if (runType == OccGridRLE::CONSTANT_DATA) { |
---|
| 196 | element = rleScanline->getNextElement(); |
---|
| 197 | this->putNextElement(element); |
---|
| 198 | } |
---|
| 199 | else { |
---|
| 200 | for (i=0; i<length; i++) { |
---|
| 201 | element = rleScanline->getNextElement(); |
---|
| 202 | this->putNextElement(element); |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | */ |
---|
| 208 | |
---|
| 209 | inline void |
---|
| 210 | OccGridRLE::putNextElement(OccElement *element) |
---|
| 211 | { |
---|
| 212 | OccElement *newElem = |
---|
| 213 | (OccElement *)this->elementChunker->alloc(sizeof(OccElement)); |
---|
| 214 | newElem->value = element->value; |
---|
| 215 | newElem->totalWeight = element->totalWeight; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
| 219 | inline void |
---|
| 220 | OccGridRLE::putNextLength(RunLength length) |
---|
| 221 | { |
---|
| 222 | RunLength *newLength = |
---|
| 223 | (RunLength *)this->lengthChunker->alloc(sizeof(RunLength)); |
---|
| 224 | *newLength = length; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | inline OccElement * |
---|
| 229 | OccGridRLE::getNextElement() |
---|
| 230 | { |
---|
| 231 | return currentElem++; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | inline RunLength |
---|
| 235 | OccGridRLE::getNextLength() |
---|
| 236 | { |
---|
| 237 | return *currentLength++; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | |
---|
| 241 | inline int |
---|
| 242 | OccGridRLE::getRunType(RunLength *length) |
---|
| 243 | { |
---|
| 244 | int flag; |
---|
| 245 | |
---|
| 246 | |
---|
| 247 | if (*length == OccGridRLE::END_OF_RUN) |
---|
| 248 | return OccGridRLE::END_OF_RUN; |
---|
| 249 | |
---|
| 250 | flag = *length & OccGridRLE::HIGHEST_BIT; |
---|
| 251 | *length = *length & ~OccGridRLE::HIGHEST_BIT; |
---|
| 252 | if (flag) { |
---|
| 253 | return OccGridRLE::VARYING_DATA; |
---|
| 254 | } else { |
---|
| 255 | return OccGridRLE::CONSTANT_DATA; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | |
---|
| 260 | inline void |
---|
| 261 | OccGridRLE::setRunType(RunLength *length, int runType) |
---|
| 262 | { |
---|
| 263 | if (runType == OccGridRLE::END_OF_RUN) |
---|
| 264 | *length = OccGridRLE::END_OF_RUN; |
---|
| 265 | else if (runType == OccGridRLE::VARYING_DATA) |
---|
| 266 | *length = *length | OccGridRLE::HIGHEST_BIT; |
---|
| 267 | |
---|
| 268 | // else leave it alone |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | inline int |
---|
| 273 | OccGridRLE::decideType(OccElement *element) |
---|
| 274 | { |
---|
| 275 | if ( (element->value == emptyNoWeight.value && |
---|
| 276 | element->totalWeight == emptyNoWeight.totalWeight) || |
---|
| 277 | (element->value == fullNoWeight.value && |
---|
| 278 | element->totalWeight == fullNoWeight.totalWeight) ) { |
---|
| 279 | |
---|
| 280 | return OccGridRLE::CONSTANT_DATA; |
---|
| 281 | |
---|
| 282 | } else { |
---|
| 283 | |
---|
| 284 | return OccGridRLE::VARYING_DATA; |
---|
| 285 | |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | #endif |
---|
| 290 | |
---|