[vtkusers] Problem getting vtkActor2D and vtkActor to render simultaneously
Dominic
domaniak at gmail.com
Fri Feb 1 11:44:23 EST 2013
On 13-02-01 11:36 AM, Bill Lorensen wrote:
> Can you change the wiki example to illustrate your problem? You don't
> need qt to do that.
>
Yes, in this exemple, if you move the superquadricActor away of the
viewport with your mouse, the vtkActor2D color change.
#include <vtkVersion.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkImageCanvasSource2D.h>
#include <vtkImageActor.h>
#include <vtkImageData.h>
#include <vtkJPEGReader.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSuperquadricSource.h>
#include <vtkImageMapper.h>
#include <vtkActor2D.h>
#include <vtkTexture.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
static void CreateColorImage(vtkImageData*);
int main(int argc, char *argv[])
{
vtkSmartPointer<vtkImageData> imageData;
// Verify input arguments
if ( argc > 1 )
{
//Read the image
vtkSmartPointer<vtkJPEGReader> jpegReader =
vtkSmartPointer<vtkJPEGReader>::New();
if( !jpegReader->CanReadFile( argv[1] ) )
{
std::cerr << "Error reading file " << argv[1] << std::endl;
return EXIT_FAILURE;
}
jpegReader->SetFileName ( argv[1] );
jpegReader->Update();
imageData = jpegReader->GetOutput();
}
else
{
vtkSmartPointer<vtkImageCanvasSource2D> canvasSource =
vtkSmartPointer<vtkImageCanvasSource2D>::New();
canvasSource->SetExtent(0, 100, 0, 100, 0, 0);
canvasSource->SetScalarTypeToUnsignedChar();
canvasSource->SetNumberOfScalarComponents(3);
canvasSource->SetDrawColor(127, 127, 100);
canvasSource->FillBox(0, 100, 0, 100);
canvasSource->SetDrawColor(100, 255, 255);
canvasSource->FillTriangle(10, 10, 25, 10, 25, 25);
canvasSource->SetDrawColor(255, 100, 255);
canvasSource->FillTube(75, 75, 0, 75, 5.0);
canvasSource->Update();
imageData = canvasSource->GetOutput();
}
// Create an image actor to display the image
vtkSmartPointer<vtkImageActor> imageActor =
vtkSmartPointer<vtkImageActor>::New();
#if VTK_MAJOR_VERSION <= 5
imageActor->SetInput(imageData);
#else
imageActor->SetInputData(imageData);
#endif
// Create a renderer to display the image in the background
vtkSmartPointer<vtkRenderer> backgroundRenderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkImageData> colorImage =
vtkSmartPointer<vtkImageData>::New();
CreateColorImage(colorImage);
vtkSmartPointer<vtkImageMapper> imageMapper =
vtkSmartPointer<vtkImageMapper>::New();
imageMapper->SetInputConnection(colorImage->GetProducerPort());
imageMapper->SetColorWindow(255);
imageMapper->SetColorLevel(127.5);
vtkSmartPointer<vtkActor2D> imageActor2D =
vtkSmartPointer<vtkActor2D>::New();
imageActor2D->SetMapper(imageMapper);
imageActor2D->SetPosition(0, 0);
vtkSmartPointer<vtkTexture> texture =
vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(colorImage->GetProducerPort());
// Create a superquadric
vtkSmartPointer<vtkSuperquadricSource> superquadricSource =
vtkSmartPointer<vtkSuperquadricSource>::New();
superquadricSource->SetPhiRoundness(1.1);
superquadricSource->SetThetaRoundness(.2);
// Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> superquadricMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
superquadricMapper->SetInputConnection(superquadricSource->GetOutputPort());
vtkSmartPointer<vtkActor> superquadricActor =
vtkSmartPointer<vtkActor>::New();
superquadricActor->SetMapper(superquadricMapper);
superquadricActor->SetTexture(texture);
vtkSmartPointer<vtkRenderer> sceneRenderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
// Set up the render window and renderers such that there is
// a background layer and a foreground layer
backgroundRenderer->SetLayer(0);
backgroundRenderer->InteractiveOff();
sceneRenderer->SetLayer(1);
renderWindow->SetNumberOfLayers(2);
renderWindow->AddRenderer(backgroundRenderer);
renderWindow->AddRenderer(sceneRenderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add actors to the renderers
sceneRenderer->AddActor(superquadricActor);
sceneRenderer->AddActor2D(imageActor2D);
backgroundRenderer->AddActor(imageActor);
// Render once to figure out where the background camera will be
renderWindow->Render();
// Set up the background camera to fill the renderer with the image
double origin[3];
double spacing[3];
int extent[6];
imageData->GetOrigin( origin );
imageData->GetSpacing( spacing );
imageData->GetExtent( extent );
vtkCamera* camera = backgroundRenderer->GetActiveCamera();
camera->ParallelProjectionOn();
double xc = origin[0] + 0.5*(extent[0] + extent[1])*spacing[0];
double yc = origin[1] + 0.5*(extent[2] + extent[3])*spacing[1];
//double xd = (extent[1] - extent[0] + 1)*spacing[0];
double yd = (extent[3] - extent[2] + 1)*spacing[1];
double d = camera->GetDistance();
camera->SetParallelScale(0.5*yd);
camera->SetFocalPoint(xc,yc,0.0);
camera->SetPosition(xc,yc,d);
// Render again to set the correct view
renderWindow->Render();
// Interact with the window
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
void CreateColorImage(vtkImageData* image)
{
unsigned int dim = 50;
image->SetDimensions(dim, dim, 1);
#if VTK_MAJOR_VERSION <= 5
image->SetNumberOfScalarComponents(3);
image->SetScalarTypeToUnsignedChar();
image->AllocateScalars();
#else
image->AllocateScalars(VTK_UNSIGNED_CHAR,3);
#endif
for(unsigned int x = 0; x < dim; x++)
{
for(unsigned int y = 0; y < dim; y++)
{
unsigned char* pixel = static_cast<unsigned
char*>(image->GetScalarPointer(x,y,0));
if(x < dim/2)
{
pixel[0] = 255;
pixel[1] = 0;
}
else
{
pixel[0] = 0;
pixel[1] = 255;
}
pixel[2] = 0;
}
}
image->Modified();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130201/611831d8/attachment.htm>
More information about the vtkusers
mailing list