def test_qfigure(self): fig = vpl.QtFigure("a qt widget figure") self.assertIs(fig, vpl.gcf()) direction = np.array([1, 0, 0]) vpl.quiver(np.array([0, 0, 0]), direction) vpl.view(camera_direction=direction) vpl.reset_camera() vpl.show()
def test_view(): vpl.auto_figure(True) vpl.close() grads = np.array(vpl.geometry.orthogonal_bases(np.random.rand(3))) point = np.random.uniform(-10, 10, 3) vpl.quiver(np.broadcast_to(point, (3, 3)), grads, color=np.eye(3)) vpl.view(focal_point=point, camera_position=point - grads[0], up_view=grads[1]) vpl.reset_camera() vpl.text("Should be looking in the direction of the red arrow, " "with the green arrow pointing up") # Linux seems to need an extra prod to render this for some reason. vpl.show(block=False)
def test_multi_figures(self): vpl.close() vpl.auto_figure(False) plot = vpl.plot(np.random.uniform(-10, 10, (10, 3)), join_ends=True) figs = [] for i in range(1, 4): fig = vpl.figure("figure {}".format(i)) fig += plot vpl.view(camera_direction=np.random.uniform(-1, 1, 3), fig=fig) vpl.reset_camera(fig) fig.show(False) figs.append(fig) fig.show() vpl.auto_figure(True)
def button_pressed_cb(self): """Plot commands can be called in callbacks. The current working figure is still self.figure and will remain so until a new figure is created explicitly. So the ``fig=self.figure`` arguments below aren't necessary but are recommended for larger, more complex scenarios. """ # Randomly place a ball. vpl.scatter(np.random.uniform(-30, 30, 3), color=np.random.rand(3), fig=self.figure) # Reposition the camera to better fit to the balls. vpl.reset_camera(self.figure) # Without this the figure will not redraw unless you click on it. self.figure.update()
def test_qfigure(): vpl.QtFigure._abc_assert_no_abstract_methods() self = vpl.QtFigure("a Qt widget figure") assert self is vpl.gcf() direction = np.array([1, 0, 0]) vpl.quiver(np.array([0, 0, 0]), direction) vpl.view(camera_direction=direction) vpl.reset_camera() self.show(block=False) self.close() self.showMaximized(block=not VTKPLOTLIB_WINDOWLESS_TEST) out = vpl.screenshot_fig(fig=self) vpl.close(fig=self) globals().update(locals()) return out
def test_view(self): vpl.auto_figure(True) vpl.close() grads = np.array(vpl.geometry.orthogonal_bases(np.random.rand(3))) point = np.random.uniform(-10, 10, 3) vpl.quiver(np.broadcast_to(point, (3, 3)), grads, color=np.eye(3)) vpl.view(focal_point=point, camera_position=point - grads[0], up_view=grads[1]) # vpl.view(camera_direction=grads[0], # up_view=grads[1], # ) # vpl.reset_camera() # vpl.view(point) vpl.text( "Should be looking in the direction of the red arrow, with the green arrow pointing up" ) vpl.show()
def get_projections(file, camera_directions=CAMERA_DIRS): """ Get projection views in given camera directions from an STL file. Returns: Numpy array of shape (PROJ_SHAPE, len(camera_directions)). """ views = [] for dir in camera_directions[::-1]: mesh = Mesh.from_file(file) vpl.mesh_plot(mesh, color="black") r = vpl.view(camera_position=(0, 0, 0), camera_direction=dir) vpl.reset_camera() vpl.zoom_to_contents(padding=1) # Upscale first so downscale is more accurate arr = vpl.screenshot_fig(magnification=10, off_screen=True) # Change 3-channel RGB image to single channel binary matrix arr = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY) arr[arr == 0] = 1 arr[arr == 218] = 0 arr = cv2.resize(arr, dsize=PROJ_SHAPE, interpolation=cv2.INTER_LINEAR) vpl.close() views.append(arr) views = np.array(views).reshape( (PROJ_SHAPE[0], PROJ_SHAPE[1], len(camera_directions))) return views
@property def window_name(self): if hasattr(self, "_renWin"): return self.renWin.GetWindowName() return self._window_name @window_name.setter def window_name(self, window_name): if hasattr(self, "_renWin"): self.renWin.SetWindowName(window_name) self._window_name = window_name if __name__ == "__main__": Figure._abc_assert_no_abstract_methods() import vtkplotlib as vpl self = vpl.figure("a normal vtk figure") # vpl.plot(np.random.uniform(-10, 10, (20, 3))) direction = np.array([1, 0, 0]) vpl.quiver(np.array([0, 0, 0]), direction) vpl.view(camera_direction=direction) vpl.reset_camera() # vpl.save_fig(Path.home() / "img.jpg", 1080) self.show()