1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #include "vvBinaryImageOverlayActor.h"
21 #include <vtkImageActor.h>
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkMarchingSquares.h>
25 #include <vtkImageClip.h>
26 #include <vtkImageData.h>
27 #include <vtkPolyDataMapper.h>
28 #include <vtkProperty.h>
29 #include <vtkImageMapToRGBA.h>
30 #include <vtkLookupTable.h>
32 //------------------------------------------------------------------------------
33 vvBinaryImageOverlayActor::vvBinaryImageOverlayActor() {
41 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 vvBinaryImageOverlayActor::~vvBinaryImageOverlayActor() {
46 for (unsigned int i = 0; i < mSlicer->GetImage()->GetVTKImages().size(); i++) {
47 mSlicer->GetRenderer()->RemoveActor(mImageActorList[i]);
49 mImageActorList.clear();
52 //------------------------------------------------------------------------------
55 //------------------------------------------------------------------------------
56 void vvBinaryImageOverlayActor::setColor(double r, double g, double b) {
61 //------------------------------------------------------------------------------
64 //------------------------------------------------------------------------------
65 void vvBinaryImageOverlayActor::setSlicer(vvSlicer * slicer) {
68 //------------------------------------------------------------------------------
71 //------------------------------------------------------------------------------
72 void vvBinaryImageOverlayActor::initialize() {
74 std::cerr << "ERROR. Please use setSlicer before setSlicer in vvBinaryImageOverlayActor." << std::endl;
79 std::cerr << "ERROR. Please use setImage before setSlicer in vvBinaryImageOverlayActor." << std::endl;
82 // Create an actor for each time slice
83 for (unsigned int numImage = 0; numImage < mSlicer->GetImage()->GetVTKImages().size(); numImage++) {
86 // how many intensity ?
89 vtkImageMapToRGBA * mOverlayMapper = vtkImageMapToRGBA::New();
90 mOverlayMapper->SetInput(mImage->GetVTKImages()[0]); // DS TODO : to change if it is 4D !!!
91 vtkLookupTable * lut = vtkLookupTable::New();
94 lut->SetNumberOfTableValues(2);
95 lut->SetTableValue(mBackgroundValue, 0, 0, 0, 0.0); // BG
96 lut->SetTableValue(1, mColor[0], mColor[1], mColor[2], mAlpha); // FG
98 mOverlayMapper->SetLookupTable(lut);
100 vtkImageActor * mOverlayActor = vtkImageActor::New();
101 mOverlayActor->SetInput(mOverlayMapper->GetOutput());
102 mOverlayActor->SetPickable(0);
103 mOverlayActor->SetVisibility(true);
104 //mOverlayActor->SetOpacity(1.0);
106 mMapperList.push_back(mOverlayMapper);
107 mImageActorList.push_back(mOverlayActor);
108 mSlicer->GetRenderer()->AddActor(mOverlayActor);
111 //------------------------------------------------------------------------------
114 //------------------------------------------------------------------------------
115 void vvBinaryImageOverlayActor::setImage(vvImage::Pointer image, double bg) {
117 mBackgroundValue = bg;
119 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 void vvBinaryImageOverlayActor::hideActors() {
124 if (!mSlicer) return;
125 mSlice = mSlicer->GetSlice();
126 for(unsigned int i=0; i<mImageActorList.size(); i++) {
127 mImageActorList[i]->VisibilityOff();
130 //------------------------------------------------------------------------------
133 //------------------------------------------------------------------------------
134 void vvBinaryImageOverlayActor::showActors() {
135 if (!mSlicer) return;
136 mSlice = mSlicer->GetSlice();
137 mTSlice = mSlicer->GetTSlice();
138 // for(unsigned int i=0; i<mSquaresActorList.size(); i++) {
139 mImageActorList[mTSlice]->VisibilityOn();
143 //------------------------------------------------------------------------------
146 //------------------------------------------------------------------------------
147 void vvBinaryImageOverlayActor::update(int slicer, int slice) {
148 if (!mSlicer) return;
150 if (mPreviousSlice == mSlicer->GetSlice()) {
151 if (mPreviousTSlice == mSlicer->GetTSlice()) {
152 //DD("=========== NOTHING");
153 return; // Nothing to do
158 mSlice = mSlicer->GetSlice();
159 mTSlice = mSlicer->GetTSlice();
162 int * imageExtent = mSlicer->GetExtent();
163 int orientation = mSlicer->GetOrientation();
165 ComputeExtent(orientation, mSlice, imageExtent, maskExtent);
166 //ComputeExtent(imageExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
167 ComputeExtent(maskExtent, maskExtent, mSlicer->GetImage()->GetFirstVTKImageData(), mImage->GetFirstVTKImageData());
168 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
169 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
170 mSlicer->ClipDisplayedExtent(maskExtent, mMapperList[mTSlice]->GetInput()->GetWholeExtent());
171 // std::cout << "maskExtent = " << maskExtent[0] << " " << maskExtent[1] << " " << maskExtent[2] << " "
172 // << maskExtent[3] << " " << maskExtent[4] << " " << maskExtent[5] << std::endl;
173 SetDisplayExtentAndCameraPosition(orientation, mSlice, maskExtent, mImageActorList[mTSlice], 0.0);
175 // set previous slice
176 mPreviousTSlice = mSlicer->GetTSlice();
177 mPreviousSlice = mSlicer->GetSlice();
179 //------------------------------------------------------------------------------
182 //------------------------------------------------------------------------------
183 void vvBinaryImageOverlayActor::ComputeExtent(int orientation,
187 // std::cout << "InExtent = " << inExtent[0] << " " << inExtent[1] << " " << inExtent[2] << " "
188 // << inExtent[3] << " " << inExtent[4] << " " << inExtent[5] << std::endl;
190 switch (orientation) {
191 case vtkImageViewer2::SLICE_ORIENTATION_XY:
192 for(int i=0; i<4; i++) outExtent[i] = inExtent[i];
193 outExtent[4] = outExtent[5] = slice;
195 case vtkImageViewer2::SLICE_ORIENTATION_XZ:
196 for(int i=0; i<2; i++) outExtent[i] = inExtent[i];
197 for(int i=4; i<6; i++) outExtent[i] = inExtent[i];
198 outExtent[2] = outExtent[3] = slice;
200 case vtkImageViewer2::SLICE_ORIENTATION_YZ:
201 for(int i=2; i<6; i++) outExtent[i] = inExtent[i];
202 outExtent[0] = outExtent[1] = slice;
205 // std::cout << "OutExtent = " << outExtent[0] << " " << outExtent[1] << " " << outExtent[2] << " "
206 // << outExtent[3] << " " << outExtent[4] << " " << outExtent[5] << std::endl;
208 //------------------------------------------------------------------------------
211 //----------------------------------------------------------------------------
212 void vvBinaryImageOverlayActor::ComputeExtent(int * inExtent, int * outExtent, vtkImageData * image, vtkImageData * overlay)
214 outExtent[0] = (( image->GetOrigin()[0] + inExtent[0]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
215 overlay->GetSpacing()[0];
216 outExtent[1] = (( image->GetOrigin()[0] + inExtent[1]*image->GetSpacing()[0] ) - overlay->GetOrigin()[0]) /
217 overlay->GetSpacing()[0];
218 outExtent[2] = (( image->GetOrigin()[1] + inExtent[2]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
219 overlay->GetSpacing()[1];
220 outExtent[3] = (( image->GetOrigin()[1] + inExtent[3]*image->GetSpacing()[1] ) - overlay->GetOrigin()[1]) /
221 overlay->GetSpacing()[1];
222 outExtent[4] = (( image->GetOrigin()[2] + inExtent[4]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
223 overlay->GetSpacing()[2];
224 outExtent[5] = (( image->GetOrigin()[2] + inExtent[5]*image->GetSpacing()[2] ) - overlay->GetOrigin()[2]) /
225 overlay->GetSpacing()[2];
227 //----------------------------------------------------------------------------
230 //------------------------------------------------------------------------------
231 void vvBinaryImageOverlayActor::SetDisplayExtentAndCameraPosition(int orientation,
234 vtkImageActor * actor,
236 actor->SetDisplayExtent(extent);
239 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XY) {
240 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[2] > slice)
241 actor->SetPosition(0,0, position);
243 actor->SetPosition(0,0, -position);
245 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_XZ) {
246 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[1] > slice)
247 actor->SetPosition(0,position,0);
249 actor->SetPosition(0,-position,0);
251 if (orientation == vtkImageViewer2::SLICE_ORIENTATION_YZ) {
252 if (mSlicer->GetRenderer()->GetActiveCamera()->GetPosition()[0] > slice)
253 actor->SetPosition(position,0, 0);
255 actor->SetPosition(-position,0, 0);
259 //------------------------------------------------------------------------------