From 415e3c9daa37a46b0af450996c22857f2ffacc30 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Wed, 6 Dec 2023 17:40:38 -0500 Subject: [PATCH] Add initial support for using colormaps in scatter export. --- glue_ar/export.py | 24 +++++++++++++++++------- glue_ar/scatter.py | 24 ++++++++++++++++++++---- glue_ar/tools.py | 7 +++---- 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/glue_ar/export.py b/glue_ar/export.py index 8aa9388..08f88bf 100644 --- a/glue_ar/export.py +++ b/glue_ar/export.py @@ -1,4 +1,5 @@ -from os.path import extsep, splitext +from os import remove +from os.path import splitext import pyvista as pv from gltflib import GLTF @@ -34,14 +35,23 @@ def export_gl_by_extension(exporter, filepath): # We want alphaMode as BLEND # see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#alpha-coverage def export_gl(plotter, filepath, with_alpha=True): - export_gl_by_extension(plotter, filepath) + path, ext = splitext(filepath) + gltf_path = filepath + glb = ext == ".glb" + if glb: + gltf_path = path + ".gltf" - if with_alpha: - gl = GLTF.load(filepath) - for material in gl.model.materials: - material.alphaMode = "BLEND" + plotter.export_gltf(gltf_path) + + if glb or with_alpha: + gl = GLTF.load(gltf_path) + if with_alpha: + for material in gl.model.materials: + material.alphaMode = "BLEND" export_gl_by_extension(gl, filepath) - + # if glb: + # remove(gltf_path) + def export_modelviewer(output_path, gltf_path, alt_text): html = f""" diff --git a/glue_ar/scatter.py b/glue_ar/scatter.py index add2629..97d419f 100644 --- a/glue_ar/scatter.py +++ b/glue_ar/scatter.py @@ -35,10 +35,26 @@ def scatter_layer_as_glyphs(viewer_state, layer_state, glyph): def scatter_layer_as_multiblock(viewer_state, layer_state): data = xyz_for_layer(viewer_state, layer_state, scaled=True) - spheres = [pv.Sphere(center=p, radius=layer_state.size_scaling * layer_state.size / 600, phi_resolution=8, theta_resolution=8) for p in data] + theta_resolution = 8 + phi_resolution = 8 + spheres = [pv.Sphere(center=p, radius=layer_state.size_scaling * layer_state.size / 600, phi_resolution=phi_resolution, theta_resolution=theta_resolution) for p in data] blocks = pv.MultiBlock(spheres) - return { - "data": blocks.extract_geometry(), - "color": layer_color(layer_state), + geometry = blocks.extract_geometry() + info = { + "data": geometry, "opacity": layer_state.alpha } + if layer_state.color_mode == "Fixed": + info["color"] = layer_color(layer_state) + else: + sphere_cells = 2 * (phi_resolution - 2) * theta_resolution # The number of cells on each sphere + sphere_points = 2 + (phi_resolution - 2) * theta_resolution # The number of points on each sphere + cmap_values = layer_state.layer[layer_state.cmap_attribute] + # cell_cmap_values = [y for x in cmap_values for y in (x,) * sphere_cells] + point_cmap_values = [y for x in cmap_values for y in (x,) * sphere_points] + # geometry.cell_data["colors"] = cell_cmap_values + geometry.point_data["colors"] = point_cmap_values + info["cmap"] = layer_state.cmap.name # This assumes that we're using a matplotlib colormap + info["clim"] = [layer_state.cmap_vmin, layer_state.cmap_vmax] + info["scalars"] = "colors" + return info diff --git a/glue_ar/tools.py b/glue_ar/tools.py index b720a20..a1ff468 100644 --- a/glue_ar/tools.py +++ b/glue_ar/tools.py @@ -31,10 +31,10 @@ def activate(self): # output_filename = "test.obj" # plotter.export_obj(output_filename) - output_filename = "test.glb" + output_filename = "scatter.glb" export_gl(plotter, output_filename, with_alpha=True) - export_modelviewer("test.html", output_filename, "Testing visualization") + export_modelviewer("scatter.html", output_filename, "Testing visualization") @viewer_tool @@ -46,10 +46,9 @@ class GLVolumeExportTool(Tool): def activate(self): plotter = pv.Plotter() - meshes = create_meshes(self.viewer.state, use_gaussian_filter=True, smoothing_iteration_count=5) + meshes = create_meshes(self.viewer.state, use_gaussian_filter=True, smoothing_iteration_count=10) for data in meshes.values(): mesh = data.pop("mesh") plotter.add_mesh(mesh, color=data["color"], opacity=data["opacity"]) - plotter.export_obj("volume.obj") export_gl(plotter, "volume.gltf", with_alpha=True) # Do we want alpha for volume renderings? export_modelviewer("volume.html", "volume.gltf", "Testing visualization")