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 365 366 367
//! Intersection between a ray and a face, in 3D
use fj_math::{Plane, Point, Scalar};
use crate::{
algorithms::intersect::face_point::FacePointIntersection,
geometry::curve::GlobalPath,
objects::{Face, HalfEdge},
storage::Handle,
};
use super::{HorizontalRayToTheRight, Intersect};
impl Intersect for (&HorizontalRayToTheRight<3>, &Face) {
type Intersection = RayFaceIntersection;
fn intersect(self) -> Option<Self::Intersection> {
let (ray, face) = self;
let plane = match face.surface().geometry().u {
GlobalPath::Circle(_) => todo!(
"Casting a ray against a swept circle is not supported yet"
),
GlobalPath::Line(line) => Plane::from_parametric(
line.origin(),
line.direction(),
face.surface().geometry().v,
),
};
if plane.is_parallel_to_vector(&ray.direction()) {
let a = plane.origin();
let b = plane.origin() + plane.u();
let c = plane.origin() + plane.v();
let d = ray.origin;
let [a, b, c, d] = [a, b, c, d]
.map(|point| [point.x, point.y, point.z])
.map(|point| point.map(Scalar::into_f64))
.map(|[x, y, z]| robust::Coord3D { x, y, z });
if robust::orient3d(a, b, c, d) == 0. {
return Some(RayFaceIntersection::RayHitsFaceAndAreParallel);
} else {
return None;
}
}
// The pattern in this assertion resembles `ax*by = ay*bx`, which holds
// true if the vectors `a = (ax, ay)` and `b = (bx, by)` are parallel.
//
// We're looking at the plane's direction vectors here, but we're
// ignoring their x-components. By doing that, we're essentially
// projecting those vectors into the yz-plane.
//
// This means that the following assertion verifies that the projections
// of the plane's direction vectors into the yz-plane are not parallel.
// If they were, then the plane could only be parallel to the x-axis,
// and thus our ray.
//
// We already handled the case of the ray and plane being parallel
// above. The following assertion should thus never be triggered.
assert_ne!(
plane.u().y * plane.v().z,
plane.u().z * plane.v().y,
"Plane and ray are parallel; should have been ruled out previously"
);
// Let's figure out the intersection between the ray and the plane.
let (t, u, v) = {
// The following math would get *very* unwieldy with those
// full-length variable names. Let's define some short-hands.
let orx = ray.origin.x;
let ory = ray.origin.y;
let orz = ray.origin.z;
let opx = plane.origin().x;
let opy = plane.origin().y;
let opz = plane.origin().z;
let d1x = plane.u().x;
let d1y = plane.u().y;
let d1z = plane.u().z;
let d2x = plane.v().x;
let d2y = plane.v().y;
let d2z = plane.v().z;
// Let's figure out where the intersection between the ray and the
// plane is. By equating the parametric equations of the ray and the
// plane, we get a vector equation, which in turn gives us a system
// of three equations with three unknowns: `t` (for the ray) and
// `u`/`v` (for the plane).
//
// Since the ray's direction vector is `(1, 0, 0)`, it works out
// such that `t` is not in the equations for y and z, meaning we can
// solve those equations for `u` and `v` independently.
//
// By doing some math, we get the following solutions:
let v = (d1y * (orz - opz) + (opy - ory) * d1z)
/ (d1y * d2z - d2y * d1z);
let u = (ory - opy - d2y * v) / d1y;
let t = opx - orx + d1x * u + d2x * v;
(t, u, v)
};
if t < Scalar::ZERO {
// Ray points away from plane.
return None;
}
let point = Point::from([u, v]);
let intersection = match (face, &point).intersect()? {
FacePointIntersection::PointIsInsideFace => {
RayFaceIntersection::RayHitsFace
}
FacePointIntersection::PointIsOnEdge(edge) => {
RayFaceIntersection::RayHitsEdge(edge)
}
FacePointIntersection::PointIsOnVertex(vertex) => {
RayFaceIntersection::RayHitsVertex(vertex)
}
};
Some(intersection)
}
}
/// A hit between a ray and a face
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RayFaceIntersection {
/// The ray hits the face itself
RayHitsFace,
/// The ray is parallel to the face
RayHitsFaceAndAreParallel,
/// The ray hits an edge
RayHitsEdge(Handle<HalfEdge>),
/// The ray hits a vertex
RayHitsVertex(Point<2>),
}
#[cfg(test)]
mod tests {
use fj_math::Point;
use crate::{
algorithms::{
intersect::{
ray_face::RayFaceIntersection, HorizontalRayToTheRight,
Intersect,
},
transform::TransformObject,
},
objects::{Cycle, Face},
operations::{BuildCycle, BuildFace, Insert, UpdateFace, UpdateRegion},
services::Services,
};
#[test]
fn ray_misses_whole_surface() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.yz_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([-1., 0., 0.], &mut services);
assert_eq!((&ray, &face).intersect(), None);
services.only_validate(face);
}
#[test]
fn ray_hits_face() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.yz_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([1., 0., 0.], &mut services);
assert_eq!(
(&ray, &face).intersect(),
Some(RayFaceIntersection::RayHitsFace)
);
services.only_validate(face);
}
#[test]
fn ray_hits_surface_but_misses_face() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.yz_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([0., 0., 2.], &mut services);
assert_eq!((&ray, &face).intersect(), None);
services.only_validate(face);
}
#[test]
fn ray_hits_edge() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.yz_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([1., 1., 0.], &mut services);
let edge = face
.region()
.exterior()
.half_edges()
.find(|edge| edge.start_position() == Point::from([-1., 1.]))
.unwrap();
assert_eq!(
(&ray, &face).intersect(),
Some(RayFaceIntersection::RayHitsEdge(edge.clone()))
);
services.only_validate(face);
}
#[test]
fn ray_hits_vertex() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.yz_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([1., 1., 1.], &mut services);
let vertex = face
.region()
.exterior()
.half_edges()
.find(|half_edge| {
half_edge.start_position() == Point::from([-1., -1.])
})
.map(|half_edge| half_edge.start_position())
.unwrap();
assert_eq!(
(&ray, &face).intersect(),
Some(RayFaceIntersection::RayHitsVertex(vertex))
);
services.only_validate(face);
}
#[test]
fn ray_is_parallel_to_surface_and_hits() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.xy_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
assert_eq!(
(&ray, &face).intersect(),
Some(RayFaceIntersection::RayHitsFaceAndAreParallel)
);
services.only_validate(face);
}
#[test]
fn ray_is_parallel_to_surface_and_misses() {
let mut services = Services::new();
let ray = HorizontalRayToTheRight::from([0., 0., 0.]);
let face =
Face::unbound(services.objects.surfaces.xy_plane(), &mut services)
.update_region(|region| {
region
.update_exterior(|_| {
Cycle::polygon(
[[-1., -1.], [1., -1.], [1., 1.], [-1., 1.]],
&mut services,
)
.insert(&mut services)
})
.insert(&mut services)
});
let face = face.translate([0., 0., 1.], &mut services);
assert_eq!((&ray, &face).intersect(), None);
services.only_validate(face);
}
}