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