OGS
VtkAppendArrayFilter.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ** VTK INCLUDES **
6
7#include <vtkCellData.h>
8#include <vtkDoubleArray.h>
9#include <vtkInformation.h>
10#include <vtkInformationVector.h>
11#include <vtkObjectFactory.h>
12#include <vtkPointData.h>
13#include <vtkSmartPointer.h>
14#include <vtkStreamingDemandDrivenPipeline.h>
15#include <vtkUnstructuredGrid.h>
16
17#include "BaseLib/Logging.h"
18
20
22
24
25void VtkAppendArrayFilter::PrintSelf(ostream& os, vtkIndent indent)
26{
27 this->Superclass::PrintSelf(os, indent);
28 os << indent << "== VtkAppendArrayFilter ==" << endl;
29}
30
31int VtkAppendArrayFilter::RequestData(vtkInformation* /*request*/,
32 vtkInformationVector** inputVector,
33 vtkInformationVector* outputVector)
34{
35 if (this->_array.empty())
36 {
37 ERR("VtkAppendArrayFilter::RequestData(): Selection array is empty.");
38 return 0;
39 }
40 vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
41 vtkUnstructuredGrid* input = vtkUnstructuredGrid::SafeDownCast(
42 inInfo->Get(vtkDataObject::DATA_OBJECT()));
43
44 vtkSmartPointer<vtkDoubleArray> colors =
45 vtkSmartPointer<vtkDoubleArray>::New();
46 colors->SetNumberOfComponents(1);
47 std::size_t arrayLength = this->_array.size();
48 colors->SetNumberOfValues(arrayLength);
49 colors->SetName("Selection");
50
51 std::size_t nCells = input->GetNumberOfCells();
52 if (nCells > arrayLength)
53 WARN(
54 "VtkAppendArrayFilter::RequestData(): Number of cells exceeds "
55 "selection array length. Surplus cells won't be examined.");
56
57 for (std::size_t i = 0; i < arrayLength; i++)
58 {
59 colors->SetValue(i, _array[i]);
60 }
61
62 vtkInformation* outInfo = outputVector->GetInformationObject(0);
63 vtkUnstructuredGrid* output = vtkUnstructuredGrid::SafeDownCast(
64 outInfo->Get(vtkDataObject::DATA_OBJECT()));
65 output->CopyStructure(input);
66 output->GetPointData()->PassData(input->GetPointData());
67 output->GetCellData()->PassData(input->GetCellData());
68 output->GetCellData()->AddArray(colors);
69 output->GetCellData()->SetActiveScalars(_array_name.c_str());
70
71 return 1;
72}
73
74void VtkAppendArrayFilter::SetArray(const std::string& array_name,
75 const std::vector<double>& new_array)
76{
77 this->_array_name = array_name;
78 this->_array = new_array;
79}
void ERR(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:40
void WARN(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:34
vtkStandardNewMacro(VtkAppendArrayFilter)
std::vector< double > _array
void SetArray(const std::string &array_name, const std::vector< double > &new_array)
~VtkAppendArrayFilter() override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
The filter logic.
void PrintSelf(ostream &os, vtkIndent indent) override
Prints the mesh data to an output stream.