Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions examples/volume-3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Package: Graphic Server Protocol / Matplotlib
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause
"""
From https://datoviz.org/gallery/visuals/volume/
"""

# Experiment to handle intellisense in VSCode
# from gsp.core.types import Color
import matplotlib.pyplot as plt
import numpy as np
import common.asset_downloader as asset_downloader
from gsp_matplotlib import glm
from common.launcher import parse_args

############################
# Download/read the volume data
#
import common.asset_downloader as asset_downloader
import gzip

# Parse command line arguments
core, visual, render = parse_args()

####################################################
# Download/read the volume data
#

volume_path = asset_downloader.download_data("volumes/allen_mouse_brain_rgba.npy.gz")
print(f"Loaded point cloud data from {volume_path}")
with gzip.open(volume_path, "rb") as f:
volume_data = np.load(f, allow_pickle=True)

# Normalize volume_data colors from [0, 255] to [0, 1]
volume_data = volume_data / 255.0

####################################################
# Create canvas+viewport for the GSP scene
#
canvas = core.Canvas(width=512, height=512, dpi=250.0)
viewport = core.Viewport(canvas=canvas, x=0, y=0, width=512, height=512, color=(0, 0, 0, 1))

######################################################
# Create a texture from the volume data
#

texture_3d = core.Texture(volume_data, volume_data.shape)

#####################################################
# Create a volume from the texture
#

bound_x = (-1, 1)
bound_y = (-1, 1)
bound_z = (-1, 1)
volume = visual.Volume(
texture_3d=texture_3d,
bounds_3d=(bound_x, bound_y, bound_z),
downsample_ratio=0.00005,
jitter_position_factor=0.000,
point_size=200.0,
)

############################

render(canvas, [viewport], [volume])
17 changes: 9 additions & 8 deletions gsp/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause

from . data import Data
from . list import List
from . buffer import Buffer
from . canvas import Canvas
from . viewport import Viewport
from . types import Color, Marker, Measure
from . types import Matrix, Vec2, Vec3, Vec4
from . types import LineCap, LineStyle, LineJoin
from .data import Data
from .list import List
from .buffer import Buffer
from .canvas import Canvas
from .viewport import Viewport
from .texture import Texture
from .types import Color, Marker, Measure
from .types import Matrix, Vec2, Vec3, Vec4
from .types import LineCap, LineStyle, LineJoin
30 changes: 30 additions & 0 deletions gsp/core/texture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Package: Graphic Server Protocol
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause
from __future__ import annotations

from gsp import Object
from gsp.io.command import command
import numpy as np


class Texture(Object):
"""
A texture is a rectangular two-dimensional image that can be
applied to a surface in 3D space.
"""

@command("core.Texture")
def __init__(self, texture_data: np.ndarray, shape: tuple):
"""
A texture is a rectangular two-dimensional image.

Parameters
----------

texture_data:
The image data of the texture.
shape:
The shape of the texture (height, width, channels).
"""
Object.__init__(self)
16 changes: 9 additions & 7 deletions gsp/visual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause

from . visual import Visual
from . pixels import Pixels
from . points import Points
from . markers import Markers
from . segments import Segments
from . paths import Paths
from . polygons import Polygons
from .visual import Visual
from .pixels import Pixels
from .points import Points
from .markers import Markers
from .segments import Segments
from .paths import Paths
from .polygons import Polygons

# from . image import Image
# from . mesh import Mesh
from .volume import Volume
43 changes: 43 additions & 0 deletions gsp/visual/volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Package: Graphic Server Protocol
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause

import numpy as np
from gsp.visual import Visual
from gsp.core import Buffer, Color
from gsp.transform import Transform
from gsp.io.command import command


class Volume(Visual):
"""
A volume is a three-dimensional shape composed of a set of voxels.
"""

@command("visual.Volume")
def __init__(
self,
positions: Transform | Buffer,
sizes: Transform | Buffer | float,
fill_colors: Transform | Buffer | Color,
):
super().__init__()

# These variables are available prior to rendering and may be
# tracked
self._in_variables = {
"positions": positions,
"fill_colors": fill_colors,
"sizes": sizes,
"viewport": None,
}

# These variables exists only during rendering and are
# available on server side only. We have thus to make
# sure they are not tracked.
n = len(positions)
self._out_variables = {
"screen[positions]": np.empty((n, 3), np.float32),
"fill_colors": np.empty((n, 4), np.float32),
"sizes": np.empty(n, np.float32),
}
14 changes: 8 additions & 6 deletions gsp_matplotlib/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause

#from . data import Data
from . list import List
from . buffer import Buffer
from . canvas import Canvas
from . viewport import Viewport
# from . data import Data
from .list import List
from .buffer import Buffer
from .canvas import Canvas
from .viewport import Viewport
from .texture import Texture
from gsp.core import Color, Marker, Measure
#from . types import LineCap, LineStyle, LineJoin

# from . types import LineCap, LineStyle, LineJoin
26 changes: 26 additions & 0 deletions gsp_matplotlib/core/texture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Package: Graphic Server Protocol / Matplotlib
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause
# from __future__ import annotations
import numpy as np
from gsp import core


class Texture(core.Texture):

__doc__ = core.Texture.__doc__

def __init__(self, texture_data: np.ndarray, shape: tuple):

super().__init__(texture_data=texture_data, shape=shape)

self._texture_data = texture_data.flatten()
self._shape = shape

@property
def data(self) -> np.ndarray:
return self._texture_data

@property
def shape(self) -> tuple:
return self._shape
14 changes: 8 additions & 6 deletions gsp_matplotlib/visual/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
# Authors: Nicolas P .Rougier <[email protected]>
# License: BSD 3 clause

from . pixels import Pixels
from . points import Points
from . markers import Markers
from . segments import Segments
from . paths import Paths
from . polygons import Polygons
from .pixels import Pixels
from .points import Points
from .markers import Markers
from .segments import Segments
from .paths import Paths
from .polygons import Polygons

# from . image import Image
# from . mesh import Mesh
from .volume import Volume
Loading