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 <vtkCell.h>
#include "vtkUnstructuredGrid.h" #include <vtkPointData.h>
#include <vtkCellData.h>
#include <vtkUnstructuredGrid.h>
#include <algorithm> #include <algorithm>
#include <vtkActor.h> #include <vtkActor.h>
#include <vtkCamera.h> #include <vtkCamera.h>
@ -20,33 +22,14 @@
#include <vtkXMLPolyDataReader.h> #include <vtkXMLPolyDataReader.h>
#include <vtkPiecewiseFunction.h> #include <vtkPiecewiseFunction.h>
#include <vtkCellIterator.h> #include <vtkCellIterator.h>
#include <vtkUnstructuredGridAlgorithm.h>
#include <vtkDoubleArray.h>
#include <array> #include <array>
#include <vector> #include <vector>
#include <algorithm> #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) { void cellAngles(vtkDataSet *dataSet, vtkIdList *idList, double *angles) {
// std::cout << "nb points: " << idList->GetNumberOfIds() << std::endl; // std::cout << "nb points: " << idList->GetNumberOfIds() << std::endl;
double a[3], b[3], c[3], d[3]; 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) { int main(int argc, char **argv) {
if (argc != 2) { if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " FILE.vtk" << std::endl; std::cerr << "Usage: " << argv[0] << " FILE.vtk" << std::endl;
@ -108,28 +151,26 @@ int main(int argc, char **argv) {
// vtkNew<vtkXMLPolyDataReader> reader; // vtkNew<vtkXMLPolyDataReader> reader;
vtkNew<vtkUnstructuredGridReader> reader; vtkNew<vtkUnstructuredGridReader> reader;
reader->SetFileName(argv[1]); reader->SetFileName(argv[1]);
reader->Update(); // reader->Update();
vtkUnstructuredGrid *grid = reader->GetOutput(); vtkNew<AddAngleComputationAlgorithm> addAngles;
auto it = grid->NewCellIterator(); addAngles->SetInputConnection(reader->GetOutputPort());
std::vector<double> angles(grid->GetNumberOfCells() * 12); addAngles->Update();
size_t i = 0; vtkUnstructuredGrid *grid = addAngles->GetOutput();
for (it->InitTraversal(); !it->IsDoneWithTraversal(); it->GoToNextCell()) { auto *angles = vtkDoubleArray::SafeDownCast(grid->GetCellData()->GetArray("angles"));
if (it->GetCellType() != VTK_TETRA) continue; for (ssize_t i = 0; i < angles->GetNumberOfTuples(); i++) {
// double angles[12]; std::cout << "cell " << i << ": ";
cellAngles(grid, it->GetPointIds(), angles.data() + i); for (ssize_t j = 0; j < angles->GetNumberOfComponents(); j++) {
i += 12; std::cout << angles->GetTuple(i)[j] << ", ";
// for (size_t i = 0; i < 12; i++) { }
// std::cout << angles[i] << ", "; std::cout << "\b\b \n";
// }
// std::cout << "\b\b \n";
} }
std::cout << "avg: " << average(angles) // std::cout << "avg: " << average(angles)
<< ", stddev: " << standard_deviation(angles) // << ", stddev: " << standard_deviation(angles)
<< ", min: " << *std::min_element(angles.begin(), angles.end()) // << ", min: " << *std::min_element(angles.begin(), angles.end())
<< ", max: " << *std::max_element(angles.begin(), angles.end()) << std::endl; // << ", max: " << *std::max_element(angles.begin(), angles.end()) << std::endl;
vtkNew<vtkOpenGLProjectedTetrahedraMapper> volumeMapper; vtkNew<vtkOpenGLProjectedTetrahedraMapper> volumeMapper;
volumeMapper->SetInputConnection(reader->GetOutputPort()); volumeMapper->SetInputConnection(addAngles->GetOutputPort());
// vtkNew<vtkPolyDataMapper> mapper; // vtkNew<vtkPolyDataMapper> mapper;
// mapper->SetInputConnection(reader->GetOutputPort()); // mapper->SetInputConnection(reader->GetOutputPort());