ifc_georef/context/
chain.rs1use axiolid_core::Transform3;
20
21use crate::conversion::ProjectToMap;
22use crate::error::{GeorefError, GeorefResult};
23
24pub fn compose_project_frame(
39 operation: &ProjectToMap,
40 project_frame: Transform3,
41) -> GeorefResult<Transform3> {
42 let determinant = project_frame.matrix3.determinant();
43 if !determinant.is_finite() || determinant.abs() <= f64::EPSILON {
44 return Err(GeorefError::DegenerateProjectFrame);
45 }
46 Ok(operation.transform * project_frame)
47}
48
49#[cfg(test)]
50mod tests {
51 use std::sync::Arc;
52
53 use axiolid_core::{Mat3, Point3, Vec3};
54 use ifc_model::value::Value;
55 use ifc_model::{Entity, EntityId, Model};
56
57 use super::*;
58 use crate::conversion::resolve_project_to_map;
59
60 fn id(value: u64) -> EntityId {
61 EntityId(value)
62 }
63
64 fn r(value: u64) -> Value {
65 Value::Ref(id(value))
66 }
67
68 fn real(value: f64) -> Value {
69 Value::Real(value)
70 }
71
72 fn model_with_map_conversion() -> Model {
73 let mut model = Model::new();
74 model.insert(
75 id(1),
76 Entity::new("IFCGEOMETRICREPRESENTATIONCONTEXT", vec![]),
77 );
78 model.insert(
79 id(2),
80 Entity::new(
81 "IFCPROJECTEDCRS",
82 vec![Value::Text(Arc::from("EPSG:25832"))],
83 ),
84 );
85 model.insert(
86 id(4),
87 Entity::new(
88 "IFCMAPCONVERSION",
89 vec![
90 r(1),
91 r(2),
92 real(1000.0),
93 real(2000.0),
94 real(50.0),
95 Value::Null,
96 Value::Null,
97 real(1.0),
98 ],
99 ),
100 );
101 model
102 }
103
104 #[test]
105 fn composes_a_translated_project_frame_into_map_coordinates() {
106 let model = model_with_map_conversion();
107 let operation = resolve_project_to_map(&model, id(4), 1.0).expect("resolves");
108
109 let project_frame =
112 Transform3::from_mat3_translation(Mat3::IDENTITY, Vec3::new(10.0, 20.0, 0.0));
113 let map_frame = compose_project_frame(&operation, project_frame).expect("composes");
114
115 let mapped = map_frame.transform_point3(Point3::ZERO);
116 assert_eq!(mapped, Vec3::new(1010.0, 2020.0, 50.0));
117 }
118
119 #[test]
120 fn refuses_a_singular_project_frame() {
121 let model = model_with_map_conversion();
122 let operation = resolve_project_to_map(&model, id(4), 1.0).expect("resolves");
123
124 let singular = Transform3::from_mat3_translation(
126 Mat3::from_cols(
127 Vec3::new(1.0, 0.0, 0.0),
128 Vec3::ZERO,
129 Vec3::new(0.0, 0.0, 1.0),
130 ),
131 Vec3::ZERO,
132 );
133
134 assert!(matches!(
135 compose_project_frame(&operation, singular),
136 Err(GeorefError::DegenerateProjectFrame)
137 ));
138 }
139
140 #[test]
141 fn composition_order_applies_the_project_frame_before_the_map_operation() {
142 let model = model_with_map_conversion();
143 let operation = resolve_project_to_map(&model, id(4), 1.0).expect("resolves");
144
145 let rotated = Transform3::from_mat3_translation(
147 Mat3::from_cols(Vec3::new(0.0, 1.0, 0.0), Vec3::new(-1.0, 0.0, 0.0), Vec3::Z),
148 Vec3::new(5.0, 0.0, 0.0),
149 );
150 let map_frame = compose_project_frame(&operation, rotated).expect("composes");
151
152 let mapped = map_frame.transform_point3(Point3::new(1.0, 0.0, 0.0));
155 assert_eq!(mapped, Vec3::new(1005.0, 2001.0, 50.0));
156 }
157}