goad-py 1.2.2__tar.gz → 1.2.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. {goad_py-1.2.2 → goad_py-1.2.3}/Cargo.lock +2 -1
  2. {goad_py-1.2.2 → goad_py-1.2.3}/Cargo.toml +2 -2
  3. {goad_py-1.2.2 → goad_py-1.2.3}/PKG-INFO +1 -1
  4. goad_py-1.2.3/examples/field_at_initial_beam.rs +83 -0
  5. {goad_py-1.2.2 → goad_py-1.2.3}/examples/lidar_batch_test.rs +1 -1
  6. {goad_py-1.2.2 → goad_py-1.2.3}/pyproject.toml +1 -1
  7. {goad_py-1.2.2 → goad_py-1.2.3}/src/beam.rs +57 -50
  8. {goad_py-1.2.2 → goad_py-1.2.3}/src/clip.rs +3 -16
  9. {goad_py-1.2.2 → goad_py-1.2.3}/src/field.rs +9 -0
  10. {goad_py-1.2.2 → goad_py-1.2.3}/src/geom.rs +175 -5
  11. {goad_py-1.2.2 → goad_py-1.2.3}/src/lib.rs +1 -0
  12. {goad_py-1.2.2 → goad_py-1.2.3}/src/problem.rs +115 -142
  13. goad_py-1.2.3/src/recording.rs +625 -0
  14. {goad_py-1.2.2 → goad_py-1.2.3}/src/settings/constants.rs +2 -2
  15. {goad_py-1.2.2 → goad_py-1.2.3}/src/settings.rs +14 -4
  16. {goad_py-1.2.2 → goad_py-1.2.3}/.github/workflows/CI.yml +0 -0
  17. {goad_py-1.2.2 → goad_py-1.2.3}/.gitignore +0 -0
  18. {goad_py-1.2.2 → goad_py-1.2.3}/LICENSE +0 -0
  19. {goad_py-1.2.2 → goad_py-1.2.3}/README.md +0 -0
  20. {goad_py-1.2.2 → goad_py-1.2.3}/config/default.toml +0 -0
  21. {goad_py-1.2.2 → goad_py-1.2.3}/config_editor.sh +0 -0
  22. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/generate_ensemble.py +0 -0
  23. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/generate_plate.py +0 -0
  24. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/generate_roughness_sweep.py +0 -0
  25. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/main.rs +0 -0
  26. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/plate-render.png +0 -0
  27. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/plot_roughness_sweep.py +0 -0
  28. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/results.json +0 -0
  29. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/roughened_edge15.0_sigma0p1_merge1p0_hexagonal_column_l5.0_r25.0_6904c8.obj +0 -0
  30. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/run_ensemble.py +0 -0
  31. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/run_individual.py +0 -0
  32. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/run_roughness_sweep.py +0 -0
  33. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/run_simulation.py +0 -0
  34. {goad_py-1.2.2 → goad_py-1.2.3}/examples/backscattering/settings.json +0 -0
  35. {goad_py-1.2.2 → goad_py-1.2.3}/examples/capture_vertices.rs +0 -0
  36. {goad_py-1.2.2 → goad_py-1.2.3}/examples/convergence.rs +0 -0
  37. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/8col.obj +0 -0
  38. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/clip_test.obj +0 -0
  39. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/concave1.obj +0 -0
  40. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/concave2.obj +0 -0
  41. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cone.obj +0 -0
  42. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cube.obj +0 -0
  43. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cube2.obj +0 -0
  44. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cube_inside_cube.obj +0 -0
  45. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cube_inside_hex.obj +0 -0
  46. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cube_inside_ico.obj +0 -0
  47. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/cubes.obj +0 -0
  48. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex.obj +0 -0
  49. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex2.obj +0 -0
  50. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex3.obj +0 -0
  51. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex4.obj +0 -0
  52. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex5.obj +0 -0
  53. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex6.obj +0 -0
  54. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex7.obj +0 -0
  55. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex_20_30_30.obj +0 -0
  56. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex_distort.obj +0 -0
  57. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex_hollow.obj +0 -0
  58. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/hex_indented.obj +0 -0
  59. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/icosphere1.obj +0 -0
  60. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/multiple.obj +0 -0
  61. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/multiple2.obj +0 -0
  62. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/multiple3.obj +0 -0
  63. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/octo.obj +0 -0
  64. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/octo2.obj +0 -0
  65. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/para.obj +0 -0
  66. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/para_rough1.obj +0 -0
  67. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plane_xy.obj +0 -0
  68. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plane_xy_rect.obj +0 -0
  69. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plane_xy_rect_split.obj +0 -0
  70. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plane_yz.obj +0 -0
  71. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plate.obj +0 -0
  72. {goad_py-1.2.2 → goad_py-1.2.3}/examples/data/plate_distort.obj +0 -0
  73. {goad_py-1.2.2 → goad_py-1.2.3}/examples/diffraction_plot.rs +0 -0
  74. {goad_py-1.2.2 → goad_py-1.2.3}/examples/distortion.rs +0 -0
  75. {goad_py-1.2.2 → goad_py-1.2.3}/examples/lidar_convergence_test.rs +0 -0
  76. {goad_py-1.2.2 → goad_py-1.2.3}/examples/multiproblem.rs +0 -0
  77. {goad_py-1.2.2 → goad_py-1.2.3}/examples/problem-diff.rs +0 -0
  78. {goad_py-1.2.2 → goad_py-1.2.3}/examples/problem1.rs +0 -0
  79. {goad_py-1.2.2 → goad_py-1.2.3}/examples/regen_mueller_refs/main.rs +0 -0
  80. {goad_py-1.2.2 → goad_py-1.2.3}/examples/simplify.rs +0 -0
  81. {goad_py-1.2.2 → goad_py-1.2.3}/mkdocs.yml +0 -0
  82. {goad_py-1.2.2 → goad_py-1.2.3}/python/goad/__init__.py +0 -0
  83. {goad_py-1.2.2 → goad_py-1.2.3}/python/goad/_goad.pyi +0 -0
  84. {goad_py-1.2.2 → goad_py-1.2.3}/python/goad/bar.py +0 -0
  85. {goad_py-1.2.2 → goad_py-1.2.3}/python/goad/py.typed +0 -0
  86. {goad_py-1.2.2 → goad_py-1.2.3}/setup.sh +0 -0
  87. {goad_py-1.2.2 → goad_py-1.2.3}/src/_quickstart.rs +0 -0
  88. {goad_py-1.2.2 → goad_py-1.2.3}/src/bin/stub_gen.rs +0 -0
  89. {goad_py-1.2.2 → goad_py-1.2.3}/src/bins.rs +0 -0
  90. {goad_py-1.2.2 → goad_py-1.2.3}/src/cancel.rs +0 -0
  91. {goad_py-1.2.2 → goad_py-1.2.3}/src/containment.rs +0 -0
  92. {goad_py-1.2.2 → goad_py-1.2.3}/src/convergence/convergeable.rs +0 -0
  93. {goad_py-1.2.2 → goad_py-1.2.3}/src/convergence/progress.rs +0 -0
  94. {goad_py-1.2.2 → goad_py-1.2.3}/src/convergence/python.rs +0 -0
  95. {goad_py-1.2.2 → goad_py-1.2.3}/src/convergence.rs +0 -0
  96. {goad_py-1.2.2 → goad_py-1.2.3}/src/diff.rs +0 -0
  97. {goad_py-1.2.2 → goad_py-1.2.3}/src/diff2.rs +0 -0
  98. {goad_py-1.2.2 → goad_py-1.2.3}/src/distortion.rs +0 -0
  99. {goad_py-1.2.2 → goad_py-1.2.3}/src/filelog.rs +0 -0
  100. {goad_py-1.2.2 → goad_py-1.2.3}/src/fresnel.rs +0 -0
  101. {goad_py-1.2.2 → goad_py-1.2.3}/src/main.rs +0 -0
  102. {goad_py-1.2.2 → goad_py-1.2.3}/src/multiproblem.rs +0 -0
  103. {goad_py-1.2.2 → goad_py-1.2.3}/src/orientation.rs +0 -0
  104. {goad_py-1.2.2 → goad_py-1.2.3}/src/output.rs +0 -0
  105. {goad_py-1.2.2 → goad_py-1.2.3}/src/params.rs +0 -0
  106. {goad_py-1.2.2 → goad_py-1.2.3}/src/powers.rs +0 -0
  107. {goad_py-1.2.2 → goad_py-1.2.3}/src/python.rs +0 -0
  108. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/component.rs +0 -0
  109. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/integrate.rs +0 -0
  110. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/mueller.rs +0 -0
  111. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/python.rs +0 -0
  112. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/results.rs +0 -0
  113. {goad_py-1.2.2 → goad_py-1.2.3}/src/result/scatt_result.rs +0 -0
  114. {goad_py-1.2.2 → goad_py-1.2.3}/src/result.rs +0 -0
  115. {goad_py-1.2.2 → goad_py-1.2.3}/src/settings/cli.rs +0 -0
  116. {goad_py-1.2.2 → goad_py-1.2.3}/src/settings/loading.rs +0 -0
  117. {goad_py-1.2.2 → goad_py-1.2.3}/src/settings/validation.rs +0 -0
  118. {goad_py-1.2.2 → goad_py-1.2.3}/src/snell.rs +0 -0
  119. {goad_py-1.2.2 → goad_py-1.2.3}/src/zones.rs +0 -0
  120. {goad_py-1.2.2 → goad_py-1.2.3}/template/custom_bins.toml +0 -0
  121. {goad_py-1.2.2 → goad_py-1.2.3}/template/goad_pbs.sh +0 -0
  122. {goad_py-1.2.2 → goad_py-1.2.3}/tests/common/cases.rs +0 -0
  123. {goad_py-1.2.2 → goad_py-1.2.3}/tests/compare_solvers.rs +0 -0
  124. {goad_py-1.2.2 → goad_py-1.2.3}/tests/convergence_merge_tests.rs +0 -0
  125. {goad_py-1.2.2 → goad_py-1.2.3}/tests/diffraction_tests.rs +0 -0
  126. {goad_py-1.2.2 → goad_py-1.2.3}/tests/helpers.rs +0 -0
  127. {goad_py-1.2.2 → goad_py-1.2.3}/tests/mueller_regression_tests.rs +0 -0
  128. {goad_py-1.2.2 → goad_py-1.2.3}/tests/optical_theorem_tests.rs +0 -0
  129. {goad_py-1.2.2 → goad_py-1.2.3}/tests/orientation_sampling_tests.rs +0 -0
  130. {goad_py-1.2.2 → goad_py-1.2.3}/tests/test_basic.py +0 -0
  131. {goad_py-1.2.2 → goad_py-1.2.3}/tests/test_data/fixed_hex_30_20_20_mueller_scatgrid +0 -0
  132. {goad_py-1.2.2 → goad_py-1.2.3}/tests/test_data/fixed_hex_30_30_30_mueller_scatgrid +0 -0
  133. {goad_py-1.2.2 → goad_py-1.2.3}/tests/test_data/hex.obj +0 -0
