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
|
/*=========================================================================
Program: Visualization Toolkit
Module: DistributedData.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.
=========================================================================*/
// This test focus on noticed buggy behaviour based on the smallest basis
#include "vtkTestUtilities.h"
#include "vtkSortedTableStreamer.h"
#include "vtkTable.h"
#include "vtkDoubleArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkSmartPointer.h"
#include "vtkMultiProcessController.h"
#include "vtkDummyController.h"
#include <float.h>
// ----------------------------------------------------------------------------
void fillArray(vtkDoubleArray* array, double* dataPointer, int dataSize, const char* name)
{
array->SetName(name);
array->SetNumberOfComponents(1);
array->Allocate(dataSize);
for(int i=0;i<dataSize;i++)
{
array->InsertNextTuple1(dataPointer[i]);
}
}
// ----------------------------------------------------------------------------
// Return true if both array are equals
bool compareArray(vtkTable* table, const char* arrayName, double* dataPointer, int dataSize, bool print)
{
vtkDoubleArray* array = vtkDoubleArray::SafeDownCast(table->GetColumnByName(arrayName));
if(!array)
return false;
if(dataSize != array->GetNumberOfTuples())
return false;
for(int i=0;i<dataSize;i++)
{
if(print) cout << "Sorted value: " << array->GetValue(i) << " expected " << dataPointer[i] << endl;
if(array->GetValue(i) != dataPointer[i])
return false;
}
return true;
}
// ----------------------------------------------------------------------------
int sortWithSimilarValues(bool debug)
{
const int size = 10;
double dataArray[size] = { 0,1,2,1,3,1,3,1,2,100000 };
double sortedArray[size] = { 0,1,1,1,1,2,2,3,3,100000 };
vtkSmartPointer<vtkDoubleArray> dataToSort = vtkSmartPointer<vtkDoubleArray>::New();
fillArray(dataToSort.GetPointer(), dataArray, size, "data");
vtkSmartPointer<vtkTable> input = vtkSmartPointer<vtkTable>::New();
input->AddColumn(dataToSort);;
vtkSmartPointer<vtkSortedTableStreamer> sortingfilter = vtkSmartPointer<vtkSortedTableStreamer>::New();
sortingfilter->SetInputData(input.GetPointer());
sortingfilter->SetSelectedComponent(0);
sortingfilter->SetColumnNameToSort("data");
sortingfilter->SetBlock(0);
sortingfilter->SetBlockSize(1024);
sortingfilter->Update();
if(!compareArray(sortingfilter->GetOutput(), "data", sortedArray, size, debug))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// ----------------------------------------------------------------------------
int sortWithEpsilonValues(bool debug)
{
double epsilon = 1000 * FLT_EPSILON; // FIXME why the delta need to be so big ???
const int size = 10;
double dataArray[size];
double sortedArray[size];
for(int i=0;i<size;i++)
{
dataArray[size - i - 1] = sortedArray[i] = 10 + epsilon * i ;
}
vtkSmartPointer<vtkDoubleArray> dataToSort = vtkSmartPointer<vtkDoubleArray>::New();
fillArray(dataToSort.GetPointer(), dataArray, size, "data");
vtkSmartPointer<vtkTable> input = vtkSmartPointer<vtkTable>::New();
input->AddColumn(dataToSort);;
vtkSmartPointer<vtkSortedTableStreamer> sortingfilter = vtkSmartPointer<vtkSortedTableStreamer>::New();
sortingfilter->SetInputData(input.GetPointer());
sortingfilter->SetSelectedComponent(0);
sortingfilter->SetColumnNameToSort("data");
sortingfilter->SetBlock(0);
sortingfilter->SetBlockSize(1024);
sortingfilter->Update();
if(!compareArray(sortingfilter->GetOutput(), "data", sortedArray, size, debug))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// ----------------------------------------------------------------------------
int sortMagnitudeOnUnsignedCharVector()
{
vtkSmartPointer<vtkUnsignedCharArray> dataToSort = vtkSmartPointer<vtkUnsignedCharArray>::New();
dataToSort->SetNumberOfComponents(3);
dataToSort->SetName("data");
dataToSort->Allocate(3 * 128 * 128 * 128);
for(int r=0;r<256;r+=2)
{
for(int g=0;g<256;g+=2)
{
for(int b=0;b<256;b+=2)
{
dataToSort->InsertNextTuple3(r,g,b);
}
}
}
vtkSmartPointer<vtkTable> input = vtkSmartPointer<vtkTable>::New();
input->AddColumn(dataToSort);;
vtkSmartPointer<vtkSortedTableStreamer> sortingfilter = vtkSmartPointer<vtkSortedTableStreamer>::New();
sortingfilter->SetInputData(input.GetPointer());
sortingfilter->SetSelectedComponent(-1); // Magnitude
sortingfilter->SetColumnNameToSort("data");
sortingfilter->SetBlock(0);
sortingfilter->SetBlockSize(1024);
sortingfilter->Update();
return EXIT_SUCCESS;
}
// ----------------------------------------------------------------------------
int TestSortingTable(int vtkNotUsed(argc), char **vtkNotUsed(argv))
{
// Create Fake MPI controller
vtkDummyController* ctrl = vtkDummyController::New();
vtkMultiProcessController::SetGlobalController(ctrl);
int result = 0;
bool debug = false;
// --------------------------------------------------------------------------
cout << "Testing sorting with similar values: "
<< ((result += sortWithSimilarValues(debug)) ? "FAILED" : "SUCCESS")
<< endl;
// --------------------------------------------------------------------------
cout << "Testing sorting with epsilon values: "
<< ((result += sortWithEpsilonValues(debug)) ? "FAILED" : "SUCCESS")
<< endl;
// --------------------------------------------------------------------------
cout << "Testing sorting with magnitude on unsigned char: "
<< ((result += sortMagnitudeOnUnsignedCharVector())
? "FAILED" : "SUCCESS")
<< endl;
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
// Delete Fake MPI controller
vtkMultiProcessController::SetGlobalController(0);
ctrl->Delete();
return result;
}
|