four-bar 7.0.5

Four🍀bar library provides simulation and synthesis function for four-bar linkages.
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
use std::f64::consts::TAU;

/// State of the linkage.
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Default, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "lowercase")
)]
pub enum Stat {
    /// Circuit 1-1
    #[default]
    #[cfg_attr(feature = "serde", serde(alias = "C1B1"))]
    C1B1 = 1,
    /// Circuit 1-2
    #[cfg_attr(feature = "serde", serde(alias = "C1B2"))]
    C1B2 = 2,
    /// Circuit 2-1
    #[cfg_attr(feature = "serde", serde(alias = "C2B1"))]
    C2B1 = 3,
    /// Circuit 2-2
    #[cfg_attr(feature = "serde", serde(alias = "C2B2"))]
    C2B2 = 4,
}

impl std::fmt::Display for Stat {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::C1B1 => write!(f, "Circuit 1-1"),
            Self::C1B2 => write!(f, "Circuit 1-2"),
            Self::C2B1 => write!(f, "Circuit 2-1"),
            Self::C2B2 => write!(f, "Circuit 2-2"),
        }
    }
}

/// Error for state conversion.
#[derive(Debug)]
pub struct StatError;

impl std::fmt::Display for StatError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "invalid state")
    }
}

impl std::error::Error for StatError {}

impl TryFrom<u8> for Stat {
    type Error = StatError;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Self::C1B1),
            2 => Ok(Self::C1B2),
            3 => Ok(Self::C2B1),
            4 => Ok(Self::C2B2),
            _ => Err(StatError),
        }
    }
}

impl Stat {
    /// Get the lowercase name.
    pub const fn name_lowercase(&self) -> &'static str {
        match self {
            Self::C1B1 => "c1b1",
            Self::C1B2 => "c1b2",
            Self::C2B1 => "c2b1",
            Self::C2B2 => "c2b2",
        }
    }

    /// Get the uppercase name.
    pub const fn name_uppercase(&self) -> &'static str {
        match self {
            Self::C1B1 => "C1B1",
            Self::C1B2 => "C1B2",
            Self::C2B1 => "C2B1",
            Self::C2B2 => "C2B2",
        }
    }

    /// Check if the state is on circuit 1.
    pub fn is_c1(&self) -> bool {
        matches!(self, Self::C1B1 | Self::C1B2)
    }

    /// Check if the state is on branch 1.
    pub fn is_b1(&self) -> bool {
        matches!(self, Self::C1B1 | Self::C2B1)
    }

    /// Switch the circuit.
    pub fn switch_circuit(&mut self) {
        *self = match self {
            Self::C1B1 => Self::C2B1,
            Self::C1B2 => Self::C2B2,
            Self::C2B1 => Self::C1B1,
            Self::C2B2 => Self::C1B2,
        };
    }

    /// Switch the branch.
    pub fn switch_branch(&mut self) {
        *self = match self {
            Self::C1B1 => Self::C1B2,
            Self::C1B2 => Self::C1B1,
            Self::C2B1 => Self::C2B2,
            Self::C2B2 => Self::C2B1,
        };
    }
}

/// Angle boundary types. The input angle range.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Copy, Clone, PartialEq, Default, Debug)]
pub enum AngleBound {
    /// Closed curve
    Closed,
    /// Open curve with 1 circuits 2 branches (`[start, end]`)
    OpenC1B2([f64; 2]),
    /// Open curve with 2 circuits 2 branches (`[start, end]`)
    OpenC2B2([f64; 2]),
    /// Invalid
    #[default]
    Invalid,
}

impl AngleBound {
    /// The minimum input angle bound. (Ď€/2)
    pub const MIN_ANGLE: f64 = std::f64::consts::FRAC_PI_2;

