Skip to content

Add cglm and datoviz recipes - #3419

Draft
T-Dynamos wants to merge 11 commits into
kivy:developfrom
T-Dynamos:add-datoviz-cglm-recipes
Draft

T-Dynamos wants to merge 11 commits into
kivy:developfrom
T-Dynamos:add-datoviz-cglm-recipes

Conversation

@T-Dynamos

Copy link
Copy Markdown
Member

Adds recipes for cglm (C math library, libcglm.so) and datoviz (Vulkan-based scientific visualization library using PyProjectRecipe + CMake/NDK toolchain).

Tested on arm64-v8a (Android API 24+, NDK r28c, Python 3.14) with an offscreen/Vulkan 3D scene rendering on device.

@coveralls

coveralls commented Nov 27, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 63.68% (+0.006%) from 63.674% — T-Dynamos:add-datoviz-cglm-recipes into kivy:develop

@T-Dynamos
T-Dynamos marked this pull request as draft November 27, 2026 13:24
@T-Dynamos

T-Dynamos commented Nov 27, 2026 •

Copy link
Copy Markdown
Member Author

Minimal example using datoviz + Kivy for real-time 3D rendering (offscreen Vulkan view blitted into a Kivy Texture):

import ctypes
import math
import time
import numpy as np

import datoviz as dvz
import datoviz.raw as dvz_raw
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics import Rectangle
from kivy.graphics.texture import Texture
from kivy.uix.widget import Widget


class Datoviz3DWidget(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._scene = dvz_raw.dvz_scene()
        self._figure = dvz_raw.dvz_figure(self._scene, 800, 600, 0)
        self._panel = dvz_raw.dvz_panel_full(self._figure)
        self._app = dvz_raw.dvz_app(self._scene)
        self._view = self._tex = None
        self._last_size = (0, 0)
        self._t0 = time.perf_counter()

        # 3D wave surface mesh
        n = 32
        xs = np.linspace(-1.5, 1.5, n, dtype=np.float32)
        gx, gz = np.meshgrid(xs, xs)
        r = np.sqrt(gx * gx + gz * gz)
        gy = 0.45 * np.cos(2.5 * r) * np.exp(-0.35 * r)
        pos = np.column_stack([gx.ravel(), gy.ravel(), gz.ravel()]).astype(np.float32)
        h = (gy.ravel() - gy.min()) / max(1e-6, gy.max() - gy.min())
        colors = np.column_stack([40 + 200 * h, 180 - 80 * np.abs(h - 0.5), 240 - 190 * h, np.full_like(h, 255)]).astype(np.uint8)
        idx = [
            v
            for i in range(n - 1)
            for j in range(n - 1)
            for k in (i * n + j,)
            for v in (k, k + 1, k + n, k + 1, k + n + 1, k + n)
        ]

        mesh = dvz_raw.dvz_mesh(self._scene, 0)
        dvz.dvz_visual_set_data_many(mesh, {"position": pos, "color": colors})
        dvz.dvz_visual_set_index_data(mesh, np.array(idx, dtype=np.uint32))
        dvz_raw.dvz_panel_add_visual(self._panel, mesh, None)

        cam = dvz_raw.dvz_camera_desc()
        cam.projection.fov_y = 0.65
        cam.projection.near_clip, cam.projection.far_clip = 0.05, 50.0
        dvz_raw.dvz_panel_set_camera_desc(self._panel, ctypes.byref(cam))
        self._cam = dvz_raw.dvz_panel_camera(self._panel)

        with self.canvas:
            self._rect = Rectangle(pos=self.pos, size=self.size)
        self.bind(
            pos=lambda *_: setattr(self._rect, "pos", self.pos),
            size=lambda *_: setattr(self._rect, "size", self.size),
        )
        Clock.schedule_interval(self._render, 0.0)

    def _render(self, _dt):
        w, h = max(1, round(self.width)), max(1, round(self.height))
        if w <= 1 or h <= 1:
            return
        if self._view is None:
            self._view = dvz_raw.dvz_view_offscreen(self._app, self._figure, w, h)
            self._last_size = (w, h)
        elif (w, h) != self._last_size:
            dvz_raw.dvz_view_resize(self._view, w, h)
            self._last_size = (w, h)

        t = time.perf_counter() - self._t0
        view = dvz_raw.dvz_camera_view()
        view.eye[:] = (3.2 * math.cos(t), 1.9, 3.2 * math.sin(t))
        view.target[:] = (0.0, 0.0, 0.0)
        view.up[:] = (0.0, 1.0, 0.0)
        dvz_raw.dvz_camera_set_view(self._cam, view)

        dvz_raw.dvz_app_run(self._app, 1)
        rgba = dvz.dvz_view_capture_rgba(self._view)
        ch, cw = rgba.shape[:2]
        if self._tex is None or self._tex.size != (cw, ch):
            self._tex = Texture.create(size=(cw, ch), colorfmt="rgba")
            self._tex.flip_vertical()
            self._rect.texture = self._tex
        self._tex.blit_buffer(memoryview(rgba).cast("B"), colorfmt="rgba", bufferfmt="ubyte")
        self.canvas.ask_update()


class Datoviz3DApp(App):
    def build(self):
        return Datoviz3DWidget()

    def on_stop(self):
        Clock.unschedule(self.root._render)
        dvz_raw.dvz_app_destroy(self.root._app)
        dvz_raw.dvz_scene_destroy(self.root._scene)


if __name__ == "__main__":
    Datoviz3DApp().run()
datoviz_kivy_3d

@T-Dynamos
T-Dynamos marked this pull request as ready for review November 27, 2026 19:50

@AndreMiras AndreMiras left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, jus concerned about the url "git+" format with a hash rather than a tag, that may fail as soon as datoviz have a new commit. Maybe use the archive format instead

Comment on lines +15 to +16
version = "7e1ba52513ea07f2c3a8c13b0979f1ccdb04bb11"
url = "git+https://github.com/datoviz/datoviz.git"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p4a uses git fetch --tags --depth 1 and this may break as soon as datoviz upstream commits on top of main.
Maybe use archive/<sha>.tar.gz instead, e.g.

url = "https://github.com/datoviz/datoviz/archive/{version}.tar.gz"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datoviz uses a lot of submodules we can't use source tar, I will request them to tag a new release.

@T-Dynamos
T-Dynamos marked this pull request as draft November 28, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants