[vtkusers] Update pixel value and display the changes in a widget

Luis Roberto P. Paula luisrpp at gmail.com
Thu Feb 5 11:53:31 EST 2009


Hi All,

I have developed a callback to update the pixel value of an image being
displayed in a widget. The idea is to set the pixels to zero in the points
where the mouse is over and the left button is pressed.

The problem is that I can't see any changes in the image, even after calling
the Render method.

This is the code:

class vtkImageInteractionCallback : public vtkCommand
{
public:

  static vtkImageInteractionCallback *New() {
    return new vtkImageInteractionCallback;
  };

  vtkImageInteractionCallback() {
    this->Slicing = 0;
  };

  void SetInteractor(vtkRenderWindowInteractor *interactor) {
    this->Interactor = interactor;
  };

  void SetImageViewer(vtkImageViewer2* viewer) {
    this->viewer = viewer;
  };

  void SetPicker(vtkPointPicker* picker) {
    this->picker = picker;
  };

  void performAction() {
    if (!picker->Pick(this->Interactor->GetEventPosition()[0],
                      this->Interactor->GetEventPosition()[1],
                      0,
                      viewer->GetRenderer())) {
      return;
    }
   double ptMapped[3];
   picker->GetMapperPosition(ptMapped);

   double dSpacing[3];
   viewer->GetInput()->GetSpacing(dSpacing);
   ptMapped[2] = viewer->GetSlice() * dSpacing[2];

   printf("x=%d : y=%d\n", (int)ptMapped[0], (int)ptMapped[1]);

   // Update the image
   vtkImageData* image = viewer->GetInput(); // It is a 2D image.
   image->AllocateScalars();
   image->SetScalarComponentFromDouble(ptMapped[0], ptMapped[1], 0, 0,
image->GetScalarTypeMin());
   image->Update();
   viewer->SetInput(image);

   printf("GetScalarTypeMin: %f\n", image->GetScalarTypeMin());
   viewer->Render();
  };

  virtual void Execute(vtkObject *, unsigned long event, void *) {
    if (event == vtkCommand::LeftButtonPressEvent) {
      this->Slicing = 1;
      performAction();
    }
    else if (event == vtkCommand::LeftButtonReleaseEvent) {
      this->Slicing = 0;
      performAction();
    }
    else if (event == vtkCommand::MouseMoveEvent) {
      if (this->Slicing) {
        performAction();
      }
    }
  };

private:
  int Slicing;
  vtkRenderWindowInteractor* Interactor;
  vtkPointPicker* picker;
  vtkImageViewer2* viewer;
};

Thanks in advance,
Luis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090205/6f687d1d/attachment.htm>


More information about the vtkusers mailing list