pub struct Quad {
pub corners: [Point; 4],
}Expand description
Four corners, in order: top-left, top-right, bottom-right, bottom-left.
The order is the contract. It matches the unit square’s (0,0), (1,0),
(1,1), (0,1), which is how a texture is indexed, so “top-left” means the
corner the picture’s top-left goes to — whatever that does to the geometry
on screen.
Fields§
§corners: [Point; 4]The corners, clockwise from the picture’s top-left.
Implementations§
Source§impl Quad
impl Quad
Sourcepub const fn new(corners: [Point; 4]) -> Self
pub const fn new(corners: [Point; 4]) -> Self
A quad from four corners, in the documented order.
Sourcepub fn from_rect(rect: Rect) -> Self
pub fn from_rect(rect: Rect) -> Self
The rectangle as a quad — the case with no keystone at all.
Sourcepub fn keystone(rect: Rect, inset: f64) -> Self
pub fn keystone(rect: Rect, inset: f64) -> Self
A symmetric keystone: the top edge narrowed by inset at each end.
The shape a projector makes when it is aimed upwards, and the one most people want when they reach for “trapezoid”.
Sourcepub fn signed_area(self) -> f64
pub fn signed_area(self) -> f64
Twice the signed area, by the shoelace formula. Positive is clockwise in a y-down coordinate system.
Sourcepub fn is_convex(self) -> bool
pub fn is_convex(self) -> bool
Whether the quad is convex and non-degenerate.
A concave or self-intersecting quad has no single projective map onto
it, so this is not a style preference — it is the precondition for
homography meaning anything.
Sourcepub fn homography(self) -> Result<Mat3>
pub fn homography(self) -> Result<Mat3>
The projective map from the unit square onto this quad.
(0,0) lands on the first corner, (1,0) on the second, and so on, so
feeding it texture coordinates maps the picture into the quad. The
bottom row is the perspective part: it is zero exactly when the quad is
a parallelogram, which is the only case where linear interpolation would
have been right.
This is Heckbert’s unit-square-to-quadrilateral construction; it is closed-form, so there is no solve and no iteration.
§Errors
Error::Placement if the quad is concave, self-intersecting, or has
three corners in a line.
Sourcepub fn inverse_homography(self) -> Result<Mat3>
pub fn inverse_homography(self) -> Result<Mat3>
The map the other way: from this quad back to the unit square.
This is what a fragment shader uses. It starts at a screen pixel and has to find the texel, which is the inverse direction from the one that is natural to write down.
§Errors
As homography.