csx 0.0.2

Pure Rust continuous-structure geometry and material modeling, compatible with CSXCAD and openEMS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! The continuous structure: the complete geometry and material model.
//!
//! A [`ContinuousStructure`] is the root object of a model. It owns
//!
//! | part | type | purpose |
//! |---|---|---|
//! | properties | [`Property`] (via [`PropertyId`] handles) | *what* a region is: material, metal, port, probe, ... |
//! | primitives | [`Primitive`], owned by each property | *where* the property applies |
//! | mesh | [`RectilinearGrid`] | FDTD mesh lines |
//! | parameters | [`ParameterSet`] | named values used by expressions |
//! | background | [`BackgroundMaterial`] | the medium outside every material |
//!
//! # Queries
//!
//! * [`ContinuousStructure::property_at`] — the winning property at a point
//!   (highest primitive priority; ties go to the property added first).
//! * [`ContinuousStructure::properties_at`] — every property at a point.
//! * [`ContinuousStructure::medium_at`] — ε, μ, κ, σ at a point.
//! * [`ContinuousStructure::spatial_index`] — an accelerated
//!   [`SpatialIndex`] for many point queries.
//! * [`ContinuousStructure::bounding_box`],
//!   [`ContinuousStructure::primitives_in`] — spatial extent.
//! * [`ContinuousStructure::validate`] — consistency warnings
//!   ([`ValidationIssue`]).
//!
//! # Example
//!
//! ```
//! use csx::primitives::BoxShape;
//! use csx::properties::Material;
//! use csx::{ContinuousStructure, CoordSystem, Primitive, Property, TypeMask, Vec3};
//!
//! let mut s = ContinuousStructure::new();
//! let sub = s.add_property(Property::material("substrate", Material::new(3.38, 0.0)));
//! s.add_primitive(sub, BoxShape::new([0.0, 0.0, 0.0], [10.0, 10.0, 0.8]))?;
//! let gnd = s.add_property(Property::metal("ground"));
//! s.add_primitive(gnd, Primitive::new(BoxShape::new([0.0, 0.0, 0.0], [10.0, 10.0, 0.0]))
//!     .with_priority(10))?;
//!
//! let p = Vec3::new(5.0, 5.0, 0.4);
//! let hit = s.property_at(p, CoordSystem::Cartesian, TypeMask::ANY).unwrap();
//! assert_eq!(hit.property.name, "substrate");
//! assert_eq!(s.medium_at(p, CoordSystem::Cartesian).unwrap().epsilon, [3.38; 3]);
//! // metal is reported as `None`
//! assert!(s.medium_at(Vec3::new(5.0, 5.0, 0.0), CoordSystem::Cartesian).is_none());
//! # Ok::<(), csx::Error>(())
//! ```

use std::path::PathBuf;

use crate::coords::CoordSystem;
use crate::error::{Error, Result};
use crate::grid::RectilinearGrid;
use crate::math::{BoundingBox, Vec3};
use crate::param::{ParameterSet, Scalar};
use crate::primitives::bvh::Bvh;
use crate::primitives::{Primitive, Shape};
use crate::properties::{MaterialQuantity, Property, PropertyKind, TypeMask};

/// Material filling all space not covered by any material primitive
/// (`<BackgroundMaterial>`; default: vacuum).
///
/// ```
/// use csx::ContinuousStructure;
/// use csx::properties::MaterialQuantity;
///
/// let mut s = ContinuousStructure::new();
/// assert_eq!(s.background.value(MaterialQuantity::Epsilon), 1.0);
/// s.background.epsilon.set(1.0006); // air
/// assert_eq!(s.background.value(MaterialQuantity::Epsilon), 1.0006);
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct BackgroundMaterial {
    /// Relative permittivity.
    pub epsilon: Scalar,
    /// Relative permeability.
    pub mue: Scalar,
    /// Electric conductivity.
    pub kappa: Scalar,
    /// Magnetic conductivity.
    pub sigma: Scalar,
    /// Unrecognised attributes preserved from files.
    pub extra_attributes: Vec<(String, String)>,
}

impl Default for BackgroundMaterial {
    fn default() -> Self {
        BackgroundMaterial {
            epsilon: Scalar::new(1.0),
            mue: Scalar::new(1.0),
            kappa: Scalar::new(0.0),
            sigma: Scalar::new(0.0),
            extra_attributes: Vec::new(),
        }
    }
}

impl BackgroundMaterial {
    fn update(&mut self, p: &ParameterSet) -> Result<()> {
        self.epsilon.update(p)?;
        self.mue.update(p)?;
        self.kappa.update(p)?;
        self.sigma.update(p)
    }

