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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkMergeVectorComponents.h"
#include "vtkArrayDispatch.h"
#include "vtkDataArray.h"
#include "vtkDataArrayRange.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkExecutive.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkSMPTools.h"
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkMergeVectorComponents);
//------------------------------------------------------------------------------
vtkMergeVectorComponents::vtkMergeVectorComponents()
{
this->XArrayName = nullptr;
this->YArrayName = nullptr;
this->ZArrayName = nullptr;
this->OutputVectorName = nullptr;
this->AttributeType = vtkDataObject::POINT;
}
//------------------------------------------------------------------------------
vtkMergeVectorComponents::~vtkMergeVectorComponents()
{
this->SetXArrayName(nullptr);
this->SetYArrayName(nullptr);
this->SetZArrayName(nullptr);
this->SetOutputVectorName(nullptr);
}
//------------------------------------------------------------------------------
int vtkMergeVectorComponents::FillInputPortInformation(int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
return 1;
}
//------------------------------------------------------------------------------
namespace
{
template <typename ArrayTypeX, typename ArrayTypeY, typename ArrayTypeZ>
class MergeVectorComponentsFunctor
{
ArrayTypeX* ArrayX;
ArrayTypeY* ArrayY;
ArrayTypeZ* ArrayZ;
vtkDoubleArray* Vector;
vtkMergeVectorComponents* Filter;
public:
MergeVectorComponentsFunctor(ArrayTypeX* arrayX, ArrayTypeY* arrayY, ArrayTypeZ* arrayZ,
vtkDataArray* vector, vtkMergeVectorComponents* filter)
: ArrayX(arrayX)
, ArrayY(arrayY)
, ArrayZ(arrayZ)
, Vector(vtkDoubleArray::FastDownCast(vector))
, Filter(filter)
{
}
void operator()(vtkIdType begin, vtkIdType end)
{
// mark out ranges as single component for better perf
auto inX = vtk::DataArrayValueRange<1>(this->ArrayX, begin, end).begin();
auto inY = vtk::DataArrayValueRange<1>(this->ArrayY, begin, end).begin();
auto inZ = vtk::DataArrayValueRange<1>(this->ArrayZ, begin, end).begin();
auto outVector = vtk::DataArrayTupleRange<3>(this->Vector, begin, end);
bool isFirst = vtkSMPTools::GetSingleThread();
for (auto tuple : outVector)
{
if (isFirst)
{
this->Filter->CheckAbort();
}
if (this->Filter->GetAbortOutput())
{
break;
}
tuple[0] = *inX++;
tuple[1] = *inY++;
tuple[2] = *inZ++;
}
}
};
struct MergeVectorComponentsWorker
{
template <typename ArrayTypeX, typename ArrayTypeY, typename ArrayTypeZ>
void operator()(ArrayTypeX* arrayX, ArrayTypeY* arrayY, ArrayTypeZ* arrayZ, vtkDataArray* vector,
vtkMergeVectorComponents* filter)
{
MergeVectorComponentsFunctor<ArrayTypeX, ArrayTypeY, ArrayTypeZ> functor(
arrayX, arrayY, arrayZ, vector, filter);
vtkSMPTools::For(0, vector->GetNumberOfTuples(), functor);
}
};
} // namespace
//------------------------------------------------------------------------------
int vtkMergeVectorComponents::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector, vtkInformationVector* outputVector)
{
vtkDebugMacro(<< "Merging vector components...");
// check that the attribute type is set
if (AttributeType != vtkDataObject::POINT && AttributeType != vtkDataObject::CELL)
{
vtkErrorMacro(<< "No attribute-type is set!");
return 1;
}
// check that array names are set
if (this->XArrayName == nullptr || this->YArrayName == nullptr || this->ZArrayName == nullptr)
{
vtkErrorMacro(<< "No array names were set!");
return 1;
}
// get the input and output
vtkDataSet* input = vtkDataSet::SafeDownCast(vtkDataObject::GetData(inputVector[0], 0));
vtkDataSet* output = vtkDataSet::SafeDownCast(vtkDataObject::GetData(outputVector, 0));
output->CopyStructure(input);
vtkFieldData* inFD = input->GetAttributesAsFieldData(this->AttributeType);
vtkFieldData* outFD = output->GetAttributesAsFieldData(this->AttributeType);
// get the point-data arrays
vtkDataArray* xFD = inFD->GetArray(this->XArrayName);
vtkDataArray* yFD = inFD->GetArray(this->YArrayName);
vtkDataArray* zFD = inFD->GetArray(this->ZArrayName);
// check if the provided array names correspond to valid arrays
if ((xFD == nullptr || xFD->GetNumberOfTuples() < 1) ||
(yFD == nullptr || yFD->GetNumberOfTuples() < 1) ||
(zFD == nullptr || zFD->GetNumberOfTuples() < 1))
{
vtkErrorMacro(<< "No arrays with the provided names exist!");
return 1;
}
std::string outVectorName;
// if output vector name is unset, we define a default name
if (this->OutputVectorName == nullptr)
{
outVectorName = "combinationVector";
}
else
{
outVectorName = this->OutputVectorName;
}
vtkDataArray* vectorFD = vtkDataArray::CreateDataArray(VTK_DOUBLE);
vectorFD->SetNumberOfComponents(3);
vectorFD->SetNumberOfTuples(xFD->GetNumberOfTuples());
vectorFD->SetName(outVectorName.c_str());
using Dispatcher = vtkArrayDispatch::Dispatch3SameValueType;
if (!Dispatcher::Execute(xFD, yFD, zFD, MergeVectorComponentsWorker{}, vectorFD, this))
{
MergeVectorComponentsWorker{}(xFD, yFD, zFD, vectorFD, this);
}
// add array and copy field data of same type
outFD->PassData(inFD);
outFD->AddArray(vectorFD);
vectorFD->Delete();
// copy all the other attribute types
vtkFieldData *inOtherFD, *outOtherFD;
vtkDataObject::AttributeTypes otherAttributeType;
for (unsigned int i = 0; i < vtkDataObject::AttributeTypes::NUMBER_OF_ATTRIBUTE_TYPES; ++i)
{
otherAttributeType = static_cast<vtkDataObject::AttributeTypes>(i);
if (this->AttributeType != otherAttributeType)
{
inOtherFD = input->GetAttributesAsFieldData(otherAttributeType);
outOtherFD = output->GetAttributesAsFieldData(otherAttributeType);
// check that attribute type exists
if (inOtherFD != nullptr && outOtherFD != nullptr)
{
outOtherFD->PassData(inOtherFD);
}
}
}
return 1;
}
//------------------------------------------------------------------------------
void vtkMergeVectorComponents::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "XArrayName: " << (this->XArrayName ? this->XArrayName : "(nullptr)") << endl;
os << indent << "YArrayName: " << (this->YArrayName ? this->YArrayName : "(nullptr)") << endl;
os << indent << "ZArrayName: " << (this->ZArrayName ? this->ZArrayName : "(nullptr)") << endl;
os << indent
<< "OutputVectorName: " << (this->OutputVectorName ? this->OutputVectorName : "(nullptr)")
<< endl;
os << indent << "AttributeType: " << this->AttributeType << endl;
}
VTK_ABI_NAMESPACE_END
|