[vtkusers] Problem getting vtkActor2D and vtkActor to render simultaneously

Bill Lorensen bill.lorensen at gmail.com
Thu Jan 31 08:37:51 EST 2013


See if this example helps:
http://vtk.org/Wiki/VTK/Examples/Cxx/Images/BackgroundImage


On Tue, Jan 29, 2013 at 10:57 AM, Dominic <domaniak at gmail.com> wrote:
> On 13-01-18 10:45 AM, Dominic Plourde wrote:
>
> I have a similar problem. I'm using VTK 5.10.1 on Ubuntu 11.10 The
> vtkActor2D disappear when I moving other 3D actors outside the viewport. If
> I remove the vtkAxesActor, my vtkActor2D is not visible. My test code:
>
> #include <vtkVersion.h>
> #include <vtkImageData.h>
> #include <vtkSmartPointer.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkInteractorStyleImage.h>
> #include <vtkRenderer.h>
> #include <vtkImageMapper.h>
> #include <vtkActor2D.h>
>
>
> #include <vtkPolygon.h>
> #include <vtkPolyData.h>
> #include <vtkProperty.h>
> #include <vtkPolyDataMapper.h>
> #include <vtkCellData.h>
> #include <vtkCellArray.h>
>
> #include <vtkTexture.h>
> #include <vtkImageData.h>
> #include <vtkFloatArray.h>
> #include <vtkPointData.h>
> #include <vtkQImageToImageSource.h>
> #include <vtkAxesActor.h>
> #include <vtkVersion.h>
>
> #include <QImage>
> #include <QApplication>
>
> static void CreateColorImage(vtkImageData*);
>
> int main(int i, char** c)
> {
>   QApplication qapp(i, c);
>
>   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> imageActor =
> vtkSmartPointer<vtkActor2D>::New();
>   imageActor->SetMapper(imageMapper);
>   imageActor->SetPosition(0, 0);
>
>   // Create a vtkPoints object and store the points in it
>   vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
>
>   // Create a cell array to store the polygon in and add the polygon to it
>   vtkSmartPointer<vtkCellArray> polygons =
> vtkSmartPointer<vtkCellArray>::New();
>
>   // Read the image which will be the texture
>   QImage* qimage = new QImage("logo.png");
>
>   vtkSmartPointer<vtkQImageToImageSource> qImageToVtk =
> vtkSmartPointer<vtkQImageToImageSource>::New();
>   qImageToVtk->SetQImage(qimage);
>
>   vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
>   texture->SetInputConnection(qImageToVtk->GetOutputPort());
>
>   vtkSmartPointer<vtkFloatArray> textureCoordinates =
> vtkSmartPointer<vtkFloatArray>::New();
>   textureCoordinates->SetNumberOfComponents(3);
>   textureCoordinates->SetName("TextureCoordinates");
>
>   // Create a polydata to store everything in
>   vtkSmartPointer<vtkPolyData> polygonPolyData =
> vtkSmartPointer<vtkPolyData>::New();
>
>   float tuple[3] = {0.0, 0.0, 0.0};
>   textureCoordinates->InsertNextTuple(tuple);
>   tuple[0] = 1.0; tuple[1] = 0.0; tuple[2] = 0.0;
>   textureCoordinates->InsertNextTuple(tuple);
>   tuple[0] = 1.0; tuple[1] = 1.0; tuple[2] = 0.0;
>   textureCoordinates->InsertNextTuple(tuple);
>   tuple[0] = 0.0; tuple[1] = 1.0; tuple[2] = 0.0;
>   textureCoordinates->InsertNextTuple(tuple);
>
>   // Add the points to the dataset
>   polygonPolyData->SetPoints(points);
>   polygonPolyData->SetPolys(polygons);
>   polygonPolyData->GetPointData()->SetTCoords(textureCoordinates);
>
>   // Setup actor and mapper
>   vtkSmartPointer<vtkPolyDataMapper> mapper =
> vtkSmartPointer<vtkPolyDataMapper>::New();
>   mapper->SetInput(polygonPolyData);
>
>   vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
>   actor->SetMapper(mapper);
>   actor->SetTexture(texture);
>   actor->GetProperty()->SetColor(0.0, 0.62, 0.85);
>
>   points->SetNumberOfPoints(4);
>   points->SetPoint(0, 0, 0, 0);
>   points->SetPoint(1, 10, 0, 0);
>   points->SetPoint(2, 10, 10, 0);
>   points->SetPoint(3, 0, 10, 0);
>
>   vtkSmartPointer<vtkPolygon> top = vtkSmartPointer<vtkPolygon>::New();
>   top->GetPointIds()->SetNumberOfIds(4);
>   top->GetPointIds()->SetId(3, 0);
>   top->GetPointIds()->SetId(2, 1);
>   top->GetPointIds()->SetId(1, 2);
>   top->GetPointIds()->SetId(0, 3);
>
>   polygons->InsertNextCell(top);
>
>   vtkSmartPointer<vtkAxesActor> axes = vtkSmartPointer<vtkAxesActor>::New();
>
>   vtkSmartPointer<vtkRenderer> renderer =
> vtkSmartPointer<vtkRenderer>::New();
>   vtkSmartPointer<vtkRenderWindow> renderWindow =
> vtkSmartPointer<vtkRenderWindow>::New();
>   renderWindow->AddRenderer(renderer);
>
>   // Setup render window interactor
>   vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
> vtkSmartPointer<vtkRenderWindowInteractor>::New();
>   vtkSmartPointer<vtkInteractorStyleImage> style =
> vtkSmartPointer<vtkInteractorStyleImage>::New();
>   renderWindowInteractor->SetInteractorStyle(style);
>
>   // Render and start interaction
>   renderWindowInteractor->SetRenderWindow(renderWindow);
>
>   renderer->AddActor(actor);
>   renderer->AddActor(axes);
>   renderer->AddActor2D(imageActor);
>
>   renderWindow->Render();
>   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();
> }
>
> Elvis Dowson wrote
> Hi, In the following code, I cannot get both vtkActor2D and vtkActor to
> display simultaneously. If I enable 2D map source and text source, it
> displays the map and the text. If I enable the 3D cylinder source, the 3D
> cylinder doesnt get displayed. However, if I disable the 2D map source, the
> cylinder and the text message appears. How can I get the 2D map to display
> below, and the 3D cylinder to appear to hover above the 2D map ? Am I mixing
> things here? Should I be using vtkImageActor instead of vtkActor2D, so that
> there is a 3D image plane and then display the 3D source? I really dont want
> to do that at the moment. Please help! :-) Best regards, Elvis Dowson
>
> #include "qapplication.h"
> #include <QtGui>
>
> #include <vtkActor.h>
> #include <vtkActor2D.h>
> #include <vtkImageActor.h>
> #include <vtkTextActor.h>
> #include <vtkJPEGReader.h>
> #include <vtkRenderer.h>
> #include <vtkRenderWindow.h>
> #include <vtkRenderWindowInteractor.h>
> #include <vtkImageMapper.h>
> #include <vtkPolyDataMapper.h>
> #include <vtkCylinderSource.h>
>
>
> #if QT_VERSION >= 0x040000
> # include "ui_GUI4.h"
> #endif
>
> #define PROTOTYPE_2D_MAP_SOURCE
> #define PROTOTYPE_2D_TEXT_SOURCE
> #define PROTOTYPE_3D_CYLINDER_SOURCE
>
>
> int main(int argc, char** argv)
> {
>    QApplication app(argc, argv);
> 	
> #if QT_VERSION >= 0x040000
> 	Ui::Dialog ui;
> 	QDialog dialog;
> #endif
> 	
> 	ui.setupUi(&dialog);
> 	dialog.setAttribute(Qt::WA_QuitOnClose);
> 	
> 	// Qt vtk integration
> 	vtkRenderer* ren = NULL;
> 	ren = vtkRenderer::New();
> 		ui.qvtkWidget->GetRenderWindow()->AddRenderer(ren);
> 	
> 	// Prototype: 2D Map Source
> 	// ----------------------------
> 	#ifdef PROTOTYPE_2D_MAP_SOURCE
>   	{
> 	// Declare object instance variables
> 	vtkJPEGReader* imageReader	= NULL;
> 	vtkImageMapper* imageMapper = NULL;
> 	vtkActor2D* mapActor		= NULL;			
> 			
> 	// Read 2D image file
> 	imageReader = vtkJPEGReader::New();
> 		imageReader->SetFileName("/Users/elvis/Image/sample.jpg");
> 		imageReader->Update();
> 	
> 	// Map 2D image file
> 	imageMapper = vtkImageMapper::New();
> 		imageMapper->SetInput(imageReader->GetOutput());
> 		imageMapper->SetColorWindow(255);
> 		imageMapper->SetColorLevel(127.5);
> 		
> 	// Actor in scene
> 	mapActor = vtkActor2D::New();
> 		mapActor->SetMapper(imageMapper);	
>
> 	// Actor in scene
> 	mapActor = vtkActor2D::New();
> 		//mapActor->SetLayerNumber(0);
> 		mapActor->SetMapper(imageMapper);			
>
> 	// Add Actor to renderer
> 	ren->AddActor2D(mapActor);
> 	}
> 	#endif
>
> 	// Prototype: 2D Text Source
> 	// -----------------------------
> 	#ifdef PROTOTYPE_2D_TEXT_SOURCE
> 	{
> 		// Declare object instance variables
> 		vtkTextActor* textActor;
> 		
> 		// Initialize objects
> 		textActor = NULL;
> 		
> 		// Actor in scene
> 		textActor = vtkTextActor::New();
> 		textActor->SetTextScaleMode(vtkTextActor::TEXT_SCALE_MODE_NONE);
> 		textActor->SetInput("This is a text message!");
> 		
> 		// Add Actor to renderer
> 		ren->AddActor2D(textActor);
> 	}
> 	#endif
> 	
>
> 	// Prototype: 3D Cyclinder Source
> 	// ----------------------------------
> 	#ifdef PROTOTYPE_3D_CYLINDER_SOURCE	
> 	{
> 	// Declare object instance variables
> 	vtkCylinderSource* source;
> 	vtkPolyDataMapper* mapper;
> 	vtkActor* actor;
>
> 	// Initialize objects
> 	source = NULL;
> 	mapper = NULL;
> 	actor = NULL;
> 	
> 	// Geometry
> 	source = vtkCylinderSource::New();
> 	
> 	// Mapper
> 	mapper = vtkPolyDataMapper::New();
> 	mapper->ImmediateModeRenderingOn();
> 	mapper->SetInputConnection(source->GetOutputPort());
> 	
> 	// Actor in scene
> 	actor = vtkActor::New();
> 	actor->SetMapper(mapper);
> 	
> 	// Add Actor to renderer
> 	ren->AddActor(actor);
> 	}
> 	#endif
> 	
> 	// Reset camera
> 	ren->ResetCamera();
> 	
> 	// Render the scene
> 	ren->GetRenderWindow()->Render();
> 	
> 	// Display the dialog window
> 	dialog.show();
>
>    return app.exec();
> }
>
> _______________________________________________ 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
>
>
> ________________________________
> View this message in context: Re: Problem getting vtkActor2D and vtkActor to
> render simultaneously
> Sent from the VTK - Users mailing list archive at Nabble.com.
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>
> Has anyone encountered this?
>
> Thanks,
> Dominic
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>



-- 
Unpaid intern in BillsBasement at noware dot com



More information about the vtkusers mailing list