ifc-geometry 0.4.4

IFC semantic views lowered into the format-neutral geometry DAG.
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
//! The flanged catalogue sections: I, L, T, U, Z, C and the asymmetric I.
//!
//! Split from the closed sections purely for file size; the shared
//! helpers and the WHERE-rule discipline are identical.

use ifc_model::{Entity, EntityId, Transaction, Value};

use crate::error::GeometryError;
use crate::slots::section_slot as slot;

use super::super::require_finite;
use super::{
    non_negative, parameterized, positive, put_opt, rule, AsymmetricIDims, CShapeDims, FlangedDims,
    IShapeDims, ProfileHeader,
};

/// The optional radii and slope an I-section may carry.
///
/// Grouped rather than passed positionally: five trailing `Option<f64>`
/// arguments at a call site are indistinguishable from one another, and
/// transposing two of them writes a valid record describing the wrong
/// beam.
#[derive(Debug, Clone, Copy, Default)]
pub struct IShapeExtras {
    /// `FilletRadius`: the web-to-flange transition.
    pub fillet_radius: Option<f64>,
    /// `FlangeEdgeRadius`: the rounding at the flange tip.
    pub flange_edge_radius: Option<f64>,
    /// `FlangeSlope`, in radians.
    pub flange_slope: Option<f64>,
}

/// Stage an `IfcIShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and each of the
/// entity's three WHERE rules: `ValidFlangeThickness`
/// (`2 * FlangeThickness < OverallDepth`), `ValidWebThickness`
/// (`WebThickness < OverallWidth`), and `ValidFilletRadius`, which bounds
/// the fillet by both the web clearance and the flange clearance.
pub fn i_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: IShapeDims,
    extras: IShapeExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCISHAPEPROFILEDEF";
    let IShapeDims {
        width,
        depth,
        web_thickness,
        flange_thickness,
    } = dims;
    positive(T, "OverallWidth", width)?;
    positive(T, "OverallDepth", depth)?;
    positive(T, "WebThickness", web_thickness)?;
    positive(T, "FlangeThickness", flange_thickness)?;
    non_negative(T, "FilletRadius", extras.fillet_radius)?;
    non_negative(T, "FlangeEdgeRadius", extras.flange_edge_radius)?;
    if let Some(slope) = extras.flange_slope {
        require_finite(T, "FlangeSlope", &[slope])?;
    }
    if 2.0 * flange_thickness >= depth {
        return Err(rule(
            T,
            "FlangeThickness",
            format!("2 * {flange_thickness} is not below OverallDepth {depth}"),
        ));
    }
    if web_thickness >= width {
        return Err(rule(
            T,
            "WebThickness",
            format!("{web_thickness} is not below OverallWidth {width}"),
        ));
    }
    if let Some(fillet) = extras.fillet_radius {
        let web_room = (width - web_thickness) / 2.0;
        let flange_room = (depth - 2.0 * flange_thickness) / 2.0;
        if fillet > web_room || fillet > flange_room {
            return Err(rule(
                T,
                "FilletRadius",
                format!("{fillet} exceeds the room left by the web ({web_room}) or flanges ({flange_room})"),
            ));
        }
    }
    let mut attrs = parameterized(T, 10, header);
    attrs[slot::I_WIDTH] = Value::Real(width);
    attrs[slot::I_DEPTH] = Value::Real(depth);
    attrs[slot::I_WEB] = Value::Real(web_thickness);
    attrs[slot::I_FLANGE] = Value::Real(flange_thickness);
    put_opt(&mut attrs, slot::I_FILLET, extras.fillet_radius);
    put_opt(&mut attrs, slot::I_EDGE, extras.flange_edge_radius);
    put_opt(&mut attrs, slot::I_SLOPE, extras.flange_slope);
    Ok(tx.create(Entity::new(T, attrs)))
}
/// The optional radii and slope an L-section may carry.
#[derive(Debug, Clone, Copy, Default)]
pub struct LShapeExtras {
    /// `Width`: absent means a equal-legged angle of side `Depth`.
    pub width: Option<f64>,
    /// `FilletRadius`: the inner corner.
    pub fillet_radius: Option<f64>,
    /// `EdgeRadius`: the leg tips.
    pub edge_radius: Option<f64>,
    /// `LegSlope`, in radians.
    pub leg_slope: Option<f64>,
}

/// Stage an `IfcLShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the
/// `ValidThickness` rule: the thickness must be below the depth, and
/// below the width when one is stated. An equal-legged angle omits
/// `Width`, so the width half of the rule only applies when present.
pub fn l_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    depth: f64,
    thickness: f64,
    extras: LShapeExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCLSHAPEPROFILEDEF";
    positive(T, "Depth", depth)?;
    positive(T, "Thickness", thickness)?;
    if let Some(width) = extras.width {
        positive(T, "Width", width)?;
    }
    non_negative(T, "FilletRadius", extras.fillet_radius)?;
    non_negative(T, "EdgeRadius", extras.edge_radius)?;
    if let Some(slope) = extras.leg_slope {
        require_finite(T, "LegSlope", &[slope])?;
    }
    if thickness >= depth {
        return Err(rule(
            T,
            "Thickness",
            format!("{thickness} is not below Depth {depth}"),
        ));
    }
    if let Some(width) = extras.width {
        if thickness >= width {
            return Err(rule(
                T,
                "Thickness",
                format!("{thickness} is not below Width {width}"),
            ));
        }
    }
    let mut attrs = parameterized(T, 9, header);
    attrs[slot::L_DEPTH] = Value::Real(depth);
    attrs[slot::L_THICKNESS] = Value::Real(thickness);
    put_opt(&mut attrs, slot::L_WIDTH, extras.width);
    put_opt(&mut attrs, slot::L_FILLET, extras.fillet_radius);
    put_opt(&mut attrs, slot::L_EDGE, extras.edge_radius);
    put_opt(&mut attrs, slot::L_SLOPE, extras.leg_slope);
    Ok(tx.create(Entity::new(T, attrs)))
}
/// The optional radii and slopes a T-section may carry.
#[derive(Debug, Clone, Copy, Default)]
pub struct TShapeExtras {
    /// `FilletRadius`: the web-to-flange transition.
    pub fillet_radius: Option<f64>,
    /// `FlangeEdgeRadius`: the flange tips.
    pub flange_edge_radius: Option<f64>,
    /// `WebEdgeRadius`: the free end of the web.
    pub web_edge_radius: Option<f64>,
    /// `WebSlope`, in radians.
    pub web_slope: Option<f64>,
    /// `FlangeSlope`, in radians.
    pub flange_slope: Option<f64>,
}

/// Stage an `IfcTShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the two
/// WHERE rules: `FlangeThickness < Depth` and
/// `WebThickness < FlangeWidth`.
pub fn t_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: FlangedDims,
    extras: TShapeExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCTSHAPEPROFILEDEF";
    let FlangedDims {
        depth,
        flange_width,
        web_thickness,
        flange_thickness,
    } = dims;
    positive(T, "Depth", depth)?;
    positive(T, "FlangeWidth", flange_width)?;
    positive(T, "WebThickness", web_thickness)?;
    positive(T, "FlangeThickness", flange_thickness)?;
    non_negative(T, "FilletRadius", extras.fillet_radius)?;
    non_negative(T, "FlangeEdgeRadius", extras.flange_edge_radius)?;
    non_negative(T, "WebEdgeRadius", extras.web_edge_radius)?;
    for (attribute, slope) in [
        ("WebSlope", extras.web_slope),
        ("FlangeSlope", extras.flange_slope),
    ] {
        if let Some(slope) = slope {
            require_finite(T, attribute, &[slope])?;
        }
    }
    if flange_thickness >= depth {
        return Err(rule(
            T,
            "FlangeThickness",
            format!("{flange_thickness} is not below Depth {depth}"),
        ));
    }
    if web_thickness >= flange_width {
        return Err(rule(
            T,
            "WebThickness",
            format!("{web_thickness} is not below FlangeWidth {flange_width}"),
        ));
    }
    let mut attrs = parameterized(T, 12, header);
    attrs[slot::T_DEPTH] = Value::Real(depth);
    attrs[slot::T_WIDTH] = Value::Real(flange_width);
    attrs[slot::T_WEB] = Value::Real(web_thickness);
    attrs[slot::T_FLANGE] = Value::Real(flange_thickness);
    put_opt(&mut attrs, slot::T_FILLET, extras.fillet_radius);
    put_opt(&mut attrs, slot::T_FLANGE_EDGE, extras.flange_edge_radius);
    put_opt(&mut attrs, slot::T_WEB_EDGE, extras.web_edge_radius);
    put_opt(&mut attrs, slot::T_WEB_SLOPE, extras.web_slope);
    put_opt(&mut attrs, slot::T_FLANGE_SLOPE, extras.flange_slope);
    Ok(tx.create(Entity::new(T, attrs)))
}
/// The optional radii and slope a U-section (channel) may carry.
#[derive(Debug, Clone, Copy, Default)]
pub struct UShapeExtras {
    /// `FilletRadius`: the web-to-flange transition.
    pub fillet_radius: Option<f64>,
    /// `EdgeRadius`: the flange tips.
    pub edge_radius: Option<f64>,
    /// `FlangeSlope`, in radians.
    pub flange_slope: Option<f64>,
}

/// Stage an `IfcUShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the two
/// WHERE rules. Note the flange bound is `Depth / 2`, not `Depth`: a
/// channel has two flanges, so a thickness at half the depth already
/// closes the section.
pub fn u_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: FlangedDims,
    extras: UShapeExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCUSHAPEPROFILEDEF";
    let FlangedDims {
        depth,
        flange_width,
        web_thickness,
        flange_thickness,
    } = dims;
    positive(T, "Depth", depth)?;
    positive(T, "FlangeWidth", flange_width)?;
    positive(T, "WebThickness", web_thickness)?;
    positive(T, "FlangeThickness", flange_thickness)?;
    non_negative(T, "FilletRadius", extras.fillet_radius)?;
    non_negative(T, "EdgeRadius", extras.edge_radius)?;
    if let Some(slope) = extras.flange_slope {
        require_finite(T, "FlangeSlope", &[slope])?;
    }
    if flange_thickness >= depth / 2.0 {
        return Err(rule(
            T,
            "FlangeThickness",
            format!(
                "{flange_thickness} is not below half the Depth ({})",
                depth / 2.0
            ),
        ));
    }
    if web_thickness >= flange_width {
        return Err(rule(
            T,
            "WebThickness",
            format!("{web_thickness} is not below FlangeWidth {flange_width}"),
        ));
    }
    let mut attrs = parameterized(T, 10, header);
    attrs[slot::U_DEPTH] = Value::Real(depth);
    attrs[slot::U_WIDTH] = Value::Real(flange_width);
    attrs[slot::U_WEB] = Value::Real(web_thickness);
    attrs[slot::U_FLANGE] = Value::Real(flange_thickness);
    put_opt(&mut attrs, slot::U_FILLET, extras.fillet_radius);
    put_opt(&mut attrs, slot::U_EDGE, extras.edge_radius);
    put_opt(&mut attrs, slot::U_SLOPE, extras.flange_slope);
    Ok(tx.create(Entity::new(T, attrs)))
}

/// The optional radii a Z-section may carry.
#[derive(Debug, Clone, Copy, Default)]
pub struct ZShapeExtras {
    /// `FilletRadius`: the web-to-flange transitions.
    pub fillet_radius: Option<f64>,
    /// `EdgeRadius`: the flange tips.
    pub edge_radius: Option<f64>,
}

