[vtkusers] setSlice problem on QVTKWidget

Massi Massinissa.Bandou at USherbrooke.ca
Thu Sep 6 16:42:47 EDT 2012


Hi David,
I replaced them all (reader, imageViewer and myInteractorStyle) in the cpp
file. According to this
example:http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOMSeries ,
I just want to display the series on a QVTKWidget but the program crashes
every time when I try to change the slice by pressing UP or Down. So I
decided to replace all the smartpointers by normal pointers and it works.
By the way, I'm using Netbeans IDE with Qt Designer integrated on it. I
tried to keep the same code, but I added some in bold. 


******************Old code:***************
class StatusMessage {
public:
   static std::string Format(int slice, int maxSlice) {
      std::stringstream tmp;
      tmp << "Slice Number  " << slice + 1 << "/" << maxSlice + 1;
      return tmp.str();
   }
};
 
 
// Define own interaction style
class myVtkInteractorStyleImage : public vtkInteractorStyleImage
{
public:
   static myVtkInteractorStyleImage* New();
   vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);
 
protected:
   vtkImageViewer2* _ImageViewer;
   vtkTextMapper* _StatusMapper;
   int _Slice;
   int _MinSlice;
   int _MaxSlice;
 
public:
   void SetImageViewer(vtkImageViewer2* imageViewer) {
      _ImageViewer = imageViewer;
      _MinSlice = imageViewer->GetSliceMin();
      _MaxSlice = imageViewer->GetSliceMax();
      _Slice = _MinSlice;
      cout << "Slicer: Min = " << _MinSlice << ", Max = " << _MaxSlice <<
std::endl;
   }
 
   void SetStatusMapper(vtkTextMapper* statusMapper) {
      _StatusMapper = statusMapper;
   }
 
 
protected:
   void MoveSliceForward() {
      if(_Slice < _MaxSlice) {
         _Slice += 1;
         cout << "MoveSliceForward::Slice = " << _Slice << std::endl;
         _ImageViewer->SetSlice(_Slice);
         std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
         _StatusMapper->SetInput(msg.c_str());
         _ImageViewer->Render();
      }
   }
 
   void MoveSliceBackward() {
      if(_Slice > _MinSlice) {
         _Slice -= 1;
         cout << "MoveSliceBackward::Slice = " << _Slice << std::endl;
         _ImageViewer->SetSlice(_Slice);
         std::string msg = StatusMessage::Format(_Slice, _MaxSlice);
         _StatusMapper->SetInput(msg.c_str());
         _ImageViewer->Render();
      }
   }
 
 
   virtual void OnKeyDown() {
      std::string key = this->GetInteractor()->GetKeySym();
      if(key.compare("Up") == 0) {
         //cout << "Up arrow key was pressed." << endl;
         MoveSliceForward();
      }
      else if(key.compare("Down") == 0) {
         //cout << "Down arrow key was pressed." << endl;
         MoveSliceBackward();
      }
      // forward event
      vtkInteractorStyleImage::OnKeyDown();
   }
 }

