use crate::{
ColumnEq, ColumnKind, ColumnName, ColumnShape, CompositeFilterMarker, CompositeMarker,
CursorValue, DefaultMarker, DefaultVecMarker, FieldName, PayloadFilter, QuantifiedMarker,
};
use super::{NormCoord, Point2, Point2Filter};
impl<T> crate::DeclaredLeaves for Quad<T> {}
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Quad<T> {
top_left: Point2<T>,
top_right: Point2<T>,
bottom_right: Point2<T>,
bottom_left: Point2<T>,
}
impl<T> Quad<T> {
#[inline]
#[must_use]
pub const fn new(
top_left: Point2<T>,
top_right: Point2<T>,
bottom_right: Point2<T>,
bottom_left: Point2<T>,
) -> Self {
Self {
top_left,
top_right,
bottom_right,
bottom_left,
}
}
#[inline]
pub const fn top_left(&self) -> Point2<T>
where
T: Copy,
{
self.top_left
}
#[inline]
pub const fn top_right(&self) -> Point2<T>
where
T: Copy,
{
self.top_right
}
#[inline]
pub const fn bottom_right(&self) -> Point2<T>
where
T: Copy,
{
self.bottom_right
}
#[inline]
pub const fn bottom_left(&self) -> Point2<T>
where
T: Copy,
{
self.bottom_left
}
#[inline]
pub const fn corners(&self) -> [Point2<T>; 4]
where
T: Copy,
{
[
self.top_left,
self.top_right,
self.bottom_right,
self.bottom_left,
]
}
#[must_use]
pub fn bounds(&self) -> super::BoundingBox<T>
where
T: Copy + PartialOrd + core::ops::Sub<Output = T>,
{
let corners = self.corners();
let (mut min_x, mut min_y) = (corners[0].x(), corners[0].y());
let (mut max_x, mut max_y) = (min_x, min_y);
for corner in &corners[1..] {
let (x, y) = (corner.x(), corner.y());
if x < min_x {
min_x = x;
}
if max_x < x {
max_x = x;
}
if y < min_y {
min_y = y;
}
if max_y < y {
max_y = y;
}
}
super::BoundingBox::new(min_x, min_y, max_x - min_x, max_y - min_y)
}
}
impl Quad<NormCoord> {
pub fn try_unit(
top_left: (f32, f32),
top_right: (f32, f32),
bottom_right: (f32, f32),
bottom_left: (f32, f32),
) -> Result<Self, super::OutsideTheUnitInterval> {
let corner = |(x, y): (f32, f32)| Point2::try_unit(x, y);
Ok(Self::new(
corner(top_left)?,
corner(top_right)?,
corner(bottom_right)?,
corner(bottom_left)?,
))
}
}
pub(super) const TOP_LEFT: &FieldName = FieldName::new("top_left");
pub(super) const TOP_RIGHT: &FieldName = FieldName::new("top_right");
pub(super) const BOTTOM_RIGHT: &FieldName = FieldName::new("bottom_right");
pub(super) const BOTTOM_LEFT: &FieldName = FieldName::new("bottom_left");
impl<T: ColumnKind> ColumnKind for Quad<T> {
const SEGMENTS: &'static [ColumnShape] = {
assert!(
T::SEGMENTS.is_empty(),
"a quad's component spans ONE column: a composite component would make the quad's own width \
depend on it, and a fixed-arity row cannot state that"
);
&[
ColumnShape::Composite {
field: Some(ColumnName::of(TOP_LEFT)),
segments: <Point2<T> as ColumnKind>::SEGMENTS,
},
ColumnShape::Composite {
field: Some(ColumnName::of(TOP_RIGHT)),
segments: <Point2<T> as ColumnKind>::SEGMENTS,
},
ColumnShape::Composite {
field: Some(ColumnName::of(BOTTOM_RIGHT)),
segments: <Point2<T> as ColumnKind>::SEGMENTS,
},
ColumnShape::Composite {
field: Some(ColumnName::of(BOTTOM_LEFT)),
segments: <Point2<T> as ColumnKind>::SEGMENTS,
},
]
};
}
impl<T: ColumnEq> ColumnEq for Quad<T> {
#[inline]
fn column_eq(&self, other: &Self) -> bool {
self.top_left.column_eq(&other.top_left)
&& self.top_right.column_eq(&other.top_right)
&& self.bottom_right.column_eq(&other.bottom_right)
&& self.bottom_left.column_eq(&other.bottom_left)
}
}
impl<Db, T> crate::StoredAs<Db> for Quad<T>
where
T: crate::StoredAs<Db>,
{
const WORDS: &'static [&'static str] = &[
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
<T as crate::StoredAs<Db>>::WORDS[0],
];
}
impl<T: crate::CollectionElement> crate::CollectionElement for Quad<T> {}
#[cfg(feature = "sqlx")]
impl<T, Db> crate::Carrier<Db> for Quad<T>
where
Db: sqlx::Database,
T: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
<Point2<T> as crate::Carrier<Db>>::push_bind(&self.top_left, qb);
qb.push(", ");
<Point2<T> as crate::Carrier<Db>>::push_bind(&self.top_right, qb);
qb.push(", ");
<Point2<T> as crate::Carrier<Db>>::push_bind(&self.bottom_right, qb);
qb.push(", ");
<Point2<T> as crate::Carrier<Db>>::push_bind(&self.bottom_left, qb);
}
}
#[cfg(feature = "sqlx")]
impl<'a, R, T> crate::Assemble<'a, R> for Quad<T>
where
R: sqlx::Row,
T: crate::Assemble<'a, R>,
{
fn assemble(row: &'a R, under: Option<&crate::Path<'_>>) -> sqlx::Result<Self> {
let top_left: Point2<T> = crate::assembled(row, TOP_LEFT, under)?;
let top_right: Point2<T> = crate::assembled(row, TOP_RIGHT, under)?;
let bottom_right: Point2<T> = crate::assembled(row, BOTTOM_RIGHT, under)?;
let bottom_left: Point2<T> = crate::assembled(row, BOTTOM_LEFT, under)?;
Ok(Self::new(top_left, top_right, bottom_right, bottom_left))
}
}
impl<T: CursorValue> CursorValue for Quad<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
for corner in [
&self.top_left,
&self.top_right,
&self.bottom_right,
&self.bottom_left,
] {
CursorValue::write_cursor(corner, out);
}
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let corner = bytes.len() / 4;
if corner == 0 || corner * 4 != bytes.len() {
return None;
}
let read =
|nth: usize| <Point2<T> as CursorValue>::read_cursor(&bytes[nth * corner..][..corner]);
Some(Self::new(read(0)?, read(1)?, read(2)?, read(3)?))
}
}
impl<T: DefaultMarker> DefaultMarker for Quad<T> {
type Marker = CompositeMarker<Quad<T>>;
}
impl<T: DefaultMarker> DefaultVecMarker for Quad<T> {
type Marker = QuantifiedMarker<Vec<Quad<T>>>;
}
impl<T> CompositeFilterMarker for Quad<T>
where
T: DefaultMarker,
{
type Filter = QuadFilter<PayloadFilter<T>>;
type ColumnFilter = QuadFilter<PayloadFilter<T>>;
}
pub struct QuadFilter<CF> {
pub(super) top_left: Option<Point2Filter<CF>>,
pub(super) top_right: Option<Point2Filter<CF>>,
pub(super) bottom_right: Option<Point2Filter<CF>>,
pub(super) bottom_left: Option<Point2Filter<CF>>,
}
impl<CF> QuadFilter<CF> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
top_left: None,
top_right: None,
bottom_right: None,
bottom_left: None,
}
}
crate::graphql::filter::opt_accessor!(
top_left: Point2Filter<CF> => with_top_left, set_top_left, maybe_top_left, update_top_left
);
crate::graphql::filter::opt_accessor!(
top_right: Point2Filter<CF> => with_top_right, set_top_right, maybe_top_right, update_top_right
);
crate::graphql::filter::opt_accessor!(
bottom_right: Point2Filter<CF> =>
with_bottom_right, set_bottom_right, maybe_bottom_right, update_bottom_right
);
crate::graphql::filter::opt_accessor!(
bottom_left: Point2Filter<CF> =>
with_bottom_left, set_bottom_left, maybe_bottom_left, update_bottom_left
);
}
impl<CF: crate::GraphqlFilter> QuadFilter<CF> {
pub fn is_empty(&self) -> bool {
[
&self.top_left,
&self.top_right,
&self.bottom_right,
&self.bottom_left,
]
.into_iter()
.all(|corner| corner.as_ref().is_none_or(crate::GraphqlFilter::is_empty))
}
}
impl<CF: crate::GraphqlFilter> crate::GraphqlFilter for QuadFilter<CF> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
impl<CF> Default for QuadFilter<CF> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<CF: core::fmt::Debug> core::fmt::Debug for QuadFilter<CF> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("QuadFilter")
.field("top_left", &self.top_left)
.field("top_right", &self.top_right)
.field("bottom_right", &self.bottom_right)
.field("bottom_left", &self.bottom_left)
.finish()
}
}
impl<CF: Clone> Clone for QuadFilter<CF> {
fn clone(&self) -> Self {
Self {
top_left: self.top_left.clone(),
top_right: self.top_right.clone(),
bottom_right: self.bottom_right.clone(),
bottom_left: self.bottom_left.clone(),
}
}
}
impl<CF: PartialEq> PartialEq for QuadFilter<CF> {
fn eq(&self, other: &Self) -> bool {
self.top_left == other.top_left
&& self.top_right == other.top_right
&& self.bottom_right == other.bottom_right
&& self.bottom_left == other.bottom_left
}
}