    /// Name of the angle bound.
    pub const fn description(&self) -> &'static str {
        match self {
            Self::Closed => "Closed curve",
            Self::OpenC1B2(_) => "Open curve with 1 circuits 2 branches",
            Self::OpenC2B2(_) => "Open curve with 2 circuits 2 branches",
            Self::Invalid => "Invalid",
        }
    }

    /// Check angle bound from a planar loop.
    pub fn from_planar_loop(mut planar_loop: [f64; 4], stat: Stat) -> Self {
        let [l1, l2, l3, l4] = planar_loop;
        planar_loop.sort_unstable_by(|a, b| a.partial_cmp(b).unwrap());
        if planar_loop[3] > planar_loop[..3].iter().sum() {
            return Self::Invalid;
        }
        match (l1 + l2 <= l3 + l4, (l1 - l2).abs() >= (l3 - l4).abs()) {
            (true, true) => Self::Closed,
            (true, false) => {
                let l33 = l3 - l4;
                let d = (l1 * l1 + l2 * l2 - l33 * l33) / (2. * l1 * l2);
                Self::OpenC1B2([d.acos(), TAU - d.acos()])
            }
            (false, true) => {
                let l33 = l3 + l4;
                let d = (l1 * l1 + l2 * l2 - l33 * l33) / (2. * l1 * l2);
                Self::OpenC1B2([-d.acos(), d.acos()])
            }
            (false, false) => {
                let tmp1 = l1 * l1 + l2 * l2;
                let tmp2 = 2. * l1 * l2;
                let l33 = l3 - l4;
                let d1 = (tmp1 - l33 * l33) / tmp2;
                let l33 = l3 + l4;
                let d2 = (tmp1 - l33 * l33) / tmp2;
                if stat.is_b1() {
                    Self::OpenC2B2([TAU - d2.acos(), TAU - d1.acos()])
                } else {
                    Self::OpenC2B2([d1.acos(), d2.acos()])
                }
            }
        }
    }

    /// Create a open and its reverse angle bound.
    pub fn open_and_rev_at(a: f64, b: f64) -> [Self; 2] {
        // No matter the type of the angle bound `OpenC1B2` or `OpenC2B2`
        [Self::OpenC1B2([a, b]), Self::OpenC1B2([b, a])]
    }

    /// Check the state is the same to the provided mode.
    pub fn check_mode(self, is_open: bool) -> Self {
        if self.is_valid() && self.is_open() == is_open {
            self
        } else {
            Self::Invalid
        }
    }

    /// Angle range must greater than [`AngleBound::MIN_ANGLE`].
    pub fn check_min(self) -> Self {
        match self {
            Self::OpenC1B2([a, b]) | Self::OpenC2B2([a, b]) => {
                let b = if b > a { b } else { b + TAU };
                if b - a > Self::MIN_ANGLE {
                    self
                } else {
                    Self::Invalid
                }
            }
            _ => self,
        }
    }

    /// Turn into boundary values.
    pub fn to_value(self) -> Option<[f64; 2]> {
        match self {
            Self::Closed => Some([0., TAU]),
            Self::OpenC1B2(a) | Self::OpenC2B2(a) => Some(a),
            Self::Invalid => None,
        }
    }

    /// Return true if the bounds is open.
    pub fn is_open(&self) -> bool {
        matches!(self, Self::OpenC1B2(_) | Self::OpenC2B2(_))
    }

    /// Check if the data is valid.
    pub fn is_valid(&self) -> bool {
        !matches!(self, Self::Invalid)
    }

    /// List all states.
    pub fn get_states(&self) -> Vec<Stat> {
        match self {
            Self::Closed => vec![Stat::C1B1, Stat::C2B1],
            Self::OpenC1B2(_) => vec![Stat::C1B1, Stat::C1B2],
            Self::OpenC2B2(_) => vec![Stat::C1B1, Stat::C1B2, Stat::C2B1, Stat::C2B2],
            Self::Invalid => vec![Stat::C1B1],
        }
    }
}

/// Type of the four-bar linkage.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub enum FourBarTy {
    /// Grashof double crank (Drag-link)
    GCCC,
    /// Grashof crank rocker
    GCRR,
    /// Grashof double rocker
    GRCR,
    /// Grashof rocker crank
    GRRC,
    /// Non-Grashof triple rocker (ground link is the longest)
    RRR1,
    /// Non-Grashof triple rocker (driver link is the longest)
    RRR2,
    /// Non-Grashof triple rocker (coupler link is the longest)
    RRR3,
    /// Non-Grashof triple rocker (follower link is the longest)
    RRR4,
    /// Invalid
    Invalid,
}

