]> Creatis software - clitk.git/blobdiff - vv/vvSlicer.cxx
Merge branch 'master' of git://git.creatis.insa-lyon.fr/clitk
[clitk.git] / vv / vvSlicer.cxx
index 2a5c9247bbe3b0884d392521fa302a81c5e8c797..924eb1d8161d096452e7849df145da07c17fa1d9 100644 (file)
@@ -41,6 +41,7 @@
 #include <vtkDataArray.h>
 #include <vtkFloatArray.h>
 #include <vtkClipPolyData.h>
+#include <vtkActor2DCollection.h>
 #include <vtkGlyph3D.h>
 #include <vtkMath.h>
 #include <vtkCursor3D.h>
@@ -82,16 +83,18 @@ vvSlicer::vvSlicer()
   mImage = NULL;
   mReducedExtent = new int[6];
   mCurrentTSlice = 0;
+  mCurrentFusionTSlice = 0;
+  mCurrentOverlayTSlice = 0;
   mUseReducedExtent = false;
 
   mCurrent[0] = -VTK_DOUBLE_MAX;
   mCurrent[1] = -VTK_DOUBLE_MAX;
   mCurrent[2] = -VTK_DOUBLE_MAX;
 
-  mCursor[0] = -VTK_DOUBLE_MAX;
-  mCursor[1] = -VTK_DOUBLE_MAX;
-  mCursor[2] = -VTK_DOUBLE_MAX;
-  mCursor[3] = -VTK_DOUBLE_MAX;
+  mCursor[0] = 0;//-VTK_DOUBLE_MAX;
+  mCursor[1] = 0;//-VTK_DOUBLE_MAX;
+  mCursor[2] = 0;//-VTK_DOUBLE_MAX;
+  mCursor[3] = 0;//-VTK_DOUBLE_MAX;
 
   mSubSampling = 5;
   mScale = 1;
@@ -148,6 +151,8 @@ vvSlicer::vvSlicer()
 
   mSlicingTransform = vtkSmartPointer<vtkTransform>::New();
   mConcatenatedTransform = vtkSmartPointer<vtkTransform>::New();
+  mConcatenatedFusionTransform = vtkSmartPointer<vtkTransform>::New();
+  mConcatenatedOverlayTransform = vtkSmartPointer<vtkTransform>::New();
 }
 //------------------------------------------------------------------------------
 
@@ -307,7 +312,7 @@ void vvSlicer::SetCurrentPosition(double x, double y, double z, int t)
   mCurrentBeforeSlicingTransform[1]=y;
   mCurrentBeforeSlicingTransform[2]=z;
   mSlicingTransform->GetInverse()->TransformPoint(mCurrentBeforeSlicingTransform,mCurrent);
-  mCurrentTSlice = t;
+  SetTSlice(t);
 }
 //------------------------------------------------------------------------------
 
@@ -369,8 +374,13 @@ void vvSlicer::SetOverlay(vvImage::Pointer overlay)
       mOverlayReslice->AutoCropOutputOn();
       mOverlayReslice->SetBackgroundColor(-1000,-1000,-1000,1);
     }
-    mOverlayReslice->SetResliceTransform(mOverlay->GetTransform()[0]);
+
+    mConcatenatedOverlayTransform->Identity();
+    mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[0]);
+    mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
+    mOverlayReslice->SetResliceTransform(mConcatenatedOverlayTransform);
     mOverlayReslice->SetInput(0, mOverlay->GetFirstVTKImageData());
+    mImageReslice->UpdateInformation();
 
     if (!mOverlayMapper)
       mOverlayMapper = vtkSmartPointer<vtkImageMapToWindowLevelColors>::New();
@@ -416,8 +426,13 @@ void vvSlicer::SetFusion(vvImage::Pointer fusion)
       mFusionReslice->AutoCropOutputOn();
       mFusionReslice->SetBackgroundColor(-1000,-1000,-1000,1);
     }
-    mFusionReslice->SetResliceTransform(mFusion->GetTransform()[0]);
+
+    mConcatenatedFusionTransform->Identity();
+    mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[0]);
+    mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
+    mFusionReslice->SetResliceTransform(mConcatenatedFusionTransform);
     mFusionReslice->SetInput(0, mFusion->GetFirstVTKImageData());
+    mFusionReslice->UpdateInformation();
 
     if (!mFusionMapper)
       mFusionMapper = vtkSmartPointer<vtkImageMapToColors>::New();
@@ -566,36 +581,40 @@ void vvSlicer::SetLandmarks(vvLandmarks* landmarks)
 
     if (!mCross)
       mCross = vtkSmartPointer<vtkCursor3D>::New();
+       if (!mClipBox)
+      mClipBox = vtkSmartPointer<vtkBox>::New();
+    if (!mLandClipper)
+      mLandClipper = vtkSmartPointer<vvClipPolyData>::New();
+    if (!mLandGlyph)
+      mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
+    if (!mLandMapper)
+      mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
+    if (!mLandActor)
+      mLandActor = vtkSmartPointer<vtkActor>::New();
+
     mCross->SetFocalPoint(0.0,0.0,0.0);
     mCross->SetModelBounds(-10,10,-10,10,-10,10);
     mCross->AllOff();
     mCross->AxesOn();
 
