Skip to content

pilot: Extract shared paint_world_2d render routine (issue #754 PR1)#899

Draft
tigercosmos wants to merge 1 commit into
solvcon:masterfrom
tigercosmos:worktree-paint_world_2d_pr1
Draft

pilot: Extract shared paint_world_2d render routine (issue #754 PR1)#899
tigercosmos wants to merge 1 commit into
solvcon:masterfrom
tigercosmos:worktree-paint_world_2d_pr1

Conversation

@tigercosmos

Copy link
Copy Markdown
Member

The 2D world-drawing logic currently lives inside R2DWidget::paintWorld, where it closes over the widget's this and m_view. The offscreen QImage renderer planned next (PR2) needs that same drawing without an event loop or a widget instance, so this PR extracts the body verbatim into a free function paint_world_2d(QPainter&, WorldFp64 const&, ViewTransform2dFp64 const&) in new RPainter2d.{hpp,cpp}. R2DWidget::paintEvent now guards on m_world and calls the shared routine, which keeps the on-screen path and the future offscreen path from drifting apart. The geometry color and cosmetic line/point widths move with the code into RPainter2d's anonymous namespace. This is a pure refactor: paintEvent still draws the background, grid, axes, and origin marker itself and invokes the world geometry at the same point in the same order, so the rendered pixels are unchanged.

To let the shared routine take the world by const reference, World::points() is made const. It already returned a const reference to the shared pointer, so this only widens where it can be called and breaks no existing caller.

Built the pilot, ran the pilot Python tests (test_pilot.py and test_pilot_2d.py both green), and ran the C++ lint set (cformat, cinclude, checkascii, checktws) plus flake8 -- all clean. The two test docstrings in test_pilot_2d.py that referenced the old paintWorld name are updated to paint_world_2d.

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.

@tigercosmos tigercosmos left a comment

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.

@yungyuc Please take a look, thanks!


} // unnamed namespace

void paint_world_2d(QPainter & painter, WorldFp64 const & world, ViewTransform2dFp64 const & view)

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.

Refactor the function to here. QPainter can take the argument from R2DWidget or future QImage

Comment on lines +198 to +200
{
paint_world_2d(painter, *m_world, m_view);
}

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.

paint_world_2d is called here.

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

LGTM. If you do not want to enhance the architecture, I can just merge. Let me know what I should do.

set(MODMESH_PILOT_PYMODHEADERS
${CMAKE_CURRENT_SOURCE_DIR}/R3DWidget.hpp
${CMAKE_CURRENT_SOURCE_DIR}/R2DWidget.hpp
${CMAKE_CURRENT_SOURCE_DIR}/RPainter2d.hpp

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.

I like the namd RPainter2d. The names of R3DWidget and R2DWidget should follow. But "widget" is not informative. Do not rename hastily. Let's give it more thoughts.

The only complain I have with "painter" is that it sounds like a pixel facility, like the Windows legacy suggested. But it's not a big deal. Just to share an instinctive feeling.

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.

I changed to RWorldRenderer2d.

constexpr double GEOMETRY_LINE_WIDTH_PX = 1.5;
constexpr int GEOMETRY_POINT_WIDTH_PX = 5;

} // unnamed namespace

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.

It's called anonymous namespace. "Unnamed" is unconventional.

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.

Got it.

void paint_world_2d(QPainter & painter, WorldFp64 const & world, ViewTransform2dFp64 const & view)
{
// Map math-convention world (x, y) to Qt screen pixels; z is dropped.
auto map = [&view](double world_x, double world_y)

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.

When a lambda/closure is used but not for a functional purpose, it is a sign that the enclosing code should be made as a class and the lambda should be a member function.

But a function like this size (paint_world_2d) is short enough and readable. It's OK to write code like this.

A more extensible design is to make it a class now. By holding WorldFp64 & as a required member datum, the architecture explains itself.

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.

Sure, let's make it a class directly.

@yungyuc yungyuc added the pilot GUI and visualization label Jun 16, 2026
@yungyuc yungyuc moved this from Todo to In Progress in pilot 2D drawing basic GUI Jun 16, 2026
@yungyuc yungyuc added the geometry Geometry entities and manipulation label Jun 16, 2026
@tigercosmos tigercosmos marked this pull request as draft June 16, 2026 09:44
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

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants