Skip to content

Commit

Permalink
Removed Angle type.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bitshifter committed Oct 4, 2019
1 parent a9c5325 commit e444125
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 358 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "glam"
version = "0.7.3" # remember to update html_root_url
version = "0.8.0" # remember to update html_root_url
edition = "2018"
authors = ["Cameron Hart <cameron.hart@gmail.com>"]
description = "A simple and fast 3D math library for games and graphics"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ and the look and feel of the API has solidified.
* vectors: `Vec3`, `Vec3`, `Vec4`
* square matrices: `Mat2`, `Mat3`, `Mat4`
* a quaternion type: `Quat`
* an angle type: `Angle`
* SSE2 opimized `sin_cos`

### SIMD
Expand Down
4 changes: 2 additions & 2 deletions benches/support/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(dead_code)]
use core::f32;
use glam::f32::{rad, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
use glam::f32::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};

pub struct PCG32 {
state: u64,
Expand Down Expand Up @@ -72,7 +72,7 @@ pub fn random_quat(rng: &mut PCG32) -> Quat {
let yaw = random_radians(rng);
let pitch = random_radians(rng);
let roll = random_radians(rng);
Quat::from_rotation_ypr(rad(yaw), rad(pitch), rad(roll))
Quat::from_rotation_ypr(yaw, pitch, roll)
}

pub fn random_mat2(rng: &mut PCG32) -> Mat2 {
Expand Down
185 changes: 0 additions & 185 deletions src/f32/angle.rs

This file was deleted.

33 changes: 33 additions & 0 deletions src/f32/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,36 @@ pub unsafe fn sin_cos_sse2(x: __m128) -> (__m128, __m128) {
_mm_xor_ps(xmm2, sign_bit_cos),
)
}

#[inline]
pub fn scalar_acos(value: f32) -> f32 {
// from DirectXMath XMScalarAcos
// Clamp input to [-1,1].
let nonnegative = value >= 0.0;
let x = value.abs();
let mut omx = 1.0 - x;
if omx < 0.0 {
omx = 0.0;
}
let root = omx.sqrt();

// 7-degree minimax approximation
#[allow(clippy::approx_constant)]
let mut result =
((((((-0.001_262_491_1 * x + 0.006_670_09) * x - 0.017_088_126) * x + 0.030_891_88) * x
- 0.050_174_303)
* x
+ 0.088_978_99)
* x
- 0.214_598_8)
* x
+ 1.570_796_3;
result *= root;

// acos(x) = pi - acos(-x) when x < 0
if nonnegative {
result
} else {
std::f32::consts::PI - result
}
}
41 changes: 1 addition & 40 deletions src/f32/glam_approx.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
use crate::f32::{Angle, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
use crate::f32::{Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec4};
use approx::{AbsDiffEq, RelativeEq, UlpsEq};

impl AbsDiffEq for Angle {
type Epsilon = <f32 as AbsDiffEq>::Epsilon;
fn default_epsilon() -> Self::Epsilon {
f32::default_epsilon()
}
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
let a1 = self.radians();
let a2 = other.radians();
a1.abs_diff_eq(&a2, epsilon)
}
}

impl RelativeEq for Angle {
fn default_max_relative() -> Self::Epsilon {
f32::default_max_relative()
}
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
let a1 = self.radians();
let a2 = other.radians();
a1.relative_eq(&a2, epsilon, max_relative)
}
}

impl UlpsEq for Angle {
fn default_max_ulps() -> u32 {
f32::default_max_ulps()
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
let a1 = self.radians();
let a2 = other.radians();
a1.ulps_eq(&a2, epsilon, max_ulps)
}
}

impl AbsDiffEq for Quat {
type Epsilon = <f32 as AbsDiffEq>::Epsilon;
fn default_epsilon() -> Self::Epsilon {
Expand Down
12 changes: 7 additions & 5 deletions src/f32/mat2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{super::Angle, Vec2, Vec4};
use super::{scalar_sin_cos, Vec2, Vec4};

#[cfg(feature = "rand")]
use rand::{
Expand Down Expand Up @@ -40,9 +40,10 @@ impl Mat2 {
Self(Vec4::new(x_axis.x(), x_axis.y(), y_axis.x(), y_axis.y()))
}

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

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

Expand Down
Loading

0 comments on commit e444125

Please sign in to comment.