pfe/src/main.cc

115 lines
3.3 KiB
C++
Raw Normal View History

2022-02-17 14:02:20 +01:00
#include "angles_filter.h"
2022-02-17 17:30:04 +01:00
#include "aspect_ratio_filter.h"
2022-02-17 14:02:20 +01:00
#include <vtkCellData.h>
#include <vtkUnstructuredGrid.h>
2022-01-31 16:55:32 +01:00
#include <vtkCamera.h>
#include <vtkNamedColors.h>
2022-02-10 19:38:48 +01:00
#include <vtkVolumeProperty.h>
2022-01-31 16:55:32 +01:00
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
2022-02-09 16:24:57 +01:00
#include <vtkVolumeMapper.h>
#include <vtkVolume.h>
2022-02-10 19:38:48 +01:00
#include <vtkOpenGLProjectedTetrahedraMapper.h>
#include <vtkUnstructuredGridReader.h>
#include <vtkPiecewiseFunction.h>
2022-01-31 16:55:32 +01:00
#include <array>
#include <vector>
#include <algorithm>
2022-02-17 14:02:20 +01:00
// template <typename T>
// T average(const std::vector<T> &data) {
// T avg = 0;
// for (const T &t : data) {
// avg += t;
// }
// return avg / data.size();
// }
2022-02-17 14:02:20 +01:00
// 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());
// }
2022-02-09 16:24:57 +01:00
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " FILE.vtk" << std::endl;
return EXIT_FAILURE;
}
2022-02-17 17:30:04 +01:00
/* Reader */
2022-02-10 19:38:48 +01:00
vtkNew<vtkUnstructuredGridReader> reader;
2022-02-09 16:24:57 +01:00
reader->SetFileName(argv[1]);
2022-02-17 17:30:04 +01:00
/* Angle filter */
2022-02-17 14:02:20 +01:00
vtkNew<AnglesFilter> anglesFilter;
anglesFilter->SetInputConnection(reader->GetOutputPort());
2022-02-17 17:30:04 +01:00
/* Display angles in console. */
2022-02-17 14:02:20 +01:00
anglesFilter->Update();
vtkUnstructuredGrid *grid = anglesFilter->GetOutput();
auto *angles = 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";
}
2022-02-09 16:24:57 +01:00
2022-02-17 17:30:04 +01:00
/* Aspect ratio */
vtkNew<AspectRatioFilter> aspectRatioFilter;
aspectRatioFilter->SetInputConnection(anglesFilter->GetOutputPort());
/* Display aspect ratios in console. */
aspectRatioFilter->Update();
grid = aspectRatioFilter->GetOutput();
auto *aspectRatios = grid->GetCellData()->GetArray("aspect_ratio");
2022-02-17 17:30:04 +01:00
for (ssize_t i = 0; i < aspectRatios->GetNumberOfTuples(); i++) {
std::cout << "cell " << i << ": "
<< aspectRatios->GetTuple1(i) << "\n";
}
2022-01-31 16:55:32 +01:00
2022-02-17 17:30:04 +01:00
/* Volume rendering properties */
vtkNew<vtkOpenGLProjectedTetrahedraMapper> volumeMapper;
volumeMapper->SetInputConnection(aspectRatioFilter->GetOutputPort());
2022-02-10 19:38:48 +01:00
vtkNew<vtkVolume> volume;
volume->SetMapper(volumeMapper);
vtkNew<vtkPiecewiseFunction> transferFunction;
transferFunction->AddPoint(-1, 0);
transferFunction->AddPoint(1, 1);
2022-02-10 19:38:48 +01:00
volume->GetProperty()->SetScalarOpacity(transferFunction);
volume->GetProperty()->SetColor(transferFunction);
2022-01-31 16:55:32 +01:00
2022-02-17 17:30:04 +01:00
/* Renderer */
vtkNew<vtkNamedColors> colors;
std::array<unsigned char, 4> bkg{{26, 51, 102, 255}};
colors->SetColor("BkgColor", bkg.data());
2022-01-31 16:55:32 +01:00
vtkNew<vtkRenderer> renderer;
2022-02-10 19:38:48 +01:00
renderer->AddVolume(volume);
2022-01-31 16:55:32 +01:00
renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(300, 300);
renderWindow->AddRenderer(renderer);
2022-02-10 19:38:48 +01:00
renderWindow->SetWindowName("PFE");
2022-01-31 16:55:32 +01:00
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}