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
use std::ffi::OsString;

use plotters::{coord::Shift, prelude::*};
use scarlet::colormap::{ColorMap, ListedColorMap};

use crate::{colormap, error::VisualizerError, Backend};

use autd3_driver::{autd3_device::AUTD3, geometry::Geometry};

#[derive(Clone, Debug)]
pub struct PlotConfig {
    pub figsize: (u32, u32),
    pub cbar_size: f64,
    pub font_size: u32,
    pub label_area_size: u32,
    pub margin: u32,
    pub ticks_step: f64,
    pub cmap: ListedColorMap,
    pub fname: OsString,
}

impl Default for PlotConfig {
    fn default() -> Self {
        Self {
            figsize: (960, 640),
            cbar_size: 0.15,
            ticks_step: 10.,
            label_area_size: 80,
            margin: 10,
            font_size: 24,
            cmap: colormap::jet(),
            fname: OsString::new(),
        }
    }
}

impl PartialEq for PlotConfig {
    fn eq(&self, other: &Self) -> bool {
        self.figsize == other.figsize
            && self.cbar_size == other.cbar_size
            && self.font_size == other.font_size
            && self.label_area_size == other.label_area_size
            && self.margin == other.margin
            && self.ticks_step == other.ticks_step
            && self.cmap.vals == other.cmap.vals
            && self.fname == other.fname
    }
}

/// Backend using [plotters](https://github.com/plotters-rs/plotters)
pub struct PlottersBackend {}

impl PlottersBackend {
    fn plot_modulation_impl<B: plotters::backend::DrawingBackend>(
        root: DrawingArea<B, Shift>,
        modulation: Vec<f64>,
        config: &PlotConfig,
    ) -> Result<(), crate::error::VisualizerError>
    where
        VisualizerError:
            From<DrawingAreaErrorKind<<B as plotters::backend::DrawingBackend>::ErrorType>>,
    {
        root.fill(&WHITE)?;

        let mut chart = ChartBuilder::on(&root)
            .margin(config.margin)
            .x_label_area_size(config.label_area_size)
            .y_label_area_size(config.label_area_size)
            .build_cartesian_2d::<_, std::ops::Range<f64>>(0..modulation.len(), 0.0..1.0)?;

        chart
            .configure_mesh()
            .disable_x_mesh()
            .disable_y_mesh()
            .x_label_style(("sans-serif", config.font_size).into_text_style(&root))
            .y_label_style(("sans-serif", config.font_size).into_text_style(&root))
            .x_desc("Index")
            .y_desc("Modulation")
            .draw()?;

        chart.draw_series(LineSeries::new(
            modulation.iter().enumerate().map(|(i, &v)| (i, v)),
            BLUE.stroke_width(2),
        ))?;

        root.present()?;

        Ok(())
    }

