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
use fj_interop::ext::ArrayExt;
use fj_math::{Point, Scalar};
use crate::{
geometry::{
curve::{Curve, GlobalPath},
surface::SurfaceGeometry,
},
objects::HalfEdge,
partial::{MaybeCurve, Partial, PartialHalfEdge},
};
/// Builder API for [`PartialHalfEdge`]
pub trait HalfEdgeBuilder {
/// Update partial half-edge to be a circle, from the given radius
fn update_as_circle_from_radius(
&mut self,
radius: impl Into<Scalar>,
) -> Curve;
/// Update partial half-edge to be an arc, spanning the given angle in
/// radians
///
/// # Panics
///
/// Panics if the given angle is not within the range (-2pi, 2pi) radians.
fn update_as_arc(
&mut self,
start: Point<2>,
end: Point<2>,
angle_rad: impl Into<Scalar>,
);
/// Update partial half-edge to be a line segment
fn update_as_line_segment(
&mut self,
start: Point<2>,
end: Point<2>,
) -> Curve;
/// Update this edge from another
///
/// Infers as much information about this edge from the other, under the
/// assumption that the other edge is on a different surface.
///
/// This method is quite fragile. It might panic, or even silently fail,
/// under various circumstances. As long as you're only dealing with lines
/// and planes, you should be fine. Otherwise, please read the code of this
/// method carefully, to make sure you don't run into trouble.
fn update_from_other_edge(
&mut self,
other: &Partial<HalfEdge>,
other_prev: &Partial<HalfEdge>,
surface: &SurfaceGeometry,
);
}
impl HalfEdgeBuilder for PartialHalfEdge {
fn update_as_circle_from_radius(
&mut self,
radius: impl Into<Scalar>,
) -> Curve {
let path = Curve::circle_from_radius(radius);
self.curve = Some(path.into());
let [a_curve, b_curve] =
[Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));
for (point_boundary, point_curve) in
self.boundary.each_mut_ext().zip_ext([a_curve, b_curve])
{
*point_boundary = Some(point_curve);
}
path
}
fn update_as_arc(
&mut self,
start: Point<2>,
end: Point<2>,
angle_rad: impl Into<Scalar>,
) {
let angle_rad = angle_rad.into();
if angle_rad <= -Scalar::TAU || angle_rad >= Scalar::TAU {
panic!("arc angle must be in the range (-2pi, 2pi) radians");
}
let arc = fj_math::Arc::from_endpoints_and_angle(start, end, angle_rad);
let path = Curve::circle_from_center_and_radius(arc.center, arc.radius);
self.curve = Some(path.into());
let [a_curve, b_curve] =
[arc.start_angle, arc.end_angle].map(|coord| Point::from([coord]));
for (point_boundary, point_curve) in
self.boundary.each_mut_ext().zip_ext([a_curve, b_curve])
{
*point_boundary = Some(point_curve);
}
}
fn update_as_line_segment(
&mut self,
start: Point<2>,
end: Point<2>,
) -> Curve {
let boundary = self.boundary;
let points_surface = [start, end];
let path = if let [Some(start), Some(end)] = boundary {
let points = [start, end].zip_ext(points_surface);
let path = Curve::from_points_with_line_coords(points);
self.curve = Some(path.into());
path
} else {
let (path, _) = Curve::line_from_points(points_surface);
self.curve = Some(path.into());
for (vertex, position) in
self.boundary.each_mut_ext().zip_ext([0., 1.])
{
*vertex = Some([position].into());
}
path
};
path
}
fn update_from_other_edge(
&mut self,
other: &Partial<HalfEdge>,
other_prev: &Partial<HalfEdge>,
surface: &SurfaceGeometry,
) {
self.curve = other.read().curve.as_ref().and_then(|path| {
// We have information about the other edge's surface available. We
// need to use that to interpret what the other edge's curve path
// means for our curve path.
match surface.u {
GlobalPath::Circle(circle) => {
// The other surface is curved. We're entering some dodgy
// territory here, as only some edge cases can be
// represented using our current curve/surface
// representation.
match path {
MaybeCurve::Defined(Curve::Line(_))
| MaybeCurve::UndefinedLine => {
// We're dealing with a line on a rounded surface.
//
// Based on the current uses of this method, we can
// make some assumptions:
//
// 1. The line is parallel to the u-axis of the
// other surface.
// 2. The surface that *our* edge is in is a plane
// that is parallel to the the plane of the
// circle that defines the curvature of the other
// surface.
//
// These assumptions are necessary preconditions for
// the following code to work. But unfortunately, I
// see no way to check those preconditions here, as
// neither the other line nor our surface is
// necessarily defined yet.
//
// Handling this case anyway feels like a grave sin,
// but I don't know what else to do. If you tracked
// some extremely subtle and annoying bug back to
// this code, I apologize.
//
// I hope that I'll come up with a better curve/
// surface representation before this becomes a
// problem.
Some(MaybeCurve::UndefinedCircle {
radius: circle.radius(),
})
}
_ => {
// The other edge is a line segment in a curved
// surface. No idea how to deal with this.
todo!(
"Can't connect edge to circle on curved \
surface"
)
}
}
}
GlobalPath::Line(_) => {
// The other edge is defined on a plane.
match path {
MaybeCurve::Defined(Curve::Line(_))
| MaybeCurve::UndefinedLine => {
// The other edge is a line segment on a plane. That
// means our edge must be a line segment too.
Some(MaybeCurve::UndefinedLine)
}
_ => {
// The other edge is a circle or arc on a plane. I'm
// actually not sure what that means for our edge.
// We might be able to represent it somehow, but
// let's leave that as an exercise for later.
todo!("Can't connect edge to circle on plane")
}
}
}
}
});
self.start_vertex = other_prev.read().start_vertex.clone();
}
}