Skip to content

Commit

Permalink
Auto merge of #305 - nical:clamp, r=kvark
Browse files Browse the repository at this point in the history
Implement clamp for vectors, points and size

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/euclid/305)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Sep 24, 2018
2 parents 5ab2b59 + 3640887 commit 7a4f6f7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 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 = "euclid"
version = "0.19.1"
version = "0.19.2"
authors = ["The Servo Project Developers"]
description = "Geometry primitives"
documentation = "https://docs.rs/euclid/"
Expand Down
10 changes: 10 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ impl<T: Float, U> TypedPoint2D<T, U> {
pub fn max(self, other: Self) -> Self {
point2(self.x.max(other.x), self.y.max(other.y))
}

#[inline]
pub fn clamp(&self, start: Self, end: Self) -> Self {
self.max(start).min(end)
}
}

impl<T: Copy + Mul<T, Output = T>, U> Mul<T> for TypedPoint2D<T, U> {
Expand Down Expand Up @@ -621,6 +626,11 @@ impl<T: Float, U> TypedPoint3D<T, U> {
self.z.max(other.z),
)
}

#[inline]
pub fn clamp(&self, start: Self, end: Self) -> Self {
self.max(start).min(end)
}
}

impl<T: Round, U> TypedPoint3D<T, U> {
Expand Down
26 changes: 25 additions & 1 deletion src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use scale::TypedScale;
use vector::{TypedVector2D, vec2, BoolVector2D};
use num::*;

use num_traits::{NumCast, Signed};
use num_traits::{Float, NumCast, Signed};
use core::fmt;
use core::ops::{Add, Div, Mul, Sub};
use core::marker::PhantomData;
Expand Down Expand Up @@ -329,6 +329,30 @@ impl<T: PartialEq, U> TypedSize2D<T, U> {
}
}

impl<T: Float, U> TypedSize2D<T, U> {
#[inline]
pub fn min(self, other: Self) -> Self {
size2(
self.width.min(other.width),
self.height.min(other.height),
)
}

#[inline]
pub fn max(self, other: Self) -> Self {
size2(
self.width.max(other.width),
self.height.max(other.height),
)
}

#[inline]
pub fn clamp(&self, start: Self, end: Self) -> Self {
self.max(start).min(end)
}
}


/// Shorthand for `TypedSize2D::new(w, h)`.
pub fn size2<T, U>(w: T, h: T) -> TypedSize2D<T, U> {
TypedSize2D::new(w, h)
Expand Down
21 changes: 21 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ impl<T: Float, U> TypedVector2D<T, U> {
pub fn max(self, other: Self) -> Self {
vec2(self.x.max(other.x), self.y.max(other.y))
}

#[inline]
pub fn clamp(&self, start: Self, end: Self) -> Self {
self.max(start).min(end)
}
}

impl<T: Copy + Mul<T, Output = T>, U> Mul<T> for TypedVector2D<T, U> {
Expand Down Expand Up @@ -746,6 +751,11 @@ impl<T: Float, U> TypedVector3D<T, U> {
self.z.max(other.z),
)
}

#[inline]
pub fn clamp(&self, start: Self, end: Self) -> Self {
self.max(start).min(end)
}
}

impl<T: Copy + Mul<T, Output = T>, U1, U2> Mul<TypedScale<T, U1, U2>> for TypedVector3D<T, U1> {
Expand Down Expand Up @@ -1376,6 +1386,17 @@ mod vector3d {
assert_eq!(result, vec3(2.0, 3.0, 5.0));
}

#[test]
pub fn test_clamp() {
let p1: Vec3 = vec3(1.0, -1.0, 5.0);
let p2: Vec3 = vec3(2.0, 5.0, 10.0);
let p3: Vec3 = vec3(-1.0, 2.0, 20.0);

let result = p3.clamp(p1, p2);

assert_eq!(result, vec3(1.0, 2.0, 10.0));
}

#[test]
pub fn test_scalar_mul() {
enum Mm {}
Expand Down

0 comments on commit 7a4f6f7

Please sign in to comment.