Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

direction to plane #1213

Open
smoothumut opened this issue Dec 27, 2024 · 0 comments
Open

direction to plane #1213

smoothumut opened this issue Dec 27, 2024 · 0 comments

Comments

@smoothumut
Copy link
Contributor

smoothumut commented Dec 27, 2024

Hi Marco,
Hope, everything is great with you.

I have noticed that, vedo.Plane doesnt have a direction.
in my case a I want to create a plane and I need to adjust its direction. I couldnt do that with original Plane code so I have adjusted like this. I hope you find it useful

class Plane(Mesh):
"""Create a plane in space."""

def __init__(
    self,
    pos=(0, 0, 0),
    normal=(0, 0, 1),
    s=(1, 1),
    res=(1, 1),
    direction=None,  # Optional: direction for edge alignment
    c="gray5",
    alpha=1.0,
) -> None:
    """
    Create a plane of size `s=(xsize, ysize)` oriented perpendicular
    to vector `normal` so that it passes through point `pos`, optionally
    aligning an edge with `direction`.

    Arguments:
        pos : (list)
            position of the plane center
        normal : (list)
            normal vector to the plane
        s : (list)
            size of the plane along x and y
        res : (list)
            resolution of the plane along x and y
        direction : (list, optional)
            direction vector to align one edge of the plane
    """
    if isinstance(pos, vtki.vtkPolyData):
        super().__init__(pos, c, alpha)
    else:
        ps = vtki.new("PlaneSource")
        ps.SetResolution(res[0], res[1])
        tri = vtki.new("TriangleFilter")
        tri.SetInputConnection(ps.GetOutputPort())
        tri.Update()

        super().__init__(tri.GetOutput(), c, alpha)

        pos = utils.make3d(pos)
        normal = np.asarray(normal, dtype=float)
        axis = normal / np.linalg.norm(normal)

        # Calculate orientation using normal
        theta = np.arccos(axis[2])
        phi = np.arctan2(axis[1], axis[0])

        t = LinearTransform()
        t.scale([s[0], s[1], 1])

        # Rotate to align normal
        t.rotate_y(np.rad2deg(theta))
        t.rotate_z(np.rad2deg(phi))

        # Additional direction alignment
        if direction is not None:
            direction = np.asarray(direction, dtype=float)
            direction /= np.linalg.norm(direction)

            if s[0] <= s[1]:
                current_direction = np.asarray([0,1,0])
            elif s[0] > s[1]:
                current_direction = np.asarray([1,0,0])
                
            transformed_current_direction = t.transform_point(current_direction)
            norm_transformed_current_direction = transformed_current_direction / np.linalg.norm(transformed_current_direction)

            if np.linalg.norm(transformed_current_direction) >= 1e-6:
                angle = np.arccos(np.dot(norm_transformed_current_direction, direction))
                t.rotate(axis=axis, angle=np.rad2deg(angle))
            

        t.translate(pos)
        self.apply_transform(t)

    self.lighting("off")
    self.name = "Plane"
    self.variance = 0

In my case it works as intended. So I wanted to share if it might be useful in vedo.
what do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant