section-properties 0.4.0

Structural cross-section properties: geometric/principal moments, section moduli, warping/torsion FEM, plastic, cold-formed, and fire analysis.
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
//! Cold-formed steel sections (EN 1993-1-3, AISI S100, GB/T 6723, etc.).
//!
//! Includes lipped channels, Z-sections, hat sections, and custom profiles.

use crate::geometry::{Point, Polygon};
use crate::material::Material;
use crate::section::Section;
use crate::section_library::ParametricSection;

/// Cold-formed lipped channel (C-section with lips).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LippedChannel {
    pub depth: f64,        // h - overall depth
    pub flange_width: f64, // b - flange width
    pub lip_length: f64,   // c - lip length
    pub thickness: f64,    // t - thickness
    pub inner_radius: f64, // r - inner bend radius
    pub outer_radius: f64, // r + t
}

impl LippedChannel {
    pub fn new(
        depth: f64,
        flange_width: f64,
        lip_length: f64,
        thickness: f64,
        inner_radius: f64,
    ) -> Self {
        assert!(depth > 0.0 && flange_width > 0.0 && lip_length >= 0.0 && thickness > 0.0);
        assert!(
            2.0 * thickness < depth && thickness < flange_width && thickness < lip_length + 1e-9
        );
        Self {
            depth,
            flange_width,
            lip_length,
            thickness,
            inner_radius,
            outer_radius: inner_radius + thickness,
        }
    }

    /// Standard sizes per EN 1993-1-3 / GB/T 6723
    pub fn from_designation(designation: &str) -> Option<Self> {
        // Format: "LC{d}h{b}l{c}t{t}r{r}" or standard codes
        let dims = match designation.to_uppercase().as_str() {
            // GB/T 6723 Cold-formed equal-leg lipped channels
            "LC80X40X15X2.0" => (80.0, 40.0, 15.0, 2.0, 3.0),
            "LC100X50X15X2.0" => (100.0, 50.0, 15.0, 2.0, 3.0),
            "LC100X50X20X2.5" => (100.0, 50.0, 20.0, 2.5, 4.0),
            "LC120X50X20X2.5" => (120.0, 50.0, 20.0, 2.5, 4.0),
            "LC140X60X20X3.0" => (140.0, 60.0, 20.0, 3.0, 4.5),
            "LC150X50X20X2.5" => (150.0, 50.0, 20.0, 2.5, 4.0),
            "LC160X60X20X3.0" => (160.0, 60.0, 20.0, 3.0, 4.5),
            "LC180X60X25X3.0" => (180.0, 60.0, 25.0, 3.0, 4.5),
            "LC200X70X25X3.0" => (200.0, 70.0, 25.0, 3.0, 5.0),
            "LC200X75X25X3.0" => (200.0, 75.0, 25.0, 3.0, 5.0),
            "LC220X70X25X3.0" => (220.0, 70.0, 25.0, 3.0, 5.0),
            "LC250X75X30X3.0" => (250.0, 75.0, 30.0, 3.0, 5.0),
            "LC300X90X30X4.0" => (300.0, 90.0, 30.0, 4.0, 6.0),

            // AISI standard lipped channels
            "LC350S162-33" => (88.9, 41.1, 12.7, 0.84, 2.5), // 3.5" x 1.625" x 0.125" x 33mil
            "LC350S162-43" => (88.9, 41.1, 12.7, 1.09, 3.3),
            "LC350S162-54" => (88.9, 41.1, 12.7, 1.37, 4.1),
            "LC350S162-68" => (88.9, 41.1, 12.7, 1.73, 5.2),
            "LC350S162-97" => (88.9, 41.1, 12.7, 2.46, 7.4),
            "LC550S162-43" => (139.7, 41.1, 12.7, 1.09, 3.3),
            "LC550S162-54" => (139.7, 41.1, 12.7, 1.37, 4.1),
            "LC550S162-68" => (139.7, 41.1, 12.7, 1.73, 5.2),
            "LC800S162-54" => (203.2, 41.1, 12.7, 1.37, 4.1),
            "LC800S162-68" => (203.2, 41.1, 12.7, 1.73, 5.2),
            "LC1000S162-68" => (254.0, 41.1, 12.7, 1.73, 5.2),
            "LC1200S162-68" => (304.8, 41.1, 12.7, 1.73, 5.2),

            // Wide flange lipped channels
            "LC200S200-54" => (50.8, 50.8, 15.0, 1.37, 4.1),
            "LC250S200-68" => (63.5, 50.8, 15.0, 1.73, 5.2),

            _ => return None,
        };

        let (h, b, c, t, r) = dims;
        Some(Self::new(
            h / 1000.0,
            b / 1000.0,
            c / 1000.0,
            t / 1000.0,
            r / 1000.0,
        ))
    }
}

