comparison Resources/Samples/OrthancCppClient/Vtk/main.cpp @ 481:4f5b4b0fa626

VTK sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 16 Jul 2013 10:10:55 +0200
parents
children 733b24a00c26
comparison
equal deleted inserted replaced
480:482cde3f3c14 481:4f5b4b0fa626
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
4 * Belgium
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 **/
26
27
28 #include <iostream>
29
30 #include <vtkRenderWindow.h>
31 #include <vtkImageData.h>
32 #include <vtkPiecewiseFunction.h>
33 #include <vtkFixedPointVolumeRayCastMapper.h>
34 #include <vtkColorTransferFunction.h>
35 #include <vtkVolumeProperty.h>
36 #include <vtkRenderWindowInteractor.h>
37 #include <vtkRenderer.h>
38 #include <vtkSmartPointer.h>
39 #include <vtkOpenGLRenderer.h>
40 #include <vtkInteractorStyleTrackballCamera.h>
41
42 #include "../../../../OrthancCppClient/OrthancConnection.h"
43
44
45 class DisplayProgress : public Orthanc::ThreadedCommandProcessor::IListener
46 {
47 public:
48 virtual void SignalProgress(unsigned int current,
49 unsigned int total)
50 {
51 std::cout << "Slice loaded (" << current << "/" << total << ")" << std::endl;
52 }
53
54 virtual void SignalSuccess(unsigned int total)
55 {
56 std::cout << "Success loading image (" << total << " images)" << std::endl;
57 }
58
59 virtual void SignalCancel()
60 {
61 }
62
63 virtual void SignalFailure()
64 {
65 std::cout << "Error loading image" << std::endl;
66 }
67 };
68
69
70
71 void Display(OrthancClient::Series& series)
72 {
73 /**
74 * Load the 3D image from Orthanc into VTK.
75 **/
76
77 vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
78 image->SetDimensions(series.GetWidth(), series.GetHeight(), series.GetInstanceCount());
79 image->SetScalarType(VTK_SHORT);
80 image->AllocateScalars();
81
82 if (series.GetWidth() != 0 &&
83 series.GetHeight() != 0 &&
84 series.GetInstanceCount() != 0)
85 {
86 DisplayProgress listener;
87 series.Load3DImage(image->GetScalarPointer(0, 0, 0), Orthanc::PixelFormat_SignedGrayscale16,
88 2 * series.GetWidth(), 2 * series.GetHeight() * series.GetWidth(), listener);
89 }
90
91 float sx, sy, sz;
92 series.GetVoxelSize(sx, sy, sz);
93 image->SetSpacing(sx, sy, sz);
94
95
96 /**
97 * The following code is based on the VTK sample for MIP
98 * http://www.vtk.org/Wiki/VTK/Examples/Cxx/VolumeRendering/MinIntensityRendering
99 **/
100
101 // Create a transfer function mapping scalar value to opacity
102 double range[2];
103 image->GetScalarRange(range);
104
105 vtkSmartPointer<vtkPiecewiseFunction> opacityTransfer =
106 vtkSmartPointer<vtkPiecewiseFunction>::New();
107 opacityTransfer->AddSegment(range[0], 0.0, range[1], 1.0);
108
109 vtkSmartPointer<vtkColorTransferFunction> colorTransfer =
110 vtkSmartPointer<vtkColorTransferFunction>::New();
111 colorTransfer->AddRGBPoint(0, 1.0, 1.0, 1.0);
112 colorTransfer->AddRGBPoint(range[1], 1.0, 1.0, 1.0);
113
114 vtkSmartPointer<vtkVolumeProperty> property =
115 vtkSmartPointer<vtkVolumeProperty>::New();
116 property->SetScalarOpacity(opacityTransfer);
117 property->SetColor(colorTransfer);
118 property->SetInterpolationTypeToLinear();
119
120 // Create a Maximum Intensity Projection rendering
121 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper> mapper =
122 vtkSmartPointer<vtkFixedPointVolumeRayCastMapper>::New();
123 mapper->SetBlendModeToMaximumIntensity();
124 mapper->SetInput(image);
125
126 vtkSmartPointer<vtkVolume> volume = vtkSmartPointer<vtkVolume>::New();
127 volume->SetMapper(mapper);
128 volume->SetProperty(property);
129
130 vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkOpenGLRenderer>::New();
131 renderer->AddViewProp(volume);
132 renderer->SetBackground(0.1, 0.2, 0.3); // Background color dark blue
133
134 vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
135 vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
136
137 vtkSmartPointer<vtkRenderWindow> window = vtkSmartPointer<vtkRenderWindow>::New();
138 window->AddRenderer(renderer);
139
140 vtkSmartPointer<vtkRenderWindowInteractor> interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
141 interactor->SetRenderWindow(window);
142 interactor->SetInteractorStyle(style);
143 interactor->Start();
144 }
145
146
147 int main()
148 {
149 // Try and find a 3D image inside the local store
150 OrthancClient::OrthancConnection orthanc("http://localhost:8042");
151
152 for (unsigned int i = 0; i < orthanc.GetPatientCount(); i++)
153 {
154 OrthancClient::Patient& patient = orthanc.GetPatient(i);
155 std::cout << "Patient: " << patient.GetId() << std::endl;
156
157 for (unsigned int j = 0; j < patient.GetStudyCount(); j++)
158 {
159 OrthancClient::Study& study = patient.GetStudy(j);
160 std::cout << " Study: " << study.GetId() << std::endl;
161
162 for (unsigned int k = 0; k < study.GetSeriesCount(); k++)
163 {
164 OrthancClient::Series& series = study.GetSeries(k);
165 std::cout << " Series: " << series.GetId() << std::endl;
166
167 if (series.Is3DImage())
168 {
169 Display(series);
170 return 0;
171 }
172 else
173 {
174 std::cout << " => Not a 3D image..." << std::endl;
175 }
176 }
177 }
178 }
179
180 std::cout << "Unable to find a 3D image in the local Orthanc store" << std::endl;
181
182 return 0;
183 }