def _add_nodes_labels_to_fig(self, pb, X_Ia): text_list = [] for I, X_a in enumerate(X_Ia): k3d_text = k3d.text('%g' % I, tuple(X_a), label_box=False, size=0.8, color=0x00FF00) pb.plot_fig += k3d_text text_list.append(k3d_text) pb.objects[self.K3D_NODES_LABELS] = text_list
def __get_joint_names(self) -> List[Text]: return [ k3d.text(text=self.joint_set.names[joint_index], position=self.joint_positions[joint_index], size=DEFAULT_TEXT_SIZE, label_box=False, color=DEFAULT_COLORS.get('black')) for joint_index in range(self.joint_set.number_of_joints) ]
def __get_joint_coordinates(self) -> List[Text]: return [ k3d.text(text=COORDINATE_FORMAT.format( self.joint_positions[joint_index][0], self.joint_positions[joint_index][1], self.joint_positions[joint_index][2]), position=self.joint_positions[joint_index], size=DEFAULT_TEXT_SIZE, label_box=False, color=DEFAULT_COLORS.get('black')) for joint_index in range(self.joint_set.number_of_joints) ]
def __get_joint_names_for_video(self) -> List[Text]: return [ k3d.text(text=self.joint_set.names[joint_index], position={ current_timestamp[0]: current_timestamp[1][joint_index] for current_timestamp in self.joint_positions.items() }, size=DEFAULT_TEXT_SIZE, label_box=False, color=DEFAULT_COLORS.get('black')) for joint_index in range(self.joint_set.number_of_joints) ]
def display_3d(K,show_labels=False): """display a 3d simplicial complex using k3d, in jupyter to install it: sudo pip install k3d sudo jupyter nbextension install --py k3d sudo jupyter nbextension enable --py k3d """ import k3d if not isinstance(K.vertices[0] ,Point): raise Exception("Can display only Point-class vertices of dim 3!!!") dim=K.vertices[0].dim if dim != 3: sys.stderr.write("Not yet implemented display in dim = %s\n" % dim ) return None vertices = [v.coords for v in K.vertices] edges = [ s for s in K.simplices[1] ] faces = [] if 2 in K.simplices: faces = [ s for s in K.simplices[2] ] plt=k3d.plot(name='points') plt_points = k3d.points( positions=vertices , point_size=0.05) plt_points.shader ='3dSpecular' plt_points.color = 14 plt += plt_points # now lines: for s in edges: plt_line = k3d.line([vertices[s[0]],vertices[s[1]] ],shader='mesh', width=0.01, color=0xff0000) plt += plt_line # now convert all faces to a mesh plt_mesh = k3d.mesh(vertices,faces,color=0xff, wireframe=False, opacity=0.7, name="Simplicial Complex") plt += plt_mesh # now add the text for P in K.vertices: if show_labels and P.label is not None: plt_text = k3d.text("%s" % repr(P), position = P.coords, color=0x7700,size=1) plt += plt_text #finally display plt.display()
def __get_joint_coordinates_for_video(self) -> List[Text]: return [ k3d.text(text={ current_timestamp[0]: COORDINATE_FORMAT.format(current_timestamp[1][joint_index][0], current_timestamp[1][joint_index][1], current_timestamp[1][joint_index][2]) for current_timestamp in self.joint_positions.items() }, position={ current_timestamp[0]: current_timestamp[1][joint_index] for current_timestamp in self.joint_positions.items() }, size=DEFAULT_TEXT_SIZE, label_box=False, color=DEFAULT_COLORS.get('black')) for joint_index in range(self.joint_set.number_of_joints) ]
def add_cell_to_pb(self, pb, X_Ia, I_Fi, obj_name): plot = pb.plot_fig wb_mesh = k3d.mesh( X_Ia.astype(np.float32), I_Fi.astype(np.uint32), # opacity=0.9, color=0x999999, side='double') rand_color = random.randint(0, 0xFFFFFF) plot += wb_mesh self.k3d_mesh[obj_name] = wb_mesh # wb_points = k3d.points(X_Ia.astype(np.float32), # color=0x999999, # point_size=100) # plot +=wb_points if self.show_node_labels: texts = [] for I, X_a in enumerate(X_Ia): k3d_text = k3d.text('%g' % I, tuple(X_a), label_box=False, size=0.8, color=rand_color) plot += k3d_text texts.append(k3d_text) self.k3d_labels[obj_name] = texts wb_mesh_wireframe = k3d.mesh(X_Ia.astype(np.float32), I_Fi.astype(np.uint32), color=0x000000, wireframe=True) plot += wb_mesh_wireframe self.k3d_wireframe[obj_name] = wb_mesh_wireframe
def __init__( self, data: Union[np.ndarray, geo.SpatialData], name: str, reference_system: str, plot: k3d.Plot = None, color: int = None, visualization_method: str = "auto", show_wireframe: bool = False, ): """Create a ``SpatialDataVisualizer`` instance. Parameters ---------- data : The data that should be visualized name : Name of the data reference_system : Name of the data's reference system plot : A k3d plotting widget. color : The RGB color of the coordinate system (affects trace and label) as a 24 bit integer value. visualization_method : The initial data visualization method. Options are ``point``, ``mesh``, ``both``and ``auto``. If ``auto`` is selected, a mesh will be drawn if triangle data is available and points if not. show_wireframe : If `True`, meshes will be drawn as wireframes """ if not isinstance(data, geo.SpatialData): data = geo.SpatialData(coordinates=data) colors = [] if color is None or isinstance(color, str): if isinstance(color, str): colors = data.attributes[color] color = RGB_GREY if data.triangles is not None: triangles = data.triangles.astype(np.uint32) else: triangles = None self._reference_system = reference_system self._label_pos = data.coordinates.mean(dim=data.additional_dims).data if isinstance(self._label_pos, Q_): self._label_pos = self._label_pos.to(_DEFAULT_LEN_UNIT).m self._label = None if name is not None: self._label = k3d.text( text=name, position=self._label_pos, reference_point="cc", color=color, size=0.5, label_box=True, name=name if name is None else f"{name} (text)", ) coordinates = data.coordinates.data if isinstance(coordinates, Q_): coordinates = coordinates.to(_DEFAULT_LEN_UNIT).m self._points = k3d.points( coordinates, point_size=0.05, color=color, name=name if name is None else f"{name} (points)", ) self._mesh = None if data.triangles is not None: self._mesh = k3d.mesh( coordinates.astype(np.float32).reshape(-1, 3), triangles, side="double", color=color, attribute=colors, color_map=k3d.colormaps.matplotlib_color_maps.Viridis, wireframe=show_wireframe, name=name if name is None else f"{name} (mesh)", ) self.set_visualization_method(visualization_method) if plot is not None: plot += self._points if self._mesh is not None: plot += self._mesh if self._label is not None: plot += self._label self.data = data
def __init__( self, lcs: LocalCoordinateSystem, plot: k3d.Plot = None, name: str = None, color: int = RGB_BLACK, show_origin=True, show_trace=True, show_vectors=True, vector_scale=2.5, ): """Create a `CoordinateSystemVisualizerK3D`. Parameters ---------- lcs : Coordinate system that should be visualized plot : A k3d plotting widget. name : Name of the coordinate system color : The RGB color of the coordinate system (affects trace and label) as a 24 bit integer value. show_origin : If `True`, the origin of the coordinate system will be highlighted in the color passed as another parameter show_trace : If `True`, the trace of a time dependent coordinate system will be visualized in the color passed as another parameter show_vectors : If `True`, the the coordinate axes of the coordinate system are visualized """ coordinates, orientation = _get_coordinates_and_orientation(lcs) self._lcs = lcs self._color = color self._vector_scale = vector_scale self._vectors = k3d.vectors( origins=[coordinates for _ in range(3)], vectors=orientation.transpose() * self._vector_scale, line_width=0.05, head_size=3.0, colors=[[RGB_RED, RGB_RED], [RGB_GREEN, RGB_GREEN], [RGB_BLUE, RGB_BLUE]], labels=[], label_size=1.5, name=name if name is None else f"{name} (vectors)", ) self._vectors.visible = show_vectors self._label = None if name is not None: self._label = k3d.text( text=name, position=coordinates + 0.05, color=self._color, size=1, label_box=False, name=name if name is None else f"{name} (text)", ) self._trace = k3d.line( _get_unitless_coordinates(lcs), # type: ignore shader="thick", width=0.1, # change with .set_trait("width", value) color=color, name=name if name is None else f"{name} (line)", ) self._trace.visible = show_trace self.origin = platonic.Octahedron(size=0.1).mesh self.origin.color = color self.origin.model_matrix = _create_model_matrix(coordinates, orientation) self.origin.visible = show_origin if plot is not None: plot += self._vectors plot += self._trace plot += self.origin if self._label is not None: plot += self._label
def __init__( self, data, name: str, cs_vis: CoordinateSystemVisualizerK3D, plot: k3d.Plot = None, color: int = RGB_BLACK, visualization_method: str = "auto", show_wireframe: bool = False, ): """Create a 'SpatialDataVisualizer' instance. Parameters ---------- data : numpy.ndarray or weldx.geometry.SpatialData The data that should be visualized name : str Name of the data cs_vis : CoordinateSystemVisualizerK3D An instance of the 'CoordinateSystemVisualizerK3D'. This serves as reference coordinate system for the data and is needed to calculate the correct position of the data plot : k3d.Plot A k3d plotting widget. color : int The RGB color of the coordinate system (affects trace and label) as a 24 bit integer value. visualization_method : str The initial data visualization method. Options are 'point', 'mesh', 'both' and 'auto'. If 'auto' is selected, a mesh will be drawn if triangle data is available and points if not. show_wireframe : bool If 'True', meshes will be drawn as wireframes """ triangles = None if isinstance(data, geo.SpatialData): triangles = data.triangles data = data.coordinates.data self._cs_vis = cs_vis self._label_pos = np.mean(data, axis=0) self._label = None if name is not None: self._label = k3d.text( text=name, position=self._label_pos, reference_point="cc", color=color, size=0.5, label_box=True, ) self._points = k3d.points(data, point_size=0.05, color=color) self._mesh = None if triangles is not None: self._mesh = k3d.mesh(data, triangles, side="double", color=color, wireframe=show_wireframe) self.update_model_matrix() self.set_visualization_method(visualization_method) if plot is not None: plot += self._points if self._mesh is not None: plot += self._mesh if self._label is not None: plot += self._label
def __init__( self, lcs, plot: k3d.Plot = None, name: str = None, color: int = RGB_BLACK, show_origin=True, show_trace=True, show_vectors=True, ): """Create a `CoordinateSystemVisualizerK3D`. Parameters ---------- lcs : weldx.transformations.LocalCoordinateSystem Coordinate system that should be visualized plot : k3d.Plot A k3d plotting widget. name : str Name of the coordinate system color : int The RGB color of the coordinate system (affects trace and label) as a 24 bit integer value. show_origin : bool If `True`, the origin of the coordinate system will be highlighted in the color passed as another parameter show_trace : If `True`, the trace of a time dependent coordinate system will be visualized in the color passed as another parameter show_vectors : bool If `True`, the the coordinate axes of the coordinate system are visualized """ coordinates, orientation = self._get_coordinates_and_orientation(lcs) self._lcs = lcs self._color = color self._vectors = k3d.vectors( origins=[coordinates for _ in range(3)], vectors=orientation.transpose(), colors=[[RGB_RED, RGB_RED], [RGB_GREEN, RGB_GREEN], [RGB_BLUE, RGB_BLUE]], labels=[], label_size=1.5, ) self._vectors.visible = show_vectors self._label = None if name is not None: self._label = k3d.text( text=name, position=coordinates + 0.05, color=self._color, size=1, label_box=False, ) self._trace = k3d.line( np.array(lcs.coordinates.values, dtype="float32"), shader="simple", width=0.05, color=color, ) self._trace.visible = show_trace self.origin = platonic.Octahedron(size=0.1).mesh self.origin.color = color self.origin.model_matrix = self._create_model_matrix( coordinates, orientation) self.origin.visible = show_origin if plot is not None: plot += self._vectors plot += self._trace plot += self.origin if self._label is not None: plot += self._label
def __init__( self, data: Union[np.ndarray, SpatialData], name: str, reference_system: str, plot: k3d.Plot = None, color: int = RGB_BLACK, visualization_method: str = "auto", show_wireframe: bool = False, ): """Create a ``SpatialDataVisualizer`` instance. Parameters ---------- data : The data that should be visualized name : Name of the data reference_system : Name of the data's reference system plot : A k3d plotting widget. color : The RGB color of the coordinate system (affects trace and label) as a 24 bit integer value. visualization_method : The initial data visualization method. Options are ``point``, ``mesh``, ``both``and ``auto``. If ``auto`` is selected, a mesh will be drawn if triangle data is available and points if not. show_wireframe : If `True`, meshes will be drawn as wireframes """ triangles = None if isinstance(data, geo.SpatialData): triangles = data.triangles data = data.coordinates.data self._reference_system = reference_system self._label_pos = np.mean(data, axis=0) self._label = None if name is not None: self._label = k3d.text( text=name, position=self._label_pos, reference_point="cc", color=color, size=0.5, label_box=True, ) self._points = k3d.points(data, point_size=0.05, color=color) self._mesh = None if triangles is not None: self._mesh = k3d.mesh(data, triangles, side="double", color=color, wireframe=show_wireframe) self.set_visualization_method(visualization_method) if plot is not None: plot += self._points if self._mesh is not None: plot += self._mesh if self._label is not None: plot += self._label