[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 _SECTION_RLE_ |
---|
| 23 | #define _SECTION_RLE_ |
---|
| 24 | |
---|
| 25 | #include "vrip.h" |
---|
| 26 | #include <limits.h> |
---|
| 27 | #include "mc_more.h" |
---|
| 28 | #include "ChunkAllocator.h" |
---|
| 29 | |
---|
| 30 | typedef ushort RunLength; |
---|
| 31 | |
---|
| 32 | struct SectionElement { |
---|
| 33 | float density; |
---|
| 34 | float nx; |
---|
| 35 | float ny; |
---|
| 36 | float nz; |
---|
| 37 | float confidence; |
---|
| 38 | uchar valid; |
---|
| 39 | uchar realData; |
---|
| 40 | TriSet set; |
---|
| 41 | }; |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | class SectionScanlineRLE { |
---|
| 45 | |
---|
| 46 | public: |
---|
| 47 | |
---|
| 48 | RunLength *lengths; |
---|
| 49 | SectionElement *elements; |
---|
| 50 | RunLength *currentLength; |
---|
| 51 | SectionElement *currentElem; |
---|
| 52 | |
---|
| 53 | inline void reset(); |
---|
| 54 | inline SectionElement *getNextElement(); |
---|
| 55 | inline RunLength getNextLength(); |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | class SectionRLE { |
---|
| 60 | |
---|
| 61 | public: |
---|
| 62 | enum {CONSTANT_DATA, VARYING_DATA, END_OF_RUN=USHRT_MAX}; |
---|
| 63 | static const ushort HIGHEST_BIT = 0x8000; |
---|
| 64 | |
---|
| 65 | int xdim, ydim; |
---|
| 66 | uchar *current; |
---|
| 67 | |
---|
| 68 | RunLength **lengthAddr; |
---|
| 69 | ChunkAllocator *lengthChunker; |
---|
| 70 | RunLength *currentLength; |
---|
| 71 | |
---|
| 72 | // These three cache elements are optimizations for |
---|
| 73 | // getElement |
---|
| 74 | RunLength *cachedX; |
---|
| 75 | RunLength **cachedLengthAddr; |
---|
| 76 | SectionElement **cachedElemAddr; |
---|
| 77 | |
---|
| 78 | SectionElement **elementAddr; |
---|
| 79 | ChunkAllocator *elementChunker; |
---|
| 80 | SectionElement *currentElem; |
---|
| 81 | |
---|
| 82 | SectionElement *defaultElement; |
---|
| 83 | |
---|
| 84 | SectionScanlineRLE *rleScanline; |
---|
| 85 | |
---|
| 86 | SectionRLE(); |
---|
| 87 | SectionRLE(int,int,int); |
---|
| 88 | |
---|
| 89 | void init(int,int,int); |
---|
| 90 | void freeSpace(); |
---|
| 91 | |
---|
| 92 | void clear(); |
---|
| 93 | void reset(); |
---|
| 94 | |
---|
| 95 | uchar *allocBytes(int num); |
---|
| 96 | inline SectionElement *getElement(int xx, int yy); |
---|
| 97 | SectionElement *getElementSlow(int xx, int yy); |
---|
| 98 | inline int getRunType(RunLength *length); |
---|
| 99 | void setRunType(RunLength *length, int runType); |
---|
| 100 | void putNextElement(SectionElement *element); |
---|
| 101 | void putNextLength(RunLength length); |
---|
| 102 | inline SectionElement *getNextElement(); |
---|
| 103 | inline RunLength getNextLength(); |
---|
| 104 | void allocNewRun(int y); |
---|
| 105 | void setScanline(int y); |
---|
| 106 | SectionScanlineRLE *getRLEScanline(int yy); |
---|
| 107 | void copyScanline(SectionScanlineRLE *rleScanline, int y); |
---|
| 108 | void copy(SectionRLE *other); |
---|
| 109 | |
---|
| 110 | ~SectionRLE(); |
---|
| 111 | }; |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | inline void |
---|
| 115 | SectionScanlineRLE::reset() |
---|
| 116 | { |
---|
| 117 | currentElem = elements; |
---|
| 118 | currentLength = lengths; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | inline SectionElement * |
---|
| 122 | SectionScanlineRLE::getNextElement() |
---|
| 123 | { |
---|
| 124 | return currentElem++; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | inline RunLength |
---|
| 128 | SectionScanlineRLE::getNextLength() |
---|
| 129 | { |
---|
| 130 | return *currentLength++; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | inline int |
---|
| 135 | SectionRLE::getRunType(RunLength *length) |
---|
| 136 | { |
---|
| 137 | #if 0 |
---|
| 138 | RunLength flag; |
---|
| 139 | |
---|
| 140 | if (*length == SectionRLE::END_OF_RUN) |
---|
| 141 | return SectionRLE::END_OF_RUN; |
---|
| 142 | |
---|
| 143 | flag = *length & SectionRLE::HIGHEST_BIT; |
---|
| 144 | *length = *length & ~SectionRLE::HIGHEST_BIT; |
---|
| 145 | if (flag) { |
---|
| 146 | return SectionRLE::VARYING_DATA; |
---|
| 147 | } else { |
---|
| 148 | return SectionRLE::CONSTANT_DATA; |
---|
| 149 | } |
---|
| 150 | #else |
---|
| 151 | // Lucas: Cutting down slightly on 'if' statements |
---|
| 152 | // to make it run faster |
---|
| 153 | RunLength flag = *length; |
---|
| 154 | if (flag == SectionRLE::END_OF_RUN) { |
---|
| 155 | return SectionRLE::END_OF_RUN; |
---|
| 156 | } else { |
---|
| 157 | *length = flag & ~SectionRLE::HIGHEST_BIT; |
---|
| 158 | // Move highest bit right to be 0 or 1 |
---|
| 159 | // Danger! This assumes that CONSTANT_DATA is 0, |
---|
| 160 | // and VARYING_DATA is 1 |
---|
| 161 | // since bitshift is faster than 'if' branch |
---|
| 162 | return(flag >> sizeof(RunLength)*8-1); |
---|
| 163 | } |
---|
| 164 | #endif |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | inline SectionElement * |
---|
| 168 | SectionRLE::getNextElement() |
---|
| 169 | { |
---|
| 170 | return currentElem++; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | inline RunLength |
---|
| 174 | SectionRLE::getNextLength() |
---|
| 175 | { |
---|
| 176 | return *currentLength++; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | inline SectionElement * |
---|
| 183 | SectionRLE::getElement(int xx, int yy) |
---|
| 184 | { |
---|
| 185 | // Warning!!! Unsafe (bypasses the proper access functions), |
---|
| 186 | // but fast... |
---|
| 187 | // In the if statement below, cachedLenghthAddr might still be |
---|
| 188 | // null, but it should never check the second condition if |
---|
| 189 | // the first condition is false, since cachedX is initialized |
---|
| 190 | // to the biggest possible value. |
---|
| 191 | if (cachedX[yy] <= xx && |
---|
| 192 | cachedX[yy]+((*cachedLengthAddr[yy]) & |
---|
| 193 | (~SectionRLE::HIGHEST_BIT)) > xx) { |
---|
| 194 | // This is the right run. Just grab the right element |
---|
| 195 | if ((*cachedLengthAddr[yy])&(SectionRLE::HIGHEST_BIT)) { |
---|
| 196 | // varying data.. index into it |
---|
| 197 | return(cachedElemAddr[yy]+(xx-cachedX[yy])); |
---|
| 198 | } else { |
---|
| 199 | // constant data.. get first element |
---|
| 200 | return(cachedElemAddr[yy]); |
---|
| 201 | } |
---|
| 202 | } else { |
---|
| 203 | // If cache doesn't have right run, resort to old |
---|
| 204 | // method (which also will cache the new run. |
---|
| 205 | return(getElementSlow(xx, yy)); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // This line isn't strictly necessary, but inserting it avoids |
---|
| 209 | // a compiler complaint. (Brian Curless, 6/5/06) |
---|
| 210 | return(getElementSlow(xx, yy)); |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | #endif |
---|
| 215 | |
---|