TTK
Loading...
Searching...
No Matches
ttkFlattenMultiBlock.cpp
Go to the documentation of this file.
2
3#include <vtkFieldData.h>
4#include <vtkInformation.h>
5#include <vtkInformationVector.h>
6#include <vtkMultiBlockDataSet.h>
7#include <vtkNew.h>
8#include <vtkObjectFactory.h>
9#include <vtkUnsignedIntArray.h>
10
12
14 this->setDebugMsgPrefix("FlattenMultiBlock");
15 this->SetNumberOfInputPorts(1);
16 this->SetNumberOfOutputPorts(1);
17}
18
20 vtkInformation *info) {
21 if(port == 0) {
22 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkMultiBlockDataSet");
23 return 1;
24 }
25 return 0;
26}
27
29 vtkInformation *info) {
30 if(port == 0) {
31 info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
32 return 1;
33 }
34 return 0;
35}
36
37int ttkFlattenMultiBlock::RequestData(vtkInformation *ttkNotUsed(request),
38 vtkInformationVector **inputVector,
39 vtkInformationVector *outputVector) {
40
41 // Get input data (pointer to vtkDataObject, parent block id)
42 std::vector<std::pair<vtkDataObject *, size_t>> blocks{};
43
44 auto input = vtkMultiBlockDataSet::GetData(inputVector[0], 0);
45
46 // Sanity check
47 if(input == nullptr || input->GetNumberOfBlocks() == 0) {
48 this->printErr("No input block");
49 return 0;
50 }
51
52 for(size_t i = 0; i < input->GetNumberOfBlocks(); ++i) {
53 const auto topLvlBlock
54 = vtkMultiBlockDataSet::SafeDownCast(input->GetBlock(i));
55 if(topLvlBlock == nullptr) {
56 continue;
57 }
58 for(size_t j = 0; j < topLvlBlock->GetNumberOfBlocks(); ++j) {
59 blocks.emplace_back(topLvlBlock->GetBlock(j), i);
60 }
61 }
62
63 auto outputFlatBlocks = vtkMultiBlockDataSet::GetData(outputVector);
64
65 // Sanity check
66 if(blocks.empty()) {
67 this->printWrn("Not a hierarchy of vtkMultiBlockDataSet");
68 outputFlatBlocks->ShallowCopy(input);
69 return 1;
70 }
71
72 // Set output
73 outputFlatBlocks->SetNumberOfBlocks(blocks.size());
74
75 for(size_t i = 0; i < blocks.size(); ++i) {
76 // Set flattened blocks
77 outputFlatBlocks->SetBlock(i, blocks[i].first);
78
79 // Set FieldData
80 vtkNew<vtkUnsignedIntArray> blockId{};
81 blockId->SetName("ParentBlockId");
82 blockId->SetNumberOfTuples(1);
83 blockId->SetTuple1(0, blocks[i].second);
84
85 blocks[i].first->GetFieldData()->AddArray(blockId);
86 }
87
88 return 1;
89}
#define ttkNotUsed(x)
Mark function/method parameters that are not used in the function body at all.
Definition BaseClass.h:47
TTK processing package for flattening the top-level hierarchy of a tree vtkMultiBlockDataSet structur...
int FillOutputPortInformation(int port, vtkInformation *info) override
int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector) override
int FillInputPortInformation(int port, vtkInformation *info) override
int printWrn(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:159
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
int printErr(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:149
vtkStandardNewMacro(ttkFlattenMultiBlock)