00001 #ifndef OPENTISSUE_CORE_GEOMETRY_BARYCENTRIC_H
00002 #define OPENTISSUE_CORE_GEOMETRY_BARYCENTRIC_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_is_number.h>
00013 #include <OpenTissue/core/math/math_value_traits.h>
00014 #include <OpenTissue/core/math/math_matrix3x3.h>
00015
00016 #include <cmath>
00017 #include <cassert>
00018
00019 namespace OpenTissue
00020 {
00021 namespace geometry
00022 {
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 template<typename V>
00043 inline void barycentric_geometric(
00044 V const & x1
00045 , V const & x2
00046 , V const & x3
00047 , typename V::value_type & w1
00048 , typename V::value_type & w2
00049 )
00050 {
00051 using std::sqrt;
00052
00053 typedef typename V::value_type T;
00054 typedef typename V::value_traits value_traits;
00055
00056 V const u = x2-x1;
00057 T const uu = dot(u,u);
00058
00059 assert( is_number(uu) || !"barycentric_geometric(): NaN encountered");
00060 assert( uu > value_traits::zero() || !"barycentric_geometric(): Degenerate edge encountered");
00061
00062
00063 V const q = (dot(u, x3-x1)/ uu )*u + x1;
00064 V const a = q - x2;
00065
00066 T const aa = dot(a,a);
00067
00068 assert( is_number(aa) || !"barycentric_geometric(): NaN encountered");
00069
00070
00071 w1 = sqrt( aa / uu );
00072 w2 = value_traits::one() - w1;
00073
00074 assert( is_number(w1) || !"barycentric_geometric(): NaN encountered");
00075 assert( is_number(w2) || !"barycentric_geometric(): NaN encountered");
00076
00077 assert( w1 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00078 assert( w1 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00079 assert( w2 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00080 assert( w2 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 template<typename V>
00105 inline void barycentric_geometric(
00106 V const & x1
00107 , V const & x2
00108 , V const & x3
00109 , V const & x4
00110 , typename V::value_type & w1
00111 , typename V::value_type & w2
00112 , typename V::value_type & w3
00113 )
00114 {
00115 using std::sqrt;
00116
00117 typedef typename V::value_type T;
00118 typedef typename V::value_traits value_traits;
00119
00120 V const n = cross( x1-x3, x2-x3);
00121 T const nn = dot(n,n);
00122
00123 assert( is_number(nn) || !"barycentric_geometric(): NaN encountered");
00124 assert( nn > value_traits::zero() || !"barycentric_geometric(): Degenerate triangle encountered");
00125
00126 V const q = x4 - ( dot(n, x4 - x1)*n / nn );
00127 V const a = cross( x2-q, x3-q);
00128 V const b = cross( x1-q, x3-q);
00129
00130
00131 T const aa = dot(a,a);
00132 T const bb = dot(b,b);
00133
00134
00135 assert( is_number(aa) || !"barycentric_geometric(): NaN encountered");
00136 assert( is_number(bb) || !"barycentric_geometric(): NaN encountered");
00137
00138
00139 w1 = sqrt( aa / nn );
00140 w2 = sqrt( bb / nn );
00141 w3 = value_traits::one() - w1 - w2;
00142
00143 assert( is_number(w1) || !"barycentric_geometric(): NaN encountered");
00144 assert( is_number(w2) || !"barycentric_geometric(): NaN encountered");
00145 assert( is_number(w3) || !"barycentric_geometric(): NaN encountered");
00146
00147 assert( w1 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00148 assert( w1 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00149 assert( w2 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00150 assert( w2 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00151 assert( w3 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00152 assert( w3 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 template<typename V>
00173 inline void barycentric_geometric(
00174 V const & x1
00175 , V const & x2
00176 , V const & x3
00177 , V const & x4
00178 , V const & p
00179 , typename V::value_type & w1
00180 , typename V::value_type & w2
00181 , typename V::value_type & w3
00182 , typename V::value_type & w4
00183 )
00184 {
00185 using std::fabs;
00186
00187 typedef typename V::value_type T;
00188 typedef typename V::value_traits value_traits;
00189
00190 T const V6 = fabs( dot( (x1-x4), cross( x2-x4, x3-x4 ) ) );
00191
00192 assert( is_number(V6) || !"barycentric_geometric(): NaN encountered");
00193 assert( V6 > value_traits::zero() || !"barycentric_geometric(): Degenerate tetrahedron encountered");
00194
00195 w1 = fabs( dot( (x2-p), cross( (x3-p), (x4-p) ) ) ) / V6;
00196 w2 = fabs( dot( (x1-p), cross( (x3-p), (x4-p) ) ) ) / V6;
00197 w3 = fabs( dot( (x1-p), cross( (x2-p), (x4-p) ) ) ) / V6;
00198
00199 w4 = value_traits::one() - w1 -w2 - w3;
00200
00201 assert( is_number(w1) || !"barycentric_geometric(): NaN encountered");
00202 assert( is_number(w2) || !"barycentric_geometric(): NaN encountered");
00203 assert( is_number(w3) || !"barycentric_geometric(): NaN encountered");
00204 assert( is_number(w4) || !"barycentric_geometric(): NaN encountered");
00205
00206 assert( w1 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00207 assert( w1 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00208 assert( w2 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00209 assert( w2 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00210 assert( w3 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00211 assert( w3 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00212 assert( w4 >= value_traits::zero() || !"barycentric_geometric(): Illegal coordinate encountered");
00213 assert( w4 <= value_traits::one() || !"barycentric_geometric(): Illegal coordinate encountered");
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237 template<typename V>
00238 inline void barycentric_algebraic(
00239 V const & x1
00240 , V const & x2
00241 , V const & x3
00242 , V const & p
00243 , typename V::value_type & w1
00244 , typename V::value_type & w2
00245 , typename V::value_type & w3
00246 )
00247 {
00248 typedef typename V::value_type T;
00249 typedef math::ValueTraits<T> value_traits;
00250
00251
00252
00253
00254
00255
00256
00257 V x13 = x1-x3;
00258 V x23 = x2-x3;
00259 V x43 = p-x3;
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 T a11 = dot(x13 , x13);
00294 T a12 = dot(x13 , x23);
00295 T a22 = dot(x23 , x23);
00296 T b1 = dot(x13 , x43);
00297 T b2 = dot(x23 , x43);
00298
00299 assert( is_number(a11) || !"barycentric_algebraic(): NaN encountered");
00300 assert( is_number(a12) || !"barycentric_algebraic(): NaN encountered");
00301 assert( is_number(a22) || !"barycentric_algebraic(): NaN encountered");
00302 assert( is_number(b1) || !"barycentric_algebraic(): NaN encountered");
00303 assert( is_number(b2) || !"barycentric_algebraic(): NaN encountered");
00304
00305 assert( a22 < value_traits::zero() || a22>value_traits::zero() || !"barycentric_algebraic(): Degenerate triangle encountered");
00306
00307 T f = a12/a22;
00308 T m = (a11 - a12*f);
00309 T n = b1-(b2*f);
00310
00311 assert( is_number(f) || !"barycentric_algebraic(): NaN encountered");
00312 assert( is_number(m) || !"barycentric_algebraic(): NaN encountered");
00313 assert( is_number(n) || !"barycentric_algebraic(): NaN encountered");
00314
00315 assert( m < value_traits::zero() || m > value_traits::zero() || !"barycentric_algebraic(): potential division by zero");
00316
00317 w1 = n/m;
00318 w2 = b2/a22 - f*w1;
00319 w3 = value_traits::one() - w1 - w2;
00320
00321 assert( is_number(w1) || !"barycentric_algebraic(): NaN encountered");
00322 assert( is_number(w2) || !"barycentric_algebraic(): NaN encountered");
00323 assert( is_number(w3) || !"barycentric_algebraic(): NaN encountered");
00324 }
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 template<typename V>
00344 inline void barycentric_algebraic(
00345 V const & x1
00346 , V const & x2
00347 , V const & x3
00348 , V const & x4
00349 , V const & p
00350 , typename V::value_type & w1
00351 , typename V::value_type & w2
00352 , typename V::value_type & w3
00353 , typename V::value_type & w4
00354 )
00355 {
00356 typedef typename V::value_type T;
00357 typedef math::ValueTraits<T> value_traits;
00358 typedef math::Matrix3x3<T> M;
00359
00360 M P;
00361
00362 P(0,0) = x1(0) - x4(0);
00363 P(1,0) = x1(1) - x4(1);
00364 P(2,0) = x1(2) - x4(2);
00365
00366 P(0,1) = x2(0) - x4(0);
00367 P(1,1) = x2(1) - x4(1);
00368 P(2,1) = x2(2) - x4(2);
00369
00370 P(0,2) = x3(0) - x4(0);
00371 P(1,2) = x3(1) - x4(1);
00372 P(2,2) = x3(2) - x4(2);
00373
00374 P = math::inverse(P);
00375
00376 V w = P * V(p-x4);
00377
00378 w1 = w(0);
00379 w2 = w(1);
00380 w3 = w(2);
00381 w4 = value_traits::one() - w1 - w2 - w3;
00382
00383 assert( is_number(w1) || !"barycentric_algebraic(): NaN encountered");
00384 assert( is_number(w2) || !"barycentric_algebraic(): NaN encountered");
00385 assert( is_number(w3) || !"barycentric_algebraic(): NaN encountered");
00386 assert( is_number(w4) || !"barycentric_algebraic(): NaN encountered");
00387
00388 }
00389
00390 }
00391 }
00392
00393
00394 #endif