From 9ae9ec197576b2122727ea35441a5ec428a4a8c1 Mon Sep 17 00:00:00 2001 From: castano Date: Mon, 31 May 2010 07:40:08 +0000 Subject: [PATCH] Fix power method initial estimate as in squish. --- src/nvmath/Fitting.cpp | 24 +++++++++++++++++++++--- src/nvmath/Fitting.h | 8 ++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/nvmath/Fitting.cpp b/src/nvmath/Fitting.cpp index c359635..1e86557 100644 --- a/src/nvmath/Fitting.cpp +++ b/src/nvmath/Fitting.cpp @@ -1,4 +1,4 @@ -// This code is in the public domain -- icastano@gmail.com +// This code is in the public domain -- Ignacio Castaño #include "Fitting.h" #include "nvcore/Utils.h" // max, swap @@ -8,6 +8,24 @@ using namespace nv; // @@ Move to EigenSolver.h + +// @@ We should be able to do something cheaper... +static Vector3 estimatePrincipleComponent(const float * __restrict matrix) +{ + const Vector3 row0(matrix[0], matrix[1], matrix[2]); + const Vector3 row1(matrix[1], matrix[3], matrix[4]); + const Vector3 row2(matrix[2], matrix[4], matrix[5]); + + float r0 = lengthSquared(row0); + float r1 = lengthSquared(row1); + float r2 = lengthSquared(row2); + + if (r0 > r1 && r0 > r2) return row0; + if (r1 > r2) return row1; + return row2; +} + + static inline Vector3 firstEigenVector_PowerMethod(const float *__restrict matrix) { if (matrix[0] == 0 && matrix[3] == 0 && matrix[5] == 0) @@ -15,9 +33,9 @@ static inline Vector3 firstEigenVector_PowerMethod(const float *__restrict matri return Vector3(zero); } - const int NUM = 8; + Vector3 v = estimatePrincipleComponent(matrix); - Vector3 v(1, 1, 1); + const int NUM = 8; for (int i = 0; i < NUM; i++) { float x = v.x * matrix[0] + v.y * matrix[1] + v.z * matrix[2]; diff --git a/src/nvmath/Fitting.h b/src/nvmath/Fitting.h index 7ad6ed6..6085ea9 100644 --- a/src/nvmath/Fitting.h +++ b/src/nvmath/Fitting.h @@ -1,12 +1,12 @@ -// This code is in the public domain -- icastano@gmail.com +// This code is in the public domain -- Ignacio Castaño #pragma once #ifndef NV_MATH_FITTING_H #define NV_MATH_FITTING_H -#include -#include -#include +#include "nvmath/nvmath.h" +#include "nvmath/Vector.h" +#include "nvmath/Plane.h" namespace nv {