def ghostMaterial(self, origMaterial, solidColor): name = self._mangledNameForMaterial(True, origMaterial) if name not in self.materials: args = {'transparent': True, 'opacity': 0.25} args.update( self._colorTexArgs( *self._extractMaterialDescriptors(origMaterial), solidColor)) if (self.isLineMesh): self.materials[name] = pythreejs.LineBasicMaterial( **args, **self.commonArgs) elif (self.isPointCloud): self.materials[name] = pythreejs.PointsMaterial( **args, **self.commonArgs, size=5, sizeAttenuation=False) else: self.materials[name] = pythreejs.MeshLambertMaterial( **args, **self.commonArgs) else: # Update the existing ghost material's color (if a solid color is used) useVertexColors, textureMapDataTex = self._extractMaterialDescriptors( origMaterial) if (useVertexColors == False) and (textureMapDataTex is None): self.materials[name].color = solidColor return self.materials[name]
def get_pointcloud_pythreejs(xyz, colors): points_geometry = pythreejs.BufferGeometry(attributes=dict( position=pythreejs.BufferAttribute(xyz, normalized=False), color=pythreejs.BufferAttribute(list(map(tuple, colors))))) points_material = pythreejs.PointsMaterial(vertexColors='VertexColors') return pythreejs.Points(geometry=points_geometry, material=points_material)
def scatter_children(points, color="red", size=4): scene_children = [] geometry_point = p3js.BufferGeometry( attributes={"position": p3js.BufferAttribute(array=points)}) material_point = p3js.PointsMaterial(color=color, size=size) pts = p3js.Points(geometry_point, material_point) scene_children.append(pts) return scene_children
def display_path(th_scene, vs, **kwargs): """Display path.""" geom = three.Geometry(vertices=vs) mat_line = three.LineBasicMaterial(**kwargs) line = three.Line(geometry=geom, material=mat_line) th_scene.add(line) mat_points = three.PointsMaterial(**kwargs) points = three.Points(geometry=geom, material=mat_points) th_scene.add(points)
def add_points(self, points, c=None, shading={}, obj=None, **kwargs): shading.update(kwargs) if len(points.shape) == 1: if len(points) == 2: points = np.array([[points[0], points[1], 0]]) else: if points.shape[1] == 2: points = np.append(points, np.zeros([points.shape[0], 1]), 1) sh = self.__get_shading(shading) points = points.astype("float32", copy=False) mi = np.min(points, axis=0) ma = np.max(points, axis=0) g_attributes = { "position": p3s.BufferAttribute(points, normalized=False) } m_attributes = {"size": sh["point_size"]} if sh["point_shape"] == "circle": # Plot circles tex = p3s.DataTexture(data=gen_circle(16, 16), format="RGBAFormat", type="FloatType") m_attributes["map"] = tex m_attributes["alphaTest"] = 0.5 m_attributes["transparency"] = True else: # Plot squares pass colors, v_colors = self.__get_point_colors(points, c, sh) if v_colors: # Colors per point m_attributes["vertexColors"] = 'VertexColors' g_attributes["color"] = p3s.BufferAttribute(colors, normalized=False) else: # Colors for all points m_attributes["color"] = colors material = p3s.PointsMaterial(**m_attributes) geometry = p3s.BufferGeometry(attributes=g_attributes) points = p3s.Points(geometry=geometry, material=material) point_obj = { "geometry": geometry, "mesh": points, "material": material, "max": ma, "min": mi, "type": "Points", "wireframe": None } if obj: return self.__add_object(point_obj, obj), point_obj else: return self.__add_object(point_obj)
def material(self, useVertexColors, textureMapDataTex=None): name = self._mangledMaterialName(False, useVertexColors, textureMapDataTex) if name not in self.materials: if (self.isLineMesh): args = self._colorTexArgs(useVertexColors, textureMapDataTex, 'black') self.materials[name] = pythreejs.LineBasicMaterial( **args, **self.commonArgs) elif (self.isPointCloud): args = self._colorTexArgs(useVertexColors, textureMapDataTex, 'black') self.materials[name] = pythreejs.PointsMaterial( **args, **self.commonArgs, size=5, sizeAttenuation=False) else: args = self._colorTexArgs(useVertexColors, textureMapDataTex, 'lightgray') self.materials[name] = pythreejs.MeshLambertMaterial( **args, **self.commonArgs) return self.materials[name]
def to_tjs_points(dataset, mapper, prop): """Extract the points from a dataset and return a buffered geometry.""" attr = { 'position': array_to_float_buffer(dataset.points), } coloring = get_coloring(mapper, dataset) if coloring == 'VertexColors': colors = map_scalars(mapper, dataset.point_data.active_scalars) attr['color'] = array_to_float_buffer(colors) geo = tjs.BufferGeometry(attributes=attr) m_attr = { 'color': color_to_hex(prop.GetColor()), 'size': prop.GetPointSize() / 100, 'vertexColors': coloring, } point_mat = tjs.PointsMaterial(**m_attr) return tjs.Points(geo, point_mat)
def allocatePointsMaterial(self): return pythreejs.PointsMaterial(color='black', size=5, sizeAttenuation=False)
def __initialize_pointcloud(self): points_material = three.PointsMaterial(color='#ff0000' if self.color is None else self.color, size=self.size) points = self.__get_points_from_pointcloud() return three.Points(points, material = points_material)
def plot(self, backend="pythreejs", width=800, height=500, background="black", mesh=False, use_as_color=["red", "green", "blue"], cmap="hsv", return_scene=False, output_name="pyntcloud_plot", polylines={}): """Visualize a PyntCloud using different backends. Parameters ---------- backend: {"pythreejs", "threejs"}, optional Default: "pythreejs" Used to select one of the available libraries for plotting. width: int, optional Default: 800 height: int, optional Default: 500 background: str, optional Default: "black" Used to select the default color of the background. In some backends, i.e "pythreejs" the background can be dynamically changed. use_as_color: str or ["red", "green", "blue"], optional Default: ["red", "green", "blue"] Indicates which scalar fields will be used to colorize the rendered point cloud. cmap: str, optional Default: "hsv" Color map that will be used to convert a single scalar field into rgb. Check matplotlib cmaps. return_scene: bool, optional Default: False. Used with "pythreejs" backend in order to return the pythreejs.Scene object polylines: dict, optional Default {}. Mapping hexadecimal colors to a list of list(len(3)) representing the points of the polyline. Example: polylines={ "0xFFFFFF": [[0, 0, 0], [0, 0, 1]], "0xFF00FF": [[1, 0, 0], [1, 0, 1], [1, 1, 1]] } Returns ------- pythreejs.Scene if return_scene else None """ try: colors = self.points[use_as_color].values except KeyError: colors = None if use_as_color != ["red", "green", "blue"] and colors is not None: import matplotlib.pyplot as plt s_m = plt.cm.ScalarMappable(cmap=cmap) colors = s_m.to_rgba(colors)[:, :-1] * 255 elif colors is None: # default color orange colors = np.repeat([[255, 125, 0]], self.xyz.shape[0], axis=0) colors = colors.astype(np.uint8) ptp = self.xyz.ptp() if backend == "pythreejs": import ipywidgets import pythreejs from IPython.display import display if mesh: raise NotImplementedError( "Plotting mesh geometry with pythreejs backend is not supported yet." ) if polylines: raise NotImplementedError( "Plotting polylines with pythreejs backend is not supported yet." ) points_geometry = pythreejs.BufferGeometry(attributes=dict( position=pythreejs.BufferAttribute(self.xyz, normalized=False), color=pythreejs.BufferAttribute(list(map(tuple, colors))))) points_material = pythreejs.PointsMaterial( vertexColors='VertexColors') points = pythreejs.Points(geometry=points_geometry, material=points_material, position=tuple(self.centroid)) camera = pythreejs.PerspectiveCamera( fov=90, aspect=width / height, position=tuple( self.centroid + [0, abs(self.xyz.max(0)[1]), abs(self.xyz.max(0)[2]) * 2]), up=[0, 0, 1]) orbit_control = pythreejs.OrbitControls(controlling=camera) orbit_control.target = tuple(self.centroid) camera.lookAt(tuple(self.centroid)) scene = pythreejs.Scene(children=[points, camera], background=background) renderer = pythreejs.Renderer(scene=scene, camera=camera, controls=[orbit_control], width=width, height=height) display(renderer) size = ipywidgets.FloatSlider(min=0., max=(ptp / 100), step=(ptp / 1000)) ipywidgets.jslink((size, 'value'), (points_material, 'size')) color = ipywidgets.ColorPicker() ipywidgets.jslink((color, 'value'), (scene, 'background')) display( ipywidgets.HBox(children=[ ipywidgets.Label('Background color:'), color, ipywidgets.Label('Point size:'), size ])) return scene if return_scene else None elif backend == "threejs": points = pd.DataFrame(self.xyz, columns=["x", "y", "z"]) for n, i in enumerate(["red", "green", "blue"]): points[i] = colors[:, n] new_PyntCloud = PyntCloud(points) if mesh and self.mesh is not None: new_PyntCloud.mesh = self.mesh[["v1", "v2", "v3"]] return plot_PyntCloud(new_PyntCloud, IFrame_shape=(width, height), point_size=ptp / 100, point_opacity=0.9, output_name=output_name, polylines=polylines) else: raise NotImplementedError( "{} backend is not supported".format(backend))