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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDataSetGradient.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.
=========================================================================*/
// .SECTION Thanks
// This file is part of the generalized Youngs material interface reconstruction algorithm contributed by
// CEA/DIF - Commissariat a l'Energie Atomique, Centre DAM Ile-De-France <br>
// BP12, F-91297 Arpajon, France. <br>
// Implementation by Thierry Carrard (CEA)
#include "vtkDataSetGradient.h"
#include "vtkDataSetGradientPrecompute.h"
#include "vtkDataSet.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkCell.h"
#include "vtkMath.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
// utility macros
#define ADD_VEC(a,b) a[0]+=b[0];a[1]+=b[1];a[2]+=b[2]
#define SCALE_VEC(a,b) a[0]*=b;a[1]*=b;a[2]*=b
#define ZERO_VEC(a) a[0]=0;a[1]=0;a[2]=0
#define COPY_VEC(a,b) a[0]=b[0];a[1]=b[1];a[2]=b[2];
#define MAX_CELL_POINTS 128
#define VTK_CQS_EPSILON 1e-12
// standard constructors and factory
vtkStandardNewMacro(vtkDataSetGradient);
/*!
The default constructor
\sa ~vtkDataSetGradient()
*/
vtkDataSetGradient::vtkDataSetGradient()
: ResultArrayName(0)
{
this->SetResultArrayName("gradient");
}
/*!
The destrcutor
\sa vtkDataSetGradient()
*/
vtkDataSetGradient::~vtkDataSetGradient()
{
this->SetResultArrayName( 0 );
}
void vtkDataSetGradient::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Result array name: " << this->ResultArrayName << "\n";
}
int vtkDataSetGradient::RequestData(vtkInformation * vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
// get the info objects
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation *outInfo = outputVector->GetInformationObject(0);
// get connected input & output
vtkDataSet *_output = vtkDataSet::SafeDownCast( outInfo->Get(vtkDataObject::DATA_OBJECT()) );
vtkDataSet* _input = vtkDataSet::SafeDownCast( inInfo->Get(vtkDataObject::DATA_OBJECT()) );
if( _input==0 || _output==0 )
{
vtkErrorMacro(<<"Missing input or output \n");
return 0;
}
// get array to compute gradient from
vtkDataArray* inArray = this->GetInputArrayToProcess( 0, _input );
if( inArray==0 )
{
inArray = _input->GetPointData()->GetScalars();
}
if( inArray==0 )
{
inArray = _input->GetCellData()->GetScalars();
}
if( inArray==0 )
{
vtkErrorMacro(<<"no input array to process\n");
return 0;
}
vtkDebugMacro(<<"Input array to process : "<<inArray->GetName()<<"\n");
bool pointData;
if( _input->GetCellData()->GetArray(inArray->GetName()) == inArray )
{
pointData = false;
vtkDebugMacro(<<"cell data to point gradient\n");
}
else if( _input->GetPointData()->GetArray(inArray->GetName()) == inArray )
{
pointData = true;
vtkDebugMacro(<<"point data to cell gradient\n");
}
else
{
vtkErrorMacro(<<"input array must be cell or point data\n");
return 0;
}
// we're just adding a scalar field
_output->ShallowCopy( _input );
vtkDataArray* cqsArray = _output->GetFieldData()->GetArray("GradientPrecomputation");
vtkDataArray* sizeArray = _output->GetCellData()->GetArray("CellSize");
if( cqsArray==0 || sizeArray==0 )
{
vtkDebugMacro(<<"Couldn't find field array 'GradientPrecomputation', computing it right now.\n");
vtkDataSetGradientPrecompute::GradientPrecompute(_output);
cqsArray = _output->GetFieldData()->GetArray("GradientPrecomputation");
sizeArray = _output->GetCellData()->GetArray("CellSize");
if( cqsArray==0 || sizeArray==0 )
{
vtkErrorMacro(<<"Computation of field array 'GradientPrecomputation' or 'CellSize' failed.\n");
return 0;
}
}
vtkIdType nCells = _input->GetNumberOfCells();
vtkIdType nPoints = _input->GetNumberOfPoints();
vtkDoubleArray* gradientArray = vtkDoubleArray::New();
gradientArray->SetName( this->ResultArrayName );
gradientArray->SetNumberOfComponents(3);
if( pointData ) // compute cell gradient from point data
{
gradientArray->SetNumberOfTuples( nCells );
vtkIdType cellPoint = 0;
for(vtkIdType i=0;i<nCells;i++)
{
vtkCell* cell = _input->GetCell(i);
int np = cell->GetNumberOfPoints();
double gradient[3] = {0,0,0};
for(int p=0;p<np;p++)
{
double cqs[3], scalar;
cqsArray->GetTuple( cellPoint++ , cqs );
scalar = inArray->GetTuple1( cell->GetPointId(p) );
SCALE_VEC( cqs , scalar );
ADD_VEC( gradient , cqs );
}
SCALE_VEC( gradient , ( 1.0 / sizeArray->GetTuple1(i) ) );
gradientArray->SetTuple( i , gradient );
}
_output->GetCellData()->AddArray( gradientArray );
//_output->GetCellData()->SetVectors( gradientArray );
}
else // compute point gradient from cell data
{
gradientArray->SetNumberOfTuples( nPoints );
gradientArray->FillComponent(0, 0.0);
gradientArray->FillComponent(1, 0.0);
gradientArray->FillComponent(2, 0.0);
double * gradient = gradientArray->WritePointer(0,nPoints*3);
double * gradientDivisor = new double [nPoints];
for(vtkIdType i=0;i<nPoints;i++)
{
gradientDivisor[i] = 0.0;
}
vtkIdType cellPoint = 0;
for(vtkIdType i=0;i<nCells;i++)
{
vtkCell* cell = _input->GetCell(i);
int np = cell->GetNumberOfPoints();
double scalar = inArray->GetTuple1( i );
for(int p=0;p<np;p++)
{
double cqs[3];
double pointCoord[3];
vtkIdType pointId = cell->GetPointId(p);
cqsArray->GetTuple( cellPoint++ , cqs );
_input->GetPoint( cell->GetPointId(p), pointCoord );
scalar *= cell->GetCellDimension();
SCALE_VEC( (gradient+pointId*3) , scalar );
gradientDivisor[pointId] += vtkMath::Dot(cqs,pointCoord);
}
}
for(vtkIdType i=0;i<nPoints;i++)
{
SCALE_VEC( (gradient+i*3) , (1.0/gradientDivisor[i]) );
}
delete [] gradientDivisor;
_output->GetPointData()->AddArray( gradientArray );
//_output->GetPointData()->SetVectors( gradientArray );
}
gradientArray->Delete();
vtkDebugMacro(<<_output->GetClassName()<<" @ "<<_output<<" :\n");
return 1;
}
|