File size: 4,180 Bytes
614b8d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <vtkCamera.h>
#include <vtkColorTransferFunction.h>
#include <vtkGPUVolumeRayCastMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPiecewiseFunction.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredPointsReader.h>
#include <vtkVolume.h>
#include <vtkVolumeProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkPNGWriter.h>
#include <vtkWindowToImageFilter.h>
#include <vtkMultiThreader.h>
#define OTF_COMB()\
opacityTransferFunction->AddPoint(0.0, 0.0);\
opacityTransferFunction->AddPoint(1.0, 1.0);
#define CTF_COMB()\
colorTransferFunction->AddRGBPoint(0.0, 0.5, 0.0, 0.0);\
colorTransferFunction->AddRGBPoint(0.17, 0.0, 0.0, 1.0);\
colorTransferFunction->AddRGBPoint(0.33, 0.0, 1.0, 1.0);\
colorTransferFunction->AddRGBPoint(0.5, 0.0, 1.0, 0.0);\
colorTransferFunction->AddRGBPoint(0.67, 1.0, 1.0, 0.0);\
colorTransferFunction->AddRGBPoint(0.83, 1.0, 0.5, 0.0);\
colorTransferFunction->AddRGBPoint(1.0, 1.0, 0.0, 0.0);
int main(int argc, char* argv[])
{
vtkMultiThreader::SetGlobalMaximumNumberOfThreads(1);
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " dataset.vtk" << std::endl;
return EXIT_FAILURE;
}
// Create the standard renderer, render window and interactor
vtkNew<vtkRenderer> ren1;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren1);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
// Create the reader for the data
vtkNew<vtkStructuredPointsReader> reader;
reader->SetFileName(argv[1]);
reader->Update();
// Create transfer mapping scalar value to opacity
vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New();
OTF_COMB();
// Create transfer mapping scalar value to color
vtkNew<vtkColorTransferFunction> colorTransferFunction;
CTF_COMB()
// The property describes how the data will look
vtkNew<vtkVolumeProperty> volumeProperty;
volumeProperty->SetColor(colorTransferFunction);
volumeProperty->SetScalarOpacity(opacityTransferFunction);
volumeProperty->SetInterpolationTypeToLinear();
// The mapper / ray cast function know how to render the data
vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper;
volumeMapper->SetInputConnection(reader->GetOutputPort());
volumeMapper->SetSampleDistance(0.1);
volumeMapper->SetBlendModeToComposite();
// The volume holds the mapper and the property
vtkNew<vtkVolume> volume;
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
ren1->AddVolume(volume);
// ren1->SetBackground(0, 0, 0);
ren1->SetBackground(1, 1, 1);
ren1->ResetCameraClippingRange();
ren1->ResetCamera();
// set camera parameters from arguments
double position_x = 650;
double position_y = 650;
double position_z = 650;
double focal_point_x = 128;
double focal_point_y = 128;
double focal_point_z = 128;
double view_up_x = 1;
double view_up_y = 0;
double view_up_z = 0;
ren1->GetActiveCamera()->SetPosition(position_x,
position_y,
position_z);
ren1->GetActiveCamera()->SetFocalPoint(focal_point_x,
focal_point_y,
focal_point_z);
ren1->GetActiveCamera()->SetViewUp(view_up_x,
view_up_y,
view_up_z);
ren1->GetActiveCamera()->SetViewAngle(30);
ren1->GetActiveCamera()->SetClippingRange(0.1, 1000);
renWin->SetSize(1024, 1024);
renWin->SetWindowName("Rendering Result");
renWin->Render();
// Screenshot
vtkNew<vtkWindowToImageFilter> windowToImageFilter;
windowToImageFilter->SetInput(iren->GetRenderWindow());
windowToImageFilter->SetScale(1);
windowToImageFilter->SetInputBufferTypeToRGB();
windowToImageFilter->ReadFrontBufferOff();
vtkNew<vtkPNGWriter> writer;
std::string fileName = "../miranda_gs.png";
writer->SetFileName(fileName.c_str());
writer->SetInputConnection(windowToImageFilter->GetOutputPort());
writer->Write();
std::cout << "=======Saving rendering Done!" << endl;
// Turn on for interaction
// iren->Start();
return EXIT_SUCCESS;
}
|