comparison Samples/Vtk/main.cpp @ 2:8f22ed9d48d5

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 01 Jun 2015 13:59:12 +0200
parents
children 6d59828e2662
comparison
equal deleted inserted replaced
1:fd402e53d263 2:8f22ed9d48d5
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include <iostream>
22
23 #include <vtkRenderWindow.h>
24 #include <vtkImageData.h>
25 #include <vtkPiecewiseFunction.h>
26 #include <vtkFixedPointVolumeRayCastMapper.h>
27 #include <vtkColorTransferFunction.h>
28 #include <vtkVolumeProperty.h>
29 #include <vtkRenderWindowInteractor.h>
30 #include <vtkRenderer.h>
31 #include <vtkSmartPointer.h>
32 #include <vtkOpenGLRenderer.h>
33 #include <vtkInteractorStyleTrackballCamera.h>
34
35 #include <orthanc/OrthancCppClient.h>
36
37
38 void Display(OrthancClient::Series& series)
39 {
40 /**
41 * Load the 3D image from Orthanc into VTK.
42 **/
43
44 vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
45 image->SetDimensions(series.GetWidth(), series.GetHeight(), series.GetInstanceCount());
46 image->SetScalarType(VTK_SHORT);
47 image->AllocateScalars();
48
49 if (series.GetWidth() != 0 &&
50 series.GetHeight() != 0 &&
51 series.GetInstanceCount() != 0)
52 {
53 series.Load3DImage(image->GetScalarPointer(0, 0, 0), Orthanc::PixelFormat_SignedGrayscale16,
54 2 * series.GetWidth(), 2 * series.GetHeight() * series.GetWidth());
55 }
56
57 image->SetSpacing(series.GetVoxelSizeX(),
58 series.GetVoxelSizeY(),
59 series.GetVoxelSizeZ());
60
61
62 /**
63 * The following code is based on the VTK sample for MIP
64 * http://www.vtk.org/Wiki/VTK/Examples/Cxx/VolumeRendering/MinIntensityRendering
65 **/
66
67 // Create a transfer function mapping scalar value to opacity
68 double range[2];
69 image->GetScalarRange(range);
70
71 vtkSmartPointer<vtkPiecewiseFunction> opacityTransfer =
72 vtkSmartPointer<vtkPiecewiseFunction>::New();
73 opacityTransfer->AddSegment(range[0], 0.0, range[1], 1.0);
74
75 vtkSmartPointer<vtkColorTransferFunction> colorTransfer =
76 vtkSmartPointer<vtkColorTransferFunction>::New();
77 colorTransfer->AddRGBPoint(0, 1.0, 1.0, 1.0);
78 colorTransfer->AddRGBPoint(range[1], 1.0, 1.0, 1.0);
79
80 vtkSmartPointer<vtkVolumeProperty> property =
81 vtkSmartPointer<vtkVolumeProperty>::New();
82 property->SetScalarOpacity(opacityTransfer);
83 property->SetColor(colorTransfer);
84 property->SetInterpolationTypeToLinear();
85
86 // Create a Maximum Intensity Projection rendering
87 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper =
88 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
89 mapper->SetBlendModeToMaximumIntensity();
90 mapper->SetInput(image);
91
92 vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
93 volume->SetMapper(mapper);
94 volume->SetProperty(property);
95
96 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkOpenGLRenderer>::New();
97 renderer->AddViewProp(volume);
98 renderer->SetBackground(0.1, 0.2, 0.3); // Background color dark blue
99
100 vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
101 vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
102
103 vtkSmartPointer<vtkRenderWindow> window = vtkSmartPointer<vtkRenderWindow>::New();
104 window->AddRenderer(renderer);
105
106 vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
107 interactor->SetRenderWindow(window);
108 interactor->SetInteractorStyle(style);
109 interactor->Start();
110 }
111
112
113 int main()
114 {
115 try
116 {
117 // The following explicit initialization is not required, except
118 // if you wish to specify the full path to the shared library
119 OrthancClient::Initialize();
120
121 // Use the commented code below if you know the identifier of a
122 // series that corresponds to a 3D image.
123
124 /*
125 {
126 OrthancClient::OrthancConnection orthanc("http://localhost:8042");
127 OrthancClient::Series series(orthanc, "dc5ec3d9-6e1a7b2c-73a829f0-64c609f6-ef976a97");
128 Display(series);
129 return 0;
130 }
131 */
132
133
134 // Try and find a 3D image inside the local store
135 OrthancClient::OrthancConnection orthanc("http://localhost:8042");
136
137 for (unsigned int i = 0; i < orthanc.GetPatientCount(); i++)
138 {
139 OrthancClient::Patient patient(orthanc.GetPatient(i));
140 std::cout << "Patient: " << patient.GetId() << std::endl;
141
142 for (unsigned int j = 0; j < patient.GetStudyCount(); j++)
143 {
144 OrthancClient::Study study(patient.GetStudy(j));
145 std::cout << " Study: " << study.GetId() << std::endl;
146
147 for (unsigned int k = 0; k < study.GetSeriesCount(); k++)
148 {
149 OrthancClient::Series series(study.GetSeries(k));
150 std::cout << " Series: " << series.GetId() << std::endl;
151
152 if (series.Is3DImage())
153 {
154 Display(series);
155 return 0;
156 }
157 else
158 {
159 std::cout << " => Not a 3D image..." << std::endl;
160 }
161 }
162 }
163 }
164
165 std::cout << "Unable to find a 3D image in the local Orthanc store" << std::endl;
166
167 return 0;
168 }
169 catch (OrthancClient::OrthancClientException& e)
170 {
171 std::cerr << "EXCEPTION: [" << e.What() << "]" << std::endl;
172 return -1;
173 }
174 }