File: vtkImageTransform.cxx

package info (click to toggle)
vtk9 9.0.1%2Bdfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 133,688 kB
  • sloc: cpp: 1,568,287; ansic: 208,587; python: 87,847; xml: 8,022; java: 4,509; yacc: 4,027; sh: 2,515; perl: 2,183; lex: 1,766; objc: 143; makefile: 126; tcl: 59
file content (322 lines) | stat: -rw-r--r-- 9,119 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkImageTransform.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 "vtkImageTransform.h"

#include "vtkCellData.h"
#include "vtkDataArray.h"
#include "vtkImageData.h"
#include "vtkMatrix3x3.h"
#include "vtkMatrix4x4.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkSMPTools.h"

vtkStandardNewMacro(vtkImageTransform);

//============================================================================
//----------------------------------------------------------------------------
// Functors to support threaded execution
namespace
{ // anonymous

template <typename T>
struct InPlaceTranslatePoints
{
  T* Points;
  double* Translation;

  InPlaceTranslatePoints(double t[3], T* pts)
    : Points(pts)
    , Translation(t)
  {
  }

  void operator()(vtkIdType ptId, vtkIdType endPtId)
  {
    T* pIn = this->Points + 3 * ptId;
    T* pOut = pIn;

    for (; ptId < endPtId; ++ptId)
    {
      *pIn++ = *pOut++ + this->Translation[0];
      *pIn++ = *pOut++ + this->Translation[1];
      *pIn++ = *pOut++ + this->Translation[2];
    }
  }

  // Interface to vtkSMPTools
  static void Execute(double t[3], vtkIdType num, T* pts)
  {
    InPlaceTranslatePoints<T> translate(t, pts);
    vtkSMPTools::For(0, num, translate);
  }
}; // InPlaceTransformPoints

template <typename T>
struct InPlaceTransformPoints
{
  T* Points;
  vtkMatrix4x4* M4;

  InPlaceTransformPoints(vtkMatrix4x4* m4, T* pts)
    : Points(pts)
    , M4(m4)
  {
  }

  void operator()(vtkIdType ptId, vtkIdType endPtId)
  {
    T* pIn = this->Points + 3 * ptId;
    T tmp[3] = { 0, 0, 0 };

    for (; ptId < endPtId; ++ptId)
    {
      tmp[0] = M4->GetElement(0, 0) * pIn[0] + M4->GetElement(0, 1) * pIn[1] +
        M4->GetElement(0, 2) * pIn[2] + M4->GetElement(0, 3);
      tmp[1] = M4->GetElement(1, 0) * pIn[0] + M4->GetElement(1, 1) * pIn[1] +
        M4->GetElement(1, 2) * pIn[2] + M4->GetElement(1, 3);
      tmp[2] = M4->GetElement(2, 0) * pIn[0] + M4->GetElement(2, 1) * pIn[1] +
        M4->GetElement(2, 2) * pIn[2] + M4->GetElement(2, 3);
      *pIn++ = tmp[0];
      *pIn++ = tmp[1];
      *pIn++ = tmp[2];
    }
  }

  // Interface to vtkSMPTools
  static void Execute(vtkMatrix4x4* m4, vtkIdType num, T* pts)
  {
    InPlaceTransformPoints<T> transform(m4, pts);
    vtkSMPTools::For(0, num, transform);
  }
}; // InPlaceTransformPoints

template <typename T>
struct InPlaceTransformNormals
{
  T* Normals;
  vtkMatrix3x3* M3;
  double Determinant;
  double* Spacing;

  InPlaceTransformNormals(vtkMatrix3x3* m3, double* spacing, T* n)
    : Normals(n)
    , M3(m3)
    , Determinant(m3->Determinant())
    , Spacing(spacing)
  {
  }

  void operator()(vtkIdType ptId, vtkIdType endPtId)
  {
    T* nIn = this->Normals + 3 * ptId;
    T tmp[3] = { 0, 0, 0 };
    T toUnit = 0;

    for (; ptId < endPtId; ++ptId)
    {
      nIn[0] = nIn[0] / this->Spacing[0];
      nIn[1] = nIn[1] / this->Spacing[1];
      nIn[2] = nIn[2] / this->Spacing[2];
      tmp[0] = M3->GetElement(0, 0) * nIn[0] + M3->GetElement(0, 1) * nIn[1] +
        M3->GetElement(0, 2) * nIn[2];
      tmp[1] = M3->GetElement(1, 0) * nIn[0] + M3->GetElement(1, 1) * nIn[1] +
        M3->GetElement(1, 2) * nIn[2];
      tmp[2] = M3->GetElement(2, 0) * nIn[0] + M3->GetElement(2, 1) * nIn[1] +
        M3->GetElement(2, 2) * nIn[2];
      tmp[0] *= this->Determinant;
      tmp[1] *= this->Determinant;
      tmp[2] *= this->Determinant;
      toUnit = 1 / sqrt(tmp[0] * tmp[0] + tmp[1] * tmp[1] + tmp[2] * tmp[2]);
      *nIn++ = tmp[0] * toUnit;
      *nIn++ = tmp[1] * toUnit;
      *nIn++ = tmp[2] * toUnit;
    }
  }

  // Interface to vtkSMPTools
  static void Execute(vtkMatrix3x3* m3, double* spacing, vtkIdType num, T* n)
  {
    InPlaceTransformNormals<T> transform(m3, spacing, n);
    vtkSMPTools::For(0, num, transform);
  }
}; // InPlaceTransformNormals

template <typename T>
struct InPlaceTransformVectors
{
  T* Vectors;
  vtkMatrix3x3* M3;
  double* Spacing;

