Debian Codegraph @

examples/01-filter/point_cloud_orientation.py

raw
1 """
2 .. _point_cloud_orientation_example:
3
4 Analyze the Orientation of a Point Cloud
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
7 Fit a line, plane, and oriented bounding box to a tilted point cloud with
8 PyVista's principal-axis utilities.
9 """
10
11 import numpy as np
12 import pyvista as pv
13 from pyvista import examples
14
15 # sphinx_gallery_thumbnail_number = 2
16
17 # %%
18 # Load and tilt a point cloud
19 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 # Subsample the :func:`~pyvista.examples.downloads.download_horse_points`
21 # scan and rotate the copy off the world axes so the recovered orientation
22 # is non-trivial.
23
24 full_cloud = examples.download_horse_points()
25 rng = np.random.default_rng(seed=4)
26 sample_ids = rng.choice(full_cloud.n_points, size=3000, replace=False)
27 transform = pv.Transform().rotate_vector((1, 1, 0), 33).rotate_y(18)
28 cloud = pv.PolyData(full_cloud.points[sample_ids]).transform(transform, inplace=False)
29 cloud
30
31 # %%
32 # Pick a camera that frames the rotated cloud well.
33
34 cpos = pv.CameraPosition(
35 position=(0.4, -0.5, 0.25),
36 focal_point=cloud.center,
37 viewup=(0, 0, 1),
38 )
39
40
41 # %%
42 # Fit a line and a plane
43 # ~~~~~~~~~~~~~~~~~~~~~~
44 # The fitted line follows the cloud's dominant axis; the fitted plane spans
45 # the two strongest principal directions.
46
47 line, length, direction = pv.fit_line_to_points(
48 cloud.points,
49 init_direction='x',
50 return_meta=True,
51 )
52 plane = pv.fit_plane_to_points(cloud.points, init_normal='z')
53
54 arrow = pv.Arrow(
55 start=line.points[0],
56 direction=direction,
57 scale=length,
58 tip_length=0.12,
59 tip_radius=0.04,
60 shaft_radius=0.015,
61 )
62
63 pl = pv.Plotter()
64 pl.add_points(
65 cloud,
66 color='black',
67 point_size=4,
68 render_points_as_spheres=True,
69 opacity=0.4,
70 )
71 pl.add_mesh(plane, color='orange', opacity=0.25)
72 pl.add_mesh(arrow, color='tomato')
73 pl.camera_position = cpos
74 pl.show()
75
76
77 # %%
78 # Compare axis-aligned and oriented boxes
79 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80 # The axis-aligned box ignores the tilt; the oriented box snaps to the
81 # cloud's principal directions.
82
83 axis_aligned_box = cloud.bounding_box('outline', as_composite=False)
84 oriented_box = cloud.oriented_bounding_box(
85 'outline',
86 axis_2_direction='z',
87 as_composite=False,
88 )
89
90 pl = pv.Plotter(shape=(1, 2))
91 pl.subplot(0, 0)
92 pl.add_points(
93 cloud,
94 color='black',
95 point_size=4,
96 render_points_as_spheres=True,
97 opacity=0.4,
98 )
99 pl.add_mesh(axis_aligned_box, color='tomato', line_width=4)
100 pl.camera_position = cpos
101 pl.subplot(0, 1)
102 pl.add_points(
103 cloud,
104 color='black',
105 point_size=4,
106 render_points_as_spheres=True,
107 opacity=0.4,
108 )
109 pl.add_mesh(oriented_box, color='seagreen', line_width=4)
110 pl.camera_position = cpos
111 pl.link_views()
112 pl.show()
113
114
115 # %%
116 # Quantify the dominant directions
117 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
118 # Normalized principal-axis standard deviations report the relative spread
119 # along each fitted axis.
120
121 _, std = pv.principal_axes(cloud.points, return_std=True)
122 std / std.sum()
123 # %%
124 # .. tags:: filter