    /// Cached value of a quantity (see the update contract on
    /// [`ContinuousStructure`]).
    pub fn value(&self, q: MaterialQuantity) -> f64 {
        match q {
            MaterialQuantity::Epsilon => self.epsilon.value(),
            MaterialQuantity::Mue => self.mue.value(),
            MaterialQuantity::Kappa => self.kappa.value(),
            MaterialQuantity::Sigma => self.sigma.value(),
        }
    }
}

/// Opaque handle of a property inside a [`ContinuousStructure`].
///
/// Handles are returned by [`ContinuousStructure::add_property`] and
/// [`ContinuousStructure::property_id`]; they stay valid until the property
/// is removed and are never reused within one structure. Handles order by
/// insertion.
///
/// ```
/// use csx::{ContinuousStructure, Property};
///
/// let mut s = ContinuousStructure::new();
/// let a = s.add_property(Property::metal("a"));
/// let b = s.add_property(Property::metal("b"));
/// assert!(a < b);
/// assert_eq!(s.property_id("b"), Some(b));
/// assert_eq!(s.property(a).unwrap().id(), a.get());
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PropertyId(u32);

impl PropertyId {
    /// The numeric id, equal to [`Property::id`] of the referenced
    /// property.
    pub const fn get(self) -> u32 {
        self.0
    }
}

impl core::fmt::Display for PropertyId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "#{}", self.0)
    }
}

/// A consistency problem reported by [`ContinuousStructure::validate`].
///
/// ```
/// use csx::structure::ValidationIssue;
/// use csx::{ContinuousStructure, Property};
///
/// let mut s = ContinuousStructure::new();
/// s.add_property(Property::metal("m"));
/// let issues = s.validate();
/// assert!(matches!(&issues[0], ValidationIssue::EmptyProperty { name } if name == "m"));
/// assert_eq!(issues[0].to_string(), "property \"m\" has no primitives");
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ValidationIssue {
    /// Several properties share a name (reported for each later one);
    /// name-based lookups return the first.
    DuplicateName {
        /// The repeated name.
        name: String,
    },
    /// A property without primitives has no effect.
    EmptyProperty {
        /// Property name.
        name: String,
    },
    /// A primitive whose bounding box is empty (e.g. a polygon without
    /// vertices, a mesh file that is not loaded).
    EmptyPrimitive {
        /// Property name.
        property: String,
        /// Index of the primitive within the property.
        index: usize,
        /// Element name of the shape (`"Box"`, `"Polygon"`, ...).
        shape: &'static str,
    },
}

impl core::fmt::Display for ValidationIssue {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ValidationIssue::DuplicateName { name } => {
                write!(f, "duplicate property name {name:?}")
            }
            ValidationIssue::EmptyProperty { name } => {
                write!(f, "property {name:?} has no primitives")
            }
            ValidationIssue::EmptyPrimitive {
                property,
                index,
                shape,
            } => write!(
                f,
                "property {property:?}: primitive #{index} ({shape}) is empty"
            ),
        }
    }
}

/// The result of a point query: a property and the primitive that contains
/// the point.
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct Hit<'a> {
    /// The property.
    pub property: &'a Property,
    /// The primitive that contains the point.
    pub primitive: &'a Primitive,
    /// Index of the primitive within the property.
    pub primitive_index: usize,
}

/// Electromagnetic medium at a point, as returned by
/// [`ContinuousStructure::medium_at`]: per-axis values with weighting
/// functions applied.
///
/// | field | quantity | unit |
/// |---|---|---|
/// | `epsilon` | relative permittivity εᵣ | – |
/// | `mue` | relative permeability μᵣ | – |
/// | `kappa` | electric conductivity κ | S/m |
/// | `sigma` | magnetic conductivity σ | Ω/m |
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Medium {
    /// Relative permittivity per axis.
    pub epsilon: [f64; 3],
    /// Relative permeability per axis.
    pub mue: [f64; 3],
    /// Electric conductivity per axis.
    pub kappa: [f64; 3],
    /// Magnetic conductivity per axis.
    pub sigma: [f64; 3],
}

