Orienting cylinder to point same way as camera

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!

Hello,

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

best,

PC

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

            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

I have also tried simply setting

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

but this hasn’t worked either.