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
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
// SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
#include <vtkDenseArray.h>
#include <vtkSmartPointer.h>
#include <vtkSparseArray.h>
#include <vtkTryDowncast.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <boost/algorithm/string.hpp>
#define VTK_CREATE(type, name) vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
#define test_expression(expression) \
{ \
if (!(expression)) \
{ \
std::ostringstream buffer; \
buffer << "Expression failed at line " << __LINE__ << ": " << #expression; \
throw std::runtime_error(buffer.str()); \
} \
}
class DowncastTest
{
public:
DowncastTest(int& count)
: Count(count)
{
}
template <typename T>
void operator()(T* vtkNotUsed(array)) const
{
++Count;
}
int& Count;
private:
DowncastTest& operator=(const DowncastTest&);
};
template <template <typename> class TargetT, typename TypesT>
void SuccessTest(vtkObject* source, int line)
{
int count = 0;
if (!vtkTryDowncast<TargetT, TypesT>(source, DowncastTest(count)))
{
std::ostringstream buffer;
buffer << "Expression failed at line " << line;
throw std::runtime_error(buffer.str());
}
if (count != 1)
{
std::ostringstream buffer;
buffer << "Functor was called " << count << " times at line " << line;
throw std::runtime_error(buffer.str());
}
}
template <template <typename> class TargetT, typename TypesT>
void FailTest(vtkObject* source, int line)
{
int count = 0;
if (vtkTryDowncast<TargetT, TypesT>(source, DowncastTest(count)))
{
std::ostringstream buffer;
buffer << "Expression failed at line " << line;
throw std::runtime_error(buffer.str());
}
if (count != 0)
{
std::ostringstream buffer;
buffer << "Functor was called " << count << " times at line " << line;
throw std::runtime_error(buffer.str());
}
}
/*
// This functor increments array values in-place using a parameter passed via the algorithm (instead
of a parameter
// stored in the functor). It can work with any numeric array type.
struct IncrementValues
{
template<typename T>
void operator()(T* array, int amount) const
{
for(vtkIdType n = 0; n != array->GetNonNullSize(); ++n)
array->SetValueN(n, array->GetValueN(n) + amount);
}
};
// This functor efficiently creates a transposed array. It's one example of how you can create an
output array
// with the same type as an input array.
struct Transpose
{
Transpose(vtkSmartPointer<vtkArray>& result_matrix) : ResultMatrix(result_matrix) {}
template<typename ValueT>
void operator()(vtkDenseArray<ValueT>* input) const
{
if(input->GetDimensions() != 2 || input->GetExtents()[0] != input->GetExtents()[1])
throw std::runtime_error("A square matrix is required.");
vtkDenseArray<ValueT>* output = vtkDenseArray<ValueT>::SafeDownCast(input->DeepCopy());
for(vtkIdType i = 0; i != input->GetExtents()[0]; ++i)
{
for(vtkIdType j = i + 1; j != input->GetExtents()[1]; ++j)
{
output->SetValue(i, j, input->GetValue(j, i));
output->SetValue(j, i, input->GetValue(i, j));
}
}
this->ResultMatrix = output;
}
vtkSmartPointer<vtkArray>& ResultMatrix;
};
*/
//
//
// Here are some examples of how the algorithm might be called.
//
//
int TestArrayCasting(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
try
{
VTK_CREATE(vtkDenseArray<int>, dense_int);
VTK_CREATE(vtkDenseArray<double>, dense_double);
VTK_CREATE(vtkDenseArray<vtkStdString>, dense_string);
VTK_CREATE(vtkSparseArray<int>, sparse_int);
VTK_CREATE(vtkSparseArray<double>, sparse_double);
VTK_CREATE(vtkSparseArray<vtkStdString>, sparse_string);
SuccessTest<vtkTypedArray, vtkIntegerTypes>(dense_int, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkIntegerTypes>(sparse_int, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkIntegerTypes>(sparse_string, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkFloatingPointTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(dense_string, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkFloatingPointTypes>(sparse_string, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(dense_double, __LINE__);
FailTest<vtkTypedArray, vtkNumericTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkNumericTypes>(sparse_double, __LINE__);
FailTest<vtkTypedArray, vtkNumericTypes>(sparse_string, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(dense_int, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(dense_double, __LINE__);
SuccessTest<vtkTypedArray, vtkStringTypes>(dense_string, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(sparse_int, __LINE__);
FailTest<vtkTypedArray, vtkStringTypes>(sparse_double, __LINE__);
SuccessTest<vtkTypedArray, vtkStringTypes>(sparse_string, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_int, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_double, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(dense_string, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_int, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_double, __LINE__);
SuccessTest<vtkTypedArray, vtkAllTypes>(sparse_string, __LINE__);
SuccessTest<vtkDenseArray, vtkAllTypes>(dense_int, __LINE__);
FailTest<vtkDenseArray, vtkAllTypes>(sparse_int, __LINE__);
FailTest<vtkSparseArray, vtkAllTypes>(dense_int, __LINE__);
SuccessTest<vtkSparseArray, vtkAllTypes>(sparse_int, __LINE__);
return 0;
}
catch (std::exception& e)
{
cerr << e.what() << endl;
return 1;
}
}
|