1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
use crate::geom::{tri, vertex, Cuboid, Range, Rect, Tri, Vertex, Vertex2d, Vertex3d};
use core::ops::{Deref, Index};
/// The number of vertices in a quad.
pub const NUM_VERTICES: u8 = 4;
/// The number of triangles that make up a quad.
pub const NUM_TRIANGLES: u8 = 2;
/// The same as `triangles`, but instead returns the vertex indices for each triangle.
pub const TRIANGLE_INDEX_TRIS: TrianglesIndexTris = [[0, 1, 2], [0, 2, 3]];
pub type TrianglesIndexTris = [[usize; tri::NUM_VERTICES as usize]; NUM_TRIANGLES as usize];
/// The number of indices used to describe each triangle in the quad.
pub const NUM_TRIANGLE_INDICES: u8 = 6;
/// The same as `triangles`, but instead returns the vertex indices for each triangle.
pub const TRIANGLE_INDICES: [usize; NUM_TRIANGLE_INDICES as usize] = [0, 1, 2, 0, 2, 3];
/// A quad represented by its four vertices.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Quad<V = vertex::Default>(pub [V; NUM_VERTICES as usize]);
/// An `Iterator` yielding the two triangles that make up a quad.
#[derive(Clone, Debug)]
pub struct Triangles<V = vertex::Default> {
a: Option<Tri<V>>,
b: Option<Tri<V>>,
}
/// A simple iterator yielding each vertex in a `Quad`.
#[derive(Clone, Debug)]
pub struct Vertices<V = vertex::Default> {
quad: Quad<V>,
index: u8,
}
impl<V> Quad<V>
where
V: Vertex,
{
/// Produce an iterator yielding each vertex in the `Quad`.
pub fn vertices(self) -> Vertices<V> {
vertices(self)
}
/// Produce the centroid of the quad, aka the "mean"/"average" vertex.
pub fn centroid(&self) -> V
where
V: vertex::Average,
{
centroid(self)
}
/// Triangulates the given quad, represented by four points that describe its edges in either
/// clockwise or anti-clockwise order.
///
/// # Example
///
/// The following rectangle
///
/// ```ignore
/// a b
/// --------
/// | |
/// | |
/// | |
/// --------
/// d c
///
/// ```
///
/// given as
///
/// ```ignore
/// triangles([a, b, c, d])
/// ```
///
/// returns
///
/// ```ignore
/// (Tri([a, b, c]), Tri([a, c, d]))
/// ```
///
/// Here's a basic code example:
///
/// ```
/// # use nannou_core as nannou;
/// use nannou::geom::{self, pt2, Quad, Tri};
///
/// fn main() {
/// let a = pt2(0.0, 1.0);
/// let b = pt2(1.0, 1.0);
/// let c = pt2(1.0, 0.0);
/// let d = pt2(0.0, 0.0);
/// let quad = Quad([a, b, c, d]);
/// let triangles = geom::quad::triangles(&quad);
/// assert_eq!(triangles, (Tri([a, b, c]), Tri([a, c, d])));
/// }
/// ```
#[inline]
pub fn triangles(&self) -> (Tri<V>, Tri<V>) {
triangles(self)
}
/// The same as `triangles` but provided as an **Iterator**.
pub fn triangles_iter(&self) -> Triangles<V> {
triangles_iter(self)
}
/// The bounding `Rect` of the quad.
pub fn bounding_rect(self) -> Rect<V::Scalar>
where
V: Vertex2d,
{
let (a, b, c, d) = self.into();
let ([ax, ay], b, c, d) = (a.point2(), b.point2(), c.point2(), d.point2());
let rect = Rect {
x: Range::new(ax, ax),
y: Range::new(ay, ay),
};
rect.stretch_to_point(b)
.stretch_to_point(c)
.stretch_to_point(d)
}
/// The bounding `Rect` of the triangle.
pub fn bounding_cuboid(self) -> Cuboid<V::Scalar>
where
V: Vertex3d,
{
let (a, b, c, d) = self.into();
let ([ax, ay, az], b, c, d) = (a.point3(), b.point3(), c.point3(), d.point3());
let cuboid = Cuboid {
x: Range::new(ax, ax),
y: Range::new(ay, ay),
z: Range::new(az, az),
};
cuboid
.stretch_to_point(b)
.stretch_to_point(c)
.stretch_to_point(d)
}
/// Map the **Quad**'s vertices to a new type.
pub fn map_vertices<F, V2>(self, mut map: F) -> Quad<V2>
where
F: FnMut(V) -> V2,
{
let (a, b, c, d) = self.into();
Quad([map(a), map(b), map(c), map(d)])
}
}
/// Produce an iterator yielding each vertex in the given **Quad**.
pub fn vertices<V>(quad: Quad<V>) -> Vertices<V> {
let index = 0;
Vertices { quad, index }
}
/// Produce the centroid of the quad, aka the "mean"/"average" vertex.
pub fn centroid<V>(quad: &Quad<V>) -> V
where
V: vertex::Average,
{
crate::geom::centroid(quad.iter().cloned()).unwrap()
}
/// Triangulates the given quad, represented by four points that describe its edges in either
/// clockwise or anti-clockwise order.
///
/// # Example
///
/// The following rectangle
///
/// ```ignore
///
/// a b
/// --------
/// | |
/// | |
/// | |
/// --------
/// d c
///
/// ```
///
/// given as
///
/// ```ignore
/// triangles([a, b, c, d])
/// ```
///
/// returns
///
/// ```ignore
/// (Tri([a, b, c]), Tri([a, c, d]))
/// ```
///
/// Here's a basic code example:
///
/// ```
/// # use nannou_core as nannou;
/// use nannou::geom::{self, pt2, Quad, Tri};
///
/// fn main() {
/// let a = pt2(0.0, 1.0);
/// let b = pt2(1.0, 1.0);
/// let c = pt2(1.0, 0.0);
/// let d = pt2(0.0, 0.0);
/// let quad = Quad([a, b, c, d]);
/// let triangles = geom::quad::triangles(&quad);
/// assert_eq!(triangles, (Tri([a, b, c]), Tri([a, c, d])));
/// }
/// ```
#[inline]
pub fn triangles<V>(q: &Quad<V>) -> (Tri<V>, Tri<V>)
where
V: Vertex,
{
let a = Tri::from_index_tri(&q.0, &TRIANGLE_INDEX_TRIS[0]);
let b = Tri::from_index_tri(&q.0, &TRIANGLE_INDEX_TRIS[1]);
(a, b)
}
/// The same as `triangles` but provided as an `Iterator`.
pub fn triangles_iter<V>(points: &Quad<V>) -> Triangles<V>
where
V: Vertex,
{
let (a, b) = triangles(points);
Triangles {
a: Some(a),
b: Some(b),
}
}
impl<V> Iterator for Triangles<V> {
type Item = Tri<V>;
fn next(&mut self) -> Option<Self::Item> {
self.a.take().or_else(|| self.b.take())
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<V> DoubleEndedIterator for Triangles<V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.b.take().or_else(|| self.a.take())
}
}
impl<V> ExactSizeIterator for Triangles<V> {
fn len(&self) -> usize {
match (&self.a, &self.b) {
(&Some(_), &Some(_)) => 2,
(&None, &Some(_)) => 0,
_ => 1,
}
}
}
impl<V> Iterator for Vertices<V>
where
V: Clone,
{
type Item = V;
fn next(&mut self) -> Option<Self::Item> {
if self.index < NUM_VERTICES {
let v = self.quad[self.index as usize].clone();
self.index += 1;
Some(v)
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.len();
(len, Some(len))
}
}
impl<V> ExactSizeIterator for Vertices<V>
where
V: Clone,
{
fn len(&self) -> usize {
NUM_VERTICES as usize - self.index as usize
}
}
impl<V> Deref for Quad<V> {
type Target = [V; NUM_VERTICES as usize];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<V> From<[V; NUM_VERTICES as usize]> for Quad<V>
where
V: Vertex,
{
fn from(points: [V; NUM_VERTICES as usize]) -> Self {
Quad(points)
}
}
impl<V> From<(V, V, V, V)> for Quad<V>
where
V: Vertex,
{
fn from((a, b, c, d): (V, V, V, V)) -> Self {
Quad([a, b, c, d])
}
}
impl<V> Into<[V; NUM_VERTICES as usize]> for Quad<V>
where
V: Vertex,
{
fn into(self) -> [V; NUM_VERTICES as usize] {
self.0
}
}
impl<V> Into<(V, V, V, V)> for Quad<V>
where
V: Vertex,
{
fn into(self) -> (V, V, V, V) {
(self[0], self[1], self[2], self[3])
}
}
impl<V> AsRef<Quad<V>> for Quad<V>
where
V: Vertex,
{
fn as_ref(&self) -> &Quad<V> {
self
}
}
impl<V> AsRef<[V; NUM_VERTICES as usize]> for Quad<V>
where
V: Vertex,
{
fn as_ref(&self) -> &[V; NUM_VERTICES as usize] {
&self.0
}
}
impl<V> Index<usize> for Quad<V>
where
V: Vertex,
{
type Output = V;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}