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
use pyo3::prelude::*;
use std::fmt;
use crate::Etype;
use crate::Vec3;
///
/// Struct defining a point on the surface of a model grid (e.g. of a star or disc etc.)
/// A `Point` has a position, a direction which is the surface normal, an area,
/// a relative gravity, a vector of phase pairs defining when the point is eclipsed by
/// another model component, and a flux.
///
#[pyclass(skip_from_py_object)]
#[derive(Clone, Debug)]
pub struct Point {
#[pyo3(get)]
pub position: Vec3,
#[pyo3(get)]
pub direction: Vec3,
#[pyo3(get)]
pub area: f32,
#[pyo3(get)]
pub gravity: f32,
#[pyo3(get)]
pub eclipse: Etype,
#[pyo3(get)]
pub flux: f32,
}
#[pymethods]
impl Point {
///
/// Creates a new Point.
///
#[new]
pub fn new(position: Vec3, direction: Vec3, area: f64, gravity: f64, eclipse: Etype) -> Self {
Self {
position,
direction,
area: area as f32,
gravity: gravity as f32,
eclipse,
flux: 0.0,
}
}
///
/// sets the point's flux.
///
pub fn set_flux(&mut self, flux: f32) {
self.flux = flux;
}
///
///checks that the given phase is not during one of the
/// phase ranges when the point is eclipsed.
///
pub fn is_visible(&self, phase: f64) -> bool {
let phi: f64 = phase - phase.floor();
for &(p1, p2) in &self.eclipse {
if (phi >= p1 && phi <= p2) || phi <= p2 - 1.0 {
return false;
}
}
true
}
///
/// This version of is_visible will not correct for phases outside
/// of expected range to speed up large loops.
/// run phase = phase - phase.floor();
/// outside of loop beforehand
///
pub fn is_visible_phase_normed(&self, phase: f64) -> bool {
for &(p1, p2) in &self.eclipse {
if (phase >= p1 && phase <= p2) || phase <= p2 - 1.0 {
return false;
}
}
true
}
fn __repr__(&self) -> String {
// We use the `format!` macro to create a string. Its first argument is a
// format string, followed by any number of parameters which replace the
// `{}`'s in the format string.
format!("Point({}, {}, {}, {}, {:?}, {})", self.position, self.direction, self.area, self.gravity, self.eclipse, self.flux)
}
}
impl Default for Point {
fn default() -> Self {
Self::new(
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 0.0),
0.0,
0.0,
vec![(0.0, 0.0)],
)
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Point({}, {}, {}, {}, {:?}, {})",
self.position,
self.direction,
self.area,
self.gravity,
self.eclipse,
self.flux
)
}
}