/// The complete model: properties with their primitives, the mesh, the
/// parameters and the background material.
///
/// # The update contract
///
/// Values that may be expressions ([`Scalar`], polygon vertices, weighting
/// functions, transformations, user-defined functions) cache their evaluated
/// form. Plain numbers are usable immediately; anything referring to
/// parameters is evaluated by [`ContinuousStructure::update`]. Call it after
/// changing [`ContinuousStructure::parameters`] or adding parametric
/// content, before querying. Caches that could silently go stale
/// (compiled user functions, loaded mesh files) are only reachable through
/// setters that invalidate them.
///
/// ```
/// use csx::primitives::BoxShape;
/// use csx::{ContinuousStructure, CoordSystem, Property, ScalarPoint, TypeMask, Vec3};
///
/// let mut s = ContinuousStructure::new();
/// s.parameters.set("w", 2.0);
/// let id = s.add_property(Property::metal("strip"));
/// s.add_primitive(id, BoxShape::new([0.0, 0.0, 0.0], ScalarPoint::from(["w", "1", "0"])))?;
/// s.update()?; // evaluate `w`
///
/// let probe = Vec3::new(1.5, 0.5, 0.0);
/// assert!(s.property_at(probe, CoordSystem::Cartesian, TypeMask::ANY).is_some());
///
/// s.parameters.set("w", 1.0);
/// s.update()?; // geometry follows the parameter
/// assert!(s.property_at(probe, CoordSystem::Cartesian, TypeMask::ANY).is_none());
/// # Ok::<(), csx::Error>(())
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ContinuousStructure {
    properties: Vec<Property>,
    next_id: u32,
    /// Mesh.
    pub grid: RectilinearGrid,
    /// Named parameters used by expressions.
    pub parameters: ParameterSet,
    /// Background material.
    pub background: BackgroundMaterial,
    /// Default coordinate system of primitives.
    pub coord_system: CoordSystem,
    /// Directory against which relative file names (mesh files) are
    /// resolved.
    pub base_dir: Option<PathBuf>,
    /// Unrecognised attributes preserved from files.
    pub extra_attributes: Vec<(String, String)>,
    /// Unrecognised child elements preserved from files.
    pub extra_elements: Vec<crate::io::dom::Element>,
}

impl ContinuousStructure {
    /// Empty structure: Cartesian, no properties, empty grid with
    /// `delta_unit = 1`, vacuum background.
    pub fn new() -> Self {
        Self::default()
    }

    /// Adds a property and returns its handle. Parametric values that do not
    /// depend on parameters are usable immediately; call
    /// [`ContinuousStructure::update`] after adding parametric content.
    pub fn add_property(&mut self, mut p: Property) -> PropertyId {
        self.next_id += 1;
        p.id = self.next_id;
        self.properties.push(p);
        PropertyId(self.next_id)
    }

    /// Adds a primitive to the property `id` and returns it for further
    /// configuration.
    ///
    /// ```
    /// use csx::primitives::Sphere;
    /// use csx::{ContinuousStructure, Property};
    ///
    /// let mut s = ContinuousStructure::new();
    /// let id = s.add_property(Property::metal("ball"));
    /// s.add_primitive(id, Sphere::new([0.0, 0.0, 0.0], 1.0))?.priority = 5;
    /// assert_eq!(s.property(id).unwrap().primitives[0].priority, 5);
    /// # Ok::<(), csx::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// [`Error::Invalid`] if `id` does not refer to a property of this
    /// structure (e.g. it was removed).
    pub fn add_primitive(
        &mut self,
        id: PropertyId,
        prim: impl Into<Primitive>,
    ) -> Result<&mut Primitive> {
        let p = self
            .property_mut(id)
            .ok_or_else(|| Error::Invalid(format!("no property {id:?}")))?;
        Ok(p.add_primitive(prim))
    }

    /// Removes a property, returning it (detached, with [`Property::id`]
    /// reset to 0). Returns `None` if `id` is not in this structure.
    pub fn remove_property(&mut self, id: PropertyId) -> Option<Property> {
        let i = self.properties.iter().position(|p| p.id == id.get())?;
        let mut p = self.properties.remove(i);
        p.id = 0;
        Some(p)
    }

    /// All properties in insertion order.
    pub fn properties(&self) -> &[Property] {
        &self.properties
    }

    /// Mutable iterator over all properties.
    pub fn properties_mut(&mut self) -> impl Iterator<Item = &mut Property> {
        self.properties.iter_mut()
    }

    /// Properties matching `mask`, in insertion order.
    ///
    /// ```
    /// use csx::properties::Material;
    /// use csx::{ContinuousStructure, Property, PropertyType, TypeMask};
    ///
    /// let mut s = ContinuousStructure::new();
    /// s.add_property(Property::material("fr4", Material::new(4.3, 0.0)));
    /// s.add_property(Property::metal("pec"));
    /// assert_eq!(s.properties_of(TypeMask::MATERIALS).count(), 1);
    /// assert_eq!(s.properties_of(PropertyType::Metal).next().unwrap().name, "pec");
    /// ```
    pub fn properties_of(&self, mask: impl Into<TypeMask>) -> impl Iterator<Item = &Property> {
        let mask = mask.into();
        self.properties
            .iter()
            .filter(move |p| mask.contains(p.property_type()))
    }

    /// Property by handle.
    pub fn property(&self, id: PropertyId) -> Option<&Property> {
        self.properties.iter().find(|p| p.id == id.get())
    }

    /// Mutable property by handle.
    pub fn property_mut(&mut self, id: PropertyId) -> Option<&mut Property> {
        self.properties.iter_mut().find(|p| p.id == id.get())
    }

    /// First property with the given name.
    pub fn property_by_name(&self, name: &str) -> Option<&Property> {
        self.properties.iter().find(|p| p.name == name)
    }

    /// Mutable first property with the given name.
    pub fn property_by_name_mut(&mut self, name: &str) -> Option<&mut Property> {
        self.properties.iter_mut().find(|p| p.name == name)
    }

    /// Handle of the first property with the given name.
    pub fn property_id(&self, name: &str) -> Option<PropertyId> {
        self.property_by_name(name).map(|p| PropertyId(p.id))
    }

    /// Iterates over all `(property, primitive)` pairs.
    pub fn primitives(&self) -> impl Iterator<Item = (&Property, &Primitive)> {
        self.properties
            .iter()
            .flat_map(|p| p.primitives.iter().map(move |q| (p, q)))
    }

    /// Re-evaluates every parametric value against [`Self::parameters`] and
    /// loads external mesh files of `PolyhedronReader` primitives that are
    /// not loaded yet (relative paths are resolved against
    /// [`Self::base_dir`]).
    ///
    /// # Errors
    ///
    /// * [`Error::UnknownVariable`] / [`Error::Parse`] if an expression
    ///   refers to an undefined parameter or is malformed.
    /// * [`Error::Invalid`] for values outside their domain (negative radius,
    ///   singular transformation, NaN result, ...), prefixed with the
    ///   property name.
    /// * [`Error::Io`] / [`Error::Format`] if a mesh file cannot be read.
    ///
    /// Values updated before the failing one keep their new value.
    pub fn update(&mut self) -> Result<()> {
        self.update_parameters()?;
        for p in &mut self.properties {
            for prim in &mut p.primitives {
                if let Shape::PolyhedronReader(r) = &mut prim.shape
                    && !r.is_loaded()
                {
                    r.load(self.base_dir.as_deref())?;
                }
            }
        }
        Ok(())
    }

    /// Re-evaluates every parametric value against [`Self::parameters`]
    /// without touching external files.
    ///
    /// # Errors
    ///
    /// Same as [`Self::update`], except for the mesh file errors.
    pub fn update_parameters(&mut self) -> Result<()> {
        let params = &self.parameters;
        self.background.update(params)?;
        for p in &mut self.properties {
            p.update(params)?;
        }
        Ok(())
    }

    /// Finds the highest-priority primitive containing `p` (expressed in
    /// `cs`) among properties matching `mask`. On equal priority the
    /// primitive of the earlier property (then the earlier primitive) wins,
    /// as in CSXCAD.
    ///
    /// Primitives without their own coordinate system use
    /// [`Self::coord_system`]. Use [`Self::spatial_index`] for many queries.
    ///
    /// ```
    /// use csx::primitives::BoxShape;
    /// use csx::properties::Material;
    /// use csx::{ContinuousStructure, CoordSystem, Primitive, Property, TypeMask, Vec3};
    ///
    /// let mut s = ContinuousStructure::new();
    /// let low = s.add_property(Property::material("low", Material::new(2.0, 0.0)));
    /// let high = s.add_property(Property::material("high", Material::new(9.0, 0.0)));
    /// s.add_primitive(low, Primitive::new(BoxShape::new([0.0; 3], [2.0; 3])).with_priority(1))?;
    /// s.add_primitive(high, Primitive::new(BoxShape::new([1.0; 3], [3.0; 3])).with_priority(2))?;
    ///
    /// let p = Vec3::new(1.5, 1.5, 1.5);
    /// let hit = s.property_at(p, CoordSystem::Cartesian, TypeMask::ANY).unwrap();
    /// assert_eq!(hit.property.name, "high");
    /// assert_eq!(hit.primitive_index, 0);
    /// assert_eq!(s.properties_at(p, CoordSystem::Cartesian, TypeMask::ANY).len(), 2);
    /// // queries may be given in cylindrical coordinates (ρ, α, z)
    /// let cyl = Vec3::new(0.5, 0.0, 0.5);
    /// let hit = s.property_at(cyl, CoordSystem::Cylindrical, TypeMask::ANY).unwrap();
    /// assert_eq!(hit.property.name, "low");
    /// # Ok::<(), csx::Error>(())
    /// ```
    pub fn property_at(
        &self,
        p: Vec3,
        cs: CoordSystem,
        mask: impl Into<TypeMask>,
    ) -> Option<Hit<'_>> {
        let mask = mask.into();
        let mut best: Option<Hit<'_>> = None;
        for prop in self
            .properties
            .iter()
            .filter(|q| mask.contains(q.property_type()))
        {
            for (i, prim) in prop.primitives.iter().enumerate() {
                if best.is_some_and(|b| b.primitive.priority >= prim.priority) {
                    continue;
                }
                if prim.contains_in(p, cs, self.coord_system) {
                    best = Some(Hit {
                        property: prop,
                        primitive: prim,
                        primitive_index: i,
                    });
                }
            }
        }
        best
    }

    /// Every primitive (of properties matching `mask`) that contains `p`,
    /// highest priority first; ties keep insertion order. The first entry is
    /// the result of [`Self::property_at`].
    pub fn properties_at(
        &self,
        p: Vec3,
        cs: CoordSystem,
        mask: impl Into<TypeMask>,
    ) -> Vec<Hit<'_>> {
        let mask = mask.into();
        let mut hits: Vec<Hit<'_>> = Vec::new();
        for prop in self
            .properties
            .iter()
            .filter(|q| mask.contains(q.property_type()))
        {
            for (i, prim) in prop.primitives.iter().enumerate() {
                if prim.contains_in(p, cs, self.coord_system) {
                    hits.push(Hit {
                        property: prop,
                        primitive: prim,
                        primitive_index: i,
                    });
                }
            }
        }
        // stable sort keeps insertion order, so earlier ones win ties
        hits.sort_by_key(|h| core::cmp::Reverse(h.primitive.priority));
        hits
    }

    /// The physical medium at `p`: the highest-priority material/metal
    /// primitive, or the background material. Metals are reported as
    /// `None`. Weighting functions are evaluated at the Cartesian position
    /// of `p`.
    ///
    /// ```
    /// use csx::primitives::BoxShape;
    /// use csx::properties::Material;
    /// use csx::{ContinuousStructure, CoordSystem, Property, Vec3};
    ///
    /// let mut m = Material::new(2.0, 0.01);
    /// m.epsilon.set_weight("1 + x"); // ε grows along x
    /// let mut s = ContinuousStructure::new();
    /// let id = s.add_property(Property::material("graded", m));
    /// s.add_primitive(id, BoxShape::new([0.0; 3], [1.0; 3]))?;
    ///
    /// let med = s.medium_at(Vec3::new(0.5, 0.5, 0.5), CoordSystem::Cartesian).unwrap();
    /// assert_eq!(med.epsilon, [3.0; 3]);
    /// assert_eq!(med.kappa, [0.01; 3]);
    /// // outside: background (vacuum)
    /// let air = s.medium_at(Vec3::new(5.0, 0.0, 0.0), CoordSystem::Cartesian).unwrap();
    /// assert_eq!(air.epsilon, [1.0; 3]);
    /// # Ok::<(), csx::Error>(())
    /// ```
    pub fn medium_at(&self, p: Vec3, cs: CoordSystem) -> Option<Medium> {
        let hit = self.property_at(
            p,
            cs,
            TypeMask::MATERIALS | TypeMask::of(crate::PropertyType::Metal),
        );
        let cart = cs.to_cartesian(p);
        match hit.map(|h| &h.property.kind) {
            Some(PropertyKind::Metal) => None,
            Some(k) if let Some(m) = k.material() => Some(Medium {
                epsilon: m.values_at(MaterialQuantity::Epsilon, cart),
                mue: m.values_at(MaterialQuantity::Mue, cart),
                kappa: m.values_at(MaterialQuantity::Kappa, cart),
                sigma: m.values_at(MaterialQuantity::Sigma, cart),
            }),
            // No hit (the mask only matches materials and metals).
            _ => {
                let b = &self.background;
                Some(Medium {
                    epsilon: [b.epsilon.value(); 3],
                    mue: [b.mue.value(); 3],
                    kappa: [b.kappa.value(); 3],
                    sigma: [b.sigma.value(); 3],
                })
            }
        }
    }

    /// Union of the bounding boxes of all primitives (in `cs`). Unbounded
    /// primitives (user-defined regions) are ignored; an empty structure
    /// gives [`BoundingBox::EMPTY`].
    pub fn bounding_box(&self, cs: CoordSystem) -> BoundingBox {
        self.primitives()
            .map(|(_, q)| q.bounding_box_in(cs, self.coord_system))
            .filter(|b| b.min.is_finite() && b.max.is_finite())
            .fold(BoundingBox::EMPTY, |a, b| a.union(&b))
    }

    /// Primitives (of properties matching `mask`) whose bounding box in `cs`
    /// intersects `region`.
    pub fn primitives_in<'a>(
        &'a self,
        region: &'a BoundingBox,
        cs: CoordSystem,
        mask: impl Into<TypeMask>,
    ) -> impl Iterator<Item = (&'a Property, &'a Primitive)> + 'a {
        let mask = mask.into();
        let dcs = self.coord_system;
        self.primitives()
            .filter(move |(p, _)| mask.contains(p.property_type()))
            .filter(move |(_, q)| q.bounding_box_in(cs, dcs).intersects(region))
    }

    /// Builds an index for fast repeated point queries in coordinate system
    /// `cs`, restricted to properties matching `mask`.
    ///
    /// The index borrows the structure; rebuild it after modifying the
    /// model.
    ///
    /// ```
    /// use csx::primitives::BoxShape;
    /// use csx::{ContinuousStructure, CoordSystem, Property, TypeMask, Vec3};
    ///
    /// let mut s = ContinuousStructure::new();
    /// for i in 0..100 {
    ///     let id = s.add_property(Property::metal(format!("m{i}")));
    ///     let x = i as f64;
    ///     s.add_primitive(id, BoxShape::new([x, 0.0, 0.0], [x + 0.5, 1.0, 1.0]))?;
    /// }
    /// let index = s.spatial_index(CoordSystem::Cartesian, TypeMask::ANY);
    /// assert_eq!(index.query(Vec3::new(42.25, 0.5, 0.5)).unwrap().property.name, "m42");
    /// assert!(index.query(Vec3::new(42.75, 0.5, 0.5)).is_none());
    /// # Ok::<(), csx::Error>(())
    /// ```
    #[must_use]
    pub fn spatial_index(&self, cs: CoordSystem, mask: impl Into<TypeMask>) -> SpatialIndex<'_> {
        SpatialIndex::new(self, cs, mask.into())
    }

    /// Consistency checks: duplicate names, properties without primitives,
    /// primitives with empty bounds. Returns the issues found (empty if the
    /// model is consistent); they are warnings, not errors.
    ///
    /// ```
    /// use csx::primitives::BoxShape;
    /// use csx::{ContinuousStructure, Property};
    ///
    /// let mut s = ContinuousStructure::new();
    /// let id = s.add_property(Property::metal("pec"));
    /// s.add_primitive(id, BoxShape::new([0.0; 3], [1.0; 3]))?;
    /// assert!(s.validate().is_empty());
    ///
    /// s.add_property(Property::metal("pec"));
    /// for issue in s.validate() {
    ///     println!("warning: {issue}");
    /// }
    /// assert_eq!(s.validate().len(), 2); // duplicate name + no primitives
    /// # Ok::<(), csx::Error>(())
    /// ```
    #[must_use]
    pub fn validate(&self) -> Vec<ValidationIssue> {
        let mut w = Vec::new();
        for (i, p) in self.properties.iter().enumerate() {
            if self.properties[..i].iter().any(|q| q.name == p.name) {
                w.push(ValidationIssue::DuplicateName {
                    name: p.name.clone(),
                });
            }
            if p.primitives.is_empty() {
                w.push(ValidationIssue::EmptyProperty {
                    name: p.name.clone(),
                });
            }
            for (k, q) in p.primitives.iter().enumerate() {
                let b = q.bounding_box(self.coord_system);
                if b.is_empty() {
                    w.push(ValidationIssue::EmptyPrimitive {
                        property: p.name.clone(),
                        index: k,
                        shape: q.shape.type_name(),
                    });
                }
            }
        }
        w
    }
}

struct Entry<'a> {
    bbox: BoundingBox,
    hit: Hit<'a>,
}

/// Point-query accelerator over a borrowed structure.
///
/// Primitives are ranked by priority (highest first, ties: first added
/// first) and their bounding boxes are stored in a bounding volume
/// hierarchy, so a query only runs exact inside tests on the few primitives
/// whose boxes contain the point, in rank order.
pub struct SpatialIndex<'a> {
    entries: Vec<Entry<'a>>,
    bvh: Bvh,
    cs: CoordSystem,
    default_cs: CoordSystem,
}

impl core::fmt::Debug for SpatialIndex<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SpatialIndex")
            .field("primitives", &self.entries.len())
            .field("cs", &self.cs)
            .field("default_cs", &self.default_cs)
            .finish_non_exhaustive()
    }
}

impl<'a> SpatialIndex<'a> {
    fn new(s: &'a ContinuousStructure, cs: CoordSystem, mask: TypeMask) -> Self {
        let mut entries: Vec<Entry<'a>> = Vec::new();
        for prop in s
            .properties
            .iter()
            .filter(|q| mask.contains(q.property_type()))
        {
            for (i, prim) in prop.primitives.iter().enumerate() {
                let b = prim.bounding_box(s.coord_system);
                // Pad by a tolerance comparable to the one used by the
                // shapes' own inside tests (relative to size and position).
                let scale = b.size().norm().max(b.min.norm()).max(b.max.norm());
                let bbox = if b.is_empty() {
                    b
                } else {
                    b.expanded(1e-9 * scale)
                };
                entries.push(Entry {
                    bbox,
                    hit: Hit {
                        property: prop,
                        primitive: prim,
                        primitive_index: i,
                    },
                });
            }
        }
        entries.sort_by_key(|e| core::cmp::Reverse(e.hit.primitive.priority));
        let boxes: Vec<BoundingBox> = entries.iter().map(|e| e.bbox).collect();
        SpatialIndex {
            bvh: Bvh::build(&boxes),
            entries,
            cs,
            default_cs: s.coord_system,
        }
    }

    /// The winning property at `p` (expressed in the index's coordinate
    /// system); same semantics as [`ContinuousStructure::property_at`].
    pub fn query(&self, p: Vec3) -> Option<Hit<'a>> {
        let c = self.cs.to_cartesian(p);
        // Candidate ranks (indices into `entries`), kept on the stack when
        // there are few of them.
        let mut buf = [0u32; 32];
        let mut n = 0usize;
        let mut spill: Vec<u32> = Vec::new();
        self.bvh.visit(
            |b| b.contains(c),
            |i| {
                if self.entries[i as usize].bbox.contains(c) {
                    if n < buf.len() {
                        buf[n] = i;
                        n += 1;
                    } else {
                        spill.push(i);
                    }
                }
                true
            },
        );
        let cand: &mut [u32] = if spill.is_empty() {
            &mut buf[..n]
        } else {
            spill.extend_from_slice(&buf[..n]);
            &mut spill
        };
        cand.sort_unstable();
        cand.iter()
            .map(|&i| &self.entries[i as usize])
            .find(|e| e.hit.primitive.contains_in(p, self.cs, self.default_cs))
            .map(|e| e.hit)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::primitives::{BoxShape, Sphere};
    use crate::properties::Material;

    fn model() -> ContinuousStructure {
        let mut s = ContinuousStructure::new();
        let sub = s.add_property(Property::material("sub", Material::new(4.0, 0.0)));
        s.add_primitive(
            sub,
            Primitive::new(BoxShape::new([0.0, 0.0, 0.0], [10.0, 10.0, 1.0])).with_priority(1),
        )
        .unwrap();
        let m = s.add_property(Property::metal("pec"));
        s.add_primitive(
            m,
            Primitive::new(Sphere::new([5.0, 5.0, 1.0], 0.5)).with_priority(5),
        )
        .unwrap();
        s
    }

    /// On equal priority the earlier property wins (verified in CSXCAD).
    #[test]
    fn priority_ties() {
        let mut s = ContinuousStructure::new();
        let a = s.add_property(Property::material("A", Material::new(2.0, 0.0)));
        let b = s.add_property(Property::material("B", Material::new(3.0, 0.0)));
        let unit =
            || Primitive::new(BoxShape::new([0.0, 0.0, 0.0], [1.0, 1.0, 1.0])).with_priority(3);
        s.add_primitive(b, unit()).unwrap();
        s.add_primitive(a, unit()).unwrap();
        let p = Vec3::new(0.5, 0.5, 0.5);
        let hit = s
            .property_at(p, CoordSystem::Cartesian, TypeMask::ANY)
            .unwrap();
        assert_eq!(hit.property.name, "A");
        let all = s.properties_at(p, CoordSystem::Cartesian, TypeMask::ANY);
        assert_eq!(all[0].property.name, "A");
        let idx = s.spatial_index(CoordSystem::Cartesian, TypeMask::ANY);
        assert_eq!(idx.query(p).unwrap().property.name, "A");
    }

    #[test]
    fn priority() {
        let s = model();
        let h = s
            .property_at(
                Vec3::new(5.0, 5.0, 0.8),
                CoordSystem::Cartesian,
                TypeMask::ANY,
            )
            .unwrap();
        assert_eq!(h.property.name, "pec");
        let h = s
            .property_at(
                Vec3::new(1.0, 1.0, 0.5),
                CoordSystem::Cartesian,
                TypeMask::ANY,
            )
            .unwrap();
        assert_eq!(h.property.name, "sub");
        assert!(
            s.property_at(
                Vec3::new(1.0, 1.0, 2.0),
                CoordSystem::Cartesian,
                TypeMask::ANY
            )
            .is_none()
        );
        assert_eq!(
            s.properties_at(
                Vec3::new(5.0, 5.0, 0.8),
                CoordSystem::Cartesian,
                TypeMask::ANY
            )
            .len(),
            2
        );
        let idx = s.spatial_index(CoordSystem::Cartesian, TypeMask::ANY);
        for p in [
            Vec3::new(5.0, 5.0, 0.8),
            Vec3::new(1.0, 1.0, 0.5),
            Vec3::new(1.0, 1.0, 2.0),
        ] {
            assert_eq!(
                idx.query(p).map(|h| &h.property.name),
                s.property_at(p, CoordSystem::Cartesian, TypeMask::ANY)
                    .map(|h| &h.property.name)
            );
        }
    }

    #[test]
    fn media() {
        let s = model();
        assert_eq!(
            s.medium_at(Vec3::new(1.0, 1.0, 0.5), CoordSystem::Cartesian)
                .unwrap()
                .epsilon,
            [4.0; 3]
        );
        assert_eq!(
            s.medium_at(Vec3::new(1.0, 1.0, 5.0), CoordSystem::Cartesian)
                .unwrap()
                .epsilon,
            [1.0; 3]
        );
        assert!(
            s.medium_at(Vec3::new(5.0, 5.0, 1.0), CoordSystem::Cartesian)
                .is_none()
        );
        let b = s.bounding_box(CoordSystem::Cartesian);
        assert_eq!(b.max, Vec3::new(10.0, 10.0, 1.5));
    }

    #[test]
    fn parametric() {
        let mut s = ContinuousStructure::new();
        s.parameters.set("w", 2.0);
        let id = s.add_property(Property::metal("m"));
        s.add_primitive(
            id,
            BoxShape {
                start: [0.0, 0.0, 0.0].into(),
                stop: crate::ScalarPoint(["w".into(), "w*2".into(), 1.0.into()]),
            },
        )
        .unwrap();
        s.update().unwrap();
        assert!(
            s.property_at(
                Vec3::new(1.9, 3.9, 0.5),
                CoordSystem::Cartesian,
                TypeMask::ANY
            )
            .is_some()
        );
        s.parameters.set("w", 1.0);
        s.update().unwrap();
        assert!(
            s.property_at(
                Vec3::new(1.9, 3.9, 0.5),
                CoordSystem::Cartesian,
                TypeMask::ANY
            )
            .is_none()
        );
        assert!(s.validate().is_empty());
        assert!(s.remove_property(id).is_some());
    }

    #[test]
    fn index_matches_linear_scan() {
        use crate::primitives::UserDefined;

        let mut seed = 11u64;
        let mut rnd = move || {
            seed = seed
                .wrapping_mul(6364136223846793005)
                .wrapping_add(1442695040888963407);
            (seed >> 11) as f64 / (1u64 << 53) as f64
        };
        let mut s = ContinuousStructure::new();
        for k in 0..60 {
            let id = s.add_property(Property::material(format!("m{k}"), Material::new(1.0, 0.0)));
            // Several primitives per property, few distinct priorities so
            // that ties are common.
            for _ in 0..1 + k % 3 {
                let c = Vec3::new(rnd() * 20.0, rnd() * 20.0, rnd() * 20.0);
                let d = Vec3::new(rnd() * 4.0, rnd() * 4.0, rnd() * 4.0);
                let prim = if rnd() < 0.7 {
                    Primitive::new(BoxShape::new(c - d, c + d))
                } else {
                    Primitive::new(Sphere::new(c, d[0]))
                };
                s.add_primitive(id, prim.with_priority((rnd() * 4.0) as i32))
                    .unwrap();
            }
        }
        // An unbounded primitive must be found everywhere it applies.
        let u = s.add_property(Property::metal("half"));
        s.add_primitive(
            u,
            Primitive::new(UserDefined::new("z > 15")).with_priority(2),
        )
        .unwrap();
        let idx = s.spatial_index(CoordSystem::Cartesian, TypeMask::ANY);
        for _ in 0..5000 {
            let p = Vec3::new(rnd() * 22.0 - 1.0, rnd() * 22.0 - 1.0, rnd() * 22.0 - 1.0);
            let a = idx.query(p);
            let b = s.property_at(p, CoordSystem::Cartesian, TypeMask::ANY);
            match (a, b) {
                (None, None) => {}
                (Some(a), Some(b)) => {
                    assert!(core::ptr::eq(a.primitive, b.primitive), "point {p:?}");
                    assert_eq!(a.property.id(), b.property.id());
                    assert_eq!(a.primitive_index, b.primitive_index);
                }
                _ => panic!("index and linear scan disagree at {p:?}"),
            }
        }
    }
}