[vtkusers] Actors2D not magnified by vtkWindowToImageFilter->SetMagnification()
Christopher Bruns
cmbruns at stanford.edu
Tue Sep 19 19:53:06 EDT 2006
As usual vtk does just about everything I need. And I am extremely
grateful for the patch to correct vtkRibbonFilter that I got via this
board a few months ago. But I have encountered one more problem.
I want to save an image file of my vtk render window. I want the
image to be of higher resolution than that shown in the vtk render
window. The recommended way to save such an enchanced resolution image
is to set vtkWindowToImageFilter->SetMagnification() to a value larger
that one.
This methods works fine for the rendered 3D objects, but my Actor2D
objects are either not scaled (text and polydata), or scaled but
bizarrely placed and tiled (images). I have included an example test
C++ program to demonstrate the problem, in case there is anyone who has
the time and knowledge to suggest a workaround. Thanks in advance.
-Christopher Bruns
// Example program to demonstrate non-magnification of Actor2Ds
// TextActor and polydata are not magnified,
// while image is magnified, but is also wrongly duplicated and tiled
across the screen
#include "vtkActor2D.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkImageData.h"
#include "vtkTextMapper.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkTextProperty.h"
#include "vtkWindowToImageFilter.h"
#include "vtkPNGWriter.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkPolyDataMapper2D.h"
#include "vtkPolyData.h"
#include "vtkProperty2D.h"
#include "vtkImageMapper.h"
#include "vtkConeSource.h"
#include "vtkActor.h"
#include "vtkPolyDataMapper.h";
int main() {
// 3D cone
vtkConeSource * coneSource = vtkConeSource::New();
vtkPolyDataMapper * coneMapper = vtkPolyDataMapper::New();
coneMapper->SetInput(coneSource->GetOutput());
vtkActor * coneActor = vtkActor::New();
coneActor->SetMapper(coneMapper);
// Create small 2D image
vtkImageData * imageData = vtkImageData::New();
int w = 10;
int h = 10;
imageData->SetDimensions(w,h,1);
imageData->SetScalarTypeToUnsignedChar();
imageData->SetNumberOfScalarComponents(4);
imageData->AllocateScalars();
for (int x = 0; x < w; ++x)
for (int y = 0; y < h; ++y) {
imageData->SetScalarComponentFromDouble(x, y, 0, 3,
127); // alpha
imageData->SetScalarComponentFromDouble(x, y, 0, 0, 0);
// red
imageData->SetScalarComponentFromDouble(x, y, 0, 1,
255); // green
imageData->SetScalarComponentFromDouble(x, y, 0, 2, 0);
// blue
}
vtkImageMapper * imageMapper = vtkImageMapper::New();
imageMapper->SetInput(imageData);
imageMapper->SetColorWindow(255.0);
imageMapper->SetColorLevel(127.5);
imageMapper->SetRenderToRectangle(1);
vtkActor2D * imageActor = vtkActor2D::New();
imageActor->SetMapper(imageMapper);
imageActor->SetPosition(10,50);
imageActor->SetPosition2(0.3,0.3);
// Create Line Segment
vtkPoints * points = vtkPoints::New();
points->InsertNextPoint(10, 30, 0);
points->InsertNextPoint(60, 40, 0);
vtkCellArray * cells = vtkCellArray::New();
cells->InsertNextCell(points->GetNumberOfPoints());
for (int i = 0; i < points->GetNumberOfPoints(); i ++)
cells->InsertCellPoint(i);
vtkPolyData * polyData = vtkPolyData::New();
polyData->SetPoints(points);
polyData->SetLines(cells);
vtkPolyDataMapper2D * mapper = vtkPolyDataMapper2D::New();
mapper->SetInput(polyData);
vtkActor2D * lineActor = vtkActor2D::New();
lineActor->SetMapper(mapper);
lineActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
lineActor->GetProperty()->SetOpacity(1.0);
lineActor->GetProperty()->SetLineWidth(3.0);
lineActor->SetVisibility(1);
vtkTextMapper * textMapper = vtkTextMapper::New();
textMapper->SetInput("Test String");
textMapper->GetTextProperty()->SetColor(0, 0, 0);
vtkActor2D * textActor = vtkActor2D::New();
textActor->SetMapper(textMapper);
textActor->SetPosition(10,10);
// Renderer
vtkRenderer *ren1 = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
renWin->AddRenderer(ren1);
// Trackball interaction by default
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
vtkInteractorStyleSwitch *trackBall = vtkInteractorStyleSwitch::New();
trackBall->SetCurrentStyleToTrackballCamera();
iren->SetInteractorStyle(trackBall);
// Window
iren->SetRenderWindow(renWin);
ren1->SetBackground(1.0,1.0,1.0); // white background
ren1->AddActor(coneActor); // magnifies perfectly
ren1->AddActor(textActor); // no magification
ren1->AddActor(lineActor); // no magification
ren1->AddActor(imageActor); // magnified, but misplaced, duplicated,
and tiled
renWin->SetSize(100, 100);
renWin->Render();
// write normal sized image
vtkWindowToImageFilter * wti = vtkWindowToImageFilter::New();
wti->SetInput(renWin);
wti->SetMagnification(1);
wti->Update();
vtkPNGWriter * pngWriter = vtkPNGWriter::New();
pngWriter->SetFileName("normalSize.png");
pngWriter->SetInput(wti->GetOutput());
pngWriter->Write();
// write magnified image
vtkWindowToImageFilter * wti2 = vtkWindowToImageFilter::New();
wti2->SetInput(renWin);
wti2->SetMagnification(2);
wti2->Update();
vtkPNGWriter * pngWriter2 = vtkPNGWriter::New();
pngWriter2->SetFileName("doubleSize.png");
pngWriter2->SetInput(wti2->GetOutput());
pngWriter2->Write();
iren->Start();
// Clean up data structures -- TODO - incomplete
ren1->Delete();
renWin->Delete();
pngWriter->Delete();
pngWriter2->Delete();
wti->Delete();
wti2->Delete();
iren->Delete();
trackBall->Delete();
textActor->Delete();
textMapper->Delete();
lineActor->Delete();
mapper->Delete();
polyData->Delete();
cells->Delete();
points->Delete();
imageActor->Delete();
imageMapper->Delete();
imageData->Delete();
return 0;
}
More information about the vtkusers
mailing list