impl ParametricSection for LippedChannel {
    fn build(&self) -> Section {
        let h = self.depth;
        let b = self.flange_width;
        let c = self.lip_length;
        let t = self.thickness;
        let _r = self.inner_radius;

        let hh = h / 2.0;

        // Lipped channel: web on left (x ∈ [0, t]), flanges extend RIGHT.
        // Lips fold inward at flange outer ends (x = b).
        // CCW outline starting from web outer bottom-left.
        let mut vertices = Vec::new();

        vertices.push(Point::new(0.0, -hh));
        vertices.push(Point::new(0.0, hh));
        vertices.push(Point::new(b, hh));
        if c > 0.0 {
            vertices.push(Point::new(b, hh - t - c));
            vertices.push(Point::new(b - t, hh - t - c));
            vertices.push(Point::new(b - t, hh - t));
        } else {
            vertices.push(Point::new(b, hh - t));
        }
        vertices.push(Point::new(t, hh - t));
        vertices.push(Point::new(t, -hh + t));
        if c > 0.0 {
            vertices.push(Point::new(b - t, -hh + t));
            vertices.push(Point::new(b - t, -hh + t + c));
            vertices.push(Point::new(b, -hh + t + c));
        } else {
            vertices.push(Point::new(b, -hh + t));
        }
        vertices.push(Point::new(b, -hh));

        let mut clean = Vec::new();
        for v in vertices {
            if clean.last() != Some(&v) {
                clean.push(v);
            }
        }
        if clean.len() > 1 && clean[0] == clean[clean.len() - 1] {
            clean.pop();
        }

        Section::new(Polygon::new(clean), Vec::new())
    }

    fn designation(&self) -> String {
        format!(
            "LC{:.0}x{:.0}x{:.0}x{:.1}",
            self.depth * 1000.0,
            self.flange_width * 1000.0,
            self.lip_length * 1000.0,
            self.thickness * 1000.0
        )
    }
}

/// Cold-formed Z-section (Z-purlin).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ZSection {
    pub depth: f64,
    pub flange_width: f64,
    pub lip_length: f64,
    pub thickness: f64,
    pub inner_radius: f64,
}

impl ZSection {
    pub fn new(
        depth: f64,
        flange_width: f64,
        lip_length: f64,
        thickness: f64,
        inner_radius: f64,
    ) -> Self {
        assert!(depth > 0.0 && flange_width > 0.0 && lip_length >= 0.0 && thickness > 0.0);
        Self {
            depth,
            flange_width,
            lip_length,
            thickness,
            inner_radius,
        }
    }

