From 8955712d132c07f153acd7120918f1a821ed30e3 Mon Sep 17 00:00:00 2001 From: ccolin Date: Thu, 17 Feb 2022 14:02:20 +0100 Subject: [PATCH] refactor angle filter --- CMakeLists.txt | 6 +- src/add_angles_computation_algorithm.cc | 87 +++++++++++++++ src/add_angles_computation_algorithm.h | 25 +++++ src/main.cc | 134 ++++-------------------- 4 files changed, 136 insertions(+), 116 deletions(-) create mode 100644 src/add_angles_computation_algorithm.cc create mode 100644 src/add_angles_computation_algorithm.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a2cc230..c7dbc37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,11 @@ add_executable(pfe) target_compile_features(pfe PRIVATE cxx_std_17) -target_sources(pfe PRIVATE src/main.cc) +target_sources(pfe PRIVATE + src/main.cc + src/add_angles_computation_algorithm.cc + src/add_angles_computation_algorithm.h) + target_link_libraries(pfe PRIVATE VTK::CommonCore VTK::ViewsCore diff --git a/src/add_angles_computation_algorithm.cc b/src/add_angles_computation_algorithm.cc new file mode 100644 index 0000000..95759fa --- /dev/null +++ b/src/add_angles_computation_algorithm.cc @@ -0,0 +1,87 @@ +#include "add_angles_computation_algorithm.h" + +#include +#include +#include +#include +#include + + +vtkStandardNewMacro(AddAngleComputationAlgorithm); + + +vtkTypeBool AddAngleComputationAlgorithm::RequestData( + vtkInformation *request, + vtkInformationVector **inputVector, + vtkInformationVector *outputVector) { + (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; +} + + +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]; + dataSet->GetPoint(idList->GetId(0), a); + dataSet->GetPoint(idList->GetId(1), b); + dataSet->GetPoint(idList->GetId(2), c); + dataSet->GetPoint(idList->GetId(3), d); + // std::cout << "ids " << idList->GetId(0) + // << " " << idList->GetId(1) + // << " " << idList->GetId(2) + // << " " << idList->GetId(3) << std::endl; + // std::cout << "coords" << std::endl + // << a[0] << ", " << a[1] << ", " << a[2] << std::endl + // << b[0] << ", " << b[1] << ", " << b[2] << std::endl + // << c[0] << ", " << c[1] << ", " << c[2] << std::endl + // << d[0] << ", " << d[1] << ", " << d[2] << std::endl; + double ab[3], ac[3], ad[3], + ba[3], bc[3], bd[3], + ca[3], cb[3], cd[3], + da[3], db[3], dc[3]; + vtkMath::Subtract(b, a, ab); + vtkMath::Subtract(c, a, ac); + vtkMath::Subtract(d, a, ad); + vtkMath::Subtract(a, b, ba); + vtkMath::Subtract(c, b, bc); + vtkMath::Subtract(d, b, bd); + vtkMath::Subtract(a, c, ca); + vtkMath::Subtract(b, c, cb); + vtkMath::Subtract(d, c, cd); + vtkMath::Subtract(a, d, da); + vtkMath::Subtract(b, d, db); + vtkMath::Subtract(c, d, dc); + angles[0] = vtkMath::AngleBetweenVectors(ab, ac); + angles[1] = vtkMath::AngleBetweenVectors(ac, ad); + angles[2] = vtkMath::AngleBetweenVectors(ad, ab); + angles[3] = vtkMath::AngleBetweenVectors(ba, bc); + angles[4] = vtkMath::AngleBetweenVectors(bc, bd); + angles[5] = vtkMath::AngleBetweenVectors(bd, ba); + angles[6] = vtkMath::AngleBetweenVectors(ca, cb); + angles[7] = vtkMath::AngleBetweenVectors(cb, cd); + angles[8] = vtkMath::AngleBetweenVectors(cd, ca); + angles[9] = vtkMath::AngleBetweenVectors(da, db); + angles[10] = vtkMath::AngleBetweenVectors(db, dc); + angles[11] = vtkMath::AngleBetweenVectors(dc, da); +} diff --git a/src/add_angles_computation_algorithm.h b/src/add_angles_computation_algorithm.h new file mode 100644 index 0000000..40a5ebd --- /dev/null +++ b/src/add_angles_computation_algorithm.h @@ -0,0 +1,25 @@ +#ifndef ADD_ANGLES_COMPUTATION_ALGORITHM_H +#define ADD_ANGLES_COMPUTATION_ALGORITHM_H + + +#include +#include + + +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; + +protected: + ~AddAngleComputationAlgorithm() override = default; +}; + + +#endif diff --git a/src/main.cc b/src/main.cc index 33ad8b5..b7df902 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,15 +1,9 @@ -#include -#include +#include "add_angles_computation_algorithm.h" + #include #include -#include -#include #include -#include #include -#include -#include -#include #include #include #include @@ -18,11 +12,7 @@ #include #include #include -#include -#include #include -#include -#include #include #include @@ -30,111 +20,25 @@ #include -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]; - dataSet->GetPoint(idList->GetId(0), a); - dataSet->GetPoint(idList->GetId(1), b); - dataSet->GetPoint(idList->GetId(2), c); - dataSet->GetPoint(idList->GetId(3), d); - // std::cout << "ids " << idList->GetId(0) - // << " " << idList->GetId(1) - // << " " << idList->GetId(2) - // << " " << idList->GetId(3) << std::endl; - // std::cout << "coords" << std::endl - // << a[0] << ", " << a[1] << ", " << a[2] << std::endl - // << b[0] << ", " << b[1] << ", " << b[2] << std::endl - // << c[0] << ", " << c[1] << ", " << c[2] << std::endl - // << d[0] << ", " << d[1] << ", " << d[2] << std::endl; - double ab[3], ac[3], ad[3], - ba[3], bc[3], bd[3], - ca[3], cb[3], cd[3], - da[3], db[3], dc[3]; - vtkMath::Subtract(b, a, ab); - vtkMath::Subtract(c, a, ac); - vtkMath::Subtract(d, a, ad); - vtkMath::Subtract(a, b, ba); - vtkMath::Subtract(c, b, bc); - vtkMath::Subtract(d, b, bd); - vtkMath::Subtract(a, c, ca); - vtkMath::Subtract(b, c, cb); - vtkMath::Subtract(d, c, cd); - vtkMath::Subtract(a, d, da); - vtkMath::Subtract(b, d, db); - vtkMath::Subtract(c, d, dc); - angles[0] = vtkMath::AngleBetweenVectors(ab, ac); - angles[1] = vtkMath::AngleBetweenVectors(ac, ad); - angles[2] = vtkMath::AngleBetweenVectors(ad, ab); - angles[3] = vtkMath::AngleBetweenVectors(ba, bc); - angles[4] = vtkMath::AngleBetweenVectors(bc, bd); - angles[5] = vtkMath::AngleBetweenVectors(bd, ba); - angles[6] = vtkMath::AngleBetweenVectors(ca, cb); - angles[7] = vtkMath::AngleBetweenVectors(cb, cd); - angles[8] = vtkMath::AngleBetweenVectors(cd, ca); - angles[9] = vtkMath::AngleBetweenVectors(da, db); - angles[10] = vtkMath::AngleBetweenVectors(db, dc); - angles[11] = vtkMath::AngleBetweenVectors(dc, da); -} +// template +// T average(const std::vector &data) { +// T avg = 0; +// for (const T &t : data) { +// avg += t; +// } +// return avg / data.size(); +// } -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()); -} +// 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) {