switch to using a filter and storing angles in the vtk data object

This commit is contained in:
ccolin 2022-02-15 14:41:02 +01:00
parent b894ac2d95
commit 41502f0c33

View File

@ -1,5 +1,7 @@
#include "vtkCell.h"
#include "vtkUnstructuredGrid.h"
#include <vtkCell.h>
#include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkUnstructuredGrid.h>
#include <algorithm>
#include <vtkActor.h>
#include <vtkCamera.h>
@ -20,33 +22,14 @@
#include <vtkXMLPolyDataReader.h>
#include <vtkPiecewiseFunction.h>
#include <vtkCellIterator.h>
#include <vtkUnstructuredGridAlgorithm.h>
#include <vtkDoubleArray.h>
#include <array>
#include <vector>
#include <algorithm>
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());
}
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<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) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " FILE.vtk" << std::endl;
@ -108,28 +151,26 @@ int main(int argc, char **argv) {
// vtkNew<vtkXMLPolyDataReader> reader;
vtkNew<vtkUnstructuredGridReader> reader;
reader->SetFileName(argv[1]);
reader->Update();
vtkUnstructuredGrid *grid = reader->GetOutput();
auto it = grid->NewCellIterator();
std::vector<double> 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<AddAngleComputationAlgorithm> 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 << "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 << "\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;
vtkNew<vtkOpenGLProjectedTetrahedraMapper> volumeMapper;
volumeMapper->SetInputConnection(reader->GetOutputPort());
volumeMapper->SetInputConnection(addAngles->GetOutputPort());
// vtkNew<vtkPolyDataMapper> mapper;
// mapper->SetInputConnection(reader->GetOutputPort());