]> Creatis software - bbtk.git/blobdiff - packages/vtk/src/bbvtkImagePlanes.cxx
bbtk now depends on crea !
[bbtk.git] / packages / vtk / src / bbvtkImagePlanes.cxx
index c80aad8bfa36459e54991a59d7e2affac85febbb..277d346dce5172d75313595742d36bd061b5184b 100644 (file)
@@ -1,20 +1,33 @@
-/*=========================================================================
-                                                                                
+/*=========================================================================                                                                               
   Program:   bbtk
   Module:    $RCSfile: bbvtkImagePlanes.cxx,v $
   Language:  C++
-  Date:      $Date: 2008/10/08 14:46:44 $
-  Version:   $Revision: 1.7 $
-                                                                                
-  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
-  l'Image). All rights reserved. See Doc/License.txt or
-  http://www.creatis.insa-lyon.fr/Public/bbtk/License.html for details.
-                                                                                
-     This software is distributed WITHOUT ANY WARRANTY; without even
-     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
-     PURPOSE.  See the above copyright notices for more information.
-                                                                                
+  Date:      $Date: 2008/12/09 12:54:05 $
+  Version:   $Revision: 1.16 $
 =========================================================================*/
+
+/* ---------------------------------------------------------------------
+
+* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
+* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
+*
+*  This software is governed by the CeCILL-B license under French law and 
+*  abiding by the rules of distribution of free software. You can  use, 
+*  modify and/ or redistribute the software under the terms of the CeCILL-B 
+*  license as circulated by CEA, CNRS and INRIA at the following URL 
+*  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
+*  or in the file LICENSE.txt.
+*
+*  As a counterpart to the access to the source code and  rights to copy,
+*  modify and redistribute granted by the license, users are provided only
+*  with a limited warranty  and the software's author,  the holder of the
+*  economic rights,  and the successive licensors  have only  limited
+*  liability. 
+*
+*  The fact that you are presently reading this means that you have had
+*  knowledge of the CeCILL-B license and that you accept its terms.
+* ------------------------------------------------------------------------ */                                                                         
+
 /**
  *  \file 
  *  \brief 
 #include "vtkCellPicker.h"
 #include "vtkProperty.h"
 
+#include "vtkMetaImageWriter.h"
+#include "vtkPNGWriter.h"
+
 #include "bbstdCast.h"
+#include <vtkCommand.h>
+
+#include "vtkImageData.h"
+//#include "vtkOutlineFilter.h"
+//#include "vtkPolyDataMapper.h"
+//#include "vtkActor.h"
+#include "vtkImagePlaneWidget.h"
+#include "vtkCellPicker.h"
+//#include "vtkProperty.h"
+
+//#include "vtkRenderer.h"
+//#include "vtkCamera.h"
+
+#include "vtkPlaneWidget.h"
+
+
 namespace bbstd
 {
+
   //====================================================================
   BBTK_BLACK_BOX_TEMPLATE2_IMPLEMENTATION(Cast,
                                          bbtk::AtomicBlackBox);
@@ -38,6 +71,8 @@ namespace bbstd
 using namespace bbstd;
 namespace bbvtk
 {
+
+
   //====================================================================
   // Add the specialized adaptors to the package
   typedef vtkImagePlaneWidget* I;
@@ -50,42 +85,81 @@ namespace bbvtk
 namespace bbvtk
 {
 
+  //================================================================
+ class myCallbackPlane : public vtkCommand
+ {
+ public:
+   static myCallbackPlane *New()
+      {
+         return new myCallbackPlane;
+      }
+   virtual void Execute(vtkObject *caller, unsigned long, void*)
+   {
+       currentBlackBox->Process();
+       currentBlackBox->bbSignalOutputModification();
+   } 
+   void SetCurrentBlackBox(ImagePlanes *cBB) {currentBlackBox = cBB;};    
+   void SetVtkPlaneWidget( vtkImagePlaneWidget *planeWidget );
+   myCallbackPlane() {};
+
+ private:
+   vtkPlaneWidget *planeWidget;
+   ImagePlanes *currentBlackBox;
+ };
+  //================================================================
+
+
+
+  //================================================================
 
    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ImagePlanes)
    BBTK_BLACK_BOX_IMPLEMENTATION(ImagePlanes,bbtk::AtomicBlackBox);
 
    void ImagePlanes::bbUserConstructor() 
    { 
-     Init();
+     bbSetOutputPlaneX(0);
+     bbSetOutputPlaneY(0);
+     bbSetOutputPlaneZ(0);
+     bbSetOutputImageX(0);
+     bbSetOutputImageY(0);
+     bbSetOutputImageZ(0);
      bbSetInputIn(0);
      std::vector<double> vect;
-     vect.push_back(512);
-     vect.push_back(256);
-     bbSetInputWindowLevel (vect);
+     vect.push_back(0);
+     vect.push_back(0);
+     bbSetInputWindowLevel (vect);  
    }
    
-   void ImagePlanes::bbUserCopyConstructor() 
+   void ImagePlanes::bbUserCopyConstructor(bbtk::BlackBox::Pointer
    { 
-     Init();
+     bbSetOutputPlaneX(0);
+     bbSetOutputPlaneY(0);
+     bbSetOutputPlaneZ(0);
+     bbSetOutputImageX(0);
+     bbSetOutputImageY(0);
+     bbSetOutputImageZ(0);
    }
 
    void ImagePlanes::Init() 
-   { 
-     
+   {  
+
+     /// CREATION DES WIDGETS
+     if (bbGetOutputPlaneX() != 0) return;
+       
      // The shared picker enables us to use 3 planes at one time
      // and gets the picking order right
      vtkCellPicker* picker = vtkCellPicker::New();
      picker->SetTolerance(0.005);
   
-     // The 3 image plane widgets are used to probe the dataset.
-     planeWidgetX = vtkImagePlaneWidget::New();
+     // The 3 image plane widgets 
+     vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
      planeWidgetX->DisplayTextOn();
      planeWidgetX->SetPicker(picker);
      planeWidgetX->SetKeyPressActivationValue('x');
      vtkProperty* prop1 = planeWidgetX->GetPlaneProperty();
      prop1->SetColor(1, 0, 0);
 
-     planeWidgetY = vtkImagePlaneWidget::New();
+     vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
      planeWidgetY->DisplayTextOn();
      planeWidgetY->SetPicker(picker);
      planeWidgetY->SetKeyPressActivationValue('y');
@@ -93,10 +167,7 @@ namespace bbvtk
      prop2->SetColor(1, 1, 0);
      planeWidgetY->SetLookupTable(planeWidgetX->GetLookupTable());
 
-     // for the z-slice, turn off texture interpolation:
-     // interpolation is now nearest neighbour, to demonstrate
-     // cross-hair cursor snapping to pixel centers
-     planeWidgetZ = vtkImagePlaneWidget::New();
+     vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
      planeWidgetZ->DisplayTextOn();
      planeWidgetZ->SetPicker(picker);
      planeWidgetZ->SetKeyPressActivationValue('z');
@@ -107,22 +178,26 @@ namespace bbvtk
      bbSetOutputPlaneX(planeWidgetX);
      bbSetOutputPlaneY(planeWidgetY);
      bbSetOutputPlaneZ(planeWidgetZ);
+     bbSetOutputImageX(planeWidgetX->GetResliceOutput());
+     bbSetOutputImageY(planeWidgetY->GetResliceOutput());
+     bbSetOutputImageZ(planeWidgetZ->GetResliceOutput());
 
      picker->UnRegister(NULL);
-     //        planeWidgetX->Register(NULL);
-     //        planeWidgetY->Register(NULL);
-     //        planeWidgetZ->Register(NULL);
+     
+     myCallbackPlane *_myCallback = myCallbackPlane::New();
+     _myCallback->SetCurrentBlackBox(this);
+     planeWidgetX->AddObserver(vtkCommand::InteractionEvent,_myCallback);
+     planeWidgetY->AddObserver(vtkCommand::InteractionEvent,_myCallback);
+     planeWidgetZ->AddObserver(vtkCommand::InteractionEvent,_myCallback);  
    }
 
 //---------------------------------------------------------------------
   void ImagePlanes::bbUserDestructor()
   {
-    //planeWidgetX->UnRegister(NULL);//Delete();
-    //planeWidgetY->UnRegister(NULL);//Delete();
-    //planeWidgetZ->UnRegister(NULL);//Delete();
-    planeWidgetX->Delete();
-    planeWidgetY->Delete();
-    planeWidgetZ->Delete();
+
+    if (bbGetOutputPlaneX()) bbGetOutputPlaneX()->UnRegister(NULL);
+    if (bbGetOutputPlaneY()) bbGetOutputPlaneY()->UnRegister(NULL);
+    if (bbGetOutputPlaneZ()) bbGetOutputPlaneZ()->UnRegister(NULL);
   }
   
 //---------------------------------------------------------------------  
@@ -130,24 +205,53 @@ namespace bbvtk
    {
      if (bbGetInputIn()!=0)
        {
-        int xMin, xMax, yMin, yMax, zMin, zMax;
-        bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
-
-        planeWidgetX->SetInput(bbGetInputIn());
-        planeWidgetX->SetPlaneOrientationToXAxes();
-        planeWidgetX->SetSlicePosition((xMax+xMin)/2.); 
-
-        planeWidgetY->SetInput(bbGetInputIn());
-        planeWidgetY->SetPlaneOrientationToYAxes();
-        planeWidgetY->SetSlicePosition((yMax+yMin)/2.); 
-
-        planeWidgetZ->SetInput(bbGetInputIn());
-        planeWidgetZ->SetPlaneOrientationToZAxes();
-        planeWidgetZ->SetSlicePosition((zMax+zMin)/2.); 
+        // Create the widgets if not already done
+        Init();
 
-       // planeWidgetZ->SetWindowLevel(512,256);
+        if ( bbGetInputStatus("In") != bbtk::UPTODATE )
+          {
+            // Input image has changed : reinitialize planes
+            image = bbGetInputIn();
 
-         planeWidgetZ->SetWindowLevel(bbGetInputWindowLevel()[0],bbGetInputWindowLevel()[1]);
+            int xMin, xMax, yMin, yMax, zMin, zMax;
+            bbGetInputIn()->GetExtent(xMin, xMax, yMin, yMax, zMin, zMax);
+            
+            // Initial values : center of the volume (in real world, not in pixels!)
+            double xSpacing, ySpacing, zSpacing;
+            bbGetInputIn()->GetSpacing(xSpacing, ySpacing, zSpacing);
+            
+            bbGetOutputPlaneX()->SetInput(bbGetInputIn());
+            bbGetOutputPlaneX()->SetPlaneOrientationToXAxes();  
+            bbGetOutputPlaneX()->SetSlicePosition((xMax+xMin)/2.*xSpacing);
+            
+            bbGetOutputPlaneY()->SetInput(bbGetInputIn());
+            bbGetOutputPlaneY()->SetPlaneOrientationToYAxes();
+            bbGetOutputPlaneY()->SetSlicePosition((yMax+yMin)/2.*ySpacing);
+            
+            bbGetOutputPlaneZ()->SetInput(bbGetInputIn());
+            bbGetOutputPlaneZ()->SetPlaneOrientationToZAxes();
+            bbGetOutputPlaneZ()->SetSlicePosition((zMax+zMin)/2.*zSpacing);
+            
+            if (bbGetInputWindowLevel()[0]!=0)
+              {
+                bbGetOutputPlaneZ()->SetWindowLevel(bbGetInputWindowLevel()[0],
+                                                    bbGetInputWindowLevel()[1]);
+              }
+            else 
+              {
+                double *range = image->GetScalarRange();
+                bbGetOutputPlaneZ()->SetWindowLevel(range[1] - range[0],
+                                                    0.5*(range[1]+range[0]));
+              }
+          }
+        
+        
+        // UPDATE DES SORTIES 
+        bbGetOutputPlaneX()->GetResliceOutput()->Update();
+        bbGetOutputPlaneY()->GetResliceOutput()->Update(); 
+        bbGetOutputPlaneZ()->GetResliceOutput()->Update(); 
+        
+                
        }
    }
 }//namespace bbtk