File indexing completed on 2026-11-26 09:25:02
0001 import gc
0002 import weakref
0003
0004 import numpy as np
0005 import pytest
0006
0007 import acts
0008
0009 alignment = pytest.importorskip("acts.examples.alignment")
0010
0011
0012 def test_structure_without_frame():
0013 structure = alignment.AlignableStructure(acts.GeometryIdentifier(volume=1))
0014 assert not structure.hasTransform
0015 with pytest.raises(RuntimeError, match="no frame provider"):
0016 structure.localToGlobalTransform(
0017 acts.GeometryContext.dangerouslyDefaultConstruct()
0018 )
0019 with pytest.raises(ValueError, match="provider is empty"):
0020 alignment.AlignableStructure(
0021 acts.GeometryIdentifier(volume=1), transformProvider=None
0022 )
0023
0024
0025 def test_structure_fixed_frame():
0026 translation = [12.0, -8.0, 30.0]
0027 rotation = np.array([[0.0, -1.0, 0.0], [1.0, 0.0, 0.0], [0.0, 0.0, 1.0]])
0028 frame = acts.Transform3(
0029 acts.Vector3(*translation),
0030 acts.RotationMatrix3(*(acts.Vector3(*rotation[:, i]) for i in range(3))),
0031 )
0032 structure = alignment.AlignableStructure(
0033 acts.GeometryIdentifier(volume=1), transform=frame
0034 )
0035 del frame
0036 gc.collect()
0037 assert structure.hasTransform
0038 resolved = structure.localToGlobalTransform(
0039 acts.GeometryContext.dangerouslyDefaultConstruct()
0040 )
0041 np.testing.assert_allclose(resolved.translation, translation)
0042 np.testing.assert_allclose(
0043 [[resolved.rotation[i, j] for j in range(3)] for i in range(3)], rotation
0044 )
0045
0046
0047 def test_structure_owns_frame_provider():
0048 initial_translation = [1.0, 2.0, 3.0]
0049 updated_translation = [4.0, 5.0, 6.0]
0050
0051 class Provider:
0052 def __init__(self):
0053 self.frame = acts.Transform3(acts.Vector3(*initial_translation))
0054
0055 def __call__(self, gctx):
0056 assert isinstance(gctx, acts.GeometryContext)
0057 return self.frame
0058
0059 provider = Provider()
0060 retained = weakref.ref(provider)
0061 structure = alignment.AlignableStructure(
0062 acts.GeometryIdentifier(volume=1), transformProvider=provider
0063 )
0064 del provider
0065 gc.collect()
0066 assert retained() is not None
0067 context = acts.GeometryContext.dangerouslyDefaultConstruct()
0068 first = structure.localToGlobalTransform(context)
0069 retained().frame = acts.Transform3(acts.Vector3(*updated_translation))
0070 second = structure.localToGlobalTransform(context)
0071 np.testing.assert_allclose(first.translation, initial_translation)
0072 np.testing.assert_allclose(second.translation, updated_translation)
0073 del structure
0074 gc.collect()
0075 assert retained() is None
0076
0077
0078 @pytest.mark.parametrize("initial_kind", ["missing", "fixed"])
0079 def test_structure_set_transform(initial_kind):
0080 context = acts.GeometryContext.dangerouslyDefaultConstruct()
0081 structure = alignment.AlignableStructure(acts.GeometryIdentifier(volume=1))
0082 fixed_translation = [12.0, -8.0, 30.0]
0083 provider_translation = [-4.0, 5.0, 6.0]
0084
0085 with pytest.raises(ValueError, match="provider is empty"):
0086 structure.setTransform(transformProvider=None)
0087 assert not structure.hasTransform
0088
0089 if initial_kind == "fixed":
0090 frame = acts.Transform3(acts.Vector3(*fixed_translation))
0091 structure.setTransform(transform=frame)
0092 del frame
0093 gc.collect()
0094 assert structure.hasTransform
0095 np.testing.assert_allclose(
0096 structure.localToGlobalTransform(context).translation, fixed_translation
0097 )
0098
0099 class Provider:
0100 def __call__(self, gctx):
0101 assert isinstance(gctx, acts.GeometryContext)
0102 return acts.Transform3(acts.Vector3(*provider_translation))
0103
0104 provider = Provider()
0105 retained = weakref.ref(provider)
0106 structure.setTransform(transformProvider=provider)
0107 del provider
0108 gc.collect()
0109 assert retained() is not None
0110 assert structure.hasTransform
0111 np.testing.assert_allclose(
0112 structure.localToGlobalTransform(context).translation, provider_translation
0113 )
0114
0115 with pytest.raises(ValueError, match="provider is empty"):
0116 structure.setTransform(transformProvider=None)
0117 assert retained() is not None
0118 np.testing.assert_allclose(
0119 structure.localToGlobalTransform(context).translation, provider_translation
0120 )
0121
0122 structure.setTransform(transform=acts.Transform3(acts.Vector3(*fixed_translation)))
0123 gc.collect()
0124 assert retained() is None
0125 with pytest.raises(ValueError, match="provider is empty"):
0126 structure.setTransform(transformProvider=None)
0127 assert structure.hasTransform
0128 np.testing.assert_allclose(
0129 structure.localToGlobalTransform(context).translation, fixed_translation
0130 )