-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add a few transform convenience methods #2055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,6 +112,22 @@ impl GlobalTransform { | |
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) | ||
} | ||
|
||
/// Returns the inverse of this transform. | ||
/// | ||
/// The inverse consists of applying the inverse rotation, scale, and | ||
/// translation in reverse order. | ||
#[inline] | ||
pub fn inverse(&self) -> Transform { | ||
let rotation = self.rotation.inverse(); | ||
let scale = rotation * -self.scale; | ||
let translation = scale * -self.translation; | ||
Transform { | ||
rotation, | ||
scale, | ||
translation, | ||
} | ||
} | ||
|
||
/// Get the unit vector in the local x direction | ||
#[inline] | ||
pub fn local_x(&self) -> Vec3 { | ||
|
@@ -195,6 +211,24 @@ impl GlobalTransform { | |
value | ||
} | ||
|
||
/// Returns the result of applying this [`GlobalTransform`] to a [`Vec3`] interpreted as a point. | ||
/// | ||
/// This applies rotation, scale, and translation. It's the equivalent of using w=1 in | ||
/// homogeneous coordinates. | ||
#[inline] | ||
pub fn transform_point(&self, point: Vec3) -> Vec3 { | ||
self.scale * (self.rotation * point) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does translation get applied here? |
||
} | ||
|
||
/// Returns the result of applying this [`GlobalTransform`] to a [`Vec3`] interpreted as a vector. | ||
/// | ||
/// This applies rotation and scale, but not translation. It's the equivalent of using w=0 in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we swap the comments / math here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah... I sure did. lol. I'll fix that |
||
/// homogeneous coordinates. | ||
#[inline] | ||
pub fn transform_vector(&self, vector: Vec3) -> Vec3 { | ||
self.scale * (self.rotation * point) + self.translation | ||
} | ||
|
||
#[doc(hidden)] | ||
#[inline] | ||
pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,6 +124,22 @@ impl Transform { | |
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) | ||
} | ||
|
||
/// Returns the inverse of this transform. | ||
/// | ||
/// The inverse consists of applying the inverse rotation, scale, and | ||
/// translation in reverse order. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think spelling out A, then B, then C is clearer than "reverse order". |
||
#[inline] | ||
pub fn inverse(&self) -> Transform { | ||
let rotation = self.rotation.inverse(); | ||
let scale = rotation * -self.scale; | ||
let translation = scale * -self.translation; | ||
Transform { | ||
rotation, | ||
scale, | ||
translation, | ||
} | ||
} | ||
|
||
/// Get the unit vector in the local x direction. | ||
#[inline] | ||
pub fn local_x(&self) -> Vec3 { | ||
|
@@ -207,6 +223,24 @@ impl Transform { | |
value | ||
} | ||
|
||
/// Returns the result of applying this [`Transform`] to a [`Vec3`] interpreted as a point. | ||
/// | ||
/// This applies rotation, scale, and translation. It's the equivalent of using w=1 in | ||
/// homogeneous coordinates. | ||
#[inline] | ||
pub fn transform_point(&self, point: Vec3) -> Vec3 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does having two functions of the same name but different function arguments work in Rust? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rust doesn't have function overloading, so it should not work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of the functions is on |
||
self.scale * (self.rotation * point) | ||
} | ||
|
||
/// Returns the result of applying this [`Transform`] to a [`Vec3`] interpreted as a vector. | ||
/// | ||
/// This applies rotation and scale, but not translation. It's the equivalent of using w=0 in | ||
/// homogeneous coordinates. | ||
#[inline] | ||
pub fn transform_vector(&self, vector: Vec3) -> Vec3 { | ||
self.scale * (self.rotation * point) + self.translation | ||
} | ||
|
||
/// Changes the `scale` of this [`Transform`], multiplying the current `scale` by | ||
/// `scale_factor`. | ||
#[inline] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you link an appropriate explanatory article on homogenous coordinates as part of this? It's pretty dense for beginners.
https://www.tomdalling.com/blog/modern-opengl/explaining-homogenous-coordinates-and-projective-geometry/ looks quite nice.