Skip to content

pilot: Add offscreen 2D world renderer and Python binding (issue #754 PR2)#900

Draft
tigercosmos wants to merge 2 commits into
solvcon:masterfrom
tigercosmos:worktree-paint_world_2d_pr2
Draft

pilot: Add offscreen 2D world renderer and Python binding (issue #754 PR2)#900
tigercosmos wants to merge 2 commits into
solvcon:masterfrom
tigercosmos:worktree-paint_world_2d_pr2

Conversation

@tigercosmos

@tigercosmos tigercosmos commented Jun 15, 2026

Copy link
Copy Markdown
Member

!! BLOCK BY #899 !!

This builds on PR1's shared paint_world_2d routine to add an offscreen renderer that needs no widget and no running event loop, so a World can be turned into an image headlessly. The new C++ free function render_world_2d (in RPainter2d) fills the canvas background and draws the world geometry into a QImage; wrap_pilot.cpp exposes it to Python as pilot.render_world_2d, which returns PNG-encoded bytes, and pilot.save_world_2d, which writes a file whose format follows the filename suffix. Both raise ValueError on a None world or a non-positive size. This is the headless capture path the deterministic corpus and the PR3 pixel-test gate need, mirroring R3DWidget's saveImage/clipImage but for the strictly-2D widget.

This is PR2 of the issue-#754 stack and depends on PR1 (#899). Until PR1 merges to master, this PR's diff also includes PR1's commit (the paint_world_2d extraction); the relevant change here is the latest commit, "Add offscreen 2D world renderer and Python binding".

To use it, build a World, frame it with a 2D view transform, and ask for the image. The render functions paint with QPainter, so run them through the pilot binary, which provides the QApplication (plain python3 has none):

import modmesh
from modmesh import pilot

world = modmesh.WorldFp64()
world.add_circle(0.0, 0.0, 1.0)
world.add_rectangle(-2.0, -2.0, 2.0, 2.0)

view = modmesh.ViewTransform2dFp64()
view.pan(400.0, 300.0)   # world origin at the image center (width/2, height/2)
view.zoom = 60.0         # pixels per world unit

png_bytes = pilot.render_world_2d(world, view, 800, 600)   # -> PNG bytes
pilot.save_world_2d(world, view, 800, 600, "/tmp/world.png")
env PYTHONPATH=$(pwd) \
  ./build/rel<pyvminor>/cpp/binary/pilot/pilot.app/Contents/MacOS/pilot \
  --mode=python demo.py

The tests in test_pilot_2d.py render a known world headless and assert the result is a valid PNG of the requested size, that a world with geometry does not encode identically to an empty one (something is actually painted), that output is byte-stable across runs, and that the None-world and non-positive-size error paths raise. The endpoint-to-pixel mapping assertions remain PR3's gate.

Related to #754.

…PR1)

Move the body of R2DWidget::paintWorld into a free paint_world_2d routine in new RPainter2d.{hpp,cpp} so the on-screen widget and a future offscreen image renderer share one code path. Pure refactor; pixels unchanged.
…con#754 PR2)

Add render_world_2d, a free routine that paints a World into an offscreen QImage through the PR1 paint_world_2d routine with no widget or event loop, and expose it to Python via wrap_pilot.cpp as pilot.render_world_2d (PNG bytes) and pilot.save_world_2d (to a file). Wire both through the _pilot_core bridge. Tests render a known world headless and check the PNG, size, that geometry changes pixels, determinism, and error paths.
@yungyuc yungyuc added pilot GUI and visualization geometry Geometry entities and manipulation labels Jun 16, 2026

@yungyuc yungyuc 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.

Leave inline annotation to indicate the parts that is not included in PR #899 and supposed to be reviewed.

@tigercosmos tigercosmos marked this pull request as draft June 16, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

geometry Geometry entities and manipulation pilot GUI and visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants