File: vtkCellDerivatives.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (299 lines) | stat: -rw-r--r-- 9,625 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
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
/*=========================================================================

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

#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkGenericCell.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"

#include <cmath>

vtkStandardNewMacro(vtkCellDerivatives);

vtkCellDerivatives::vtkCellDerivatives()
{
  this->VectorMode = VTK_VECTOR_MODE_COMPUTE_GRADIENT;
  this->TensorMode = VTK_TENSOR_MODE_COMPUTE_GRADIENT;

  // by default process active point scalars
  this->SetInputArrayToProcess(0,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,
                               vtkDataSetAttributes::SCALARS);

  // by default process active point vectors
  this->SetInputArrayToProcess(1,0,0,vtkDataObject::FIELD_ASSOCIATION_POINTS,
                               vtkDataSetAttributes::VECTORS);
}

int vtkCellDerivatives::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
  // get the info objects
  vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the input and output
  vtkDataSet *input = vtkDataSet::SafeDownCast(
    inInfo->Get(vtkDataObject::DATA_OBJECT()));
  vtkDataSet *output = vtkDataSet::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  vtkPointData *pd=input->GetPointData(), *outPD=output->GetPointData();
  vtkCellData *cd=input->GetCellData(), *outCD=output->GetCellData();
  vtkDataArray *inScalars=this->GetInputArrayToProcess(0, inputVector);
  vtkDataArray *inVectors=this->GetInputArrayToProcess(1, inputVector);
  vtkDoubleArray *outGradients=NULL;
  vtkDoubleArray *outVorticity=NULL;
  vtkDoubleArray *outTensors=NULL;
  vtkIdType numCells=input->GetNumberOfCells();
  int computeScalarDerivs=1, computeVectorDerivs=1, computeVorticity=1, subId;

  // Initialize
  vtkDebugMacro(<<"Computing cell derivatives");

  // First, copy the input to the output as a starting point
  output->CopyStructure( input );

  // Check input
  if ( numCells < 1 )
  {
    vtkErrorMacro("No cells to generate derivatives from");
    return 1;
  }

  // Figure out what to compute
  if ( inScalars && this->VectorMode == VTK_VECTOR_MODE_COMPUTE_GRADIENT )
  {
    outGradients = vtkDoubleArray::New();
    outGradients->SetNumberOfComponents(3);
    outGradients->SetNumberOfTuples(numCells);
    outGradients->SetName("ScalarGradient");
  }
  else
  {
    computeScalarDerivs = 0;
  }

  if ( inVectors && this->VectorMode == VTK_VECTOR_MODE_COMPUTE_VORTICITY )
  {
    outVorticity = vtkDoubleArray::New();
    outVorticity->SetNumberOfComponents(3);
    outVorticity->SetNumberOfTuples(numCells);
    outVorticity->SetName("Vorticity");
  }
  else
  {
    computeVorticity = 0;
  }

  if (inVectors && ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GRADIENT ||
                     this->TensorMode == VTK_TENSOR_MODE_COMPUTE_STRAIN ||
                     this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GREEN_LAGRANGE_STRAIN ))
  {
    outTensors = vtkDoubleArray::New();
    outTensors->SetNumberOfComponents(9);
    outTensors->SetNumberOfTuples(numCells);
    if ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_STRAIN )
    {
      outTensors->SetName("Strain");
    }
    else if ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GREEN_LAGRANGE_STRAIN )
    {
      outTensors->SetName("GreenLagrangeStrain");
    }
    else
    {
      outTensors->SetName("VectorGradient");
    }
  }
  else
  {
    computeVectorDerivs = 0;
  }

  // If just passing data forget the loop
  if ( computeScalarDerivs || computeVectorDerivs || computeVorticity )
  {
    double pcoords[3], derivs[9], tens[9], w[3], *scalars, *vectors;
    vtkGenericCell *cell = vtkGenericCell::New();
    vtkIdType cellId;
    vtkDoubleArray *cellScalars=vtkDoubleArray::New();
    if ( computeScalarDerivs )
    {
      cellScalars->SetNumberOfComponents(inScalars->GetNumberOfComponents());
      cellScalars->Allocate(cellScalars->GetNumberOfComponents()*VTK_CELL_SIZE);
      cellScalars->SetName("Scalars");
    }
    vtkDoubleArray *cellVectors=vtkDoubleArray::New();
    cellVectors->SetNumberOfComponents(3);
    cellVectors->Allocate(3*VTK_CELL_SIZE);
    cellVectors->SetName("Vectors");

    // Loop over all cells computing derivatives
    vtkIdType progressInterval = numCells/20 + 1;
    for (cellId=0; cellId < numCells; cellId++)
    {
      if ( ! (cellId % progressInterval) )
      {
        vtkDebugMacro(<<"Computing cell #" << cellId);
        this->UpdateProgress (static_cast<double>(cellId)/numCells);
      }

      input->GetCell(cellId, cell);
      subId = cell->GetParametricCenter(pcoords);

      if ( computeScalarDerivs )
      {
        inScalars->GetTuples(cell->PointIds, cellScalars);
        scalars = cellScalars->GetPointer(0);
        cell->Derivatives(subId, pcoords, scalars, 1, derivs);
        outGradients->SetTuple(cellId, derivs);
      }

      if ( computeVectorDerivs || computeVorticity )
      {
        inVectors->GetTuples(cell->PointIds, cellVectors);
        vectors = cellVectors->GetPointer(0);
        cell->Derivatives(0, pcoords, vectors, 3, derivs);

        // Insert appropriate tensor
        if ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GRADIENT)
        {
          outTensors->InsertTuple(cellId, derivs);
        }
        else if (this->TensorMode == VTK_TENSOR_MODE_COMPUTE_STRAIN)
        {
          tens[0] = 0.5*(derivs[0]+derivs[0]);
          tens[1] = 0.5*(derivs[1]+derivs[3]);
          tens[2] = 0.5*(derivs[2]+derivs[6]);
          tens[3] = 0.5*(derivs[3]+derivs[1]);
          tens[4] = 0.5*(derivs[4]+derivs[4]);
          tens[5] = 0.5*(derivs[5]+derivs[7]);
          tens[6] = 0.5*(derivs[6]+derivs[2]);
          tens[7] = 0.5*(derivs[7]+derivs[5]);
          tens[8] = 0.5*(derivs[8]+derivs[8]);

          outTensors->InsertTuple(cellId, tens);
        }
        else if (this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GREEN_LAGRANGE_STRAIN)
        {
          tens[0] = 0.5*(derivs[0]+derivs[0]+derivs[0]*derivs[0]+derivs[3]*derivs[3]+derivs[6]*derivs[6]);
          tens[1] = 0.5*(derivs[1]+derivs[3]+derivs[0]*derivs[1]+derivs[3]*derivs[4]+derivs[6]*derivs[7]);
          tens[2] = 0.5*(derivs[2]+derivs[6]+derivs[0]*derivs[2]+derivs[3]*derivs[5]+derivs[6]*derivs[8]);
          tens[3] = 0.5*(derivs[3]+derivs[1]+derivs[1]*derivs[0]+derivs[4]*derivs[3]+derivs[7]*derivs[6]);
          tens[4] = 0.5*(derivs[4]+derivs[4]+derivs[1]*derivs[1]+derivs[4]*derivs[4]+derivs[7]*derivs[7]);
          tens[5] = 0.5*(derivs[5]+derivs[7]+derivs[1]*derivs[2]+derivs[4]*derivs[5]+derivs[7]*derivs[8]);
          tens[6] = 0.5*(derivs[6]+derivs[2]+derivs[2]*derivs[0]+derivs[5]*derivs[3]+derivs[8]*derivs[6]);
          tens[7] = 0.5*(derivs[7]+derivs[5]+derivs[2]*derivs[1]+derivs[5]*derivs[4]+derivs[8]*derivs[7]);
          tens[8] = 0.5*(derivs[8]+derivs[8]+derivs[2]*derivs[2]+derivs[5]*derivs[5]+derivs[8]*derivs[8]);

          outTensors->InsertTuple(cellId, tens);
        }
        else if (this->TensorMode == VTK_TENSOR_MODE_PASS_TENSORS)
        {
          // do nothing.
        }

        if ( computeVorticity )
        {
          w[0] = derivs[7] - derivs[5];
          w[1] = derivs[2] - derivs[6];
          w[2] = derivs[3] - derivs[1];
          outVorticity->SetTuple(cellId, w);
        }
      }
    }//for all cells

    cell->Delete();
    cellScalars->Delete();
    cellVectors->Delete();
  }//if something to compute

  // Pass appropriate data through to output
  outPD->PassData(pd);
  outCD->PassData(cd);
  if (outGradients)
  {
    outCD->SetVectors(outGradients);
    outGradients->Delete();
  }
  if (outVorticity)
  {
    outCD->SetVectors(outVorticity);
    outVorticity->Delete();
  }
  if (outTensors)
  {
    outCD->SetTensors(outTensors);
    outTensors->Delete();
  }

  return 1;
}

const char *vtkCellDerivatives::GetVectorModeAsString(void)
{
  if ( this->VectorMode == VTK_VECTOR_MODE_PASS_VECTORS )
  {
    return "PassVectors";
  }
  else if ( this->VectorMode == VTK_VECTOR_MODE_COMPUTE_GRADIENT )
  {
    return "ComputeGradient";
  }
  else //VTK_VECTOR_MODE_COMPUTE_VORTICITY
  {
    return "ComputeVorticity";
  }
}

const char *vtkCellDerivatives::GetTensorModeAsString(void)
{
  if ( this->TensorMode == VTK_TENSOR_MODE_PASS_TENSORS )
  {
    return "PassTensors";
  }
  else if ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_GRADIENT )
  {
    return "ComputeGradient";
  }
  else if ( this->TensorMode == VTK_TENSOR_MODE_COMPUTE_STRAIN )
  {
    return "ComputeStrain";
  }
  else //VTK_TENSOR_MODE_COMPUTE_GREEN_LAGRANGE_STRAIN
  {
    return "ComputeGreenLagrangeStrain";
  }
}

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

  os << indent << "Vector Mode: " << this->GetVectorModeAsString()
     << endl;

  os << indent << "Tensor Mode: " << this->GetTensorModeAsString()
     << endl;
}