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
extern crate cgmath;
pub use self::cgmath::*;
pub use self::cgmath::num_traits::{Float, NumCast, One};
use std::ops::Add;
pub fn pt1<S>(x: S) -> Point1<S> {
Point1 { x }
}
pub fn pt2<S>(x: S, y: S) -> Point2<S> {
Point2 { x, y }
}
pub fn pt3<S>(x: S, y: S, z: S) -> Point3<S> {
Point3 { x, y, z }
}
pub fn map_range<X, Y>(val: X, in_min: X, in_max: X, out_min: Y, out_max: Y) -> Y
where
X: NumCast,
Y: NumCast,
{
let val_f: f64 = NumCast::from(val).unwrap();
let in_min_f: f64 = NumCast::from(in_min).unwrap();
let in_max_f: f64 = NumCast::from(in_max).unwrap();
let out_min_f: f64 = NumCast::from(out_min).unwrap();
let out_max_f: f64 = NumCast::from(out_max).unwrap();
NumCast::from(
(val_f - in_min_f) / (in_max_f - in_min_f) * (out_max_f - out_min_f) + out_min_f
).unwrap()
}
pub fn partial_max<T>(a: T, b: T) -> T
where
T: PartialOrd,
{
if a >= b { a } else { b }
}
pub fn partial_min<T>(a: T, b: T) -> T
where
T: PartialOrd,
{
if a <= b { a } else { b }
}
pub fn clamp<T>(n: T, start: T, end: T) -> T
where
T: PartialOrd,
{
if start <= end {
if n < start { start } else if n > end { end } else { n }
} else {
if n < end { end } else if n > start { start } else { n }
}
}
pub fn two<S>() -> S
where
S: Add<Output=S> + One,
{
S::one() + S::one()
}
#[inline]
pub fn fmod<F>(numer: F, denom: F) -> F
where
F: Float,
{
let rquot: F = (numer / denom).floor();
numer - rquot * denom
}