Skip to content

Report OpenSCAD callout anchors as pixel positions for diagram callouts - #112

Merged
Malcolmnixon merged 2 commits into
mainfrom
feature/scad-anchors
Nov 5, 2026
Merged

Malcolmnixon merged 2 commits into
mainfrom
feature/scad-anchors

Conversation

@Malcolmnixon

Copy link
Copy Markdown
Member

Pull Request

Description

Adds callout anchors to OpenSCAD renders. A diagram can now point leader lines at features of a rendered 3D model, whether it is drawn by hand or by an AI agent, and whether it embeds the model through SVG <image href> (#110) or places the rendered image itself.

  • Declaring an anchor: in the model, write echo("anchor", "name", [x, y, z]);. This is OpenSCAD-compatible: plain OpenSCAD just prints the line, and CanvasNet still prints it too.
    • The point is transformed by the enclosing translate, rotate, scale, mirror and multmatrix, using the same matrices as the geometry, so it lands in world space.
    • Duplicate names are all kept, in echo order.
    • A malformed anchor gives a warning beginning scad-anchor-invalid: and is skipped.
    • An anchor under resize, an extrusion, projection or offset is skipped with a warning, and one under a singular transform is dropped, as the geometry is.
  • Library API:
    • Render3D gets Camera.TryProject(point, width, height, out pixel), which uses exactly the rasterizer's view transform, projection and fit margins.
    • Scad gets a public ScadAnchor(Name, Point, Pixel?, IsInsideImage, Location) record and the ScadRenderOptions.AnchorHandler option.
  • Tool: canvasnet render model.scad -o out.png --json adds "anchors": [{ "name", "point": [x,y,z], "pixel": [x,y], "inside" }].
    • Pixels are continuous, with the origin at the image's top-left corner.
    • They are mapped through crop, trim, resize, fit, pad, flip and rotate, at any angle. After --deskew the pixel is null.
    • Text output is unchanged.
    • The help covers anchors: the scad topic, the json field and an Examples/scad-anchors.scad example.

Demo: an SVG diagram that embeds part.scad, with callouts placed from the anchors in canvasnet render part.scad --json:

anchors

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code quality improvement

Related Issues

User request: embed 3D OpenSCAD visualizations in 2D diagrams with callouts. This is the follow-up to #110.

Pre-Submission Checklist

Before submitting this pull request, ensure you have completed the following:

Build and Test

  • Code builds successfully and all tests pass: pwsh ./build.ps1 ran 49,161 tests with 0 failed and 33 skipped (existing symlink and platform skips).
  • Code produces zero warnings

Code Quality

  • New code has appropriate XML documentation comments
  • Static analyzer warnings have been addressed

Quality Checks

Please run the following checks before submitting:

  • All linters pass: pwsh ./lint.ps1 passed, along with the Pandoc harness and render-images.ps1 -Check

Testing

  • Added unit tests for new functionality:
    • anchor parsing, including valid, malformed, transformed and unsupported contexts
    • TryProject against rendered pixels in orthographic, perspective and auto-fit views
    • tool JSON, and pixel mapping through every pipeline step, including a sampling test against a rotated image
    • a Viewer embedding regression test
  • Updated existing tests if behavior changed
  • All tests follow the AAA (Arrange, Act, Assert) pattern
  • Test coverage is maintained or improved

Documentation

  • Updated README.md (if applicable)
  • Updated docs/ documentation (if applicable)
  • Added code examples for new features (if applicable)
  • Updated requirements.yaml (if applicable)

Additional Notes

  • Not included: an occlusion (visible) flag. The renderer's depth buffer is tile-local and discarded, so this would need a retained depth buffer.
  • Possible follow-ups:
    • anchors through the Viewer SVG embedding path
    • anchors in inspect
    • exact anchors under resize and the extrusions
    • a real Code field on ScadDiagnostic (today the code is a message prefix)
  • Behavior change: a model that already used echo("anchor", ...) for something else now gets a warning.

…ojected pixel positions in the library and 'render --json'

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings November 5, 2026 04:40

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Active moderate and critical findings remain, including inconsistent anchor handling, an unconditional traversal regression, and a help example referencing a nonexistent resource.

3 open issues
What changed in this PR

Adds end-to-end OpenSCAD callout-anchor support, exposing transformed 3D anchor points and projected pixel positions through the Scad API and CLI JSON output.

Changes:

  • Adds anchor declaration parsing, validation, transformation tracking, diagnostics, and public reporting.
  • Adds camera projection and image-pipeline mapping through crop, resize, fit, padding, flipping, rotation, and deskew operations.
  • Adds tests, documentation, examples, requirements, verification records, and architecture metadata.
  • Review findings include a broken help-example path, projection and transform-consistency issues, an unnecessary anchor traversal for existing builds, and documentation discrepancies.
File Description
test/​DemaConsulting.CanvasNet.Viewer.Tests/​CanvasNetViewerSystemIntegrationTests.cs Verifies anchored SCAD embedding preserves rendered output.
test/​DemaConsulting.CanvasNet.Tool.Tests/​Imaging/​SourceLoaderScadTests.cs Tests loading anchors and their initial pixel positions.
test/​DemaConsulting.CanvasNet.Tool.Tests/​CanvasNetToolTests.cs Tests JSON output, diagnostics, pipeline mapping, and text behavior.
test/​DemaConsulting.CanvasNet.Scad.Tests/​ScadImageSourceTests.cs Verifies anchors do not affect embedded rendering.
test/​DemaConsulting.CanvasNet.Scad.Tests/​ScadAnchorTests.cs Tests public anchor value semantics and defaults.
test/​DemaConsulting.CanvasNet.Scad.Tests/​Geometry/​ScadTransformsTests.cs Tests shared transform-matrix behavior.
test/​DemaConsulting.CanvasNet.Scad.Tests/​Geometry/​ScadGeometryTestSupport.cs Exposes resolved anchors in geometry test results.
test/​DemaConsulting.CanvasNet.Scad.Tests/​Geometry/​ScadGeometryBuilderAnchorTests.cs Tests anchor transforms, ordering, unsupported contexts, and geometry invariance.
test/​DemaConsulting.CanvasNet.Scad.Tests/​Evaluation/​ScadEvaluatorTestSupport.cs Adds anchor formatting to evaluator test output.
test/​DemaConsulting.CanvasNet.Scad.Tests/​Evaluation/​ScadEvaluatorAnchorTests.cs Tests anchor declaration parsing and validation.
test/​DemaConsulting.CanvasNet.Render3D.Tests/​SceneRendering/​CameraTests.cs Tests camera projection across supported view modes.
src/​DemaConsulting.CanvasNet.Tool/​Imaging/​SourceModels.cs Adds loaded-source and tool-level anchor models.
src/​DemaConsulting.CanvasNet.Tool/​Imaging/​SourceLoader.Scad.cs Collects anchors while rendering SCAD sources.
src/​DemaConsulting.CanvasNet.Tool/​Imaging/​ImagePipeline.cs Makes the pipeline extensible for anchor mapping.
src/​DemaConsulting.CanvasNet.Tool/​Imaging/​ImagePipeline.Anchors.cs Maps anchor pixels through image operations.
src/​DemaConsulting.CanvasNet.Tool/​Commands/​RenderCommand.cs Emits anchor data in JSON results.
src/​DemaConsulting.CanvasNet.Tool/​Commands/​Help/​scad.json Documents SCAD anchors and the example command.
src/​DemaConsulting.CanvasNet.Tool/​Commands/​Help/​json.json Documents the JSON anchors field.
src/​DemaConsulting.CanvasNet.Tool/​Commands/​Help/​Examples/​scad-anchors.scad Adds a runnable SCAD anchor example.
src/​DemaConsulting.CanvasNet.Scad/​ScadRenderOptions.cs Adds the public anchor callback option.
src/​DemaConsulting.CanvasNet.Scad/​ScadDocument.Render.cs Resolves, projects, and reports anchors during rendering.
src/​DemaConsulting.CanvasNet.Scad/​ScadAnchor.cs Defines the public anchor record.
src/​DemaConsulting.CanvasNet.Scad/​Geometry/​ScadTransforms.cs Shares transform computation between geometry and anchors.
src/​DemaConsulting.CanvasNet.Scad/​Geometry/​ScadGeometryContext.cs Carries resolved anchor data through scene construction.
src/​DemaConsulting.CanvasNet.Scad/​Geometry/​ScadGeometryBuilder.Walk.cs Keeps anchor instances out of generated geometry.
src/​DemaConsulting.CanvasNet.Scad/​Geometry/​ScadGeometryBuilder.cs Integrates anchor collection into scene building.
src/​DemaConsulting.CanvasNet.Scad/​Geometry/​ScadGeometryBuilder.Anchors.cs Resolves transformed anchor world points.
src/​DemaConsulting.CanvasNet.Scad/​Evaluation/​ScadInstance.cs Adds the anchor instance kind.
src/​DemaConsulting.CanvasNet.Scad/​Evaluation/​ScadEvaluator.Statements.cs Parses and validates anchor declarations.
src/​DemaConsulting.CanvasNet.Scad/​Evaluation/​ScadEvaluator.Expressions.cs Preserves expression-form echo behavior.
src/​DemaConsulting.CanvasNet.Scad/​Evaluation/​ScadEvaluator.Calls.cs Preserves non-statement echo behavior.
src/​DemaConsulting.CanvasNet.Render3D/​SceneRendering/​Camera.cs Adds world-point-to-pixel projection.
README.md Briefly documents the anchor API and CLI feature.
docs/​verification/​canvas-net-tool/​imaging/​source-loader.md Documents source-loader anchor verification.
docs/​verification/​canvas-net-tool/​imaging/​image-pipeline.md Documents anchor mapping verification.
docs/​verification/​canvas-net-tool/​commands/​render-command.md Documents CLI anchor verification.
docs/​verification/​canvas-net-tool/​commands/​help-topics.md Covers help-example verification.
docs/​verification/​canvas-net-tool.md Adds tool-level anchor verification.
docs/​verification/​canvas-net-scad/​scad-geometry/​scad-transforms.md Documents transform verification.
docs/​verification/​canvas-net-scad/​scad-geometry/​scad-geometry-builder.md Documents geometry-builder anchor verification.
docs/​verification/​canvas-net-scad/​scad-evaluation/​scad-evaluator.md Documents evaluator anchor verification.
docs/​verification/​canvas-net-scad/​scad-document.md Documents rendering and callback verification.
docs/​verification/​canvas-net-scad.md Adds system-level SCAD anchor verification.
docs/​verification/​canvas-net-render3d/​scene-rendering/​camera.md Documents camera projection verification.
docs/​user_guide/​canvas-net-tool.md Documents CLI anchor JSON and pixel mapping.
docs/​user_guide/​canvas-net-scad.md Documents SCAD anchor declarations and callbacks.
docs/​user_guide/​canvas-net-render3d.md Documents the camera projection API.
docs/​sysml2/​model/​canvas-net-tool/​imaging/​image-pipeline.sysml Updates pipeline traceability.
docs/​sysml2/​model/​canvas-net-tool/​commands/​render-command.sysml Updates render-command traceability.
docs/​sysml2/​model/​canvas-net-scad/​scad-geometry/​scad-geometry-builder.sysml Updates geometry-builder traceability.
docs/​sysml2/​model/​canvas-net-scad/​scad-evaluation/​scad-evaluator.sysml Updates evaluator traceability.
docs/​sysml2/​model/​canvas-net-scad/​scad-document.sysml Adds anchor API and test artifact references.
docs/​reqstream/​canvas-net-tool/​imaging/​source-loader.yaml Adds source-loader anchor requirements.
docs/​reqstream/​canvas-net-tool/​imaging/​image-pipeline.yaml Adds pipeline anchor-mapping requirements.
docs/​reqstream/​canvas-net-tool/​commands/​render-command.yaml Adds CLI anchor-output requirements.
docs/​reqstream/​canvas-net-tool.yaml Adds tool-level anchor requirements.
docs/​reqstream/​canvas-net-scad/​scad-geometry/​scad-transforms.yaml Adds shared transform requirements.
docs/​reqstream/​canvas-net-scad/​scad-geometry/​scad-geometry-builder.yaml Adds geometry-builder anchor requirements.
docs/​reqstream/​canvas-net-scad/​scad-evaluation/​scad-evaluator.yaml Adds evaluator anchor requirements.
docs/​reqstream/​canvas-net-scad/​scad-document.yaml Adds public anchor-reporting requirements.
docs/​reqstream/​canvas-net-scad.yaml Adds system-level SCAD anchor requirements.
docs/​reqstream/​canvas-net-render3d/​scene-rendering/​camera.yaml Adds camera projection requirements.
docs/​reqstream/​canvas-net-render3d/​scene-rendering.yaml Includes projection in scene-rendering scope.
docs/​design/​canvas-net-tool/​imaging/​source-loader.md Describes loaded-source anchor data.
docs/​design/​canvas-net-tool/​imaging/​image-pipeline.md Specifies anchor-coordinate mapping.
docs/​design/​canvas-net-tool/​imaging.md Adds anchor mapping to the imaging design.
docs/​design/​canvas-net-tool/​commands/​render-command.md Describes JSON anchors and output behavior.
docs/​design/​canvas-net-scad/​scad-geometry/​scad-transforms.md Describes shared transform handling.
docs/​design/​canvas-net-scad/​scad-geometry/​scad-geometry-builder.md Describes anchor traversal and world-point resolution.
docs/​design/​canvas-net-scad/​scad-geometry.md Adds anchor behavior to the geometry design.
docs/​design/​canvas-net-scad/​scad-evaluation/​scad-evaluator.md Defines anchor declaration semantics.
docs/​design/​canvas-net-scad/​scad-evaluation.md Updates the evaluation data model.
docs/​design/​canvas-net-scad/​scad-document.md Describes the public anchor API and render flow.
docs/​design/​canvas-net-scad.md Adds anchors to the SCAD system design.
docs/​design/​canvas-net-render3d/​scene-rendering/​camera.md Describes Camera.TryProject.
.reviewmark.yaml Registers anchor-related source and test files.
.cspell.yaml Adds deskew to the project dictionary.

Review effort: Lite

…els without anchors

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings November 5, 2026 05:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Approval should wait until the conflicting singular-transform anchor semantics are resolved and the full cross-package build, test, lint, and integration behavior receive human validation.

No open issues 🎉

3 resolved since last review

Review effort: Lite

@Malcolmnixon
Malcolmnixon merged commit c070e48 into main Nov 5, 2026
16 checks passed
@Malcolmnixon
Malcolmnixon deleted the feature/scad-anchors branch November 5, 2026 06:15
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.

2 participants