refactor angle filter

This commit is contained in:
ccolin 2022-02-17 14:02:20 +01:00
parent 41502f0c33
commit 8955712d13
4 changed files with 136 additions and 116 deletions

View File

@ -8,7 +8,11 @@ add_executable(pfe)
target_compile_features(pfe PRIVATE cxx_std_17) 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 target_link_libraries(pfe PRIVATE
VTK::CommonCore VTK::CommonCore
VTK::ViewsCore VTK::ViewsCore

View File

@ -0,0 +1,87 @@
#include "add_angles_computation_algorithm.h"
#include <vtkUnstructuredGrid.h>
#include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkCellIterator.h>
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<vtkDoubleArray> 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);
}

View File

@ -0,0 +1,25 @@
#ifndef ADD_ANGLES_COMPUTATION_ALGORITHM_H
#define ADD_ANGLES_COMPUTATION_ALGORITHM_H
#include <vtkUnstructuredGridAlgorithm.h>
#include <vtkIdList.h>
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

View File

@ -1,15 +1,9 @@
#include <vtkCell.h> #include "add_angles_computation_algorithm.h"
#include <vtkPointData.h>
#include <vtkCellData.h> #include <vtkCellData.h>
#include <vtkUnstructuredGrid.h> #include <vtkUnstructuredGrid.h>
#include <algorithm>
#include <vtkActor.h>
#include <vtkCamera.h> #include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h> #include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkVolumeProperty.h> #include <vtkVolumeProperty.h>
#include <vtkRenderWindow.h> #include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h> #include <vtkRenderWindowInteractor.h>
@ -18,11 +12,7 @@
#include <vtkVolume.h> #include <vtkVolume.h>
#include <vtkOpenGLProjectedTetrahedraMapper.h> #include <vtkOpenGLProjectedTetrahedraMapper.h>
#include <vtkUnstructuredGridReader.h> #include <vtkUnstructuredGridReader.h>
#include <vtkPolyDataReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkPiecewiseFunction.h> #include <vtkPiecewiseFunction.h>
#include <vtkCellIterator.h>
#include <vtkUnstructuredGridAlgorithm.h>
#include <vtkDoubleArray.h> #include <vtkDoubleArray.h>
#include <array> #include <array>
@ -30,111 +20,25 @@
#include <algorithm> #include <algorithm>
void cellAngles(vtkDataSet *dataSet, vtkIdList *idList, double *angles) { // template <typename T>
// std::cout << "nb points: " << idList->GetNumberOfIds() << std::endl; // T average(const std::vector<T> &data) {
double a[3], b[3], c[3], d[3]; // T avg = 0;
dataSet->GetPoint(idList->GetId(0), a); // for (const T &t : data) {
dataSet->GetPoint(idList->GetId(1), b); // avg += t;
dataSet->GetPoint(idList->GetId(2), c); // }
dataSet->GetPoint(idList->GetId(3), d); // return avg / data.size();
// 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);
}
class AddAngleComputationAlgorithm : public vtkUnstructuredGridAlgorithm { // template <typename T>
public: // T standard_deviation(const std::vector<T> &data) {
static AddAngleComputationAlgorithm *New(); // T avg = average(data);
vtkTypeMacro(AddAngleComputationAlgorithm, vtkUnstructuredGridAlgorithm); // T stddev = 0;
// for (const T &t : data) {
vtkTypeBool RequestData(vtkInformation *request, // stddev += (t - avg) * (t - avg);
vtkInformationVector **inputVector, // }
vtkInformationVector *outputVector) override { // return std::sqrt(stddev / data.size());
(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<vtkDoubleArray> 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 <typename T>
T average(const std::vector<T> &data) {
T avg = 0;
for (const T &t : data) {
avg += t;
}
return avg / data.size();
}
template <typename T>
T standard_deviation(const std::vector<T> &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) { int main(int argc, char **argv) {