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
#![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.moi_x();
    /// ```
    pub fn moi_x(&self) -> f64 {
        match self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
            } => {
                StructuralShape::Rod {
                    radius: *outer_radius,
                }
                .moi_x()
                    - StructuralShape::Rod {
                        radius: (outer_radius - thickness),
                    }
                    .moi_x()
            }
            StructuralShape::IBeam {
                width,
                height,
                flange_thickness,
                web_thickness,
            } => {
                StructuralShape::Rectangle {
                    width: *width,
                    height: *height,
                }
                .moi_x()
                    - 2.0
                        * StructuralShape::Rectangle {
                            width: ((width - web_thickness) / 2.0),
                            height: (height - 2.0 * flange_thickness),
                        }
                        .moi_x()
            }
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
            } => {
                StructuralShape::Rectangle {
                    width: *width,
                    height: *height,
                }
                .moi_x()
                    - StructuralShape::Rectangle {
                        width: (width - 2.0 * thickness),
                        height: (height - 2.0 * thickness),
                    }
                    .moi_x()
            }
            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.moi_y();
    /// ```
    pub fn moi_y(&self) -> f64 {
        match self {
            StructuralShape::Pipe { .. } => self.moi_x(),
            StructuralShape::IBeam {
                height,
                width,
                flange_thickness,
                web_thickness,
            } => {
                2.0 * StructuralShape::Rectangle {
                    height: *width,
                    width: *flange_thickness,
                }
                .moi_x()
                    + StructuralShape::Rectangle {
                        height: *web_thickness,
                        width: (height - 2.0 * flange_thickness),
                    }
                    .moi_x()
            }
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
            } => StructuralShape::BoxBeam {
                width: *height,
                height: *width,
                thickness: *thickness,
            }
            .moi_x(),
            StructuralShape::Rod { .. } => self.moi_x(),
            StructuralShape::Rectangle { .. } => self.moi_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 - 2.0 * thickness) * (height - 2.0 * thickness),
            StructuralShape::Rod { radius } => std::f64::consts::PI * radius.powi(2),
            StructuralShape::Rectangle { width, height } => width * height,
        }
    }

    /// This function returns the moment of intertia of the structural shape around the x-axis when
    /// displaced perpendicular to the axis by a distance d
    /// ```
    /// use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::Rod{radius: 2.0};
    /// let area = shape.moi_x_d(2.0);
    /// ```
    pub fn moi_x_d(&self, d: f64) -> f64 {
        self.moi_x() + self.area() * d.powi(2)
    }

    /// This function returns the moment of intertia of the structural shape around the y-axis when
    /// displaced perpendicular to the axis by a distance d
    /// ```
    /// use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::Rod{radius: 2.0};
    /// let area = shape.moi_y_d(2.0);
    /// ```
    pub fn moi_y_d(&self, d: f64) -> f64 {
        self.moi_y() + self.area() * d.powi(2)
    }
}