def generateImages(self, mesh, path): axisName = 'w' for axis in self.axes: if axisName == 'w': axisName = 'x' elif axisName == 'x': mesh.rotate(np.array([1, 0, 0]), math.radians(45), None) axisName = 'y' elif axisName == 'y': mesh.rotate(np.array([0, 1, 0]), math.radians(45), None) axisName = 'z' elif axisName == 'z': mesh.rotate(np.array([0.5, 0.866, 0]), math.radians(45), None) axisName = 'zy' for x in range(math.floor(self.fullRotation / self.rotationAngle)): if axisName == 'z': mesh.rotate(np.array([0, 1, 0]), math.radians(5), None) # dual rotation for extra variation # rotate(axis, theta=0, point=None) mesh.rotate(axis, math.radians(self.rotationAngle), None) # Plot the mesh vtkplotlib.mesh_plot(mesh, color="blue") vtkplotlib.save_fig( os.path.normpath(path + '/_' + axisName + str(x) + '.png')) #saves the figure as an image vtkplotlib.figure.close(vtkplotlib.gcf())
def generate_thumbnail(infile, outfile, size=None): """Generate a thumbnail or previewfile into 'outfile' using the design file in 'infile'""" mesh = Mesh.from_file(infile) vpl.mesh_plot(mesh) # Front of design and slightly up vpl.view(camera_position=[0, -1, 0.5]) vpl.save_fig(outfile, pixels=size or [1280, 1280], off_screen=True)
def test_doc_05(self): import vtkplotlib as vpl from stl.mesh import Mesh # path = "if you have an STL file then put it's path here." # Otherwise vtkplotlib comes with a small STL file for demos/testing. path = vpl.data.get_rabbit_stl() # Read the STL using numpy-stl mesh = Mesh.from_file(path) # Plot the mesh vpl.mesh_plot(mesh)
def test_doc_07(self): import vtkplotlib as vpl from stl.mesh import Mesh import numpy as np # Open an STL as before path = vpl.data.get_rabbit_stl() mesh = Mesh.from_file(path) # `tri_scalars` must have one value per triangle and have shape (n,) or (n, 1). # Create some scalars showing "how upwards facing" each triangle is. tri_scalars = np.inner(mesh.units, np.array([0, 0, 1])) vpl.mesh_plot(mesh, tri_scalars=tri_scalars)
def test_mesh(self): import time fig = vpl.gcf() path = vpl.data.get_rabbit_stl() _mesh = Mesh.from_file(path) self = vpl.mesh_plot(_mesh.vectors) fig.show(False) t0 = time.time() for i in range(100): # self.color = np.random.random(3) # print(self.color) self.set_tri_scalars((_mesh.x[:, 0] + 3 * i) % 20) _mesh.rotate(np.ones(3), .1, np.mean(_mesh.vectors, (0, 1))) fig.update() self.update_points() # time.sleep(.01) if (time.time() - t0) > 1: break fig.show()
def test(): from stl.mesh import Mesh import vtkplotlib as vpl mesh = Mesh.from_file(vpl.data.get_rabbit_stl()) plot = vpl.mesh_plot(mesh, scalars=mesh.x) vpl.scalar_bar(plot) vpl.show()
def test_doc_06(self): import vtkplotlib as vpl from stl.mesh import Mesh # Open an STL as before path = vpl.data.get_rabbit_stl() mesh = Mesh.from_file(path) # Plot it with the z values as the scalars. scalars is 'per vertex' or 1 # value for each corner of each triangle and should have shape (n, 3). plot = vpl.mesh_plot(mesh, scalars=mesh.z) # Optionally the plot created by mesh_plot can be passed to color_bar vpl.color_bar(plot, "Heights")
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
def create_figure(path_dict, figure_path, path_dict2=None, pair_mapping=None, transp_backg=False): assert ((path_dict2 is None) + (pair_mapping is None)) != 1, \ 'please specify all kwargs or none of them' if pair_mapping is not None: # for k in tqdm(pair_mapping): # mesh= Mesh.from_file(path_dict[k[0]]) # mesh2 = Mesh.from_file(path_dict2[k[1]]) for k, values in tqdm(pair_mapping.items()): mesh = Mesh.from_file(path_dict[k]) mesh = _add_normalizing_vector_point(mesh, 300, -300) fig = vpl.figure() fig.background_color = 'black' vpl.mesh_plot(mesh, color='pink', opacity=0.3) #make dendrite translucent for v in values: # add second, third,.. .stl to same plot mesh2 = Mesh.from_file(path_dict2[str(v)]) vpl.mesh_plot(mesh2) save_path = figure_path + str(k) + '.png' vpl.save_fig( save_path, magnification=5, off_screen=True, ) if transp_backg == True: #make black background transparent _transparent_background(save_path) fig.close() else: for k in tqdm(path_dict): # Read the STL using numpy-stl mesh = Mesh.from_file(path_dict[k]) if debug == True: mesh = _add_normalizing_vector_point(mesh, 300, -300) fig = vpl.figure() fig.background_color = 'black' vpl.mesh_plot(mesh) save_path = figure_path + str(k) + '.png' vpl.save_fig( save_path, magnification=5, off_screen=True, ) if transp_backg == True: #make black background transparent _transparent_background(save_path) fig.close()
def test(*spam): import vtkplotlib as vpl import numpy as np self = vpl.PolyData() vectors = vpl.mesh_plot(vpl.data.get_rabbit_stl(), fig=None).vectors points = vectors.reshape((-1, 3)) self.points = points polygons = np.arange(len(points)).reshape((-1, 3)) self.polygons = polygons point_colors = vpl.colors.normalise(points, axis=0) #[:, 0] self.point_colors = point_colors if not VTKPLOTLIB_WINDOWLESS_TEST: self.quick_show() else: self.to_plot(fig=None) self.polygons, self.lines = self.lines, self.polygons if not VTKPLOTLIB_WINDOWLESS_TEST: self.quick_show() else: self.to_plot(fig=None) del self.lines del self.polygons self.lines = self.polygons = polygons copy = self.copy() assert np.array_equal(self.points, copy.points) assert np.array_equal(self.polygons, copy.polygons) assert np.array_equal(self.lines, copy.lines) assert np.array_equal(self.point_colors, copy.point_colors) assert np.array_equal(self.polygon_colors, copy.polygon_colors) copy.points += [100, 0, 0] (self + copy).to_plot() repr(self) globals().update(locals())
def test_mesh(): import time fig = vpl.gcf() path = vpl.data.get_rabbit_stl() _mesh = numpy_stl().Mesh.from_file(path) mp = vpl.mesh_plot(_mesh.vectors) fig.show(False) for i in range(10): mp.tri_scalars = (_mesh.x[:, 0] + 3 * i) % 20 _mesh.rotate(np.ones(3), .1, np.mean(_mesh.vectors, (0, 1))) mp.vectors = _mesh.vectors fig.update() time.sleep(.01)
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 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 _mouse_move_cb(self, invoker, name): if self._click_location: self.pick.update() if self._clicks_are_equal(self._click_location, self.pick.point_2D): return self._click_location = None # Only calling the super event with the mouse button down (which rotates # the model for left click) when we are sure that this click is not # meant to place a marker reduces the slight jolt when you click on with # a sensitive mouse. Move the lines below to the top of this method to # see what I mean. if self._super_on_mouse_move: call_super_callback() if __name__ == "__main__": import vtkplotlib as vpl fig = vpl.QtFigure2() style = fig.style balls = vpl.quick_test_plot() rabbit = vpl.mesh_plot(vpl.data.get_rabbit_stl()) rabbit.vertices -= [i.mean() for i in vpl.unzip_axes(rabbit.vertices)] rabbit.vertices /= 5 text = vpl.text("text") vpl.show()
import vtkplotlib as vpl from stl.mesh import Mesh # Open an STL as before path = vpl.data.get_rabbit_stl() mesh = Mesh.from_file(path) # Plot it with the z values as the scalars. scalars is 'per vertex' or 1 # value for each corner of each triangle and should have shape (n, 3). plot = vpl.mesh_plot(mesh, scalars=mesh.z) # Optionally the plot created by mesh_plot can be passed to color_bar vpl.color_bar(plot, "Heights") vpl.show()
import vtkplotlib as vpl from stl.mesh import Mesh # Read the STL using numpy-stl mesh = Mesh.from_file('/home/javacasm/Descargas/Bearing_608_V02_tolMin.stl') # Plot the mesh vpl.mesh_plot(mesh) # Show the figure vpl.show()
import vtkplotlib as vpl from stl.mesh import Mesh import numpy as np # Open an STL as before path = vpl.data.get_rabbit_stl() mesh = Mesh.from_file(path) # `tri_scalars` must have one value per triangle and have shape (n,) or (n, 1). # Create some scalars showing "how upwards facing" each triangle is. tri_scalars = np.inner(mesh.units, np.array([0, 0, 1])) vpl.mesh_plot(mesh, tri_scalars=tri_scalars) vpl.show()
def test_scalar_bar(): plot = vpl.mesh_plot(vpl.data.get_rabbit_stl()) plot.scalars = (plot.vertices / 5) % 1 self = vpl.scalar_bar(plot, "criss-crossy pattern")
import vtkplotlib as vpl from stl.mesh import Mesh path = "V_HULL.stl" # Read the STL using numpy-stl mesh = Mesh.from_file(path) # Plot the mesh vpl.mesh_plot(mesh, color='red') # Show the figure vpl.show()
def test_legend(): import vtkplotlib as vpl self = vpl.legend(None, fig=None) assert self.fig is None assert self.length == 0 vpl.gcf().add_plot(self) self.set_entry(label="Blue Square", color="blue") sphere = vpl.scatter([0, 5, 10], color="g", fig=None, label="Ball") self.set_entry(sphere, color="b") self.set_entry( sphere, "Green ball", ) rabbit = vpl.mesh_plot(vpl.data.get_rabbit_stl()) self.set_entry(rabbit, "rabbit") rabbit_wire = vpl.plot(rabbit.vectors, color=rabbit.vectors[:, :, 0], label="octopus") self.set_entry(rabbit_wire) assert self.legend.GetEntryString(self.length - 1) == "octopus" # self.set_entry(vpl.quiver(np.zeros(3), np.array([-1, 0, 1])), "right") self.set_entry(None, label="shark", icon=vpl.data.ICONS["Right"]) for size in ((.3, .4), (.3, .4, 0)): self.size = size assert np.array_equal(self.size, [.3, .4, 0]) position = np.array(1) - self.size self.position = position assert np.array_equal(self.position, position) with pytest.raises(TypeError): self.set_entry(object()) length = self.length for i in range(2): eggs = vpl.text3d("eggs", label="eggs") self.add_plots([eggs]) # Re-adding labels shouldn't cause the legend to grow assert self.length == length + 1 vpl.text("text") auto_legend = vpl.legend(position=(0, 0)) auto_legend_no_label = vpl.legend(position=(0, .7), allow_no_label=True, allow_non_polydata_plots=True, color=(.2, .3, .4, .5)) assert auto_legend_no_label.color == (.2, .3, .4) assert auto_legend_no_label.opacity == .5 self.set_entry(vpl.scatter(np.arange(12).reshape((-1, 3)), label="scatter"), label="fish", index=self.length + 3) self.add_plots(vpl.gcf().plots)