MCPcopy Create free account
hub / github.com/Kitware/VTK / Sort

Method Sort

Common/DataModel/vtkSortFieldData.cxx:25–88  ·  view source on GitHub ↗

------------------------------------------------------------------------------ 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.

Source from the content-addressed store, hash-verified

23// field data and sort them. Initially we just need to generate the sort
24// indices which are then applied to each array in turn.
25vtkIdType* vtkSortFieldData::Sort(
26 vtkFieldData* fd, const char* arrayName, int k, int retIndices, int dir)
27{
28 // Verify the input
29 if (fd == nullptr || arrayName == nullptr)
30 {
31 vtkGenericWarningMacro("SortFieldData needs valid input");
32 return nullptr;
33 }
34 int pos;
35 vtkAbstractArray* array = fd->GetAbstractArray(arrayName, pos);
36 if (pos < 0)
37 {
38 vtkGenericWarningMacro("Sorting array not found.");
39 return nullptr;
40 }
41 int numComp = array->GetNumberOfComponents();
42 if (k < 0 || k >= numComp)
43 {
44 vtkGenericWarningMacro("Cannot sort by column "
45 << k << " since the array only has columns 0 through " << (numComp - 1));
46 return nullptr;
47 }
48 vtkIdType numKeys = array->GetNumberOfTuples();
49 if (numKeys <= 0)
50 {
51 return nullptr;
52 }
53
54 // Create and initialize the sorting indices
55 vtkIdType* idx = vtkSortDataArray::InitializeSortIndices(numKeys);
56
57 // Sort and generate the sorting indices
58 void* dataIn = array->GetVoidPointer(0);
59 int dataType = array->GetDataType();
60 vtkSortDataArray::GenerateSortIndices(dataType, dataIn, numKeys, numComp, k, idx);
61
62 // Now loop over all arrays in the field data. Those that are the
63 // same length as the sorting indices are processed. Otherwise they
64 // are skipped and remain unchanged.
65 int nc, numArrays = fd->GetNumberOfArrays();
66 for (int arrayNum = 0; arrayNum < numArrays; ++arrayNum)
67 {
68 array = fd->GetAbstractArray(arrayNum);
69 if (array != nullptr && array->GetNumberOfTuples() == numKeys)
70 { // process the array
71 dataIn = array->GetVoidPointer(0);
72 dataType = array->GetDataType();
73 nc = array->GetNumberOfComponents();
74 vtkSortDataArray::ShuffleArray(idx, dataType, numKeys, nc, array, dataIn, dir);
75 }
76 }
77
78 // Clean up
79 if (retIndices)
80 {
81 return idx;
82 }

Callers

nothing calls this directly

Calls 7

ShuffleArrayFunction · 0.85
GetAbstractArrayMethod · 0.80
GetNumberOfComponentsMethod · 0.45
GetNumberOfTuplesMethod · 0.45
GetVoidPointerMethod · 0.45
GetDataTypeMethod · 0.45
GetNumberOfArraysMethod · 0.45

Tested by

no test coverage detected