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
use std::hash::{Hash, Hasher};
use fj_interop::mesh::Color;
use fj_math::Triangle;
use crate::{
geometry::Surface,
shape::{Handle, Shape},
};
use super::{builder::FaceBuilder, edges::Cycle};
/// A face of a shape
///
/// # Equality
///
/// Please refer to [`crate::kernel::topology`] for documentation on the
/// equality of topological objects.
///
/// # Validation
///
/// A face that is part of a [`Shape`] must be structurally sound. That means
/// the surface and any cycles it refers to, must be part of the same shape.
#[derive(Clone, Debug, Eq, Ord, PartialOrd)]
pub enum Face {
/// A face of a shape
///
/// A face is defined by a surface, and is bounded by edges that lie in that
/// surface.
Face {
/// The surface that defines this face
surface: Handle<Surface>,
/// The cycles that bound the face on the outside
///
/// # Implementation Note
///
/// Since these cycles bound the face, the edges they consist of must
/// lie in the surface. The data we're using here is 3-dimensional
/// though, so no such limitation is enforced.
///
/// It might be less error-prone to specify the edges in surface
/// coordinates.
exteriors: Vec<Handle<Cycle>>,
/// The cycles that bound the face on the inside
///
/// Each of these cycles defines a hole in the face.
///
/// # Implementation note
///
/// See note on `exterior` field.
interiors: Vec<Handle<Cycle>>,
/// The color of the face
color: [u8; 4],
},
/// The triangles of the face
///
/// Representing faces as a collection of triangles is a temporary state.
/// The plan is to eventually represent faces as a geometric surface,
/// bounded by edges. While the transition is being made, this variant is
/// still required.
Triangles(Vec<(Triangle<3>, Color)>),
}
impl Face {
/// Build a face using the [`FaceBuilder`] API
pub fn builder(surface: Surface, shape: &mut Shape) -> FaceBuilder {
FaceBuilder::new(surface, shape)
}
/// Access the surface that the face refers to
///
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`].
pub fn surface(&self) -> Surface {
match self {
Self::Face { surface, .. } => surface.get(),
_ => {
// No code that still uses triangle representation is calling
// this method.
unreachable!()
}
}
}
/// Access the exterior cycles that the face refers to
///
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`]s.
pub fn exteriors(&self) -> impl Iterator<Item = Cycle> + '_ {
match self {
Self::Face { exteriors, .. } => {
exteriors.iter().map(|handle| handle.get())
}
_ => {
// No code that still uses triangle representation is calling
// this method.
unreachable!()
}
}
}
/// Access the interior cycles that the face refers to
///
/// This is a convenience method that saves the caller from dealing with the
/// [`Handle`]s.
pub fn interiors(&self) -> impl Iterator<Item = Cycle> + '_ {
match self {
Self::Face { interiors, .. } => {
interiors.iter().map(|handle| handle.get())
}
_ => {
// No code that still uses triangle representation is calling
// this method.
unreachable!()
}
}
}
/// Access all cycles that the face refers to
///
/// This is equivalent to chaining the iterators returned by
/// [`Face::exteriors`] and [`Face::interiors`].
pub fn all_cycles(&self) -> impl Iterator<Item = Cycle> + '_ {
self.exteriors().chain(self.interiors())
}
}
impl PartialEq for Face {
fn eq(&self, other: &Self) -> bool {
self.surface() == other.surface()
&& self.exteriors().eq(other.exteriors())
&& self.interiors().eq(other.interiors())
}
}
impl Hash for Face {
fn hash<H: Hasher>(&self, state: &mut H) {
self.surface().hash(state);
for cycle in self.all_cycles() {
cycle.hash(state);
}
}
}