| #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; |
| } |
|
|
| |
| vtkNew<vtkRenderer> ren1; |
|
|
| vtkNew<vtkRenderWindow> renWin; |
| renWin->AddRenderer(ren1); |
|
|
| vtkNew<vtkRenderWindowInteractor> iren; |
| iren->SetRenderWindow(renWin); |
|
|
| |
| vtkNew<vtkStructuredPointsReader> reader; |
| reader->SetFileName(argv[1]); |
| reader->Update(); |
|
|
| |
| vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction = vtkSmartPointer<vtkPiecewiseFunction>::New(); |
| OTF_COMB(); |
|
|
| |
| vtkNew<vtkColorTransferFunction> colorTransferFunction; |
| CTF_COMB() |
|
|
| |
| vtkNew<vtkVolumeProperty> volumeProperty; |
| volumeProperty->SetColor(colorTransferFunction); |
| volumeProperty->SetScalarOpacity(opacityTransferFunction); |
|
|
| volumeProperty->SetInterpolationTypeToLinear(); |
|
|
| |
| vtkNew<vtkGPUVolumeRayCastMapper> volumeMapper; |
| volumeMapper->SetInputConnection(reader->GetOutputPort()); |
| volumeMapper->SetSampleDistance(0.1); |
| volumeMapper->SetBlendModeToComposite(); |
|
|
| |
| vtkNew<vtkVolume> volume; |
| volume->SetMapper(volumeMapper); |
| volume->SetProperty(volumeProperty); |
|
|
| ren1->AddVolume(volume); |
| |
| ren1->SetBackground(1, 1, 1); |
|
|
| ren1->ResetCameraClippingRange(); |
| ren1->ResetCamera(); |
| |
| |
| 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(); |
|
|
| |
| 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; |
|
|
| |
| |
|
|
| return EXIT_SUCCESS; |
| } |
|
|