/// Stage an `IfcZShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the
/// `ValidFlangeThickness` rule (`FlangeThickness < Depth / 2`). A Z has
/// two opposed flanges, so the bound is half the depth.
pub fn z_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: FlangedDims,
    extras: ZShapeExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCZSHAPEPROFILEDEF";
    let FlangedDims {
        depth,
        flange_width,
        web_thickness,
        flange_thickness,
    } = dims;
    positive(T, "Depth", depth)?;
    positive(T, "FlangeWidth", flange_width)?;
    positive(T, "WebThickness", web_thickness)?;
    positive(T, "FlangeThickness", flange_thickness)?;
    non_negative(T, "FilletRadius", extras.fillet_radius)?;
    non_negative(T, "EdgeRadius", extras.edge_radius)?;
    if flange_thickness >= depth / 2.0 {
        return Err(rule(
            T,
            "FlangeThickness",
            format!(
                "{flange_thickness} is not below half the Depth ({})",
                depth / 2.0
            ),
        ));
    }
    let mut attrs = parameterized(T, 9, header);
    attrs[slot::Z_DEPTH] = Value::Real(depth);
    attrs[slot::Z_FLANGE_WIDTH] = Value::Real(flange_width);
    attrs[slot::Z_WEB] = Value::Real(web_thickness);
    attrs[slot::Z_FLANGE] = Value::Real(flange_thickness);
    put_opt(&mut attrs, slot::Z_FILLET, extras.fillet_radius);
    put_opt(&mut attrs, slot::Z_EDGE, extras.edge_radius);
    Ok(tx.create(Entity::new(T, attrs)))
}
/// Stage an `IfcCShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the three
/// WHERE rules: `Girth < Depth / 2`, `WallThickness` below half of both
/// `Width` and `Depth`, and `InternalFilletRadius` within the room the
/// wall leaves on both axes.
pub fn c_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: CShapeDims,
    internal_fillet_radius: Option<f64>,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCCSHAPEPROFILEDEF";
    let CShapeDims {
        depth,
        width,
        wall_thickness,
        girth,
    } = dims;
    positive(T, "Depth", depth)?;
    positive(T, "Width", width)?;
    positive(T, "WallThickness", wall_thickness)?;
    positive(T, "Girth", girth)?;
    non_negative(T, "InternalFilletRadius", internal_fillet_radius)?;
    if girth >= depth / 2.0 {
        return Err(rule(
            T,
            "Girth",
            format!("{girth} is not below half the Depth ({})", depth / 2.0),
        ));
    }
    if wall_thickness >= width / 2.0 || wall_thickness >= depth / 2.0 {
        return Err(rule(
            T,
            "WallThickness",
            format!(
                "{wall_thickness} is not below half of both Width ({}) and Depth ({})",
                width / 2.0,
                depth / 2.0
            ),
        ));
    }
    if let Some(fillet) = internal_fillet_radius {
        let across_width = width / 2.0 - wall_thickness;
        let across_depth = depth / 2.0 - wall_thickness;
        if fillet > across_width || fillet > across_depth {
            return Err(rule(
                T,
                "InternalFilletRadius",
                format!(
                    "{fillet} exceeds the room the wall leaves ({across_width} by {across_depth})"
                ),
            ));
        }
    }
    let mut attrs = parameterized(T, 8, header);
    attrs[slot::C_DEPTH] = Value::Real(depth);
    attrs[slot::C_WIDTH] = Value::Real(width);
    attrs[slot::C_WALL] = Value::Real(wall_thickness);
    attrs[slot::C_GIRTH] = Value::Real(girth);
    put_opt(&mut attrs, slot::C_FILLET, internal_fillet_radius);
    Ok(tx.create(Entity::new(T, attrs)))
}
/// The optional attributes an asymmetric I-section may carry.
///
/// `TopFlangeThickness` is optional in the schema; when absent the top
/// flange is taken to match the bottom, and `ValidFlangeThickness` does
/// not apply.
#[derive(Debug, Clone, Copy, Default)]
pub struct AsymmetricIExtras {
    /// `TopFlangeThickness`.
    pub top_flange_thickness: Option<f64>,
    /// `BottomFlangeFilletRadius`.
    pub bottom_flange_fillet_radius: Option<f64>,
    /// `TopFlangeFilletRadius`.
    pub top_flange_fillet_radius: Option<f64>,
    /// `BottomFlangeEdgeRadius`.
    pub bottom_flange_edge_radius: Option<f64>,
    /// `TopFlangeEdgeRadius`.
    pub top_flange_edge_radius: Option<f64>,
    /// `BottomFlangeSlope`, in radians.
    pub bottom_flange_slope: Option<f64>,
    /// `TopFlangeSlope`, in radians.
    pub top_flange_slope: Option<f64>,
}

