[vtk-developers] parallel camera: how to get image to fill viewport?
Steve M. Robbins
steven.robbins at videotron.ca
Tue Nov 15 14:39:41 EST 2005
This has elicited no response on vtkusers.
May I bend the ear of the vtk gods?
Is my request feasible or am I on the wrong track?
----- Forwarded message from "Steve M. Robbins" <steven.robbins at videotron.ca> -----
Date: Sat, 12 Nov 2005 03:20:50 -0500
From: "Steve M. Robbins" <steven.robbins at videotron.ca>
Subject: [vtkusers] parallel camera: how to get image to fill viewport?
To: VTK List <vtkusers at vtk.org>
Howdy,
I'm using vtkImageViewer2 to render an image. I'd like to set the
camera scale so that the image fills the window width or height.
I have 1 viewport that fills the entire window.
I've set the camera to parallel projection, set the focal point to the
centre of the image, set the position to just above the centre, and
set view up to the Y axis. After some time grepping through the
sources, I've found that setting the camera parallel scale to (
windowHeight - 1 ) / 2 works well. See attached code for details.
So I figure that if the image dimension and window dimension match,
the transformation should be the identity. I've stumbled across the
following comment in vtkViewport.cxx:
// the 0.5 offset is here because the viewport uses pixel centers
// while the display uses pixel edges.
So I started computing the various transforms and dumping them (see
attached code). It turns out that my code will generate an identity
mapping between world and viewport, but there's a half-pixel shift in
world-to-display. So I am presuming that the world coordinate system
specifies pixel centres, too. True?
At any rate, I can get an identity world->viewport mapping when the
image and viewport sizes agree, but only when they are square.
As the attached code and output shows, when the width != height,
I no longer get the identity.
How should I set up the camera so that the image fills the viewport?
Thanks,
-Steve
P.S. Attached code was built and run against CVS HEAD, as of
late Friday evening. Also built and run against 4.2 with
similar results.
import vtk.vtkCamera;
import vtk.vtkCoordinate;
import vtk.vtkRenderWindow;
import vtk.vtkRenderer;
import vtk.vtkVersion;
public class TestParallelCamera
{
static
{
System.loadLibrary( "vtkCommonJava" );
System.loadLibrary( "vtkFilteringJava" );
System.loadLibrary( "vtkIOJava" );
System.loadLibrary( "vtkImagingJava" );
System.loadLibrary( "vtkGraphicsJava" );
System.loadLibrary( "vtkRenderingJava" );
}
private final vtkRenderWindow renderWindow;
private final vtkRenderer renderer;
private final vtkCamera camera;
interface Converter
{
public double[] convert( double x, double y );
}
public TestParallelCamera()
{
renderer = new vtkRenderer();
//renderer.DebugOn();
renderWindow = new vtkRenderWindow();
renderWindow.AddRenderer( renderer );
camera = renderer.GetActiveCamera();
camera.ParallelProjectionOn();
//camera.DebugOn();
}
void setupCamera( int width, int height )
{
renderWindow.SetSize( width, height );
double cameraDistance = 10;
double sliceHalfThickness = 1;
double xFocal = ( width - 1 ) / 2.0;
double yFocal = ( height - 1 ) / 2.0;
camera.SetFocalPoint( xFocal, yFocal, 0.0 );
camera.SetPosition( xFocal, yFocal, cameraDistance );
camera.SetViewUp( 0, 1, 0 );
camera.SetClippingRange( cameraDistance - sliceHalfThickness, cameraDistance + sliceHalfThickness );
camera.SetParallelScale( ( height - 1 ) / 2.0 );
//System.out.println( renderer.Print() );
//System.out.println( camera.Print() );
}
private void dumpTransformMatrix( String title, Converter converter )
{
double[] originDisplay = converter.convert( 0, 0 );
double[] xDisplay = converter.convert( 1, 0 );
double[] yDisplay = converter.convert( 0, 1 );
xDisplay[0] -= originDisplay[0];
xDisplay[1] -= originDisplay[1];
yDisplay[0] -= originDisplay[0];
yDisplay[1] -= originDisplay[1];
System.out.println( title );
System.out.println( xDisplay[0] + " " + yDisplay[0] + " " + originDisplay[0] );
System.out.println( xDisplay[1] + " " + yDisplay[1] + " " + originDisplay[1] );
System.out.println();
}
void dumpTransform()
{
System.out.println( "Renderer tiled aspect ratio: " + renderer.GetTiledAspectRatio() );
System.out.println( "Camera parallel scale: " + camera.GetParallelScale() );
System.out.println();
dumpTransformMatrix( "Normalized Viewport-to-Viewport transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToNormalizedViewport();
coord.SetValue( x, y );
return coord.GetComputedDoubleViewportValue( renderer );
}
} );
dumpTransformMatrix( "Normalized Display-to-Display transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToNormalizedDisplay();
coord.SetValue( x, y );
return coord.GetComputedDoubleDisplayValue( renderer );
}
} );
dumpTransformMatrix( "View-to-Viewport transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToView();
coord.SetValue( x, y );
return coord.GetComputedDoubleViewportValue( renderer );
}
} );
dumpTransformMatrix( "View-to-Display transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToView();
coord.SetValue( x, y );
return coord.GetComputedDoubleDisplayValue( renderer );
}
} );
dumpTransformMatrix( "World-to-Viewport transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToWorld();
coord.SetValue( x, y );
return coord.GetComputedDoubleViewportValue( renderer );
}
} );
dumpTransformMatrix( "World-to-Display transform",
new Converter()
{
public double[] convert( double x, double y )
{
vtkCoordinate coord = new vtkCoordinate();
coord.SetCoordinateSystemToWorld();
coord.SetValue( x, y );
return coord.GetComputedDoubleDisplayValue( renderer );
}
} );
}
public static void main( String[] args )
{
vtkVersion version = new vtkVersion();
System.out.println( "Loaded VTK version " + version.GetVTKVersion() );
System.out.println( " Source version: " + version.GetVTKSourceVersion() );
TestParallelCamera tester = new TestParallelCamera();
System.out.println( "5x5" );
tester.setupCamera( 5, 5 );
tester.dumpTransform();
System.out.println();
System.out.println( "15x5" );
tester.setupCamera( 15, 5 );
tester.dumpTransform();
}
}
Loaded VTK version 5.1.0
Source version: vtk version 5.1.0, vtk source $Revision: 1.2260 $, $Date: 2005/11/11 10:51:14 $ (GMT)
5x5
Renderer tiled aspect ratio: 1.0
Camera parallel scale: 2.0
Normalized Viewport-to-Viewport transform
4.0 0.0 0.0
0.0 4.0 0.0
Normalized Display-to-Display transform
5.0 0.0 0.0
0.0 5.0 0.0
View-to-Viewport transform
2.0 0.0 2.0
0.0 2.0 2.0
View-to-Display transform
2.0 0.0 2.5
0.0 2.0 2.5
World-to-Viewport transform
1.0 0.0 0.0
0.0 1.0 0.0
World-to-Display transform
1.0 0.0 0.5
0.0 1.0 0.5
15x5
Renderer tiled aspect ratio: 3.0
Camera parallel scale: 2.0
Normalized Viewport-to-Viewport transform
14.0 0.0 0.0
0.0 4.0 0.0
Normalized Display-to-Display transform
15.0 0.0 0.0
0.0 5.0 0.0
View-to-Viewport transform
7.0 0.0 7.0
0.0 2.0 2.0
View-to-Display transform
7.0 0.0 7.5
0.0 2.0 2.5
World-to-Viewport transform
1.1666666666666665 0.0 -1.1666666666666656
0.0 1.0 0.0
World-to-Display transform
1.1666666666666665 0.0 -0.6666666666666656
0.0 1.0 0.5
_______________________________________________
This is the private VTK discussion list.
Please keep messages on-topic. Check the FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers
----- End forwarded message -----
More information about the vtk-developers
mailing list