-    if (!mLandGlyph)
-      mLandGlyph = vtkSmartPointer<vtkGlyph3D>::New();
+    mLandClipper->SetClipFunction(mClipBox);
+    mLandClipper->InsideOutOn();
+    mLandClipper->SetInput(mLandmarks->GetOutput());
+
     mLandGlyph->SetSource(mCross->GetOutput());
-    mLandGlyph->SetInput(landmarks->GetOutput());
+    mLandGlyph->SetInput(mLandClipper->GetOutput());
     //mLandGlyph->SetIndexModeToScalar();
-    mLandGlyph->SetRange(0,1);
-    mLandGlyph->ScalingOff();
+    //mLandGlyph->SetRange(0,1);
+    //mLandGlyph->ScalingOff();
 
-    mLandGlyph->SetColorModeToColorByScalar();
-
-    if (!mClipBox)
-      mClipBox = vtkSmartPointer<vtkBox>::New();
-    if (!mLandClipper)
-      mLandClipper = vtkSmartPointer<vtkClipPolyData>::New();
-    mLandClipper->InsideOutOn();
-    mLandClipper->SetInput(mLandGlyph->GetOutput());
-    mLandClipper->SetClipFunction(mClipBox);
+    //mLandGlyph->SetColorModeToColorByScalar();
+    
+    mLandGlyph->SetScaleModeToDataScalingOff();
+    mLandGlyph->SetIndexModeToOff();
 
-    if (!mLandMapper)
-      mLandMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
-    mLandMapper->SetInputConnection(mLandClipper->GetOutputPort());
+    mLandMapper->SetInputConnection(mLandGlyph->GetOutputPort());
     //mLandMapper->ScalarVisibilityOff();
 
-    if (!mLandActor)
-      mLandActor = vtkSmartPointer<vtkActor>::New();
     mLandActor->SetMapper(mLandMapper);
     mLandActor->GetProperty()->SetColor(255,10,212);
     mLandActor->SetPickable(0);
@@ -694,29 +713,44 @@ void vvSlicer::SetVFLog(int log)
 void vvSlicer::SetTSlice(int t)
 {
   if (t < 0)
-    t = 0;
+    mCurrentTSlice = 0;
   else if ((unsigned int)t >= mImage->GetVTKImages().size())
-    t = mImage->GetVTKImages().size() -1;
+    mCurrentTSlice = mImage->GetVTKImages().size() -1;
+  else
+    mCurrentTSlice = t;
 
   // Update transform
   mConcatenatedTransform->Identity();
-  mConcatenatedTransform->Concatenate(mImage->GetTransform()[t]);
+  mConcatenatedTransform->Concatenate(mImage->GetTransform()[mCurrentTSlice]);
   mConcatenatedTransform->Concatenate(mSlicingTransform);
 
   // Update image data
-  mCurrentTSlice = t;
   mImageReslice->SetInput( mImage->GetVTKImages()[mCurrentTSlice] );
   if (mVF && mVFActor->GetVisibility()) {
     if (mVF->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
       mVOIFilter->SetInput(mVF->GetVTKImages()[mCurrentTSlice]);
   }
   if (mOverlay && mOverlayActor->GetVisibility()) {
-    if (mOverlay->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
-      mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentTSlice] );
+    if (mOverlay->GetVTKImages().size() > (unsigned int)t) {
+      mCurrentOverlayTSlice = t;
+      mOverlayReslice->SetInput( mOverlay->GetVTKImages()[mCurrentOverlayTSlice] );
+
+      // Update overlay transform
+      mConcatenatedOverlayTransform->Identity();
+      mConcatenatedOverlayTransform->Concatenate(mOverlay->GetTransform()[mCurrentOverlayTSlice]);
+      mConcatenatedOverlayTransform->Concatenate(mSlicingTransform);
+    }
   }
   if (mFusion && mFusionActor->GetVisibility()) {
-    if (mFusion->GetVTKImages().size() > (unsigned int)mCurrentTSlice)
-      mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentTSlice]);
+    if (mFusion->GetVTKImages().size() > (unsigned int)t) {
+      mCurrentFusionTSlice = t;
+      mFusionReslice->SetInput( mFusion->GetVTKImages()[mCurrentFusionTSlice]);
+
+      // Update fusion transform
+      mConcatenatedFusionTransform->Identity();
+      mConcatenatedFusionTransform->Concatenate(mFusion->GetTransform()[mCurrentFusionTSlice]);
+      mConcatenatedFusionTransform->Concatenate(mSlicingTransform);
+    }
   }
   if (mSurfaceCutActors.size() > 0)
     for (std::vector<vvMeshActor*>::iterator i=mSurfaceCutActors.begin();
@@ -734,6 +768,32 @@ int vvSlicer::GetTSlice()
 }
 //------------------------------------------------------------------------------
 
