The discussed changes to Hyperplane, the ParametrizedLine class, and the

API update in Regression...
This commit is contained in:
Benoit Jacob
2008-08-31 04:25:30 +00:00
parent 5c8c09e021
commit 5c34d8e20a
8 changed files with 255 additions and 119 deletions

View File

@@ -26,16 +26,16 @@
#include <Eigen/Geometry>
#include <Eigen/LU>
template<typename PlaneType> void hyperplane(const PlaneType& _plane)
template<typename HyperplaneType> void hyperplane(const HyperplaneType& _plane)
{
/* this test covers the following files:
Hyperplane.h
*/
const int dim = _plane.dim();
typedef typename PlaneType::Scalar Scalar;
typedef typename HyperplaneType::Scalar Scalar;
typedef typename NumTraits<Scalar>::Real RealScalar;
typedef Matrix<Scalar, PlaneType::DimAtCompileTime, 1> VectorType;
typedef Matrix<Scalar, HyperplaneType::AmbientDimAtCompileTime, 1> VectorType;
VectorType p0 = VectorType::Random(dim);
VectorType p1 = VectorType::Random(dim);
@@ -43,8 +43,8 @@ template<typename PlaneType> void hyperplane(const PlaneType& _plane)
VectorType n0 = VectorType::Random(dim).normalized();
VectorType n1 = VectorType::Random(dim).normalized();
PlaneType pl0(n0, p0);
PlaneType pl1(n1, p1);
HyperplaneType pl0(n0, p0);
HyperplaneType pl1(n1, p1);
Scalar s0 = ei_random<Scalar>();
Scalar s1 = ei_random<Scalar>();
@@ -52,11 +52,43 @@ template<typename PlaneType> void hyperplane(const PlaneType& _plane)
VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
VERIFY_IS_APPROX( n1.dot(n1), Scalar(1) );
VERIFY_IS_MUCH_SMALLER_THAN( pl0.distanceTo(p0), Scalar(1) );
VERIFY_IS_APPROX( pl1.distanceTo(p1 + n1 * s0), s0 );
VERIFY_IS_MUCH_SMALLER_THAN( pl1.distanceTo(pl1.project(p0)), Scalar(1) );
VERIFY_IS_MUCH_SMALLER_THAN( pl1.distanceTo(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
VERIFY_IS_MUCH_SMALLER_THAN( pl0.absDistance(p0), Scalar(1) );
VERIFY_IS_APPROX( pl1.signedDistance(p1 + n1 * s0), s0 );
VERIFY_IS_MUCH_SMALLER_THAN( pl1.signedDistance(pl1.projection(p0)), Scalar(1) );
VERIFY_IS_MUCH_SMALLER_THAN( pl1.absDistance(p1 + pl1.normal().unitOrthogonal() * s1), Scalar(1) );
}
template<typename Scalar> void lines()
{
typedef Hyperplane<Scalar, 2> HLine;
typedef ParametrizedLine<Scalar, 2> PLine;
typedef Matrix<Scalar,2,1> Vector;
typedef Matrix<Scalar,3,1> CoeffsType;
for(int i = 0; i < 10; i++)
{
Vector center = Vector::Random();
Vector u = Vector::Random();
Vector v = Vector::Random();
Scalar a = ei_random<Scalar>();
HLine line_u = HLine::Through(center + u, center + a*u);
HLine line_v = HLine::Through(center + v, center + a*v);
// the line equations should be normalized so that a^2+b^2=1
VERIFY_IS_APPROX(line_u.normal().norm(), Scalar(1));
VERIFY_IS_APPROX(line_v.normal().norm(), Scalar(1));
Vector result = line_u.intersection(line_v);
// the lines should intersect at the point we called "center"
VERIFY_IS_APPROX(result, center);
// check conversions between two types of lines
CoeffsType converted_coeffs(HLine(PLine(line_u)).coeffs());
converted_coeffs *= line_u.coeffs()(0)/converted_coeffs(0);
VERIFY(line_u.coeffs().isApprox(converted_coeffs));
}
}
void test_hyperplane()
@@ -66,6 +98,7 @@ void test_hyperplane()
CALL_SUBTEST( hyperplane(Hyperplane<float,3>()) );
CALL_SUBTEST( hyperplane(Hyperplane<double,4>()) );
CALL_SUBTEST( hyperplane(Hyperplane<std::complex<double>,5>()) );
CALL_SUBTEST( hyperplane(Hyperplane<double,Dynamic>(13)) );
CALL_SUBTEST( lines<float>() );
CALL_SUBTEST( lines<double>() );
}
}

View File

