def test_conversions(): vpl.quick_test_plot() arr = vpl.screenshot_fig() vpl.close() image_data = vpl.image_io.vtkimagedata_from_array(arr) arr2 = vpl.image_io.vtkimagedata_to_array(image_data) assert np.array_equal(arr, arr2)
def test_figure_io(): vpl.close() assert vpl.gcf(False) is None vpl.auto_figure(False) assert vpl.gcf() is None with pytest.raises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() assert vpl.gcf() is None del fig vpl.auto_figure(True) fig = vpl.gcf() assert fig is not None assert fig is vpl.gcf() vpl.close() assert vpl.gcf(False) is None vpl.scf(fig) assert fig is vpl.gcf() vpl.close() fig = vpl.figure() assert fig is vpl.gcf() vpl.close()
def test_figure_io(self): vpl.close() self.assertIs(vpl.gcf(False), None) vpl.auto_figure(False) self.assertIs(vpl.gcf(), None) with self.assertRaises(vpl.figures.figure_manager.NoFigureError): vpl.screenshot_fig() fig = vpl.figure() self.assertIs(vpl.gcf(), None) del fig vpl.auto_figure(True) fig = vpl.gcf() self.assertTrue(fig is not None) self.assertIs(fig, vpl.gcf()) vpl.close() self.assertIs(vpl.gcf(False), None) vpl.scf(fig) self.assertIs(fig, vpl.gcf()) vpl.close() fig = vpl.figure() self.assertIs(fig, vpl.gcf()) vpl.close()
def test_add_remove(self): fig = vpl.figure() plots = vpl.quick_test_plot(None) fig += plots fig.show(False) fig -= plots for i in plots: fig += i fig.update() time.sleep(.05) for i in plots: fig -= i fig.update() time.sleep(.05) vpl.close(fig)
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 test_trim_image(): fig = vpl.figure() # Shouldn't do anything if the figure is empty. assert vpl.screenshot_fig(trim_pad_width=.05).shape[:2] == fig.render_size vpl.quick_test_plot() arr = vpl.screenshot_fig() vpl.close() trimmed = vpl.image_io.trim_image(arr, fig.background_color, 10) background_color = np.asarray(fig.background_color) * 255 # Check that no non-background coloured pixels have been lost. assert (arr != background_color).any(-1).sum() \ == (trimmed != background_color).any(-1).sum() return trimmed
def test_type_normalise(): mesh = numpy_stl().Mesh.from_file(path) vectors = mesh.vectors unique_points = set(tuple(i) for i in vectors.reshape(len(vectors) * 3, 3)) points_enum = {point: i for (i, point) in enumerate(unique_points)} points = np.array(sorted(unique_points, key=points_enum.get)) point_args = np.apply_along_axis(lambda x: points_enum[tuple(x)], -1, vectors) vpl.plots.MeshPlot.NUMPY_STL_AVAILABLE = False for fmt in (path, mesh, vectors, (points, point_args)): normalised = vpl.mesh_plot(fmt).vectors assert np.array_equal(normalised, vectors) vpl.plots.MeshPlot.NUMPY_STL_AVAILABLE = True vpl.close()
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 test_save(self): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) # I can't get python2 to cooperate with unicode here. # The os functions just don't like them. if sys.version[0] == "3": path = Path.cwd() / u"ҢघԝઌƔࢳܢˀા\\Հએࡓ\u061cཪЈतயଯ\u0886.png" try: os.mkdir(str(path.parent)) vpl.save_fig(path) self.assertTrue(path.exists()) os.remove(str(path)) finally: if path.parent.exists(): os.rmdir(str(path.parent)) else: path = Path.cwd() / "image.png" vpl.save_fig(path) os.remove(str(path)) array = vpl.screenshot_fig(2) self.assertEqual(array.shape, tuple(i * 2 for i in vpl.gcf().render_size) + (3, )) plt.imshow(array) plt.show() shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3))) vpl.screenshot_fig(pixels=shape).shape # The following will fail depending on VTK version # self.assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() fig = vpl.figure() for plot in plots: fig += plot vpl.show()
def test_save(): plots = vpl.scatter(np.random.uniform(-10, 10, (30, 3))) path = TEST_DIR / "name.png" if path.exists(): os.remove(str(path)) vpl.save_fig(path) assert path.exists() array = vpl.screenshot_fig(magnification=2) assert array.shape == tuple(i * 2 for i in vpl.gcf().render_size) + (3, ) shape = tuple(i * j for (i, j) in zip(vpl.gcf().render_size, (2, 3))) vpl.screenshot_fig(pixels=shape).shape # The following will fail depending on VTK version # .assertEqual(vpl.screenshot_fig(pixels=shape).shape, # shape[::-1] + (3,)) vpl.close() return array
def renderimage(tread,options): reifenflanke = 'rimAndShoulder/reifenflanke.stl' modulfelge = 'rimAndShoulder/modulfelge.stl' # Read the STL using numpy-stl geom = mesh.Mesh.from_file(tread) shoulder = mesh.Mesh.from_file(reifenflanke) rim = mesh.Mesh.from_file(modulfelge) # Plot the mesh vpl.mesh_plot(geom,color=[76,76,76]) if options == 'full': vpl.mesh_plot(shoulder,color=[76,76,76]) vpl.mesh_plot(rim,color=[1,1,1]) # Show the figure vpl.view(camera_direction = [0.8,0.5,0],up_view=[0,0,1]) #fig = vpl.gcf() #fig.background_color = "transparent" #vpl.show() #name = 'render.png' vpl.save_fig('render/render.png',off_screen=True,magnification=2) vpl.close()
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