-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfence_add_surface.py
More file actions
95 lines (77 loc) · 3.79 KB
/
fence_add_surface.py
File metadata and controls
95 lines (77 loc) · 3.79 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pyvista as pv
import numpy as np
def fence_add_surface(plotter,xs,ys,zs,colors,ds_fac=2,cmap='viridis',opacity=1,lighting=[0.4,0.8,0]):
"""
% (C) Nick Holschuh - Amherst College - 2026 ([email protected])
% This function uses PyVista to add a horizontal surface to an interactive fence diagram
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are:
%
% plotter - The Pyvista plotter object generated by "fence_initiate"
% xs - 1d array of x values
% ys - 1d array of y values
% zs - 2d array of z values
% colors - 2d array (for colormapped) or 3d array (for rgb) colors for surface
% ds_fac - Integer used to downsample the surface in both horizontal dimensions
% opacity - Value between 0 and 1 indicating fence opacity. Large values are better
% lighting - three value array describing the ambient, diffuse, and specular light (0-1)
%
%%%%%%%%%%%%%%%
% The output is a dictionary containing:
%
% plotter - The plotter object which can be passed into later functions
% mesh - The way PyVista works requires that the mesh still be in memory. So
% preserving independent mesh objects is required to generate the final html file
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
def format_colors(colors, m, n, ds_fac=ds_fac):
c = np.asarray(colors)
# 1. Fix data type and scale
if np.issubdtype(c.dtype, np.floating) and c.max() <= 1.0:
c = c * 255
c = c.astype(np.uint8)
# 2. Fix dimensionality and order
if c.size == m * n:
# Handle (n, m) transposing; otherwise force to (m, n)
c = c.T if c.shape == (n, m) and m != n else c.reshape(m, n)
c = c[::ds_fac,::ds_fac]
elif c.size == 3 * m * n:
# If color channel is last e.g. (m, n, 3), move it to front
if c.ndim == 3 and c.shape[-1] == 3 and m != 3 and n != 3:
c = np.moveaxis(c, -1, 0)
# If the spatial axes are swapped e.g. (3, n, m), transpose them
if c.shape[1:] == (n, m) and m != n:
c = c.transpose(0, 2, 1)
c = c[:,::ds_fac,::ds_fac]
else:
raise ValueError(f"Colors size ({c.size}) does not match mxn or 3xmxn.")
return c
#print(colors.shape)
colors = format_colors(colors,len(xs),len(ys),ds_fac)
x = xs[::ds_fac]
y = ys[::ds_fac]
xgrid, ygrid = np.meshgrid(x, y)
zgrid = zs[::ds_fac,::ds_fac]
#print(xgrid.shape,ygrid.shape,zgrid.shape,colors.shape)
# Step A: Move the color channels to the end -> shape becomes (50, 50, 3)
colors_transposed = np.transpose(colors, (1, 2, 0))
# Step B: Flatten it into a 2D list of RGB pixels -> shape becomes (2500, 3)
colors_flat = colors_transposed.reshape(-1, 3)
# Step C: Ensure the values are integers between 0 and 255
colors_flat = colors_flat.astype(np.uint8)
# Create a structured grid directly from your 2D coordinate arrays
mesh = pv.StructuredGrid(xgrid, ygrid, zgrid)
# Assign your flattened RGB array to the points of the mesh
mesh.point_data['my_rgb_colors'] = colors_flat
# Add the mesh. rgb=True is the magic word that tells PyVista to use true colors!
plotter.add_mesh(mesh,
scalars='my_rgb_colors',
rgb=True,
opacity=opacity,
ambient=lighting[0], # Boost this to brighten the raw RGB colors
diffuse=lighting[1], # Standard matte scattering
specular=lighting[2], # No shiny glare
nan_opacity=0.0
)
return plotter, mesh