Fraxinus  17.12
An IGT application
cxVolumetricRep.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 
34 #include "cxVolumetricRep.h"
35 
36 #include <vtkPiecewiseFunction.h>
37 #include <vtkColorTransferFunction.h>
38 #include <vtkVolumeProperty.h>
39 
40 #include <vtkSmartVolumeMapper.h>
41 
42 //#if VTK_MINOR_VERSION >= 6
43  #include <vtkGPUVolumeRayCastMapper.h>
44 //#endif
45 
46 #include <vtkImageData.h>
47 #include <vtkVolume.h>
48 #include <vtkRenderer.h>
49 #include <vtkMatrix4x4.h>
50 
51 #include "cxView.h"
52 #include "cxImage.h"
53 #include "cxImageTF3D.h"
54 #include "cxSlicePlaneClipper.h"
55 #include "cxTypeConversions.h"
56 #include "vtkForwardDeclarations.h"
57 
58 #include "cxImageMapperMonitor.h"
59 
60 #include "cxVolumeProperty.h"
61 
62 typedef vtkSmartPointer<class vtkGPUVolumeRayCastMapper> vtkGPUVolumeRayCastMapperPtr;
63 
64 namespace cx
65 {
68  mVolume(vtkVolumePtr::New()),
69  mVolumeProperty(cx::VolumeProperty::create()),
70  mMaxVoxels(0)
71 {
73  mVolume->SetProperty(mVolumeProperty->getVolumeProperty());
74 }
75 
77 {}
78 
80 {
81 //#if (( VTK_MINOR_VERSION >= 6 )||( VTK_MAJOR_VERSION >5 ))
82  vtkGPUVolumeRayCastMapperPtr mapper = vtkGPUVolumeRayCastMapperPtr::New();
83  mMapper = mapper;
84  mMapper->SetBlendModeToComposite();
85  mVolume->SetMapper( mMapper );
86 //#endif
87 }
88 
89 //TODO: Use vtkSmartVolumeMapper instead
91 {
92  vtkVolumeTextureMapper3DPtr mapper = vtkVolumeTextureMapper3DPtr::New();
93  mMapper = mapper;
94 
95  // from snws
96 #ifndef CX_VTK_OPENGL2
97  mapper->SetPreferredMethodToNVidia();
98 #endif
99  mMapper->SetBlendModeToComposite();
100 
101  mVolume->SetMapper( mMapper );
102 }
103 
105 {
106  view->getRenderer()->AddVolume(mVolume);
107 }
108 
110 {
111  view->getRenderer()->RemoveVolume(mVolume);
112 }
113 
115 {
116  return mImage;
117 }
118 
120 {
121  if (image==mImage)
122  {
123  return;
124  }
125 
126  if (mImage)
127  {
128  mVolumeProperty->setImage(ImagePtr());
131  mMonitor.reset();
132  mMapper->SetInputData( (vtkImageData*)NULL );
133  }
134 
135  mImage = image;
136 
137  if (mImage)
138  {
141  mVolumeProperty->setImage(mImage);
142  this->vtkImageDataChangedSlot();
144  }
145 }
146 
148 {
149  return (mImage == image);
150 }
151 
156 {
157  this->updateVtkImageDataSlot();
158  this->transformChangedSlot();
159 }
160 
164 {
165  if (!mImage)
166  {
167  return;
168  }
169  mVolume->SetUserMatrix(mImage->get_rMd().getVtkMatrix());
170 }
171 
173 {
174  if (!mImage)
175  return;
176 
177  vtkImageDataPtr volume = mImage->resample(this->mMaxVoxels);
178  mMapper->SetInputData(volume);
179 }
180 
182 {
183  mMaxVoxels = maxVoxels;
184 }
185 
186 
187 //---------------------------------------------------------
188 } // namespace cx
189 //---------------------------------------------------------
vtkSmartPointer< class vtkGPUVolumeRayCastMapper > vtkGPUVolumeRayCastMapperPtr
vtkSmartPointer< class vtkVolume > vtkVolumePtr
virtual void addRepActorsToViewRenderer(ViewPtr view)
vtkVolumeMapperPtr mMapper
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
static ImageMapperMonitorPtr create(vtkVolumePtr volume, ImagePtr image)
void transformChanged()
emitted when transform is changed
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
long mMaxVoxels
always resample volume below this size.
boost::shared_ptr< class View > ViewPtr
cx::VolumePropertyPtr mVolumeProperty
virtual ImagePtr getImage()
get the reps image
Display a volume in 3D.
virtual bool hasImage(ImagePtr image) const
check if the reps has the image
virtual void setImage(ImagePtr image)
set the reps image
vtkVolumePtr mVolume
void setUseGPUVolumeRayCastMapper()
void setUseVolumeTextureMapper()
void vtkImageDataChanged()
emitted when the vktimagedata are invalidated and must be retrieved anew.
vtkSmartPointer< class vtkVolumeTextureMapper3D > vtkVolumeTextureMapper3DPtr
void setMaxVolumeSize(long maxVoxels)
set max volume size for rendering. Must be set before setImage()
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
cx::ImageMapperMonitorPtr mMonitor
helper object for visualizing clipping/cropping
Namespace for all CustusX production code.