Skip to content

Commit dc5ff76

Browse files
committed
add one or many
1 parent ed8d53f commit dc5ff76

File tree

4 files changed

+175
-170
lines changed

4 files changed

+175
-170
lines changed

.github/workflows/rust.yml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
name: Rust
2-
3-
on:
4-
push:
5-
branches: [ "main" ]
6-
pull_request:
7-
branches: [ "main" ]
8-
9-
env:
10-
CARGO_TERM_COLOR: always
11-
12-
jobs:
13-
build:
14-
15-
runs-on: ubuntu-latest
16-
17-
steps:
18-
- uses: actions/checkout@v4
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose

LICENSE.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
MIT License
2-
3-
Copyright (c) 2024 Pyxrs
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT License
2+
3+
Copyright (c) 2024 Pyxrs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# Pstd
2-
A bloated custom standard library for Rust
1+
# Pstd
2+
A bloated custom standard library for Rust

src/lib.rs

Lines changed: 130 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,130 @@
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

Comments
 (0)