forked from bitshifter/mathbench-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.rs
206 lines (158 loc) · 5.33 KB
/
vec.rs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
mod support;
use cgmath::{self, InnerSpace};
use glam;
use mathbench::mint_support::{random_mint_vec2, random_mint_vec3, random_mint_vec4};
use nalgebra;
use rand_pcg::Pcg64Mcg;
const NUM_ITERS: usize = 1024;
fn vec2_normalize_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv = random_mint_vec2(&mut rng);
let gv: glam::Vec2 = mv.into();
let gvn = gv.normalize();
let uv: ultraviolet::Vec2 = mv.into();
let uvn: mint::Vector2<f32> = uv.normalized().into();
let nv: nalgebra::Vector2<f32> = mv.into();
let nvn = nv.normalize();
let cv: cgmath::Vector2<f32> = mv.into();
let cvn = cv.normalize();
// use nalgebra as assumed correct answer
let mvn: mint::Vector2<f32> = nvn.into();
assert_ulps_eq!(cvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(gvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(uvn, mvn, epsilon = 1e-6);
}
fn vec3_dot_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv1 = random_mint_vec3(&mut rng);
let mv2 = random_mint_vec3(&mut rng);
let gv1: glam::Vec3 = mv1.into();
let gv2: glam::Vec3 = mv2.into();
let gd = gv1.dot(gv2);
let uv1: ultraviolet::Vec3 = mv1.into();
let uv2: ultraviolet::Vec3 = mv2.into();
let ud = uv1.dot(uv2);
let nv1: nalgebra::Vector3<f32> = mv1.into();
let nv2: nalgebra::Vector3<f32> = mv2.into();
let nd = nv1.dot(&nv2);
let cv1: cgmath::Vector3<f32> = mv1.into();
let cv2: cgmath::Vector3<f32> = mv2.into();
let cd = cv1.dot(cv2);
// use nalgebra as assumed correct answer
assert_ulps_eq!(cd, nd, epsilon = 1e-6);
assert_ulps_eq!(gd, nd, epsilon = 1e-6);
assert_ulps_eq!(ud, nd, epsilon = 1e-6);
}
fn vec3_cross_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv1 = random_mint_vec3(&mut rng);
let mv2 = random_mint_vec3(&mut rng);
let gv1: glam::Vec3 = mv1.into();
let gv2: glam::Vec3 = mv2.into();
let gc = gv1.cross(gv2);
let uv1: ultraviolet::Vec3 = mv1.into();
let uv2: ultraviolet::Vec3 = mv2.into();
let uc: mint::Vector3<f32> = uv1.cross(uv2).into();
let nv1: nalgebra::Vector3<f32> = mv1.into();
let nv2: nalgebra::Vector3<f32> = mv2.into();
let nc = nv1.cross(&nv2);
let cv1: cgmath::Vector3<f32> = mv1.into();
let cv2: cgmath::Vector3<f32> = mv2.into();
let cc = cv1.cross(cv2);
// use nalgebra as assumed correct answer
let mc: mint::Vector3<f32> = nc.into();
assert_ulps_eq!(cc, mc.into(), epsilon = 1e-6);
assert_ulps_eq!(gc, mc.into(), epsilon = 1e-6);
assert_ulps_eq!(uc, mc, epsilon = 1e-6);
}
fn vec3_normalize_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv = random_mint_vec3(&mut rng);
let gv: glam::Vec3 = mv.into();
let gvn = gv.normalize();
let uv: ultraviolet::Vec3 = mv.into();
let uvn: mint::Vector3<f32> = uv.normalized().into();
let nv: nalgebra::Vector3<f32> = mv.into();
let nvn = nv.normalize();
let cv: cgmath::Vector3<f32> = mv.into();
let cvn = cv.normalize();
// use nalgebra as assumed correct answer
let mvn: mint::Vector3<f32> = nvn.into();
assert_ulps_eq!(cvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(gvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(uvn, mvn, epsilon = 1e-6);
}
fn vec4_dot_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv1 = random_mint_vec4(&mut rng);
let mv2 = random_mint_vec4(&mut rng);
let gv1: glam::Vec4 = mv1.into();
let gv2: glam::Vec4 = mv2.into();
let gd = gv1.dot(gv2);
let uv1: ultraviolet::Vec4 = mv1.into();
let uv2: ultraviolet::Vec4 = mv2.into();
let ud = uv1.dot(uv2);
let nv1: nalgebra::Vector4<f32> = mv1.into();
let nv2: nalgebra::Vector4<f32> = mv2.into();
let nd = nv1.dot(&nv2);
let cv1: cgmath::Vector4<f32> = mv1.into();
let cv2: cgmath::Vector4<f32> = mv2.into();
let cd = cv1.dot(cv2);
// use nalgebra as assumed correct answer
assert_ulps_eq!(cd, nd, epsilon = 1e-6);
assert_ulps_eq!(gd, nd, epsilon = 1e-6);
assert_ulps_eq!(ud, nd, epsilon = 1e-6);
}
fn vec4_normalize_compare() {
let mut rng = Pcg64Mcg::new(rand::random());
let mv = random_mint_vec4(&mut rng);
let gv: glam::Vec4 = mv.into();
let gvn = gv.normalize();
let uv: ultraviolet::Vec4 = mv.into();
let uvn: mint::Vector4<f32> = uv.normalized().into();
let nv: nalgebra::Vector4<f32> = mv.into();
let nvn = nv.normalize();
let cv: cgmath::Vector4<f32> = mv.into();
let cvn = cv.normalize();
// use nalgebra as assumed correct answer
let mvn: mint::Vector4<f32> = nvn.into();
assert_ulps_eq!(cvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(gvn, mvn.into(), epsilon = 1e-6);
assert_ulps_eq!(uvn, mvn, epsilon = 1e-6);
}
#[test]
fn test_vec2_normalize() {
for _ in 0..NUM_ITERS {
vec2_normalize_compare();
}
}
#[test]
fn test_vec3_dot() {
for _ in 0..NUM_ITERS {
vec3_dot_compare();
}
}
#[test]
fn test_vec3_cross() {
for _ in 0..NUM_ITERS {
vec3_cross_compare();
}
}
#[test]
fn test_vec3_normalize() {
for _ in 0..NUM_ITERS {
vec3_normalize_compare();
}
}
#[test]
fn test_vec4_dot() {
for _ in 0..NUM_ITERS {
vec4_dot_compare();
}
}
#[test]
fn test_vec4_normalize() {
for _ in 0..NUM_ITERS {
vec4_normalize_compare();
}
}