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

ENH: new vtkCoordinate support

parent 54cd20c7
......@@ -49,6 +49,7 @@ vtkCellTypes \
vtkCharArray \
vtkCollection \
vtkContourValues \
vtkCoordinate \
vtkDataObject \
vtkDataSetAttributes \
vtkDoubleArray \
......
......@@ -56,15 +56,9 @@ vtkActor2D::vtkActor2D()
this->Visibility = 1; // ON
this->SelfCreatedProperty = 0;
this->Property = (vtkProperty2D*) NULL;
this->PositionType = VTK_VIEW_COORD;
this->DisplayPosition[0] = 0;
this->DisplayPosition[1] = 0;
this->ViewPosition[0] = -1.0;
this->ViewPosition[1] = -1.0;
this->WorldPosition[0] = 0;
this->WorldPosition[1] = 0;
this->WorldPosition[2] = 0;
this->Mapper = (vtkMapper2D*) NULL;
this->PositionCoordinate = vtkCoordinate::New();
this->PositionCoordinate->SetCoordinateSystem(VTK_VIEWPORT);
}
// Description:
......@@ -73,6 +67,11 @@ vtkActor2D::vtkActor2D()
vtkActor2D::~vtkActor2D()
{
if (this->SelfCreatedProperty) this->Property->Delete();
if (this->PositionCoordinate)
{
this->PositionCoordinate->Delete();
this->PositionCoordinate = NULL;
}
}
void vtkActor2D::PrintSelf(ostream& os, vtkIndent indent)
......@@ -83,210 +82,30 @@ void vtkActor2D::PrintSelf(ostream& os, vtkIndent indent)
os << indent << "Layer Number: " << this->LayerNumber << "\n";
os << indent << "Visibility: " << (this->Visibility ? "On\n" : "Off\n");
char posString[64];
switch (this->PositionType)
os << indent << "PositionCoordinate: " << this->PositionCoordinate << "\n";
if (this->PositionCoordinate)
{
case VTK_VIEW_COORD:
strcpy(posString, "VTK_VIEW_COORD\0");
break;
case VTK_DISPLAY_COORD:
strcpy(posString, "VTK_DISPLAY_COORD\0");
break;
case VTK_WORLD_COORD:
strcpy(posString, "VTK_WORLD_COORD\0");
break;
default:
strcpy(posString, "UNKNOWN!\0");
break;
this->PositionCoordinate->PrintSelf(os, indent.GetNextIndent());
}
os << indent << "Position Type: " << posString << "\n";
os << indent << "Display Position: (" << this->DisplayPosition[0] << ","
<< this->DisplayPosition[1] << ")\n";
os << indent << "View Position: (" << this->ViewPosition[0] << ","
<< this->ViewPosition[1] << ")\n";
os << indent << "World Position: (" << this->WorldPosition[0] << ","
<< this->WorldPosition[1] << "," << this->WorldPosition[2] << ")\n";
os << indent << "Self Created Property: " << (this->SelfCreatedProperty ? "Yes\n" : "No\n");
os << indent << "Property: " << this->Property << "\n";
if (this->Property) this->Property->PrintSelf(os, indent.GetNextIndent());
os << indent << "Mapper: " << this->Mapper << "\n";
if (this->Mapper) this->Mapper->PrintSelf(os, indent.GetNextIndent());
}
// Description:
// Sets the actor2D's position in view coordinates. Updates
// the PositionType.
void vtkActor2D::SetViewPosition(float XPos, float YPos)
{
if ((XPos != this->ViewPosition[0]) || (YPos != this->ViewPosition[1]))
{
this->ViewPosition[0] = XPos;
this->ViewPosition[1] = YPos;
this->Modified();
}
this->PositionType = VTK_VIEW_COORD;
}
// Description:
// Set the actor2D's position in display coordinates. Updates
// the PositionType.
// Set the actor2D's position in display coordinates.
void vtkActor2D::SetDisplayPosition(int XPos, int YPos)
{
if ((XPos != this->DisplayPosition[0]) || (YPos != this->DisplayPosition[1]))
{
this->DisplayPosition[0] = XPos;
this->DisplayPosition[1] = YPos;
this->Modified();
}
this->PositionType = VTK_DISPLAY_COORD;
}
// Description:
// Set the actor2D's position in world coordinates. Updates
// the PositionType. To have an actor2D follow a regular actor,
// just set the 2D actor's world position to the position of the
// regular actor.
void vtkActor2D::SetWorldPosition(float XPos, float YPos, float ZPos)
{
if ( (XPos != this->WorldPosition[0]) ||
(YPos != this->WorldPosition[1]) ||
(ZPos != this->WorldPosition[2]))
{
this->WorldPosition[0] = XPos;
this->WorldPosition[1] = YPos;
this->WorldPosition[2] = ZPos;
this->Modified();
}
this->PositionType = VTK_WORLD_COORD;
}
// Description:
// Returns the actor's pixel position relative to the viewports
// lower left corner
int *vtkActor2D::GetComputedViewportPixelPosition(vtkViewport* viewport)
{
float* actorPos;
float temp[2];
// Get the actor's position in viewport coordinates
switch (this->PositionType)
if (!this->PositionCoordinate)
{
case VTK_VIEW_COORD:
viewport->SetViewPoint(this->ViewPosition[0], this->ViewPosition[1], 0);
viewport->ViewToDisplay();
actorPos = viewport->GetDisplayPoint();
break;
case VTK_DISPLAY_COORD:
// Put the int DisplayPosition into a float array
// and point actorPos at that array
temp[0] = this->DisplayPosition[0];
temp[1] = this->DisplayPosition[1];
actorPos = temp;
break;
case VTK_WORLD_COORD:
viewport->SetWorldPoint(this->WorldPosition[0], this->WorldPosition[1],
this->WorldPosition[2], 1);
viewport->WorldToDisplay();
actorPos = viewport->GetDisplayPoint();
break;
default:
vtkErrorMacro(<< "Unknown position type: " << this->PositionType);
break;
this->PositionCoordinate = vtkCoordinate::New();
}
int* winSize = viewport->GetVTKWindow()->GetSize();
float* vpt = viewport->GetViewport();
this->ComputedDisplayPosition[0] =
(int)(actorPos[0] + 0.5 - vpt[0]*winSize[0]);
this->ComputedDisplayPosition[1] =
(int)(actorPos[1] + 0.5 - vpt[1]*winSize[1]);
return this->ComputedDisplayPosition;
}
// Description:
// Returns the actor's pixel position in a window with origin
// in the upper left (drawing origin for both X and Win32).
int *vtkActor2D::GetComputedDisplayPosition(vtkViewport* viewport)
{
float* actorPos;
float temp[2];
// Get the actor's position in viewport coordinates
switch (this->PositionType)
{
case VTK_VIEW_COORD:
viewport->SetViewPoint(this->ViewPosition[0], this->ViewPosition[1], 0);
viewport->ViewToDisplay();
actorPos = viewport->GetDisplayPoint();
break;
case VTK_DISPLAY_COORD:
// Put the int DisplayPosition into a float array
// and point actorPos at that array
temp[0] = this->DisplayPosition[0];
temp[1] = this->DisplayPosition[1];
actorPos = temp;
break;
case VTK_WORLD_COORD:
viewport->SetWorldPoint(this->WorldPosition[0], this->WorldPosition[1],
this->WorldPosition[2], 1);
viewport->WorldToDisplay();
actorPos = viewport->GetDisplayPoint();
break;
default:
vtkErrorMacro(<< "Unknown position type: " << this->PositionType);
break;
}
int* winSize = viewport->GetVTKWindow()->GetSize();
this->ComputedDisplayPosition[0] = (int)(actorPos[0] + 0.5);
// X and Win32 drawing origins have 0,0 at top left
this->ComputedDisplayPosition[1] = (int)(winSize[1] - (actorPos[1] + 0.5));
return this->ComputedDisplayPosition;
}
float *vtkActor2D::GetComputedWorldPosition(vtkViewport* viewport)
{
float* actorPos;
static float worldPos[3];
// Get the actor's position in viewport coordinates
switch (this->PositionType)
{
case VTK_VIEW_COORD:
viewport->SetViewPoint(this->ViewPosition[0], this->ViewPosition[1], 0);
viewport->ViewToWorld();
actorPos = viewport->GetWorldPoint();
worldPos[0] = actorPos[0];
worldPos[1] = actorPos[1];
worldPos[2] = actorPos[2];
break;
case VTK_DISPLAY_COORD:
viewport->SetDisplayPoint(this->DisplayPosition[0], this->DisplayPosition[1], 0);
viewport->DisplayToWorld();
actorPos = viewport->GetWorldPoint();
worldPos[0] = actorPos[0];
worldPos[1] = actorPos[1];
worldPos[2] = actorPos[2];
break;
case VTK_WORLD_COORD:
worldPos[0] = this->WorldPosition[0];
worldPos[1] = this->WorldPosition[1];
worldPos[2] = this->WorldPosition[2];
break;
default:
vtkErrorMacro(<< "Unknown position type: " << this->PositionType);
break;
}
return worldPos;
this->PositionCoordinate->SetCoordinateSystem(VTK_DISPLAY);
this->PositionCoordinate->SetValue((float)XPos,(float)YPos,0.0);
}
// Description:
......
......@@ -55,15 +55,11 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#define __vtkActor2D_h
#include "vtkReferenceCount.h"
#include "vtkTransform.h"
#include "vtkCoordinate.h"
class vtkMapper2D;
class vtkProperty2D;
class vtkViewport;
#define VTK_VIEW_COORD 0
#define VTK_DISPLAY_COORD 1
#define VTK_WORLD_COORD 2
class VTK_EXPORT vtkActor2D : public vtkReferenceCount
{
......@@ -96,49 +92,23 @@ public:
vtkProperty2D* GetProperty();
vtkSetObjectMacro(Property, vtkProperty2D);
vtkGetVectorMacro(ViewPosition, float, 2);
void SetViewPosition(float XPos, float YPos);
void SetViewPosition(float arr[2]) {this->SetViewPosition(arr[0], arr[1]);};
vtkSetReferenceCountedObjectMacro(PositionCoordinate,vtkCoordinate);
vtkGetObjectMacro(PositionCoordinate,vtkCoordinate);
vtkGetVectorMacro(DisplayPosition, int, 2);
void SetDisplayPosition(int XPos, int YPos);
void SetDisplayPosition(int arr[2]) {this->SetDisplayPosition(arr[0], arr[1]);};
vtkGetVectorMacro(WorldPosition, float, 3);
void SetWorldPosition(float XPos, float YPos, float ZPos);
void SetWorldPosition(float arr[3]) {this->SetWorldPosition(arr[0], arr[1], arr[2]);};
virtual int *GetComputedDisplayPosition(vtkViewport* viewport);
virtual int *GetComputedViewportPixelPosition(vtkViewport* viewport);
virtual float *GetComputedWorldPosition(vtkViewport* viewport);
vtkSetMacro(PositionType, int);
vtkGetMacro(PositionType, int);
void SetPositionTypeToView() {this->SetPositionType(VTK_VIEW_COORD);};
void SetPositionTypeToDisplay() {this->SetPositionType(VTK_DISPLAY_COORD);};
void SetPositionTypeToWorld() {this->SetPositionType(VTK_WORLD_COORD);};
void SetDisplayPosition(int,int);
protected:
protected:
float Orientation;
float Scale[2];
float ViewPosition[2];
int DisplayPosition[2];
float WorldPosition[3];
int ComputedDisplayPosition[2];
int PositionType;
int LayerNumber;
int Visibility;
int SelfCreatedProperty;
vtkTransform Transform;
vtkProperty2D *Property;
vtkMapper2D *Mapper;
vtkCoordinate *PositionCoordinate;
};
#endif
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCoordinate.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 1993-1998 Ken Martin, Will Schroeder, Bill Lorensen.
This software is copyrighted by Ken Martin, Will Schroeder and Bill Lorensen.
The following terms apply to all files associated with the software unless
explicitly disclaimed in individual files. This copyright specifically does
not apply to the related textbook "The Visualization Toolkit" ISBN
013199837-4 published by Prentice Hall which is covered by its own copyright.
The authors hereby grant permission to use, copy, and distribute this
software and its documentation for any purpose, provided that existing
copyright notices are retained in all copies and that this notice is included
verbatim in any distributions. Additionally, the authors grant permission to
modify this software and its documentation for any purpose, provided that
such modifications are not distributed without the explicit consent of the
authors and that existing copyright notices are retained in all copies. Some
of the algorithms implemented by this software are patented, observe all
applicable patent law.
IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF,
EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN
"AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
=========================================================================*/
#include "vtkCoordinate.h"
#include "vtkViewport.h"
// Description:
// Creates an Coordinate with the following defaults:
// value of 0, 0, 0 in world coordinates
vtkCoordinate::vtkCoordinate()
{
this->CoordinateSystem = VTK_WORLD;
this->Value[0] = 0.0;
this->Value[1] = 0.0;
this->Value[2] = 0.0;
this->ReferenceCoordinate = NULL;
this->Computing = 0;
}
// Description:
// Destroy a Coordinate.
vtkCoordinate::~vtkCoordinate()
{
this->SetReferenceCoordinate(NULL);
}
void vtkCoordinate::PrintSelf(ostream& os, vtkIndent indent)
{
this->vtkReferenceCount::PrintSelf(os,indent);
char posString[64];
switch (this->CoordinateSystem)
{
case VTK_DISPLAY:
strcpy(posString, "Display"); break;
case VTK_NORMALIZED_DISPLAY:
strcpy(posString, "Normalized Display"); break;
case VTK_VIEWPORT:
strcpy(posString, "Viewport"); break;
case VTK_NORMALIZED_VIEWPORT:
strcpy(posString, "Normalized Viewport"); break;
case VTK_VIEW:
strcpy(posString, "View"); break;
case VTK_WORLD:
strcpy(posString, "World"); break;
default:
strcpy(posString, "UNKNOWN!"); break;
}
os << indent << "Coordinate System: " << posString << "\n";
os << indent << "Value: (" << this->Value[0] << ","
<< this->Value[1] << "," << this->Value[2] << ")\n";
os << indent << "ReferenceCoordinate: " << this->ReferenceCoordinate << "\n";
}
float *vtkCoordinate::GetComputedWorldValue(vtkViewport* viewport)
{
float *val = this->ComputedWorldValue;
// prevent infinite loops
if (this->Computing) return val;
this->Computing = 1;
val[0] = this->Value[0];
val[1] = this->Value[1];
val[2] = this->Value[2];
if (this->ReferenceCoordinate && this->CoordinateSystem != VTK_WORLD)
{
float RefValue[3];
int *ival;
ival = this->ReferenceCoordinate->GetComputedDisplayValue(viewport);
RefValue[0] = (float)(ival[0]);
RefValue[1] = (float)(ival[1]);
RefValue[2] = (float)(ival[2]);
// convert to current coordinate system
switch (this->CoordinateSystem)
{
case VTK_NORMALIZED_DISPLAY:
viewport->DisplayToNormalizedDisplay(RefValue[0],RefValue[1]);
case VTK_VIEWPORT:
viewport->DisplayToNormalizedDisplay(RefValue[0],RefValue[1]);
viewport->NormalizedDisplayToViewport(RefValue[0],RefValue[1]);
case VTK_NORMALIZED_VIEWPORT:
viewport->DisplayToNormalizedDisplay(RefValue[0],RefValue[1]);
viewport->NormalizedDisplayToViewport(RefValue[0],RefValue[1]);
viewport->ViewportToNormalizedViewport(RefValue[0],RefValue[1]);
case VTK_VIEW:
viewport->DisplayToNormalizedDisplay(RefValue[0],RefValue[1]);
viewport->NormalizedDisplayToViewport(RefValue[0],RefValue[1]);
viewport->ViewportToNormalizedViewport(RefValue[0],RefValue[1]);
viewport->NormalizedViewportToView(RefValue[0],RefValue[1],RefValue[2]);
}
// add to current value
val[0] += RefValue[0];
val[1] += RefValue[1];
val[2] += RefValue[2];
}
// compute our WC
switch (this->CoordinateSystem)
{
case VTK_DISPLAY:
viewport->DisplayToNormalizedDisplay(val[0],val[1]);
case VTK_NORMALIZED_DISPLAY:
viewport->NormalizedDisplayToViewport(val[0],val[1]);
case VTK_VIEWPORT:
viewport->ViewportToNormalizedViewport(val[0],val[1]);
case VTK_NORMALIZED_VIEWPORT:
viewport->NormalizedViewportToView(val[0],val[1],val[2]);
case VTK_VIEW:
viewport->ViewToWorld(val[0],val[1],val[2]);
}
if (this->ReferenceCoordinate && this->CoordinateSystem == VTK_WORLD)
{
float *RefValue;
RefValue = this->ReferenceCoordinate->GetComputedWorldValue(viewport);
val[0] += RefValue[0];
val[1] += RefValue[1];
val[2] += RefValue[2];
}
this->Computing = 0;
return val;
}
int *vtkCoordinate::GetComputedViewportValue(vtkViewport* viewport)
{
int *d = this->GetComputedDisplayValue(viewport);
float f[2];
f[0] = (float)d[0];
f[1] = (float)d[1];
viewport->DisplayToNormalizedDisplay(f[0],f[1]);
viewport->NormalizedDisplayToViewport(f[0],f[1]);
this->ComputedViewportValue[0] = (int)f[0];
this->ComputedViewportValue[1] = (int)f[1];
return this->ComputedViewportValue;
}
int *vtkCoordinate::GetComputedLocalDisplayValue(vtkViewport* viewport)
{
float a[2];
this->GetComputedDisplayValue(viewport);
a[0] = (float)this->ComputedDisplayValue[0];
a[1] = (float)this->ComputedDisplayValue[1];
viewport->DisplayToLocalDisplay(a[0],a[1]);
this->ComputedDisplayValue[0] = (int)a[0];
this->ComputedDisplayValue[1] = (int)a[1];
vtkDebugMacro("Returning LocalDisplayValue of : " <<
this->ComputedDisplayValue[0] << " , " <<
this->ComputedDisplayValue[1]);
return this->ComputedDisplayValue;
}
int *vtkCoordinate::GetComputedDisplayValue(vtkViewport* viewport)
{
float val[3];
// prevent infinite loops
if (this->Computing) return this->ComputedDisplayValue;
this->Computing = 1;
val[0] = this->Value[0];
val[1] = this->Value[1];
val[2] = this->Value[2];
// compute our WC
switch (this->CoordinateSystem)
{
case VTK_WORLD:
if (this->ReferenceCoordinate)
{
float *RefValue;
RefValue = this->ReferenceCoordinate->GetComputedWorldValue(viewport);
val[0] += RefValue[0];
val[1] += RefValue[1];
val[2] += RefValue[2];
}
viewport->WorldToView(val[0],val[1],val[2]);
case VTK_VIEW:
viewport->ViewToNormalizedViewport(val[0],val[1],val[2]);
case VTK_NORMALIZED_VIEWPORT:
viewport->NormalizedViewportToViewport(val[0],val[1]);
case VTK_VIEWPORT:
viewport->ViewportToNormalizedDisplay(val[0],val[1]);
case VTK_NORMALIZED_DISPLAY:
viewport->NormalizedDisplayToDisplay(val[0],val[1]);
}
this->ComputedDisplayValue[0] = (int)(val[0]);
this->ComputedDisplayValue[1] = (int)(val[1]);
// if we have a reference coordinate then get that first
if (this->ReferenceCoordinate && this->CoordinateSystem != VTK_WORLD)
{
int *RefValue;
RefValue = this->ReferenceCoordinate->GetComputedDisplayValue(viewport);
this->ComputedDisplayValue[0] += RefValue[0];
this->ComputedDisplayValue[1] += RefValue[1];
}
this->Computing = 0;
vtkDebugMacro("Returning DisplayValue of : " <<
this->ComputedDisplayValue[0] << " , " <<
this->ComputedDisplayValue[1]);
return this->ComputedDisplayValue;
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCoordinate.h
Language: C++