    pub fn from_designation(designation: &str) -> Option<Self> {
        let dims = match designation.to_uppercase().as_str() {
            // Common Z-purlin sizes (mm)
            "Z100X50X15X2.0" => (100.0, 50.0, 15.0, 2.0, 3.0),
            "Z120X50X15X2.0" => (120.0, 50.0, 15.0, 2.0, 3.0),
            "Z140X60X20X2.5" => (140.0, 60.0, 20.0, 2.5, 4.0),
            "Z150X50X20X2.5" => (150.0, 50.0, 20.0, 2.5, 4.0),
            "Z160X60X20X3.0" => (160.0, 60.0, 20.0, 3.0, 4.5),
            "Z180X60X25X3.0" => (180.0, 60.0, 25.0, 3.0, 4.5),
            "Z200X70X25X3.0" => (200.0, 70.0, 25.0, 3.0, 5.0),
            "Z200X75X25X3.0" => (200.0, 75.0, 25.0, 3.0, 5.0),
            "Z250X75X30X3.0" => (250.0, 75.0, 30.0, 3.0, 5.0),
            "Z300X90X30X4.0" => (300.0, 90.0, 30.0, 4.0, 6.0),

            // AISI Z-sections
            "Z350S162-33" => (88.9, 41.1, 12.7, 0.84, 2.5),
            "Z350S162-43" => (88.9, 41.1, 12.7, 1.09, 3.3),
            "Z350S162-54" => (88.9, 41.1, 12.7, 1.37, 4.1),
            "Z550S162-54" => (139.7, 41.1, 12.7, 1.37, 4.1),
            "Z800S162-68" => (203.2, 41.1, 12.7, 1.73, 5.2),

            _ => return None,
        };

        let (h, b, c, t, r) = dims;
        Some(Self::new(
            h / 1000.0,
            b / 1000.0,
            c / 1000.0,
            t / 1000.0,
            r / 1000.0,
        ))
    }
}

impl ParametricSection for ZSection {
    fn build(&self) -> Section {
        let h = self.depth;
        let b = self.flange_width;
        let c = self.lip_length;
        let t = self.thickness;
        let _r = self.inner_radius;

        let hh = h / 2.0;

        // Z-section: top flange extends LEFT from web, bottom flange extends RIGHT.
        // Web: x ∈ [0, t], y ∈ [-hh, hh]
        // Top flange: x ∈ [-b, 0], y ∈ [hh-t, hh]
        // Bottom flange: x ∈ [t, t+b], y ∈ [-hh, -hh+t]
        // CCW outline starting from top-left outer corner.
        let mut vertices = Vec::new();

        vertices.push(Point::new(-b, hh));
        vertices.push(Point::new(0.0, hh));
        vertices.push(Point::new(t, hh));
        vertices.push(Point::new(t, -hh + t));
        if c > 0.0 {
            vertices.push(Point::new(b, -hh + t));
            vertices.push(Point::new(b, -hh + t + c));
            vertices.push(Point::new(t + b, -hh + t + c));
        } else {
            vertices.push(Point::new(t + b, -hh + t));
        }
        vertices.push(Point::new(t + b, -hh));
        vertices.push(Point::new(t, -hh));
        vertices.push(Point::new(0.0, -hh));
        vertices.push(Point::new(0.0, hh - t));
        if c > 0.0 {
            vertices.push(Point::new(-b + t, hh - t));
            vertices.push(Point::new(-b + t, hh - t - c));
            vertices.push(Point::new(-b, hh - t - c));
        } else {
            vertices.push(Point::new(-b, hh - t));
        }

        let mut clean = Vec::new();
        for v in vertices {
            if clean.last() != Some(&v) {
                clean.push(v);
            }
        }
        if clean.len() > 1 && clean[0] == clean[clean.len() - 1] {
            clean.pop();
        }

        Section::new(Polygon::new(clean), Vec::new())
    }

    fn designation(&self) -> String {
        format!(
            "Z{:.0}x{:.0}x{:.0}x{:.1}",
            self.depth * 1000.0,
            self.flange_width * 1000.0,
            self.lip_length * 1000.0,
            self.thickness * 1000.0
        )
    }
}

/// Cold-formed hat section (omega section).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HatSection {
    pub depth: f64,
    pub top_flange: f64,
    pub bottom_flange: f64,
    pub lip_length: f64,
    pub thickness: f64,
    pub inner_radius: f64,
}

impl HatSection {
    pub fn new(
        depth: f64,
        top_flange: f64,
        bottom_flange: f64,
        lip_length: f64,
        thickness: f64,
        inner_radius: f64,
    ) -> Self {
        assert!(depth > 0.0 && top_flange > 0.0 && bottom_flange > 0.0 && thickness > 0.0);
        Self {
            depth,
            top_flange,
            bottom_flange,
            lip_length,
            thickness,
            inner_radius,
        }
    }
}

impl ParametricSection for HatSection {
    fn build(&self) -> Section {
        let h = self.depth;
        let bt = self.top_flange;
        let bb = self.bottom_flange;
        let c = self.lip_length;
        let t = self.thickness;

        let hh = h / 2.0;

        // Hat section outer boundary: traces the full perimeter of the section
        // including webs as solid material. The enclosed region between the webs
        // is carved out as a hole.
        //
        // Path (CCW): bottom flange bottom → right lip → right web outer face
        //   → top flange outer face → left web outer face → left lip → close
        let mut outer = Vec::new();

        // Bottom flange: bottom edge (left to right)
        outer.push(Point::new(-bb / 2.0, -hh));
        outer.push(Point::new(bb / 2.0, -hh));

        // Right lip (folds upward from bottom flange outer edge)
        if c > 0.0 {
            outer.push(Point::new(bb / 2.0, -hh + t + c));
            outer.push(Point::new(bb / 2.0 - t, -hh + t + c));
            outer.push(Point::new(bb / 2.0 - t, -hh + t));
        }

        // Right web: outer edge (bottom to top) — rises from bottom flange to top flange
        outer.push(Point::new(bt / 2.0, -hh + t));
        outer.push(Point::new(bt / 2.0, hh - t));

        // Top flange: outer face (right to left)
        outer.push(Point::new(bt / 2.0, hh));
        outer.push(Point::new(-bt / 2.0, hh));
        outer.push(Point::new(-bt / 2.0, hh - t));

        // Left web: outer edge (top to bottom) — descends from top flange to bottom flange
        outer.push(Point::new(-bt / 2.0, -hh + t));

        // Left lip (folds upward from bottom flange outer edge)
        if c > 0.0 {
            outer.push(Point::new(-bb / 2.0 + t, -hh + t));
            outer.push(Point::new(-bb / 2.0 + t, -hh + t + c));
            outer.push(Point::new(-bb / 2.0, -hh + t + c));
        }

        // Bottom flange: top edge (right to left) — closes the polygon
        outer.push(Point::new(-bb / 2.0, -hh));

        let mut clean = Vec::new();
        for v in outer {
            if clean.last() != Some(&v) {
                clean.push(v);
            }
        }
        if clean.len() > 1 && clean[0] == clean[clean.len() - 1] {
            clean.pop();
        }

        // Enclosed region between the webs (bounded by web inner faces)
        let mut holes = Vec::new();
        let hole = vec![
            Point::new(-bt / 2.0 + t, -hh + t),
            Point::new(bt / 2.0 - t, -hh + t),
            Point::new(bt / 2.0 - t, hh - t),
            Point::new(-bt / 2.0 + t, hh - t),
        ];
        holes.push(Polygon::new(hole));

        Section::new(Polygon::new(clean), holes)
    }

    fn designation(&self) -> String {
        format!(
            "HAT{:.0}x{:.0}x{:.0}x{:.0}x{:.1}",
            self.depth * 1000.0,
            self.top_flange * 1000.0,
            self.bottom_flange * 1000.0,
            self.lip_length * 1000.0,
            self.thickness * 1000.0
        )
    }
}

/// Cold-formed sigma section (Σ-section).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SigmaSection {
    pub depth: f64,
    pub top_flange: f64,
    pub bottom_flange: f64,
    pub top_lip: f64,
    pub bottom_lip: f64,
    pub thickness: f64,
    pub inner_radius: f64,
}

impl SigmaSection {
    pub fn new(
        depth: f64,
        top_flange: f64,
        bottom_flange: f64,
        top_lip: f64,
        bottom_lip: f64,
        thickness: f64,
        inner_radius: f64,
    ) -> Self {
        assert!(depth > 0.0 && top_flange > 0.0 && bottom_flange > 0.0 && thickness > 0.0);
        Self {
            depth,
            top_flange,
            bottom_flange,
            top_lip,
            bottom_lip,
            thickness,
            inner_radius,
        }
    }
}

