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
//! Plot utitlities.
//!
//! Please see [`crate::plot2d::Figure`] and [`crate::plot3d::Figure`] for more
//! information.
//!
//! # Single Plot Example
//!
//! ```
//! use four_bar::{plot::*, plot2d};
//!
//! let fig = plot2d::Figure::new().add_line("", vec![[0.; 2], [1.; 2]], Style::Line, BLACK);
//! let mut buf = String::new();
//! let svg = SVGBackend::with_string(&mut buf, (1600, 1600));
//! fig.plot(svg).unwrap();
//! ```
//!
//! # Sub-plots Example
//!
//! ```
//! use four_bar::{plot::*, plot2d};
//!
//! let fig = plot2d::Figure::new().add_line("", vec![[0.; 2], [1.; 2]], Style::Line, BLACK);
//! let mut buf = String::new();
//! let svg = SVGBackend::with_string(&mut buf, (1600, 800));
//! let (root_l, root_r) = svg.into_drawing_area().split_horizontally(800);
//! fig.plot(root_l).unwrap();
//! fig.plot(root_r).unwrap();
//! ```
use self::{ball::*, dashed_line::*};
use crate::*;
#[doc(no_inline)]
pub use plotters::{prelude::*, *};
use std::{
    borrow::Cow,
    cell::{Ref, RefCell},
    rc::Rc,
};

mod ball;
mod dashed_line;
pub mod plot2d;
pub mod plot3d;

pub(crate) type PResult<T, B> = Result<T, DrawingAreaErrorKind<<B as DrawingBackend>::ErrorType>>;
pub(crate) type Canvas<B> = DrawingArea<B, coord::Shift>;

