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 det =  self.y_axis.1*self.x_axis.0-self.y_axis.0*self.x_axis.1 ;
44                //println!("center of coordinate system i s{:?}",self.center);
45                //println!("point is {:?}", point);
46
47                let proj_x = ((point.x -self.center.0)*self.y_axis.1-(point.y -self.center.1)*self.y_axis.0  )/det;
48                let proj_y = (-(point.x -self.center.0)*self.x_axis.1+(point.y -self.center.1)*self.x_axis.0 )/det;
49
50                /*
51                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)).sqrt();
52                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)).sqrt();
53
54                 */
55                //println!("{:?}   {:?} PROJECTIONS ARE {:?}",det, (self.x_axis.0,self.x_axis.1),(proj_y,proj_x));
56                Point::new(proj_x,proj_y,Coordinates::RELATIVE(self.clone()))
57            }
58            Coordinates::RELATIVE(_) => {panic!("tried to from_absolute a point in a not absolute coordinate system :( ")}
59        }
60
61    }
62
63
64
65    pub fn plot(&self,plot: &mut Plot,color: &str){
66        let mut x_axis = Curve::new();
67        let scale = 1.0/self.x_axis.0;
68        x_axis.set_line_width(2.0);
69        x_axis.set_line_color(color);
70        x_axis.set_line_style("dashed");
71        x_axis.set_label(format!("x axis for {:?}",self.label).as_str());
72
73        x_axis.points_begin();
74        x_axis.points_add(self.center.0,self.center.1);
75        x_axis.points_add(self.center.0 + (self.x_axis.0)*scale, self.center.1 + (self.x_axis.1)*scale);
76        x_axis.points_end();
77
78
79        let mut y_axis = Curve::new();
80        y_axis.set_line_width(1.0);
81        y_axis.set_line_color(color);
82        y_axis.set_label(format!("y axis for {:?}",self.label).as_str());
83
84        y_axis.points_begin();
85        y_axis.points_add(self.center.0,self.center.1);
86        y_axis.points_add(self.center.0  + (self.y_axis.0)*scale,self.center.1  +  (self.y_axis.1)*scale);
87        y_axis.points_end();
88
89        plot.add(&x_axis);
90        plot.add(&y_axis);
91    }
92
93    /*
94
95    pub fn plot_coordinate_systems(coordinates: Vec<&Coordinates>,plot:&mut Plot){
96        let curves:Vec<(Curve,Curve)> = coordinates.iter().map(|coordinates: &&Coordinates|
97            match coordinates{
98                Coordinates::ABSOLUTE => {panic!("TODO")}
99                Coordinates::RELATIVE(coordinate_system) => {coordinate_system.plot()}
100            }
101        ).collect();
102
103        let mut legend = Legend::new();
104        legend.draw();
105
106        for axes in curves{
107            plot.add(&axes.0).grid_and_labels("x", "y");
108            plot.add(&axes.1).grid_and_labels("x", "y");
109        }
110        plot.add(&legend);
111
112    }
113
114     */
115}
116
117
118
119
120
121