[vtkusers] Help with using vtkTransform collections as vtkAssembly

Aroosha Laghaee a.laghaee at sms.ed.ac.uk
Sun Feb 12 19:04:26 EST 2006


Hello,

I am having quite a bit of trouble getting the hang of using the
vtkTransform class and am in desperate need of help. I am trying to
transform a set of vtkTransformCollections to behave in the same manner
as vtkAssembly. vtkAssembly is not adequate for my purposes as it only
transforms things in the visualisation pipeline and will not actually
transform the data set. So I am using vtkTransformPolyDataFilter for
this purpose. Some of my Transforms are being shared by different
TransformCollections  and I suspect this is the cause of the strange
behaviour I am getting but I do not know how to go about fixing it.

I have two TransformCollections the first entailing transforms 1, 2, 3,
and 4 and the second entailing transforms 3, and 4. I want the first
collection to rotate about the center of the source (vtkSphereSource)
associated with transform 1 and the second collection to rotate about
the center of the source (another vtkSphereSource) associated with
transform 3. To get the same effect as a vtkAssembly I shift all the
transforms in each collection to the desired center do my rotation and
then shift them back again.

The first time I apply these instructions to each collection I get the
desired behaviour with each collection rotating the correct amount and
around the specified center. But on subsequent moves only the second
collection (with transforms 3 and 4) rotates around the correct center.
In the case of the first collection (with all four transforms)
transforms 1 and 2 rotate around the right center but the 3rd and 4th
transforms (the ones shared between the two collections) start shifting
and seperating from their group as they rotate with the other two
transforms.

Can anyone please tell me why this is happening and how I should go
about stopping this from happening? I enclose my code below.
Many thanks,
              Aroosha

#include "vtkCubeSource.h"
#include "vtkSphereSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkTransform.h"
#include "vtkTransformPolyDataFilter.h"
#include "vtkPolyData.h"
#include "vtkAssembly.h"
#include "vtkTransformCollection.h"
#include "vtkCamera.h"
#include <windows.h>
#include <conio.h>

struct LARA{
	vtkTransformPolyDataFilter **filters;
	vtkTransformCollection **collection;
	vtkTransform **transform;
	vtkAssembly *assembly;
};

LARA dostuff()
{

	//set the sources
	vtkCubeSource *cube1 = vtkCubeSource::New();
	cube1->SetXLength(1);
	cube1->SetYLength(1);
	cube1->SetZLength(1);
	cube1->SetCenter(5,5.8,0);

	vtkSphereSource *sphere1 = vtkSphereSource::New();
	sphere1->SetRadius(0.3);
	sphere1->SetCenter(5,6.6,0);

	vtkCubeSource *cube2 = vtkCubeSource::New();
	cube2->SetXLength(1);
	cube2->SetYLength(1);
	cube2->SetZLength(1);
	cube2->SetCenter(5,4.2,0);

	vtkSphereSource *sphere2 = vtkSphereSource::New();
	sphere2->SetRadius(0.3);
	sphere2->SetCenter(5,5,0);

	//set the transforms
	vtkTransform* ct1 = vtkTransform::New();
	vtkTransform* st1 = vtkTransform::New();
	vtkTransform* ct2 = vtkTransform::New();
	vtkTransform* st2 = vtkTransform::New();

         //set the filters
	vtkTransformPolyDataFilter* cf1 = vtkTransformPolyDataFilter::New();
          cf1->SetTransform(ct1);
	cf1->SetInput(cube1->GetOutput());

	vtkTransformPolyDataFilter* sf1 = vtkTransformPolyDataFilter::New();
          sf1->SetTransform(st1);
	sf1->SetInput(sphere1->GetOutput());

	vtkTransformPolyDataFilter* cf2 = vtkTransformPolyDataFilter::New();
          cf2->SetTransform(ct2);
	cf2->SetInput(cube2->GetOutput());

	vtkTransformPolyDataFilter* sf2 = vtkTransformPolyDataFilter::New();
          sf2->SetTransform(st2);
	sf2->SetInput(sphere2->GetOutput());

	//set the mappers
	vtkPolyDataMapper *cubeMapper1 = vtkPolyDataMapper::New();
	cubeMapper1->SetInput(cf1->GetOutput());

	vtkPolyDataMapper *sphereMapper1 = vtkPolyDataMapper::New();
          sphereMapper1->SetInput(sf1->GetOutput());

	vtkPolyDataMapper *cubeMapper2 = vtkPolyDataMapper::New();
	cubeMapper2->SetInput(cf2->GetOutput());

	vtkPolyDataMapper *sphereMapper2 = vtkPolyDataMapper::New();
          sphereMapper2->SetInput(sf2->GetOutput());

	//set the actors
	vtkActor *cubeActor1 = vtkActor::New();
	cubeActor1->SetMapper(cubeMapper1);
	cubeActor1->SetOrigin(sphere1->GetCenter());

	vtkActor *sphereActor1 = vtkActor::New();
          sphereActor1->SetMapper(sphereMapper1);
	sphereActor1->SetOrigin(sphere1->GetCenter());

	vtkActor *cubeActor2 = vtkActor::New();
	cubeActor2->SetMapper(cubeMapper2);
	cubeActor2->SetOrigin(sphere2->GetCenter());

	vtkActor *sphereActor2 = vtkActor::New();
          sphereActor2->SetMapper(sphereMapper2);
	sphereActor2->SetOrigin(sphere2->GetCenter());

	//set the assembly
	vtkAssembly *arm1 = vtkAssembly::New();
	arm1->AddPart(cubeActor1);
	arm1->AddPart(sphereActor1);
	arm1->AddPart(cubeActor2);
	arm1->AddPart(sphereActor2);
	arm1->SetOrigin(sphere1->GetCenter());

	vtkTransformCollection *c2 = vtkTransformCollection::New();
	c2->AddItem(ct2);
	c2->AddItem(st2);

	vtkTransformCollection *c1 = vtkTransformCollection::New();
	c1->AddItem(ct1);
	c1->AddItem(st1);
	c1->AddItem(ct2);
	c1->AddItem(st2);

	vtkTransformCollection **c = new vtkTransformCollection *[2];
	c[0] = c1;
	c[1] = c2;

	vtkTransformPolyDataFilter **filters = new vtkTransformPolyDataFilter *[3];
	filters[0] = sf1;
	filters[1] = cf1;
	filters[2] = sf2;
	filters[3] = cf2;

	vtkTransform **t = new vtkTransform *[2];
	t[0] = st1;
	t[1] = st2;

	LARA arm;
	arm.assembly = arm1;
	arm.collection = c;
	arm.filters = filters;
	arm.transform = t;

	return arm;
}

int main()
{
	LARA lara = dostuff();

	vtkRenderer *ren= vtkRenderer::New();
	vtkAssembly *assembly = lara.assembly;
	ren->AddActor(assembly);
	ren->SetBackground(0.1,0.2,0.4);

	vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren);
	renWin->SetSize(300,300);

	renWin->Render();

	vtkTransformCollection **c = lara.collection;
	vtkTransformPolyDataFilter **filter = lara.filters;
	vtkTransform **transform = lara.transform;

	for(int k=0;k<3;k++){
	for(int i=0;i<60;i++){
		c[0]->InitTraversal();
		renWin->Render();
		for(int j=0;j<c[0]->GetNumberOfItems();j++){
			vtkTransform *t = c[0]->GetNextItem();
			t->Translate(5,6.6,0);
			t->RotateZ(1);
			t->Translate(-5,-6.6,0);
			Sleep(30);
		}
	}
          for(int n=0;n<60;n++){
		c[1]->InitTraversal();
		renWin->Render();
		for(int m=0;m<c[1]->GetNumberOfItems();m++){
			vtkTransform *t = c[1]->GetNextItem();
			t->Translate(5,5,0);
			t->RotateZ(1);
			t->Translate(-5,-5,0);
			Sleep(30);
		}
	}
	}
	getch();
	return 0;
}





More information about the vtkusers mailing list