dfki-ric / pytransform3d

3D transformations for Python.
https://dfki-ric.github.io/pytransform3d/
Other
632 stars 68 forks source link

Plotting a plane given either two vectors or normal #183

Closed ekmungi closed 2 years ago

ekmungi commented 2 years ago

Hi, I would like to extend my appreciation for the work from the developers in this library. Thank you!

Is there already a function that can plot a plane given either two vectors or a normal in pytransform3d?

Thanks. Anant.

AlexanderFabisch commented 2 years ago

Hi @ekmungi

I would like to extend my appreciation for the work from the developers in this library. Thank you!

Thanks!

Is there already a function that can plot a plane given either two vectors or a normal in pytransform3d?

It's not in this library, but I have a function for Open3D's visualizer in an internal library (see below). If you need something for matplotlib, it should be easy to rewrite. In order to integrate this function in pytransform3d, we would need a corresponding Plane class, which is a subclass of the Artist base class.

import numpy as np
import open3d as o3d
import pytransform3d.rotations as pr

def plot_plane(figure, normal, d=None, point_in_plane=None, scale=1.0,
               c=(0.5, 0.5, 0.5)):
    """Visualize plane in Hesse normal form.

    Parameters
    ----------
    figure : pytransform3d.visualizer.Figure
        Figure.

    normal : array-like, shape (3,)
        Plane normal.

    d : float, optional (default: None)
        Distance to origin in Hesse normal form.

    point_in_plane : array-like, shape (3,), optional (default: None)
        Point in plane.

    scale : float, optional (default: 1)
        Scale of visualized plane.

    c : array-like, shape (3,)
        Color as RGB. Values have to be between 0 and 1.
    """
    if point_in_plane is None:
        assert d is not None
        point_in_plane = d * normal
    x_axis, y_axis = pr.plane_basis_from_normal(normal)
    vertices = np.array([
        point_in_plane + scale * x_axis + scale * y_axis,
        point_in_plane - scale * x_axis + scale * y_axis,
        point_in_plane + scale * x_axis - scale * y_axis,
        point_in_plane - scale * x_axis - scale * y_axis,
    ])
    triangles = np.array([
        [0, 1, 2],
        [1, 3, 2],
        [2, 1, 0],
        [2, 3, 1],
    ])
    mesh = o3d.geometry.TriangleMesh(
        o3d.utility.Vector3dVector(vertices),
        o3d.utility.Vector3iVector(triangles))
    mesh.compute_vertex_normals()
    mesh.paint_uniform_color(c)
    figure.add_geometry(mesh)
ekmungi commented 2 years ago

Thank you . Perfect!

Sincerely, Anant.