Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Derived from VTK/Examples/Cxx/Medical4.cxx
- // This example reads a volume dataset and displays it via volume rendering.
- //
- #include <vtkCamera.h>
- #include <vtkColorTransferFunction.h>
- #include <vtkFixedPointVolumeRayCastMapper.h>
- #include <vtkMetaImageReader.h>
- #include <vtkNamedColors.h>
- #include <vtkPiecewiseFunction.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkSmartPointer.h>
- #include <vtkVolume.h>
- #include <vtkVolumeProperty.h>
- #include <vtkCallbackCommand.h>
- #include <array>
- vtkSmartPointer<vtkColorTransferFunction>volumeColor;
- vtkSmartPointer<vtkPiecewiseFunction> volumeScalarOpacity;
- vtkSmartPointer<vtkPiecewiseFunction> volumeGradientOpacity;
- vtkSmartPointer<vtkVolumeProperty> volumeProperty;
- vtkSmartPointer<vtkRenderWindow> renWin;
- int step = 4;
- //static int isoValue_ = 0;
- void KeypressCallbackFunction(vtkObject* caller, long unsigned int vtkNotUsed(eventId), void* vtkNotUsed(clientData), void* vtkNotUsed(callData))
- {
- std::cout << "Keypress callback" << std::endl;
- vtkRenderWindowInteractor *iren =
- static_cast<vtkRenderWindowInteractor*>(caller);
- std::cout << "Pressed: " << iren->GetKeySym() << std::endl;
- std::string key = iren->GetKeySym();
- if (key == "Up") {
- step++;
- std::cout << "Steps Up: " << step << std::endl;
- int isoValue_ = 100;
- double stepValue = (double)1 / step;
- for (int i = 0; i < step; i++) {
- double r = stepValue * 2 * i;
- double b = stepValue / 2 * i;
- if (r > 1.0) r = 1.0;
- volumeScalarOpacity->AddPoint(isoValue_, 0);
- volumeGradientOpacity->AddPoint(isoValue_, stepValue * i);
- volumeColor->AddRGBPoint(isoValue_, r, stepValue * i, b);
- isoValue_ += 50;
- }
- volumeProperty->SetColor(volumeColor);
- volumeProperty->SetScalarOpacity(volumeScalarOpacity);
- volumeProperty->SetGradientOpacity(volumeGradientOpacity);
- volumeProperty->SetInterpolationTypeToLinear();
- volumeProperty->ShadeOn();
- renWin->Render();
- }
- else if (key == "Down") {
- if (step > 0) {
- step--;
- std::cout << "Steps Down: " << step << std::endl;
- int isoValue_ = 100;
- double stepValue = (double)1 / step;
- for (int i = 0; i < step; i++) {
- double r = stepValue * 2 * i;
- double b = stepValue / 2 * i;
- if (r > 1.0) r = 1.0;
- volumeScalarOpacity->AddPoint(isoValue_, stepValue * i);
- volumeGradientOpacity->AddPoint(isoValue_, stepValue * i);
- volumeColor->AddRGBPoint(isoValue_, r, stepValue * i, b);
- isoValue_ += 50;
- }
- volumeProperty->SetColor(volumeColor);
- volumeProperty->SetScalarOpacity(volumeScalarOpacity);
- volumeProperty->SetGradientOpacity(volumeGradientOpacity);
- volumeProperty->SetInterpolationTypeToLinear();
- volumeProperty->ShadeOn();
- renWin->Render();
- }
- }
- }
- int main(int argc, char *argv[])
- {
- if (argc < 2)
- {
- cout << "Usage: " << argv[0] << "file.mhd" << endl;
- return EXIT_FAILURE;
- }
- vtkSmartPointer<vtkNamedColors> colors =
- vtkSmartPointer<vtkNamedColors>::New();
- std::array<unsigned char, 4> bkg{ { 51, 77, 102, 255 } };
- colors->SetColor("BkgColor", bkg.data());
- // Create the renderer, the render window, and the interactor. The renderer
- // draws into the render window, the interactor enables mouse- and
- // keyboard-based interaction with the scene.
- vtkSmartPointer<vtkRenderer> ren =
- vtkSmartPointer<vtkRenderer>::New();
- renWin = vtkSmartPointer<vtkRenderWindow>::New();
- renWin->AddRenderer(ren);
- vtkSmartPointer<vtkRenderWindowInteractor> iren =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- iren->SetRenderWindow(renWin);
- //******************************************************//
- vtkSmartPointer<vtkCallbackCommand> keypressCallback =
- vtkSmartPointer<vtkCallbackCommand>::New();
- keypressCallback->SetCallback(KeypressCallbackFunction);
- iren->AddObserver(vtkCommand::KeyPressEvent, keypressCallback);
- //******************************************************//
- // The following reader is used to read a series of 2D slices (images)
- // that compose the volume. The slice dimensions are set, and the
- // pixel spacing. The data Endianness must also be specified. The reader
- // uses the FilePrefix in combination with the slice number to construct
- // filenames using the format FilePrefix.%d. (In this case the FilePrefix
- // is the root name of the file: quarter.)
- vtkSmartPointer<vtkMetaImageReader> reader =
- vtkSmartPointer<vtkMetaImageReader>::New();
- reader->SetFileName(argv[1]);
- // The volume will be displayed by ray-cast alpha compositing.
- // A ray-cast mapper is needed to do the ray-casting.
- vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> volumeMapper =
- vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
- volumeMapper->SetInputConnection(reader->GetOutputPort());
- // The color transfer function maps voxel intensities to colors.
- // It is modality-specific, and often anatomy-specific as well.
- // The goal is to one color for flesh (between 500 and 1000)
- // and another color for bone (1150 and over).
- volumeColor = vtkSmartPointer<vtkColorTransferFunction>::New();
- volumeColor->AddRGBPoint(0, 0.0, 0.0, 0.0);
- volumeColor->AddRGBPoint(500, 1.0, 0.5, 0.3); //0.0, 1.0, 0.0
- volumeColor->AddRGBPoint(1000, 1.0, 0.5, 0.3);
- volumeColor->AddRGBPoint(1150, 1.0, 1.0, 0.9);
- // The opacity transfer function is used to control the opacity
- // of different tissue types.
- volumeScalarOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
- volumeScalarOpacity->AddPoint(0, 0.00);
- volumeScalarOpacity->AddPoint(500, 0.15);
- volumeScalarOpacity->AddPoint(1000, 0.15);
- volumeScalarOpacity->AddPoint(1150, 0.85);
- // The gradient opacity function is used to decrease the opacity
- // in the "flat" regions of the volume while maintaining the opacity
- // at the boundaries between tissue types. The gradient is measured
- // as the amount by which the intensity changes over unit distance.
- // For most medical data, the unit distance is 1mm.
- volumeGradientOpacity = vtkSmartPointer<vtkPiecewiseFunction>::New();
- volumeGradientOpacity->AddPoint(0, 0.0);
- volumeGradientOpacity->AddPoint(500, 0.15);
- volumeGradientOpacity->AddPoint(1000, 0.15);
- volumeGradientOpacity->AddPoint(1150, 0.85);
- // The VolumeProperty attaches the color and opacity functions to the
- // volume, and sets other volume properties. The interpolation should
- // be set to linear to do a high-quality rendering. The ShadeOn option
- // turns on directional lighting, which will usually enhance the
- // appearance of the volume and make it look more "3D". However,
- // the quality of the shading depends on how accurately the gradient
- // of the volume can be calculated, and for noisy data the gradient
- // estimation will be very poor. The impact of the shading can be
- // decreased by increasing the Ambient coefficient while decreasing
- // the Diffuse and Specular coefficient. To increase the impact
- // of shading, decrease the Ambient and increase the Diffuse and Specular.
- volumeProperty = vtkSmartPointer<vtkVolumeProperty>::New();
- volumeProperty->SetColor(volumeColor);
- volumeProperty->SetScalarOpacity(volumeScalarOpacity);
- volumeProperty->SetGradientOpacity(volumeGradientOpacity);
- volumeProperty->SetInterpolationTypeToLinear();
- volumeProperty->ShadeOn();
- volumeProperty->SetAmbient(0.4);
- volumeProperty->SetDiffuse(0.6);
- volumeProperty->SetSpecular(0.2);
- // The vtkVolume is a vtkProp3D (like a vtkActor) and controls the position
- // and orientation of the volume in world coordinates.
- vtkSmartPointer<vtkVolume> volume =
- vtkSmartPointer<vtkVolume>::New();
- volume->SetMapper(volumeMapper);
- volume->SetProperty(volumeProperty);
- // Finally, add the volume to the renderer
- ren->AddViewProp(volume);
- // Set up an initial view of the volume. The focal point will be the
- // center of the volume, and the camera position will be 400mm to the
- // patient's left (which is our right).
- vtkCamera *camera = ren->GetActiveCamera();
- double *c = volume->GetCenter();
- camera->SetViewUp(0, 0, -1);
- camera->SetPosition(c[0], c[1] - 400, c[2]);
- camera->SetFocalPoint(c[0], c[1], c[2]);
- camera->Azimuth(30.0);
- camera->Elevation(30.0);
- // Set a background color for the renderer
- ren->SetBackground(colors->GetColor3d("BkgColor").GetData());
- // Increase the size of the render window
- renWin->SetSize(640, 480);
- // Interact with the data.
- iren->Start();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement