Skip to main content

ray-tracing-shapely

DOI

A 2D optical analysis and design environment in Python, built on Shapely for geometry. It began as a translation of a JavaScript interactive simulator and grew into something with a different purpose.

What it is for

Most 2D optics tools let you watch rays. This one lets you ask questions about them afterwards, and lets you design a surface from what you want the light to do.

Every traced ray keeps its lineage — a parent, a unique identity, the surfaces it met and what happened at each one. That turns a simulation from a picture into a record you can query: which paths reach the detector, how much energy each carries, where two designs begin to differ, which rays are trapped by repeated total internal reflection. Runs are stored and compared, so "did that change help?" is a question with an answer rather than an impression.

On top of that sit layers the original simulator does not have:

  • Ray lineage as a graph — parent/child tracking, structural path signatures, bidirectional tracing that connects a forward tree from the sources to a backward tree from the detector.
  • Nonimaging design — CPC-family concentrators (DTIRC, angle transformer, RXI) and Miñano–Benítez Simultaneous Multiple Surface profile construction (RR / RX / XR / RXI), on a reusable plane-curve toolbox. Surfaces are built from optical requirements, not from vertices typed in by hand.
  • Lenses and aspheres — labelled singlets, LED collimators (a TIR lens and a Fresnel lens) and exact even aspheres as glass edges, with the coefficients in Zemax's and Optiland's order. A lens face measured by a profilometer is read from its DXF and fitted with its vertex and tilt. The fit reports which of its numbers the data do not separately determine, and becomes two models of the lens, fitted and as measured.
  • Light sources and measured LEDs — a source described once, built in 2D and in 3D, and stored with the element it lights in profile.json. An LED's rayfile (IES TM-25) is read, drawn as a radiation pattern, and sliced into the 2D plane, with the share of its light a 2D section cannot carry stated.
  • Light on a plane — the irradiance map, and the 2D line profile, of the light that leaves an optic.
  • Radiometry and eye safety — unit-checked physical quantities, and exposure-limit routines for IEC 60825-1 (lasers) and IEC 62471 (lamps and LEDs), run as explicit state machines rather than nested conditionals.
  • An agentic surface — the whole query layer is exposed as tools a language model can call, sharing one live kernel with the human at the notebook. Several runs are held at once as named contexts, a saved run is reloaded by reference, and a package built on the library adds tools of its own beside the core's.
  • Long runs, off the notebook — a procedure is a flow: a fixed diagram whose every step is recorded, so a run that stops can be read back. Its long jobs go to local processes or to Modal's serverless containers, each with an estimate of its time and cost before it starts, and a local dashboard shows what is running and what it has cost. A targeted re-simulation of the paths that matter, cut into chunks, runs this way and gives the run the kernel would have given.
  • Run persistence — parameter sweeps, stored results, cross-run comparison.

If you want an interactive simulator with a gallery of worked scenes, the upstream Ray Optics Simulation is better. This is more an analytical tool to understand a design.

Install

pip install ray-tracing-shapely

The core install stays deliberately small — shapely, numpy, sympy, svgwrite, networkx. The heavier layers are extras, so you only pay for what you use:

pip install "ray-tracing-shapely[radiometry]"    # unit-checked quantities (pint)
pip install "ray-tracing-shapely[flows]"         # observable step-by-step procedures (transitions)
pip install "ray-tracing-shapely[eye-safety]"    # IEC 60825-1 / IEC 62471
pip install "ray-tracing-shapely[agentic]"       # the MCP tool surface
pip install "ray-tracing-shapely[modal]"         # flows' jobs on Modal (the Modal client)
pip install "ray-tracing-shapely[notebook]"      # running the example notebooks (jupyterlab, papermill)
pip install "ray-tracing-shapely[all]"           # everything

Python 3.11 or newer.

Thirty seconds

from ray_tracing_shapely.core.scene import Scene
from ray_tracing_shapely.core.scene_objs import Glass, SingleRay
from ray_tracing_shapely.core.simulator import Simulator

scene = Scene()
scene.color_mode = 'linear'

