Metadata-Version: 2.4
Name: ray-tracing-shapely
Version: 0.3.1
Summary: Python ray tracing library using Shapely for computational geometry
Author: ray-tracing-shapely authors and contributors
Author-email: Michele Benetti <michele@tiberavon.com>
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/tiberavon/ray-tracing-shapely
Project-URL: Repository, https://github.com/tiberavon/ray-tracing-shapely
Project-URL: Documentation, https://github.com/tiberavon/ray-tracing-shapely#readme
Keywords: ray-tracing,optics,simulation,shapely,physics
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: shapely>=2.0.0
Requires-Dist: sympy>=1.12
Requires-Dist: numpy>=1.23.2
Requires-Dist: svgwrite>=1.4.0
Requires-Dist: networkx>=3.0
Provides-Extra: radiometry
Requires-Dist: pint>=0.24; extra == "radiometry"
Provides-Extra: flows
Requires-Dist: transitions>=0.9; extra == "flows"
Requires-Dist: graphviz>=0.20; extra == "flows"
Provides-Extra: eye-safety
Requires-Dist: ray-tracing-shapely[flows,radiometry]; extra == "eye-safety"
Provides-Extra: agentic
Requires-Dist: mcp<2,>=1.2; extra == "agentic"
Provides-Extra: modal
Requires-Dist: ray-tracing-shapely[flows]; extra == "modal"
Requires-Dist: modal>=1.6; extra == "modal"
Provides-Extra: notebook
Requires-Dist: jupyterlab; extra == "notebook"
Requires-Dist: papermill; extra == "notebook"
Provides-Extra: all
Requires-Dist: ray-tracing-shapely[agentic,eye-safety,flows,modal,notebook,radiometry]; extra == "all"
Provides-Extra: test
Requires-Dist: ray-tracing-shapely[agentic,eye-safety,flows,radiometry]; extra == "test"
Requires-Dist: pytest>=7.0.0; extra == "test"
Provides-Extra: dev
Requires-Dist: ray-tracing-shapely[all]; extra == "dev"
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"

# ray-tracing-shapely

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.23991020.svg)](https://doi.org/10.5281/zenodo.23991020)

A 2D optical **analysis and design** environment in Python, built on
[Shapely](https://shapely.readthedocs.io/) 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](https://github.com/ricktu288/ray-optics) is
better. This is more an analytical tool to understand a design.

## Install

```bash
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:

```bash
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

```python
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](https://doi.org/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](https://doi.org/10.5281/zenodo.6386611).

## Licence and attribution

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

<!-- attribution:begin -- generated by dev_doc/tools/attribution.py, do not edit -->
This project began as a translation of the
[Ray Optics Simulation](https://github.com/ricktu288/ray-optics) JavaScript project
(Copyright 2024 its authors and contributors, Apache-2.0,
[doi:10.5281/zenodo.6386611](https://doi.org/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`.
<!-- attribution:end -->

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