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
//! Linkage mechanism types.
pub use self::{
    fb::{FourBar, NormFourBar},
    mfb::{MFourBar, MNormFourBar},
    sfb::{SFourBar, SNormFourBar},
    stat::*,
    vectorized::*,
};

#[cfg(feature = "serde")]
mod impl_serde;

pub mod fb;
pub mod mfb;
pub mod sfb;
mod stat;
mod vectorized;

/// Mechanism base type. Includes normalized and unnormalized data.
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Mech<UN, NM> {
    /// Unnormalized part
    pub unnorm: UN,
    /// Normalized part
    pub norm: NM,
}

impl<UN, NM> Mech<UN, NM> {
    /// Create a new value from inner values.
    ///
    /// Please don't use [`Mech::default()`] directly, because it is an
    /// invalid value.
    ///
    /// Use [`Mech::from_norm()`] to create from a normalized part.
    pub const fn new(unnorm: UN, norm: NM) -> Self {
        Self { unnorm, norm }
    }

    /// Create a new instance from normalized part.
    pub fn from_norm<const D: usize>(norm: NM) -> Self
    where
        NM: Normalized<D, De = Self>,
        efd::U<D>: efd::EfdDim<D>,
    {
        norm.denormalize()
    }

    /// Build with inverter.
    pub fn with_stat(self, stat: Stat) -> Self
    where
        Self: Statable,
    {
        Statable::with_stat(self, stat)
    }

    /// Get the state.
    pub fn stat(&self) -> Stat
    where
        Self: Statable,
    {
        Statable::stat(self)
    }

    /// List all states.
    pub fn to_states(self) -> Vec<Self>
    where
        Self: Statable,
    {
        Statable::to_states(self)
    }

    /// List all other states.
    pub fn other_states(&self) -> Vec<Self>
    where
        Self: Statable,
    {
        Statable::other_states(self)
    }

    /// Return the type of this linkage.
    pub fn ty(&self) -> FourBarTy
    where
        Self: Statable,
    {
        Statable::ty(self)
    }

    /// Normalization.
    pub fn normalize<const D: usize>(self) -> NM
    where
        NM: Normalized<D, De = Self>,
        efd::U<D>: efd::EfdDim<D>,
    {
        Normalized::<D>::normalize(self)
    }

    /// Curve generation for coupler curve.
    pub fn curve<const D: usize>(&self, res: usize) -> Vec<[f64; D]>
    where
        Self: CurveGen<D>,
    {
        CurveGen::<D>::curve(self, res)
    }

    /// Pose generation for coupler curve.
    pub fn pose<const D: usize>(&self, res: usize) -> (Vec<[f64; D]>, Vec<[f64; D]>)
    where
        Self: PoseGen<D>,
    {
        PoseGen::<D>::pose(self, res)
    }

    /// Pose generation for coupler curve in zipped form.
    pub fn pose_zipped<const D: usize>(&self, res: usize) -> Vec<([f64; D], [f64; D])>
    where
        Self: PoseGen<D>,
    {
        PoseGen::<D>::pose_zipped(self, res)
    }

    /// Obtain the coupler curve and the extended curve in the range of motion.
    pub fn ext_curve<const D: usize>(
        &self,
        length: f64,
        res: usize,
    ) -> (Vec<[f64; D]>, Vec<[f64; D]>)
    where
        Self: PoseGen<D>,
    {
        PoseGen::<D>::ext_curve(self, length, res)
    }

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

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

    /// Input angle bounds of the linkage.
    pub fn angle_bound(&self) -> AngleBound
    where
        Self: Statable,
    {
        Statable::angle_bound(self)
    }
}

impl<UN, NM> std::ops::Deref for Mech<UN, NM> {
    type Target = NM;

    fn deref(&self) -> &Self::Target {
        &self.norm
    }
}

impl<UN, NM> std::ops::DerefMut for Mech<UN, NM> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.norm
    }
}

