-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.rs
More file actions
1024 lines (950 loc) · 29.7 KB
/
Copy pathplot.rs
File metadata and controls
1024 lines (950 loc) · 29.7 KB
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use core::f64;
use egui::{Align, Color32, Layout, Ui};
use egui_plot::{Legend, Line, PlotBounds, PlotPoints, PlotUi};
use num_complex::Complex64;
/// A struct representing a color in the RGBA color space.
///
/// This struct holds four components: red (`r`), green (`g`), blue (`b`), and
/// alpha (`a`) channels, where each channel is an 8-bit unsigned integer,
/// meaning it ranges from 0 to 255.
///
/// # Examples
///
/// ```rust
/// use control_systems_torbox::RGBAColor;
/// let red = RGBAColor::RED;
/// assert_eq!(red.r, 255);
/// assert_eq!(red.g, 0);
/// assert_eq!(red.b, 0);
/// assert_eq!(red.a, 255);
/// ```
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RGBAColor {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
impl RGBAColor {
/// Common colors represented by RGBAColor constants.
///
/// These are predefined color constants for commonly used colors.
pub const WHITE: RGBAColor = RGBAColor {
r: 255,
g: 255,
b: 255,
a: 255,
};
pub const BLACK: RGBAColor = RGBAColor {
r: 0,
g: 0,
b: 0,
a: 255,
};
pub const BLUE: RGBAColor = RGBAColor {
r: 0,
g: 0,
b: 255,
a: 255,
};
pub const RED: RGBAColor = RGBAColor {
r: 255,
g: 0,
b: 0,
a: 255,
};
pub const GREEN: RGBAColor = RGBAColor {
r: 0,
g: 255,
b: 0,
a: 255,
};
pub const YELLOW: RGBAColor = RGBAColor {
r: 255,
g: 255,
b: 0,
a: 255,
};
pub const CYAN: RGBAColor = RGBAColor {
r: 0,
g: 255,
b: 255,
a: 255,
};
pub const MAGENTA: RGBAColor = RGBAColor {
r: 255,
g: 0,
b: 255,
a: 255,
};
pub const ORANGE: RGBAColor = RGBAColor {
r: 255,
g: 165,
b: 0,
a: 255,
};
pub const PURPLE: RGBAColor = RGBAColor {
r: 128,
g: 0,
b: 128,
a: 255,
};
pub const BROWN: RGBAColor = RGBAColor {
r: 165,
g: 42,
b: 42,
a: 255,
};
pub const PINK: RGBAColor = RGBAColor {
r: 255,
g: 192,
b: 203,
a: 255,
};
pub const GRAY: RGBAColor = RGBAColor {
r: 128,
g: 128,
b: 128,
a: 255,
};
pub const LIGHT_GRAY: RGBAColor = RGBAColor {
r: 211,
g: 211,
b: 211,
a: 255,
};
pub const DARK_GRAY: RGBAColor = RGBAColor {
r: 169,
g: 169,
b: 169,
a: 255,
};
/// Creates a new `RGBAColor` instance with the specified red, green, blue,
/// and alpha values.
///
/// # Parameters
/// - `red`: The red channel value (0-255).
/// - `green`: The green channel value (0-255).
/// - `blue`: The blue channel value (0-255).
/// - `alpha`: The alpha (transparency) channel value (0-255).
pub fn new(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
Self {
r: red,
g: green,
b: blue,
a: alpha,
}
}
/// Converts the `RGBAColor` to a `Color32` type used in the `egui` library.
///
/// # Returns
/// A `Color32` representation of the color.
fn to_egui(self) -> Color32 {
Color32::from_rgba_premultiplied(self.r, self.g, self.b, self.a)
}
}
/// The `Plot` trait defines a common interface for types that can display a
/// plot. Types implementing this trait should be able to show a plot in a
/// graphical window.
pub trait Plot {
/// Displays the plot in a new window with the given dimensions and name.
///
/// # Arguments
///
/// * `width` - The width of the plot window in pixels.
/// * `height` - The height of the plot window in pixels.
/// * `name` - The name of the plot window.
///
/// # Returns
///
/// Returns a `Result` indicating success or failure.
fn show(
self,
width: usize,
height: usize,
name: &str,
) -> Result<(), Box<dyn std::error::Error + 'static>>;
}
impl<T: eframe::App> Plot for T {
/// Implements the `Plot` trait for types that implement the `eframe::App`
/// trait.
///
/// This function opens a new window with the given dimensions and runs the
/// application that implements the `eframe::App` trait.
fn show(
self,
width: usize,
height: usize,
name: &str,
) -> Result<(), Box<dyn std::error::Error + 'static>> {
let opts = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default()
.with_inner_size([width as f32, height as f32]),
..Default::default()
};
eframe::run_native(
name,
opts,
Box::new(move |_cc| Ok(Box::new(self))),
)?;
Ok(())
}
}
/// `BodePlotData` represents the data for a Bode plot, including magnitude,
/// phase, and frequency points.
#[derive(Clone)]
pub struct BodePlotData {
mag_phase_freq_points: Vec<[f64; 3]>,
name: String,
color: Option<RGBAColor>,
}
impl BodePlotData {
/// Creates a new `BodePlotData` with the given magnitude, phase, and
/// frequency points.
///
/// # Arguments
///
/// * `mag_phase_freq_points` - A vector of 3-element arrays representing
/// the magnitude, phase, and frequency points for the Bode plot.
///
/// # Returns
///
/// A new instance of `BodePlotData`.
pub fn new(mag_phase_freq_points: Vec<[f64; 3]>) -> Self {
Self {
mag_phase_freq_points,
name: "".to_string(),
color: None,
}
}
/// Sets the name for the Bode plot data.
///
/// # Arguments
///
/// * `name` - The name to set for the Bode plot data.
///
/// # Returns
///
/// A new instance of `BodePlotData` with the updated name.
pub fn set_name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
/// Sets the color for the Bode plot data.
///
/// # Arguments
///
/// * `color` - The color to set for the Bode plot data.
///
/// # Returns
///
/// A new instance of `BodePlotData` with the updated color.
pub fn set_color(mut self, color: RGBAColor) -> Self {
self.color = Some(color);
self
}
}
impl From<Vec<[f64; 3]>> for BodePlotData {
/// Converts a vector of 3-element arrays to a `BodePlotData` instance.
///
/// # Arguments
///
/// * `value` - A vector of 3-element arrays representing magnitude, phase,
/// and frequency points.
///
/// # Returns
///
/// A `BodePlotData` instance.
fn from(value: Vec<[f64; 3]>) -> Self {
BodePlotData::new(value)
}
}
/// `BodePlotOptions` holds the options for configuring a Bode plot, such as the
/// title and axis limits.
#[derive(Default, Clone)]
pub struct BodePlotOptions {
title: String,
x_limits: Option<[f64; 2]>,
}
impl BodePlotOptions {
/// Creates a new `BodePlotOptions` instance with the given title.
///
/// # Arguments
///
/// * `title` - The title to set for the Bode plot.
///
/// # Returns
///
/// A new instance of `BodePlotOptions`.
pub fn new(title: &str) -> Self {
Self {
title: title.to_string(),
x_limits: None,
}
}
/// Sets the title for the Bode plot options.
///
/// # Arguments
///
/// * `title` - The title to set for the Bode plot.
///
/// # Returns
///
/// A new instance of `BodePlotOptions` with the updated title.
pub fn set_title(mut self, title: &str) -> Self {
self.title = title.to_string();
self
}
/// Sets the x-axis limits for the Bode plot options.
///
/// # Arguments
///
/// * `x_limits` - The x-axis limits as a 2-element array `[min, max]`.
///
/// # Returns
///
/// A new instance of `BodePlotOptions` with the updated x-axis limits.
pub fn set_x_limits(mut self, x_limits: [f64; 2]) -> Self {
self.x_limits = Some(x_limits);
self
}
}
/// `BodePlot` holds the configuration options and data for a Bode plot.
#[derive(Default, Clone)]
pub struct BodePlot {
options: BodePlotOptions,
plot_data: Vec<BodePlotData>,
}
impl BodePlot {
/// Creates a new `BodePlot` with the given options.
///
/// # Arguments
///
/// * `options` - The options to configure the Bode plot.
///
/// # Returns
///
/// A new instance of `BodePlot`.
pub fn new(options: BodePlotOptions) -> Self {
Self {
options,
plot_data: vec![],
}
}
// Adds a system's Bode plot data to the `BodePlot`.
///
/// # Arguments
///
/// * `data` - The `BodePlotData` for the system.
///
/// # Returns
///
/// A mutable reference to the `BodePlot` instance for method chaining.
pub fn add_system(&mut self, data: BodePlotData) -> &mut Self {
self.plot_data.push(data);
self
}
}
/// `BodePlotEgui` is a wrapper for displaying the `BodePlot` using the `eframe`
/// GUI framework.
#[derive(Clone)]
pub struct BodePlotEgui {
bode: BodePlot,
init: bool,
shared_x_limits: [f64; 2],
last_mag_x_limits: [f64; 2],
last_phase_x_limits: [f64; 2],
}
impl BodePlotEgui {
/// Creates a new `BodePlotEgui` instance with the given `BodePlot` data.
///
/// # Arguments
///
/// * `bode` - The `BodePlot` instance to be wrapped by the `BodePlotEgui`.
///
/// # Returns
///
/// A new instance of `BodePlotEgui`.
pub fn new(bode: BodePlot) -> Self {
let bounds = BodePlotEgui::get_mag_bounds(
bode.options.x_limits,
None,
bode.plot_data
.iter()
.flat_map(|d| d.mag_phase_freq_points.iter()),
);
let x_limits = bounds_to_x_limits(&bounds);
Self {
bode,
init: false,
shared_x_limits: x_limits,
last_mag_x_limits: x_limits,
last_phase_x_limits: x_limits,
}
}
}
impl From<BodePlot> for BodePlotEgui {
/// Converts a `BodePlot` instance into a `BodePlotEgui` instance.
fn from(value: BodePlot) -> Self {
Self::new(value)
}
}
impl eframe::App for BodePlotEgui {
/// Updates the `BodePlotEgui` GUI on each frame.
///
/// This function is called to update the user interface and plot the
/// magnitude and phase plots.
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
ui.with_layout(Layout::right_to_left(Align::RIGHT), |ui| {
if ui.button(egui::RichText::new("🏠").size(20.0)).clicked() {
*self = self.bode.clone().into();
}
});
});
egui::CentralPanel::default().show(ctx, |ui| {
let plot_height = ui.available_height() / 2.0;
ui.vertical(|ui| {
ui.add_sized(
[ui.available_width(), plot_height],
|ui: &mut egui::Ui| self.add_mag_plot(ui),
);
ui.add_sized(
[ui.available_width(), plot_height],
|ui: &mut Ui| self.add_phase_plot(ui),
);
});
});
self.init = true;
}
}
impl BodePlotEgui {
fn add_mag_plot(&mut self, ui: &mut Ui) -> egui::Response {
egui_plot::Plot::new("Magnitude Plot")
.legend(Legend::default())
.show(ui, |plot_ui| {
if !self.init {
let bounds = BodePlotEgui::get_mag_bounds(
self.bode.options.x_limits,
None,
self.bode
.plot_data
.iter()
.flat_map(|data| data.mag_phase_freq_points.iter()),
);
plot_ui.set_plot_bounds(bounds);
}
BodePlotEgui::plot_mag_data(plot_ui, &self.bode.plot_data);
[self.shared_x_limits, self.last_mag_x_limits] =
BodePlotEgui::synchronize_x_axes(
plot_ui,
self.last_mag_x_limits,
self.shared_x_limits,
);
})
.response
}
fn plot_mag_data(plot_ui: &mut PlotUi, data: &[BodePlotData]) {
for data_i in data {
let plot_points = PlotPoints::from_iter(
data_i
.mag_phase_freq_points
.iter()
.map(|p| [p[2].log10(), p[0]]),
);
let mut line = Line::new(plot_points);
line = set_name_and_color_line(line, &data_i.name, &data_i.color);
plot_ui.line(line);
}
}
fn get_mag_bounds<'a, I>(
x_limits: Option<[f64; 2]>,
y_limits: Option<[f64; 2]>,
data: I,
) -> PlotBounds
where
I: Iterator<Item = &'a [f64; 3]>,
{
let xy_iter =
data.map(|mag_phase_freq| [mag_phase_freq[2], mag_phase_freq[0]]);
let bounds = get_2d_plot_bounds(x_limits, y_limits, xy_iter);
let [x_min, y_min] = bounds.min();
let [x_max, y_max] = bounds.max();
PlotBounds::from_min_max([x_min.log10(), y_min], [x_max.log10(), y_max])
}
fn add_phase_plot(&mut self, ui: &mut Ui) -> egui::Response {
egui_plot::Plot::new("Phase Plot")
.legend(Legend::default())
.show(ui, |plot_ui| {
if !self.init {
let bounds = BodePlotEgui::get_phase_bounds(
self.bode.options.x_limits,
None,
self.bode
.plot_data
.iter()
.flat_map(|data| data.mag_phase_freq_points.iter()),
);
plot_ui.set_plot_bounds(bounds);
}
BodePlotEgui::plot_phase_data(plot_ui, &self.bode.plot_data);
[self.shared_x_limits, self.last_phase_x_limits] =
BodePlotEgui::synchronize_x_axes(
plot_ui,
self.last_phase_x_limits,
self.shared_x_limits,
);
})
.response
}
fn get_phase_bounds<'a, I>(
x_limits: Option<[f64; 2]>,
y_limits: Option<[f64; 2]>,
data: I,
) -> PlotBounds
where
I: Iterator<Item = &'a [f64; 3]>,
{
let xy_iter =
data.map(|mag_phase_freq| [mag_phase_freq[2], mag_phase_freq[1]]);
let bounds = get_2d_plot_bounds(x_limits, y_limits, xy_iter);
let [x_min, y_min] = bounds.min();
let [x_max, y_max] = bounds.max();
PlotBounds::from_min_max([x_min.log10(), y_min], [x_max.log10(), y_max])
}
fn plot_phase_data(plot_ui: &mut PlotUi, data: &[BodePlotData]) {
for data_i in data {
let plot_points = PlotPoints::from_iter(
data_i
.mag_phase_freq_points
.iter()
.map(|p| [p[2].log10(), p[1]]),
);
let mut line: Line<'_> = Line::new(plot_points);
line = set_name_and_color_line(line, &data_i.name, &data_i.color);
plot_ui.line(line);
}
}
fn synchronize_x_axes(
plot_ui: &mut PlotUi,
last_x_limits: [f64; 2],
shared_x_limits: [f64; 2],
) -> [[f64; 2]; 2] {
let current_x_limits = bounds_to_x_limits(&plot_ui.plot_bounds());
let mut new_shared_x_limits = shared_x_limits;
if last_x_limits != current_x_limits {
new_shared_x_limits = current_x_limits;
} else if current_x_limits != shared_x_limits {
let mut bounds = plot_ui.plot_bounds();
bounds = bounds_set_x_limits(&bounds, shared_x_limits);
plot_ui.set_plot_bounds(bounds);
}
[new_shared_x_limits, current_x_limits]
}
}
/// Implements the `Plot` trait for the `BodePlot` struct.
///
/// This implementation defines how to display a `BodePlot` using the `Plot`
/// trait's `show` method, which delegates the rendering of the plot to the
/// `BodePlotEgui` struct for use with the `eframe` framework.
///
/// # Arguments
///
/// * `self` - The `BodePlot` instance that is being displayed.
/// * `width` - The width of the plot window in pixels.
/// * `height` - The height of the plot window in pixels.
/// * `name` - A string representing the name of the plot window, used for
/// identification in the UI.
///
/// # Returns
///
/// This method returns a `Result<(), Box<dyn std::error::Error + 'static>>`,
/// which represents the success or failure of showing the plot. If successful,
/// the result is `Ok(())`, and if there is an error, it returns a boxed error
/// type.
///
/// # Example
///
/// ```rust,no_run
/// use control_systems_torbox::*;
/// let bode_data = vec![
/// [1.0, 2.0, 3.0],
/// [1.1, 2.1, 3.1],
/// [1.2, 2.2, 3.2],
/// ];
///
/// let mut bode = BodePlot::new(BodePlotOptions::new("Sample Bode Plot"));
/// bode.add_system(BodePlotData::new(bode_data));
///
/// bode.show(800, 600, "Bode Plot Example").unwrap();
/// ```
impl Plot for BodePlot {
fn show(
self,
width: usize,
height: usize,
name: &str,
) -> Result<(), Box<dyn std::error::Error + 'static>> {
BodePlotEgui::new(self).show(width, height, name)
}
}
fn get_2d_plot_bounds<I>(
x_limits: Option<[f64; 2]>,
y_limits: Option<[f64; 2]>,
xy_iter: I,
) -> PlotBounds
where
I: Iterator<Item = [f64; 2]>,
{
let [x_min_init, x_max_init] =
x_limits.unwrap_or([-f64::INFINITY, f64::INFINITY]);
let [y_min_init, y_max_init] =
y_limits.unwrap_or([-f64::INFINITY, f64::INFINITY]);
let filtered_data = xy_iter.filter(|c| {
c[0] >= x_min_init
&& c[0] <= x_max_init
&& c[1] >= y_min_init
&& c[1] <= y_max_init
});
let [mut x_min, mut x_max] = [f64::INFINITY, -f64::INFINITY];
let [mut y_min, mut y_max] = [f64::INFINITY, -f64::INFINITY];
for c in filtered_data {
x_min = x_min.min(c[0]);
x_max = x_max.max(c[0]);
y_min = y_min.min(c[1]);
y_max = y_max.max(c[1]);
}
[x_min, x_max] = x_limits.unwrap_or([x_min, x_max]);
[y_min, y_max] = y_limits.unwrap_or([y_min, y_max]);
PlotBounds::from_min_max([x_min, y_min], [x_max, y_max])
}
fn set_name_and_color_line<'a>(
mut line: Line<'a>,
name: &str,
color: &Option<RGBAColor>,
) -> Line<'a> {
if !name.is_empty() {
line = line.name(name);
}
if let Some(color) = color {
line = line.color(color.to_egui());
}
line
}
fn bounds_to_x_limits(bounds: &PlotBounds) -> [f64; 2] {
[bounds.min()[0], bounds.max()[0]]
}
fn bounds_set_x_limits(bounds: &PlotBounds, x_limits: [f64; 2]) -> PlotBounds {
PlotBounds::from_min_max(
[x_limits[0], bounds.min()[1]],
[x_limits[1], bounds.max()[1]],
)
}
//////////////////////////////////////////////////////////////////////////////////////////////
// Nyquist
//////////////////////////////////////////////////////////////////////////////////////////////
/// A struct representing the data points for a Nyquist plot.
///
/// This struct stores the data points for the Nyquist plot, as well as optional
/// metadata such as the name of the system and the color for plotting.
///
/// # Fields
///
/// - `data_points`: A `Vec<Complex64>` that contains the data points to be
/// plotted.
/// - `name`: A `String` representing the name of the system or dataset. It is
/// used for labeling the plot.
/// - `color`: An optional `RGBAColor` that defines the color of the plot line.
#[derive(Clone, Debug)]
pub struct NyquistPlotData {
data_points: Vec<Complex64>,
name: String,
color: Option<RGBAColor>,
}
impl NyquistPlotData {
/// Creates a new `NyquistPlotData` instance with the given data points.
///
/// # Arguments
/// - `data_points`: A vector of complex numbers representing the Nyquist
/// plot data points.
///
/// # Returns
/// A new instance of `NyquistPlotData`.
pub fn new(data_points: Vec<Complex64>) -> Self {
Self {
data_points,
name: "".to_string(),
color: None,
}
}
/// Sets the name of the Nyquist plot data.
///
/// # Arguments
/// - `name`: The name to be set for the Nyquist plot data.
///
/// # Returns
/// A new instance of `NyquistPlotData` with the name set.
pub fn set_name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
/// Sets the color for the Nyquist plot data.
///
/// # Arguments
/// - `color`: The `RGBAColor` to be used for plotting.
///
/// # Returns
/// A new instance of `NyquistPlotData` with the color set.
pub fn set_color(mut self, color: RGBAColor) -> Self {
self.color = Some(color);
self
}
}
impl From<Vec<Complex64>> for NyquistPlotData {
fn from(value: Vec<Complex64>) -> Self {
NyquistPlotData::new(value)
}
}
/// A struct representing the options for a Nyquist plot.
///
/// This struct holds the configuration settings for the Nyquist plot, such as
/// title, axis limits, etc.
///
/// # Fields
///
/// - `title`: The title of the plot.
/// - `x_limits`: Optional X-axis limits as an array `[min, max]`.
/// - `y_limits`: Optional Y-axis limits as an array `[min, max]`.
#[derive(Default, Clone, Debug)]
pub struct NyquistPlotOptions {
title: String,
x_limits: Option<[f64; 2]>,
y_limits: Option<[f64; 2]>,
}
impl NyquistPlotOptions {
/// Sets the title for Nyquist plot.
pub fn set_title(mut self, title: &str) -> Self {
self.title = title.to_string();
self
}
/// Sets the x-limits for Nyquist plot.
pub fn set_x_limits(mut self, x_limits: [f64; 2]) -> Self {
self.x_limits = Some(x_limits);
self
}
/// Sets the y-limits for Nyquist plot.
pub fn set_y_limits(mut self, y_limits: [f64; 2]) -> Self {
self.y_limits = Some(y_limits);
self
}
}
/// A struct representing the Nyquist plot.
///
/// This struct contains the data and options to plot a Nyquist plot, including
/// the `NyquistPlotOptions` and the actual data in `NyquistPlotData` format.
///
/// # Fields
/// - `options`: The configuration options for the Nyquist plot (title, axis
/// limits).
/// - `plot_data`: A vector of `NyquistPlotData` representing the systems or
/// datasets to plot.
#[derive(Debug, Default, Clone)]
pub struct NyquistPlot {
options: NyquistPlotOptions,
plot_data: Vec<NyquistPlotData>,
}
impl NyquistPlot {
/// Creates a new `NyquistPlot` with the given options.
///
/// # Arguments
/// - `options`: The configuration options for the plot.
///
/// # Returns
/// A new instance of `NyquistPlot`.
pub fn new(options: NyquistPlotOptions) -> Self {
Self {
options,
plot_data: vec![],
}
}
// Adds a system's data to the Nyquist plot.
///
/// # Arguments
/// - `data`: The data for a system to be plotted.
///
/// # Returns
/// A mutable reference to the `NyquistPlot` instance for method chaining.
pub fn add_system(&mut self, data: NyquistPlotData) -> &mut Self {
self.plot_data.push(data);
self
}
}
/// A helper struct for rendering the Nyquist plot using the `eframe` framework.
///
/// This struct wraps a `NyquistPlot` and handles the display logic using the
/// `eframe::App` trait.
///
/// # Fields
/// - `nyq`: The `NyquistPlot` instance to be displayed.
/// - `init`: A flag to ensure that the plot bounds are set only once.
pub struct NyquistPlotEgui {
nyq: NyquistPlot,
init: bool,
}
impl NyquistPlotEgui {
/// Creates a new `NyquistPlotEgui` instance.
///
/// # Arguments
/// - `nyq`: The `NyquistPlot` instance to be rendered.
///
/// # Returns
/// A new instance of `NyquistPlotEgui`.
pub fn new(nyq: NyquistPlot) -> Self {
Self { nyq, init: false }
}
}
impl From<NyquistPlot> for NyquistPlotEgui {
fn from(value: NyquistPlot) -> Self {
Self::new(value)
}
}
impl eframe::App for NyquistPlotEgui {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
ui.with_layout(Layout::right_to_left(Align::RIGHT), |ui| {
if ui.button(egui::RichText::new("🏠").size(20.0)).clicked() {
*self = self.nyq.clone().into();
}
});
});
egui::CentralPanel::default().show(ctx, |ui| {
let plot =
egui_plot::Plot::new("Nyquist Plot").legend(Legend::default());
plot.show(ui, |plot_ui| {
if !self.init {
let data_iter = self
.nyq
.plot_data
.iter()
.flat_map(|nyq_data| nyq_data.data_points.iter());
let bounds_init = NyquistPlotEgui::get_plot_bounds(
self.nyq.options.x_limits,
self.nyq.options.y_limits,
data_iter,
);
plot_ui.set_plot_bounds(bounds_init);
}
NyquistPlotEgui::plot_data(plot_ui, &self.nyq.plot_data);
});
});
self.init = true;
}
}
impl NyquistPlotEgui {
fn get_plot_bounds<'a, I>(
x_limits: Option<[f64; 2]>,
y_limits: Option<[f64; 2]>,
data: I,
) -> PlotBounds
where
I: Iterator<Item = &'a Complex64>,
{
let xy_iter = data.map(|c| [c.re, c.im]);
get_2d_plot_bounds(x_limits, y_limits, xy_iter)
}
fn plot_data(plot_ui: &mut PlotUi, data: &[NyquistPlotData]) {
for data_i in data {
let plot_points = PlotPoints::from_iter(
data_i.data_points.iter().map(|c| [c.re, c.im]),
);
let mut line = Line::new(plot_points);
line = set_name_and_color_line(line, &data_i.name, &data_i.color);
plot_ui.line(line);
}
}
}
impl Plot for NyquistPlot {
/// Implements the `Plot` trait for `NyquistPlot`.
///
/// This implementation allows the `NyquistPlot` to be displayed in a window
/// by utilizing the `NyquistPlotEgui` struct.
fn show(
self,
width: usize,
height: usize,
name: &str,
) -> Result<(), Box<dyn std::error::Error + 'static>> {
NyquistPlotEgui::new(self).show(width, height, name)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{FrequencyResponse, systems::Tf, utils::traits::Continuous};
use egui_kittest::Harness;
#[test]
fn colors() {
let red = RGBAColor::RED;
let red_new = RGBAColor::new(255, 0, 0, 255);
assert_eq!(red_new, red);
}
#[test]
fn bodeplot() {
let bodeopts = BodePlotOptions::new("ERROR WRONG TITLE")
.set_title("Bodeplot title")
.set_x_limits([0.1, 100.]);
let mut bodeplot = BodePlot::new(bodeopts);
let sys: Tf<f64, Continuous> = Tf::new(&[0.0, 1.0], &[1.0, 1.0]);
// let (mag, phase, freq) = bode(sys, 0.1, 100.);
// let mag_phase_freq_vec: Vec<[f64; 3]> = mag
// .iter()
// .zip(phase.iter().zip(freq.iter()))
// .map(|(&mag, (&phase, &freq))| [mag, phase, freq])
// .collect();
let mag_phase_freq_vec = sys.bode(0.1, 100.);
bodeplot.add_system(
BodePlotData::from(mag_phase_freq_vec.clone()).set_name("Test"),
);
bodeplot.add_system(
BodePlotData::new(mag_phase_freq_vec.clone()).set_name("INLINE"),
);
let data = BodePlotData::new(mag_phase_freq_vec.clone())
.set_name("Test")
.set_color(RGBAColor::PURPLE);
bodeplot.add_system(data);
let mut data: BodePlotData = mag_phase_freq_vec.clone().into();
data = data.set_color(RGBAColor::BLACK).set_name("TEST");
bodeplot.add_system(data);
bodeplot.add_system(mag_phase_freq_vec.into());
let sys2: Tf<f64, Continuous> = Tf::new(&[1.0], &[1.0, 1.0]);
let mag_phase_freq_vec = sys2.bode(0.1, 100.);
bodeplot.add_system(mag_phase_freq_vec.into());
let app = BodePlotEgui::new(bodeplot);
let mut harness = Harness::new_eframe(|_| app);
harness.run_ok().unwrap();
}
#[test]
fn nyquistplot() {
let sys: Tf<f64, Continuous> = Tf::new(&[1.0], &[0.0, 1.0]);
let pade: Tf<f64, Continuous> = Tf::new(&[1.0, -1.0], &[1.0, 1.0]);