@@ -753,7 +753,7 @@ dependencies = [
753
753
 
754
754
  [[package]]
755
755
  name = "goad"
756
- version = "1.2.2"
756
+ version = "1.2.3"
757
757
  dependencies = [
758
758
  "anyhow",
759
759
  "chrono",
@@ -1505,6 +1505,7 @@ dependencies = [
1505
1505
  "indoc",
1506
1506
  "libc",
1507
1507
  "memoffset",
1508
+ "num-complex",
1508
1509
  "once_cell",
1509
1510
  "portable-atomic",
1510
1511
  "pyo3-build-config",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "goad"
3
- version = "1.2.2"
3
+ version = "1.2.3"
4
4
  edition = "2021"
5
5
  license = "GPL-3.0"
6
6
  description = "Compute the single scattering properties of particles much larger than the wavelength of light with geometric optics and aperture diffraction theory."
@@ -25,7 +25,7 @@ readme = "README.md"
25
25
  ndarray = "0.15"
26
26
  num-complex = { version = "0.4.6", features = ["serde"] }
27
27
  numpy = "0.27.0"
28
- pyo3 = { version = "0.27.1"}
28
+ pyo3 = { version = "0.27.1", features = ["num-complex"] }
29
29
  pyo3-stub-gen = { version = "0.17", optional = true }
30
30
  rand = "0.9.0"
31
31
  rand_distr = "0.5.1"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: goad-py
3
- Version: 1.2.2
3
+ Version: 1.2.3
4
4
  Classifier: Development Status :: 4 - Beta
5
5
  Classifier: Intended Audience :: Science/Research
6
6
  Classifier: Topic :: Scientific/Engineering :: Physics
@@ -0,0 +1,83 @@
1
+ //! Sample the near-field at a handful of query points along the initial
2
+ //! incident beam.
3
+ //!
4
+ //! Runs a recorded solve on a hex geometry, filters the recording down to
5
+ //! just the initial illumination event, then asks `BeamView::fields_at`
6
+ //! for the field contribution at each query point.
7
+ //!
8
+ //! `settings.scale = Some(1.0)` opts out of the automatic rescale-to-unit-
9
+ //! cube that `Problem::init` would otherwise apply, so query points and
10
+ //! reported distances are in the same units as the geometry file.
11
+
12
+ use goad::beam::BeamVariant;
13
+ use goad::cancel::CancelToken;
14
+ use goad::geom::Geom;
15
+ use goad::problem::Problem;
16
+ use goad::settings::load_default_config;
17
+ use nalgebra::Point3;
18
+
19
+ fn main() {
20
+ // 1. Load default settings and pin the scale so the geometry isn't
21
+ // rescaled. Coordinates below are then in the hex.obj's native
22
+ // units (vertices at z = ±5, hex radius ≈ 5).
23
+ let mut settings = load_default_config().expect("load default config");
24
+ settings.scale = Some(1.0);
25
+
26
+ let geoms = Geom::load("./examples/data/hex.obj").expect("load hex");
27
+ let geom = geoms[0].clone();
28
+ let mut problem = Problem::new(Some(geom), Some(settings)).expect("build problem");
29
+
30
+ // 2. Run the full recorded pipeline (init → orient → illuminate →
31
+ // near-field with recording → far-field → mueller → params).
32
+ // `None` for euler → no rotation; geometry sits as loaded.
33
+ let recording = problem
34
+ .run_with_recording(None, &CancelToken::noop())
35
+ .expect("recorded solve");
36
+
37
+ println!("recorded {} propagation events", recording.len());
38
+
39
+ // 3. Filter the view to just the initial illumination event.
40
+ let view = recording.view().variant(BeamVariant::Initial);
41
+ let initial_events: Vec<_> = view.events().collect();
42
+ println!("initial events in view: {}", initial_events.len());
43
+
44
+ // 4. Pick a few query points. The default illumination travels along
45
+ // -z from a quad above the geometry. With FAC=1.1 in
46
+ // basic_initial_beam and hex top at z = 5, the illumination face
47
+ // sits at z ≈ 5.5, so the initial beam's column spans
48
+ // z ∈ [5.0, 5.5]. We sample inside that slab, plus two negative
49
+ // controls (upstream of the start, off-footprint in xy).
50
+ let query_points = vec![
51
+ Point3::new(0.0, 0.0, 5.4), // inside the initial slab, near start
52
+ Point3::new(0.0, 0.0, 5.2), // inside, mid-slab
53
+ Point3::new(0.0, 0.0, 5.05), // inside, near front cap
54
+ Point3::new(0.0, 0.0, 10.0), // upstream of illumination — outside
55
+ Point3::new(0.0, 0.0, 0.0), // inside the geometry — past front cap
56
+ Point3::new(20.0, 0.0, 5.2), // off the illumination footprint in xy
57
+ ];
58
+
59
+ let results = view.fields_at(&query_points);
60
+
61
+ // 5. Report per-point contributions.
62
+ for (i, contribs) in results.iter().enumerate() {
63
+ let p = &query_points[i];
64
+ println!(
65
+ "\npoint ({:.3}, {:.3}, {:.3}): {} contribution(s)",
66
+ p.x,
67
+ p.y,
68
+ p.z,
69
+ contribs.len()
70
+ );
71
+ for c in contribs {
72
+ println!(
73
+ " event {} output {} kind={:?} distance={:.4} intensity={:.4e} phase={:.4}",
74
+ c.event_id,
75
+ c.output_index,
76
+ c.kind,
77
+ c.distance,
78
+ c.field.intensity(),
79
+ c.field.phase(),
80
+ );
81
+ }
82
+ }
83
+ }
@@ -66,7 +66,7 @@ fn run_single_config(
66
66
  euler_convention: EulerConvention::ZYZ,
67
67
  },
68
68
  seed: None,
69
- scale: 1.0,
69
+ scale: Some(1.0),
70
70
  distortion: None,
71
71
  geom_scale: None,
72
72
  fov_factor: None,
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "goad-py"
3
- version = "1.2.2"
3
+ version = "1.2.3"
4
4
  description = "Physical optics light scattering computation"
5
5
  authors = [{name = "Harry Ballington", email = "ballington@uni-wuppertal.de"}]
6
6
  readme = "README.md"
@@ -1,9 +1,8 @@
1
1
  use anyhow::Result;
2
2
  use std::f32::consts::PI;
3
+ use std::sync::atomic::{AtomicU64, Ordering};
3
4
 
4
- use geo::Coord;
5
-
6
- use nalgebra::{Complex, Matrix4, Point3, Vector3};
5
+ use nalgebra::{Complex, Matrix4, Vector3};
7
6
 
8
7
  use crate::{
9
8
  bins::SolidAngleBin,
@@ -16,53 +15,38 @@ use crate::{
16
15
  snell::get_theta_t,
17
16
  };
18
17
 
19
- #[derive(Debug, Clone, PartialEq)]
20
- pub struct BeamPropagation {
21
- pub input: Beam,
22
- pub refr_index: Complex<f32>,
23
- pub outputs: Vec<Beam>,
24
- }
18
+ /// Process-unique identifier for a beam. Assigned at construction in
19
+ /// `Beam::new`; preserved across `Clone` (a clone is the same logical beam).
20
+ pub type BeamId = u64;
25
21
 
26
- impl BeamPropagation {
27
- /// Makes a new `BeamPropagation` struct, which represents a beam propagation.
28
- pub fn new(input: Beam, outputs: Vec<Beam>) -> Self {
29
- let refr_index = input.refr_index.clone();
30
- Self {
31
- input,
32
- refr_index,
33
- outputs,
34
- }
35
- }
36
- #[allow(dead_code)]
37
- fn get_line(point: &Point3<f32>, input: &Beam) -> Vec<Coord<f32>> {
38
- let output_mid = point;
39
- let input_mid = input.face.data().midpoint;
40
- let vec = input_mid - output_mid;
41
- let input_normal = input.face.data().normal;
42
- let norm_dist_to_plane = vec.dot(&input_normal);
43
- let dist_to_plane = norm_dist_to_plane / (input_normal.dot(&input.field.prop()));
44
- // ray cast along propagation direction
45
- let intsn = output_mid + dist_to_plane * input.field.prop();
46
- vec![
47
- Coord {
48
- x: output_mid.coords.x,
49
- y: output_mid.coords.y,
50
- },
51
- Coord {
52
- x: intsn.coords.x,
53
- y: intsn.coords.y,
54
- },
55
- ]
56
- }
22
+ static BEAM_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
57
23
 
58
- pub fn input_power(&self) -> f32 {
59
- self.input.power()
60
- }
24
+ fn next_beam_id() -> BeamId {
25
+ BEAM_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
26
+ }
61
27
 
62
- pub fn output_power(&self) -> f32 {
63
- let total = self.outputs.iter().fold(0.0, |acc, x| acc + x.power());
28
+ /// Where an output beam goes after a propagation event. Shared by the
29
+ /// production batch path (via `categorise_outputs`) and the recording path.
30
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
31
+ pub enum OutputKind {
32
+ /// Re-propagated as a new internal beam.
33
+ NearField,
34
+ /// Exits geometry — feeds the far-field diffraction stage.
35
+ OutGoing,
36
+ /// External (silhouette) diffraction beam from the initial pass.
37
+ ExternalDiff,
38
+ }
64
39
 
65
- total
40
+ /// Pure classification of an (input, output) variant pair. Returns None for
41
+ /// combinations the solver drops on the floor (e.g. anything emitted by an
42
+ /// OutGoing beam — which never happens but the match must be total).
43
+ pub fn classify_output(input: &BeamVariant, output: &BeamVariant) -> Option<OutputKind> {
44
+ match (input, output) {
45
+ (BeamVariant::Default(..), BeamVariant::Default(..))
46
+ | (BeamVariant::Initial, BeamVariant::Default(..)) => Some(OutputKind::NearField),
47
+ (BeamVariant::Default(..), BeamVariant::OutGoing) => Some(OutputKind::OutGoing),
48
+ (BeamVariant::Initial, BeamVariant::ExternalDiff) => Some(OutputKind::ExternalDiff),
49
+ _ => None,
66
50
  }
67
51
  }
68
52
 
@@ -194,11 +178,12 @@ impl Beam {
194
178
  .dot(&self.field.prop())
195
179
  .abs(); // z-distance
196
180
  let wavenumber = self.wavenumber();
197
- field.wind(dist * wavenumber * n1.re); // increment phase
181
+ // Compute absorbed_intensity before applying the attenuation —
182
+ // intensity is invariant under phase winding so it can be read
183
+ // after `propagate` increments phase but before it attenuates.
198
184
  let absorbed_intensity =
199
185
  field.intensity() * (1.0 - (-wavenumber * n1.im * dist).exp().powi(2));
200
- let exp_absorption = (-wavenumber * n1.im * dist).exp(); // absorption
201
- field.mul(exp_absorption); // multiply both ampl and ampl0 by exp_absorption factor
186
+ field.propagate(dist, wavenumber, n1);
202
187
  self.absorbed_power +=
203
188
  absorbed_intensity * face.data().area.unwrap() * theta_i.cos() * n1.re;
204
189
 
@@ -468,8 +453,11 @@ impl Beam {
468
453
  }
469
454
 
470
455
  /// Contains information about a beam.
471
- #[derive(Debug, Clone, PartialEq)] // Added Default derive
456
+ #[derive(Debug, Clone)]
472
457
  pub struct Beam {
458
+ /// Process-unique identity. Set in `new`; preserved by `Clone` so cross-
459
+ /// thread / cross-batch clones are recognised as the same logical beam.
460
+ pub id: BeamId,
473
461
  pub face: Face,
474
462
  pub refr_index: Complex<f32>,
475
463
  pub rec_count: i32,
@@ -481,6 +469,23 @@ pub struct Beam {
481
469
  pub wavelength: f32,
482
470
  }
483
471
 
472
+ // Equality is over physical content; identity (`id`) is excluded so a clone
473
+ // (which intentionally shares the parent's id) still compares equal to its
474
+ // source.
475
+ impl PartialEq for Beam {
476
+ fn eq(&self, other: &Self) -> bool {
477
+ self.face == other.face
478
+ && self.refr_index == other.refr_index
479
+ && self.rec_count == other.rec_count
480
+ && self.tir_count == other.tir_count
481
+ && self.field == other.field
482
+ && self.absorbed_power == other.absorbed_power
483
+ && self.clipping_area == other.clipping_area
484
+ && self.variant == other.variant
485
+ && self.wavelength == other.wavelength
486
+ }
487
+ }
488
+
484
489
  /// Creates a new beam
485
490
  impl Beam {
486
491
  pub fn new(
@@ -493,6 +498,7 @@ impl Beam {
493
498
  wavelength: f32,
494
499
  ) -> Self {
495
500
  Self {
501
+ id: next_beam_id(),
496
502
  face,
497
503
  refr_index,
498
504
  rec_count,
@@ -538,6 +544,7 @@ impl Beam {
538
544
  let new_field = self.field.rotated(&rot3);
539
545
 
540
546
  Ok(Self {
547
+ id: self.id,
541
548
  face: new_face,
542
549
  refr_index: self.refr_index,
543
550
  rec_count: self.rec_count,
@@ -5,7 +5,7 @@ use anyhow::Result;
5
5
  use geo::{Area, BooleanOps, Simplify};
6
6
 
7
7
  use log::trace;
8
- use nalgebra::{self as na, Isometry3, Matrix4, Point3, Vector3};
8
+ use nalgebra::{Matrix4, Point3, Vector3};
9
9
  use std::cmp::Ordering;
10
10
  use std::fmt;
11
11
 
@@ -753,21 +753,8 @@ impl<'a> Clipping<'a> {
753
753
 
754
754
  /// Sets the forward and inverse transform for the clipping
755
755
  fn set_transform(&mut self) {
756
- let model = Isometry3::new(Vector3::zeros(), na::zero()); // do some sort of projection - set to nothing
757
- let origin = Point3::origin(); // camera location
758
- let target = Point3::new(self.proj.x, self.proj.y, self.proj.z); // projection direction, defines negative z-axis in new coords
759
-
760
- let up: Vector3<f32> =
761
- if self.proj.cross(&Vector3::y()).norm() < settings::COLINEAR_THRESHOLD {
762
- Vector3::x()
763
- } else {
764
- Vector3::y()
765
- };
766
-
767
- let view = Isometry3::look_at_rh(&origin, &target, &up);
768
-
769
- self.transform = (view * model).to_homogeneous(); // transform to clipping system
770
- self.itransform = self.transform.try_inverse().unwrap(); // inverse transform
756
+ self.transform = crate::geom::look_along(*self.proj);
757
+ self.itransform = self.transform.try_inverse().unwrap();
771
758
  }
772
759
 
773
760
  pub fn init_clip(&mut self) -> Result<(&Face, Vec<&Face>)> {
@@ -210,6 +210,15 @@ impl Field {
210
210
  self.phase += arg;
211
211
  }
212
212
 
213
+ /// Propagate this field forward by `distance` through a medium of
214
+ /// (possibly complex) refractive index `n` at wavenumber `k`. Phase
215
+ /// is wound by `distance · k · n.re`; amplitude is attenuated by
216
+ /// `exp(-k · n.im · distance)`.
217
+ pub fn propagate(&mut self, distance: f32, wavenumber: f32, n: Complex<f32>) {
218
+ self.wind(distance * wavenumber * n.re);
219
+ self.mul((-wavenumber * n.im * distance).exp());
220
+ }
221
+
213
222
  // Getters
214
223
 
215
224
  /// Returns the propagation vector of the field
@@ -331,6 +331,24 @@ pub struct Plane {
331
331
  pub offset: f32,
332
332
  }
333
333
 
334
+ /// World → view transform that aligns `prop` with the -z axis. This is the
335
+ /// same convention used by the near-field clipping algorithm: after the
336
+ /// transform is applied to a scene, anything moving along `prop` in the
337
+ /// world is moving along -z in the transformed frame, which makes 2D
338
+ /// polygon clipping in xy straightforward.
339
+ pub fn look_along(prop: Vector3<f32>) -> Matrix4<f32> {
340
+ let model = Isometry3::new(Vector3::zeros(), na::zero());
341
+ let origin = Point3::origin();
342
+ let target = Point3::new(prop.x, prop.y, prop.z);
343
+ let up: Vector3<f32> = if prop.cross(&Vector3::y()).norm() < settings::COLINEAR_THRESHOLD {
344
+ Vector3::x()
345
+ } else {
346
+ Vector3::y()
347
+ };
348
+ let view = Isometry3::look_at_rh(&origin, &target, &up);
349
+ (view * model).to_homogeneous()
350
+ }
351
+
334
352
  /// Represents a closed line of exterior points of a polygon 3D.
335
353
  #[derive(Debug, Clone, PartialEq)]
336
354
  pub struct FaceData {
@@ -1187,6 +1205,34 @@ impl Shape {
1187
1205
 
1188
1206
  Ok(shape)
1189
1207
  }
1208
+
1209
+ /// Refractive index of this shape as a Python complex number.
1210
+ #[getter]
1211
+ fn get_refr_index(&self) -> Complex<f32> {
1212
+ self.refr_index
1213
+ }
1214
+
1215
+ #[setter]
1216
+ fn set_refr_index(&mut self, value: Complex<f32>) {
1217
+ self.refr_index = value;
1218
+ }
1219
+
1220
+ /// Shape id, if assigned.
1221
+ #[getter]
1222
+ fn get_id(&self) -> Option<usize> {
1223
+ self.id
1224
+ }
1225
+
1226
+ fn __repr__(&self) -> String {
1227
+ let id = self
1228
+ .id
1229
+ .map(|i| i.to_string())
1230
+ .unwrap_or_else(|| "?".to_string());
1231
+ format!(
1232
+ "Shape(id={}, n={:.4}{:+.4}i, faces={}, vertices={})",
1233
+ id, self.refr_index.re, self.refr_index.im, self.num_faces, self.num_vertices
1234
+ )
1235
+ }
1190
1236
  }
1191
1237
 
1192
1238
  #[cfg_attr(feature = "stub-gen", gen_stub_pyclass)]
@@ -1355,16 +1401,17 @@ impl Geom {
1355
1401
  Ok(())
1356
1402
  }
1357
1403
 
1358
- /// Rescales the geometry so that the largest dimension is 1. Returns the
1359
- /// scaling factor.
1360
- pub fn rescale(&mut self) -> f32 {
1361
- let scale = self.compute_scale_factor();
1404
+ /// Rescales the geometry. If `scale_in` is `Some(s)`, uses `s` directly;
1405
+ /// if `None`, computes the factor that maps the largest dimension to 1
1406
+ /// and writes it back through `scale_in` so the caller can record it.
1407
+ pub fn rescale(&mut self, scale_in: &mut Option<f32>) {
1408
+ let scale = scale_in.unwrap_or_else(|| self.compute_scale_factor());
1362
1409
 
1363
1410
  for shape in self.shapes.iter_mut() {
1364
1411
  shape.rescale(scale);
1365
1412
  }
1366
1413
 
1367
- scale
1414
+ *scale_in = Some(scale);
1368
1415
  }
1369
1416
 
1370
1417
  /// Recentres the geometry so that the centre of mass is at the origin.
@@ -1549,6 +1596,58 @@ impl Geom {
1549
1596
  Ok(())
1550
1597
  }
1551
1598
 
1599
+ /// Returns the shape indices directly contained by `parent_id` according
1600
+ /// to the containment graph (i.e. shapes whose immediate parent is
1601
+ /// `parent_id`).
1602
+ pub fn children_of(&self, parent_id: usize) -> Vec<usize> {
1603
+ (0..self.num_shapes)
1604
+ .filter(|&i| self.containment_graph.get_parent(i) == Some(parent_id))
1605
+ .collect()
1606
+ }
1607
+
1608
+ /// Returns the shape indices with no parent in the containment graph.
1609
+ pub fn roots(&self) -> Vec<usize> {
1610
+ (0..self.num_shapes)
1611
+ .filter(|&i| self.containment_graph.get_parent(i).is_none())
1612
+ .collect()
1613
+ }
1614
+
1615
+ /// Render the containment graph as an indented tree using box-drawing
1616
+ /// characters. The top line shows `medium`, then each root shape and
1617
+ /// (recursively) its contents.
1618
+ pub fn containment_tree_string(&self, medium: Complex<f32>) -> String {
1619
+ fn fmt_n(n: Complex<f32>) -> String {
1620
+ let sign = if n.im >= 0.0 { '+' } else { '-' };
1621
+ format!("{:.4} {} {:.4}i", n.re, sign, n.im.abs())
1622
+ }
1623
+
1624
+ fn walk(out: &mut String, geom: &Geom, idx: usize, prefix: &str, is_last: bool) {
1625
+ let connector = if is_last { "└── " } else { "├── " };
1626
+ let n = geom.shapes[idx].refr_index;
1627
+ out.push_str(&format!(
1628
+ "{}{}shape {} : {}\n",
1629
+ prefix,
1630
+ connector,
1631
+ idx,
1632
+ fmt_n(n)
1633
+ ));
1634
+ let next_prefix = format!("{}{}", prefix, if is_last { " " } else { "│ " });
1635
+ let kids = geom.children_of(idx);
1636
+ let last_kid = kids.len().saturating_sub(1);
1637
+ for (i, &kid) in kids.iter().enumerate() {
1638
+ walk(out, geom, kid, &next_prefix, i == last_kid);
1639
+ }
1640
+ }
1641
+
1642
+ let mut out = format!("medium : {}\n", fmt_n(medium));
1643
+ let roots = self.roots();
1644
+ let last_root = roots.len().saturating_sub(1);
1645
+ for (i, &root) in roots.iter().enumerate() {
1646
+ walk(&mut out, self, root, "", i == last_root);
1647
+ }
1648
+ out
1649
+ }
1650
+
1552
1651
  pub fn vector_scale(&mut self, scale: &Vec<f32>) {
1553
1652
  if scale.len() != 3 {
1554
1653
  panic!("Scale vector must have length 3");
@@ -1683,6 +1782,77 @@ impl Geom {
1683
1782
  Err(err) => Err(PyErr::new::<PyRuntimeError, _>(err.to_string())),
1684
1783
  }
1685
1784
  }
1785
+
1786
+ /// Number of shapes in the geometry.
1787
+ #[getter]
1788
+ fn get_num_shapes(&self) -> usize {
1789
+ self.num_shapes
1790
+ }
1791
+
1792
+ /// All shapes (cloned). Mutating an element of this list does NOT update
1793
+ /// the parent Geom — use `set_refr_index(idx, n)` for that.
1794
+ #[getter]
1795
+ fn get_shapes(&self) -> Vec<Shape> {
1796
+ self.shapes.clone()
1797
+ }
1798
+
1799
+ /// Refractive index of shape `idx` as a Python complex.
1800
+ #[pyo3(name = "refr_index")]
1801
+ fn py_refr_index(&self, idx: usize) -> PyResult<Complex<f32>> {
1802
+ self.shapes
1803
+ .get(idx)
1804
+ .map(|s| s.refr_index)
1805
+ .ok_or_else(|| {
1806
+ PyErr::new::<PyRuntimeError, _>(format!(
1807
+ "shape index {} out of range (num_shapes = {})",
1808
+ idx, self.num_shapes
1809
+ ))
1810
+ })
1811
+ }
1812
+
1813
+ /// Set the refractive index of shape `idx` from a Python complex.
1814
+ #[pyo3(name = "set_refr_index")]
1815
+ fn py_set_refr_index(&mut self, idx: usize, value: Complex<f32>) -> PyResult<()> {
1816
+ let n = self.num_shapes;
1817
+ let shape = self.shapes.get_mut(idx).ok_or_else(|| {
1818
+ PyErr::new::<PyRuntimeError, _>(format!(
1819
+ "shape index {} out of range (num_shapes = {})",
1820
+ idx, n
1821
+ ))
1822
+ })?;
1823
+ shape.refr_index = value;
1824
+ Ok(())
1825
+ }
1826
+
1827
+ /// Parent shape index for `shape_id` in the containment graph, if any.
1828
+ #[pyo3(name = "parent_of")]
1829
+ fn py_parent_of(&self, shape_id: usize) -> Option<usize> {
1830
+ self.containment_graph.get_parent(shape_id)
1831
+ }
1832
+
1833
+ /// Shape indices directly contained by `parent_id`.
1834
+ #[pyo3(name = "children_of")]
1835
+ fn py_children_of(&self, parent_id: usize) -> Vec<usize> {
1836
+ self.children_of(parent_id)
1837
+ }
1838
+
1839
+ /// Shape indices with no parent.
1840
+ #[pyo3(name = "roots")]
1841
+ fn py_roots(&self) -> Vec<usize> {
1842
+ self.roots()
1843
+ }
1844
+
1845
+ /// Render the containment graph as an indented tree. `medium` defaults to
1846
+ /// vacuum (1+0j).
1847
+ #[pyo3(name = "containment_tree", signature = (medium = None))]
1848
+ fn py_containment_tree(&self, medium: Option<Complex<f32>>) -> String {
1849
+ let medium = medium.unwrap_or(Complex { re: 1.0, im: 0.0 });
1850
+ self.containment_tree_string(medium)
1851
+ }
1852
+
1853
+ fn __repr__(&self) -> String {
1854
+ self.containment_tree_string(Complex { re: 1.0, im: 0.0 })
1855
+ }
1686
1856
  }
1687
1857
 
1688
1858
  /// Calculates, rather inaccurately, the center of mass of a set of vertices.
@@ -38,6 +38,7 @@ pub mod problem;
38
38
  pub mod python;
39
39
  #[cfg(feature = "stub-gen")]
40
40
  pub use python::stub_info;
41
+ pub mod recording;
41
42
  pub mod result;
42
43
  pub mod settings;
43
44
  pub mod snell;