File: vtkHomogeneousTransform.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (227 lines) | stat: -rw-r--r-- 7,864 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkHomogeneousTransform.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkHomogeneousTransform.h"

#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkPoints.h"

VTK_ABI_NAMESPACE_BEGIN
namespace
{
void TransformVector(double M[4][4], double* outPnt, double f, double* inVec, double* outVec)
{
  // do the linear homogeneous transformation
  outVec[0] = M[0][0] * inVec[0] + M[0][1] * inVec[1] + M[0][2] * inVec[2];
  outVec[1] = M[1][0] * inVec[0] + M[1][1] * inVec[1] + M[1][2] * inVec[2];
  outVec[2] = M[2][0] * inVec[0] + M[2][1] * inVec[1] + M[2][2] * inVec[2];
  double w = M[3][0] * inVec[0] + M[3][1] * inVec[1] + M[3][2] * inVec[2];

  // apply homogeneous correction: note that the f we are using
  // is the one we calculated in the point transformation
  outVec[0] = (outVec[0] - w * outPnt[0]) * f;
  outVec[1] = (outVec[1] - w * outPnt[1]) * f;
  outVec[2] = (outVec[2] - w * outPnt[2]) * f;
}
}
//------------------------------------------------------------------------------
vtkHomogeneousTransform::vtkHomogeneousTransform()
{
  this->Matrix = vtkMatrix4x4::New();
}

//------------------------------------------------------------------------------
vtkHomogeneousTransform::~vtkHomogeneousTransform()
{
  if (this->Matrix)
  {
    this->Matrix->Delete();
  }
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Matrix: (" << this->Matrix << ")\n";
  if (this->Matrix)
  {
    this->Matrix->PrintSelf(os, indent.GetNextIndent());
  }
}

//------------------------------------------------------------------------------
template <class T1, class T2, class T3>
inline double vtkHomogeneousTransformPoint(T1 M[4][4], T2 in[3], T3 out[3])
{
  double x = M[0][0] * in[0] + M[0][1] * in[1] + M[0][2] * in[2] + M[0][3];
  double y = M[1][0] * in[0] + M[1][1] * in[1] + M[1][2] * in[2] + M[1][3];
  double z = M[2][0] * in[0] + M[2][1] * in[1] + M[2][2] * in[2] + M[2][3];
  double w = M[3][0] * in[0] + M[3][1] * in[1] + M[3][2] * in[2] + M[3][3];

  double f = 1.0 / w;
  out[0] = static_cast<T3>(x * f);
  out[1] = static_cast<T3>(y * f);
  out[2] = static_cast<T3>(z * f);

  return f;
}