macro_rules! inner_opt {
    ($($(#[$meta:meta])+ fn $name:ident($ty:ty))+) => {$(
        $(#[$meta])+
        pub fn $name(mut self, $name: $ty) -> Self {
            self.$name = $name;
            self
        }
    )+};
}

// Rounding float numbers without trailing zeros
pub(crate) fn formatter(v: &f64) -> String {
    let mut s = format!("{v:.04}");
    let sub = s.trim_end_matches('0');
    s.truncate(sub.strip_suffix('.').unwrap_or(sub).len());
    if s == "-0" {
        s.remove(0);
    }
    s
}

/// The extreme values of the data.
///
/// ```
/// use four_bar::plot::ExtBound;
///
/// let data = vec![[1.], [2.], [3.]];
/// let ext = ExtBound::from_pts(&data);
/// assert_eq!(ext.min, [1.]);
/// assert_eq!(ext.max, [3.]);
/// ```
pub struct ExtBound<const N: usize> {
    /// Minimum values.
    pub min: [f64; N],
    /// Maximum values.
    pub max: [f64; N],
}

impl<const N: usize> ExtBound<N> {
    /// Create a new instance from an iterator of points.
    pub fn from_pts<'a, I>(iter: I) -> Self
    where
        I: IntoIterator<Item = &'a [f64; N]>,
    {
        let init = Self {
            min: [f64::INFINITY; N],
            max: [f64::NEG_INFINITY; N],
        };
        iter.into_iter().fold(init, |mut bound, p| {
            p.iter()
                .zip(&mut bound.min)
                .zip(&mut bound.max)
                .for_each(|((p, min), max)| {
                    *min = min.min(*p);
                    *max = max.max(*p);
                });
            bound
        })
    }

    /// Map the extreme values to another type.
    pub fn map_to<F, R>(self, f: F) -> [R; N]
    where
        F: Fn(f64, f64) -> R,
    {
        std::array::from_fn(|i| f(self.min[i], self.max[i]))
    }

    /// Get the center of the boundary.
    pub fn center(&self) -> [f64; N] {
        std::array::from_fn(|i| (self.min[i] + self.max[i]) * 0.5)
    }

    /// Change to square boundary by the maximum range.
    ///
    /// ```
    /// use four_bar::plot::ExtBound;
    ///
    /// let ext = ExtBound { min: [0., 0.], max: [1., 2.] }.to_square(0.);
    /// assert_eq!(ext.min, [-0.5, 0.]);
    /// assert_eq!(ext.max, [1.5, 2.]);
    /// ```
    pub fn to_square(mut self, margin: f64) -> Self {
        let center = self.center();
        let width = self
            .min
            .iter()
            .zip(&self.max)
            .map(|(min, max)| (max - min).abs())
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap()
            * 0.5
            * (1. + margin);
        // Extand to same range
        self.min
            .iter_mut()
            .zip(&mut self.max)
            .zip(&center)
            .for_each(|((min, max), center)| {
                *min = center - width;
                *max = center + width;
            });
        self
    }
}

/// Line style.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum Style {
    /// Continuous Line
    Line,
    /// Dashed Line
    DashedLine,
    /// Circle Marker
    #[default]
    Circle,
    /// Dot Marker
    Dot,
    /// Triangle Marker
    Triangle,
    /// Cross Marker
    Cross,
    /// Square Marker
    Square,
}

impl Style {
    /// Style list.
    pub const LIST: [Self; 7] = [
        Self::Line,
        Self::DashedLine,
        Self::Circle,
        Self::Dot,
        Self::Triangle,
        Self::Cross,
        Self::Square,
    ];

    /// Get the style names.
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Line => "Line",
            Self::DashedLine => "Dashed Line",
            Self::Circle => "Circle",
            Self::Dot => "Dot",
            Self::Triangle => "Triangle",
            Self::Cross => "Cross",
            Self::Square => "Square",
        }
    }

    pub(crate) fn draw<'a, DB, CT, I>(
        &self,
        chart: &mut ChartContext<'a, DB, CT>,
        line: I,
        color: ShapeStyle,
        label: &str,
        font: i32,
    ) -> PResult<(), DB>
    where
        DB: DrawingBackend + 'a,
        CT: CoordTranslate,
        CT::From: Clone + 'static,
        I: Iterator<Item = CT::From> + Clone,
    {
        let dot_size = color.stroke_width * 2;
        let gap = color.stroke_width as i32;
        let has_label = !label.is_empty();
        macro_rules! impl_marker {
            ($mk:ident) => {{
                let line = line.into_iter().map(|c| $mk::new(c, dot_size, color));
                let anno = chart.draw_series(line)?;
                if has_label {
                    anno.label(label)
                        .legend(move |(x, y)| $mk::new((x + font / 2, y), dot_size, color));
                }
            }};
        }
        match self {
            Self::Line => {
                let line = LineSeries::new(line, color);
                let anno = chart.draw_series(line)?;
                if has_label {
                    anno.label(label).legend(move |(x, y)| {
                        PathElement::new([(x + gap, y), (x + font - gap, y)], color)
                    });
                }
            }
            Self::DashedLine => {
                let series = DashedPath::new(line, 10, 5, color).series();
                let anno = chart.draw_series(series)?;
                if has_label {
                    anno.label(label).legend(move |(x, y)| {
                        DashedPath::new([(x + gap, y), (x + font - gap, y)], 10, 5, color)
                    });
                }
            }
            Self::Circle => impl_marker!(Circle),
            Self::Dot => {
                let color = color.filled();
                let line = line.into_iter().map(|c| Circle::new(c, dot_size, color));
                let anno = chart.draw_series(line)?;
                if has_label {
                    anno.label(label).legend(move |c| {
                        EmptyElement::at(c)
                            + Circle::new((gap, 0), dot_size, color)
                            + Circle::new((font / 2, 0), dot_size, color)
                            + Circle::new((font - gap, 0), dot_size, color)
                    });
                }
            }
            Self::Triangle => impl_marker!(TriangleMarker),
            Self::Cross => impl_marker!(Cross),
            Self::Square => {
                let r = color.stroke_width as i32;
                let line = line
                    .into_iter()
                    .map(|c| EmptyElement::at(c) + Rectangle::new([(r, r), (-r, -r)], color));
                let anno = chart.draw_series(line)?;
                if has_label {
                    anno.label(label).legend(move |(x, y)| {
                        EmptyElement::at((x + font / 2, y))
                            + Rectangle::new([(r, r), (-r, -r)], color)
                    });
                }
            }
        }
        Ok(())
    }
}

/// Legend position option.
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub enum LegendPos {
    /// Hide Legend
    Hide,
    /// Upper Left
    UL,
    /// Middle Left
    ML,
    /// Lower Left
    LL,
    /// Upper Middle
    UM,
    /// Middle Middle
    MM,
    /// Lower Middle
    LM,
    /// Upper Right
    #[default]
    UR,
    /// Middle Right
    MR,
    /// Lower Right
    LR,
    /// Coordinate
    #[cfg_attr(feature = "clap", clap(skip))]
    Coord(i32, i32),
}

impl LegendPos {
    /// Position list.
    pub const LIST: [Self; 10] = [
        Self::Hide,
        Self::UL,
        Self::ML,
        Self::LL,
        Self::UM,
        Self::MM,
        Self::LM,
        Self::UR,
        Self::MR,
        Self::LR,
    ];

