Skip to main content

roche/
point.rs

1use crate::Etype;
2use crate::Vec3;
3
4///
5/// Struct defining a point on the surface of a model grid (e.g. of a star or disc etc.)
6/// A `Point` has a position, a direction which is the surface normal, an area,
7/// a relative gravity, a vector of phase pairs defining when the point is eclipsed by
8/// another model component, and a flux.
9///
10#[derive(Clone, Debug)]
11pub struct Point {
12    pub position: Vec3,
13    pub direction: Vec3,
14    pub area: f32,
15    pub gravity: f32,
16    pub eclipse: Etype,
17    pub flux: f32,
18}
19
20impl Point {
21    ///
22    /// Creates a new Point.
23    ///
24    pub fn new(position: Vec3, direction: Vec3, area: f64, gravity: f64, eclipse: Etype) -> Self {
25        Self {
26            position,
27            direction,
28            area: area as f32,
29            gravity: gravity as f32,
30            eclipse,
31            flux: 0.0,
32        }
33    }
34    ///
35    /// sets the point's flux.
36    ///
37    pub fn set_flux(&mut self, flux: f32) {
38        self.flux = flux;
39    }
40
41    ///
42    ///checks that the given phase is not during one of the
43    /// phase ranges when the point is eclipsed.
44    ///  
45    pub fn is_visible(&self, phase: f64) -> bool {
46        let phi: f64 = phase - phase.floor();
47        for &(p1, p2) in &self.eclipse {
48            if (phi >= p1 && phi <= p2) || phi <= p2 - 1.0 {
49                return false;
50            }
51        }
52        true
53    }
54
55    ///
56    /// This version of is_visible will not correct for phases outside
57    /// of expected range to speed up large loops.
58    /// run phase = phase - phase.floor();
59    /// outside of loop beforehand
60    ///
61    pub fn is_visible_phase_normed(&self, phase: f64) -> bool {
62        for &(p1, p2) in &self.eclipse {
63            if (phase >= p1 && phase <= p2) || phase <= p2 - 1.0 {
64                return false;
65            }
66        }
67        true
68    }
69}
70
71impl Default for Point {
72    fn default() -> Self {
73        Self::new(
74            Vec3::new(0.0, 0.0, 0.0),
75            Vec3::new(0.0, 0.0, 0.0),
76            0.0,
77            0.0,
78            vec![(0.0, 0.0)],
79        )
80    }
81}