From 41502f0c33a07159e6bea4d463d9c05b66737e5d Mon Sep 17 00:00:00 2001 From: ccolin Date: Tue, 15 Feb 2022 14:41:02 +0100 Subject: [PATCH] switch to using a filter and storing angles in the vtk data object --- src/main.cc | 125 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 83 insertions(+), 42 deletions(-) diff --git a/src/main.cc b/src/main.cc index ecda49a..33ad8b5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,5 +1,7 @@ -#include "vtkCell.h" -#include "vtkUnstructuredGrid.h" +#include +#include +#include +#include #include #include #include @@ -20,33 +22,14 @@ #include #include #include +#include +#include #include #include #include -template -T average(const std::vector &data) { - T avg = 0; - for (const T &t : data) { - avg += t; - } - return avg / data.size(); -} - - -template -T standard_deviation(const std::vector &data) { - T avg = average(data); - T stddev = 0; - for (const T &t : data) { - stddev += (t - avg) * (t - avg); - } - return std::sqrt(stddev / data.size()); -} - - void cellAngles(vtkDataSet *dataSet, vtkIdList *idList, double *angles) { // std::cout << "nb points: " << idList->GetNumberOfIds() << std::endl; double a[3], b[3], c[3], d[3]; @@ -94,6 +77,66 @@ void cellAngles(vtkDataSet *dataSet, vtkIdList *idList, double *angles) { } +class AddAngleComputationAlgorithm : public vtkUnstructuredGridAlgorithm { +public: + static AddAngleComputationAlgorithm *New(); + vtkTypeMacro(AddAngleComputationAlgorithm, vtkUnstructuredGridAlgorithm); + + vtkTypeBool RequestData(vtkInformation *request, + vtkInformationVector **inputVector, + vtkInformationVector *outputVector) override { + (void) request; + vtkUnstructuredGrid* input = + vtkUnstructuredGrid::GetData(inputVector[0]); + vtkUnstructuredGrid* output = + vtkUnstructuredGrid::GetData(outputVector); + output->CopyStructure(input); + output->GetPointData()->PassData(input->GetPointData()); + vtkCellData *cellData = output->GetCellData(); + cellData->PassData(input->GetCellData()); + vtkNew anglesArray; + anglesArray->SetName("angles"); + anglesArray->SetNumberOfComponents(12); + anglesArray->SetNumberOfTuples(input->GetNumberOfCells()); + double *anglesBase = anglesArray->GetPointer(0); + size_t i = 0; + auto it = input->NewCellIterator(); + for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) { + if (it->GetCellType() != VTK_TETRA) continue; + cellAngles(input, it->GetPointIds(), anglesBase + i); + i += 12; + } + cellData->AddArray((vtkAbstractArray *) anglesArray); + return true; + } + +protected: + ~AddAngleComputationAlgorithm() override = default; +}; + +vtkStandardNewMacro(AddAngleComputationAlgorithm); + +template +T average(const std::vector &data) { + T avg = 0; + for (const T &t : data) { + avg += t; + } + return avg / data.size(); +} + + +template +T standard_deviation(const std::vector &data) { + T avg = average(data); + T stddev = 0; + for (const T &t : data) { + stddev += (t - avg) * (t - avg); + } + return std::sqrt(stddev / data.size()); +} + + int main(int argc, char **argv) { if (argc != 2) { std::cerr << "Usage: " << argv[0] << " FILE.vtk" << std::endl; @@ -108,28 +151,26 @@ int main(int argc, char **argv) { // vtkNew reader; vtkNew reader; reader->SetFileName(argv[1]); - reader->Update(); - vtkUnstructuredGrid *grid = reader->GetOutput(); - auto it = grid->NewCellIterator(); - std::vector angles(grid->GetNumberOfCells() * 12); - size_t i = 0; - for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) { - if (it->GetCellType() != VTK_TETRA) continue; - // double angles[12]; - cellAngles(grid, it->GetPointIds(), angles.data() + i); - i += 12; - // for (size_t i = 0; i < 12; i++) { - // std::cout << angles[i] << ", "; - // } - // std::cout << "\b\b \n"; + // reader->Update(); + vtkNew addAngles; + addAngles->SetInputConnection(reader->GetOutputPort()); + addAngles->Update(); + vtkUnstructuredGrid *grid = addAngles->GetOutput(); + auto *angles = vtkDoubleArray::SafeDownCast(grid->GetCellData()->GetArray("angles")); + for (ssize_t i = 0; i < angles->GetNumberOfTuples(); i++) { + std::cout << "cell " << i << ": "; + for (ssize_t j = 0; j < angles->GetNumberOfComponents(); j++) { + std::cout << angles->GetTuple(i)[j] << ", "; + } + std::cout << "\b\b \n"; } - std::cout << "avg: " << average(angles) - << ", stddev: " << standard_deviation(angles) - << ", min: " << *std::min_element(angles.begin(), angles.end()) - << ", max: " << *std::max_element(angles.begin(), angles.end()) << std::endl; + // std::cout << "avg: " << average(angles) + // << ", stddev: " << standard_deviation(angles) + // << ", min: " << *std::min_element(angles.begin(), angles.end()) + // << ", max: " << *std::max_element(angles.begin(), angles.end()) << std::endl; vtkNew volumeMapper; - volumeMapper->SetInputConnection(reader->GetOutputPort()); + volumeMapper->SetInputConnection(addAngles->GetOutputPort()); // vtkNew mapper; // mapper->SetInputConnection(reader->GetOutputPort());