impl ParametricSection for SigmaSection {
    fn build(&self) -> Section {
        let h = self.depth;
        let bt = self.top_flange;
        let bb = self.bottom_flange;
        let ct = self.top_lip;
        let cb = self.bottom_lip;
        let t = self.thickness;

        let hh = h / 2.0;

        // Sigma section outer boundary: traces the full perimeter including webs
        // as solid material. Bottom lips fold upward from bottom flange outer edges.
        // Top lips fold downward from top flange into the enclosed region (hole).
        // The enclosed region between the webs (including top lips) is carved out as a hole.
        //
        // Path (CCW): bottom flange bottom → right bottom lip → right web outer face
        //   → top flange outer face → left web outer face → left bottom lip → close
        let mut outer = Vec::new();

        // Bottom flange: bottom edge (left to right)
        outer.push(Point::new(-bb / 2.0, -hh));
        outer.push(Point::new(bb / 2.0, -hh));

        // Right bottom lip (folds upward from bottom flange outer edge)
        if cb > 0.0 {
            outer.push(Point::new(bb / 2.0, -hh + t + cb));
            outer.push(Point::new(bb / 2.0 - t, -hh + t + cb));
            outer.push(Point::new(bb / 2.0 - t, -hh + t));
        }

        // Right web: outer edge (bottom to top)
        outer.push(Point::new(bt / 2.0, -hh + t));
        outer.push(Point::new(bt / 2.0, hh - t));

        // Top flange: outer face (right to left)
        outer.push(Point::new(bt / 2.0, hh));
        outer.push(Point::new(-bt / 2.0, hh));
        outer.push(Point::new(-bt / 2.0, hh - t));

        // Left web: outer edge (top to bottom)
        outer.push(Point::new(-bt / 2.0, -hh + t));

        // Left bottom lip (folds upward from bottom flange outer edge)
        if cb > 0.0 {
            outer.push(Point::new(-bb / 2.0 + t, -hh + t));
            outer.push(Point::new(-bb / 2.0 + t, -hh + t + cb));
            outer.push(Point::new(-bb / 2.0, -hh + t + cb));
        }

        // Bottom flange: top edge (right to left) — closes the polygon
        outer.push(Point::new(-bb / 2.0, -hh));

        let mut clean = Vec::new();
        for v in outer {
            if clean.last() != Some(&v) {
                clean.push(v);
            }
        }
        if clean.len() > 1 && clean[0] == clean[clean.len() - 1] {
            clean.pop();
        }

        // Enclosed region between the webs, including top lip notches.
        // The top lips fold downward from the top flange into this region.
        let mut holes = Vec::new();
        if ct > 0.0 {
            let hole = vec![
                Point::new(-bt / 2.0 + t, -hh + t),
                Point::new(bt / 2.0 - t, -hh + t),
                Point::new(bt / 2.0 - t, hh - t),
                Point::new(bt / 2.0, hh - t),
                Point::new(bt / 2.0, hh - t - ct),
                Point::new(bt / 2.0 - t, hh - t - ct),
                Point::new(-bt / 2.0 + t, hh - t - ct),
                Point::new(-bt / 2.0, hh - t - ct),
                Point::new(-bt / 2.0, hh - t),
                Point::new(-bt / 2.0 + t, hh - t),
            ];
            holes.push(Polygon::new(hole));
        } else {
            let hole = vec![
                Point::new(-bt / 2.0 + t, -hh + t),
                Point::new(bt / 2.0 - t, -hh + t),
                Point::new(bt / 2.0 - t, hh - t),
                Point::new(-bt / 2.0 + t, hh - t),
            ];
            holes.push(Polygon::new(hole));
        }

        Section::new(Polygon::new(clean), holes)
    }

    fn designation(&self) -> String {
        format!(
            "SIGMA{:.0}x{:.0}x{:.0}x{:.0}x{:.0}x{:.1}",
            self.depth * 1000.0,
            self.top_flange * 1000.0,
            self.bottom_flange * 1000.0,
            self.top_lip * 1000.0,
            self.bottom_lip * 1000.0,
            self.thickness * 1000.0
        )
    }
}