    fn plot_1d_impl<B: plotters::backend::DrawingBackend>(
        root: &DrawingArea<B, Shift>,
        observe_points: &[f64],
        acoustic_pressures: &[autd3_driver::defined::Complex],
        x_label: &str,
        yrange: (f64, f64),
        config: &PlotConfig,
    ) -> Result<(), crate::error::VisualizerError>
    where
        VisualizerError:
            From<DrawingAreaErrorKind<<B as plotters::backend::DrawingBackend>::ErrorType>>,
    {
        root.fill(&WHITE)?;

        let xrange = observe_points
            .iter()
            .fold((f64::MAX, f64::MIN), |acc, &x| (acc.0.min(x), acc.1.max(x)));

        let x_labels = ((xrange.1 - xrange.0).floor() / config.ticks_step) as usize + 1;

        let mut chart = ChartBuilder::on(root)
            .margin(config.margin)
            .x_label_area_size(config.label_area_size)
            .y_label_area_size(config.label_area_size)
            .build_cartesian_2d(xrange.0..xrange.1, yrange.0..yrange.1)?;

        chart
            .configure_mesh()
            .disable_x_mesh()
            .disable_y_mesh()
            .x_labels(x_labels)
            .x_label_style(("sans-serif", config.font_size).into_text_style(root))
            .y_label_style(("sans-serif", config.font_size).into_text_style(root))
            .x_desc(x_label)
            .y_desc("Amplitude [-]")
            .draw()?;

        chart.draw_series(LineSeries::new(
            observe_points
                .iter()
                .zip(acoustic_pressures.iter())
                .map(|(&x, v)| (x, v.norm())),
            BLUE.stroke_width(2),
        ))?;

        root.present()?;

        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn plot_2d_impl<B: plotters::backend::DrawingBackend>(
        root: &DrawingArea<B, Shift>,
        observe_points_x: &[f64],
        observe_points_y: &[f64],
        acoustic_pressures: &[autd3_driver::defined::Complex],
        x_label: &str,
        y_label: &str,
        zrange: (f64, f64),
        resolution: f64,
        config: &PlotConfig,
    ) -> Result<(), crate::error::VisualizerError>
    where
        VisualizerError:
            From<DrawingAreaErrorKind<<B as plotters::backend::DrawingBackend>::ErrorType>>,
    {
        root.fill(&WHITE)?;

        let main_area_size_x = (config.figsize.0 as f64 * (1.0 - config.cbar_size)) as u32;

        let (main_area, cbar_area) = root.split_horizontally(main_area_size_x);

        let color_map_size = 1000;
        let cmap: Vec<scarlet::color::RGBColor> = config
            .cmap
            .transform((0..=color_map_size).map(|x| x as f64 / color_map_size as f64));

        {
            let xrange = observe_points_x
                .iter()
                .fold((f64::MAX, f64::MIN), |acc, &x| (acc.0.min(x), acc.1.max(x)));
            let yrange = observe_points_y
                .iter()
                .fold((f64::MAX, f64::MIN), |acc, &x| (acc.0.min(x), acc.1.max(x)));

            let plot_range_x = xrange.1 - xrange.0;
            let plot_range_y = yrange.1 - yrange.0;

            let x_labels = (plot_range_x.floor() / config.ticks_step) as usize + 1;
            let y_labels = (plot_range_y.floor() / config.ticks_step) as usize + 1;

            let available_size_x = main_area_size_x - config.label_area_size - config.margin;
            let available_size_y = config.figsize.1 - config.label_area_size - config.margin * 2;

            let px_per_ps = (available_size_x as f64 / plot_range_x)
                .min(available_size_y as f64 / plot_range_y);

            let plot_size_x = (plot_range_x * px_per_ps) as u32;
            let plot_size_y = (plot_range_y * px_per_ps) as u32;

            let left_margin = config.margin
                + (main_area_size_x - plot_size_x - config.label_area_size - config.margin).max(0)
                    / 2;
            let right_margin = config.margin
                + (main_area_size_x - plot_size_x - config.label_area_size - config.margin).max(0)
                    / 2;
            let top_margin = config.margin
                + (config.figsize.1 - plot_size_y - config.label_area_size - config.margin).max(0)
                    / 2;
            let bottom_margin = config.margin
                + (config.figsize.1 - plot_size_y - config.label_area_size - config.margin).max(0)
                    / 2;

            let mut chart = ChartBuilder::on(&main_area)
                .margin_left(left_margin)
                .margin_top(top_margin)
                .margin_bottom(bottom_margin)
                .margin_right(right_margin)
                .x_label_area_size(config.label_area_size)
                .y_label_area_size(config.label_area_size)
                .build_cartesian_2d(xrange.0..xrange.1, yrange.0..yrange.1)?;

            chart
                .configure_mesh()
                .x_labels(x_labels)
                .y_labels(y_labels)
                .disable_x_mesh()
                .disable_y_mesh()
                .label_style(("sans-serif", config.font_size))
                .x_desc(x_label)
                .y_desc(y_label)
                .draw()?;

            chart.draw_series(
                itertools::iproduct!(observe_points_y, observe_points_x)
                    .zip(acoustic_pressures.iter())
                    .map(|((&y, &x), c)| {
                        #[allow(clippy::unnecessary_cast)]
                        let c: scarlet::color::RGBColor = config.cmap.transform_single(
                            ((c.norm() - zrange.0) / (zrange.1 - zrange.0)) as f64,
                        );
                        Rectangle::new(
                            [(x, y), (x + resolution, y + resolution)],
                            RGBAColor(c.int_r(), c.int_g(), c.int_b(), 1.0).filled(),
                        )
                    }),
            )?;
        }

        {
            let mut chart = ChartBuilder::on(&cbar_area)
                .margin_left(config.margin)
                .margin_top(config.margin)
                .margin_bottom(config.margin + config.label_area_size)
                .margin_right(config.margin)
                .y_label_area_size(config.label_area_size)
                .set_label_area_size(LabelAreaPosition::Left, 0)
                .set_label_area_size(LabelAreaPosition::Right, 80)
                .build_cartesian_2d(0i32..1i32, 0i32..color_map_size)?;

            chart
                .configure_mesh()
                .disable_x_axis()
                .disable_x_mesh()
                .disable_y_mesh()
                .axis_style(BLACK.stroke_width(1))
                .label_style(("sans-serif", config.font_size))
                .y_label_formatter(&|&v| {
                    format!(
                        "{:.2}",
                        zrange.0 + (zrange.1 - zrange.0) * v as f64 / color_map_size as f64
                    )
                })
                .y_desc("Amplitude [-]")
                .draw()?;

            chart.draw_series(cmap.iter().enumerate().map(|(i, c)| {
                Rectangle::new(
                    [(0, i as i32), (1, i as i32 + 1)],
                    RGBAColor(c.int_r(), c.int_g(), c.int_b(), 1.0).filled(),
                )
            }))?;

            chart.draw_series([Rectangle::new(
                [(0, 0), (1, color_map_size + 1)],
                BLACK.stroke_width(1),
            )])?;
        }

        root.present()?;

        Ok(())
    }

    fn plot_phase_impl<B: plotters::backend::DrawingBackend>(
        root: DrawingArea<B, Shift>,
        config: &PlotConfig,
        geometry: &Geometry,
        phases: Vec<f64>,
    ) -> Result<(), crate::error::VisualizerError>
    where
        VisualizerError:
            From<DrawingAreaErrorKind<<B as plotters::backend::DrawingBackend>::ErrorType>>,
    {
        root.fill(&WHITE)?;

        let main_area_size_x = (config.figsize.0 as f64 * (1.0 - config.cbar_size)) as u32;

        let (main_area, cbar_area) = root.split_horizontally(main_area_size_x);

        let color_map_size = 1000;
        let cmap: Vec<scarlet::color::RGBColor> = config
            .cmap
            .transform((0..=color_map_size).map(|x| x as f64 / color_map_size as f64));

        {
            let p = geometry
                .iter()
                .flat_map(|dev| dev.iter().map(|t| (t.position().x, t.position().y)))
                .collect::<Vec<_>>();

            let min_x =
                p.iter().fold(f64::MAX, |acc, &(x, _)| acc.min(x)) - AUTD3::TRANS_SPACING / 2.0;
            let min_y =
                p.iter().fold(f64::MAX, |acc, &(_, y)| acc.min(y)) - AUTD3::TRANS_SPACING / 2.0;
            let max_x =
                p.iter().fold(f64::MIN, |acc, &(x, _)| acc.max(x)) + AUTD3::TRANS_SPACING / 2.0;
            let max_y =
                p.iter().fold(f64::MIN, |acc, &(_, y)| acc.max(y)) + AUTD3::TRANS_SPACING / 2.0;

            let plot_range_x = max_x - min_x;
            let plot_range_y = max_y - min_y;

            let available_size_x = main_area_size_x - config.label_area_size - config.margin;
            let available_size_y = config.figsize.1 - config.label_area_size - config.margin * 2;

            let px_per_ps = (available_size_x as f64 / plot_range_x)
                .min(available_size_y as f64 / plot_range_y);

            let plot_size_x = (plot_range_x * px_per_ps) as u32;
            let plot_size_y = (plot_range_y * px_per_ps) as u32;

            let left_margin = config.margin
                + (main_area_size_x - plot_size_x - config.label_area_size - config.margin).max(0)
                    / 2;
            let right_margin = config.margin
                + (main_area_size_x - plot_size_x - config.label_area_size - config.margin).max(0)
                    / 2;
            let top_margin = config.margin
                + (config.figsize.1 - plot_size_y - config.label_area_size - config.margin).max(0)
                    / 2;
            let bottom_margin = config.margin
                + (config.figsize.1 - plot_size_y - config.label_area_size - config.margin).max(0)
                    / 2;

            let mut scatter_ctx = ChartBuilder::on(&main_area)
                .margin_left(left_margin)
                .margin_right(right_margin)
                .margin_top(top_margin)
                .margin_bottom(bottom_margin)
                .x_label_area_size(config.label_area_size)
                .y_label_area_size(config.label_area_size)
                .build_cartesian_2d(min_x..max_x, min_y..max_y)?;
            scatter_ctx
                .configure_mesh()
                .disable_x_mesh()
                .disable_y_mesh()
                .x_label_formatter(&|v| format!("{:.1}", v))
                .y_label_formatter(&|v| format!("{:.1}", v))
                .x_label_style(("sans-serif", config.font_size).into_text_style(&main_area))
                .y_label_style(("sans-serif", config.font_size).into_text_style(&main_area))
                .x_desc("x [mm]")
                .y_desc("y [mm]")
                .draw()?;

            scatter_ctx.draw_series(p.iter().zip(phases.iter()).map(|(&(x, y), &p)| {
                let v = (p / (2.0 * autd3_driver::defined::PI)) % 1.;
                let c = cmap[((v * color_map_size as f64) as usize).clamp(0, cmap.len() - 1)];
                Circle::new(
                    (x, y),
                    AUTD3::TRANS_SPACING * px_per_ps / 2.0,
                    RGBColor(c.int_r(), c.int_g(), c.int_b())
                        .filled()
                        .stroke_width(0),
                )
            }))?;
        }

        {
            let mut chart = ChartBuilder::on(&cbar_area)
                .margin_left(config.margin)
                .margin_top(config.margin)
                .margin_bottom(config.margin + config.label_area_size)
                .margin_right(config.margin)
                .y_label_area_size(config.label_area_size)
                .set_label_area_size(LabelAreaPosition::Left, 0)
                .set_label_area_size(LabelAreaPosition::Right, 80)
                .build_cartesian_2d(0i32..1i32, 0i32..color_map_size)?;

            chart
                .configure_mesh()
                .disable_x_axis()
                .y_labels(3)
                .disable_x_mesh()
                .disable_y_mesh()
                .axis_style(BLACK.stroke_width(1))
                .label_style(("sans-serif", config.font_size))
                .y_label_formatter(&|&v| {
                    if v == 0 {
                        "0".to_owned()
                    } else if v == color_map_size / 2 {
                        "π".to_owned()
                    } else {
                        "2π".to_owned()
                    }
                })
                .draw()?;

            chart.draw_series(cmap.iter().enumerate().map(|(i, c)| {
                Rectangle::new(
                    [(0, i as i32), (1, i as i32 + 1)],
                    RGBAColor(c.int_r(), c.int_g(), c.int_b(), 1.0).filled(),
                )
            }))?;

            chart.draw_series([Rectangle::new(
                [(0, 0), (1, color_map_size + 1)],
                BLACK.stroke_width(1),
            )])?;
        }

        root.present()?;

        Ok(())
    }
}

impl Backend for PlottersBackend {
    type PlotConfig = PlotConfig;

    fn new() -> Self {
        Self {}
    }

    fn initialize(&mut self) -> Result<(), crate::error::VisualizerError> {
        Ok(())
    }

    fn plot_1d(
        observe_points: Vec<f64>,
        acoustic_pressures: Vec<autd3_driver::defined::Complex>,
        _resolution: f64,
        x_label: &str,
        config: Self::PlotConfig,
    ) -> Result<(), crate::error::VisualizerError> {
        let path = std::path::Path::new(&config.fname);
        if !path.parent().map_or(true, |p| p.exists()) {
            std::fs::create_dir_all(path.parent().unwrap())?;
        }

        let yrange = acoustic_pressures
            .iter()
            .fold((f64::MAX, f64::MIN), |acc, &x| {
                (acc.0.min(x.norm()), acc.1.max(x.norm()))
            });

        if path.extension().map_or(false, |e| e == "svg") {
            Self::plot_1d_impl(
                &SVGBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &observe_points,
                &acoustic_pressures,
                x_label,
                yrange,
                &config,
            )
        } else {
            Self::plot_1d_impl(
                &BitMapBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &observe_points,
                &acoustic_pressures,
                x_label,
                yrange,
                &config,
            )
        }
    }

    fn plot_2d(
        observe_x: Vec<f64>,
        observe_y: Vec<f64>,
        acoustic_pressures: Vec<autd3_driver::defined::Complex>,
        resolution: f64,
        x_label: &str,
        y_label: &str,
        config: Self::PlotConfig,
    ) -> Result<(), crate::error::VisualizerError> {
        let path = std::path::Path::new(&config.fname);
        if !path.parent().map_or(true, |p| p.exists()) {
            std::fs::create_dir_all(path.parent().unwrap())?;
        }

        let zrange = acoustic_pressures
            .iter()
            .fold((f64::MAX, f64::MIN), |acc, &x| {
                (acc.0.min(x.norm()), acc.1.max(x.norm()))
            });

        if path.extension().map_or(false, |e| e == "svg") {
            Self::plot_2d_impl(
                &SVGBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &observe_x,
                &observe_y,
                &acoustic_pressures,
                x_label,
                y_label,
                zrange,
                resolution,
                &config,
            )
        } else {
            Self::plot_2d_impl(
                &BitMapBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &observe_x,
                &observe_y,
                &acoustic_pressures,
                x_label,
                y_label,
                zrange,
                resolution,
                &config,
            )
        }
    }

