|
| 1 | +use crate::Point; |
| 2 | +use std::f64::consts::PI; |
| 3 | +use rand::SeedableRng; |
| 4 | +use rand::Rng; |
| 5 | + |
| 6 | +pub struct DataGen; |
| 7 | + |
| 8 | +impl DataGen { |
| 9 | + /// Generate a closed, non-intersecting random polygon with specified number of edges |
| 10 | + /// |
| 11 | + /// Uses a convex hull approach: generates random points in polar coordinates |
| 12 | + /// and sorts them by angle to ensure no self-intersections. |
| 13 | + /// |
| 14 | + /// # Arguments |
| 15 | + /// * `num_edges` - Number of edges for the polygon (minimum 3) |
| 16 | + /// * `seed` - Optional seed for reproducible random generation. If None, uses system entropy |
| 17 | + pub fn random_polygon(num_edges: usize, seed: u64) -> Vec<Point> { |
| 18 | + if num_edges < 3 { |
| 19 | + panic!("Polygon must have at least 3 edges"); |
| 20 | + } |
| 21 | + |
| 22 | + let mut rng = rand::rngs::StdRng::seed_from_u64(seed); |
| 23 | + |
| 24 | + // Generate random points in polar coordinates |
| 25 | + let mut points: Vec<Point> = (0..num_edges) |
| 26 | + .map(|_| { |
| 27 | + let angle = rng.random_range(0.0..(2.0 * PI)); |
| 28 | + let radius = rng.random_range(0.5..2.0); |
| 29 | + Point::new(radius * angle.cos(), radius * angle.sin()) |
| 30 | + }) |
| 31 | + .collect(); |
| 32 | + |
| 33 | + // Sort by angle from centroid to ensure CCW ordering and no self-intersections |
| 34 | + let centroid = Self::centroid(&points); |
| 35 | + points.sort_by(|a, b| { |
| 36 | + let angle_a = (a.y - centroid.y).atan2(a.x - centroid.x); |
| 37 | + let angle_b = (b.y - centroid.y).atan2(b.x - centroid.x); |
| 38 | + angle_a.partial_cmp(&angle_b).unwrap_or(std::cmp::Ordering::Equal) |
| 39 | + }); |
| 40 | + |
| 41 | + points |
| 42 | + } |
| 43 | + |
| 44 | + fn centroid(points: &[Point]) -> Point { |
| 45 | + if points.is_empty() { |
| 46 | + return Point::new(0.0, 0.0); |
| 47 | + } |
| 48 | + |
| 49 | + let sum_x: f64 = points.iter().map(|p| p.x).sum(); |
| 50 | + let sum_y: f64 = points.iter().map(|p| p.y).sum(); |
| 51 | + let len = points.len() as f64; |
| 52 | + |
| 53 | + Point::new(sum_x / len, sum_y / len) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[cfg(test)] |
| 58 | +mod tests { |
| 59 | + use super::*; |
| 60 | + use crate::nfp_points::polygon; |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn test_random_polygon_generation() { |
| 64 | + let poly = DataGen::random_polygon(100, 42); |
| 65 | + |
| 66 | + // Verify correct number of edges |
| 67 | + assert_eq!(poly.len(), 100); |
| 68 | + |
| 69 | + // Verify polygon is closed and non-degenerate |
| 70 | + assert!(!poly.is_empty()); |
| 71 | + |
| 72 | + // Verify CCW orientation |
| 73 | + assert!(polygon::is_ccw(&poly)); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_random_polygon_minimum_size() { |
| 78 | + let poly = DataGen::random_polygon(3, 42); |
| 79 | + assert_eq!(poly.len(), 3); |
| 80 | + assert!(polygon::is_ccw(&poly)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_random_polygon_with_seed() { |
| 85 | + // Same seed should produce identical polygons |
| 86 | + let poly1 = DataGen::random_polygon(50, 12345); |
| 87 | + let poly2 = DataGen::random_polygon(50, 12345); |
| 88 | + |
| 89 | + assert_eq!(poly1.len(), poly2.len()); |
| 90 | + for (p1, p2) in poly1.iter().zip(poly2.iter()) { |
| 91 | + assert!((p1.x - p2.x).abs() < 1e-10); |
| 92 | + assert!((p1.y - p2.y).abs() < 1e-10); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + #[should_panic] |
| 98 | + fn test_random_polygon_invalid_size() { |
| 99 | + DataGen::random_polygon(2, 42); |
| 100 | + } |
| 101 | +} |
0 commit comments