vtkCylinderSource() transformation

Hello!
Using the pyransac3d algorithm, I calculate the cylinder parameters and then try to create an actor from them. I know that the parameters such as the normal determine well, as for example the height of the cylinder is correct according to the normal as well as the points selected for best fitting, However, my cylinder is still not rotated in the right direction. Why? What am I doing wrong in the code?

    def draw_cylinder(
            self, radius: float = 1.0, height: float = 5.0,
            center: tuple[float, float, float] = (0.0, 0.0, 0.0),
            normal: tuple[float, float, float] = (0.0, 0.0, 1.0),
            color: tuple[float, float, float] = (1.0, 1.0, 1.0), opacity: float = 1.0,
            res: float = 100) -> vtkActor:
        """Create and add cylinder actor for scene.

        :param center: Center of the cylinder.
        :type center: tuple[float, float, float]
        :param normal: Normal to the cylinder.
        :type normal: tuple[float, float, float]
        :param radius: Radius of the cylinder.
        :type radius: float
        :param height: Height of the cylinder.
        :type height: float
        :param color: Color of the cylinder actor.
        :type color: tuple[float, float, float]
        :param opacity: Opacity of the cylinder actor.
        :type opacity: float
        :param res: Resolution of the cylinder actor.
        :type res: float

        :return: Return created actor.
        :rtype: vtkActor
        """
        cylinder = vtkCylinderSource()
        cylinder.SetRadius(radius)
        cylinder.SetHeight(height)
        cylinder.SetResolution(res)

        transform = vtkTransform()
        transform.Translate(center)  # Set the position
        # Set the orientation based on the normal
        # You may need to adjust this depending on your coordinate system
        transform.RotateWXYZ(90, normal[0], normal[1], normal[2])
        # Apply the transformation to the actor

        mapper = vtkPolyDataMapper()
        mapper.SetInputConnection(cylinder.GetOutputPort())

        actor = vtkActor()
        actor.SetMapper(mapper)
        actor.GetProperty().SetColor(*color)
        actor.GetProperty().SetOpacity(opacity)
        actor.SetUserTransform(transform)
        self.add_actor(actor)

        return actor

Here is a example of my best fitting:

The vtkCylinderSource produces a cylinder aligned with the Y axis. So you what you need is a rotation that takes the Y axis to the new axis. If I’ve understood your question, that is.

So let’s say you want to take the Y axis to the Z axis. The rotation vector for this is the cross product of Y and Z, which is X. Therefore, you would rotate 90 degrees around X.

In more general terms, the vtkMath.SignedAngleBetweenVectors(v1, v2, v3) can be used to compute the rotation angle between vectors, given the cross-product vector.

Yup, it as you said. When i took y axis as base cylinder axis and rotate my actor i got proper fitted cylinder.