/// Cold-formed deck profile (trapezoidal sheet).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DeckProfile {
    pub depth: f64,        // h - profile height
    pub pitch: f64,        // w - pitch (width of one wave)
    pub top_width: f64,    // top flat width
    pub bottom_width: f64, // bottom flat width
    pub thickness: f64,
    pub web_angle: f64, // web angle from horizontal (degrees)
}

impl DeckProfile {
    pub fn new(
        depth: f64,
        pitch: f64,
        top_width: f64,
        bottom_width: f64,
        thickness: f64,
        web_angle: f64,
    ) -> Self {
        assert!(depth > 0.0 && pitch > 0.0 && thickness > 0.0);
        assert!(top_width >= 0.0 && bottom_width >= 0.0);
        assert!(web_angle > 0.0 && web_angle < 90.0);
        Self {
            depth,
            pitch,
            top_width,
            bottom_width,
            thickness,
            web_angle,
        }
    }

    pub fn reentrant(
        depth: f64,
        pitch: f64,
        top_width: f64,
        bottom_width: f64,
        thickness: f64,
        _reentrant_depth: f64,
    ) -> Self {
        // Reentrant profile has stiffener in web
        Self::new(depth, pitch, top_width, bottom_width, thickness, 45.0)
    }
}

impl ParametricSection for DeckProfile {
    fn build(&self) -> Section {
        let h = self.depth;
        let w = self.pitch;
        let wb = self.bottom_width;
        let _t = self.thickness;
        let alpha = self.web_angle.to_radians();

        let hh = h / 2.0;

        // Trapezoidal deck profile: outer polygon traces the corrugated
        // cross-section boundary. The web runs from the bottom flange edge
        // inward (toward center) to the top flange edge at web_angle
        // from horizontal. Bottom flange is wider, top flange is narrower.
        let web_run = h / alpha.tan();
        let x0 = -w / 2.0;
        let x1 = x0 + wb;
        let x2r = x1 - web_run;
        let x2l = x0 + web_run;

        // Outer polygon: full perimeter of one corrugation wave (CCW)
        let outer = vec![
            Point::new(x0, -hh), // bottom flange left
            Point::new(x1, -hh), // bottom flange right
            Point::new(x2r, hh), // top flange right (via right web)
            Point::new(x2l, hh), // top flange left (via left web)
        ];

        Section::new(Polygon::new(outer), Vec::new())
    }

    fn designation(&self) -> String {
        format!(
            "DECK{:.0}x{:.0}x{:.0}x{:.0}x{:.1}",
            self.depth * 1000.0,
            self.pitch * 1000.0,
            self.top_width * 1000.0,
            self.bottom_width * 1000.0,
            self.thickness * 1000.0
        )
    }
}

impl DeckProfile {
    /// Effective section properties per unit width (for slab design).
    pub fn effective_properties_per_width(
        &self,
        _material: &Material,
    ) -> crate::section_properties::SectionProperties {
        // For deck profiles, properties are typically given per meter width
        let sec = self.build();
        let props = crate::section_properties::SectionProperties::from_section(&sec);

        // Scale to per meter width
        let scale = 1.0 / self.pitch;
        crate::section_properties::SectionProperties {
            geometric: crate::section_properties::GeometricProperties {
                area: props.area * scale,
                centroid: props.centroid,
                ix: props.ix * scale,
                iy: props.iy * scale,
                ixy: props.ixy * scale,
                max_fiber_distance_y: props.max_fiber_distance_y,
                max_fiber_distance_x: props.max_fiber_distance_x,
                zxx_plus: props.zxx_plus * scale,
                zxx_minus: props.zxx_minus * scale,
                zyy_plus: props.zyy_plus * scale,
                zyy_minus: props.zyy_minus * scale,
                perimeter: props.perimeter * scale,
                qx: props.qx * scale,
                qy: props.qy * scale,
                ixx_g: props.ixx_g * scale,
                iyy_g: props.iyy_g * scale,
                ixy_g: props.ixy_g * scale,
            },
            principal: crate::section_properties::PrincipalProperties {
                i11: props.principal.i11 * scale,
                i22: props.principal.i22 * scale,
                phi: props.principal.phi,
                z11_plus: props.principal.z11_plus * scale,
                z11_minus: props.principal.z11_minus * scale,
                z22_plus: props.principal.z22_plus * scale,
                z22_minus: props.principal.z22_minus * scale,
            },
            gyration: crate::section_properties::GyrationProperties {
                rx: props.gyration.rx,
                ry: props.gyration.ry,
                r11: props.gyration.r11,
                r22: props.gyration.r22,
                polar: props.gyration.polar,
            },
        }
    }
}

