File: vtkImplicitFunction.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 (260 lines) | stat: -rw-r--r-- 7,454 bytes parent folder | download | duplicates (4)
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
/*=========================================================================

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

#include "vtkAbstractTransform.h"
#include "vtkArrayDispatch.h"
#include "vtkDataArrayRange.h"
#include "vtkMath.h"
#include "vtkTransform.h"

#include <algorithm>

vtkCxxSetObjectMacro(vtkImplicitFunction, Transform, vtkAbstractTransform);

vtkImplicitFunction::vtkImplicitFunction()
{
  this->Transform = nullptr;
}

vtkImplicitFunction::~vtkImplicitFunction()
{
  // static_cast needed since otherwise the
  // call to SetTransform becomes ambiguous
  this->SetTransform(static_cast<vtkAbstractTransform*>(nullptr));
}

namespace
{

template <class Func>
struct FunctionWorker
{
  Func F;
  FunctionWorker(Func f)
    : F(f)
  {
  }
  template <typename SourceArray, typename DestinationArray>
  void operator()(SourceArray* input, DestinationArray* output)
  {
    vtkIdType numTuples = input->GetNumberOfTuples();
    output->SetNumberOfTuples(numTuples);

    const auto srcTuples = vtk::DataArrayTupleRange<3>(input);
    auto dstValues = vtk::DataArrayValueRange<1>(output);

    using DstValueT = typename decltype(dstValues)::ValueType;

    double in[3];
    auto destIter = dstValues.begin();
    for (auto tuple = srcTuples.cbegin(); tuple != srcTuples.cend(); ++tuple, ++destIter)
    {
      in[0] = static_cast<double>((*tuple)[0]);
      in[1] = static_cast<double>((*tuple)[1]);
      in[2] = static_cast<double>((*tuple)[2]);
      *destIter = static_cast<DstValueT>(this->F(in));
    }
  }
};

class SimpleFunction
{
public:
  SimpleFunction(vtkImplicitFunction* function)
    : Function(function)
  {
  }
  double operator()(double in[3]) { return this->Function->EvaluateFunction(in); }

private:
  vtkImplicitFunction* Function;
};

class TransformFunction
{
public:
  TransformFunction(vtkImplicitFunction* function, vtkAbstractTransform* transform)
    : Function(function)
    , Transform(transform)
  {
  }
  double operator()(double in[3])
  {
    Transform->TransformPoint(in, in);
    return this->Function->EvaluateFunction(in);
  }

private:
  vtkImplicitFunction* Function;
  vtkAbstractTransform* Transform;
};

} // end anon namespace

void vtkImplicitFunction::FunctionValue(vtkDataArray* input, vtkDataArray* output)
{
  if (!this->Transform)
  {
    this->EvaluateFunction(input, output);
  }
  else // pass point through transform
  {
    FunctionWorker<TransformFunction> worker(TransformFunction(this, this->Transform));
    typedef vtkTypeList::Create<float, double> InputTypes;
    typedef vtkTypeList::Create<float, double> OutputTypes;
    typedef vtkArrayDispatch::Dispatch2ByValueType<InputTypes, OutputTypes> MyDispatch;
    if (!MyDispatch::Execute(input, output, worker))
    {
      worker(input, output); // Use vtkDataArray API if dispatch fails.
    }
  }
}

void vtkImplicitFunction::EvaluateFunction(vtkDataArray* input, vtkDataArray* output)
{

  // defend against uninitialized output datasets.
  output->SetNumberOfComponents(1);
  output->SetNumberOfTuples(input->GetNumberOfTuples());

  FunctionWorker<SimpleFunction> worker(SimpleFunction(this));
  typedef vtkTypeList::Create<float, double> InputTypes;
  typedef vtkTypeList::Create<float, double> OutputTypes;
  typedef vtkArrayDispatch::Dispatch2ByValueType<InputTypes, OutputTypes> MyDispatch;
  if (!MyDispatch::Execute(input, output, worker))
  {
    worker(input, output); // Use vtkDataArray API if dispatch fails.
  }
}

// Evaluate function at position x-y-z and return value. Point x[3] is
// transformed through transform (if provided).
double vtkImplicitFunction::FunctionValue(const double x[3])
{
  if (!this->Transform)
  {
    return this->EvaluateFunction(const_cast<double*>(x));
  }
  else // pass point through transform
  {
    double pt[3];
    this->Transform->TransformPoint(x, pt);
    return this->EvaluateFunction(pt);
  }

  /* Return negative if determinant of Jacobian matrix is negative,
     i.e. if the transformation has a flip.  This is more 'correct'
     than the above behaviour, because it turns the implicit surface
     inside-out in the same way that polygonal surfaces are turned
     inside-out by a flip.  It takes up too many valuable CPU cycles
     to check the determinant on every function evaluation, though.
  {
    double pt[3];
    double A[3][3];
    this->Transform->Update();
    this->Transform->InternalTransformDerivative(x,pt,A);
    double val = this->EvaluateFunction((double *)pt);

    if (vtkMath::Determinant3x3(A) < 0)
      {
      return -val;
      }
    else
      {
      return +val;
      }
    }
  */
}

// Evaluate function gradient at position x-y-z and pass back vector. Point
// x[3] is transformed through transform (if provided).
void vtkImplicitFunction::FunctionGradient(const double x[3], double g[3])
{
  if (!this->Transform)
  {
    this->EvaluateGradient(const_cast<double*>(x), g);
  }
  else // pass point through transform
  {
    double pt[3];
    double A[3][3];
    this->Transform->Update();
    this->Transform->InternalTransformDerivative(x, pt, A);
    this->EvaluateGradient(static_cast<double*>(pt), g);

    // The gradient must be transformed using the same math as is
    // use for a normal to a surface: it must be multiplied by the
    // inverse of the transposed inverse of the Jacobian matrix of
    // the transform, which is just the transpose of the Jacobian.
    vtkMath::Transpose3x3(A, A);
    vtkMath::Multiply3x3(A, g, g);

    /* If the determinant of the Jacobian matrix is negative,
       then the gradient points in the opposite direction.  This
       behaviour is actually incorrect, but is necessary to
       balance the incorrect behaviour of FunctionValue.  Otherwise,
       if you feed certain VTK filters a transform with a flip
       the gradient will point in the wrong direction and they
       will never converge to a result */

    if (vtkMath::Determinant3x3(A) < 0)
    {
      g[0] = -g[0];
      g[1] = -g[1];
      g[2] = -g[2];
    }
  }
}

// Overload standard modified time function. If Transform is modified,
// then this object is modified as well.
vtkMTimeType vtkImplicitFunction::GetMTime()
{
  vtkMTimeType mTime = this->vtkObject::GetMTime();
  vtkMTimeType TransformMTime;

  if (this->Transform != nullptr)
  {
    TransformMTime = this->Transform->GetMTime();
    mTime = (TransformMTime > mTime ? TransformMTime : mTime);
  }

  return mTime;
}

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

  if (this->Transform)
  {
    os << indent << "Transform:\n";
    this->Transform->PrintSelf(os, indent.GetNextIndent());
  }
  else
  {
    os << indent << "Transform: (None)\n";
  }
}

void vtkImplicitFunction::SetTransform(const double elements[16])
{
  vtkTransform* transform = vtkTransform::New();
  transform->SetMatrix(elements);
  this->SetTransform(transform);
  transform->Delete();
}