int main(int argc, char *argv[]){

QApplication app(argc, argv);
ImageCoregistration form;

QString str = widget.MovingImageLocation2D->text();
const char *filename = str.toLatin1();

vtkSmartPointer<vtkDICOMImageReader> reader =
vtkSmartPointer<vtkDICOMImageReader>::New();
reader->SetDirectoryName(filename);
reader->Update();
    
vtkSmartPointer<vtkImageViewer2> imageViewer =
vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
    
vtkSmartPointer<vtkTextProperty> sliceTextProp =
vtkSmartPointer<vtkTextProperty>::New();
sliceTextProp->SetFontFamilyToCourier();
sliceTextProp->SetFontSize(20);
sliceTextProp->SetVerticalJustificationToBottom();
sliceTextProp->SetJustificationToLeft();
    
vtkSmartPointer<vtkTextMapper> sliceTextMapper =
vtkSmartPointer<vtkTextMapper>::New();
std::string msg = StatusMessage::Format(imageViewer->GetSliceMin(),
imageViewer->GetSliceMax());
sliceTextMapper->SetInput(msg.c_str());
sliceTextMapper->SetTextProperty(sliceTextProp);
    
vtkSmartPointer<vtkActor2D> sliceTextActor =
vtkSmartPointer<vtkActor2D>::New();
sliceTextActor->SetMapper(sliceTextMapper);
sliceTextActor->SetPosition(15,10);
    
vtkSmartPointer<vtkTextProperty> usageTextProp =
vtkSmartPointer<vtkTextProperty>::New();
usageTextProp->SetFontFamilyToCourier();
usageTextProp->SetFontSize(14);
usageTextProp->SetVerticalJustificationToTop();
usageTextProp->SetJustificationToLeft();
    
vtkSmartPointer<vtkTextMapper> usageTextMapper =
vtkSmartPointer<vtkTextMapper>::New();
usageTextMapper->SetInput("-Slice with mouse wheel\n or Up/Down-Key\n"
    "-Zoom with pressed right\n mouse button while dragging");
usageTextMapper->SetTextProperty(usageTextProp);
    
vtkSmartPointer<vtkActor2D> usageTextActor =
vtkSmartPointer<vtkActor2D>::New();
usageTextActor->SetMapper(usageTextMapper);
usageTextActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
usageTextActor->GetPositionCoordinate()->SetValue(0.05,0.95);
  
vtkSmartPointer<myVtkInteractorStyleImage> myInteractorStyle =
vtkSmartPointer<myVtkInteractorStyleImage>::New();
    
myInteractorStyle->SetImageViewer(imageViewer);
myInteractorStyle->SetStatusMapper(sliceTextMapper);
imageViewer->GetRenderer()->AddActor2D(sliceTextActor);
imageViewer->GetRenderer()->AddActor2D(usageTextActor);
imageViewer->GetRenderer()->GradientBackgroundOn();
 
widget.qvtkWidget_2->SetRenderWindow(imageViewer->GetRenderWindow());
widget.qvtkWidget_2->GetRenderWindow()->GetInteractor()->SetInteractorStyle(myInteractorStyle);
imageViewer->Render();
widget.qvtkWidget_2->update();

form.show();
return app.exec();

}


****************Replaced by:***************
class StatusMessage{  
public:
    static std::string Format(int slice, int maxSlice){
        std::stringstream tmp;
        tmp<<"Slice Number: "<< slice+1 <<"/"<<maxSlice +1;
        return tmp.str();
    }
};

class myVtkInteractorStyleImage : public vtkInteractorStyleImage{
public:
   <b>myVtkInteractorStyleImage(): ImageViewer(NULL),StatusMapper(NULL){};*
    Ui::ImageCoregistration widget;
public:
    static myVtkInteractorStyleImage *New();
    vtkTypeMacro(myVtkInteractorStyleImage,vtkInteractorStyleImage);
protected:
    vtkImageViewer2* ImageViewer;
    vtkTextMapper *StatusMapper;
    int Slice;
    int MinSlice;
    int MaxSlice;
public:
    void SetImageViewer(vtkImageViewer2* imageViewer){
        *this->ImageViewer = imageViewer;*
        MinSlice = imageViewer->GetSliceMin();
        MaxSlice = imageViewer->GetSliceMax();
        Slice = MinSlice;
        cout<< "Slicer: Min =" << MinSlice <<", Max =
"<<MaxSlice&lt;&lt;std::endl;
    }
    
