# Orienting cylinder to point same way as camera

**URL:** https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586
**Category:** Support
**Created:** [May 8, 2025, 5:25am UTC](https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586 "2025-05-08T05:25:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jimiheinonen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jimiheinonen](https://discourse.vtk.org/u/jimiheinonen)
#### Post date: [May 8, 2025, 5:25am UTC](https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586/1 "2025-05-08T05:25:43Z")

</div>

Hello! Is there a way to orient a cylinder (used for boolean operations) to point so that the ends are in the same direction as which the active camera is viewing? I don’t really understand how the orientation in the camera works, so I’m having difficulties setting the orientation of the cylinder using SetOrientation()

thanks in advance for any help!

---

<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: [May 8, 2025, 8:54pm UTC](https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586/2 "2025-05-08T20:54:31Z")

</div>

Hello,

What have you done so far? Can you, please, share the code?

best,

PC

---

<div class="post-metadata">

### Author: ![jimiheinonen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jimiheinonen](https://discourse.vtk.org/u/jimiheinonen)
#### Post date: [May 9, 2025, 6:27am UTC](https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586/3 "2025-05-09T06:27:09Z")

</div>

Hello, here’s a snippet of the code I’m attempting to use:

```auto
            this->cylinderActor->SetPosition(pos);

            // Have two vectors, need to calculate cross product and dot product between them
            double* point1 = this->GetCurrentRenderer()->GetActiveCamera()->GetPosition();
            double* point2 = this->cylinderActor->GetPosition();

            double length1 = sqrt(pow(point1[0], 2) + pow(point1[1], 2) + pow(point1[2], 2));
            double length2 = sqrt(pow(point2[0], 2) + pow(point2[1], 2) + pow(point2[2], 2));
            double cosangle = cos(vtkMath::Dot(point1, point2) / length1 * length2);
            double angle = acos(cosangle); // Angle
            double axis[3];
            vtkMath::Cross(point1, point2, axis);

            // Now we have axis and angle, convert it to Euler rotations
            double heading = heading = atan2(axis[1] * sin(angle) - axis[0] * axis[2] * (1 - cos(angle)), 1 - (pow(axis[1], 2) + pow(axis[2], 2)) * (1 - cos(angle)));
            double attitude = asin(axis[0] * axis[1] * (1 - cos(angle)) + axis[2] * sin(angle));
            double bank = atan2(axis[0] * sin(angle) - axis[1] * axis[2] * (1 - cos(angle)), 1 - (pow(axis[0], 2) + pow(axis[2], 2)) * (1 - cos(angle)));
            double orient[3] = {heading, attitude, bank};

            this->cylinderActor->SetOrientation(orient);

```

I am actually attempting to get the cylinder to face the camera (so it kind of faces away from the central point of the camera position). The above produces wrong results for orientation (and attitude ends up as not a number). I’m not sure where the maths are wrong, I’ve been referring to this answer: [https://stackoverflow.com/questions/15101103/euler-angles-between-two-3d-vectors](https://stackoverflow.com/questions/15101103/euler-angles-between-two-3d-vectors)

I have also tried simply setting

```auto
this->cylinderActor->SetOrientation(this->GetCurrentRenderer()->GetActiveCamera()->GetOrientation()

```

but this hasn’t worked either.

---

<div class="post-metadata">

### Author: ![jimiheinonen](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/j/22d042/32.png) [@jimiheinonen](https://discourse.vtk.org/u/jimiheinonen)
#### Post date: [May 12, 2025, 8:39am UTC](https://discourse.vtk.org/t/orienting-cylinder-to-point-same-way-as-camera/15586/4 "2025-05-12T08:39:56Z")

</div>

Hello, any one have any ideas? I’ve managed to solve for Z rotation, but when attempting to do rotation in X, it produces weird rotation results. The code I have for getting the angles is

```auto
double* point1 = this->GetCurrentRenderer()->GetActiveCamera()->GetPosition();
double* point2 = this->cylinderActor->GetPosition();

// Find x and z angle
double XY = atan2(point2[1] - point1[1], point2[0] - point1[0]) * 180 / M_PI;
double ZY = atan2(point2[2] - point1[2], point2[1] - point1[1]) * 180 / M_PI;
double ZX = atan2(point2[2] - point1[2], point2[0] - point1[0]) * 180 / M_PI;

double orient[3] = { 0, 0, XY - 90 }; // TODO: How to solve for X rotation?

this->cylinderActor->SetOrientation(orient);

```

If I set the X orientation to anything, even an arbitrary angle, the orientation is wrong. I have tried using the ZY and ZX angles above but they don’t give a workable result.

Any help is appreciated!
