Commit 2293abdf authored by Marcus Hanwell's avatar Marcus Hanwell
Browse files

ENH: Added tests for vtkMatrix3x3, vtkTransform2D and vtkPoints2D.

parent db1a4c3d
......@@ -15,6 +15,7 @@ CREATE_TEST_SOURCELIST(Tests ${KIT}CxxTests.cxx
TestDirectory.cxx
TestFastNumericConversion.cxx
TestMath.cxx
TestMatrix3x3.cxx
TestMinimalStandardRandomSequence.cxx
TestPolynomialSolversUnivariate.cxx
TestSmartPointer.cxx
......
#include "vtkMatrix3x3.h"
#include "vtkTransform2D.h"
#include "vtkPoints2D.h"
#include "vtkSmartPointer.h"
#include <math.h>
#include <vtkstd/limits>
// Perform a fuzzy compare of floats/doubles
template<class A>
bool fuzzyCompare(A a, A b) {
return fabs(a - b) < vtkstd::numeric_limits<A>::epsilon();
}
// Perform a fuzzy compare of floats/doubles, specify the allowed tolerance
template<class A>
bool fuzzyCompare(A a, A b, A epsilon) {
return fabs(a - b) < epsilon;
}
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
int TestMatrix3x3(int,char *[])
{
// Instantiate a vtkMatrix3x3 and test out the funtions.
VTK_CREATE(vtkMatrix3x3, matrix);
cout << "Testing vtkMatrix3x3..." << endl;
if (!matrix->IsIdentity())
{
vtkGenericWarningMacro("Matrix should be initialized to identity.");
return 1;
}
matrix->Invert();
if (!matrix->IsIdentity())
{
vtkGenericWarningMacro("Inverse of identity should be identity.");
return 1;
}
// Check copying and comparison
VTK_CREATE(vtkMatrix3x3, matrix2);
matrix2->DeepCopy(matrix);
if (*matrix != *matrix2)
{
vtkGenericWarningMacro("DeepCopy of vtkMatrix3x3 failed.");
return 1;
}
if (!(*matrix == *matrix2))
{
vtkGenericWarningMacro("Problem with vtkMatrix3x3::operator==");
return 1;
}
matrix2->SetElement(0, 0, 5.0);
if (!(*matrix != *matrix2))
{
vtkGenericWarningMacro("Problem with vtkMatrix3x3::operator!=");
return 1;
}
if (*matrix == *matrix2)
{
vtkGenericWarningMacro("Problem with vtkMatrix3x3::operator==");
return 1;
}
if (!fuzzyCompare(matrix2->GetElement(0, 0), 5.0))
{
vtkGenericWarningMacro("Value not stored in matrix properly.");
return 1;
}
matrix2->SetElement(1, 2, 42.0);
if (!fuzzyCompare(matrix2->GetElement(1, 2), 42.0))
{
vtkGenericWarningMacro("Value not stored in matrix properly.");
return 1;
}
// Test matrix transpose
matrix2->Transpose();
if (!fuzzyCompare(matrix2->GetElement(0, 0), 5.0) ||
!fuzzyCompare(matrix2->GetElement(2, 1), 42.0))
{
vtkGenericWarningMacro("vtkMatrix::Transpose failed.");
return 1;
}
matrix2->Invert();
if (!fuzzyCompare(matrix2->GetElement(0, 0), 0.2) ||
!fuzzyCompare(matrix2->GetElement(2, 1), -42.0))
{
vtkGenericWarningMacro("vtkMatrix::Transpose failed.");
return 1;
}
// Not test the 2D transform with some 2D points
VTK_CREATE(vtkTransform2D, transform);
VTK_CREATE(vtkPoints2D, points);
VTK_CREATE(vtkPoints2D, points2);
points->SetNumberOfPoints(3);
points->SetPoint(0, 0.0, 0.0);
points->SetPoint(1, 3.0, 4.9);
points->SetPoint(2, 42.0, 69.0);
transform->TransformPoints(points, points2);
for (int i = 0; i < 3; ++i)
{
double p1[2], p2[2];
points->GetPoint(i, p1);
points2->GetPoint(i, p2);
if (!fuzzyCompare(p1[0], p2[0], 1e-5) ||
!fuzzyCompare(p1[1], p2[1], 1e-5))
{
vtkGenericWarningMacro("Identity transform moved points."
<< " Delta: "
<< p1[0] - (p2[0]-2.0)
<< ", "
<< p1[1] - (p2[1]-6.9));
return 1;
}
}
transform->Translate(2.0, 6.9);
transform->TransformPoints(points, points2);
for (int i = 0; i < 3; ++i)
{
double p1[2], p2[2];
points->GetPoint(i, p1);
points2->GetPoint(i, p2);
if (!fuzzyCompare(p1[0], p2[0] - 2.0, 1e-5) ||
!fuzzyCompare(p1[1], p2[1] - 6.9, 1e-5))
{
vtkGenericWarningMacro("Translation transform failed. Delta: "
<< p1[0] - (p2[0]-2.0)
<< ", "
<< p1[1] - (p2[1]-6.9));
return 1;
}
}
transform->InverseTransformPoints(points2, points2);
for (int i = 0; i < 3; ++i)
{
double p1[2], p2[2];
points->GetPoint(i, p1);
points2->GetPoint(i, p2);
if (!fuzzyCompare(p1[0], p2[0], 1e-5) ||
!fuzzyCompare(p1[1], p2[1], 1e-5))
{
vtkGenericWarningMacro("Inverse transform did not return original points."
<< " Delta: "
<< p1[0] - (p2[0]-2.0)
<< ", "
<< p1[1] - (p2[1]-6.9));
return 1;
}
}
return 0;
}
......@@ -152,6 +152,8 @@ public:
{return &(this->Element[i][0]);}
const double *operator[](unsigned int i) const
{ return &(this->Element[i][0]); }
bool operator==(const vtkMatrix3x3&);
bool operator!=(const vtkMatrix3x3&);
void Adjoint(vtkMatrix3x3 &in,vtkMatrix3x3 &out)
{this->Adjoint(&in,&out);}
double Determinant(vtkMatrix3x3 &in)
......@@ -213,4 +215,34 @@ inline bool vtkMatrix3x3::IsIdentity()
}
}
inline bool vtkMatrix3x3::operator==(const vtkMatrix3x3 &other)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (Element[i][j] != other.Element[i][j])
{
return false;
}
}
}
return true;
}
inline bool vtkMatrix3x3::operator!=(const vtkMatrix3x3 &other)
{
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (Element[i][j] != other.Element[i][j])
{
return true;
}
}
}
return false;
}
#endif
......@@ -19,7 +19,7 @@
#include <stdlib.h>
vtkCxxRevisionMacro(vtkTransform2D, "1.1");
vtkCxxRevisionMacro(vtkTransform2D, "1.2");
vtkStandardNewMacro(vtkTransform2D);
//----------------------------------------------------------------------------
......@@ -174,6 +174,8 @@ void vtkTransform2D::GetTranspose(vtkMatrix3x3 *transpose)
}
//----------------------------------------------------------------------------
namespace { // Anonmymous namespace
template <class T1, class T2, class T3>
inline double vtkHomogeneousTransformPoint2D(T1 M[9],
T2 in[2], T3 out[2])
......@@ -189,6 +191,8 @@ inline double vtkHomogeneousTransformPoint2D(T1 M[9],
return f;
}
} // End anonymous namespace
//----------------------------------------------------------------------------
void vtkTransform2D::TransformPoints(float *inPts, float *outPts, int n)
{
......@@ -223,7 +227,7 @@ void vtkTransform2D::TransformPoints(vtkPoints2D *inPts, vtkPoints2D *outPts)
{
inPts->GetPoint(i,point);
vtkHomogeneousTransformPoint2D(M,point,point);
outPts->InsertPoint(i, point);
outPts->SetPoint(i, point);
}
}
......@@ -273,6 +277,6 @@ void vtkTransform2D::InverseTransformPoints(vtkPoints2D *inPts, vtkPoints2D *out
{
inPts->GetPoint(i,point);
vtkHomogeneousTransformPoint2D(M,point,point);
outPts->InsertPoint(i, point);
outPts->SetPoint(i, point);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment