Skip to main content

astroimsim_geometry/
coordinate_system.rs

1use plotpy::{Curve, Legend, Plot};
2use serde::{Deserialize, Serialize};
3use crate::points::Point;
4
5#[derive(Clone, Debug,Serialize)]
6pub enum Coordinates {
7    ABSOLUTE,
8    RELATIVE(CoordinateSystem),
9}
10
11#[derive(Clone, Debug,Serialize,Deserialize)]
12pub struct CoordinateSystem{
13    pub x_axis: (f64, f64),
14    pub y_axis: (f64,f64),
15    pub center: (f64,f64),
16    pub label: &'static str,
17}
18
19
20impl Coordinates{
21    pub fn plot(&self, plot:&mut Plot, color:&str){
22        match &self{
23            Coordinates::ABSOLUTE => {panic!("implement me :(")}
24            Coordinates::RELATIVE(c) => {c.plot(plot,color )}
25        }
26    }
27}
28
29impl CoordinateSystem{
30    pub fn new(x_axis:(f64,f64),y_axis:(f64,f64),center: (f64,f64),label:&'static str) -> CoordinateSystem{
31
32
33        CoordinateSystem{
34            x_axis,
35            y_axis,
36            center,
37            label,
38        }
39    }
40    pub fn point_from_absolute(&self, point:Point) -> Point{
41        match point.coordinates{
42            Coordinates::ABSOLUTE => {
43                let proj_x = (point.x*self.x_axis.0 + point.y*self.x_axis.1)/(self.x_axis.0.powi(2) + self.x_axis.1.powi(2));
44                let proj_y = (point.x*self.y_axis.0 + point.y*self.y_axis.1)/(self.y_axis.0.powi(2) + self.y_axis.1.powi(2));
45                //println!("{:?} PROJECTIONS ARE {:?}",(self.x_axis.0,self.x_axis.1),(proj_y,proj_x));
46                Point::new(proj_x,proj_y,Coordinates::RELATIVE(self.clone()))
47            }
48            Coordinates::RELATIVE(_) => {panic!("tried to from_absolute a point in a not absolute coordinate system :( ")}
49        }
50
51    }
52
53
54
55    pub fn plot(&self,plot: &mut Plot,color: &str){
56        let mut x_axis = Curve::new();
57        let scale = 1.0/self.x_axis.0;
58        x_axis.set_line_width(2.0);
59        x_axis.set_line_color(color);
60        x_axis.set_line_style("dashed");
61        x_axis.set_label(format!("x axis for {:?}",self.label).as_str());
62
63        x_axis.points_begin();
64        x_axis.points_add(self.center.0,self.center.1);
65        x_axis.points_add(self.center.0 + (self.x_axis.0)*scale, self.center.1 + (self.x_axis.1)*scale);
66        x_axis.points_end();
67
68
69        let mut y_axis = Curve::new();
70        y_axis.set_line_width(1.0);
71        y_axis.set_line_color(color);
72        y_axis.set_label(format!("y axis for {:?}",self.label).as_str());
73
74        y_axis.points_begin();
75        y_axis.points_add(self.center.0,self.center.1);
76        y_axis.points_add(self.center.0  + (self.y_axis.0)*scale,self.center.1  +  (self.y_axis.1)*scale);
77        y_axis.points_end();
78
79        plot.add(&x_axis);
80        plot.add(&y_axis);
81    }
82    
83    /*
84
85    pub fn plot_coordinate_systems(coordinates: Vec<&Coordinates>,plot:&mut Plot){
86        let curves:Vec<(Curve,Curve)> = coordinates.iter().map(|coordinates: &&Coordinates|
87            match coordinates{
88                Coordinates::ABSOLUTE => {panic!("TODO")}
89                Coordinates::RELATIVE(coordinate_system) => {coordinate_system.plot()}
90            }
91        ).collect();
92
93        let mut legend = Legend::new();
94        legend.draw();
95
96        for axes in curves{
97            plot.add(&axes.0).grid_and_labels("x", "y");
98            plot.add(&axes.1).grid_and_labels("x", "y");
99        }
100        plot.add(&legend);
101
102    }
103    
104     */
105}
106
107
108
109
110
111