/// Stage an `IfcAsymmetricIShapeProfileDef`.
///
/// # Errors
///
/// Refuses a non-positive dimension, a negative radius, and the four
/// WHERE rules: the web must be narrower than both flanges, each fillet
/// is bounded by its own flange's clearance, and the two flange
/// thicknesses together must stay below the overall depth when a top
/// thickness is stated.
pub fn asymmetric_i_shape(
    tx: &mut Transaction,
    header: ProfileHeader<'_>,
    dims: AsymmetricIDims,
    extras: AsymmetricIExtras,
) -> Result<EntityId, GeometryError> {
    const T: &str = "IFCASYMMETRICISHAPEPROFILEDEF";
    let AsymmetricIDims {
        bottom_flange_width,
        overall_depth,
        web_thickness,
        bottom_flange_thickness,
        top_flange_width,
    } = dims;
    positive(T, "BottomFlangeWidth", bottom_flange_width)?;
    positive(T, "OverallDepth", overall_depth)?;
    positive(T, "WebThickness", web_thickness)?;
    positive(T, "BottomFlangeThickness", bottom_flange_thickness)?;
    positive(T, "TopFlangeWidth", top_flange_width)?;
    if let Some(top) = extras.top_flange_thickness {
        positive(T, "TopFlangeThickness", top)?;
    }
    non_negative(
        T,
        "BottomFlangeFilletRadius",
        extras.bottom_flange_fillet_radius,
    )?;
    non_negative(T, "TopFlangeFilletRadius", extras.top_flange_fillet_radius)?;
    non_negative(
        T,
        "BottomFlangeEdgeRadius",
        extras.bottom_flange_edge_radius,
    )?;
    non_negative(T, "TopFlangeEdgeRadius", extras.top_flange_edge_radius)?;
    for (attribute, slope) in [
        ("BottomFlangeSlope", extras.bottom_flange_slope),
        ("TopFlangeSlope", extras.top_flange_slope),
    ] {
        if let Some(slope) = slope {
            require_finite(T, attribute, &[slope])?;
        }
    }
    if web_thickness >= bottom_flange_width || web_thickness >= top_flange_width {
        return Err(rule(
            T,
            "WebThickness",
            format!(
                "{web_thickness} is not below both flange widths ({bottom_flange_width}, {top_flange_width})"
            ),
        ));
    }
    if let Some(top) = extras.top_flange_thickness {
        if bottom_flange_thickness + top >= overall_depth {
            return Err(rule(
                T,
                "TopFlangeThickness",
                format!(
                    "{bottom_flange_thickness} + {top} is not below OverallDepth {overall_depth}"
                ),
            ));
        }
    }
    for (attribute, fillet, width) in [
        (
            "BottomFlangeFilletRadius",
            extras.bottom_flange_fillet_radius,
            bottom_flange_width,
        ),
        (
            "TopFlangeFilletRadius",
            extras.top_flange_fillet_radius,
            top_flange_width,
        ),
    ] {
        if let Some(fillet) = fillet {
            let room = (width - web_thickness) / 2.0;
            if fillet > room {
                return Err(rule(
                    T,
                    attribute,
                    format!("{fillet} exceeds the flange clearance ({room})"),
                ));
            }
        }
    }
    let mut attrs = parameterized(T, 15, header);
    attrs[slot::AI_BOTTOM_WIDTH] = Value::Real(bottom_flange_width);
    attrs[slot::AI_DEPTH] = Value::Real(overall_depth);
    attrs[slot::AI_WEB] = Value::Real(web_thickness);
    attrs[slot::AI_BOTTOM_FLANGE] = Value::Real(bottom_flange_thickness);
    attrs[slot::AI_TOP_WIDTH] = Value::Real(top_flange_width);
    put_opt(&mut attrs, slot::AI_TOP_FLANGE, extras.top_flange_thickness);
    put_opt(
        &mut attrs,
        slot::AI_BOTTOM_FILLET,
        extras.bottom_flange_fillet_radius,
    );
    put_opt(
        &mut attrs,
        slot::AI_TOP_FILLET,
        extras.top_flange_fillet_radius,
    );
    put_opt(
        &mut attrs,
        slot::AI_BOTTOM_EDGE,
        extras.bottom_flange_edge_radius,
    );
    put_opt(&mut attrs, slot::AI_TOP_EDGE, extras.top_flange_edge_radius);
    put_opt(
        &mut attrs,
        slot::AI_BOTTOM_SLOPE,
        extras.bottom_flange_slope,
    );
    put_opt(&mut attrs, slot::AI_TOP_SLOPE, extras.top_flange_slope);
    Ok(tx.create(Entity::new(T, attrs)))
}