    fn plot_modulation(
        modulation: Vec<f64>,
        config: Self::PlotConfig,
    ) -> Result<(), crate::error::VisualizerError> {
        let path = std::path::Path::new(&config.fname);
        if !path.parent().map_or(true, |p| p.exists()) {
            std::fs::create_dir_all(path.parent().unwrap())?;
        }

        if path.extension().map_or(false, |e| e == "svg") {
            Self::plot_modulation_impl(
                SVGBackend::new(&config.fname, config.figsize).into_drawing_area(),
                modulation,
                &config,
            )
        } else {
            Self::plot_modulation_impl(
                BitMapBackend::new(&config.fname, config.figsize).into_drawing_area(),
                modulation,
                &config,
            )
        }
    }

    fn plot_phase(
        config: Self::PlotConfig,
        geometry: &autd3_driver::geometry::Geometry,
        phases: Vec<f64>,
    ) -> Result<(), crate::error::VisualizerError> {
        let path = std::path::Path::new(&config.fname);
        if !path.parent().map_or(true, |p| p.exists()) {
            std::fs::create_dir_all(path.parent().unwrap())?;
        }

        if path.extension().map_or(false, |e| e == "svg") {
            Self::plot_phase_impl(
                SVGBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &config,
                geometry,
                phases,
            )
        } else {
            Self::plot_phase_impl(
                BitMapBackend::new(&config.fname, config.figsize).into_drawing_area(),
                &config,
                geometry,
                phases,
            )
        }
    }
}