Skip to main content

Quad

Struct Quad 

Source
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

Source

pub const fn new(corners: [Point; 4]) -> Self

A quad from four corners, in the documented order.

Source

pub fn from_rect(rect: Rect) -> Self

The rectangle as a quad — the case with no keystone at all.

Source

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”.

Source

pub fn signed_area(self) -> f64

Twice the signed area, by the shoelace formula. Positive is clockwise in a y-down coordinate system.

Source

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.

Source

pub fn bounds(self) -> Rect

The axis-aligned box around the quad.

Source

pub fn contains(self, point: Point) -> bool

Whether a point is inside the quad.

Source

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.

Source

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.

Source

pub fn is_affine(self) -> bool

Whether this quad needs the perspective divide at all.

A parallelogram does not: its map is affine, linear interpolation is exact, and a renderer may take a cheaper path.

Trait Implementations§

Source§

impl Clone for Quad

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Quad

Source§

impl Debug for Quad

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Quad

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Quad

Auto Trait Implementations§

§

impl Freeze for Quad

§

impl RefUnwindSafe for Quad

§

impl Send for Quad

§

impl Sync for Quad

§

impl Unpin for Quad

§

impl UnsafeUnpin for Quad

§

impl UnwindSafe for Quad

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.