87 lines
2.6 KiB
C++
87 lines
2.6 KiB
C++
/* -----------------------------------------------------------------------------
|
|
|
|
Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
a copy of this software and associated documentation files (the
|
|
"Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
-------------------------------------------------------------------------- */
|
|
|
|
#include "maths.h"
|
|
#include <cfloat>
|
|
|
|
namespace squish {
|
|
|
|
Sym3x3 ComputeWeightedCovariance( int n, Vec3 const* points, float const* weights, Vec3::Arg metric )
|
|
{
|
|
// compute the centroid
|
|
float total = 0.0f;
|
|
Vec3 centroid( 0.0f );
|
|
for( int i = 0; i < n; ++i )
|
|
{
|
|
total += weights[i];
|
|
centroid += weights[i]*points[i];
|
|
}
|
|
centroid /= total;
|
|
|
|
// accumulate the covariance matrix
|
|
Sym3x3 covariance( 0.0f );
|
|
for( int i = 0; i < n; ++i )
|
|
{
|
|
Vec3 a = (points[i] - centroid) * metric;
|
|
Vec3 b = weights[i]*a;
|
|
|
|
covariance[0] += a.X()*b.X();
|
|
covariance[1] += a.X()*b.Y();
|
|
covariance[2] += a.X()*b.Z();
|
|
covariance[3] += a.Y()*b.Y();
|
|
covariance[4] += a.Y()*b.Z();
|
|
covariance[5] += a.Z()*b.Z();
|
|
}
|
|
|
|
// return it
|
|
return covariance;
|
|
}
|
|
|
|
|
|
Vec3 ComputePrincipleComponent( Sym3x3 const& matrix )
|
|
{
|
|
const int NUM = 8;
|
|
|
|
Vec3 v(1, 1, 1);
|
|
for(int i = 0; i < NUM; i++) {
|
|
float x = v.X() * matrix[0] + v.Y() * matrix[1] + v.Z() * matrix[2];
|
|
float y = v.X() * matrix[1] + v.Y() * matrix[3] + v.Z() * matrix[4];
|
|
float z = v.X() * matrix[2] + v.Y() * matrix[4] + v.Z() * matrix[5];
|
|
|
|
float norm = std::max(std::max(x, y), z);
|
|
float iv = 1.0f / norm;
|
|
if (norm == 0.0f) { // @@ I think this is not necessary in this case!!
|
|
return Vec3(0.0f);
|
|
}
|
|
|
|
v = Vec3(x*iv, y*iv, z*iv);
|
|
}
|
|
|
|
return v;
|
|
}
|
|
|
|
|
|
} // namespace squish
|