ifc_geometry/authoring/
spiral.rs1use ifc_model::{Entity, EntityId, Transaction, Value};
14
15use crate::error::GeometryError;
16
17use super::{invalid, require_finite};
18
19#[derive(Debug, Clone, Copy, PartialEq)]
25pub enum SpiralKind {
26 Clothoid {
28 constant: f64,
30 },
31 Cosine {
33 cosine: f64,
35 constant: Option<f64>,
37 },
38 Sine {
40 sine: f64,
42 linear: Option<f64>,
44 constant: Option<f64>,
46 },
47 SecondOrder {
49 quadratic: f64,
51 linear: Option<f64>,
53 constant: Option<f64>,
55 },
56 ThirdOrder {
58 cubic: f64,
60 quadratic: Option<f64>,
62 linear: Option<f64>,
64 constant: Option<f64>,
66 },
67 SeventhOrder {
69 septic: f64,
71 lower: [Option<f64>; 7],
73 },
74}
75
76impl SpiralKind {
77 fn entity_name(self) -> &'static str {
79 match self {
80 Self::Clothoid { .. } => "IFCCLOTHOID",
81 Self::Cosine { .. } => "IFCCOSINESPIRAL",
82 Self::Sine { .. } => "IFCSINESPIRAL",
83 Self::SecondOrder { .. } => "IFCSECONDORDERPOLYNOMIALSPIRAL",
84 Self::ThirdOrder { .. } => "IFCTHIRDORDERPOLYNOMIALSPIRAL",
85 Self::SeventhOrder { .. } => "IFCSEVENTHORDERPOLYNOMIALSPIRAL",
86 }
87 }
88
89 fn leading_attribute(self) -> &'static str {
91 match self {
92 Self::Clothoid { .. } => "ClothoidConstant",
93 Self::Cosine { .. } => "CosineTerm",
94 Self::Sine { .. } => "SineTerm",
95 Self::SecondOrder { .. } => "QuadraticTerm",
96 Self::ThirdOrder { .. } => "CubicTerm",
97 Self::SeventhOrder { .. } => "SepticTerm",
98 }
99 }
100
101 fn terms(self) -> (f64, Vec<Option<f64>>) {
103 match self {
104 Self::Clothoid { constant } => (constant, Vec::new()),
105 Self::Cosine { cosine, constant } => (cosine, vec![constant]),
106 Self::Sine {
107 sine,
108 linear,
109 constant,
110 } => (sine, vec![linear, constant]),
111 Self::SecondOrder {
112 quadratic,
113 linear,
114 constant,
115 } => (quadratic, vec![linear, constant]),
116 Self::ThirdOrder {
117 cubic,
118 quadratic,
119 linear,
120 constant,
121 } => (cubic, vec![quadratic, linear, constant]),
122 Self::SeventhOrder { septic, lower } => (septic, lower.to_vec()),
123 }
124 }
125}
126
127pub fn spiral(
135 tx: &mut Transaction,
136 position: EntityId,
137 kind: SpiralKind,
138) -> Result<EntityId, GeometryError> {
139 let entity = kind.entity_name();
140 let (leading, lower) = kind.terms();
141
142 require_finite(entity, kind.leading_attribute(), &[leading])?;
143 if leading == 0.0 {
144 return Err(invalid(
145 entity,
146 kind.leading_attribute(),
147 "expected a non-zero leading term",
148 ));
149 }
150 let present: Vec<f64> = lower.iter().flatten().copied().collect();
151 require_finite(entity, "term", &present)?;
152
153 let mut attrs = vec![Value::Ref(position), Value::Real(leading)];
154 attrs.extend(
155 lower
156 .into_iter()
157 .map(|t| t.map_or(Value::Null, Value::Real)),
158 );
159 Ok(tx.create(Entity::new(entity, attrs)))
160}