|
1 | | -pub use chrono as time; |
2 | | -pub use closure; |
3 | | -#[cfg(feature = "rand")] |
4 | | -pub use fastrand; |
5 | | -#[cfg(feature = "image")] |
6 | | -pub use image; |
7 | | -#[cfg(feature = "log")] |
8 | | -pub use log; |
9 | | -#[cfg(feature = "rand")] |
10 | | -pub use rand; |
11 | | -pub use rayon; |
12 | | -pub use relative_path as path; |
13 | | - |
14 | | -pub mod prelude { |
15 | | - pub use closure::closure; |
16 | | - #[cfg(feature = "log")] |
17 | | - pub use log::{debug, error, info, trace, warn}; |
18 | | - pub use std::time::{Duration, Instant}; |
19 | | -} |
20 | | - |
21 | | -pub mod anyhow { |
22 | | - pub use anyhow::{anyhow, bail, ensure, Chain, Context, Error, Ok}; |
23 | | - pub type AnyResult<T> = anyhow::Result<T>; |
24 | | -} |
25 | | - |
26 | | -#[cfg(feature = "async")] |
27 | | -pub mod r#async { |
28 | | - pub use futures::*; |
29 | | -} |
30 | | - |
31 | | -pub mod collections { |
32 | | - pub use ahash as insecure; |
33 | | -} |
34 | | - |
35 | | -/// Concurrency |
36 | | -pub mod sync { |
37 | | - // Channels |
38 | | - pub use flume::*; |
39 | | - // Locks |
40 | | - pub use parking_lot::*; |
41 | | - // Collections |
42 | | - pub use scc::*; |
43 | | -} |
44 | | - |
45 | | -/// Returns input value clamped to the interval `[min, max]`. |
46 | | -#[must_use] |
47 | | -#[inline] |
48 | | -pub fn clamp<T: PartialOrd>(val: T, min: T, max: T) -> T { |
49 | | - if val > min { |
50 | | - if val < max { |
51 | | - val |
52 | | - } else { |
53 | | - max |
54 | | - } |
55 | | - } else { |
56 | | - min |
57 | | - } |
58 | | -} |
59 | | - |
60 | | -/// Returns input value clamped to the interval `[min, max]`. |
61 | | -#[inline] |
62 | | -pub fn clamp_ref<T: PartialOrd>(val: &mut T, min: T, max: T) { |
63 | | - if *val > min { |
64 | | - if *val > max { |
65 | | - *val = max; |
66 | | - } |
67 | | - } else { |
68 | | - *val = min; |
69 | | - } |
70 | | -} |
71 | | - |
72 | | -pub trait ClampRef { |
73 | | - fn clamp_ref(&mut self, min: Self, max: Self); |
74 | | -} |
75 | | - |
76 | | -impl<T: PartialOrd> ClampRef for T { |
77 | | - fn clamp_ref(&mut self, min: Self, max: Self) { |
78 | | - clamp_ref(self, min, max); |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -#[cfg(feature = "game")] |
83 | | -pub mod game { |
84 | | - pub use glam; |
85 | | - pub use noise; |
86 | | - |
87 | | - pub fn sun_direction(time_of_day: f32, latitude: f32, time_of_year: f32) -> glam::Vec3A { |
88 | | - use std::f32::consts::PI; |
89 | | - |
90 | | - // Convert time of day from [0, 1] to [-π, π] |
91 | | - let solar_time_rads = 2.0 * PI * (time_of_day - 0.5); |
92 | | - |
93 | | - // Convert time of year from [0, 1] to [-0.41, 0.41] |
94 | | - // offset by 0.25 so declination is 0 at equinoxes |
95 | | - let declination_rads = (2.0 * PI * (time_of_year - 0.25)).sin() * 23.45f32.to_radians(); |
96 | | - |
97 | | - // Convert latitude from [0, 1] to [-π/2, π/2] |
98 | | - let latitude_rads = PI * (latitude - 0.5); |
99 | | - |
100 | | - // equations adapted from https://stackoverflow.com/questions/8708048/position-of-the-sun-given-time-of-day-latitude-and-longitude |
101 | | - let zenith_rads = (latitude_rads.sin() * declination_rads.sin() |
102 | | - + latitude_rads.cos() * declination_rads.cos() * solar_time_rads.cos()) |
103 | | - .acos(); |
104 | | - |
105 | | - let mut azimuth_rads = ((latitude_rads.sin() * zenith_rads.cos() - declination_rads.sin()) |
106 | | - / (latitude_rads.cos() * zenith_rads.sin())) |
107 | | - .acos(); |
108 | | - |
109 | | - let elevation_rads = (declination_rads.sin() * latitude_rads.sin() |
110 | | - + declination_rads.cos() * latitude_rads.cos() * solar_time_rads.cos()) |
111 | | - .asin(); |
112 | | - |
113 | | - if solar_time_rads > 0.0f32 { |
114 | | - azimuth_rads += PI; |
115 | | - } else { |
116 | | - azimuth_rads = 3.0 * PI - azimuth_rads; |
117 | | - } |
118 | | - |
119 | | - glam::Vec3A::new( |
120 | | - azimuth_rads.sin() * elevation_rads.cos(), |
121 | | - elevation_rads.sin(), |
122 | | - azimuth_rads.cos() * elevation_rads.cos(), |
123 | | - ) |
124 | | - } |
125 | | -} |
| 1 | +pub use chrono as time; |
| 2 | +pub use closure; |
| 3 | +#[cfg(feature = "rand")] |
| 4 | +pub use fastrand; |
| 5 | +#[cfg(feature = "image")] |
| 6 | +pub use image; |
| 7 | +#[cfg(feature = "log")] |
| 8 | +pub use log; |
| 9 | +#[cfg(feature = "rand")] |
| 10 | +pub use rand; |
| 11 | +pub use rayon; |
| 12 | +pub use relative_path as path; |
| 13 | + |
| 14 | +pub mod prelude { |
| 15 | + pub use closure::closure; |
| 16 | + #[cfg(feature = "log")] |
| 17 | + pub use log::{debug, error, info, trace, warn}; |
| 18 | + pub use std::time::{Duration, Instant}; |
| 19 | +} |
| 20 | + |
| 21 | +pub mod anyhow { |
| 22 | + pub use anyhow::{anyhow, bail, ensure, Chain, Context, Error, Ok}; |
| 23 | + pub type AnyResult<T> = anyhow::Result<T>; |
| 24 | +} |
| 25 | + |
| 26 | +#[cfg(feature = "async")] |
| 27 | +pub mod r#async { |
| 28 | + pub use futures::*; |
| 29 | +} |
| 30 | + |
| 31 | +pub mod collections { |
| 32 | + pub use ahash as insecure; |
| 33 | + |
| 34 | + pub enum OneOrMany<T> { |
| 35 | + One(T), |
| 36 | + Many(Vec<T>), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Concurrency |
| 41 | +pub mod sync { |
| 42 | + // Channels |
| 43 | + pub use flume::*; |
| 44 | + // Locks |
| 45 | + pub use parking_lot::*; |
| 46 | + // Collections |
| 47 | + pub use scc::*; |
| 48 | +} |
| 49 | + |
| 50 | +/// Returns input value clamped to the interval `[min, max]`. |
| 51 | +#[must_use] |
| 52 | +#[inline] |
| 53 | +pub fn clamp<T: PartialOrd>(val: T, min: T, max: T) -> T { |
| 54 | + if val > min { |
| 55 | + if val < max { |
| 56 | + val |
| 57 | + } else { |
| 58 | + max |
| 59 | + } |
| 60 | + } else { |
| 61 | + min |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Returns input value clamped to the interval `[min, max]`. |
| 66 | +#[inline] |
| 67 | +pub fn clamp_ref<T: PartialOrd>(val: &mut T, min: T, max: T) { |
| 68 | + if *val > min { |
| 69 | + if *val > max { |
| 70 | + *val = max; |
| 71 | + } |
| 72 | + } else { |
| 73 | + *val = min; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub trait ClampRef { |
| 78 | + fn clamp_ref(&mut self, min: Self, max: Self); |
| 79 | +} |
| 80 | + |
| 81 | +impl<T: PartialOrd> ClampRef for T { |
| 82 | + fn clamp_ref(&mut self, min: Self, max: Self) { |
| 83 | + clamp_ref(self, min, max); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(feature = "game")] |
| 88 | +pub mod game { |
| 89 | + pub use glam; |
| 90 | + pub use noise; |
| 91 | + |
| 92 | + pub fn sun_direction(time_of_day: f32, latitude: f32, time_of_year: f32) -> glam::Vec3A { |
| 93 | + use std::f32::consts::PI; |
| 94 | + |
| 95 | + // Convert time of day from [0, 1] to [-π, π] |
| 96 | + let solar_time_rads = 2.0 * PI * (time_of_day - 0.5); |
| 97 | + |
| 98 | + // Convert time of year from [0, 1] to [-0.41, 0.41] |
| 99 | + // offset by 0.25 so declination is 0 at equinoxes |
| 100 | + let declination_rads = (2.0 * PI * (time_of_year - 0.25)).sin() * 23.45f32.to_radians(); |
| 101 | + |
| 102 | + // Convert latitude from [0, 1] to [-π/2, π/2] |
| 103 | + let latitude_rads = PI * (latitude - 0.5); |
| 104 | + |
| 105 | + // equations adapted from https://stackoverflow.com/questions/8708048/position-of-the-sun-given-time-of-day-latitude-and-longitude |
| 106 | + let zenith_rads = (latitude_rads.sin() * declination_rads.sin() |
| 107 | + + latitude_rads.cos() * declination_rads.cos() * solar_time_rads.cos()) |
| 108 | + .acos(); |
| 109 | + |
| 110 | + let mut azimuth_rads = ((latitude_rads.sin() * zenith_rads.cos() - declination_rads.sin()) |
| 111 | + / (latitude_rads.cos() * zenith_rads.sin())) |
| 112 | + .acos(); |
| 113 | + |
| 114 | + let elevation_rads = (declination_rads.sin() * latitude_rads.sin() |
| 115 | + + declination_rads.cos() * latitude_rads.cos() * solar_time_rads.cos()) |
| 116 | + .asin(); |
| 117 | + |
| 118 | + if solar_time_rads > 0.0f32 { |
| 119 | + azimuth_rads += PI; |
| 120 | + } else { |
| 121 | + azimuth_rads = 3.0 * PI - azimuth_rads; |
| 122 | + } |
| 123 | + |
| 124 | + glam::Vec3A::new( |
| 125 | + azimuth_rads.sin() * elevation_rads.cos(), |
| 126 | + elevation_rads.sin(), |
| 127 | + azimuth_rads.cos() * elevation_rads.cos(), |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
0 commit comments