  InPlaceTransformVectors(vtkMatrix3x3* m3, double* spacing, T* v)
    : Vectors(v)
    , M3(m3)
    , Spacing(spacing)
  {
  }

  void operator()(vtkIdType ptId, vtkIdType endPtId)
  {
    T* nIn = this->Vectors + 3 * ptId;
    T tmp[3] = { 0, 0, 0 };

    for (; ptId < endPtId; ++ptId)
    {
      nIn[0] = nIn[0] / this->Spacing[0];
      nIn[1] = nIn[1] / this->Spacing[1];
      nIn[2] = nIn[2] / this->Spacing[2];
      tmp[0] = M3->GetElement(0, 0) * nIn[0] + M3->GetElement(0, 1) * nIn[1] +
        M3->GetElement(0, 2) * nIn[2];
      tmp[1] = M3->GetElement(1, 0) * nIn[0] + M3->GetElement(1, 1) * nIn[1] +
        M3->GetElement(1, 2) * nIn[2];
      tmp[2] = M3->GetElement(2, 0) * nIn[0] + M3->GetElement(2, 1) * nIn[1] +
        M3->GetElement(2, 2) * nIn[2];
      *nIn++ = tmp[0];
      *nIn++ = tmp[1];
      *nIn++ = tmp[2];
    }
  }

  // Interface to vtkSMPTools
  static void Execute(vtkMatrix3x3* m3, double* spacing, vtkIdType num, T* v)
  {
    InPlaceTransformVectors<T> transform(m3, spacing, v);
    vtkSMPTools::For(0, num, transform);
  }
}; // InPlaceTransformVectors

} // anonymous namespace

//============================================================================
//----------------------------------------------------------------------------
// Here is the VTK class proper.
//----------------------------------------------------------------------------
void vtkImageTransform::TransformPointSet(vtkImageData* im, vtkPointSet* ps)
{
  // Check input
  if (im == nullptr || ps == nullptr)
  {
    return;
  }

  // Nothing to do if the direction matrix is the identity
  vtkMatrix4x4* m4 = im->GetIndexToPhysicalMatrix();
  if (m4->IsIdentity())
  {
    return;
  }

  // Make sure points are available
  vtkIdType numPts = ps->GetNumberOfPoints();
  if (numPts < 1)
  {
    return;
  }

  // Grab the points-related-data and process as appropriate
  vtkDataArray* pts = ps->GetPoints()->GetData();
  vtkMatrix3x3* m3 = im->GetDirectionMatrix();
  double* ar = im->GetSpacing();

  // If there is no rotation or spacing, only translate
  if (m3->IsIdentity() && ar[0] == 1 && ar[1] == 1 && ar[2] == 1)
  {
    vtkImageTransform::TranslatePoints(im->GetOrigin(), pts);
    return;
  }

  vtkImageTransform::TransformPoints(m4, pts);

  vtkDataArray* normals = ps->GetPointData()->GetNormals();
  if (normals != nullptr)
  {
    vtkImageTransform::TransformNormals(m3, ar, normals);
  }

  vtkDataArray* vectors = ps->GetPointData()->GetVectors();
  if (vectors != nullptr)
  {
    vtkImageTransform::TransformVectors(m3, ar, vectors);
  }

  // Grab the cells-related-data and process as appropriate
  normals = ps->GetCellData()->GetNormals();
  if (normals != nullptr)
  {
    vtkImageTransform::TransformNormals(m3, ar, normals);
  }

  vectors = ps->GetCellData()->GetVectors();
  if (vectors != nullptr)
  {
    vtkImageTransform::TransformVectors(m3, ar, vectors);
  }
}

//----------------------------------------------------------------------------
void vtkImageTransform::TranslatePoints(double* t, vtkDataArray* da)
{
  void* pts = da->GetVoidPointer(0);
  vtkIdType num = da->GetNumberOfTuples();

  switch (da->GetDataType())
  {
    vtkTemplateMacro(InPlaceTranslatePoints<VTK_TT>::Execute(t, num, (VTK_TT*)pts));
  }
}

//----------------------------------------------------------------------------
void vtkImageTransform::TransformPoints(vtkMatrix4x4* m4, vtkDataArray* da)
{
  void* pts = da->GetVoidPointer(0);
  vtkIdType num = da->GetNumberOfTuples();

  switch (da->GetDataType())
  {
    vtkTemplateMacro(InPlaceTransformPoints<VTK_TT>::Execute(m4, num, (VTK_TT*)pts));
  }
}

//----------------------------------------------------------------------------
void vtkImageTransform::TransformNormals(vtkMatrix3x3* m3, double spacing[3], vtkDataArray* da)
{
  void* n = da->GetVoidPointer(0);
  vtkIdType num = da->GetNumberOfTuples();

  switch (da->GetDataType())
  {
    vtkTemplateMacro(InPlaceTransformNormals<VTK_TT>::Execute(m3, spacing, num, (VTK_TT*)n));
  }
}

//----------------------------------------------------------------------------
void vtkImageTransform::TransformVectors(vtkMatrix3x3* m3, double spacing[3], vtkDataArray* da)
{
  void* v = da->GetVoidPointer(0);
  vtkIdType num = da->GetNumberOfTuples();

  switch (da->GetDataType())
  {
    vtkTemplateMacro(InPlaceTransformVectors<VTK_TT>::Execute(m3, spacing, num, (VTK_TT*)v));
  }
}

//----------------------------------------------------------------------------
void vtkImageTransform::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}