+//------------------------------------------------------------------------------
+int vvSlicer::GetMaxCurrentTSlice()
+{
+  int t = mCurrentTSlice;
+  if(mOverlay)
+    t = std::max(t, mCurrentOverlayTSlice);
+  if(mFusion)
+    t = std::max(t, mCurrentFusionTSlice);
+  return t;
+}
+//------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------
+int vvSlicer::GetFusionTSlice()
+{
+  return mCurrentFusionTSlice;
+}
+//------------------------------------------------------------------------------
+
+//------------------------------------------------------------------------------
+int vvSlicer::GetOverlayTSlice()
+{
+  return mCurrentOverlayTSlice;
+}
+//------------------------------------------------------------------------------
+
 //------------------------------------------------------------------------------
 void vvSlicer::SetSliceOrientation(int orientation)
 {
@@ -758,15 +818,22 @@ void vvSlicer::SetSliceOrientation(int orientation)
     AdjustResliceToSliceOrientation(mOverlayReslice);
 
   // Update the viewer
-  int *range = this->GetSliceRange();
-  if (range)
-    this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
-
+  
   // Go to current cursor position
   // double* cursorPos = GetCursorPosition();
   // DDV(cursorPos, 3);
   // SetCurrentPosition(cursorPos[0],cursorPos[1],cursorPos[2],cursorPos[3]);
 
+  if (this->Renderer && this->GetInput()) {
+    double s = mCursor[orientation];
+    double sCursor = (s - this->GetInput()->GetOrigin()[orientation])/this->GetInput()->GetSpacing()[orientation];
+    this->Slice = static_cast<int>(sCursor);
+  }
+  
+//   int *range = this->GetSliceRange();
+//   if (range)
+//     this->Slice = static_cast<int>((range[0] + range[1]) * 0.5);
+
   this->UpdateOrientation();
   this->UpdateDisplayExtent();
 
@@ -1197,7 +1264,7 @@ void vvSlicer::SetOverlayColorLevel(double level)
 //----------------------------------------------------------------------------
 
 //----------------------------------------------------------------------------
-// Returns the min an the max value in a 41x41 region around the mouse pointer
+// Returns the min an the max value in a 20%x20% region around the mouse pointer
 void vvSlicer::GetExtremasAroundMousePointer(double & min, double & max, vtkImageData *image, vtkTransform *transform)
 {
   //Get mouse pointer position in view coordinates
@@ -1386,13 +1453,38 @@ void vvSlicer::UpdateLandmarks()
 {
   vtkPolyData *pd = static_cast<vtkPolyData*>(mLandClipper->GetInput());
   if (pd->GetPoints()) {
-    mLandGlyph->SetRange(0,1);
-    mLandGlyph->Modified();
-    mLandGlyph->Update();
+    //mLandGlyph->SetRange(0,1);
+    //mLandGlyph->Modified();
+    //mLandGlyph->Update();
 
     mClipBox->Modified();
     mLandClipper->Update();
     mLandMapper->Update();
+    //Let's add the captions
+    //First remove all captions:
+    for(unsigned int i=0;i<mLandLabelActors.size();i++) {
+       this->Renderer->RemoveActor2D(mLandLabelActors[i]);
+       //allActors2D->Remove (mLandLabelActors[i]);
+    }
+    mLandLabelActors.clear();
+    //Next add the captions to the displayed points
+    for (vtkIdType id=0; id<mLandClipper->GetOutput()->GetNumberOfPoints(); id++) {
+         double *position = mLandClipper->GetOutput()->GetPoint(id);
+      vtkStdString label = static_cast<vtkStringArray*>(mLandClipper->GetOutput()->GetPointData()->GetAbstractArray("labels"))->GetValue(id);
+      vtkSmartPointer<vtkCaptionActor2D> label_actor = vtkSmartPointer<vtkCaptionActor2D>::New();
+      label_actor->SetCaption(label);
+      label_actor->SetAttachmentPoint(position);
+      label_actor->GetCaptionTextProperty()->SetColor(1,0,0);
+      label_actor->GetCaptionTextProperty()->SetOrientation(33.333333);
+      label_actor->GetCaptionTextProperty()->SetFontFamilyToTimes();
+      label_actor->GetCaptionTextProperty()->SetBold(0);
+      label_actor->GetCaptionTextProperty()->SetFontSize(6);
+      label_actor->BorderOff();
+      label_actor->LeaderOff();
+      label_actor->ThreeDimensionalLeaderOff();
+      mLandLabelActors.push_back(label_actor);
+      this->Renderer->AddActor2D(mLandLabelActors[id]);
+     }
   }
 
 }
@@ -1425,6 +1517,14 @@ void vvSlicer::SetSlice(int slice)
 }
 //----------------------------------------------------------------------------
 
+//----------------------------------------------------------------------------
+int vvSlicer::GetTMax() {
+  int tmax = (int)mImage->GetVTKImages().size() - 1;
+  if(mOverlay)
+    tmax = std::max(tmax, (int)mOverlay->GetVTKImages().size()-1);
+  return tmax;
+}
+//----------------------------------------------------------------------------
 
 //----------------------------------------------------------------------------
 void vvSlicer::SetContourSlice()