//------------------------------------------------------------------------------
// computes a coordinate transformation and also returns the Jacobian matrix
template <class T1, class T2, class T3, class T4>
inline void vtkHomogeneousTransformDerivative(T1 M[4][4], T2 in[3], T3 out[3], T4 derivative[3][3])
{
  double f = vtkHomogeneousTransformPoint(M, in, out);

  for (int i = 0; i < 3; i++)
  {
    derivative[0][i] = static_cast<T4>((M[0][i] - M[3][i] * out[0]) * f);
    derivative[1][i] = static_cast<T4>((M[1][i] - M[3][i] * out[1]) * f);
    derivative[2][i] = static_cast<T4>((M[2][i] - M[3][i] * out[2]) * f);
  }
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::InternalTransformPoint(const float in[3], float out[3])
{
  vtkHomogeneousTransformPoint(this->Matrix->Element, in, out);
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::InternalTransformPoint(const double in[3], double out[3])
{
  vtkHomogeneousTransformPoint(this->Matrix->Element, in, out);
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::InternalTransformDerivative(
  const float in[3], float out[3], float derivative[3][3])
{
  vtkHomogeneousTransformDerivative(this->Matrix->Element, in, out, derivative);
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::InternalTransformDerivative(
  const double in[3], double out[3], double derivative[3][3])
{
  vtkHomogeneousTransformDerivative(this->Matrix->Element, in, out, derivative);
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::TransformPoints(vtkPoints* inPts, vtkPoints* outPts)
{
  vtkIdType n = inPts->GetNumberOfPoints();
  double(*M)[4] = this->Matrix->Element;
  double point[3];

  this->Update();

  for (int i = 0; i < n; i++)
  {
    inPts->GetPoint(i, point);

    vtkHomogeneousTransformPoint(M, point, point);

    outPts->InsertNextPoint(point);
  }
}

//------------------------------------------------------------------------------
// Transform the normals and vectors using the derivative of the
// transformation.  Either inNms or inVrs can be set to nullptr.
// Normals are multiplied by the inverse transpose of the transform
// derivative, while vectors are simply multiplied by the derivative.
// Note that the derivative of the inverse transform is simply the
// inverse of the derivative of the forward transform.
void vtkHomogeneousTransform::TransformPointsNormalsVectors(vtkPoints* inPts, vtkPoints* outPts,
  vtkDataArray* inNms, vtkDataArray* outNms, vtkDataArray* inVrs, vtkDataArray* outVrs,
  int nOptionalVectors, vtkDataArray** inVrsArr, vtkDataArray** outVrsArr)
{
  vtkIdType n = inPts->GetNumberOfPoints();
  double(*M)[4] = this->Matrix->Element;
  double L[4][4];
  double inPnt[3], outPnt[3], inNrm[3], outNrm[3], inVec[3], outVec[3];
  double w;

  this->Update();

  if (inNms)
  { // need inverse of the matrix to calculate normals
    vtkMatrix4x4::DeepCopy(*L, this->Matrix);
    vtkMatrix4x4::Invert(*L, *L);
    vtkMatrix4x4::Transpose(*L, *L);
  }

  for (int i = 0; i < n; i++)
  {
    inPts->GetPoint(i, inPnt);

    // do the coordinate transformation, get 1/w
    double f = vtkHomogeneousTransformPoint(M, inPnt, outPnt);
    outPts->InsertNextPoint(outPnt);

    if (inVrs)
    {
      inVrs->GetTuple(i, inVec);
      TransformVector(M, outPnt, f, inVec, outVec);
      outVrs->InsertNextTuple(outVec);
    }

    if (inVrsArr)
    {
      for (int iArr = 0; iArr < nOptionalVectors; iArr++)
      {
        inVrsArr[iArr]->GetTuple(i, inVec);
        TransformVector(M, outPnt, f, inVec, outVec);
        outVrsArr[iArr]->InsertNextTuple(outVec);
      }
    }

    if (inNms)
    {
      inNms->GetTuple(i, inNrm);

      // calculate the w component of the normal
      w = -(inNrm[0] * inPnt[0] + inNrm[1] * inPnt[1] + inNrm[2] * inPnt[2]);

      // perform the transformation in homogeneous coordinates
      outNrm[0] = L[0][0] * inNrm[0] + L[0][1] * inNrm[1] + L[0][2] * inNrm[2] + L[0][3] * w;
      outNrm[1] = L[1][0] * inNrm[0] + L[1][1] * inNrm[1] + L[1][2] * inNrm[2] + L[1][3] * w;
      outNrm[2] = L[2][0] * inNrm[0] + L[2][1] * inNrm[1] + L[2][2] * inNrm[2] + L[2][3] * w;

      // re-normalize
      vtkMath::Normalize(outNrm);
      outNms->InsertNextTuple(outNrm);
    }
  }
}

//------------------------------------------------------------------------------
// update and copy out the current matrix
void vtkHomogeneousTransform::GetMatrix(vtkMatrix4x4* m)
{
  this->Update();
  m->DeepCopy(this->Matrix);
}

//------------------------------------------------------------------------------
void vtkHomogeneousTransform::InternalDeepCopy(vtkAbstractTransform* transform)
{
  vtkHomogeneousTransform* t = static_cast<vtkHomogeneousTransform*>(transform);

  this->Matrix->DeepCopy(t->Matrix);
}
VTK_ABI_NAMESPACE_END