Commit 7c402003 authored by Sebastien Barre's avatar Sebastien Barre

Add interactor/interactor style

parent 2d68f312
......@@ -17,8 +17,9 @@
=========================================================================*/
#include "vtkImageViewer.h"
#include "vtkObjectFactory.h"
#include "vtkInteractorStyleImage.h"
vtkCxxRevisionMacro(vtkImageViewer, "1.37");
vtkCxxRevisionMacro(vtkImageViewer, "1.38");
vtkStandardNewMacro(vtkImageViewer);
//----------------------------------------------------------------------------
......@@ -34,6 +35,11 @@ vtkImageViewer::vtkImageViewer()
this->Actor2D->SetMapper(this->ImageMapper);
this->Renderer->AddActor2D(this->Actor2D);
this->RenderWindow->AddRenderer(this->Renderer);
this->FirstRender = 1;
this->Interactor = 0;
this->InteractorStyle = 0;
}
......@@ -44,6 +50,15 @@ vtkImageViewer::~vtkImageViewer()
this->Actor2D->Delete();
this->RenderWindow->Delete();
this->Renderer->Delete();
if (this->Interactor)
{
this->Interactor->Delete();
}
if (this->InteractorStyle)
{
this->InteractorStyle->Delete();
}
}
//----------------------------------------------------------------------------
......@@ -69,20 +84,144 @@ void vtkImageViewer::SetPosition(int a[2])
}
void vtkImageViewer::Render()
class vtkImageViewerCallback : public vtkCommand
{
public:
static vtkImageViewerCallback *New() {
return new vtkImageViewerCallback; }
void Execute(vtkObject *caller, unsigned long event, void *callData)
{
if (event == vtkCommand::ResetWindowLevelEvent && this->IV->GetInput())
{
this->IV->GetInput()->UpdateInformation();
this->IV->GetInput()->SetUpdateExtent(this->IV->GetInput()->GetWholeExtent());
this->IV->GetInput()->Update();
float *range = this->IV->GetInput()->GetScalarRange();
this->IV->SetColorWindow(range[1] - range[0]);
this->IV->SetColorLevel(0.5 * (range[1] + range[0]));
this->IV->Render();
return;
}
if (callData)
{
this->InitialWindow = this->IV->GetColorWindow();
this->InitialLevel = this->IV->GetColorLevel();
return;
}
// adjust the window level here
vtkInteractorStyleImage *isi =
static_cast<vtkInteractorStyleImage *>(caller);
int *size = this->IV->GetRenderWindow()->GetSize();
float window = this->InitialWindow;
float level = this->InitialLevel;
// compute normalized delta
float dx = 4.0 * (isi->GetWindowLevelCurrentPosition()[0] -
isi->GetWindowLevelStartPosition()[0]) / size[0];
float dy = 4.0 * (isi->GetWindowLevelStartPosition()[1] -
isi->GetWindowLevelCurrentPosition()[1]) / size[1];
// scale by current values
if (fabs(window) > 0.01)
{
dx = dx * window;
}
else
{
dx = dx * (window < 0 ? -0.01 : 0.01);
}
if (fabs(level) > 0.01)
{
dy = dy * level;
}
else
{
dy = dy * (level < 0 ? -0.01 : 0.01);
}
// abs so that direction does not flip
if (window < 0.0)
{
dx = -1*dx;
}
if (level < 0.0)
{
dy = -1*dy;
}
// compute new window level
float newWindow = dx + window;
float newLevel;
newLevel = level - dy;
// stay away from zero and really
if (fabs(newWindow) < 0.01)
{
newWindow = 0.01*(newWindow < 0 ? -1 : 1);
}
if (fabs(newLevel) < 0.01)
{
newLevel = 0.01*(newLevel < 0 ? -1 : 1);
}
this->IV->SetColorWindow(newWindow);
this->IV->SetColorLevel(newLevel);
this->IV->Render();
}
vtkImageViewer *IV;
float InitialWindow;
float InitialLevel;
};
void vtkImageViewer::SetupInteractor(vtkRenderWindowInteractor *rwi)
{
// initialize the size if not set yet
if (this->RenderWindow->GetSize()[0] == 0 && this->ImageMapper->GetInput())
if (this->Interactor && rwi != this->Interactor)
{
// get the size from the mappers input
this->ImageMapper->GetInput()->UpdateInformation();
int *ext = this->ImageMapper->GetInput()->GetWholeExtent();
// if it would be smaller than 100 by 100 then limit to 100 by 100
int xs = ext[1] - ext[0] + 1;
int ys = ext[3] - ext[2] + 1;
this->RenderWindow->SetSize(xs < 150 ? 150 : xs,
ys < 100 ? 100 : ys);
this->Interactor->Delete();
}
if (!this->InteractorStyle)
{
this->InteractorStyle = vtkInteractorStyleImage::New();
vtkImageViewerCallback *cbk = vtkImageViewerCallback::New();
cbk->IV = this;
this->InteractorStyle->AddObserver(vtkCommand::WindowLevelEvent, cbk);
this->InteractorStyle->AddObserver(vtkCommand::ResetWindowLevelEvent, cbk);
cbk->Delete();
}
if (!this->Interactor)
{
this->Interactor = rwi;
rwi->Register(this);
}
this->Interactor->SetInteractorStyle(this->InteractorStyle);
this->Interactor->SetRenderWindow(this->RenderWindow);
}
void vtkImageViewer::Render()
{
if (this->FirstRender)
{
// initialize the size if not set yet
if (this->RenderWindow->GetSize()[0] == 0 && this->ImageMapper->GetInput())
{
// get the size from the mappers input
this->ImageMapper->GetInput()->UpdateInformation();
int *ext = this->ImageMapper->GetInput()->GetWholeExtent();
// if it would be smaller than 100 by 100 then limit to 100 by 100
int xs = ext[1] - ext[0] + 1;
int ys = ext[3] - ext[2] + 1;
this->RenderWindow->SetSize(xs < 150 ? 150 : xs,
ys < 100 ? 100 : ys);
}
this->FirstRender = 0;
}
this->RenderWindow->Render();
}
......@@ -33,6 +33,8 @@
#include "vtkImageMapper.h"
#include "vtkRenderWindow.h"
class vtkInteractorStyleImage;
class VTK_RENDERING_EXPORT vtkImageViewer : public vtkObject
{
public:
......@@ -101,11 +103,17 @@ public:
void SetSize(int a,int b) {this->RenderWindow->SetSize(a,b);};
virtual void SetSize(int a[2]);
vtkImageMapper *GetImageMapper() {return this->ImageMapper;};
vtkActor2D *GetActor2D() {return this->Actor2D;};
vtkRenderWindow *GetRenderWindow() {return this->RenderWindow;};
vtkRenderer *GetRenderer() {return this->Renderer;};
// Description:
// Get the internal objects
vtkGetObjectMacro(RenderWindow,vtkRenderWindow);
vtkGetObjectMacro(Renderer, vtkRenderer);
vtkGetObjectMacro(ImageMapper,vtkImageMapper);
vtkGetObjectMacro(Actor2D,vtkActor2D);
// Description:
// Create and attach an interactor for this window
void SetupInteractor(vtkRenderWindowInteractor *);
protected:
vtkImageViewer();
~vtkImageViewer();
......@@ -114,6 +122,9 @@ protected:
vtkRenderer *Renderer;
vtkImageMapper *ImageMapper;
vtkActor2D *Actor2D;
int FirstRender;
vtkRenderWindowInteractor *Interactor;
vtkInteractorStyleImage *InteractorStyle;
private:
vtkImageViewer(const vtkImageViewer&); // Not implemented.
void operator=(const vtkImageViewer&); // Not implemented.
......
......@@ -20,7 +20,7 @@
#include "vtkInteractorStyleImage.h"
#include "vtkCommand.h"
vtkCxxRevisionMacro(vtkImageViewer2, "1.10");
vtkCxxRevisionMacro(vtkImageViewer2, "1.11");
vtkStandardNewMacro(vtkImageViewer2);
//----------------------------------------------------------------------------
......@@ -97,16 +97,16 @@ public:
this->IV->GetInput()->SetUpdateExtent(this->IV->GetInput()->GetWholeExtent());
this->IV->GetInput()->Update();
float *range = this->IV->GetInput()->GetScalarRange();
this->IV->GetWindowLevel()->SetWindow(range[1] - range[0]);
this->IV->GetWindowLevel()->SetLevel(0.5 * (range[1] + range[0]));
this->IV->SetColorWindow(range[1] - range[0]);
this->IV->SetColorLevel(0.5 * (range[1] + range[0]));
this->IV->Render();
return;
}
if (callData)
{
this->InitialWindow = this->IV->GetWindowLevel()->GetWindow();
this->InitialLevel = this->IV->GetWindowLevel()->GetLevel();
this->InitialWindow = this->IV->GetColorWindow();
this->InitialLevel = this->IV->GetColorLevel();
return;
}
......@@ -167,8 +167,8 @@ public:
newLevel = 0.01*(newLevel < 0 ? -1 : 1);
}
this->IV->GetWindowLevel()->SetWindow(newWindow);
this->IV->GetWindowLevel()->SetLevel(newLevel);
this->IV->SetColorWindow(newWindow);
this->IV->SetColorLevel(newLevel);
this->IV->Render();
}
......
......@@ -18,6 +18,7 @@
#include <stdlib.h>
#include "vtkTkImageViewerWidget.h"
#include "vtkRenderWindowInteractor.h"
#ifdef _WIN32
#pragma warning ( disable : 4273 )
......@@ -271,7 +272,17 @@ static void vtkTkImageViewerWidget_Destroy(char *memPtr)
if (self->ImageViewer)
{
if (self->ImageViewer->GetReferenceCount() > 1)
int netRefCount = 0;
netRefCount = self->ImageViewer->GetReferenceCount();
/*
if (self->ImageViewer->GetRenderWindow()->GetInteractor() &&
self->ImageViewer->GetRenderWindow()->GetInteractor()->GetRenderWindow() == self->ImageViewer->GetRenderWindow() &&
self->ImageViewer->GetRenderWindow()->GetInteractor()->GetReferenceCount() == 1)
{
netRefCount = netRefCount - 1;
}
*/
if (netRefCount > 1)
{
vtkGenericWarningMacro("A TkImageViewerWidget is being destroyed before it associated vtkImageViewer is destroyed. This is very bad and usually due to the order in which objects are being destroyed. Always destroy the vtkImageViewer before destroying the user interface components.");
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment