what is the best way to automatically find a good angle/position of the camera when looking at a 2d plane? #1417
-
DescriptionHello all, I am a very new user of pyvista or any other code based visualization tools (only using paraview GUI for the past years) I have a few 2d planes with some scalar data (might not always be perfect 2D, but relatively 2D, or would like to be viewed as 2d planes). I would like to know what would be the best way to automatically fit them in to the frame and put the camera direction perpendicular to the center of the plane. Basically I am trying to find a good way to define their positioning by my data's traits (for example width, length, diagonal, figure center, cell normals...). I looked at the bunny example on this page. https://docs.pyvista.org/core/camera.html near_range = 0.3
far_range = 0.8
unit_vector = np.array(camera.direction) / np.linalg.norm(
np.array([camera.focal_point]) - np.array([camera.position])
)
xyz = camera.position + unit_vector * 0.6 - np.mean(bunny.points, axis=0)
bunny.translate(xyz) I hope someone can give me a quick walkthrough or point me to a full tutorial on this topic. Thanks a lot! Junting Chen Example Data |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
It is depends on what axes your 2D data is on. If it is on X-Y axis, Your camera direction must be (0.0, 0.0, -1.0).
Sorry for lack of explanation. That code is actually the moving bunny mesh to another point. Maybe it it better to make example without moving mesh. |
Beta Was this translation helpful? Give feedback.
-
I find the following example works often for me: plotter = pyvista.Plotter()
plotter.add_mesh(mesh_2D)
plotter.view_xy() # if mesh_2D is on the xy plane.
plotter.show() |
Beta Was this translation helpful? Give feedback.
-
What I would do in a general case is take the camera, and position it such that its axis is parallel with the plane's normal and it lies on the center of the plane: import pyvista as pv
import numpy as np
# generate random plane
rng = np.random.default_rng()
center, direction = rng.uniform(size=(2, 3)) * 2 - 1
plane = pv.Plane(center=center, direction=direction)
plotter = pv.Plotter()
plotter.add_mesh(plane)
# find the plane's center and normal from scratch
center = plane.center
normal = plane.compute_normals()['Normals'].mean(axis=0)
# align camera: focus on center, position at center + normal
plotter.camera.focal_point = center
plotter.camera.position = center + normal
# reset camera to put entire mesh in view
plotter.reset_camera()
plotter.show() If there are other meshes in the scene then you'll need something like plotter.reset_camera(bounds=plane.bounds) If you also want to orient your plane with the x and y axes, you'll need more work. You'll have to compute the direction of the x or y axis of your plane, and figure out the angle with which you have to Finally, if you're happy with the above setup you might consider setting the interaction style to plotter.enable_image_style() |
Beta Was this translation helpful? Give feedback.
-
I think the best way is to start with a fixed
|
Beta Was this translation helpful? Give feedback.
@chen112p
It is depends on what axes your 2D data is on. If it is on X-Y axis, Your camera direction must be (0.0, 0.0, -1.0).
Sorry but pyvista does not have function to fit mesh automatically. You would have to adjust the camera posi…