ifc_geometry/curve/
conic.rs1use crate::error::GeometryResult;
24use crate::resource::placement::Axis2Placement;
25use crate::resource::resolve;
26use crate::slots::Slots;
27use ifc_model::{Entity, EntityId, Model};
28
29pub(crate) mod circle_slot {
34 pub const POSITION: usize = 0;
36 pub const RADIUS: usize = 1;
38}
39
40pub(crate) mod ellipse_slot {
44 pub const POSITION: usize = 0;
46 pub const SEMI_AXIS_1: usize = 1;
48 pub const SEMI_AXIS_2: usize = 2;
50}
51
52#[derive(Debug, Clone, Copy)]
54pub struct Circle<'m> {
55 slots: Slots<'m>,
56}
57
58impl<'m> Circle<'m> {
59 pub fn new(id: EntityId, entity: &'m Entity) -> Self {
61 Self {
62 slots: Slots::new(id, entity),
63 }
64 }
65
66 pub fn id(&self) -> EntityId {
68 self.slots.id()
69 }
70
71 pub fn position_ref(&self) -> GeometryResult<EntityId> {
78 self.slots.req_ref(circle_slot::POSITION, "Position")
79 }
80
81 pub fn position<'v>(&self, model: &'v Model) -> GeometryResult<Axis2Placement<'v>> {
86 resolve::axis2_placement(model, self.id(), self.position_ref()?)
87 }
88
89 pub fn radius(&self) -> GeometryResult<f64> {
94 let r = self.slots.req_f64(circle_slot::RADIUS, "Radius")?;
95 if r > 0.0 {
96 Ok(r)
97 } else {
98 Err(self
99 .slots
100 .degenerate(format!("Radius must be positive, found {r}")))
101 }
102 }
103}
104
105#[derive(Debug, Clone, Copy)]
107pub struct Ellipse<'m> {
108 slots: Slots<'m>,
109}
110
111impl<'m> Ellipse<'m> {
112 pub fn new(id: EntityId, entity: &'m Entity) -> Self {
114 Self {
115 slots: Slots::new(id, entity),
116 }
117 }
118
119 pub fn id(&self) -> EntityId {
121 self.slots.id()
122 }
123
124 pub fn position_ref(&self) -> GeometryResult<EntityId> {
126 self.slots.req_ref(ellipse_slot::POSITION, "Position")
127 }
128
129 pub fn position<'v>(&self, model: &'v Model) -> GeometryResult<Axis2Placement<'v>> {
134 resolve::axis2_placement(model, self.id(), self.position_ref()?)
135 }
136
137 pub fn semi_axis_1(&self) -> GeometryResult<f64> {
139 self.positive(ellipse_slot::SEMI_AXIS_1, "SemiAxis1")
140 }
141
142 pub fn semi_axis_2(&self) -> GeometryResult<f64> {
146 self.positive(ellipse_slot::SEMI_AXIS_2, "SemiAxis2")
147 }
148
149 pub fn semi_axes(&self) -> GeometryResult<(f64, f64)> {
154 Ok((self.semi_axis_1()?, self.semi_axis_2()?))
155 }
156
157 fn positive(&self, index: usize, name: &'static str) -> GeometryResult<f64> {
158 let v = self.slots.req_f64(index, name)?;
159 if v > 0.0 {
160 Ok(v)
161 } else {
162 Err(self
163 .slots
164 .degenerate(format!("{name} must be positive, found {v}")))
165 }
166 }
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172 use ifc_model::Value;
173
174 fn circle(radius: Value) -> Entity {
175 Entity::new("IFCCIRCLE", vec![Value::Ref(EntityId(5)), radius])
176 }
177
178 fn ellipse(a: f64, b: f64) -> Entity {
179 Entity::new(
180 "IFCELLIPSE",
181 vec![Value::Ref(EntityId(5)), Value::Real(a), Value::Real(b)],
182 )
183 }
184
185 fn placement_model(type_name: &str) -> Model {
186 let mut model = Model::new();
187 let coords = Value::List(vec![Value::Real(1.0), Value::Real(2.0)]);
188 model.insert(EntityId(4), Entity::new("IFCCARTESIANPOINT", vec![coords]));
189 let placement = Entity::new(type_name, vec![Value::Ref(EntityId(4))]);
190 model.insert(EntityId(5), placement);
191 model
192 }
193
194 #[test]
196 fn conic_positions_resolve_to_the_select_member_the_file_wrote() {
197 let e = circle(Value::Real(1.0));
198 let model = placement_model("IFCAXIS2PLACEMENT2D");
199 let position = Circle::new(EntityId(1), &e).position(&model).unwrap();
200 assert!(matches!(position, Axis2Placement::TwoD(_)));
201 assert_eq!(position.location(&model).unwrap(), [1.0, 2.0, 0.0]);
202
203 let e = ellipse(2.0, 1.0);
204 let model = placement_model("IFCAXIS2PLACEMENT3D");
205 let position = Ellipse::new(EntityId(1), &e).position(&model).unwrap();
206 assert!(matches!(position, Axis2Placement::ThreeD(_)));
207 let transform = position.transform(&model).unwrap();
208 assert_eq!(transform.apply([0.0; 3]), [1.0, 2.0, 0.0]);
209 }
210
211 #[test]
213 fn a_conic_placed_by_an_axis1_placement_is_a_typed_error() {
214 let model = placement_model("IFCAXIS1PLACEMENT");
215 let e = circle(Value::Real(1.0));
216 let err = Circle::new(EntityId(1), &e).position(&model).unwrap_err();
217 assert!(matches!(
218 err,
219 crate::GeometryError::WrongEntityType {
220 entity: EntityId(5),
221 ..
222 }
223 ));
224 let e = ellipse(2.0, 1.0);
225 let err = Ellipse::new(EntityId(1), &e)
226 .position(&Model::new())
227 .unwrap_err();
228 assert_eq!(err.entity(), Some(EntityId(1)));
229 }
230
231 #[test]
232 fn circle_reads_position_and_radius_from_conic_inherited_slots() {
233 let e = circle(Value::Real(2.5));
234 let view = Circle::new(EntityId(1), &e);
235 assert_eq!(view.position_ref().unwrap(), EntityId(5));
236 assert_eq!(view.radius().unwrap(), 2.5);
237 }
238
239 #[test]
242 fn circle_radius_reads_through_a_positive_length_measure_wrapper() {
243 let e = circle(Value::Typed {
244 type_name: "IFCPOSITIVELENGTHMEASURE".into(),
245 value: Box::new(Value::Real(3.0)),
246 });
247 assert_eq!(Circle::new(EntityId(1), &e).radius().unwrap(), 3.0);
248 }
249
250 #[test]
251 fn zero_radius_circle_is_degenerate_not_a_zero_length_curve() {
252 let e = circle(Value::Real(0.0));
253 let err = Circle::new(EntityId(9), &e).radius().unwrap_err();
254 assert!(err.to_string().contains("#9"), "got: {err}");
255 assert!(err.to_string().contains("positive"), "got: {err}");
256 assert!(
257 !err.is_unsupported(),
258 "a bad radius is corruption, not a gap"
259 );
260 }
261
262 #[test]
263 fn negative_radius_circle_is_degenerate() {
264 let e = circle(Value::Real(-1.0));
265 assert!(Circle::new(EntityId(1), &e).radius().is_err());
266 }
267
268 #[test]
269 fn ellipse_returns_both_semi_axes_in_declaration_order() {
270 let e = ellipse(4.0, 2.0);
271 assert_eq!(
272 Ellipse::new(EntityId(1), &e).semi_axes().unwrap(),
273 (4.0, 2.0)
274 );
275 }
276
277 #[test]
280 fn ellipse_accepts_a_second_semi_axis_larger_than_the_first() {
281 let e = ellipse(1.0, 7.0);
282 assert_eq!(
283 Ellipse::new(EntityId(1), &e).semi_axes().unwrap(),
284 (1.0, 7.0)
285 );
286 }
287
288 #[test]
289 fn zero_semi_axis_is_degenerate_and_names_which_one() {
290 let e = ellipse(4.0, 0.0);
291 let err = Ellipse::new(EntityId(1), &e).semi_axis_2().unwrap_err();
292 assert!(err.to_string().contains("SemiAxis2"), "got: {err}");
293 }
294}