# How to obtain the polydata added in trackball actor interactor.

**URL:** https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306
**Category:** Support
**Created:** [December 23, 2022, 9:57am UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306 "2022-12-23T09:57:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [December 23, 2022, 9:57am UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306/1 "2022-12-23T09:57:55Z")

</div>

I have a `vtkPolyData`, and generate a `vtkActor`. I add this `vtkActor` to `vtkInteractorStyleTrackballActor`. After some `rotation/pan/spin` action, the `position/scale/orientation` of `vtkActor` has been changed, but the `vtkPolyData` is not changed.

I want to modify the `vtkPolyData` by `position/scale/orientation`, and get a new `vtkPolyData`.

My code is:

```nohighlight
auto matrix = actor->GetMatrix();
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
transform->SetMatrix(matrix);
transform->Update();

vtkSmartPointer<vtkTransformPolyDataFilter> filter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();

filter->SetInputData(oldPolyData);
filter->SetTransform(transform);
filter->Update();

auto newPolyData = filter->GetOutput();

actor->SetOrientation(0, 0, 0);
actor->SetOrigin(0, 0, 0);
actor->SetScale(1, 1, 1);
actor->SetPosition(0, 0, 0);
actor->SetUserTransform(nullptr);
actor->SetUserMatrix(nullptr);
actor->GetMapper()->SetInputData(newPolyData);

```

But, the result is wrong~~~

 ![1671789297(1)](https://discourse.vtk.org/uploads/default/original/2X/e/e7537863a25151fd4f68a1c6b6491f854980a333.png)

The gray actor is the target, and yellow line is the bounds of the actor.

If only `spin` happen, the actor would get out of the bounds.

![image](https://discourse.vtk.org/uploads/default/original/2X/8/8acd7e353c8fcf0f0ceaaef97a8e10db01541263.png)

Is there anything wrong with my code? Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [December 25, 2022, 5:17pm UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306/2 "2022-12-25T17:17:27Z")

</div>

Hello,

How are you populating `oldPolyData`? I mean, what are the typical values of XYZ used to make it?

regards,

Paulo

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [December 26, 2022, 8:56am UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306/3 "2022-12-26T08:56:21Z")

</div>

@Paulo_Carvalho Thanks for kindly reply. I have written a complete code to reproduce my problem:

```auto

import vtkmodules.all as vtk

# cylinder = vtk.vtkCylinderSource()
# cylinder.Update()
# polydata = cylinder.GetOutput()

reader = vtk.vtkSTLReader()
reader.SetFileName('polydata.stl')
reader.Update()
polydata = reader.GetOutput()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputData(polydata)

actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.SetPosition(10, 10, 20)
actor.RotateX(30)
actor.RotateY(30)
actor.RotateZ(30)

def lineActor(p1, p2):

    lineSource = vtk.vtkLineSource()
    lineSource.SetPoint1(p1[0], p1[1], p1[2])
    lineSource.SetPoint2(p2[0], p2[1], p2[2])
    lineSource.Update()

    lineMapper = vtk.vtkPolyDataMapper()
    lineMapper.SetInputData(lineSource.GetOutput())
    lineActor = vtk.vtkActor()
    lineActor.SetMapper(lineMapper)
    lineActor.GetProperty().SetColor(1, 1, 0)
    lineActor.GetProperty().SetLineWidth(5)
    return lineActor

bounds = actor.GetBounds()
xmin, xmax, ymin, ymax, zmin, zmax = bounds

ps = [
    [xmin, ymin, zmin, xmax, ymin, zmin],
    [xmax, ymin, zmin, xmax, ymax, zmin],
    [xmin, ymax, zmin, xmax, ymax, zmin],
    [xmin, ymin, zmin, xmin, ymax, zmin],

    [xmin, ymin, zmax, xmax, ymin, zmax],
    [xmax, ymin, zmax, xmax, ymax, zmax],
    [xmin, ymax, zmax, xmax, ymax, zmax],
    [xmin, ymin, zmax, xmin, ymax, zmax],

    [xmin, ymin, zmin, xmin, ymin, zmax],
    [xmax, ymin, zmin, xmax, ymin, zmax],
    [xmax, ymax, zmin, xmax, ymax, zmax],
    [xmin, ymax, zmin, xmin, ymax, zmax]
]

renderer = vtk.vtkRenderer()
renderer.AddActor(actor)

for p in ps:
    lineA = lineActor(p1=[p[0], p[1], p[2]], p2=[p[3], p[4], p[5]])
    renderer.AddActor(lineA)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(renderer)
iren = vtk.vtkRenderWindowInteractor()

def mouseMoveEvent(iren, event):
    print('mouse move')
    matrix = actor.GetMatrix()
    transform = vtk.vtkTransform()
    transform.SetMatrix(matrix)
    transform.Update()
    print(actor.GetCenter())
    oldPolyData = actor.GetMapper().GetInput()

    filter = vtk.vtkTransformPolyDataFilter()
    filter.SetInputData(oldPolyData)
    filter.SetTransform(transform)
    filter.Update()

    newPolyData = filter.GetOutput()
    actor.SetOrigin(0, 0, 0)
    actor.SetOrientation(0, 0, 0)
    actor.SetScale(1, 1, 1)
    actor.SetPosition(0, 0, 0)
    actor.SetUserTransform(None)
    actor.GetMapper().SetInputData(newPolyData)

iren.AddObserver('MouseMoveEvent', mouseMoveEvent)

style = vtk.vtkInteractorStyleTrackballActor()
iren.SetInteractorStyle(style)
iren.SetRenderWindow(renWin)
iren.Initialize()
iren.Start()

```

My test data is:

[polydata.stl](https://discourse.vtk.org/uploads/short-url/lPlN8z1MFLdtgERYqbX6WfBRcqW.stl) (41.1 KB)

Steps to reproduct my problem:

1. when the code is run, and the result is:

![image](https://discourse.vtk.org/uploads/default/original/2X/2/2f0d365a132cb17bbc60e3a7da71cd191eca4a79.png)

And the printed center is: `(41.48747253417969, 123.91775894165039, 9.721415996551514)`

The white actor is my target, and the yellow line is the bounds of target. We can see that the target is included by the yellow line.

1. Spin the actor: `ctrl+left press in the white actor`, please notes that the `ctrl` board is pressed so that `spin` would happen for the target.The left mouse button is pressed and move, the target would spin:

![image](https://discourse.vtk.org/uploads/default/original/2X/7/7268f69f2c1f85128e46a2b2e9236643f975c21a.png)

we can see that the target is still included by the yellow lines. The printing is stopped.

1. Releases the left mouse button, and move the mouse, we can see the printed center is: `(41.556962966918945, 124.36083602905273, 9.856724977493286)`. And it is changed.

2. Repeating step 2/3, we can obtain the following figure, and the target is not included by the yellow lines.

![image](https://discourse.vtk.org/uploads/default/original/2X/a/ae3d508ee1094f651195f334dfdd2b64c6e9b9e5.png)

When repeating step 2/3, we must iterative `ctrl+press left mouse and move` and `release left mouse and move`. We can see the printed center is changing.

Strangely, when I replace the target with a `vtk.vtkCylinderSource()`, everything is ok.

Any suggestion is appreciated~~~

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 6, 2023, 2:02pm UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306/7 "2023-01-06T14:02:18Z")

</div>

Hello,

It appears to me that the model is off-center. I mean, the geometric center of the object does not correspond to the origin of the actor. Maybe you can achive the desired effect if you set the actor’s origin to match the center of the model’s geometry (mean of all vertexes’ XYZ).

take care,

Paulo

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [January 6, 2023, 2:11pm UTC](https://discourse.vtk.org/t/how-to-obtain-the-polydata-added-in-trackball-actor-interactor/10306/8 "2023-01-06T14:11:18Z")

</div>

Thanks for your kindly reply.

> [@Paulo\_Carvalho](#):
>
> Maybe you can achive the desired effect if you set the actor’s origin to match the center of the model’s geometry (mean of all vertexes’ XYZ)

It works.

After reading the c++ source code, I figure out the bug reason.

The c++ code is:

```auto
void vtkInteractorStyleTrackballActor::Spin()
{
  if (this->CurrentRenderer == nullptr || this->InteractionProp == nullptr)
  {
    return;
  }

  vtkRenderWindowInteractor* rwi = this->Interactor;
  vtkCamera* cam = this->CurrentRenderer->GetActiveCamera();

  // Get the axis to rotate around = vector from eye to origin

  double* obj_center = this->InteractionProp->GetCenter();

  double motion_vector[3];
  double view_point[3];
  ...

```

The actor center is obtained: `double* obj_center = this->InteractionProp->GetCenter();`. But, the `vtkTransformPolyDataFilter->GetOutput()->GetCenter()` is not equal to the `actor->GetCenter()`.

After the changing:

```auto
    newPolyData = filter.GetOutput()
    actor.SetOrigin(0, 0, 0)
    actor.SetOrientation(0, 0, 0)
    actor.SetScale(1, 1, 1)
    actor.SetPosition(0, 0, 0)
    actor.SetUserTransform(None)
    actor.SetOrigin(newPolyData.GetCenter()) ######### set the actor's origin
    actor.GetMapper().SetInputData(newPolyData)

```

It works as I expect.
