File: TestMatrix3x3.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (157 lines) | stat: -rw-r--r-- 4,417 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#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;
}