File: project-points-tessellate.py

package info (click to toggle)
python-pyvista 0.44.1-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 159,804 kB
  • sloc: python: 72,164; sh: 118; makefile: 68
file content (77 lines) | stat: -rw-r--r-- 1,778 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Project points to a plane and Tessellate
----------------------------------------

Using pyvista and numpy, generate a 3D point cloud, project it to a plane, and
tessellate it.

This demonstrates how to use
:class:`pyvista.UnstructuredGridFilters.delaunay_2d` and a simple numpy
function that projects points to a plane.

"""

from __future__ import annotations

import numpy as np

import pyvista as pv

# %%
# Project Points
# ~~~~~~~~~~~~~~
# Create a point cloud and project it to a plane.

num_points = 100
rng = np.random.default_rng(seed=0)  # Seed rng for reproducibility
point_cloud = rng.random((num_points, 3))

# Define a plane
origin = [0, 0, 0]
normal = [0, 0, 1]
plane = pv.Plane(center=origin, direction=normal)


def project_points_to_plane(points, plane_origin, plane_normal):
    """Project points to a plane."""
    vec = points - plane_origin
    dist = np.dot(vec, plane_normal)
    return points - np.outer(dist, plane_normal)


projected_points = project_points_to_plane(point_cloud, origin, normal)

# Create a polydata object with projected points
polydata = pv.PolyData(projected_points)

# Mesh using delaunay_2d and pyvista
mesh = polydata.delaunay_2d()


# %%
# Visualize the Result
# ~~~~~~~~~~~~~~~~~~~~

# Create a plane for visualization
plane_vis = pv.Plane(
    center=origin,
    direction=normal,
    i_size=2,
    j_size=2,
    i_resolution=10,
    j_resolution=10,
)

# plot it
pl = pv.Plotter()
pl.add_mesh(mesh, show_edges=True, color='white', opacity=0.5, label='Tessellated mesh')
pl.add_mesh(
    pv.PolyData(point_cloud),
    color='red',
    render_points_as_spheres=True,
    point_size=10,
    label='Points to project',
)
pl.add_mesh(plane_vis, color='blue', opacity=0.1, label='Projection Plane')
pl.add_legend()
pl.show()