pyvista-support
pyvista-support copied to clipboard
Problem with NumPy texture mapping
I've been following instructions on this page, can replicate the examples there, but can't make it work for my own problem.
Description
I have a 3D structured grid and an image that I want to overlay on it. The grid is based on data (stereo reconstruction) not a function, and the data source is given below.
import numpy as np
from matplotlib import cm
import pickle
import pyvista as pv
vars = pickle.load(open('data.p', 'rb'))
X = vars['X']
Y = vars['Y']
Z = vars['Z']
image = vars['image']
print('Z shape', Z.shape)
print('image shape', image.shape)
grid = pv.StructuredGrid(X, Y, Z)
tex = pv.numpy_to_texture(image[::-1,:]) # flip texture top to bottom
grid.texture_map_to_plane(inplace=True)
plotter = pv.Plotter()
plotter.add_mesh(grid, texture=tex)
plotter.camera_position = np.array(
[(0.07503859607946789, -0.10668551787851316, 0.8259781553143458),
(-0.00025559105431308127, -0.00025559105431309515, 1.7847194073969184),
(-0.006901873996314264, -0.9939303744479336, 0.10979423885217895)]
)
plotter.show(window_size=(2000,2000))
Here is a snapshot of the 3D scene with texture mapping:
The texture looks vertically compressed, it repeats about three times. Circles indicate distinctive texture which is repeating.
The image is exactly the same size as the X, Y and Z arrays.
For reference here is the image which is used for texture:
Data
The data is in a pickle file you can get from here
wget https://www.dropbox.com/s/rgwca3arw7bzwr6/data.p
We have at least one known bug that affects texture arrays, but hopefully this isn't related (see the mangled first plot in the Topographic Map example).
Could you please try replacing your example's pickle workflow with numpy.savez/numpy.load with allow_pickle=False? This would make it safe to load your data, allowing me to play with it :)
In the meantime, does passing use_bounds=True to grid.texture_map_to_plane not help? (Or origin, point_u, point_v for a more general orientation of the plane.)
Here we go.
import numpy as np
from matplotlib import cm
import pyvista as pv
with np.load('data.npz') as data:
X = data['X']
Y = data['Y']
Z = data['Z']
image = data['image']
print('Z shape', Z.shape)
print('image shape', image.shape)
grid = pv.StructuredGrid(X, Y, Z)
tex = pv.numpy_to_texture(image[::-1,:])
grid.texture_map_to_plane(inplace=True)
plotter = pv.Plotter()
plotter.add_mesh(grid, texture=tex)
plotter.camera_position = np.array(
[(0.07503859607946789, -0.10668551787851316, 0.8259781553143458),
(-0.00025559105431308127, -0.00025559105431309515, 1.7847194073969184),
(-0.006901873996314264, -0.9939303744479336, 0.10979423885217895)]
)
plotter.show(window_size=(2000,2000))
print(cpos)
and the data file is
wget https://www.dropbox.com/s/50q1ryuw3wdp9v5/data.npz
And yes, use_bounds=True fixes it. Texture mapping not something I'm very familiar with so the documentation is wasted on me, I was just reading the pictures and examples :) Thanks.
Any thought about anaglyph support in the future?
And it works with an RGB image as well. I dropped back to greyscale to simplify the debugging.
And yes, use_bounds=True fixes it. Texture mapping not something I'm very familiar with so the documentation is wasted on me, I was just reading the pictures and examples :) Thanks.
And it works with an RGB image as well. I dropped back to greyscale to simplify the debugging.
Excellent! And thanks for the switch to npz, I can see your mesh now. If you don't pass anything to specify where the plane goes, PyVista leaves it to VTK. And according to the VTK docs the default is that
A least squares method is used to generate the plane automatically.
I suspect the jumpiness in your data threw off the least-squares fitting, leading to a smaller plane to map to.
Any thought about anaglyph support in the future?
I have no idea about internals, sorry :) How would anaglyph support work here? Are there other parts of VTK that have anaglyph support?
Thanks for your help. Indeed my data is jumpy, it has some significant outliers.
I don't know much about VTK but vtkRenderWindow seems to have support for stereo display, anaglyphs as well as shutter glasses. Here's a link to one end of the ball of twine...