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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSortFieldData.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 "vtkSortFieldData.h"
#include "vtkAbstractArray.h"
#include "vtkFieldData.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
//-------------------------------------------------------------------------
vtkStandardNewMacro(vtkSortFieldData);
//-------------------------------------------------------------------------
vtkSortFieldData::vtkSortFieldData()
{
}
//---------------------------------------------------------------------------
vtkSortFieldData::~vtkSortFieldData()
{
}
//-------------------------------------------------------------------------
// Using vtkSortDataArray, it's easy to loop over all of the arrays in the
// field data and sort them. Initially we just need to generate the sort
// indices which are then applied to each array in turn.
vtkIdType* vtkSortFieldData::
Sort( vtkFieldData *fd, const char *arrayName, int k, int retIndices, int dir)
{
// Verify the input
if ( fd == NULL || arrayName == NULL )
{
vtkGenericWarningMacro("SortFieldData needs valid input");
return NULL;
}
int pos;
vtkAbstractArray *array = fd->GetAbstractArray(arrayName, pos);
if ( pos < 0 )
{
vtkGenericWarningMacro("Sorting array not found.");
return NULL;
}
int numComp = array->GetNumberOfComponents();
if ( k < 0 || k >= numComp )
{
vtkGenericWarningMacro( "Cannot sort by column " << k <<
" since the array only has columns 0 through " << (numComp-1) );
return NULL;
}
vtkIdType numKeys = array->GetNumberOfTuples();
if ( numKeys <= 0 )
{
return NULL;
}
// Create and initialize the sorting indices
vtkIdType *idx = vtkSortDataArray::InitializeSortIndices(numKeys);
// Sort and generate the sorting indices
void *dataIn = array->GetVoidPointer(0);
int dataType = array->GetDataType();
vtkSortDataArray::GenerateSortIndices(dataType, dataIn, numKeys,
numComp, k, idx);
// Now loop over all arrays in the field data. Those that are the
// same length as the sorting indices are processed. Otherwise they
// are skipped and remain unchanged.
int nc, numArrays = fd->GetNumberOfArrays();
for (int arrayNum=0; arrayNum < numArrays; ++arrayNum)
{
array = fd->GetAbstractArray(arrayNum);
if ( array != NULL && array->GetNumberOfTuples() == numKeys )
{//process the array
dataIn = array->GetVoidPointer(0);
dataType = array->GetDataType();
nc = array->GetNumberOfComponents();
vtkSortDataArray::ShuffleArray(idx, dataType, numKeys, nc,
array, dataIn, dir);
}
}
// Clean up
if ( retIndices )
{
return idx;
}
else
{
delete [] idx;
return NULL;
}
}
//-------------------------------------------------------------------------
void vtkSortFieldData::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
// vtkSortFieldData methods -------------------------------------------------------
|