/// Custom cold-formed profile from arbitrary points.
#[derive(Debug, Clone)]
pub struct CustomColdFormed {
    pub vertices: Vec<Point>,
    pub holes: Vec<Vec<Point>>,
    pub thickness: f64,
    pub material: Option<Material>,
}

impl CustomColdFormed {
    pub fn new(vertices: Vec<Point>, thickness: f64) -> Self {
        Self {
            vertices,
            holes: Vec::new(),
            thickness,
            material: None,
        }
    }

    pub fn with_holes(mut self, holes: Vec<Vec<Point>>) -> Self {
        self.holes = holes;
        self
    }

    pub fn with_material(mut self, material: Material) -> Self {
        self.material = Some(material);
        self
    }
}

impl ParametricSection for CustomColdFormed {
    fn build(&self) -> Section {
        let outer = Polygon::new(self.vertices.clone());
        let holes: Vec<Polygon> = self.holes.iter().map(|h| Polygon::new(h.clone())).collect();
        Section::new(outer, holes)
    }

    fn designation(&self) -> String {
        "CUSTOM".to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::material::presets::STEEL_S355;

    #[test]
    fn lipped_channel_basic() {
        let lc = LippedChannel::new(0.2, 0.07, 0.015, 0.002, 0.003);
        let sec = lc.build();
        assert!(sec.area() > 0.0);

        let props = crate::section_properties::SectionProperties::from_section(&sec);
        assert!(props.ix > 0.0);
        assert!(props.iy > 0.0);
    }

    #[test]
    fn lipped_channel_area() {
        let h = 0.2;
        let b = 0.07;
        let c = 0.015;
        let t = 0.002;
        let lc = LippedChannel::new(h, b, c, t, 0.003);
        let sec = lc.build();
        let area = sec.area();
        let expected = t * (h + 2.0 * b + 2.0 * c - 2.0 * t);
        assert!(
            (area - expected).abs() / expected < 1e-6,
            "LippedChannel area: got {}, expected {}",
            area,
            expected
        );
    }

    #[test]
    fn lipped_channel_gb() {
        let lc = LippedChannel::from_designation("LC200X70X25X3.0").unwrap();
        assert!((lc.depth - 0.2).abs() < 1e-6);
        assert!((lc.flange_width - 0.07).abs() < 1e-6);
        assert!((lc.lip_length - 0.025).abs() < 1e-6);
        assert!((lc.thickness - 0.003).abs() < 1e-6);
    }

    #[test]
    fn lipped_channel_aisi() {
        let lc = LippedChannel::from_designation("LC550S162-54").unwrap();
        assert!((lc.depth - 0.1397).abs() < 1e-4);
        assert!((lc.thickness - 0.00137).abs() < 1e-5);
    }

    #[test]
    fn z_section_basic() {
        let z = ZSection::new(0.2, 0.07, 0.015, 0.002, 0.003);
        let sec = z.build();
        assert!(sec.area() > 0.0);

        // Z-section should have Ix > Iy typically
        let props = crate::section_properties::SectionProperties::from_section(&sec);
        assert!(props.ix > 0.0);
    }

    #[test]
    fn z_section_area_check() {
        let h = 0.2;
        let b = 0.07;
        let c = 0.015;
        let t = 0.002;
        let z = ZSection::new(h, b, c, t, 0.003);
        let sec = z.build();
        let area = sec.area();
        let expected = t * (h + 2.0 * b + 2.0 * c);
        assert!(
            (area - expected).abs() / expected < 1e-6,
            "Cold-formed Z area: got {}, expected {}",
            area,
            expected
        );
    }

    #[test]
    fn z_section_gb() {
        let z = ZSection::from_designation("Z200X75X25X3.0").unwrap();
        assert!((z.depth - 0.2).abs() < 1e-6);
    }

    #[test]
    fn hat_section() {
        let hat = HatSection::new(0.15, 0.05, 0.08, 0.015, 0.002, 0.003);
        let sec = hat.build();
        assert!(sec.area() > 0.0);
    }

    #[test]
    fn hat_section_area() {
        let h = 0.15;
        let bt = 0.05;
        let bb = 0.08;
        let c = 0.015;
        let t = 0.002;
        let hat = HatSection::new(h, bt, bb, c, t, 0.003);
        let sec = hat.build();
        let area = sec.area();
        let expected = bb * t + 2.0 * t * (h - 2.0 * t) + bt * t + 2.0 * c * t;
        assert!(
            (area - expected).abs() / expected < 1e-6,
            "HatSection area: got {}, expected {}",
            area,
            expected
        );
    }

    #[test]
    fn sigma_section() {
        let sigma = SigmaSection::new(0.2, 0.06, 0.08, 0.02, 0.02, 0.002, 0.003);
        let sec = sigma.build();
        assert!(sec.area() > 0.0);
    }

    #[test]
    fn sigma_section_area() {
        let h = 0.2;
        let bt = 0.06;
        let bb = 0.08;
        let ct = 0.02;
        let cb = 0.02;
        let t = 0.002;
        let sigma = SigmaSection::new(h, bt, bb, ct, cb, t, 0.003);
        let sec = sigma.build();
        let area = sec.area();

        // `build()` constructs a SOLID section: a single outer polygon with the
        // enclosed region between the webs carved out as one hole. The material
        // area must therefore equal outer area minus the hole(s).
        let outer: f64 = sec.outer.area();
        let holes: f64 = sec.holes.iter().map(|h| h.area()).sum();
        assert!(
            (area - (outer - holes)).abs() < 1e-9,
            "area {} should equal outer {} minus holes {}",
            area,
            outer,
            holes
        );
        assert!(area > 0.0);

        // The idealized thin-wall (centerline) area is a lower bound on the
        // material enclosed by the solid drawing.
        let thin_wall = bb * t + 2.0 * t * cb + 2.0 * t * (h - 2.0 * t) + bt * t + 2.0 * ct * t;
        assert!(
            area >= thin_wall - 1e-12,
            "solid area {} must not be less than thin-wall material {}",
            area,
            thin_wall
        );
    }

    #[test]
    fn deck_profile() {
        let deck = DeckProfile::new(0.05, 0.3, 0.1, 0.15, 0.001, 45.0);
        let sec = deck.build();
        assert!(sec.area() > 0.0);

        let props = deck.effective_properties_per_width(&STEEL_S355);
        assert!(props.area > 0.0);
        assert!(props.ix > 0.0);
    }

    #[test]
    fn deck_profile_area() {
        let h = 0.05;
        let wt = 0.1;
        let wb = 0.15;
        let t = 0.001;
        let web_angle = 45.0_f64;
        let deck = DeckProfile::new(h, 0.3, wt, wb, t, web_angle);
        let sec = deck.build();
        let area = sec.area();
        // Trapezoidal polygon area: (bottom_width + top_width) / 2 * height
        // top_width = wb - 2 * web_run, web_run = h / tan(angle)
        let alpha = web_angle.to_radians();
        let web_run = h / alpha.tan();
        let top_w = wb - 2.0 * web_run;
        let expected = 0.5 * (wb + top_w) * h;
        assert!(
            (area - expected).abs() / expected < 1e-6,
            "DeckProfile area: got {}, expected {}",
            area,
            expected
        );
    }

    #[test]
    fn custom_cold_formed() {
        let vertices = vec![
            Point::new(0.0, 0.0),
            Point::new(0.1, 0.0),
            Point::new(0.1, 0.1),
            Point::new(0.0, 0.1),
        ];
        let custom = CustomColdFormed::new(vertices, 0.002);
        let sec = custom.build();
        assert!((sec.area() - 0.01).abs() < 1e-10);
    }
}