Commit e2ef8f4d authored by Ken Martin's avatar Ken Martin
Browse files

new image viewer

parent cf5a47ee
......@@ -25,11 +25,13 @@ vtkImageMapper
vtkImager
vtkImagerCollection
vtkImageViewer
vtkImageViewer2
vtkImageWindow
vtkImagingFactory
vtkImporter
vtkInteractorStyle
vtkInteractorStyleFlight
vtkInteractorStyleImage
vtkInteractorStyleJoystickActor
vtkInteractorStyleJoystickCamera
vtkInteractorStyleSwitch
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageViewer2.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
of any contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
* Modified source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "vtkImageViewer2.h"
#include "vtkObjectFactory.h"
#include "vtkInteractorStyleImage.h"
//----------------------------------------------------------------------------
vtkImageViewer2* vtkImageViewer2::New()
{
// First try to create the object from the vtkObjectFactory
vtkObject* ret = vtkObjectFactory::CreateInstance("vtkImageViewer2");
if(ret)
{
return (vtkImageViewer2*)ret;
}
// If the factory was unable to create the object, then create it here.
return new vtkImageViewer2;
}
//----------------------------------------------------------------------------
vtkImageViewer2::vtkImageViewer2()
{
this->RenderWindow = vtkRenderWindow::New();
this->Renderer = vtkRenderer::New();
this->ImageActor = vtkImageActor::New();
this->WindowLevel = vtkImageMapToWindowLevelColors::New();
// setup the pipeline
this->ImageActor->SetInput(this->WindowLevel->GetOutput());
this->Renderer->AddProp(this->ImageActor);
this->RenderWindow->AddRenderer(this->Renderer);
this->FirstRender = 1;
this->Interactor = 0;
this->InteractorStyle = 0;
}
//----------------------------------------------------------------------------
vtkImageViewer2::~vtkImageViewer2()
{
this->WindowLevel->Delete();
this->ImageActor->Delete();
this->Renderer->Delete();
this->RenderWindow->Delete();
if (this->Interactor)
{
this->Interactor->Delete();
}
if (this->InteractorStyle)
{
this->InteractorStyle->Delete();
}
}
//----------------------------------------------------------------------------
void vtkImageViewer2::PrintSelf(ostream& os, vtkIndent indent)
{
vtkObject::PrintSelf(os, indent);
os << indent << *this->RenderWindow << endl;
os << indent << *this->Renderer << endl;
os << indent << *this->ImageActor << endl;
}
//----------------------------------------------------------------------------
void vtkImageViewer2::SetSize(int a[2])
{
this->SetSize(a[0],a[1]);
}
//----------------------------------------------------------------------------
void vtkImageViewer2::SetPosition(int a[2])
{
this->SetPosition(a[0],a[1]);
}
class vtkImageViewer2Callback : public vtkCommand
{
public:
void Execute(vtkObject *caller, unsigned long event, void *callData)
{
if (callData)
{
this->InitialWindow = this->IV->GetWindowLevel()->GetWindow();
this->InitialLevel = this->IV->GetWindowLevel()->GetLevel();
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->GetWindowLevel()->SetWindow(newWindow);
this->IV->GetWindowLevel()->SetLevel(newLevel);
this->IV->Render();
}
vtkImageViewer2 *IV;
float InitialWindow;
float InitialLevel;
};
void vtkImageViewer2::SetupInteractor(vtkRenderWindowInteractor *rwi)
{
if (this->Interactor && rwi != this->Interactor)
{
this->Interactor->Delete();
}
if (!this->InteractorStyle)
{
this->InteractorStyle = vtkInteractorStyleImage::New();
vtkImageViewer2Callback *cbk = new vtkImageViewer2Callback;
cbk->IV = this;
this->InteractorStyle->AddObserver(vtkCommand::WindowLevelEvent,cbk);
}
if (!this->Interactor)
{
this->Interactor = rwi;
rwi->Register(this);
}
this->Interactor->SetInteractorStyle(this->InteractorStyle);
this->Interactor->SetRenderWindow(this->RenderWindow);
}
void vtkImageViewer2::Render()
{
if (this->FirstRender)
{
// initialize the size if not set yet
if (this->RenderWindow->GetSize()[0] == 0 && this->ImageActor->GetInput())
{
// get the size from the mappers input
this->WindowLevel->GetInput()->UpdateInformation();
int *ext = this->WindowLevel->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->Renderer->GetActiveCamera()->SetParallelScale(xs < 150 ? 75 : xs/2);
}
this->Renderer->GetActiveCamera()->ParallelProjectionOn();
this->FirstRender = 0;
}
this->RenderWindow->Render();
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageViewer2.h
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
of any contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
* Modified source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
// .NAME vtkImageViewer - Display a 2d image.
// .SECTION Description
// vtkImageViewer is a convenience class for displaying a 2d image. It
// packages up the functionality found in vtkRenderWindow, vtkImager,
// vtkActor2D and vtkImageMapper into a single easy to use class. Behind the
// scenes these four classes are actually used to to provide the required
// functionality. vtkImageViewer is simply a wrapper around them.
// .SECTION See Also
// vtkRenderWindow vtkImager vtkImageMapper vtkActor2D
#ifndef __vtkImageViewer2_h
#define __vtkImageViewer2_h
#include "vtkObject.h"
#include "vtkRenderWindow.h"
#include "vtkImageActor.h"
#include "vtkImageMapToWindowLevelColors.h"
class vtkInteractorStyleImage;
// For placement of origin in the viewer.
#define VTK_IMAGE_VIEWER_UPPER_LEFT 0
#define VTK_IMAGE_VIEWER_LOWER_LEFT 1
class VTK_EXPORT vtkImageViewer2 : public vtkObject
{
public:
static vtkImageViewer2 *New();
vtkTypeMacro(vtkImageViewer2,vtkObject);
void PrintSelf(ostream& os, vtkIndent indent);
// Description:
// Get name of rendering window
char *GetWindowName() {return this->RenderWindow->GetWindowName();};
// Description:
// Render the resulting image.
virtual void Render(void);
// Description:
// Set/Get the input to the viewer.
void SetInput(vtkImageData *in) {this->WindowLevel->SetInput(in);};
vtkImageData *GetInput() { return this->WindowLevel->GetInput();};
// Description:
// What is the possible Min/ Max z slices available.
int GetWholeZMin() {return this->ImageActor->GetWholeZMin();};
int GetWholeZMax() {return this->ImageActor->GetWholeZMax();};
// Description:
// Set/Get the current Z Slice to display
int GetZSlice() {return this->ImageActor->GetZSlice();};
void SetZSlice(int s) {this->ImageActor->SetZSlice(s);};
// Description:
// Sets window/level for mapping pixels to colors.
float GetColorWindow() {return this->WindowLevel->GetWindow();};
float GetColorLevel() {return this->WindowLevel->GetLevel();};
void SetColorWindow(float s) {this->WindowLevel->SetWindow(s);};
void SetColorLevel(float s) {this->WindowLevel->SetLevel(s);};
// Description:
// These are here for using a tk window.
void SetDisplayId(void *a) {this->RenderWindow->SetDisplayId(a);};
void SetWindowId(void *a) {this->RenderWindow->SetWindowId(a);};
void SetParentId(void *a) {this->RenderWindow->SetParentId(a);};
// Description:
// Set/Get the position in screen coordinates of the rendering window.
int *GetPosition() {return this->RenderWindow->GetPosition();};
void SetPosition(int a,int b) {this->RenderWindow->SetPosition(a,b);};
virtual void SetPosition(int a[2]);
// Description:
// Set/Get the size of the window in screen coordinates.
int *GetSize() {return this->RenderWindow->GetSize();};
void SetSize(int a,int b) {this->RenderWindow->SetSize(a,b);};
virtual void SetSize(int a[2]);
// Description:
// Get the internal Window Imager and Mapper
vtkGetObjectMacro(RenderWindow,vtkRenderWindow);
vtkGetObjectMacro(Renderer, vtkRenderer);
vtkGetObjectMacro(ImageActor,vtkImageActor);
vtkGetObjectMacro(WindowLevel,vtkImageMapToWindowLevelColors);
// Description:
// Create and attach an interactor for this window
void SetupInteractor(vtkRenderWindowInteractor *);
protected:
vtkImageViewer2();
~vtkImageViewer2();
vtkImageViewer2(const vtkImageViewer2&);
void operator=(const vtkImageViewer2&);
vtkImageMapToWindowLevelColors *WindowLevel;
vtkRenderWindow *RenderWindow;
vtkRenderer *Renderer;
vtkImageActor *ImageActor;
int FirstRender;
vtkRenderWindowInteractor *Interactor;
vtkInteractorStyleImage *InteractorStyle;
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkInteractorStyleImage.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-2001 Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither name of Ken Martin, Will Schroeder, or Bill Lorensen nor the names
of any contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
* Modified source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=========================================================================*/
#include "vtkInteractorStyleImage.h"
#include "vtkObjectFactory.h"
#include "vtkMath.h"
#include "vtkCommand.h"
//----------------------------------------------------------------------------
vtkInteractorStyleImage *vtkInteractorStyleImage::New()
{
// First try to create the object from the vtkObjectFactory
vtkObject* ret = vtkObjectFactory::CreateInstance("vtkInteractorStyleImage");
if(ret)
{
return (vtkInteractorStyleImage*)ret;
}
// If the factory was unable to create the object, then create it here.
return new vtkInteractorStyleImage;
}
//----------------------------------------------------------------------------
vtkInteractorStyleImage::vtkInteractorStyleImage()
{
this->MotionFactor = 10.0;
this->State = VTK_INTERACTOR_STYLE_IMAGE_NONE;
this->RadianToDegree = 180.0 / vtkMath::Pi();
this->WindowLevelStartPosition[0] = 0;
this->WindowLevelStartPosition[1] = 0;
this->WindowLevelCurrentPosition[0] = 0;
this->WindowLevelCurrentPosition[1] = 0;
}
//----------------------------------------------------------------------------
vtkInteractorStyleImage::~vtkInteractorStyleImage()
{
}
//----------------------------------------------------------------------------
void vtkInteractorStyleImage::OnMouseMove(int vtkNotUsed(ctrl),
int vtkNotUsed(shift),
int x, int y)
{
if (this->State == VTK_INTERACTOR_STYLE_IMAGE_WINDOW_LEVEL)
{
this->FindPokedCamera(x, y);
this->WindowLevelXY(x, y);
}
else if (this->State == VTK_INTERACTOR_STYLE_IMAGE_PAN)
{
this->FindPokedCamera(x, y);
this->PanXY(x, y, this->LastPos[0], this->LastPos[1]);
}
else if (this->State == VTK_INTERACTOR_STYLE_IMAGE_ZOOM)
{
this->FindPokedCamera(x, y);
this->DollyXY(x - this->LastPos[0], y - this->LastPos[1]);
}
else if (this->State == VTK_INTERACTOR_STYLE_IMAGE_SPIN)
{
this->FindPokedCamera(x, y);
this->SpinXY(x, y, this->LastPos[0], this->LastPos[1]);
}
this->LastPos[0] = x;
this->LastPos[1] = y;
}
//----------------------------------------------------------------------------
void vtkInteractorStyleImage::WindowLevelXY(int x, int y)
{
this->WindowLevelCurrentPosition[0] = x;
this->WindowLevelCurrentPosition[1] = y;
if (this->HasObserver(vtkCommand::WindowLevelEvent))
{
this->InvokeEvent(vtkCommand::WindowLevelEvent,NULL);
return;
}
}
//----------------------------------------------------------------------------
void vtkInteractorStyleImage::PanXY(int x, int y, int oldX, int oldY)
{
vtkCamera *cam;
double viewFocus[4], focalDepth, viewPoint[3];
float newPickPoint[4], oldPickPoint[4], motionVector[3];
if (this->CurrentRenderer == NULL)
{
return;
}
// calculate the focal depth since we'll be using it a lot
cam = this->CurrentRenderer->GetActiveCamera();
cam->GetFocalPoint(viewFocus);
this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1],
viewFocus[2], viewFocus);
focalDepth = viewFocus[2];
this->ComputeDisplayToWorld(double(x), double(y),
focalDepth, newPickPoint);
// has to recalc old mouse point since the viewport has moved,
// so can't move it outside the loop
this->ComputeDisplayToWorld(double(oldX),double(oldY),
focalDepth, oldPickPoint);
// camera motion is reversed
motionVector[0] = oldPickPoint[0] - newPickPoint[0];
motionVector[1] = oldPickPoint[1] - newPickPoint[1];
motionVector[2] = oldPickPoint[2] - newPickPoint[2];
cam->GetFocalPoint(viewFocus);
cam->GetPosition(viewPoint);
cam->SetFocalPoint(motionVector[0] + viewFocus[0],
motionVector[1] + viewFocus[1],
motionVector[2] + viewFocus[2]);
cam->SetPosition(motionVector[0] + viewPoint[0],
motionVector[1] + viewPoint[1],
motionVector[2] + viewPoint[2]);
vtkRenderWindowInteractor *rwi = this->Interactor;
if (this->CurrentLight)
{
/* get the first light */
this->CurrentLight->SetPosition(cam->GetPosition());
this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
}
rwi->Render();
}
//----------------------------------------------------------------------------
void vtkInteractorStyleImage::DollyXY(int vtkNotUsed(dx), int dy)
{
vtkCamera *cam;
double dyf = this->MotionFactor * (double)(dy) / (double)(this->Center[1]);
double zoomFactor = pow((double)1.1, dyf);
if (this->CurrentRenderer == NULL)
{
return;
}
cam = this->CurrentRenderer->GetActiveCamera();
if (cam->GetParallelProjection())
{
cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
}
else
{
cam->Dolly(zoomFactor);
this->CurrentRenderer->ResetCameraClippingRange();
}
vtkRenderWindowInteractor *rwi = this->Interactor;
if (this->CurrentLight)
{
/* get the first light */
this->CurrentLight->SetPosition(cam->GetPosition());
this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
}