impl FourBarTy {
    /// Detect from four-bar loop `[l1, l2, l3, l4]`.
    pub fn from_loop(fb_loop: [f64; 4]) -> Self {
        let mut i = 0;
        let mut fb_loop = fb_loop.map(|l| {
            let ret = (i, l);
            i += 1;
            ret
        });
        fb_loop.sort_unstable_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap());
        let [(s_ind, s), (_, p), (_, q), (l_ind, l)] = fb_loop;
        if l > s + p + q {
            Self::Invalid
        } else if s + l < p + q {
            [Self::GCCC, Self::GCRR, Self::GRCR, Self::GRRC][s_ind]
        } else {
            [Self::RRR1, Self::RRR2, Self::RRR3, Self::RRR4][l_ind]
        }
    }

    /// Name of the type.
    pub const fn name(&self) -> &'static str {
        match self {
            Self::GCCC => "Grashof double crank (Drag-link, GCCC)",
            Self::GCRR => "Grashof crank rocker (GCRR)",
            Self::GRCR => "Grashof double rocker (GRCR)",
            Self::GRRC => "Grashof rocker crank (GRRC)",
            Self::RRR1 => "Non-Grashof triple rocker (RRR1)",
            Self::RRR2 => "Non-Grashof triple rocker (RRR2)",
            Self::RRR3 => "Non-Grashof triple rocker (RRR3)",
            Self::RRR4 => "Non-Grashof triple rocker (RRR4)",
            Self::Invalid => "Invalid",
        }
    }

    /// Check if the type is valid.
    pub const fn is_valid(&self) -> bool {
        !matches!(self, Self::Invalid)
    }

    /// Return true if the type is Grashof linkage.
    pub const fn is_grashof(&self) -> bool {
        matches!(self, Self::GCCC | Self::GCRR | Self::GRCR | Self::GRRC)
    }

    /// Return true if the type has continuous motion.
    pub const fn is_closed_curve(&self) -> bool {
        matches!(self, Self::GCCC | Self::GCRR)
    }

    /// Return true if the type has non-continuous motion.
    pub const fn is_open_curve(&self) -> bool {
        matches!(
            self,
            Self::GRCR | Self::GRRC | Self::RRR1 | Self::RRR2 | Self::RRR3 | Self::RRR4
        )
    }
}

/// State of the linkage.
pub trait Statable: PlanarLoop + Clone {
    /// Get the state mutable reference.
    fn stat_mut(&mut self) -> &mut Stat;
    /// Get the state.
    fn stat(&self) -> Stat;

    /// Set the state.
    fn set_stat(&mut self, stat: Stat) {
        *self.stat_mut() = stat;
    }

    /// Build with state.
    fn with_stat(mut self, stat: Stat) -> Self {
        self.set_stat(stat);
        self
    }

    /// Return the type of this linkage.
    fn ty(&self) -> FourBarTy {
        FourBarTy::from_loop(self.planar_loop())
    }

    /// Input angle bounds of the linkage.
    fn angle_bound(&self) -> AngleBound {
        let stat = self.stat();
        AngleBound::from_planar_loop(self.planar_loop(), stat)
    }

    /// Check if the linkage is valid.
    fn is_valid(&self) -> bool {
        self.angle_bound().is_valid()
    }

    /// Check if the bounds is open.
    ///
    /// Please check [`Statable::is_valid()`] first.
    fn is_open(&self) -> bool {
        self.angle_bound().is_open()
    }

    /// Return `true` will actives the inversion.
    fn inv(&self) -> bool {
        !self.stat().is_c1()
    }

    /// List all states from a linkage.
    fn to_states(self) -> Vec<Self> {
        let bound = self.angle_bound();
        self.states_from_bound(bound)
    }

    /// List all states except the current state from a linkage.
    fn other_states(&self) -> Vec<Self> {
        self.other_states_from_bound(self.angle_bound())
    }

    /// List all states from a calculated bound.
    fn states_from_bound(self, bound: AngleBound) -> Vec<Self> {
        let mut states = self.other_states_from_bound(bound);
        states.push(self);
        states
    }

    /// List all states except the current state from a calculated bound.
    fn other_states_from_bound(&self, bound: AngleBound) -> Vec<Self> {
        let stat = self.stat();
        bound
            .get_states()
            .into_iter()
            .filter(|s| *s != stat)
            .map(|s| self.clone().with_stat(s))
            .collect()
    }
}

impl<S> Statable for S
where
    S: std::ops::DerefMut + PlanarLoop + Clone,
    S::Target: Statable,
{
    fn stat_mut(&mut self) -> &mut Stat {
        self.deref_mut().stat_mut()
    }

    fn stat(&self) -> Stat {
        self.deref().stat()
    }
}

/// Planar loop of the linkage.
pub trait PlanarLoop {
    /// Get the planar loop.
    fn planar_loop(&self) -> [f64; 4];
    /// Set the link lengths as the planar loop.
    fn set_to_planar_loop(&mut self) {}
}