    void SetStatusMapper(vtkTextMapper* statusMapper){
        StatusMapper = statusMapper;
    }
protected:
    void MoveSliceForward(){
        if (Slice &lt; MaxSlice){
            Slice += 1;
            cout&lt;&lt;&quot;MoveSliceForward::Slice =
&quot;&lt;&lt;Slice&lt;&lt;std::endl;
            &lt;b>this->ImageViewer->SetSlice(Slice);  * 
            std::string msg = StatusMessage::Format(Slice, MaxSlice);
            StatusMapper->SetInput(msg.c_str());
            ImageViewer->Render();
        }
    }
    void MoveSliceBackward(){
        if (Slice > MinSlice){
            Slice -= 1;
            cout<<"MoveSliceBackward::Slice = "<<Slice&lt;&lt;std::endl;         
           &lt;b> this->ImageViewer->SetSlice(Slice); *;   
            std::string msg = StatusMessage::Format(Slice, MaxSlice);
            StatusMapper->SetInput(msg.c_str());
            ImageViewer->Render();
        }
    }
    virtual void OnKeyDown(){
        std::string key = this->GetInteractor()->GetKeySym();
        if (key.compare("Up") == 0){
            MoveSliceForward();
        }
        else if(key.compare("Down") == 0){
            MoveSliceBackward();
        }
 
    }    
};

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
ImageCoregistration form;

QString str = widget.MovingImageLocation2D->text();
const char *filename = str.toLatin1();

vtkDICOMImageReader* reader = vtkDICOMImageReader::New();
reader->SetDirectoryName(filename);
reader->Update();
    
vtkImageViewer2* imageViewer = vtkImageViewer2::New();
imageViewer->SetInputConnection(reader->GetOutputPort());
    
vtkTextProperty* sliceTextProp = vtkTextProperty::New();
sliceTextProp->SetFontFamilyToCourier();
sliceTextProp->SetFontSize(20);
sliceTextProp->SetVerticalJustificationToBottom();
sliceTextProp->SetJustificationToLeft();
    
vtkTextMapper* sliceTextMapper = vtkTextMapper::New();
std::string msg = StatusMessage::Format(imageViewer->GetSliceMin(),
imageViewer->GetSliceMax());
sliceTextMapper->SetInput(msg.c_str());
sliceTextMapper->SetTextProperty(sliceTextProp);
    
vtkActor2D* sliceTextActor = vtkActor2D::New();
sliceTextActor->SetMapper(sliceTextMapper);
sliceTextActor->SetPosition(15,10);
    
vtkTextProperty* usageTextProp = vtkTextProperty::New();
usageTextProp->SetFontFamilyToCourier();
usageTextProp->SetFontSize(14);
usageTextProp->SetVerticalJustificationToTop();
usageTextProp->SetJustificationToLeft();
    
vtkTextMapper* usageTextMapper = vtkTextMapper::New();
usageTextMapper->SetInput("-Slice with mouse wheel\n or Up/Down-Key\n"
    "-Zoom with pressed right\n mouse button while dragging");
usageTextMapper->SetTextProperty(usageTextProp);
    
vtkActor2D* usageTextActor = vtkActor2D::New();
usageTextActor->SetMapper(usageTextMapper);
usageTextActor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedDisplay();
usageTextActor->GetPositionCoordinate()->SetValue(0.05,0.95);
  
myVtkInteractorStyleImage* myInteractorStyle =
myVtkInteractorStyleImage::New();
    
myInteractorStyle->SetImageViewer(imageViewer);
myInteractorStyle->SetStatusMapper(sliceTextMapper);
imageViewer->GetRenderer()->AddActor2D(sliceTextActor);
imageViewer->GetRenderer()->AddActor2D(usageTextActor);
imageViewer->GetRenderer()->GradientBackgroundOn();
 
widget.qvtkWidget_2->SetRenderWindow(imageViewer->GetRenderWindow());
widget.qvtkWidget_2->GetRenderWindow()->GetInteractor()->SetInteractorStyle(myInteractorStyle);
imageViewer->Render();
widget.qvtkWidget_2->update();

form.show();
return app.exec();
}

thx for your Help!!

Massi




--
View this message in context: http://vtk.1045678.n5.nabble.com/setSlice-problem-on-QVTKWidget-tp5715880p5715911.html
Sent from the VTK - Users mailing list archive at Nabble.com.



More information about the vtkusers mailing list