15 static inline bool rtIsNaN(
double value ) {
return (value != value); }
16 static inline bool rtIsInf(
double value )
18 return (value == std::numeric_limits< double >::infinity()
19 || value == -std::numeric_limits< double >::infinity());
22 double b_xnrm2(
int n,
const double x[3],
int ix0 )
35 y = std::abs( x[ix0 - 1] );
39 scale = 3.3121686421112381E-170;
40 kend = ( ix0 +
n ) - 1;
41 for( k = ix0; k <= kend; k++ )
43 absxk = std::abs( x[k - 1] );
57 y = scale * std::sqrt( y );
64 double xnrm2(
int n,
const double x[9],
int ix0 )
77 y = std::abs( x[ix0 - 1] );
81 scale = 3.3121686421112381E-170;
82 kend = ( ix0 +
n ) - 1;
83 for( k = ix0; k <= kend; k++ )
85 absxk = std::abs( x[k - 1] );
99 y = scale * std::sqrt( y );
106 void b_xaxpy(
int n,
double a,
const double x[9],
int ix0,
double y[3],
int iy0 )
112 if( ( n < 1 ) || ( a == 0.0 ) )
120 for( k = 0; k <= i3; k++ )
129 void c_xaxpy(
int n,
double a,
const double x[3],
int ix0,
double y[9],
int iy0 )
135 if( ( n < 1 ) || ( a == 0.0 ) )
143 for( k = 0; k <= i4; k++ )
152 void xaxpy(
int n,
double a,
int ix0,
double y[9],
int iy0 )
158 if( ( n < 1 ) || ( a == 0.0 ) )
166 for( k = 0; k <=
i2; k++ )
175 double xdotc(
int n,
const double x[9],
int ix0,
const double y[9],
int iy0 )
186 for( k = 0; k <
n; k++ )
188 d += x[ix - 1] * y[iy - 1];
197 void xrotg(
double * a,
double *
b,
double *
c,
double *
s )
206 absa = std::abs( *a );
207 absb = std::abs( *b );
225 scale *= std::sqrt( ads * ads + bds * bds );
250 void xrot(
double x[9],
int ix0,
int iy0,
double c,
double s )
257 temp = c * x[ix] + s * x[iy];
258 x[iy] = c * x[iy] - s * x[ix];
262 temp = c * x[ix] + s * x[iy];
263 x[iy] = c * x[iy] - s * x[ix];
267 temp = c * x[ix] + s * x[iy];
268 x[iy] = c * x[iy] - s * x[ix];
272 void b_sqrt(
double * x ) { *x = std::sqrt( *x ); }
274 void xswap(
double x[9],
int ix0,
int iy0 )
298 void svd_3x3(
const double A[9],
double U[9],
double s[3],
double V[9] )
304 bool apply_transform;
321 memcpy( &b_A[0], &A[0], 9U *
sizeof(
double ) );
328 memset( &U[0], 0, 9U *
sizeof(
double ) );
329 memset( &Vf[0], 0, 9U *
sizeof(
double ) );
330 apply_transform =
false;
331 nrm = xnrm2( 3, b_A, 1 );
334 apply_transform =
true;
340 if( std::abs( nrm ) >= 1.0020841800044864E-292 )
343 for( qp1 = 1; qp1 < 4; qp1++ )
350 for( qp1 = 1; qp1 < 4; qp1++ )
364 for( kase = 2; kase < 4; kase++ )
366 qjj = 3 * (kase - 1);
367 if( apply_transform )
369 xaxpy( 3, -(xdotc( 3, b_A, 1, b_A, qjj + 1 ) / b_A[0]), 1, b_A, qjj + 1 );
372 e[kase - 1] = b_A[qjj];
375 for( qp1 = 1; qp1 < 4; qp1++ )
377 U[qp1 - 1] = b_A[qp1 - 1];
380 nrm = b_xnrm2( 2, e, 2 );
397 if( std::abs( e[0] ) >= 1.0020841800044864E-292 )
400 for( qp1 = 2; qp1 < 4; qp1++ )
407 for( qp1 = 2; qp1 < 4; qp1++ )
415 for( qp1 = 2; qp1 < 4; qp1++ )
420 for( kase = 2; kase < 4; kase++ )
422 b_xaxpy( 2, e[kase - 1], b_A, 3 * (kase - 1) + 2, work, 2 );
425 for( kase = 2; kase < 4; kase++ )
427 c_xaxpy( 2, -e[kase - 1] / e[1], work, 2, b_A, 3 * (kase - 1) + 2 );
431 for( qp1 = 2; qp1 < 4; qp1++ )
433 Vf[qp1 - 1] = e[qp1 - 1];
436 apply_transform =
false;
437 nrm = xnrm2( 2, b_A, 5 );
440 apply_transform =
true;
446 if( std::abs( nrm ) >= 1.0020841800044864E-292 )
449 for( qp1 = 5; qp1 < 7; qp1++ )
456 for( qp1 = 5; qp1 < 7; qp1++ )
470 for( kase = 3; kase < 4; kase++ )
472 if( apply_transform )
474 xaxpy( 2, -(xdotc( 2, b_A, 5, b_A, 8 ) / b_A[4]), 5, b_A, 8 );
478 for( qp1 = 2; qp1 < 4; qp1++ )
480 U[qp1 + 2] = b_A[qp1 + 2];
490 for( q = 1; q >= 0; q-- )
496 for( kase = qp1; kase < 4; kase++ )
498 qjj = (q + 3 * (kase - 1)) + 1;
499 xaxpy( 3 - q, -(xdotc( 3 - q, U, qq + 1, U, qjj ) / U[qq]), qq + 1, U, qjj );
502 for( qp1 = q + 1; qp1 < 4; qp1++ )
504 kase = (qp1 + 3 *
q) - 1;
523 for( q = 2; q >= 0; q-- )
525 if( (q + 1 <= 1) && (e[0] != 0.0) )
527 xaxpy( 2, -(xdotc( 2, Vf, 2, Vf, 5 ) / Vf[1]), 2, Vf, 5 );
528 xaxpy( 2, -(xdotc( 2, Vf, 2, Vf, 8 ) / Vf[1]), 2, Vf, 8 );
539 for( q = 0; q < 3; q++ )
543 nrm = std::abs( b_s[q] );
553 for( qp1 = kase + 1; qp1 <= qjj; qp1++ )
559 if( (q + 1 < 3) && (e[q] != 0.0) )
561 nrm = std::abs( e[q] );
567 for( qp1 = kase + 1; qp1 <= qjj; qp1++ )
573 nrm = std::abs( b_s[q] );
574 r = std::abs( e[q] );
575 if( (nrm > r) ||
rtIsNaN( r ) )
580 if( (!(snorm > r)) && (!
rtIsNaN( r )) )
586 while( (m + 2 > 0) && (qq < 75) )
599 nrm = std::abs( e[qp1] );
600 if( (nrm <= 2.2204460492503131E-16
601 * (std::abs( b_s[qp1] ) + std::abs( b_s[qp1 + 1] )))
602 || (nrm <= 1.0020841800044864E-292)
603 || ((qq > 20) && (nrm <= 2.2204460492503131E-16 * snorm)) )
613 }
while( exitg1 == 0 );
615 if( qp1 + 1 == m + 1 )
624 while( (!exitg2) && (kase >= qp1 + 1) )
627 if( kase == qp1 + 1 )
636 nrm = std::abs( e[kase - 1] );
641 nrm += std::abs( e[kase - 2] );
644 r = std::abs( b_s[kase - 1] );
645 if( (r <= 2.2204460492503131E-16 * nrm) || (r <= 1.0020841800044864E-292) )
661 else if( qjj == m + 2 )
678 for( qp1 = qjj; qp1 >= q + 1; qp1-- )
680 xrotg( &b_s[qp1 - 1], &r, &sm, &sqds );
687 xrot( Vf, 1 + 3 * (qp1 - 1), 1 + 3 * (m + 1), sm, sqds );
694 for( qp1 = q + 1; qp1 <= m + 2; qp1++ )
696 xrotg( &b_s[qp1 - 1], &r, &sm, &sqds );
700 xrot( U, 1 + 3 * (qp1 - 1), 1 + 3 * (q - 1), sm, sqds );
706 scale = std::abs( b_s[m + 1] );
707 r = std::abs( b_s[m] );
708 if( (!(scale > r)) && (!
rtIsNaN( r )) )
713 r = std::abs( e[m] );
714 if( (!(scale > r)) && (!
rtIsNaN( r )) )
719 r = std::abs( b_s[q] );
720 if( (!(scale > r)) && (!
rtIsNaN( r )) )
725 r = std::abs( e[q] );
726 if( (!(scale > r)) && (!
rtIsNaN( r )) )
731 sm = b_s[m + 1] /
scale;
735 b = ((nrm + sm) * (nrm - sm) + r *
r) / 2.0;
738 if( (b != 0.0) || (nrm != 0.0) )
754 r += (sqds + sm) * (sqds - sm);
755 nrm = sqds * (e[
q] /
scale);
756 for( qp1 = q + 1; qp1 <= kase; qp1++ )
758 xrotg( &r, &nrm, &sm, &sqds );
766 e[qp1 - 1] = sm * b - sqds * nrm;
769 xrot( Vf, 1 + 3 * (qp1 - 1), 1 + 3 * qp1, sm, sqds );
770 b_s[qp1 - 1] = sm * nrm + sqds *
b;
771 xrotg( &b_s[qp1 - 1], &r, &sm, &sqds );
773 r = sm * b + sqds * b_s[qp1];
774 b_s[qp1] = -sqds * b + sm * b_s[qp1];
777 xrot( U, 1 + 3 * (qp1 - 1), 1 + 3 * qp1, sm, sqds );
790 for( qp1 = kase + 1; qp1 <= qjj; qp1++ )
792 Vf[qp1 - 1] = -Vf[qp1 - 1];
797 while( (q + 1 < 3) && (b_s[q] < b_s[qp1]) )
802 xswap( Vf, 1 + 3 * q, 1 + 3 * (q + 1) );
803 xswap( U, 1 + 3 * q, 1 + 3 * (q + 1) );
814 for( qp1 = 0; qp1 < 3; qp1++ )
817 V[3 * qp1] = Vf[3 * qp1];
845 for( br = 0; br < 9; br++ )
856 for( i0 = 0; i0 < 9; i0++ )
857 n[i0] = std::numeric_limits< double >::quiet_NaN();
861 svd_3x3( m, U, s, V );
862 absxk = std::abs( s[0] );
865 if( absxk <= 2.2250738585072014E-308 )
867 absxk = 4.94065645841247E-324;
871 frexp( absxk, &vcol );
872 absxk = std::ldexp( 1.0, vcol - 53 );
877 absxk = std::numeric_limits< double >::quiet_NaN();
883 while( (br < 3) && (s[br] > absxk) )
892 for( j = 0; j <=
r; j++ )
896 for( br = vcol; br <= i0; br++ )
904 for( vcol = 0; vcol <= 6; vcol += 3 )
912 (
unsigned int)(((j - i0) + 1)
913 * static_cast<int>(
sizeof(
double ))) );
918 for( vcol = 0; vcol <= 6; vcol += 3 )
923 for( ib = br; ib <= i0; ib += 3 )
928 for( ic = j; ic <=
i1; ic++ )
931 n[ic - 1] += U[ib - 1] * V[ia];
GLboolean GLboolean GLboolean b
GLenum GLenum GLenum GLenum GLenum scale
static bool rtIsNaN(double value)
static bool rtIsInf(double value)
GLboolean GLboolean GLboolean GLboolean a
GLdouble GLdouble GLdouble q
void pinv_3x3(const double in[9], double out[9])