/// A normalized data type. This type can denormalized to another.
///
/// Usually, this type is smaller than the denormalized type.
pub trait Normalized<const D: usize>: Sized
where
    efd::U<D>: efd::EfdDim<D>,
{
    /// Denormalized target, which should be transformable.
    type De: Transformable<D>;
    /// Method to convert types.
    ///
    /// Usually, the data will become bigger.
    fn denormalize(self) -> Self::De;
    /// Inverse method to convert types.
    fn normalize(de: Self::De) -> Self;

    /// Normalize in-placed.
    ///
    /// For optimization reason, this method is required to specialize.
    fn normalize_inplace(de: &mut Self::De);

    /// Denormalized with transformation.
    fn trans_denorm(self, geo: &efd::GeoVar<efd::Rot<D>, D>) -> Self::De {
        self.denormalize().transform(geo)
    }
}

/// Transformation ability.
pub trait Transformable<const D: usize>: Sized
where
    efd::U<D>: efd::EfdDim<D>,
{
    /// Transform in placed.
    fn transform_inplace(&mut self, geo: &efd::GeoVar<efd::Rot<D>, D>);

    /// Build with transformation.
    fn transform(mut self, geo: &efd::GeoVar<efd::Rot<D>, D>) -> Self {
        self.transform_inplace(geo);
        self
    }
}

/// Curve generation behavior.
pub trait CurveGen<const D: usize>: Statable {
    /// Get the position with inversion flag.
    fn pos_s(&self, t: f64, inv: bool) -> Option<[[f64; D]; 5]>;

    /// Get the position with input angle.
    fn pos(&self, t: f64) -> Option<[[f64; D]; 5]> {
        self.pos_s(t, self.inv())
    }

    /// Generator for all positions in specified angle.
    ///
    /// For optimization reason, this method is required to specialize if
    /// [`CurveGen::pos_s()`] is inherited from a base type.
    fn pos_iter<I>(&self, iter: I) -> impl Iterator<Item = [[f64; D]; 5]>
    where
        I: IntoIterator<Item = f64>,
    {
        let inv = self.inv();
        iter.into_iter().filter_map(move |t| self.pos_s(t, inv))
    }

    /// Generator for all curves in specified angle.
    fn curves_in(&self, start: f64, end: f64, res: usize) -> Vec<[[f64; D]; 3]> {
        self.pos_iter(linspace(start, end, res))
            .map(|[.., p3, p4, p5]| [p3, p4, p5])
            .collect()
    }

    /// Generator for coupler curve in specified angle.
    fn curve_in(&self, start: f64, end: f64, res: usize) -> Vec<[f64; D]> {
        self.pos_iter(linspace(start, end, res))
            .map(|[.., p5]| p5)
            .collect()
    }

    /// Generator for curves.
    fn curves(&self, res: usize) -> Vec<[[f64; D]; 3]> {
        self.angle_bound()
            .to_value()
            .map(|[start, end]| self.curves_in(start, end, res))
            .unwrap_or_default()
    }

    /// Generator for coupler curve.
    fn curve(&self, res: usize) -> Vec<[f64; D]> {
        self.angle_bound()
            .to_value()
            .map(|[start, end]| self.curve_in(start, end, res))
            .unwrap_or_default()
    }

    /// Generator for coupler curve by an input angle list.
    fn curve_by(&self, t: &[f64]) -> Vec<[f64; D]> {
        self.pos_iter(t.iter().copied())
            .map(|[.., p5]| p5)
            .collect()
    }
}

impl<N, const D: usize> CurveGen<D> for N
where
    N: Normalized<D> + Statable + Clone,
    N::De: CurveGen<D>,
    efd::U<D>: efd::EfdDim<D>,
{
    fn pos_s(&self, t: f64, inv: bool) -> Option<[[f64; D]; 5]> {
        self.clone().denormalize().pos_s(t, inv)
    }

    fn pos_iter<I>(&self, iter: I) -> impl Iterator<Item = [[f64; D]; 5]>
    where
        I: IntoIterator<Item = f64>,
    {
        let de = self.clone().denormalize();
        let inv = de.inv();
        iter.into_iter().filter_map(move |t| de.pos_s(t, inv))
    }
}

fn linspace(start: f64, end: f64, res: usize) -> impl Iterator<Item = f64> {
    use std::f64::consts::TAU;
    let end = if end > start { end } else { end + TAU };
    let step = (end - start) / res as f64;
    (0..res).map(move |n| start + n as f64 * step)
}

/// Pose generation behavior.
pub trait PoseGen<const D: usize>: CurveGen<D> {
    /// Obtain the pose (an unit vector) from known position.
    fn uvec(&self, pos: &[[f64; D]; 3]) -> [f64; D];

