[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 "DepthMap.h" |
---|
| 23 | |
---|
| 24 | void |
---|
| 25 | fillBackground(DepthMap *dm) |
---|
| 26 | { |
---|
| 27 | int xx, yy, xlim; |
---|
| 28 | float z, lastOutZ, lastInZ, newZ, slope; |
---|
| 29 | int in, lastOut, lastIn, i; |
---|
| 30 | |
---|
| 31 | for (yy = 0; yy < dm->ydim; yy++) { |
---|
| 32 | // Fill in the border |
---|
| 33 | xx = 0; |
---|
| 34 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 35 | while (!IS_VALID_DEPTH(z) && xx < dm->xdim) { |
---|
| 36 | xx++; |
---|
| 37 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 38 | } |
---|
| 39 | xlim = xx; |
---|
| 40 | |
---|
| 41 | if (xlim != dm->xdim) { |
---|
| 42 | for (xx = 0; xx < xlim; xx++) { |
---|
| 43 | dm->elems[xx+yy*dm->xdim].z = z; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | xx = dm->xdim - 1; |
---|
| 47 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 48 | while (!IS_VALID_DEPTH(z) && xx >= 0) { |
---|
| 49 | xx--; |
---|
| 50 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 51 | } |
---|
| 52 | xlim = xx; |
---|
| 53 | |
---|
| 54 | for (xx = xlim+1; xx < dm->xdim; xx++) { |
---|
| 55 | dm->elems[xx+yy*dm->xdim].z = z; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | else { |
---|
| 59 | for (xx = 0; xx < xlim; xx++) { |
---|
| 60 | dm->elems[xx+yy*dm->xdim].z = -1000; |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | void |
---|
| 67 | makeSilhouette(DepthMap *dm) |
---|
| 68 | { |
---|
| 69 | int xx, yy; |
---|
| 70 | float z; |
---|
| 71 | |
---|
| 72 | for (yy = 0; yy < dm->ydim; yy++) { |
---|
| 73 | for (xx = 0; xx < dm->xdim; xx++) { |
---|
| 74 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 75 | if (IS_VALID_DEPTH(z)) { |
---|
| 76 | dm->elems[xx+yy*dm->xdim].z = -FAR_AWAY_DEPTH; |
---|
| 77 | } else { |
---|
| 78 | dm->elems[xx+yy*dm->xdim].z = FAR_AWAY_DEPTH/2; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | void |
---|
| 86 | fillGaps(DepthMap *dm) |
---|
| 87 | { |
---|
| 88 | int xx, yy, xlim; |
---|
| 89 | float z, lastOutZ, lastInZ, newZ, slope; |
---|
| 90 | int in, lastOut, lastIn, i; |
---|
| 91 | |
---|
| 92 | for (yy = 0; yy < dm->ydim; yy++) { |
---|
| 93 | |
---|
| 94 | // Fill in gaps with simple linear interpolation |
---|
| 95 | // between depths of adjacent valid pixels |
---|
| 96 | |
---|
| 97 | in = FALSE; |
---|
| 98 | lastOut = -1; |
---|
| 99 | |
---|
| 100 | for (xx = 0; xx < dm->xdim; xx++) { |
---|
| 101 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 102 | if (IS_VALID_DEPTH(z) && !in) { |
---|
| 103 | |
---|
| 104 | lastIn = xx; |
---|
| 105 | // not used: // lastInZ = z; |
---|
| 106 | in = TRUE; |
---|
| 107 | |
---|
| 108 | if (lastOut == -1) |
---|
| 109 | continue; |
---|
| 110 | |
---|
| 111 | slope = (z-lastOutZ)/(lastIn - lastOut); |
---|
| 112 | newZ = lastOutZ + slope; |
---|
| 113 | for (i = lastOut+1; i <lastIn; i++, newZ+=slope) { |
---|
| 114 | dm->elems[i+yy*dm->xdim].z = newZ; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | else if (!IS_VALID_DEPTH(z) && in) { |
---|
| 118 | lastOut = xx-1; |
---|
| 119 | lastOutZ = dm->elems[xx-1+yy*dm->xdim].z; |
---|
| 120 | in = FALSE; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | void |
---|
| 128 | extendEdges(DepthMap *dm) |
---|
| 129 | { |
---|
| 130 | int xx, yy, xlim; |
---|
| 131 | float z, lastOutZ, lastInZ, newZ, slope; |
---|
| 132 | int in, lastOut, lastIn, i; |
---|
| 133 | |
---|
| 134 | for (yy = 0; yy < dm->ydim; yy++) { |
---|
| 135 | |
---|
| 136 | // Fill in gaps with simple linear interpolation |
---|
| 137 | // between depths of adjacent valid pixels |
---|
| 138 | |
---|
| 139 | in = FALSE; |
---|
| 140 | lastOut = -1; |
---|
| 141 | |
---|
| 142 | for (xx = 0; xx < dm->xdim; xx++) { |
---|
| 143 | z = dm->elems[xx+yy*dm->xdim].z; |
---|
| 144 | if (IS_VALID_DEPTH(z) && !in) { |
---|
| 145 | |
---|
| 146 | lastIn = xx; |
---|
| 147 | // not used: // lastInZ = z; |
---|
| 148 | in = TRUE; |
---|
| 149 | |
---|
| 150 | if (lastOut == -1) |
---|
| 151 | continue; |
---|
| 152 | |
---|
| 153 | slope = (z-lastOutZ)/(lastIn - lastOut); |
---|
| 154 | newZ = lastOutZ + slope; |
---|
| 155 | for (i = lastOut+1; i <lastIn; i++, newZ+=slope) { |
---|
| 156 | dm->elems[i+yy*dm->xdim].z = newZ; |
---|
| 157 | dm->elems[i+yy*dm->xdim].conf = 0; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | else if (!IS_VALID_DEPTH(z) && in) { |
---|
| 161 | lastOut = xx-1; |
---|
| 162 | lastOutZ = dm->elems[xx-1+yy*dm->xdim].z; |
---|
| 163 | in = FALSE; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|