Skip to content

Commit e444125

Browse files
committed
Removed Angle type.
Everything that used to take `Angle` now takes `f32` in radians. The only exception is functions that take FOV for creating perspective functions, these expect angles in degrees. Fixes #22.
1 parent a9c5325 commit e444125

18 files changed

+155
-358
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "glam"
3-
version = "0.7.3" # remember to update html_root_url
3+
version = "0.8.0" # remember to update html_root_url
44
edition = "2018"
55
authors = ["Cameron Hart <cameron.hart@gmail.com>"]
66
description = "A simple and fast 3D math library for games and graphics"

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ and the look and feel of the API has solidified.
1616
* vectors: `Vec3`, `Vec3`, `Vec4`
1717
* square matrices: `Mat2`, `Mat3`, `Mat4`
1818
* a quaternion type: `Quat`
19-
* an angle type: `Angle`
2019
* SSE2 opimized `sin_cos`
2120

2221
### SIMD

benches/support/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(dead_code)]
22
use core::f32;
3-
use glam::f32::{rad, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
3+
use glam::f32::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
44

55
pub struct PCG32 {
66
state: u64,
@@ -72,7 +72,7 @@ pub fn random_quat(rng: &mut PCG32) -> Quat {
7272
let yaw = random_radians(rng);
7373
let pitch = random_radians(rng);
7474
let roll = random_radians(rng);
75-
Quat::from_rotation_ypr(rad(yaw), rad(pitch), rad(roll))
75+
Quat::from_rotation_ypr(yaw, pitch, roll)
7676
}
7777

7878
pub fn random_mat2(rng: &mut PCG32) -> Mat2 {

src/f32/angle.rs

-185
This file was deleted.

src/f32/funcs.rs

+33
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,36 @@ pub unsafe fn sin_cos_sse2(x: __m128) -> (__m128, __m128) {
139139
_mm_xor_ps(xmm2, sign_bit_cos),
140140
)
141141
}
142+
143+
#[inline]
144+
pub fn scalar_acos(value: f32) -> f32 {
145+
// from DirectXMath XMScalarAcos
146+
// Clamp input to [-1,1].
147+
let nonnegative = value >= 0.0;
148+
let x = value.abs();
149+
let mut omx = 1.0 - x;
150+
if omx < 0.0 {
151+
omx = 0.0;
152+
}
153+
let root = omx.sqrt();
154+
155+
// 7-degree minimax approximation
156+
#[allow(clippy::approx_constant)]
157+
let mut result =
158+
((((((-0.001_262_491_1 * x + 0.006_670_09) * x - 0.017_088_126) * x + 0.030_891_88) * x
159+
- 0.050_174_303)
160+
* x
161+
+ 0.088_978_99)
162+
* x
163+
- 0.214_598_8)
164+
* x
165+
+ 1.570_796_3;
166+
result *= root;
167+
168+
// acos(x) = pi - acos(-x) when x < 0
169+
if nonnegative {
170+
result
171+
} else {
172+
std::f32::consts::PI - result
173+
}
174+
}

src/f32/glam_approx.rs

+1-40
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,6 @@
1-
use crate::f32::{Angle, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
1+
use crate::f32::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
22
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
33

4-
impl AbsDiffEq for Angle {
5-
type Epsilon = <f32 as AbsDiffEq>::Epsilon;
6-
fn default_epsilon() -> Self::Epsilon {
7-
f32::default_epsilon()
8-
}
9-
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
10-
let a1 = self.radians();
11-
let a2 = other.radians();
12-
a1.abs_diff_eq(&a2, epsilon)
13-
}
14-
}
15-
16-
impl RelativeEq for Angle {
17-
fn default_max_relative() -> Self::Epsilon {
18-
f32::default_max_relative()
19-
}
20-
fn relative_eq(
21-
&self,
22-
other: &Self,
23-
epsilon: Self::Epsilon,
24-
max_relative: Self::Epsilon,
25-
) -> bool {
26-
let a1 = self.radians();
27-
let a2 = other.radians();
28-
a1.relative_eq(&a2, epsilon, max_relative)
29-
}
30-
}
31-
32-
impl UlpsEq for Angle {
33-
fn default_max_ulps() -> u32 {
34-
f32::default_max_ulps()
35-
}
36-
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
37-
let a1 = self.radians();
38-
let a2 = other.radians();
39-
a1.ulps_eq(&a2, epsilon, max_ulps)
40-
}
41-
}
42-
434
impl AbsDiffEq for Quat {
445
type Epsilon = <f32 as AbsDiffEq>::Epsilon;
456
fn default_epsilon() -> Self::Epsilon {

src/f32/mat2.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{super::Angle, Vec2, Vec4};
1+
use super::{scalar_sin_cos, Vec2, Vec4};
22

33
#[cfg(feature = "rand")]
44
use rand::{
@@ -40,9 +40,10 @@ impl Mat2 {
4040
Self(Vec4::new(x_axis.x(), x_axis.y(), y_axis.x(), y_axis.y()))
4141
}
4242

43+
/// Create a 2x2 matrix containing scale and rotation (in radians).
4344
#[inline]
44-
pub fn from_scale_angle(scale: Vec2, angle: Angle) -> Self {
45-
let (sin, cos) = angle.sin_cos();
45+
pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
46+
let (sin, cos) = scalar_sin_cos(angle);
4647
let (scale_x, scale_y) = scale.into();
4748
Self(Vec4::new(
4849
cos * scale_x,
@@ -52,9 +53,10 @@ impl Mat2 {
5253
))
5354
}
5455

56+
/// Create a 2x2 matrix containing a rotation (in radians).
5557
#[inline]
56-
pub fn from_angle(angle: Angle) -> Self {
57-
let (sin, cos) = angle.sin_cos();
58+
pub fn from_angle(angle: f32) -> Self {
59+
let (sin, cos) = scalar_sin_cos(angle);
5860
Self(Vec4::new(cos, sin, -sin, cos))
5961
}
6062

0 commit comments

Comments
 (0)