    /// Get the option names.
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Hide => "Hide",
            Self::UL => "Upper Left",
            Self::ML => "Middle Left",
            Self::LL => "Lower Left",
            Self::UM => "Upper Middle",
            Self::MM => "Middle Middle",
            Self::LM => "Lower Middle",
            Self::UR => "Upper Right",
            Self::MR => "Middle Right",
            Self::LR => "Lower Right",
            Self::Coord(_, _) => "Coordinate",
        }
    }

    /// Transform to plotters option.
    pub fn to_plotter_pos(&self) -> Option<SeriesLabelPosition> {
        use SeriesLabelPosition::*;
        Some(match self {
            Self::Hide => None?,
            Self::UL => UpperLeft,
            Self::ML => MiddleLeft,
            Self::LL => LowerLeft,
            Self::UM => UpperMiddle,
            Self::MM => MiddleMiddle,
            Self::LM => LowerMiddle,
            Self::UR => UpperRight,
            Self::MR => MiddleRight,
            Self::LR => LowerRight,
            &Self::Coord(x, y) => Coordinate(x, y),
        })
    }
}

/// Drawing options of a line series.
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(default)
)]
pub struct LineData<'a, C: Clone> {
    /// Label of the line
    pub label: Cow<'a, str>,
    /// Line data
    pub line: Cow<'a, [C]>,
    /// Line style
    pub style: Style,
    /// Line color
    pub color: [u8; 3],
    /// Whether the line is filled
    pub filled: bool,
}

impl<'a, C: Clone> Default for LineData<'a, C> {
    fn default() -> Self {
        Self {
            label: Cow::Borrowed(""),
            line: Cow::Borrowed(&[]),
            style: Style::default(),
            color: [0; 3],
            filled: false,
        }
    }
}

impl<'a, C: Clone> LineData<'a, C> {
    pub(crate) fn color(&self) -> (RGBAColor, bool) {
        let color = RGBAColor(self.color[0], self.color[1], self.color[2], 1.);
        (color, self.filled)
    }
}

/// Option type base.
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(default)
)]
#[derive(Clone)]
pub struct FigureBase<'a, 'b, M: Clone, C: Clone> {
    /// Linkage
    pub fb: Option<Cow<'b, M>>,
    /// Input angle
    pub angle: Option<f64>,
    /// Line data
    pub lines: Vec<Rc<RefCell<LineData<'a, C>>>>,
    /// Drawing options
    pub opt: Opt<'a>,
}

impl<M: Clone, C: Clone> Default for FigureBase<'_, '_, M, C> {
    fn default() -> Self {
        Self {
            fb: None,
            angle: None,
            opt: Default::default(),
            lines: Default::default(),
        }
    }
}

impl<'a, 'b, M: Clone, C: Clone> FigureBase<'a, 'b, M, C> {
    /// Create a new instance with linkage.
    pub fn new(fb: Option<M>) -> Self {
        Self { fb: fb.map(Cow::Owned), ..Default::default() }
    }

    /// From an optional linkage setting.
    pub fn new_ref(fb: Option<&'b M>) -> Self {
        Self { fb: fb.map(Cow::Borrowed), ..Default::default() }
    }

    /// Attach linkage.
    pub fn with_fb(self, fb: M) -> Self {
        FigureBase { fb: Some(Cow::Owned(fb)), ..self }
    }

    /// Attach linkage with its reference.
    pub fn with_fb_ref(self, fb: &'b M) -> Self {
        FigureBase { fb: Some(Cow::Borrowed(fb)), ..self }
    }

    /// Remove linkage.
    pub fn remove_fb(self) -> Self {
        Self { fb: None, ..self }
    }

    /// Set the input angle of the linkage.
    ///
    /// If the angle value is not in the range of
    /// [`fb::FourBarBase::angle_bound()`], the actual angle will be the
    /// midpoint.
    pub fn angle(self, angle: f64) -> Self {
        Self { angle: Some(angle), ..self }
    }

    /// Set the font family.
    pub fn font_family(mut self, family: impl Into<Cow<'a, str>>) -> Self {
        self.font_family.replace(family.into());
        self
    }

    inner_opt! {
        /// Set the line stroke of the linkage.
        fn stroke(u32)
        /// Set font size.
        fn font(f64)
        /// Use grid in the plot.
        fn grid(bool)
        /// Show the axis.
        fn axis(bool)
        /// Set legend position.
        fn legend(LegendPos)
    }

    /// Set the inner options.
    pub fn with_opt(self, opt: Opt<'a>) -> Self {
        Self { opt, ..self }
    }

