1 /* -----------------------------------------------------------------------------
3 Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 -------------------------------------------------------------------------- */
28 The symmetric eigensystem solver algorithm is from
29 http://www.geometrictools.com/Documentation/EigenSymmetric3x3.pdf
37 Sym3x3
ComputeWeightedCovariance( int n
, Vec3
const* points
, float const* weights
)
39 // compute the centroid
41 Vec3
centroid( 0.0f
);
42 for( int i
= 0; i
< n
; ++i
)
45 centroid
+= weights
[i
]*points
[i
];
49 // accumulate the covariance matrix
50 Sym3x3
covariance( 0.0f
);
51 for( int i
= 0; i
< n
; ++i
)
53 Vec3 a
= points
[i
] - centroid
;
54 Vec3 b
= weights
[i
]*a
;
56 covariance
[0] += a
.X()*b
.X();
57 covariance
[1] += a
.X()*b
.Y();
58 covariance
[2] += a
.X()*b
.Z();
59 covariance
[3] += a
.Y()*b
.Y();
60 covariance
[4] += a
.Y()*b
.Z();
61 covariance
[5] += a
.Z()*b
.Z();
68 static Vec3
GetMultiplicity1Evector( Sym3x3
const& matrix
, float evalue
)
72 m
[0] = matrix
[0] - evalue
;
75 m
[3] = matrix
[3] - evalue
;
77 m
[5] = matrix
[5] - evalue
;
81 u
[0] = m
[3]*m
[5] - m
[4]*m
[4];
82 u
[1] = m
[2]*m
[4] - m
[1]*m
[5];
83 u
[2] = m
[1]*m
[4] - m
[2]*m
[3];
84 u
[3] = m
[0]*m
[5] - m
[2]*m
[2];
85 u
[4] = m
[1]*m
[2] - m
[4]*m
[0];
86 u
[5] = m
[0]*m
[3] - m
[1]*m
[1];
88 // find the largest component
89 float mc
= std::fabs( u
[0] );
91 for( int i
= 1; i
< 6; ++i
)
93 float c
= std::fabs( u
[i
] );
101 // pick the column with this component
105 return Vec3( u
[0], u
[1], u
[2] );
109 return Vec3( u
[1], u
[3], u
[4] );
112 return Vec3( u
[2], u
[4], u
[5] );
116 static Vec3
GetMultiplicity2Evector( Sym3x3
const& matrix
, float evalue
)
120 m
[0] = matrix
[0] - evalue
;
123 m
[3] = matrix
[3] - evalue
;
125 m
[5] = matrix
[5] - evalue
;
127 // find the largest component
128 float mc
= std::fabs( m
[0] );
130 for( int i
= 1; i
< 6; ++i
)
132 float c
= std::fabs( m
[i
] );
140 // pick the first eigenvector based on this index
145 return Vec3( -m
[1], m
[0], 0.0f
);
148 return Vec3( m
[2], 0.0f
, -m
[0] );
152 return Vec3( 0.0f
, -m
[4], m
[3] );
155 return Vec3( 0.0f
, -m
[5], m
[4] );
159 Vec3
ComputePrincipleComponent( Sym3x3
const& matrix
)
161 // compute the cubic coefficients
162 float c0
= matrix
[0]*matrix
[3]*matrix
[5]
163 + 2.0f
*matrix
[1]*matrix
[2]*matrix
[4]
164 - matrix
[0]*matrix
[4]*matrix
[4]
165 - matrix
[3]*matrix
[2]*matrix
[2]
166 - matrix
[5]*matrix
[1]*matrix
[1];
167 float c1
= matrix
[0]*matrix
[3] + matrix
[0]*matrix
[5] + matrix
[3]*matrix
[5]
168 - matrix
[1]*matrix
[1] - matrix
[2]*matrix
[2] - matrix
[4]*matrix
[4];
169 float c2
= matrix
[0] + matrix
[3] + matrix
[5];
171 // compute the quadratic coefficients
172 float a
= c1
- ( 1.0f
/3.0f
)*c2
*c2
;
173 float b
= ( -2.0f
/27.0f
)*c2
*c2
*c2
+ ( 1.0f
/3.0f
)*c1
*c2
- c0
;
175 // compute the root count check
176 float Q
= 0.25f
*b
*b
+ ( 1.0f
/27.0f
)*a
*a
*a
;
178 // test the multiplicity
179 if( FLT_EPSILON
< Q
)
181 // only one root, which implies we have a multiple of the identity
184 else if( Q
< -FLT_EPSILON
)
186 // three distinct roots
187 float theta
= std::atan2( std::sqrt( -Q
), -0.5f
*b
);
188 float rho
= std::sqrt( 0.25f
*b
*b
- Q
);
190 float rt
= std::pow( rho
, 1.0f
/3.0f
);
191 float ct
= std::cos( theta
/3.0f
);
192 float st
= std::sin( theta
/3.0f
);
194 float l1
= ( 1.0f
/3.0f
)*c2
+ 2.0f
*rt
*ct
;
195 float l2
= ( 1.0f
/3.0f
)*c2
- rt
*( ct
+ float(sqrt( 3.0f
))*st
);
196 float l3
= ( 1.0f
/3.0f
)*c2
- rt
*( ct
- float(sqrt( 3.0f
))*st
);
199 if( std::fabs( l2
) > std::fabs( l1
) )
201 if( std::fabs( l3
) > std::fabs( l1
) )
204 // get the eigenvector
205 return GetMultiplicity1Evector( matrix
, l1
);
207 else // if( -FLT_EPSILON <= Q && Q <= FLT_EPSILON )
212 rt
= -std::pow( -0.5f
*b
, 1.0f
/3.0f
);
214 rt
= std::pow( 0.5f
*b
, 1.0f
/3.0f
);
216 float l1
= ( 1.0f
/3.0f
)*c2
+ rt
; // repeated
217 float l2
= ( 1.0f
/3.0f
)*c2
- 2.0f
*rt
;
219 // get the eigenvector
220 if( std::fabs( l1
) > std::fabs( l2
) )
221 return GetMultiplicity2Evector( matrix
, l1
);
223 return GetMultiplicity1Evector( matrix
, l2
);
227 } // namespace squish