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
use std::fmt::{Debug, Display};
use std::hash::Hash;
use bitflags::_core::ops::{AddAssign, MulAssign, SubAssign};
use nalgebra::allocator::Allocator;
use nalgebra::{DefaultAllocator, DimName, RealField, VectorN};
use num::{Bounded, CheckedAdd, CheckedMul, CheckedSub, FromPrimitive, Integer, ToPrimitive};
pub trait ThreadSafe: Sync + Send {}
impl<T> ThreadSafe for T where T: Sync + Send {}
pub trait Index:
Copy
+ Hash
+ Integer
+ Bounded
+ CheckedAdd
+ CheckedSub
+ CheckedMul
+ AddAssign
+ SubAssign
+ MulAssign
+ FromPrimitive
+ ToPrimitive
+ Default
+ Debug
+ Display
+ ThreadSafe
+ 'static
{
fn to_real<R: Real>(self) -> Option<R> {
R::from_f64(self.to_f64()?)
}
fn to_real_unchecked<R: Real>(self) -> R {
R::from_f64(self.to_f64().unwrap()).unwrap()
}
fn times(self, n: i32) -> Self {
self.mul(Self::from_i32(n).unwrap())
}
}
pub trait Real: RealField + FromPrimitive + ToPrimitive + Debug + Default + ThreadSafe {
fn try_convert<T: Real>(self) -> Option<T> {
Some(T::from_f64(self.to_f64()?)?)
}
fn try_convert_vec_from<R, D>(vec: &VectorN<R, D>) -> Option<VectorN<Self, D>>
where
R: Real,
D: DimName,
DefaultAllocator: Allocator<R, D>,
DefaultAllocator: Allocator<Self, D>,
{
let mut converted = VectorN::<Self, D>::zeros();
for i in 0..D::dim() {
converted[i] = vec[i].try_convert()?
}
Some(converted)
}
fn to_index<I: Index>(self) -> Option<I> {
I::from_f64(self.to_f64()?)
}
fn to_index_unchecked<I: Index>(self) -> I {
I::from_f64(self.to_f64().unwrap()).unwrap()
}
fn times(self, n: i32) -> Self {
self.mul(Self::from_i32(n).unwrap())
}
fn times_f64(self, x: f64) -> Self {
self.mul(Self::from_f64(x).unwrap())
}
}
impl<T> Index for T where
T: Copy
+ Hash
+ Integer
+ Bounded
+ CheckedAdd
+ CheckedSub
+ CheckedMul
+ AddAssign
+ SubAssign
+ MulAssign
+ FromPrimitive
+ ToPrimitive
+ Debug
+ Default
+ Display
+ ThreadSafe
+ 'static
{
}
impl<T: RealField + FromPrimitive + ToPrimitive + Debug + Default + ThreadSafe + 'static> Real
for T
{
}