comparison Framework/Toolbox/GeometryToolbox.h @ 177:83200c4d07ca wasm

fix interpolation
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Mar 2018 12:32:01 +0100
parents 15d92d93738b
children 964118e7e6de
comparison
equal deleted inserted replaced
176:ab9c799f5de1 177:83200c4d07ca
93 const Vector& normal, 93 const Vector& normal,
94 double d, 94 double d,
95 const Vector& origin, 95 const Vector& origin,
96 const Vector& direction); 96 const Vector& direction);
97 97
98 inline float ComputeBilinearInterpolationInternal(float x, 98 inline float ComputeBilinearInterpolationUnitSquare(float x,
99 float y, 99 float y,
100 float f00, // source(x, y) 100 float f00, // source(0, 0)
101 float f01, // source(x + 1, y) 101 float f01, // source(1, 0)
102 float f10, // source(x, y + 1) 102 float f10, // source(0, 1)
103 float f11); // source(x + 1, y + 1) 103 float f11); // source(1, 1)
104 104
105 inline float ComputeBilinearInterpolation(float x, 105 inline float ComputeTrilinearInterpolationUnitSquare(float x,
106 float y, 106 float y,
107 float f00, // source(x, y) 107 float z,
108 float f01, // source(x + 1, y) 108 float f000, // source(0, 0, 0)
109 float f10, // source(x, y + 1) 109 float f001, // source(1, 0, 0)
110 float f11); // source(x + 1, y + 1) 110 float f010, // source(0, 1, 0)
111 111 float f011, // source(1, 1, 0)
112 inline float ComputeTrilinearInterpolation(float x, 112 float f100, // source(0, 0, 1)
113 float y, 113 float f101, // source(1, 0, 1)
114 float z, 114 float f110, // source(0, 1, 1)
115 float f000, // source(x, y, z) 115 float f111); // source(1, 1, 1)
116 float f001, // source(x + 1, y, z)
117 float f010, // source(x, y + 1, z)
118 float f011, // source(x + 1, y + 1, z)
119 float f100, // source(x, y, z + 1)
120 float f101, // source(x + 1, y, z + 1)
121 float f110, // source(x, y + 1, z + 1)
122 float f111); // source(x + 1, y + 1, z + 1)
123 }; 116 };
124 } 117 }
125 118
126 119
127 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolationInternal(float x, 120 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolationUnitSquare(float x,
128 float y, 121 float y,
129 float f00, 122 float f00,
130 float f01, 123 float f01,
131 float f10, 124 float f10,
132 float f11) 125 float f11)
133 { 126 {
134 // This function only works on fractional parts 127 // This function only works within the unit square
135 assert(x >= 0 && y >= 0 && x < 1 && y < 1); 128 assert(x >= 0 && y >= 0 && x <= 1 && y <= 1);
136 129
137 // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square 130 // https://en.wikipedia.org/wiki/Bilinear_interpolation#Unit_square
138 return (f00 * (1.0f - x) * (1.0f - y) + 131 return (f00 * (1.0f - x) * (1.0f - y) +
139 f01 * x * (1.0f - y) + 132 f01 * x * (1.0f - y) +
140 f10 * (1.0f - x) * y + 133 f10 * (1.0f - x) * y +
141 f11 * x * y); 134 f11 * x * y);
142 } 135 }
143 136
144 137
145 float OrthancStone::GeometryToolbox::ComputeBilinearInterpolation(float x, 138 float OrthancStone::GeometryToolbox::ComputeTrilinearInterpolationUnitSquare(float x,
146 float y, 139 float y,
147 float f00, 140 float z,
148 float f01, 141 float f000,
149 float f10, 142 float f001,
150 float f11) 143 float f010,
144 float f011,
145 float f100,
146 float f101,
147 float f110,
148 float f111)
151 { 149 {
152 // Compute the fractional part of (x,y)
153 float xx = x - std::floor(x);
154 float yy = y - std::floor(y);
155
156 return ComputeBilinearInterpolationInternal(xx, yy, f00, f01, f10, f11);
157 }
158
159
160 float OrthancStone::GeometryToolbox::ComputeTrilinearInterpolation(float x,
161 float y,
162 float z,
163 float f000,
164 float f001,
165 float f010,
166 float f011,
167 float f100,
168 float f101,
169 float f110,
170 float f111)
171 {
172 float xx = x - std::floor(x);
173 float yy = y - std::floor(y);
174 float zz = z - std::floor(z);
175
176 // "In practice, a trilinear interpolation is identical to two 150 // "In practice, a trilinear interpolation is identical to two
177 // bilinear interpolation combined with a linear interpolation" 151 // bilinear interpolation combined with a linear interpolation"
178 // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method 152 // https://en.wikipedia.org/wiki/Trilinear_interpolation#Method
179 float a = ComputeBilinearInterpolationInternal(xx, yy, f000, f001, f010, f011); 153 float a = ComputeBilinearInterpolationUnitSquare(x, y, f000, f001, f010, f011);
180 float b = ComputeBilinearInterpolationInternal(xx, yy, f100, f101, f110, f111); 154 float b = ComputeBilinearInterpolationUnitSquare(x, y, f100, f101, f110, f111);
181 155
182 return (1.0f - zz) * a + zz * b; 156 return (1.0f - z) * a + z * b;
183 } 157 }