    /// Add a line.
    pub fn add_line<S, L>(self, label: S, line: L, style: Style, color: RGBColor) -> Self
    where
        S: Into<Cow<'a, str>>,
        L: Into<Cow<'a, [C]>>,
    {
        let color = ShapeStyle::from(color);
        self.add_line_data(LineData {
            label: label.into(),
            line: line.into(),
            style,
            color: [color.color.0, color.color.1, color.color.2],
            filled: color.filled,
        })
    }

    /// Add a line from a [`LineData`] instance.
    pub fn add_line_data(mut self, data: LineData<'a, C>) -> Self {
        self.push_line_data(data);
        self
    }

    /// Add a line in-placed.
    pub fn push_line<S, L>(&mut self, label: S, line: L, style: Style, color: RGBColor)
    where
        S: Into<Cow<'a, str>>,
        L: Into<Cow<'a, [C]>>,
    {
        let color = ShapeStyle::from(color);
        self.push_line_data(LineData {
            label: label.into(),
            line: line.into(),
            style,
            color: [color.color.0, color.color.1, color.color.2],
            filled: color.filled,
        });
    }

    /// Add a line from a [`LineData`] instance in-placed.
    pub fn push_line_data(&mut self, data: LineData<'a, C>) {
        self.lines.push(Rc::new(RefCell::new(data)));
    }

    /// Add a line with default settings in-placed.
    pub fn push_line_default<S, L>(&mut self, label: S, line: L)
    where
        S: Into<Cow<'a, str>>,
        L: Into<Cow<'a, [C]>>,
    {
        self.push_line_data(LineData {
            label: label.into(),
            line: line.into(),
            ..Default::default()
        });
    }

    /// Iterate over lines.
    pub fn lines(&self) -> impl Iterator<Item = Ref<LineData<'a, C>>> {
        self.lines.iter().map(|packed| packed.borrow())
    }

    /// Get a mutable reference to the lines.
    pub fn lines_mut(&mut self) -> &mut Vec<Rc<RefCell<LineData<'a, C>>>> {
        &mut self.lines
    }

    #[inline]
    pub(crate) fn check_empty<B: DrawingBackend>(&self) -> PResult<(), B> {
        (!self.lines.is_empty() || self.fb.is_some())
            .then_some(())
            .ok_or(DrawingAreaErrorKind::LayoutError)
    }

    pub(crate) fn get_joints<D: efd::EfdDim>(&self) -> Option<[efd::Coord<D>; 5]>
    where
        M: fb::CurveGen<D>,
    {
        use std::f64::consts::TAU;
        let fb = self.fb.as_deref()?;
        let [start, end] = fb.angle_bound().to_value()?;
        let end = if end > start { end } else { end + TAU };
        let angle = match self.angle {
            Some(angle) if (start..end).contains(&angle) => angle,
            _ => start + (end - start) * 0.8,
        };
        fb.pos(angle)
    }

    // (stroke, dot_size)
    pub(crate) fn get_dot_size(&self) -> (u32, u32) {
        (self.stroke, (self.stroke as f32 * 1.5) as u32)
    }

    #[inline]
    fn get_family(&self) -> &str {
        const DEFAULT_FONT: &str = "Times New Roman";
        self.font_family.as_deref().unwrap_or(DEFAULT_FONT)
    }

    pub(crate) fn get_font(&self) -> TextStyle {
        (self.get_family(), self.font).into_font().color(&BLACK)
    }

    pub(crate) fn get_font3d(&self) -> TextStyle {
        (self.get_family(), self.font * 1.3)
            .into_font()
            .color(&BLACK)
    }
}

impl<'a, M: Clone, C: Clone> std::ops::Deref for FigureBase<'a, '_, M, C> {
    type Target = Opt<'a>;
    fn deref(&self) -> &Self::Target {
        &self.opt
    }
}

impl<'a, M: Clone, C: Clone> std::ops::DerefMut for FigureBase<'a, '_, M, C> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.opt
    }
}

/// 2D/3D plot option.
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(default)
)]
#[derive(Clone, PartialEq)]
pub struct Opt<'a> {
    /// Stroke size
    pub stroke: u32,
    /// Font size
    pub font: f64,
    /// Font family
    pub font_family: Option<Cow<'a, str>>,
    /// Show grid
    pub grid: bool,
    /// Show axis
    pub axis: bool,
    /// Legend position
    pub legend: LegendPos,
}

impl Default for Opt<'_> {
    fn default() -> Self {
        Self {
            stroke: 7,
            font: 90.,
            font_family: None,
            grid: false,
            axis: true,
            legend: LegendPos::default(),
        }
    }
}