@@ -26,21 +26,21 @@
#include <Eigen/Regression>
template<typename VectorType,
typename BigVecType>
typename HyperplaneType>
void makeNoisyCohyperplanarPoints(int numPoints,
VectorType **points,
BigVecType *coeffs,
HyperplaneType *hyperplane,
typename VectorType::Scalar noiseAmplitude )
{
typedef typename VectorType::Scalar Scalar;
const int size = points[0]->size();
// pick a random hyperplane, store the coefficients of its equation
coeffs->resize(size + 1);
hyperplane->coeffs().resize(size + 1);
for(int j = 0; j < size + 1; j++)
{
do {
coeffs->coeffRef(j) = ei_random<Scalar>();
} while(ei_abs(coeffs->coeffRef(j)) < 0.5);
hyperplane->coeffs().coeffRef(j) = ei_random<Scalar>();
} while(ei_abs(hyperplane->coeffs().coeff(j)) < 0.5);
}
// now pick numPoints random points on this hyperplane
@@ -51,8 +51,8 @@ void makeNoisyCohyperplanarPoints(int numPoints,
{
cur_point = VectorType::Random(size)/*.normalized()*/;
// project cur_point onto the hyperplane
Scalar x = - (coeffs->start(size).cwise()*cur_point).sum();
cur_point *= coeffs->coeff(size) / x;
Scalar x = - (hyperplane->coeffs().start(size).cwise()*cur_point).sum();
cur_point *= hyperplane->coeffs().coeff(size) / x;
} while( ei_abs(cur_point.norm()) < 0.5
|| ei_abs(cur_point.norm()) > 2.0 );
}
@@ -63,18 +63,17 @@ void makeNoisyCohyperplanarPoints(int numPoints,
}
template<typename VectorType,
typename BigVecType>
typename HyperplaneType>
void check_fitHyperplane(int numPoints,
VectorType **points,
BigVecType *coeffs,
const HyperplaneType& original,
typename VectorType::Scalar tolerance)
{
int size = points[0]->size();
BigVecType result(size + 1);
HyperplaneType result(size);
fitHyperplane(numPoints, points, &result);
result /= result.coeff(size);
result *= coeffs->coeff(size);
typename VectorType::Scalar error = (result - *coeffs).norm() / coeffs->norm();
result.coeffs() *= original.coeffs().coeff(size)/result.coeffs().coeff(size);
typename VectorType::Scalar error = (result.coeffs() - original.coeffs()).norm() / original.coeffs().norm();
VERIFY(ei_abs(error) < ei_abs(tolerance));
}
@@ -86,31 +85,33 @@ void test_regression()
Vector2f points2f [1000];
Vector2f *points2f_ptrs [1000];
for(int i = 0; i < 1000; i++) points2f_ptrs[i] = &(points2f[i]);
Vector3f coeffs3f;
Hyperplane<float,2> coeffs3f;
makeNoisyCohyperplanarPoints(1000, points2f_ptrs, &coeffs3f, 0.01f);
CALL_SUBTEST(check_fitHyperplane(10, points2f_ptrs, &coeffs3f, 0.05f));
CALL_SUBTEST(check_fitHyperplane(100, points2f_ptrs, &coeffs3f, 0.01f));
CALL_SUBTEST(check_fitHyperplane(1000, points2f_ptrs, &coeffs3f, 0.002f));
CALL_SUBTEST(check_fitHyperplane(10, points2f_ptrs, coeffs3f, 0.05f));
CALL_SUBTEST(check_fitHyperplane(100, points2f_ptrs, coeffs3f, 0.01f));
CALL_SUBTEST(check_fitHyperplane(1000, points2f_ptrs, coeffs3f, 0.002f));
}
{
Vector4d points4d [1000];
Vector4d *points4d_ptrs [1000];
for(int i = 0; i < 1000; i++) points4d_ptrs[i] = &(points4d[i]);
Matrix<double,5,1> coeffs5d;
Hyperplane<float,4> coeffs5d;
makeNoisyCohyperplanarPoints(1000, points4d_ptrs, &coeffs5d, 0.01);
CALL_SUBTEST(check_fitHyperplane(10, points4d_ptrs, &coeffs5d, 0.05));
CALL_SUBTEST(check_fitHyperplane(100, points4d_ptrs, &coeffs5d, 0.01));
CALL_SUBTEST(check_fitHyperplane(1000, points4d_ptrs, &coeffs5d, 0.002));
CALL_SUBTEST(check_fitHyperplane(10, points4d_ptrs, coeffs5d, 0.05));
CALL_SUBTEST(check_fitHyperplane(100, points4d_ptrs, coeffs5d, 0.01));
CALL_SUBTEST(check_fitHyperplane(1000, points4d_ptrs, coeffs5d, 0.002));
}
{
VectorXcd *points11cd_ptrs[1000];
for(int i = 0; i < 1000; i++) points11cd_ptrs[i] = new VectorXcd(11);
VectorXcd *coeffs12cd = new VectorXcd(12);
Hyperplane<std::complex<double>,Dynamic> *coeffs12cd = new Hyperplane<std::complex<double>,Dynamic>(11);
makeNoisyCohyperplanarPoints(1000, points11cd_ptrs, coeffs12cd, 0.01);
CALL_SUBTEST(check_fitHyperplane(100, points11cd_ptrs, coeffs12cd, 0.025));
CALL_SUBTEST(check_fitHyperplane(1000, points11cd_ptrs, coeffs12cd, 0.006));
CALL_SUBTEST(check_fitHyperplane(100, points11cd_ptrs, *coeffs12cd, 0.025));
CALL_SUBTEST(check_fitHyperplane(1000, points11cd_ptrs, *coeffs12cd, 0.006));
delete coeffs12cd;
for(int i = 0; i < 1000; i++) delete points11cd_ptrs[i];
}
}
}

View File

@@ -128,7 +128,6 @@ template<typename MatrixType> void submatrices(const MatrixType& m)
VERIFY(ones.row(r1).sum() == Scalar(cols));
VERIFY(ones.col(c1).dot(ones.col(c2)) == Scalar(rows));
std::cerr << ones.row(r1).dot(ones.row(r2)) << " == " << cols << "\n";
VERIFY(ones.row(r1).dot(ones.row(r2)) == Scalar(cols));
}