prism = Glass(scene)
prism.path = [
    {'x': 100, 'y': 50,  'arc': False},
    {'x': 200, 'y': 50,  'arc': False},
    {'x': 150, 'y': 150, 'arc': False},
]
prism.refIndex = 1.5
scene.add_object(prism)

ray = SingleRay(scene)
ray.p1 = {'x': 50,  'y': 100}
ray.p2 = {'x': 100, 'y': 100}
scene.add_object(ray)

segments = Simulator(scene, max_rays=1000).run()
print(f"Traced {len(segments)} ray segments")   # Traced 16 ray segments

Sixteen segments from one incident ray: it refracts into the glass, reflects internally, and splits at each surface it meets. Render the result as SVG with SVGRenderer, and every element it draws carries the identity of the ray or object that produced it, so a diagram can be clicked back to the simulation that made it.

Documentation

The code is documented in two layers, both shipped with the package:

  • dev_doc/concepts/ — why the code has the shape it has. Thirty-seven numbered concepts, each stating one claim and the evidence for it, written for a reader who wants to understand the design rather than just call it.
  • dev_doc/architecture/ — what is true now: a component map, a port register, and contracts stating what each boundary assumes and guarantees.

Docstrings cite both, so a question that starts in the code has somewhere to go.

Citing

If you use this software, please cite both this project and the Ray Optics Simulation project it is in part translated from. CITATION.cff carries the metadata for both; GitHub will format it for you from the Cite this repository link.

This project: doi:10.5281/zenodo.23991020 — that is the concept DOI and always resolves to the latest release. Each release also has its own DOI, which is what to cite if you need to pin the exact version a result came from.

Upstream: doi:10.5281/zenodo.6386611.

Licence and attribution

Apache-2.0. See LICENSE.txt and NOTICE.txt.

This project began as a translation of the Ray Optics Simulation JavaScript project (Copyright 2024 its authors and contributors, Apache-2.0, doi:10.5281/zenodo.6386611), and has grown a great deal of new work on top of it.

Of this distribution's 220 source files, 35 are translations of that project — the ray-tracing engine and its scene objects — and 185 were written for this project: the analysis layer, the nonimaging design methods, the radiometric and photobiological layers, the agentic surface, and the tests and examples. Every file says which it is in its own header, and NOTICE.txt lists the translated set.

If you use this software please cite both, per CITATION.cff.

Status

Early development: the translation of the original simulator is not complete, and the API is not yet stable. Issues and descriptions of what you are trying to do are the most useful contribution right now .

Metadata

Release files for ray-tracing-shapely 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ray-tracing-shapely 0.3.1
File Size Uploaded Provenance Actions
ray_tracing_shapely-0.3.1.tar.gz 834.5 kB With provenance Details for ray_tracing_shapely-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ray-tracing-shapely 0.3.1
File Interpreter ABI Platform Actions
ray_tracing_shapely-0.3.1-py3-none-any.whl Python 3 none any Details for ray_tracing_shapely-0.3.1-py3-none-any.whl

Total release size: 1.9 MB

Release files / ray_tracing_shapely-0.3.1.tar.gz

Download URL ray_tracing_shapely-0.3.1.tar.gz
Size 834.5 kB
Tags Source
SHA-256 checksum
How to use checksums
bb1c91acc048c81243d64f5e58cecf64cea871763cd0ad3374f93c3041f0acd8
BLAKE2b-256 checksum
How to use checksums
8f0e8efdab5793ea91b696830b3a9282f227de317d6ef2bbde5169126f0ff916
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Release files / ray_tracing_shapely-0.3.1-py3-none-any.whl

Download URL ray_tracing_shapely-0.3.1-py3-none-any.whl
Size 1.0 MB
Tags Python 3
SHA-256 checksum
How to use checksums
b39f64119b527fe7956eca271ed6894cc1f4fd8b58c1fb4a71851d1f22ec8807
BLAKE2b-256 checksum
How to use checksums
d4f428dab3aa9b34a20156e3e2a73e66a984319fdbc89026c5697849cc615a12
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

Security for ray-tracing-shapely 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ray_tracing_shapely-0.3.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page