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
#![warn(clippy::all)]
#![warn(missing_docs)]
#![warn(missing_doc_code_examples)]
#![warn(clippy::missing_docs_in_private_items)]

//! This package provides utilities for designing and analyzing truss structures

/// This enum contains different structural shapes
#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum StructuralShape {
    /// This is a pipe with an outer_radius and a thickness
    Pipe {
        /// Outer radius of hte pipe
        outer_radius: f64,
        /// Thickness of the pipe wall
        thickness: f64,
    },
    /// This is an I-Beam, with a width, height, web thickness, and flange thickness
    IBeam {
        /// Width of the beam
        width: f64,
        /// Height of the beam
        height: f64,
        /// Thickness of the web
        web_thickness: f64,
        /// Thickness of the flange
        flange_thickness: f64,
    },
    /// This is a box beam with a width, height, and thickness
    BoxBeam {
        /// Width of the box beam
        width: f64,
        /// Height of the box beam
        height: f64,
        /// Thickness of the wall
        thickness: f64,
    },
    /// This is a rod with a radius only
    Rod {
        /// Radius of the road
        radius: f64,
    },
    /// This is a solid rectangular with width and height
    Rectangle {
        /// Width of the rectangle
        width: f64,
        /// Height of the rectangle
        height: f64,
    },
}

impl StructuralShape {
    /// This function returns the moment of inertia of the structural shape around the x-axis
    /// ```
    /// use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::Rod{radius: 2.0};
    /// let area = shape.moment_of_inertia_x();
    /// ```
    pub fn moment_of_inertia_x(&self) -> f64 {
        match self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
            } => {
                std::f64::consts::PI * (outer_radius.powi(4) - (outer_radius - thickness).powi(4))
                    / 4.0
            }
            StructuralShape::IBeam {
                width,
                height,
                flange_thickness,
                web_thickness,
            } => {
                width * height.powi(3) / 12.0
                    - 2.0
                        * ((width - web_thickness) / 2.0)
                        * (height - 2.0 * flange_thickness).powi(3)
                        / 12.0
            }
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
            } => {
                width * height.powi(3) / 12.0
                    - (width - thickness) * (height - thickness).powi(3) / 12.0
            }
            StructuralShape::Rod { radius } => std::f64::consts::PI * radius.powi(4) / 4.0,
            StructuralShape::Rectangle { width, height } => width * height.powi(3) / 12.0,
        }
    }

    /// This function returns the moment of inertia of hte structural shape around the y-axis
    /// ```
    /// use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::Rod{radius: 2.0};
    /// let area = shape.moment_of_inertia_y();
    /// ```
    pub fn moment_of_inertia_y(&self) -> f64 {
        match self {
            StructuralShape::Pipe { .. } => self.moment_of_inertia_x(),
            StructuralShape::IBeam { .. } => 0.0,
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
            } => {
                height * width.powi(3) / 12.0
                    - (height - thickness) * (width - thickness).powi(3) / 12.0
            }
            StructuralShape::Rod { .. } => self.moment_of_inertia_x(),
            StructuralShape::Rectangle { .. } => self.moment_of_inertia_x(),
        }
    }

    /// This function returns the cross-sectional area of the structural shape
    /// ```
    /// use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::Rod{radius: 2.0};
    /// let area = shape.area();
    /// ```
    pub fn area(&self) -> f64 {
        match *self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
            } => std::f64::consts::PI * (outer_radius.powi(2) - (outer_radius - thickness).powi(2)),
            StructuralShape::IBeam {
                width,
                height,
                web_thickness,
                flange_thickness,
            } => width * height - (height - 2.0 * flange_thickness) * (width - web_thickness),
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
            } => width * height - (width - thickness) * (height - thickness),
            StructuralShape::Rod { radius } => std::f64::consts::PI * radius.powi(2),
            StructuralShape::Rectangle { width, height } => width * height,
        }
    }
}