From 02e541ea294e5bcc49000576b19297837b6013ac Mon Sep 17 00:00:00 2001 From: "Ankur Sinha (Ankur Sinha Gmail)" Date: Wed, 21 Aug 2024 19:16:12 +0100 Subject: [PATCH] feat(vispy): remove no longer needed instanced meshes --- pyneuroml/plot/PlotMorphologyVispy.py | 134 -------------------------- 1 file changed, 134 deletions(-) diff --git a/pyneuroml/plot/PlotMorphologyVispy.py b/pyneuroml/plot/PlotMorphologyVispy.py index 934f711d..74f8fdaa 100644 --- a/pyneuroml/plot/PlotMorphologyVispy.py +++ b/pyneuroml/plot/PlotMorphologyVispy.py @@ -998,140 +998,6 @@ def plot_3D_cell_morphology( return meshdata -def create_instanced_meshes(meshdata, plot_type, current_view, min_width): - """Internal function to plot instanced meshes from mesh data. - - It is more efficient to collect all the segments that require the same - cylindrical mesh and to create instanced meshes for them. - - See: https://vispy.org/api/vispy.scene.visuals.html#vispy.scene.visuals.InstancedMesh - - :param meshdata: meshdata to plot: dictionary with: - key: (r1, r2, length) - value: [(prox, dist, color, offset)] - :param plot_type: type of plot - :type plot_type: str - :param current_view: vispy viewbox to use - :type current_view: ViewBox - :param min_width: minimum width of tubes - :type min_width: float - """ - total_mesh_instances = 0 - for d, i in meshdata.items(): - total_mesh_instances += len(i) - logger.debug( - f"Visualising {len(meshdata.keys())} meshes with {total_mesh_instances} instances" - ) - - pbar = progressbar.ProgressBar( - max_value=total_mesh_instances, - widgets=[progressbar.SimpleProgress(), progressbar.Bar(), progressbar.Timer()], - redirect_stdout=True, - ) - progress_ctr = 0 - for d, i in meshdata.items(): - r1 = float(d[0]) - r2 = float(d[1]) - length = float(d[2]) - - # actual plotting bits - if plot_type == "constant": - r1 = min_width - r2 = min_width - - if r1 < min_width: - r1 = min_width - if r2 < min_width: - r2 = min_width - - seg_mesh = None - # 1: for points, we set the prox/dist to None since they only have - # positions. - # 2: single compartment cells with r1, r2, and length 0 - # Note: we can't check if r1 == r2 == length because there - # may be cylinders with such a set of parameters - - if r1 == r2 and ((i[0][0] is None and i[0][1] is None) or (length == 0.0)): - seg_mesh = create_sphere(9, 9, radius=r1) - logger.debug(f"Created spherical mesh template with radius {r1}") - else: - rows = 2 + int(length / 2) - seg_mesh = create_cylindrical_mesh( - rows=rows, cols=9, radius=[r1, r2], length=length, closed=True - ) - logger.debug( - f"Created cylinderical mesh template with radii {r1}, {r2}, {length}" - ) - - instance_positions = [] - instance_transforms = [] - instance_colors = [] - - # if in a notebook, only update once per mesh, but not per mesh - # instance - if pynml_in_jupyter: - pbar.update(progress_ctr) - - for num, im in enumerate(i): - # if not in a notebook, update for each mesh instance - if not pynml_in_jupyter: - pbar.update(progress_ctr) - - progress_ctr += 1 - prox = im[0] - dist = im[1] - color = im[2] - offset = im[3] - - # points, spherical meshes - if prox is not None and dist is not None: - orig_vec = [0, 0, length] - dir_vector = [dist.x - prox.x, dist.y - prox.y, dist.z - prox.z] - k = numpy.cross(orig_vec, dir_vector) - mag_k = numpy.linalg.norm(k) - - if mag_k != 0.0: - k = k / mag_k - theta = math.acos( - numpy.dot(orig_vec, dir_vector) - / (numpy.linalg.norm(orig_vec) * numpy.linalg.norm(dir_vector)) - ) - logger.debug(f"k is {k}, theta is {theta}") - rot_matrix = rotate(math.degrees(theta), k).T - rot_obj = Rotation.from_matrix(rot_matrix[:3, :3]) - else: - logger.debug("k is [0..], using zeros for rotation matrix") - rot_matrix = numpy.zeros((3, 3)) - rot_obj = Rotation.from_matrix(rot_matrix) - - instance_positions.append( - [offset[0] + prox.x, offset[1] + prox.y, offset[2] + prox.z] - ) - else: - instance_positions.append(offset) - rot_matrix = numpy.zeros((3, 3)) - rot_obj = Rotation.from_matrix(rot_matrix) - - instance_transforms.append(rot_obj.as_matrix()) - instance_colors.append(color) - - assert len(instance_positions) == len(instance_transforms) - logger.debug( - f"Instanced: positions: {instance_positions}, transforms: {instance_transforms}" - ) - - mesh = InstancedMesh( - meshdata=seg_mesh, - instance_positions=instance_positions, - instance_transforms=instance_transforms, - instance_colors=instance_colors, - parent=current_view.scene, - ) - # TODO: add a shading filter for light? - assert mesh is not None - pbar.finish() - - def plot_3D_schematic( cell: Cell, segment_groups: typing.Optional[typing.List[SegmentGroup]],