    /// Generator for all poses in specified angle.
    ///
    /// For optimization reason, this method is required to specialize if
    /// [`CurveGen::pos_s()`] and [`PoseGen::uvec()`] are inherited from a base
    /// type.
    fn uvec_iter<I>(&self, iter: I) -> impl Iterator<Item = ([f64; D], [f64; D])>
    where
        I: IntoIterator<Item = f64>,
    {
        self.pos_iter(iter)
            .map(|[.., p3, p4, p5]| (p5, self.uvec(&[p3, p4, p5])))
    }

    /// Obtain the continuous pose from known position.
    fn pose_in(&self, start: f64, end: f64, res: usize) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.uvec_iter(linspace(start, end, res)).unzip()
    }

    /// Obtain the continuous pose in the range of motion.
    fn pose(&self, res: usize) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.angle_bound()
            .to_value()
            .map(|[start, end]| self.pose_in(start, end, res))
            .unwrap_or_default()
    }

    /// Obtain the continuous pose by an input angle list.
    fn pose_by(&self, t: &[f64]) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.uvec_iter(t.iter().copied()).unzip()
    }

    /// Obtain the zipped pose from known position.
    fn pose_zipped_in(&self, start: f64, end: f64, res: usize) -> Vec<([f64; D], [f64; D])> {
        self.uvec_iter(linspace(start, end, res)).collect()
    }

    /// Obtain the zipped pose in the range of motion.
    fn pose_zipped(&self, res: usize) -> Vec<([f64; D], [f64; D])> {
        self.angle_bound()
            .to_value()
            .map(|[start, end]| self.pose_zipped_in(start, end, res))
            .unwrap_or_default()
    }

    /// Obtain the zipped pose by an input angle list.
    fn pose_zipped_by(&self, t: &[f64]) -> Vec<([f64; D], [f64; D])> {
        self.uvec_iter(t.iter().copied()).collect()
    }

    /// Obtain the pose from known position.
    fn pose_from_curves(&self, curves: &[[[f64; D]; 3]]) -> Vec<[f64; D]> {
        curves.iter().map(|pos| self.uvec(pos)).collect()
    }

    /// Generator for the coupler points and the extended points in specified
    /// angle.
    fn ext_iter<I>(&self, length: f64, iter: I) -> impl Iterator<Item = ([f64; D], [f64; D])>
    where
        I: IntoIterator<Item = f64>,
    {
        self.uvec_iter(iter)
            .map(move |(p, v)| (p, std::array::from_fn(|i| p[i] + length * v[i])))
    }

    /// Obtain the coupler curve and the extended curve in the range of motion.
    fn ext_curve_in(
        &self,
        length: f64,
        start: f64,
        end: f64,
        res: usize,
    ) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.ext_iter(length, linspace(start, end, res)).unzip()
    }

    /// Obtain the coupler curve and the extended curve in the range of motion.
    fn ext_curve(&self, length: f64, res: usize) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.angle_bound()
            .to_value()
            .map(|[start, end]| self.ext_curve_in(length, start, end, res))
            .unwrap_or_default()
    }

    /// Obtain the coupler curve and the extended curve by an input angle list.
    fn ext_curve_by(&self, length: f64, t: &[f64]) -> (Vec<[f64; D]>, Vec<[f64; D]>) {
        self.ext_iter(length, t.iter().copied()).unzip()
    }
}

impl<N, const D: usize> PoseGen<D> for N
where
    N: Normalized<D> + Statable + Clone,
    N::De: PoseGen<D>,
    efd::U<D>: efd::EfdDim<D>,
{
    fn uvec(&self, pos: &[[f64; D]; 3]) -> [f64; D] {
        self.clone().denormalize().uvec(pos)
    }

    fn uvec_iter<I>(&self, iter: I) -> impl Iterator<Item = ([f64; D], [f64; D])>
    where
        I: IntoIterator<Item = f64>,
    {
        let de = self.clone().denormalize();
        let inv = de.inv();
        iter.into_iter().filter_map(move |t| {
            let [.., p3, p4, p5] = de.pos_s(t, inv)?;
            Some((p5, de.uvec(&[p3, p4, p5])))
        })
    }
}