# Rotating an object around it's origin after translation

**URL:** <https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759>\
**Category:** Development\
**Created:** [November 21, 2023, 10:28am UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759 "2023-11-21T10:28:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimia\_ghodoosi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kimia_ghodoosi/32/7758_2.png) [@kimia\_ghodoosi](https://discourse.vtk.org/u/kimia_ghodoosi)\
**Post date:** [November 21, 2023, 10:28am UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/1 "2023-11-21T10:28:04Z")

</div>

Hi!  
I want to translate my object and then rotate it around the translated origin not the main origin of the page’s coordinate.I don’t know how i can solve the problem. can any one help me?(my code is shown below)

- the code in the translation function is this:

```auto
    vtkSmartPointer<vtkTransformFilter> transformFilter = vtkSmartPointer<vtkTransformFilter>::New();
    transformFilter->SetInputData(updatedPolydata);

    vtkSmartPointer<vtkTransform> translation = vtkSmartPointer<vtkTransform>::New();
    translation->Translate(tx, ty, tz);
    translation->Update();

    transformFilter->SetTransform(translation);
    transformFilter->Update();

    vtkSmartPointer<vtkPolyData> translatedSTL = transformFilter->GetPolyDataOutput();

    mapper->SetInputData(translatedSTL);
    mapper->Update();
    actor->SetMapper(mapper);

```

- the code in the rotation function is this:

```auto
    vtkSmartPointer<vtkTransform> rotation = vtkSmartPointer<vtkTransform>::New();
    rotation->RotateX(x_degree);
    rotation->RotateY(y_degree);
    rotation->RotateZ(z_degree);
    rotation->Update();

    vtkSmartPointer<vtkTransformFilter> rotationFilter = vtkSmartPointer<vtkTransformFilter>::New();
    rotationFilter->SetInputData(updatedPolydata);
    rotationFilter->SetTransform(rotation);
    rotationFilter->Update();

    vtkSmartPointer<vtkPolyData> rotatedSTL = rotationFilter->GetPolyDataOutput();

    mapper->SetInputData(rotatedSTL);
    mapper->Update();
    actor->SetMapper(mapper);

```

---

<div class="post-metadata">

**Author:** ![kimia\_ghodoosi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kimia_ghodoosi/32/7758_2.png) [@kimia\_ghodoosi](https://discourse.vtk.org/u/kimia_ghodoosi)\
**Post date:** [November 21, 2023, 12:42pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/2 "2023-11-21T12:42:00Z")

</div>

sorry, it’s necessary for me to find the answer.can you help me?  
@mwestphal

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [November 21, 2023, 1:17pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/3 "2023-11-21T13:17:25Z")

</div>

Hi @kimia_ghodoosi

If you need a fast and necessary answer, please contact Kitware directly for professional support:

> **[Get Support - Kitware Europe](https://www.kitware.eu/get-support/)**
>
> \[et\_pb\_section fb\_built=”1″ fullwidth=”on” custom\_padding\_last\_edited=”off|tablet” \_builder\_version=”3.22″ background\_color=”#ffffff” custom\_margin=”-21px|||||” custom\_padding\_tablet=”50px|0|50px|0″ global\_colors\_info=”{}”\]\[et\_pb\_fullwidth\_header...

Best,

---

<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:** [November 21, 2023, 6:17pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/4 "2023-11-21T18:17:56Z")

</div>

Hello,

You are applying two independent transforms (two `vtkTransform` objects). You need to composite them as a single `vtkTransform` to rotate an object about an arbitrary center. To rotate something about a center not in the global origin (e.g. (42,42,42)), you need, first, to translate A=(-42,-42,-42) towards the origin; then rotate about the origin (R), then undo the translation back to the correct location B=(+42,+42,+42). Pay attention to multiply the transform matrices in the correct order: the matrix product must be B\*R\*A and not A\*R\*B. The first transform to be applied is the last one in the multiplication order.

regards,

PC

---

<div class="post-metadata">

**Author:** ![kimia\_ghodoosi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kimia_ghodoosi/32/7758_2.png) [@kimia\_ghodoosi](https://discourse.vtk.org/u/kimia_ghodoosi)\
**Post date:** [November 22, 2023, 7:04am UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/5 "2023-11-22T07:04:17Z")

</div>

Thank you so much Paulo. my problem solved with your solution.

---

<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:** [November 22, 2023, 7:59pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/6 "2023-11-22T19:59:17Z")

</div>

Please, I recommend a nice book called “Graphics Gems”. It’s 9th chapter delves into the linear algebra necessary for computer graphics. It’s a 1990 book, but if you want to even write you own graphics engine, it’s still worth it.

take care,

PC

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)\
**Post date:** [November 25, 2023, 6:04pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/7 "2023-11-25T18:04:31Z")

</div>

Could you recommend a book only one or two years old to learn the newest trends in computer graphics (or GLSL)?

---

<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:** [November 25, 2023, 9:53pm UTC](https://discourse.vtk.org/t/rotating-an-object-around-its-origin-after-translation/12759/8 "2023-11-25T21:53:03Z")

</div>

Hmmm… “Graphics Gems” is not a book on an outdated topic. It’s on how to design a graphics system. There’s nothing trendy about that: you either know or do not know how to design a graphics engine. GLSL is a technology, for that you find plenty of materials readily available on the internet such as this OpenGL 1k-page book (includes a shader/GLSL primer) in PDF format: [https://www.cs.utexas.edu/users/fussell/courses/cs354/handouts/Addison.Wesley.OpenGL.Programming.Guide.8th.Edition.Mar.2013.ISBN.0321773039.pdf](https://www.cs.utexas.edu/users/fussell/courses/cs354/handouts/Addison.Wesley.OpenGL.Programming.Guide.8th.Edition.Mar.2013.ISBN.0321773039.pdf) . You may also want to read this VTK primer from KitWare itself: [https://gitlab.kitware.com/vtk/textbook/raw/master/VTKBook/VTKTextBook.pdf](https://gitlab.kitware.com/vtk/textbook/raw/master/VTKBook/VTKTextBook.pdf) .

best,

PC
