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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use crate::common::{PCoords, angle_signed_pi};
use crate::geom3::iso3::IsoExtensions3;
use crate::geom3::manifold::Manifold1Pos3;
use crate::{Iso3, Plane3, Point3, Result, UnitVec3, Vector3};
use parry3d_f64::na::UnitQuaternion;
use std::ops;
/// A flat circle in 3D space, defined by a radius and a world isometry. The circle can be thought
/// of as a circle of radius `r` sitting at the origin of the XY plane, then transformed by the
/// isometry to position and orientation in 3D space.
#[derive(Debug, Clone, PartialEq)]
pub struct Circle3 {
radius: f64,
iso: Iso3,
}
impl Circle3 {
/// Returns the radius of the circle.
pub fn r(&self) -> f64 {
self.radius
}
/// Returns the isometry that defines the circle's position and orientation in world space.
pub fn iso(&self) -> &Iso3 {
&self.iso
}
/// Returns the world-space center of the circle.
pub fn center(&self) -> Point3 {
self.iso * Point3::origin()
}
/// Returns the world-space unit normal of the circle's plane.
pub fn normal(&self) -> UnitVec3 {
UnitVec3::new_normalize(self.iso * Vector3::z())
}
/// Returns the plane that the circle lies in
pub fn plane(&self) -> Plane3 {
self.iso * Plane3::xy()
}
/// Returns a new circle transformed by the given isometry, without modifying the original.
pub fn new_transformed_by(&self, iso: &Iso3) -> Self {
let mut new_circle = self.clone();
new_circle.transform_by(iso);
new_circle
}
/// Transforms this circle in place by the given isometry.
pub fn transform_by(&mut self, iso: &Iso3) {
self.iso = iso * self.iso;
}
/// Flips the normal direction of the circle in place by rotating the isometry 180° around the
/// local x-axis. The zero-angle point is preserved, but the normal and winding direction are
/// reversed.
pub fn flip_normal(&mut self) {
let flip = UnitQuaternion::from_axis_angle(&Vector3::x_axis(), std::f64::consts::PI);
self.iso.rotation *= flip;
}
/// Returns a new circle with the normal direction flipped, without modifying the original.
pub fn new_flipped_normal(&self) -> Self {
let mut flipped = self.clone();
flipped.flip_normal();
flipped
}
/// Create a circle from a radius and an isometry. The circle will lie on the XY plane of the
/// isometry's local frame, centered at the isometry's origin, with the normal along the
/// isometry's z-axis.
pub fn new(radius: f64, iso: Iso3) -> Self {
Self { radius, iso }
}
/// Returns the `Manifold1Pos3` at the given angle (in radians) on the circle. The arc length
/// `l` is measured from angle 0 (the isometry's local x-axis direction).
pub fn at_angle(&self, angle: f64) -> Manifold1Pos3 {
let (sin_a, cos_a) = angle.sin_cos();
let local_point = Point3::new(self.radius * cos_a, self.radius * sin_a, 0.0);
let local_tangent = Vector3::new(-sin_a, cos_a, 0.0);
Manifold1Pos3::new(
self.radius * angle,
self.iso * local_point,
UnitVec3::new_normalize(self.iso.rotation * local_tangent),
)
}
/// Returns the angle (in radians) of the point on the circle closest to `test_point`. The
/// angle is in the range `(-π, π]` and is measured from the isometry's local x-axis.
///
/// # Arguments
///
/// * `test_point`: a point in world space to test
///
/// returns: f64
pub fn closest_angle(&self, test_point: &impl PCoords<3>) -> f64 {
let local = self.iso.inverse() * Point3::from(test_point.coords());
local.y.atan2(local.x)
}
/// Returns the `Manifold1Pos3` at the point on the circle closest to `test_point`. The arc
/// length `l` is measured from angle 0 (the isometry's local x-axis direction).
///
/// # Arguments
///
/// * `test_point`: a point in world space to test
///
/// returns: Manifold1Pos3
pub fn closest_position(&self, test_point: &impl PCoords<3>) -> Manifold1Pos3 {
self.at_angle(self.closest_angle(test_point))
}
/// Intersects the circle with a plane, returning 0, 1, or 2 intersection angles (in radians).
///
/// Returns an empty vec if the plane does not intersect the circle, a single angle if the
/// plane is tangent to the circle, or two angles if the plane cuts through it. Returns empty
/// for the degenerate case where the plane is parallel to (or coincident with) the circle's
/// plane. Angles are in the range `(-π, π]` and can be passed directly to `at_angle`.
///
/// # Arguments
///
/// * `plane`: the plane to intersect with
///
/// returns: Vec<f64>
pub fn intersect_plane(&self, plane: &Plane3) -> Vec<f64> {
let x_axis = self.iso * Vector3::x();
let y_axis = self.iso * Vector3::y();
let a = self.radius * plane.normal.dot(&x_axis);
let b = self.radius * plane.normal.dot(&y_axis);
let c = plane.d - plane.normal.dot(&self.center().coords);
let r_amp = (a * a + b * b).sqrt();
if r_amp < 1e-10 {
// Plane normal is (anti)parallel to circle normal — plane is parallel to the circle.
return vec![];
}
let ratio = c / r_amp;
if !(-1.0 - 1e-10..=1.0 + 1e-10).contains(&ratio) {
return vec![];
}
let phi = b.atan2(a);
let delta = ratio.clamp(-1.0, 1.0).acos();
if delta < 1e-9 || (std::f64::consts::PI - delta) < 1e-9 {
vec![angle_signed_pi(phi + delta)]
} else {
vec![angle_signed_pi(phi + delta), angle_signed_pi(phi - delta)]
}
}
/// Rotates the circle's isometry around its normal so that the point currently at `angle`
/// becomes the new zero angle (aligned with the local x-axis).
pub fn set_zero_angle(&mut self, angle: f64) {
let rot = UnitQuaternion::from_axis_angle(&Vector3::z_axis(), angle);
self.iso.rotation *= rot;
}
/// Returns the angle (in radians) of the point on the circle that maximizes the dot product
/// with the given direction vector. Returns an error if the direction is parallel to the
/// circle's normal (all points on the circle are equidistant in that direction).
///
/// # Arguments
///
/// * `direction`: a vector in world space; the returned angle maximizes `point · direction`
///
/// returns: Result<f64>
pub fn max_extent_angle(&self, direction: &Vector3) -> Result<f64> {
let local = self.iso.inverse().rotation * direction;
let mag = (local.x * local.x + local.y * local.y).sqrt();
if mag < 1e-10 {
return Err("direction is parallel to the circle's normal".into());
}
Ok(local.y.atan2(local.x))
}
/// Create a circle from a center point, a normal direction, and a radius. An arbitrary
/// orientation around the normal is chosen for the isometry's x/y axes.
///
/// # Arguments
///
/// * `center`: the center point of the circle in world space
/// * `normal`: the normal direction of the circle's plane
/// * `radius`: the radius of the circle
///
/// returns: Result<Circle3, Box<dyn Error, Global>>
///
/// # Examples
///
/// ```
/// use approx::assert_relative_eq;
/// use engeom::{Point3, UnitVec3, Vector3};
/// use engeom::geom3::Circle3;
///
/// let center = Point3::new(1.0, 2.0, 3.0);
/// let normal = UnitVec3::new_normalize(Vector3::new(0.0, 0.0, 1.0));
/// let circle = Circle3::from_point_normal(¢er, &normal, 5.0).unwrap();
///
/// assert_relative_eq!(circle.center(), center, epsilon = 1e-12);
/// assert_relative_eq!(circle.normal().into_inner(), normal.into_inner(), epsilon = 1e-12);
/// assert_relative_eq!(circle.r(), 5.0, epsilon = 1e-12);
/// ```
pub fn from_point_normal(center: &Point3, normal: &UnitVec3, radius: f64) -> Result<Self> {
// Choose a reference vector not parallel to the normal, so we can form a basis.
let reference = if normal.z.abs() < 0.9 {
Vector3::z()
} else {
Vector3::x()
};
let iso = Iso3::try_from_basis_zy(&normal.into_inner(), &reference, Some(*center))?;
Ok(Self::new(radius, iso))
}
}
impl ops::Mul<Circle3> for Iso3 {
type Output = Circle3;
fn mul(self, rhs: Circle3) -> Circle3 {
rhs.new_transformed_by(&self)
}
}
impl ops::Mul<&Circle3> for Iso3 {
type Output = Circle3;
fn mul(self, rhs: &Circle3) -> Circle3 {
rhs.new_transformed_by(&self)
}
}
impl ops::Mul<Circle3> for &Iso3 {
type Output = Circle3;
fn mul(self, rhs: Circle3) -> Circle3 {
rhs.new_transformed_by(self)
}
}
impl ops::Mul<&Circle3> for &Iso3 {
type Output = Circle3;
fn mul(self, rhs: &Circle3) -> Circle3 {
rhs.new_transformed_by(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Curve3;
use crate::common::linear_space;
use crate::geom3::tests::RandomGeometry;
use approx::assert_relative_eq;
use rand::RngExt;
use std::f64::consts::PI;
/// Build a circle with a non-trivial orientation
fn tilted_circle() -> Circle3 {
let center = Point3::new(1.0, 2.0, 3.0);
let normal = UnitVec3::new_normalize(Vector3::new(1.0, 1.0, 1.0));
Circle3::from_point_normal(¢er, &normal, 4.0).unwrap()
}
#[test]
fn at_angle_zero_is_on_x_axis() {
let circle = tilted_circle();
let pos = circle.at_angle(0.0);
// arc length at angle 0 is 0
assert_relative_eq!(pos.l, 0.0, epsilon = 1e-12);
// point must lie on the circle
let d = (pos.point - circle.center()).norm();
assert_relative_eq!(d, circle.r(), epsilon = 1e-12);
}
#[test]
fn at_angle_arc_length_and_on_circle() {
let circle = tilted_circle();
for &angle in &[0.0, 0.5, 1.0, -1.5, PI - 0.001] {
let pos = circle.at_angle(angle);
assert_relative_eq!(pos.l, circle.r() * angle, epsilon = 1e-12);
let d = (pos.point - circle.center()).norm();
assert_relative_eq!(d, circle.r(), epsilon = 1e-12);
}
}
#[test]
fn stress_closest_position() -> Result<()> {
let n = 1000;
let mut rg = RandomGeometry::new();
for _ in 0..n {
let circle = random_circle();
let points = linear_space(-PI, PI, 1000)
.iter()
.map(|t| circle.at_angle(*t).point)
.collect::<Vec<_>>();
let curve = Curve3::from_points(&points, 1e-10)?;
for _ in 0..10 {
let test_pt = rg.point3(10.0);
let expected = curve.at_closest_to_point(&test_pt).point();
let test_result = circle.closest_position(&test_pt);
assert_relative_eq!(test_result.point, expected, epsilon = 5e-2);
}
}
Ok(())
}
#[test]
fn stress_closest_angle_roundtrip() {
let n = 1000;
for _ in 0..n {
let circle = random_circle();
for &angle in &[0.0, 0.5, 1.0, -1.5, PI - 0.001] {
let pt = circle.at_angle(angle).point;
assert_relative_eq!(circle.closest_angle(&pt), angle, epsilon = 1e-12);
}
}
}
#[test]
fn stress_check_tangent() {
let n = 1000;
let mut rng = rand::rng();
for _ in 0..n {
let circle = random_circle();
let angle = rng.random_range(-PI..PI);
let m0 = circle.at_angle(angle);
let m = circle.at_angle(angle + 1.0f64.to_radians());
assert!(m0.as_surface_point().scalar_projection(&m) > 0.0);
}
}
fn random_circle() -> Circle3 {
let mut rg = RandomGeometry::new();
let r = rg.f64(0.8, 5.0);
let iso = rg.iso3(10.0);
Circle3::new(r, iso)
}
#[test]
fn plane_through_diameter_gives_two_angles() {
// Circle in XY plane, radius 3 at origin. Plane XZ (y=0) cuts at (±3, 0, 0).
let circle = Circle3::from_point_normal(
&Point3::origin(),
&UnitVec3::new_normalize(Vector3::z()),
3.0,
)
.unwrap();
let plane = Plane3::xz(); // y=0
let angles = circle.intersect_plane(&plane);
assert_eq!(angles.len(), 2);
// Both angles must produce points on the plane and on the circle
for &a in &angles {
let pt = circle.at_angle(a).point;
assert_relative_eq!(plane.signed_distance_to_point(&pt), 0.0, epsilon = 1e-10);
assert_relative_eq!((pt - circle.center()).norm(), circle.r(), epsilon = 1e-10);
}
// The two points should be antipodal
let p0 = circle.at_angle(angles[0]).point;
let p1 = circle.at_angle(angles[1]).point;
let mid = Point3::from((p0.coords + p1.coords) / 2.0);
assert_relative_eq!(mid, circle.center(), epsilon = 1e-10);
}
#[test]
fn plane_tangent_gives_one_angle() {
// Circle in XY plane at origin, radius 2. Plane y=2 is tangent at (0,2,0).
let circle = Circle3::from_point_normal(
&Point3::origin(),
&UnitVec3::new_normalize(Vector3::z()),
2.0,
)
.unwrap();
let plane = Plane3::new(Vector3::y_axis(), 2.0);
let angles = circle.intersect_plane(&plane);
assert_eq!(angles.len(), 1);
let pt = circle.at_angle(angles[0]).point;
assert_relative_eq!(pt, Point3::new(0.0, 2.0, 0.0), epsilon = 1e-10);
}
#[test]
fn plane_misses_gives_empty() {
let circle = Circle3::from_point_normal(
&Point3::origin(),
&UnitVec3::new_normalize(Vector3::z()),
1.0,
)
.unwrap();
let plane = Plane3::new(Vector3::y_axis(), 5.0); // y=5, outside circle
assert!(circle.intersect_plane(&plane).is_empty());
}
#[test]
fn parallel_plane_gives_empty() {
// Plane parallel to the circle's own plane
let circle = Circle3::from_point_normal(
&Point3::origin(),
&UnitVec3::new_normalize(Vector3::z()),
1.0,
)
.unwrap();
let plane = Plane3::new(Vector3::z_axis(), 1.0); // z=1, parallel but offset
assert!(circle.intersect_plane(&plane).is_empty());
}
#[test]
fn stress_intersect_plane_angles_on_circle_and_plane() {
// For a random circle and a plane through two known on-circle points, verify the
// returned angles produce points that lie on both the circle and the plane.
for _ in 0..500 {
let circle = random_circle();
let mut rng = rand::rng();
let angle = rng.random_range(-PI..PI);
// Build a plane that passes through a diameter of the circle
let p1 = circle.at_angle(angle).point;
let p2 = circle.at_angle(angle + PI).point;
let some_other = p1 + Vector3::z() * 3.0;
let plane = Plane3::from((&p1, &p2, &some_other));
let angles = circle.intersect_plane(&plane);
assert!(
!angles.is_empty(),
"expected intersection with diameter plane"
);
for &a in &angles {
let pt = circle.at_angle(a).point;
assert_relative_eq!(
plane.signed_distance_to_point(&pt).abs(),
0.0,
epsilon = 1e-8
);
assert_relative_eq!((pt - circle.center()).norm(), circle.r(), epsilon = 1e-8);
}
}
}
#[test]
fn stress_max_extent_angle() {
let n = 1000;
let mut rng = rand::rng();
for _ in 0..n {
let circle = random_circle();
let dir = Vector3::new(
rng.random_range(-1.0..1.0),
rng.random_range(-1.0..1.0),
rng.random_range(-1.0..1.0),
);
// Skip degenerate directions (parallel to normal)
let Ok(angle) = circle.max_extent_angle(&dir) else {
continue;
};
let best = circle.at_angle(angle).point;
let best_dot = best.coords.dot(&dir);
// Sample 5000 points and verify none exceeds the returned dot product
for &a in linear_space(-PI, PI, 5000).iter() {
let pt = circle.at_angle(a).point;
assert!(
pt.coords.dot(&dir) <= best_dot + 1e-6,
"found point with larger dot product than max_extent_angle result"
);
}
}
}
#[test]
fn stress_set_zero_angle() {
let n = 1000;
let mut rng = rand::rng();
for _ in 0..n {
let original = random_circle();
let angle = rng.random_range(-PI..PI);
// Capture the manifold position at `angle` before re-zeroing
let expected = original.at_angle(angle);
let mut circle = original.clone();
circle.set_zero_angle(angle);
// After re-zeroing, angle 0 should land on the same world-space point
let result = circle.at_angle(0.0);
assert_relative_eq!(result.point, expected.point, epsilon = 1e-12);
assert_relative_eq!(
result.direction.into_inner(),
expected.direction.into_inner(),
epsilon = 1e-12
);
}
}
#[test]
fn stress_intersect_plane_angles_in_range() {
let mut rng = rand::rng();
for _ in 0..1000 {
let circle = random_circle();
let angle = rng.random_range(-PI..PI);
let p1 = circle.at_angle(angle).point;
let p2 = circle.at_angle(angle + PI).point;
let some_other = p1 + Vector3::z() * 3.0;
let plane = Plane3::from((&p1, &p2, &some_other));
for a in circle.intersect_plane(&plane) {
assert!(a > -PI && a <= PI, "angle {a} is outside (-π, π]");
}
}
}
// -------------------------------------------------------------------------
// Transformation tests
// -------------------------------------------------------------------------
#[test]
fn new_transformed_by_identity_preserves_all() {
let circle = tilted_circle();
let result = circle.new_transformed_by(&Iso3::identity());
assert_relative_eq!(result.r(), circle.r(), epsilon = 1e-12);
assert_relative_eq!(result.center(), circle.center(), epsilon = 1e-12);
assert_relative_eq!(
result.normal().into_inner(),
circle.normal().into_inner(),
epsilon = 1e-12
);
}
#[test]
fn transform_by_identity_preserves_all() {
let mut circle = tilted_circle();
let original_center = circle.center();
let original_normal = circle.normal().into_inner();
let original_r = circle.r();
circle.transform_by(&Iso3::identity());
assert_relative_eq!(circle.r(), original_r, epsilon = 1e-12);
assert_relative_eq!(circle.center(), original_center, epsilon = 1e-12);
assert_relative_eq!(
circle.normal().into_inner(),
original_normal,
epsilon = 1e-12
);
}
#[test]
fn stress_transform_by() {
let mut rg = RandomGeometry::new();
for _ in 0..1000 {
let original = random_circle();
let iso = rg.iso3(10.0);
let moved = original.new_transformed_by(&iso);
for a in linear_space(-PI, PI, 50).iter() {
let test_point = moved.at_angle(*a).point;
let expected = original.at_angle(*a).point;
assert_relative_eq!(test_point, iso * expected, epsilon = 1e-12);
}
}
}
}