Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
feature(polygon-math::Vector2): add Vector2 type and other utilities
Browse files Browse the repository at this point in the history
- Add Vector2 type to polygon-math.
-
  • Loading branch information
randomPoison committed Dec 5, 2015
1 parent 953ac4b commit 137f3c3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/polygon_math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub mod quaternion;
mod test;

pub use self::point::Point;
pub use self::vector::Vector3;
pub use self::matrix::{Matrix4, Matrix3};
pub use self::vector::{Vector2, Vector3};
pub use self::matrix::{Matrix3, Matrix4};
pub use self::color::Color;
pub use self::quaternion::Quaternion;

Expand Down
10 changes: 10 additions & 0 deletions lib/polygon_math/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ impl Point {
slice::from_raw_parts(data, len * 4)
}
}

pub fn slice_as_f32_slice(raw: &[f32]) -> &[Point] {
assert!(raw.len() % 4 == 0, "To convert a slice of f32 to a slice of Point it must have a length that is a multiple of 4");

unsafe {
slice::from_raw_parts(
raw.as_ptr() as *const Point,
raw.len() / 4)
}
}
}

impl Sub for Point {
Expand Down
25 changes: 25 additions & 0 deletions lib/polygon_math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ impl Vector3 {
self.x * self.x + self.y * self.y + self.z * self.z
}

// Safely reinterprets a slice of Vector3s to a slice of f32s. This is a cheap operation and
// does not copy any data.
pub fn as_ref(vectors: &[Vector3]) -> &[f32] {
unsafe {
::std::slice::from_raw_parts(
vectors.as_ptr() as *const f32,
vectors.len() * 3)
}
}

// pub fn cross(&self, rhs: Vector3) -> Vector3 {
// Vector3::new(
// self.y * rhs.z - self.z * rhs.y,
Expand Down Expand Up @@ -280,3 +290,18 @@ impl IndexMut<usize> for Vector3 {
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Vector2 {
pub x: f32,
pub y: f32,
}

impl Vector2 {
pub fn new(x: f32, y: f32) -> Vector2 {
Vector2 {
x: x,
y: y,
}
}
}

0 comments on commit 137f3c3

Please sign in to comment.