Skip to content

Commit

Permalink
Add initial support for using colormaps in scatter export.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Dec 6, 2023
1 parent 057e32f commit 415e3c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
24 changes: 17 additions & 7 deletions glue_ar/export.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"""
Expand Down
24 changes: 20 additions & 4 deletions glue_ar/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions glue_ar/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

0 comments on commit 415e3c9

Please sign in to comment.