From c7b01be515d544c8b4ee2a74b19c84eb47422a4a Mon Sep 17 00:00:00 2001 From: apo Date: Thu, 1 May 2008 12:25:25 +0000 Subject: [PATCH] Improve VISU_ElnoGeometryFilter to be able handle vtkPolyData as its input --- src/CONVERTOR/VISU_ElnoMeshValue.hxx | 90 ++++++++++- src/PIPELINE/VISUPipeLine.cxx | 11 +- src/PIPELINE/VISU_ElnoGeometryFilter.cxx | 185 ++++++++++++++++++----- src/PIPELINE/VISU_ElnoGeometryFilter.hxx | 10 +- src/PIPELINE/VISU_ElnoWarpVector.cxx | 7 +- 5 files changed, 250 insertions(+), 53 deletions(-) diff --git a/src/CONVERTOR/VISU_ElnoMeshValue.hxx b/src/CONVERTOR/VISU_ElnoMeshValue.hxx index 3548f2e1..2de6b424 100644 --- a/src/CONVERTOR/VISU_ElnoMeshValue.hxx +++ b/src/CONVERTOR/VISU_ElnoMeshValue.hxx @@ -27,6 +27,8 @@ #include "VISU_VTKTypeList.hxx" +class vtkDataSetAttributes; + /*! \file VISU_ElnoMeshValue.hxx \brief The file contains declarations for the acess to the specific ELNO MED data @@ -47,9 +49,9 @@ namespace VISU //---------------------------------------------------------------------------- TGetElnoNodeData( vtkDataArray *theElnoDataArray, - vtkIntArray *theElnoDataMapper ) + vtkDataArray *theElnoDataMapper ) : myElnoDataArray( TVTKDataArray::SafeDownCast( theElnoDataArray ) ) - , myElnoDataMapper( theElnoDataMapper ) + , myElnoDataMapper( vtkIntArray::SafeDownCast( theElnoDataMapper ) ) {} @@ -58,7 +60,10 @@ namespace VISU operator () ( vtkIdType theCellId, vtkIdType theLocalPntId ) { myElnoDataMapper->GetTupleValue( theCellId, myElemInfo ); - return myElnoDataArray->GetPointer( myElemInfo[ 0 ] ); + + vtkIdType aPos = myElemInfo[ 0 ] + theLocalPntId * myElemInfo[ 1 ]; + + return myElnoDataArray->GetPointer( aPos ); } @@ -67,11 +72,90 @@ namespace VISU getNbComp() { myElnoDataMapper->GetTupleValue( 0, myElemInfo ); + return myElemInfo[ 1 ]; } }; + //---------------------------------------------------------------------------------------------- + template< int elno_type > + struct TSetElnoNodeData + { + typedef typename TL::TEnum2VTKArrayType< elno_type >::TResult TVTKDataArray; + typedef typename TL::TEnum2VTKBasicType< elno_type >::TResult TDataType; + + //---------------------------------------------------------------------------------------------- + TSetElnoNodeData( vtkIdType theEffectNbComp, + vtkIdType theRealNbComp, + vtkIdType theNbTuples, + const char* theDataArrayName, + const char* theMapperArrayName ) + : myElnoDataArray( TVTKDataArray::New() ) + , myElnoDataMapper( vtkIntArray::New() ) + { + myElnoDataArray->SetNumberOfComponents( theEffectNbComp ); + myElnoDataArray->SetNumberOfTuples( theNbTuples ); + myElnoDataArray->SetName( theDataArrayName ); + + myElnoDataMapper->SetNumberOfComponents( 3 ); + myElnoDataMapper->Allocate( theNbTuples * 3 ); + myElnoDataMapper->SetName( theMapperArrayName ); + + myElemInfo[ 0 ] = 0; + myElemInfo[ 1 ] = theRealNbComp; + myElemInfo[ 2 ] = 0; + } + + + //---------------------------------------------------------------------------------------------- + ~TSetElnoNodeData() + { + myElnoDataArray->Delete(); + myElnoDataMapper->Delete(); + } + + + //---------------------------------------------------------------------------------------------- + int + AddNextPointData( TDataType* theDataPtr ) + { + vtkIdType aPos = myElemInfo[ 0 ] + myElemInfo[ 2 ] * myElemInfo[ 1 ]; + + TDataType* aDataPtr = myElnoDataArray->GetPointer( aPos ); + + for ( vtkIdType aCompId = 0; aCompId < myElemInfo[ 1 ]; aCompId++ ) + *aDataPtr++ = *theDataPtr++; + + return myElemInfo[ 2 ]++; + } + + + //---------------------------------------------------------------------------------------------- + void + InsertNextCellData() + { + myElnoDataMapper->InsertNextTupleValue( myElemInfo ); + myElemInfo[ 0 ] = myElemInfo[ 2 ]; + myElemInfo[ 2 ] = 0; + } + + + //---------------------------------------------------------------------------------------------- + void + AddData( vtkDataSetAttributes* theDataSetAttributes ) + { + theDataSetAttributes->AddArray( myElnoDataArray ); + theDataSetAttributes->AddArray( myElnoDataMapper ); + } + + protected: + TVTKDataArray *myElnoDataArray; + vtkIntArray *myElnoDataMapper; + int myElemInfo[ 3 ]; + }; + + //--------------------------------------------------------------- } diff --git a/src/PIPELINE/VISUPipeLine.cxx b/src/PIPELINE/VISUPipeLine.cxx index 83890ad4..f072a6a6 100644 --- a/src/PIPELINE/VISUPipeLine.cxx +++ b/src/PIPELINE/VISUPipeLine.cxx @@ -268,11 +268,13 @@ main(int argc, char** argv) //aGeometryFilter->SetInput( aCutter->GetOutput() ); VISU::WriteToFile( aGeometryFilter->GetOutput(), "/data/apo/elno_from_geometry.vtk" ); + vtkFloatingPointType aScalarRange[ 2 ]; + aGeometryFilter->GetRange( aScalarRange ); + // To calculate and apply default scale factor //{ // vtkFloatingPointType aScale = VISU_DeformedShapePL::GetScaleFactor( anElnoWarpVector->GetOutput() ); - // vtkFloatingPointType* aRange = aGeometryFilter->GetRange(); - // aScale /= aRange[1]; + // aScale /= aScalarRange[1]; // anElnoWarpVector->SetScaleFactor( aScale ); //} @@ -280,11 +282,10 @@ main(int argc, char** argv) aMapper->SetInput( aGeometryFilter->GetOutput() ); VISU_LookupTable* aMapperTable( VISU_LookupTable::New() ); - aMapperTable->SetHueRange(0.667, 0.0); - aMapperTable->SetRange( aGeometryFilter->GetRange() ); + aMapperTable->SetHueRange( 0.667, 0.0 ); + aMapperTable->SetRange( aScalarRange ); aMapperTable->Build(); - aMapper->SetLookupTable( aMapperTable ); aMapper->SetUseLookupTableScalarRange( true ); aMapper->SetColorModeToMapScalars(); diff --git a/src/PIPELINE/VISU_ElnoGeometryFilter.cxx b/src/PIPELINE/VISU_ElnoGeometryFilter.cxx index 8f05a18d..b2ad9009 100755 --- a/src/PIPELINE/VISU_ElnoGeometryFilter.cxx +++ b/src/PIPELINE/VISU_ElnoGeometryFilter.cxx @@ -1,5 +1,6 @@ #include "VISU_ElnoGeometryFilter.hxx" #include "VISU_ConvertorUtils.hxx" +#include "VISU_ElnoMeshValue.hxx" #include #include @@ -57,6 +58,7 @@ VISU_ElnoGeometryFilter::VISU_ElnoGeometryFilter() myMinMax[0] = -VTK_DOUBLE_MAX; myMinMax[1] = VTK_DOUBLE_MAX; } + //---------------------------------------------------------------------------------------------- VISU_ElnoGeometryFilter::~VISU_ElnoGeometryFilter() { @@ -67,39 +69,147 @@ VISU_ElnoGeometryFilter::~VISU_ElnoGeometryFilter() } } + //---------------------------------------------------------------------------------------------- -int VISU_ElnoGeometryFilter::RequestData(vtkInformation *request, - vtkInformationVector **inputVector, - vtkInformationVector *outputVector) +int VISU_ElnoGeometryFilter::RequestData( vtkInformation *request, + vtkInformationVector **inputVector, + vtkInformationVector *outputVector ) { // get the info objects vtkInformation *inInfo = inputVector[0]->GetInformationObject(0); vtkInformation *outInfo = outputVector->GetInformationObject(0); // get the input and ouptut - vtkDataSet *input = vtkDataSet::SafeDownCast( - inInfo->Get(vtkDataObject::DATA_OBJECT())); - vtkPolyData *output = vtkPolyData::SafeDownCast( - outInfo->Get(vtkDataObject::DATA_OBJECT())); + vtkDataSet *input = vtkDataSet::SafeDownCast( inInfo->Get(vtkDataObject::DATA_OBJECT() ) ); + vtkPolyData *output = vtkPolyData::SafeDownCast( outInfo->Get(vtkDataObject::DATA_OBJECT() ) ); vtkIdType numCells=input->GetNumberOfCells(); + if ( numCells == 0 ) + return 0; - if(!InitInternalArray(input)) - return 1; + if ( !InitInternalArray( input ) ) + return 0; - if (numCells == 0) - { - return 1; + switch ( input->GetDataObjectType() ) { + case VTK_UNSTRUCTURED_GRID : + return this->UnstructuredGridExecute( vtkUnstructuredGrid::SafeDownCast( input ) , output, outInfo ); + case VTK_POLY_DATA : + return this->PolyDataExecute( vtkPolyData::SafeDownCast( input ), output, outInfo ); + default : + return Superclass::RequestData( request, inputVector, outputVector ); + } + + return 0; +} + + +//---------------------------------------------------------------------------------------------- +namespace +{ + template < int elno_type > + int PolyDataExecute( vtkPolyData *input, + vtkPolyData *output, + int theScalarMode, + vtkFloatingPointType* theMinMax ) + { + vtkDataArray* anElnoDataArray = input->GetCellData()->GetArray( "ELNO_FIELD" ); + vtkDataArray* anElnoDataMapper = input->GetCellData()->GetArray( "ELNO_COMPONENT_MAPPER" ); + + theMinMax[ 0 ] = VTK_DOUBLE_MAX; + theMinMax[ 1 ] = -VTK_DOUBLE_MAX; + + vtkIdType aNbConnectivity = 0; + + if ( vtkCellArray* aCellArray = input->GetVerts() ) + aNbConnectivity += aCellArray->GetSize(); + + if ( vtkCellArray* aCellArray = input->GetLines() ) + aNbConnectivity += aCellArray->GetSize(); + + if ( vtkCellArray* aCellArray = input->GetPolys() ) + aNbConnectivity += aCellArray->GetSize(); + + if ( vtkCellArray* aCellArray = input->GetStrips() ) + aNbConnectivity += aCellArray->GetSize(); + + vtkIdType aNbCells = input->GetNumberOfCells(); + vtkIdType anEffectNbComp = aNbConnectivity / aNbCells; + + VISU::TGetElnoNodeData< elno_type > aGetElnoNodeData( anElnoDataArray, anElnoDataMapper ); + typedef typename VISU::TL::TEnum2VTKBasicType< elno_type >::TResult TElnoDataType; + vtkIdType aNbDim = std::min( 3, aGetElnoNodeData.getNbComp() ); + + VISU::TSetElnoNodeData< elno_type > aSetElnoNodeData( anEffectNbComp, + 1, + aNbCells, + "ELNO_PSEUDO_SCALARS", + "ELNO_PSEUDO_SCALARS_MAPPER" ); + + input->BuildCells(); + vtkIdType aNbPts = 0, *aPts = 0; + for ( vtkIdType aCellId = 0; aCellId < aNbCells; aCellId++ ) { + input->GetCellPoints( aCellId, aNbPts, aPts ); + + for ( vtkIdType aPntId = 0; aPntId < aNbPts; aPntId++ ) { + vtkIdType aCurrentPntId = aPts[ aPntId ]; + TElnoDataType* anElnoData = aGetElnoNodeData( aCellId, aCurrentPntId ); + + TElnoDataType anElnoValue = 0; + if ( theScalarMode > 0 ) { + anElnoValue = anElnoData[ theScalarMode - 1 ]; + } else { + for ( vtkIdType aDimId = 0; aDimId < aNbDim; aDimId++ ) + anElnoValue += anElnoData[ aDimId ] * anElnoData[ aDimId ]; + + anElnoValue = TElnoDataType( sqrt( anElnoValue ) ); + } + + aSetElnoNodeData.AddNextPointData( &anElnoValue ); + + theMinMax[ 0 ] = std::min( theMinMax[ 0 ], vtkFloatingPointType( anElnoValue ) ); + theMinMax[ 1 ] = std::max( theMinMax[ 1 ], vtkFloatingPointType( anElnoValue ) ); + } + + aSetElnoNodeData.InsertNextCellData(); } + + aSetElnoNodeData.AddData( output->GetCellData() ); - if (input->GetDataObjectType() == VTK_UNSTRUCTURED_GRID){ - this->UnstructuredGridExecute(input, output, outInfo); return 1; - }else - return Superclass::RequestData(request,inputVector,outputVector); - - return 1; + } + + + //---------------------------------------------------------------------------------------------- } + + +//---------------------------------------------------------------------------------------------- +int VISU_ElnoGeometryFilter::PolyDataExecute( vtkPolyData *input, + vtkPolyData *output, + vtkInformation *outInfo ) +{ + output->CopyStructure( input ); + output->GetPointData()->PassData( input->GetPointData() ); + output->GetCellData()->PassData( input->GetCellData() ); + + vtkDataArray* anElnoDataArray = input->GetCellData()->GetArray( "ELNO_FIELD" ); + switch( anElnoDataArray->GetDataType() ){ + case VTK_DOUBLE : + return ::PolyDataExecute< VTK_DOUBLE >( input, output, myScalarMode, myMinMax ); + case VTK_FLOAT : + return ::PolyDataExecute< VTK_FLOAT >( input, output, myScalarMode, myMinMax ); + case VTK_INT : + return ::PolyDataExecute< VTK_INT >( input, output, myScalarMode, myMinMax ); + case VTK_LONG : + return ::PolyDataExecute< VTK_LONG >( input, output, myScalarMode, myMinMax ); + default : + break; + } + + return 0; +} + + //---------------------------------------------------------------------------------------------- template void BuildMinMax(TValueType* inputPtr, double *outres, const vtkIdType& theDataSize){ @@ -118,17 +228,19 @@ void BuildMinMax(TValueType* inputPtr, double *outres, const vtkIdType& theDataS inputPtr++; } } + + //---------------------------------------------------------------------------------------------- -void VISU_ElnoGeometryFilter::UnstructuredGridExecute(vtkDataSet *dataSetInput, - vtkPolyData *output, - vtkInformation *outInfo) +int VISU_ElnoGeometryFilter::UnstructuredGridExecute( vtkUnstructuredGrid *input, + vtkPolyData *output, + vtkInformation *outInfo ) { - vtkUnstructuredGrid *input= (vtkUnstructuredGrid *)dataSetInput; + // Check input vtkCellArray *Connectivity = input->GetCells(); - if (Connectivity == NULL) - { - return; - } + if ( Connectivity == NULL ) { + vtkDebugMacro(<<"Nothing to extract"); + return 0; + } vtkIdType cellId; int i; @@ -176,13 +288,6 @@ void VISU_ElnoGeometryFilter::UnstructuredGridExecute(vtkDataSet *dataSetInput, cellGhostLevels = ((vtkUnsignedCharArray*)temp)->GetPointer(0); } - // Check input - if ( Connectivity == NULL ) - { - vtkDebugMacro(<<"Nothing to extract"); - return; - } - // Determine nature of what we have to do cellIds = vtkIdList::New(); faceIds = vtkIdList::New(); @@ -688,6 +793,8 @@ void VISU_ElnoGeometryFilter::UnstructuredGridExecute(vtkDataSet *dataSetInput, { delete [] cellVis; } + + return 1; } //---------------------------------------------------------------------------------------------- @@ -703,9 +810,15 @@ vtkIdType VISU_ElnoGeometryFilter::GetScalarMode() const{ } //---------------------------------------------------------------------------------------------- -double* VISU_ElnoGeometryFilter::GetRange(){ - this->Update(); // To perform automatic update if neccesary - return myMinMax; +void VISU_ElnoGeometryFilter::GetRange( vtkFloatingPointType *theRange ) +{ + theRange[ 0 ] = +VTK_DOUBLE_MAX; + theRange[ 1 ] = -VTK_DOUBLE_MAX; + + this->Update(); // To perform automatic update if no + + theRange[ 0 ] = myMinMax[ 0 ]; + theRange[ 1 ] = myMinMax[ 1 ]; } //---------------------------------------------------------------------------------------------- diff --git a/src/PIPELINE/VISU_ElnoGeometryFilter.hxx b/src/PIPELINE/VISU_ElnoGeometryFilter.hxx index aab58854..c5418185 100755 --- a/src/PIPELINE/VISU_ElnoGeometryFilter.hxx +++ b/src/PIPELINE/VISU_ElnoGeometryFilter.hxx @@ -23,7 +23,8 @@ public: void SetScalarMode(vtkIdType theScalarMode = 0); vtkIdType GetScalarMode() const; - double* GetRange(); + + void GetRange( vtkFloatingPointType *theRange ); protected: @@ -33,11 +34,11 @@ protected: virtual int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *); + int UnstructuredGridExecute( vtkUnstructuredGrid *, vtkPolyData *, vtkInformation * ); + int PolyDataExecute( vtkPolyData *, vtkPolyData *, vtkInformation * ); - void UnstructuredGridExecute (vtkDataSet *, vtkPolyData *, vtkInformation *); void GetElementData(const vtkIdType& theElemId, vtkDataArray *theOutArray); - private: bool InitInternalArray(vtkDataSet *input); void CopyElementData(vtkDataArray *toComponents, //Components @@ -60,8 +61,7 @@ private: const vtkIdType &theCellId); private: - double myMinMax[2]; // myMinMax[0] - Min - // myMinMax[1] - Max + vtkFloatingPointType myMinMax[ 2 ]; // { Min, Max } vtkDataArray *myElnoData; vtkDataArray *myElnoMapper; vtkIdType myScalarMode; diff --git a/src/PIPELINE/VISU_ElnoWarpVector.cxx b/src/PIPELINE/VISU_ElnoWarpVector.cxx index 78825732..f47cf5d5 100644 --- a/src/PIPELINE/VISU_ElnoWarpVector.cxx +++ b/src/PIPELINE/VISU_ElnoWarpVector.cxx @@ -85,7 +85,7 @@ namespace vtkUnstructuredGrid *theInput, vtkUnstructuredGrid *theOutput, vtkDataArray *theElnoDataArray, - vtkIntArray *theElnoDataMapper ) + vtkDataArray *theElnoDataMapper ) { VISU::TGetElnoNodeData< elno_type > aGetElnoNodeData( theElnoDataArray, theElnoDataMapper ); typedef typename VISU::TL::TEnum2VTKBasicType< elno_type >::TResult TElnoDataType; @@ -145,7 +145,7 @@ namespace vtkUnstructuredGrid *theInput, vtkUnstructuredGrid *theOutput, vtkDataArray *theElnoDataArray, - vtkIntArray *theElnoDataMapper ) + vtkDataArray *theElnoDataMapper ) { switch( theElnoDataArray->GetDataType() ){ case VTK_DOUBLE: @@ -200,8 +200,7 @@ int VISU_ElnoWarpVector::RequestData( vtkInformation *vtkNotUsed(request), vtkUnstructuredGrid::SafeDownCast( outInfo->Get( vtkDataObject::DATA_OBJECT() ) ); vtkDataArray *anElnoDataArray = this->GetInputArrayToProcess( 0, inputVector ); - vtkIntArray *anElnoDataMapper = - vtkIntArray::SafeDownCast( this->GetInputArrayToProcess( 1, inputVector ) ); + vtkDataArray *anElnoDataMapper = this->GetInputArrayToProcess( 1, inputVector ); vtkPoints *aPoints = anInput->GetPoints(); switch( aPoints->GetDataType() ){ -- 2.39.2