Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/GeometryToolbox.h @ 175:15d92d93738b wasm
fix interpolation with negative coordinates
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 09 Mar 2018 10:06:40 +0100 |
parents | 316324f42848 |
children | 83200c4d07ca |
comparison
equal
deleted
inserted
replaced
174:a7674c0ae4ac | 175:15d92d93738b |
---|---|
133 { | 133 { |
134 // This function only works on fractional parts | 134 // This function only works on fractional parts |
135 assert(x >= 0 && y >= 0 && x < 1 && y < 1); | 135 assert(x >= 0 && y >= 0 && x < 1 && y < 1); |
136 | 136 |
137 // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square | 137 // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square |
138 return f00 * (1 - x) * (1 - y) + f01 * x * (1 - y) + f10 * (1 - x) * y + f11 * x * y; | 138 return (f00 * (1.0f - x) * (1.0f - y) + |
139 f01 * x * (1.0f - y) + | |
140 f10 * (1.0f - x) * y + | |
141 f11 * x * y); | |
139 } | 142 } |
140 | 143 |
141 | 144 |
142 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolation(float x, | 145 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolation(float x, |
143 float y, | 146 float y, |
144 float f00, | 147 float f00, |
145 float f01, | 148 float f01, |
146 float f10, | 149 float f10, |
147 float f11) | 150 float f11) |
148 { | 151 { |
149 assert(x >= 0 && y >= 0); | |
150 | |
151 // Compute the fractional part of (x,y) | 152 // Compute the fractional part of (x,y) |
152 float xx = x - std::floor(x); | 153 float xx = x - std::floor(x); |
153 float yy = y - std::floor(y); | 154 float yy = y - std::floor(y); |
154 | 155 |
155 return ComputeBilinearInterpolationInternal(xx, yy, f00, f01, f10, f11); | 156 return ComputeBilinearInterpolationInternal(xx, yy, f00, f01, f10, f11); |
166 float f100, | 167 float f100, |
167 float f101, | 168 float f101, |
168 float f110, | 169 float f110, |
169 float f111) | 170 float f111) |
170 { | 171 { |
171 assert(x >= 0 && y >= 0 && z >= 0); | |
172 | |
173 float xx = x - std::floor(x); | 172 float xx = x - std::floor(x); |
174 float yy = y - std::floor(y); | 173 float yy = y - std::floor(y); |
175 float zz = z - std::floor(z); | 174 float zz = z - std::floor(z); |
176 | 175 |
177 // "In practice, a trilinear interpolation is identical to two | 176 // "In practice, a trilinear interpolation is identical to two |
178 // bilinear interpolation combined with a linear interpolation" | 177 // bilinear interpolation combined with a linear interpolation" |
179 // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method | 178 // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method |
180 float a = ComputeBilinearInterpolationInternal(xx, yy, f000, f001, f010, f011); | 179 float a = ComputeBilinearInterpolationInternal(xx, yy, f000, f001, f010, f011); |
181 float b = ComputeBilinearInterpolationInternal(xx, yy, f100, f101, f110, f111); | 180 float b = ComputeBilinearInterpolationInternal(xx, yy, f100, f101, f110, f111); |
182 | 181 |
183 return (1 - zz) * a + zz * b; | 182 return (1.0f - zz) * a + zz * b; |
184 } | 183 } |