Skip to content

Commit

Permalink
Auto merge of #198 - nical:adjective-form, r=kvark
Browse files Browse the repository at this point in the history
Use verbs instead of adjectives for transformations with #[must_use].

Fixes #195.

<!-- 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/198)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored May 29, 2017
2 parents 08ab185 + 137660d commit 13b3ddc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 30 deletions.
6 changes: 6 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ impl<T: Round, U> TypedPoint2D<T, U> {
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example `{ -0.1, -0.8 }.round() == { 0.0, -1.0 }`.
#[inline]
#[must_use]
pub fn round(&self) -> Self {
point2(self.x.round(), self.y.round())
}
Expand All @@ -238,6 +239,7 @@ impl<T: Ceil, U> TypedPoint2D<T, U> {
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example `{ -0.1, -0.8 }.ceil() == { 0.0, 0.0 }`.
#[inline]
#[must_use]
pub fn ceil(&self) -> Self {
point2(self.x.ceil(), self.y.ceil())
}
Expand All @@ -249,6 +251,7 @@ impl<T: Floor, U> TypedPoint2D<T, U> {
/// This behavior is preserved for negative values (unlike the basic cast).
/// For example `{ -0.1, -0.8 }.floor() == { -1.0, -1.0 }`.
#[inline]
#[must_use]
pub fn floor(&self) -> Self {
point2(self.x.floor(), self.y.floor())
}
Expand Down Expand Up @@ -491,6 +494,7 @@ impl<T: Round, U> TypedPoint3D<T, U> {
///
/// This behavior is preserved for negative values (unlike the basic cast).
#[inline]
#[must_use]
pub fn round(&self) -> Self {
point3(self.x.round(), self.y.round(), self.z.round())
}
Expand All @@ -501,6 +505,7 @@ impl<T: Ceil, U> TypedPoint3D<T, U> {
///
/// This behavior is preserved for negative values (unlike the basic cast).
#[inline]
#[must_use]
pub fn ceil(&self) -> Self {
point3(self.x.ceil(), self.y.ceil(), self.z.ceil())
}
Expand All @@ -511,6 +516,7 @@ impl<T: Floor, U> TypedPoint3D<T, U> {
///
/// This behavior is preserved for negative values (unlike the basic cast).
#[inline]
#[must_use]
pub fn floor(&self) -> Self {
point3(self.x.floor(), self.y.floor(), self.z.floor())
}
Expand Down
7 changes: 7 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T

/// Returns the same rectangle, translated by a vector.
#[inline]
#[must_use]
pub fn translate(&self, by: &TypedVector2D<T, U>) -> TypedRect<T, U> {
Self::new(self.origin + *by, self.size)
}
Expand All @@ -181,6 +182,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
}

#[inline]
#[must_use]
pub fn inflate(&self, width: T, height: T) -> TypedRect<T, U> {
TypedRect::new(
TypedPoint2D::new(self.origin.x - width, self.origin.y - height),
Expand All @@ -189,6 +191,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
}

#[inline]
#[must_use]
pub fn inflate_typed(&self, width: Length<T, U>, height: Length<T, U>) -> TypedRect<T, U> {
self.inflate(width.get(), height.get())
}
Expand All @@ -209,6 +212,7 @@ where T: Copy + Clone + Zero + PartialOrd + PartialEq + Add<T, Output=T> + Sub<T
}

#[inline]
#[must_use]
pub fn translate_by_size(&self, size: &TypedSize2D<T, U>) -> TypedRect<T, U> {
self.translate(&size.to_vector())
}
Expand Down Expand Up @@ -366,6 +370,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect
/// avoid pixel rounding errors.
/// Note that this is *not* rounding to nearest integer if the values are negative.
/// They are always rounding as floor(n + 0.5).
#[must_use]
pub fn round(&self) -> Self {
let origin = self.origin.round();
let size = self.origin.add_size(&self.size).round() - origin;
Expand All @@ -374,6 +379,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect

/// Return a rectangle with edges rounded to integer coordinates, such that
/// the original rectangle contains the resulting rectangle.
#[must_use]
pub fn round_in(&self) -> Self {
let origin = self.origin.ceil();
let size = self.origin.add_size(&self.size).floor() - origin;
Expand All @@ -382,6 +388,7 @@ impl<T: Floor + Ceil + Round + Add<T, Output=T> + Sub<T, Output=T>, U> TypedRect

/// Return a rectangle with edges rounded to integer coordinates, such that
/// the original rectangle is contained in the resulting rectangle.
#[must_use]
pub fn round_out(&self) -> Self {
let origin = self.origin.floor();
let size = self.origin.add_size(&self.size).ceil() - origin;
Expand Down
42 changes: 27 additions & 15 deletions src/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ where T: Copy + Clone +

/// Returns the multiplication of the two matrices such that mat's transformation
/// applies after self's transformation.
#[must_use]
pub fn post_mul<NewDst>(&self, mat: &TypedTransform2D<T, Dst, NewDst>) -> TypedTransform2D<T, Src, NewDst> {
TypedTransform2D::row_major(
self.m11 * mat.m11 + self.m12 * mat.m21,
Expand All @@ -144,6 +145,7 @@ where T: Copy + Clone +

/// Returns the multiplication of the two matrices such that mat's transformation
/// applies before self's transformation.
#[must_use]
pub fn pre_mul<NewSrc>(&self, mat: &TypedTransform2D<T, NewSrc, Src>) -> TypedTransform2D<T, NewSrc, Dst> {
mat.post_mul(self)
}
Expand All @@ -159,12 +161,14 @@ where T: Copy + Clone +
}

/// Applies a translation after self's transformation and returns the resulting transform.
pub fn post_translated(&self, v: TypedVector2D<T, Dst>) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn post_translate(&self, v: TypedVector2D<T, Dst>) -> TypedTransform2D<T, Src, Dst> {
self.post_mul(&TypedTransform2D::create_translation(v.x, v.y))
}

/// Applies a translation before self's transformation and returns the resulting transform.
pub fn pre_translated(&self, v: TypedVector2D<T, Src>) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn pre_translate(&self, v: TypedVector2D<T, Src>) -> TypedTransform2D<T, Src, Dst> {
self.pre_mul(&TypedTransform2D::create_translation(v.x, v.y))
}

Expand All @@ -179,12 +183,14 @@ where T: Copy + Clone +
}

/// Applies a scale after self's transformation and returns the resulting transform.
pub fn post_scaled(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn post_scale(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
self.post_mul(&TypedTransform2D::create_scale(x, y))
}

/// Applies a scale before self's transformation and returns the resulting transform.
pub fn pre_scaled(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn pre_scale(&self, x: T, y: T) -> TypedTransform2D<T, Src, Dst> {
TypedTransform2D::row_major(
self.m11 * x, self.m12,
self.m21, self.m22 * y,
Expand All @@ -205,24 +211,28 @@ where T: Copy + Clone +
}

/// Applies a rotation after self's transformation and returns the resulting transform.
pub fn post_rotated(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn post_rotate(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
self.post_mul(&TypedTransform2D::create_rotation(theta))
}

/// Applies a rotation after self's transformation and returns the resulting transform.
pub fn pre_rotated(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
#[must_use]
pub fn pre_rotate(&self, theta: Radians<T>) -> TypedTransform2D<T, Src, Dst> {
self.pre_mul(&TypedTransform2D::create_rotation(theta))
}

/// Returns the given point transformed by this transform.
#[inline]
#[must_use]
pub fn transform_point(&self, point: &TypedPoint2D<T, Src>) -> TypedPoint2D<T, Dst> {
TypedPoint2D::new(point.x * self.m11 + point.y * self.m21 + self.m31,
point.x * self.m12 + point.y * self.m22 + self.m32)
}

/// Returns the given vector transformed by this matrix.
#[inline]
#[must_use]
pub fn transform_vector(&self, vec: &TypedVector2D<T, Src>) -> TypedVector2D<T, Dst> {
vec2(vec.x * self.m11 + vec.y * self.m21,
vec.x * self.m12 + vec.y * self.m22)
Expand All @@ -231,6 +241,7 @@ where T: Copy + Clone +
/// Returns a rectangle that encompasses the result of transforming the given rectangle by this
/// transform.
#[inline]
#[must_use]
pub fn transform_rect(&self, rect: &TypedRect<T, Src>) -> TypedRect<T, Dst> {
TypedRect::from_points(&[
self.transform_point(&rect.origin),
Expand All @@ -246,6 +257,7 @@ where T: Copy + Clone +
}

/// Returns the inverse transform if possible.
#[must_use]
pub fn inverse(&self) -> Option<TypedTransform2D<T, Dst, Src>> {
let det = self.determinant();

Expand Down Expand Up @@ -325,8 +337,8 @@ mod test {
#[test]
pub fn test_translation() {
let t1 = Mat::create_translation(1.0, 2.0);
let t2 = Mat::identity().pre_translated(vec2(1.0, 2.0));
let t3 = Mat::identity().post_translated(vec2(1.0, 2.0));
let t2 = Mat::identity().pre_translate(vec2(1.0, 2.0));
let t3 = Mat::identity().post_translate(vec2(1.0, 2.0));
assert_eq!(t1, t2);
assert_eq!(t1, t3);

Expand All @@ -338,8 +350,8 @@ mod test {
#[test]
pub fn test_rotation() {
let r1 = Mat::create_rotation(rad(FRAC_PI_2));
let r2 = Mat::identity().pre_rotated(rad(FRAC_PI_2));
let r3 = Mat::identity().post_rotated(rad(FRAC_PI_2));
let r2 = Mat::identity().pre_rotate(rad(FRAC_PI_2));
let r3 = Mat::identity().post_rotate(rad(FRAC_PI_2));
assert_eq!(r1, r2);
assert_eq!(r1, r3);

Expand All @@ -351,8 +363,8 @@ mod test {
#[test]
pub fn test_scale() {
let s1 = Mat::create_scale(2.0, 3.0);
let s2 = Mat::identity().pre_scaled(2.0, 3.0);
let s3 = Mat::identity().post_scaled(2.0, 3.0);
let s2 = Mat::identity().pre_scale(2.0, 3.0);
let s3 = Mat::identity().post_scale(2.0, 3.0);
assert_eq!(s1, s2);
assert_eq!(s1, s3);

Expand Down Expand Up @@ -403,8 +415,8 @@ mod test {

#[test]
pub fn test_pre_post() {
let m1 = Transform2D::identity().post_scaled(1.0, 2.0).post_translated(vec2(1.0, 2.0));
let m2 = Transform2D::identity().pre_translated(vec2(1.0, 2.0)).pre_scaled(1.0, 2.0);
let m1 = Transform2D::identity().post_scale(1.0, 2.0).post_translate(vec2(1.0, 2.0));
let m2 = Transform2D::identity().pre_translate(vec2(1.0, 2.0)).pre_scale(1.0, 2.0);
assert!(m1.approx_eq(&m2));

let r = Mat::create_rotation(rad(FRAC_PI_2));
Expand Down Expand Up @@ -432,7 +444,7 @@ mod test {
pub fn test_is_identity() {
let m1 = Transform2D::identity();
assert!(m1.is_identity());
let m2 = m1.post_translated(vec2(0.1, 0.0));
let m2 = m1.post_translate(vec2(0.1, 0.0));
assert!(!m2.is_identity());
}

Expand Down
37 changes: 22 additions & 15 deletions src/transform3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ where T: Copy + Clone +
}

/// Multiplies all of the transform's component by a scalar and returns the result.
#[must_use]
pub fn mul_s(&self, x: T) -> TypedTransform3D<T, Src, Dst> {
TypedTransform3D::row_major(
self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x,
Expand Down Expand Up @@ -456,12 +457,14 @@ where T: Copy + Clone +
}

/// Returns a transform with a translation applied before self's transformation.
pub fn pre_translated(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn pre_translate(&self, v: TypedVector3D<T, Src>) -> TypedTransform3D<T, Src, Dst> {
self.pre_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z))
}

/// Returns a transform with a translation applied after self's transformation.
pub fn post_translated(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn post_translate(&self, v: TypedVector3D<T, Dst>) -> TypedTransform3D<T, Src, Dst> {
self.post_mul(&TypedTransform3D::create_translation(v.x, v.y, v.z))
}

Expand All @@ -477,7 +480,8 @@ where T: Copy + Clone +
}

/// Returns a transform with a scale applied before self's transformation.
pub fn pre_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn pre_scale(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
TypedTransform3D::row_major(
self.m11 * x, self.m12, self.m13, self.m14,
self.m21 , self.m22 * y, self.m23, self.m24,
Expand All @@ -487,7 +491,8 @@ where T: Copy + Clone +
}

/// Returns a transform with a scale applied after self's transformation.
pub fn post_scaled(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn post_scale(&self, x: T, y: T, z: T) -> TypedTransform3D<T, Src, Dst> {
self.post_mul(&TypedTransform3D::create_scale(x, y, z))
}

Expand Down Expand Up @@ -529,12 +534,14 @@ where T: Copy + Clone +
}

/// Returns a transform with a rotation applied after self's transformation.
pub fn post_rotated(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn post_rotate(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
self.post_mul(&TypedTransform3D::create_rotation(x, y, z, theta))
}

/// Returns a transform with a rotation applied before self's transformation.
pub fn pre_rotated(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
#[must_use]
pub fn pre_rotate(&self, x: T, y: T, z: T, theta: Radians<T>) -> TypedTransform3D<T, Src, Dst> {
self.pre_mul(&TypedTransform3D::create_rotation(x, y, z, theta))
}

Expand Down Expand Up @@ -644,8 +651,8 @@ mod tests {
#[test]
pub fn test_translation() {
let t1 = Mf32::create_translation(1.0, 2.0, 3.0);
let t2 = Mf32::identity().pre_translated(vec3(1.0, 2.0, 3.0));
let t3 = Mf32::identity().post_translated(vec3(1.0, 2.0, 3.0));
let t2 = Mf32::identity().pre_translate(vec3(1.0, 2.0, 3.0));
let t3 = Mf32::identity().post_translate(vec3(1.0, 2.0, 3.0));
assert_eq!(t1, t2);
assert_eq!(t1, t3);

Expand All @@ -661,8 +668,8 @@ mod tests {
#[test]
pub fn test_rotation() {
let r1 = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r2 = Mf32::identity().pre_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r3 = Mf32::identity().post_rotated(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r2 = Mf32::identity().pre_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2));
let r3 = Mf32::identity().post_rotate(0.0, 0.0, 1.0, rad(FRAC_PI_2));
assert_eq!(r1, r2);
assert_eq!(r1, r3);

Expand All @@ -678,8 +685,8 @@ mod tests {
#[test]
pub fn test_scale() {
let s1 = Mf32::create_scale(2.0, 3.0, 4.0);
let s2 = Mf32::identity().pre_scaled(2.0, 3.0, 4.0);
let s3 = Mf32::identity().post_scaled(2.0, 3.0, 4.0);
let s2 = Mf32::identity().pre_scale(2.0, 3.0, 4.0);
let s3 = Mf32::identity().post_scale(2.0, 3.0, 4.0);
assert_eq!(s1, s2);
assert_eq!(s1, s3);

Expand Down Expand Up @@ -794,8 +801,8 @@ mod tests {

#[test]
pub fn test_pre_post() {
let m1 = Transform3D::identity().post_scaled(1.0, 2.0, 3.0).post_translated(vec3(1.0, 2.0, 3.0));
let m2 = Transform3D::identity().pre_translated(vec3(1.0, 2.0, 3.0)).pre_scaled(1.0, 2.0, 3.0);
let m1 = Transform3D::identity().post_scale(1.0, 2.0, 3.0).post_translate(vec3(1.0, 2.0, 3.0));
let m2 = Transform3D::identity().pre_translate(vec3(1.0, 2.0, 3.0)).pre_scale(1.0, 2.0, 3.0);
assert!(m1.approx_eq(&m2));

let r = Mf32::create_rotation(0.0, 0.0, 1.0, rad(FRAC_PI_2));
Expand Down Expand Up @@ -840,7 +847,7 @@ mod tests {
pub fn test_is_identity() {
let m1 = Transform3D::identity();
assert!(m1.is_identity());
let m2 = m1.post_translated(vec3(0.1, 0.0, 0.0));
let m2 = m1.post_translate(vec3(0.1, 0.0, 0.0));
assert!(!m2.is_identity());
}

Expand Down
Loading

0 comments on commit 13b3ddc

Please sign in to comment.