eunoia 0.1.1

A library for creating area-proportional Euler and Venn diagrams
Documentation
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
use crate::geometry::diagram::IntersectionPoint;
use crate::geometry::primitives::Point;
use crate::geometry::shapes::Circle;
use crate::geometry::traits::{Area, Closed, DiagramShape};
use rand::Rng;
use std::collections::HashMap;

pub enum OverlapMethod {
    MonteCarlo,
    Exact,
}

pub fn compute_overlaps<S: DiagramShape>(
    shapes: &[S],
    method: OverlapMethod,
    rng: &mut dyn rand::RngCore,
) -> f64 {
    match method {
        OverlapMethod::MonteCarlo => monte_carlo_overlap(shapes, 100_000, rng),
        OverlapMethod::Exact => {
            // Exact computation requires shape-specific implementations
            // For generic shapes, we fall back to Monte Carlo
            // Use compute_overlaps_circles() for exact circle computation
            monte_carlo_overlap(shapes, 100_000, rng)
        }
    }
}

/// Compute exact overlap areas for circles using geometric calculation.
///
/// This uses the `multiple_overlap_areas` function from the circle module
/// which computes exact areas using polygon + circular segment calculations.
/// For 2 circles, it falls back to the analytical intersection_area method.
///
/// # Arguments
///
/// * `circles` - The circles to compute overlaps for
///
/// # Returns
///
/// Exact intersection area where ALL circles overlap
pub fn compute_overlaps_circles(circles: &[Circle]) -> f64 {
    let n_sets = circles.len();

    if n_sets == 1 {
        return circles[0].area();
    }

    if n_sets == 2 {
        return circles[0].intersection_area(&circles[1]);
    }

    // For 3+ circles: collect intersection points and use polygon method
    let mut intersections: Vec<IntersectionPoint> = Vec::new();

    for i in 0..n_sets {
        for j in (i + 1)..n_sets {
            let pts = circles[i].intersection_points(&circles[j]);
            for point in pts {
                let adopters = (0..n_sets)
                    .filter(|&k| circles[k].contains_point(&point))
                    .collect();

                intersections.push(IntersectionPoint::new(point, (i, j), adopters));
            }
        }
    }

    // Use the exact circle overlap calculation for 3+ circles
    crate::geometry::shapes::circle::multiple_overlap_areas(circles, &intersections)
}

/// Compute overlap areas using Monte Carlo integration.
///
/// # Algorithm
///
/// 1. Filter intersection points to only those in ALL shapes
/// 2. Compute tight bounding box around the intersection region
/// 3. Generate N random points uniformly in the tight bounding box
/// 4. Count points that are in ALL shapes (intersection)
/// 5. Estimate area as: (intersection_count / total_points) × bounding_box_area
///
/// This is much more efficient than sampling the entire shape region because
/// the intersection is typically much smaller than the inclusive area.
///
/// # Arguments
///
/// * `shapes` - The shapes to compute overlaps for
/// * `n_samples` - Number of Monte Carlo samples (more = better accuracy)
///
/// # Returns
///
/// Intersection area estimate (area where ALL shapes overlap)
fn monte_carlo_overlap<S: DiagramShape>(
    shapes: &[S],
    n_samples: usize,
    rng: &mut dyn rand::RngCore,
) -> f64 {
    let n_sets = shapes.len();
    let all_shapes_mask = (1 << n_sets) - 1; // All bits set

    // Compute bounding box for sampling
    // We need a region that's guaranteed to contain the intersection.
    // The intersection of all shapes is contained within the intersection
    // of their bounding boxes.
    let (x_min, x_max, y_min, y_max) = if shapes.len() == 1 {
        // Single shape - use its bounding box
        let bbox = shapes[0].bounding_box();
        let (min_pt, max_pt) = bbox.to_points();
        (min_pt.x(), max_pt.x(), min_pt.y(), max_pt.y())
    } else {
        // Multiple shapes - compute intersection of all bounding boxes
        let mut x_min = f64::NEG_INFINITY;
        let mut x_max = f64::INFINITY;
        let mut y_min = f64::NEG_INFINITY;
        let mut y_max = f64::INFINITY;

        for shape in shapes {
            let bbox = shape.bounding_box();
            let (min_pt, max_pt) = bbox.to_points();

            // Intersection of bounding boxes
            x_min = x_min.max(min_pt.x());
            x_max = x_max.min(max_pt.x());
            y_min = y_min.max(min_pt.y());
            y_max = y_max.min(max_pt.y());
        }

        // If bounding boxes don't overlap, there's no intersection
        if x_min >= x_max || y_min >= y_max {
            return 0.0;
        }

        (x_min, x_max, y_min, y_max)
    };

    let bbox_area = (x_max - x_min) * (y_max - y_min);

    // Track region counts: region_mask -> count
    let mut region_counts: HashMap<usize, usize> = HashMap::new();

    // Generate random points and test containment
    for _ in 0..n_samples {
        let x = rng.random_range(x_min..x_max);
        let y = rng.random_range(y_min..y_max);
        let point = Point::new(x, y);

        // Determine which shapes contain this point
        let mut region_mask = 0_usize;
        for (i, shape) in shapes.iter().enumerate() {
            if shape.contains_point(&point) {
                region_mask |= 1 << i;
            }
        }

        // Increment count for this region
        if region_mask > 0 {
            *region_counts.entry(region_mask).or_insert(0) += 1;
        }
    }

    // Compute intersection area (only points in ALL shapes)
    let intersection_count = region_counts.get(&all_shapes_mask).copied().unwrap_or(0);
    (intersection_count as f64 / n_samples as f64) * bbox_area
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::shapes::Circle;
    use rand::rngs::StdRng;
    use rand::SeedableRng;

    #[test]
    fn test_monte_carlo_single_circle() {
        let mut rng = StdRng::seed_from_u64(42);
        let circle = Circle::new(Point::new(0.0, 0.0), 1.0);
        let shapes = vec![circle];

        // Expected area: π ≈ 3.14159 (single shape = full area)
        let estimated = monte_carlo_overlap(&shapes, 100_000, &mut rng);
        let expected = std::f64::consts::PI;

        // Monte Carlo should be within ~1% for 100k samples
        let error = (estimated - expected).abs() / expected;
        assert!(
            error < 0.01,
            "Error too large: {} (estimated: {}, expected: {})",
            error,
            estimated,
            expected
        );
    }

    #[test]
    fn test_monte_carlo_two_separate_circles() {
        let mut rng = StdRng::seed_from_u64(42);
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let c2 = Circle::new(Point::new(10.0, 0.0), 1.0);
        let shapes = vec![c1, c2];

        // Expected area: 0 (no overlap/intersection)
        let estimated = monte_carlo_overlap(&shapes, 100_000, &mut rng);

        assert!(
            estimated.abs() < 0.001,
            "Should be near zero but got: {}",
            estimated
        );
    }

    #[test]
    fn test_monte_carlo_two_overlapping_circles() {
        let mut rng = StdRng::seed_from_u64(42);
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let c2 = Circle::new(Point::new(1.0, 0.0), 1.0);
        let shapes = vec![c1, c2];

        // Compute intersection points
        let pts = c1.intersection_points(&c2);
        let mut intersections = Vec::new();
        for point in pts {
            let adopters = vec![0, 1]; // Both circles contain these points
            intersections.push(IntersectionPoint::new(point, (0, 1), adopters));
        }

        // Expected area: intersection area only
        let expected = c1.intersection_area(&c2);

        let estimated = monte_carlo_overlap(&shapes, 100_000, &mut rng);

        let error = (estimated - expected).abs() / expected;
        assert!(
            error < 0.02,
            "Error too large: {} (estimated: {}, expected: {})",
            error,
            estimated,
            expected
        );
    }

    #[test]
    fn test_monte_carlo_three_overlapping_circles() {
        let mut rng = StdRng::seed_from_u64(42);
        // Three circles with a common intersection region
        let c1 = Circle::new(Point::new(0.0, 0.0), 2.0);
        let c2 = Circle::new(Point::new(1.5, 0.0), 2.0);
        let c3 = Circle::new(Point::new(0.75, 1.3), 2.0);
        let shapes = vec![c1, c2, c3];

        // Should have some 3-way intersection
        let estimated = monte_carlo_overlap(&shapes, 100_000, &mut rng);

        // Just verify it's positive and reasonable
        assert!(
            estimated > 0.5 && estimated < 5.0,
            "Expected reasonable 3-way intersection area, got: {}",
            estimated
        );
    }

    #[test]
    fn test_exact_circles_two_overlapping() {
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let c2 = Circle::new(Point::new(1.0, 0.0), 1.0);
        let circles = vec![c1, c2];

        // Exact computation for circles
        let exact = compute_overlaps_circles(&circles);

        // Should match the analytical intersection area
        let expected = c1.intersection_area(&c2);

        let error = (exact - expected).abs() / expected;
        assert!(
            error < 0.001,
            "Exact should be very close: {} vs {}",
            exact,
            expected
        );
    }

    #[test]
    fn test_exact_circles_three_overlapping() {
        let c1 = Circle::new(Point::new(0.0, 0.0), 2.0);
        let c2 = Circle::new(Point::new(1.5, 0.0), 2.0);
        let c3 = Circle::new(Point::new(0.75, 1.3), 2.0);
        let circles = vec![c1, c2, c3];

        // Exact computation for 3-way intersection
        let exact = compute_overlaps_circles(&circles);

        // Should be positive (there is a 3-way intersection)
        assert!(
            exact > 0.0,
            "Expected positive 3-way intersection area, got: {}",
            exact
        );
    }

    #[test]
    fn test_exact_vs_monte_carlo_two_circles() {
        let mut rng = StdRng::seed_from_u64(42);
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let c2 = Circle::new(Point::new(1.0, 0.0), 1.0);
        let circles = vec![c1, c2];

        // Exact computation
        let exact = compute_overlaps_circles(&circles);

        // Monte Carlo computation
        let monte_carlo = monte_carlo_overlap(&circles, 100_000, &mut rng);

        // They should be within ~2% of each other
        let error = (exact - monte_carlo).abs() / exact;
        assert!(
            error < 0.02,
            "Exact and Monte Carlo should agree: exact={}, monte_carlo={}, error={}",
            exact,
            monte_carlo,
            error
        );
    }

    #[test]
    fn test_exact_vs_monte_carlo_three_circles() {
        let mut rng = StdRng::seed_from_u64(42);
        let c1 = Circle::new(Point::new(0.0, 0.0), 2.0);
        let c2 = Circle::new(Point::new(1.5, 0.0), 2.0);
        let c3 = Circle::new(Point::new(0.75, 1.3), 2.0);
        let circles = vec![c1, c2, c3];

        // Exact computation
        let exact = compute_overlaps_circles(&circles);

        // Monte Carlo computation
        let monte_carlo = monte_carlo_overlap(&circles, 100_000, &mut rng);

        // They should be within ~3% of each other (3-way is harder)
        let error = (exact - monte_carlo).abs() / exact;
        assert!(
            error < 0.03,
            "Exact and Monte Carlo should agree: exact={}, monte_carlo={}, error={}",
            exact,
            monte_carlo,
            error
        );
    }

    #[test]
    fn test_exact_vs_monte_carlo_no_overlap() {
        let mut rng = StdRng::seed_from_u64(42);
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let c2 = Circle::new(Point::new(10.0, 0.0), 1.0);
        let circles = vec![c1, c2];

        // Exact computation
        let exact = compute_overlaps_circles(&circles);

        // Monte Carlo computation
        let monte_carlo = monte_carlo_overlap(&circles, 100_000, &mut rng);

        // Both should be essentially zero
        assert!(
            exact.abs() < 0.001 && monte_carlo.abs() < 0.001,
            "Both should be near zero: exact={}, monte_carlo={}",
            exact,
            monte_carlo
        );
    }

    #[test]
    fn test_exact_vs_monte_carlo_highly_overlapping() {
        let mut rng = StdRng::seed_from_u64(42);
        // Three circles that overlap significantly
        let c1 = Circle::new(Point::new(0.0, 0.0), 2.5);
        let c2 = Circle::new(Point::new(1.0, 0.0), 2.5);
        let c3 = Circle::new(Point::new(0.5, 0.8), 2.5);
        let circles = vec![c1, c2, c3];

        // Exact computation
        let exact = compute_overlaps_circles(&circles);

        // Monte Carlo computation
        let monte_carlo = monte_carlo_overlap(&circles, 100_000, &mut rng);

        // They should be within ~3% of each other
        let error = (exact - monte_carlo).abs() / exact;
        assert!(
            error < 0.03,
            "Exact and Monte Carlo should agree: exact={}, monte_carlo={}, error={}",
            exact,
            monte_carlo,
            error
        );

        // Should have substantial area (large overlapping circles)
        assert!(
            exact > 5.0,
            "Expected large 3-way intersection for highly overlapping circles, got: {}",
            exact
        );
    }

    #[test]
    fn test_exact_vs_monte_carlo_barely_overlapping() {
        let mut rng = StdRng::seed_from_u64(42);
        // Three circles that barely overlap
        let c1 = Circle::new(Point::new(0.0, 0.0), 1.5);
        let c2 = Circle::new(Point::new(2.5, 0.0), 1.5);
        let c3 = Circle::new(Point::new(1.25, 2.0), 1.5);
        let circles = vec![c1, c2, c3];

        // Exact computation
        let exact = compute_overlaps_circles(&circles);

        // Monte Carlo computation
        let monte_carlo = monte_carlo_overlap(&circles, 100_000, &mut rng);

        // For small intersections, allow slightly larger error
        if exact > 0.1 && monte_carlo > 0.1 {
            let error = (exact - monte_carlo).abs() / exact;
            assert!(
                error < 0.05,
                "Exact and Monte Carlo should agree: exact={}, monte_carlo={}, error={}",
                exact,
                monte_carlo,
                error
            );
        } else {
            // Both should be very small
            assert!(
                exact < 0.5 && monte_carlo < 0.5,
                "Both should be small for barely overlapping circles: exact={}, monte_carlo={}",
                exact,
                monte_carlo
            );
        }
    }

    #[test]
    fn test_exact_single_circle() {
        let c = Circle::new(Point::new(2.0, 3.0), 1.5);
        let circles = vec![c];

        let exact = compute_overlaps_circles(&circles);
        let expected = c.area();

        let error = (exact - expected).abs() / expected;
        assert!(
            error < 0.001,
            "Single circle should return its area: exact={}, expected={}",
            exact,
            expected
        );
    }

    #[test]
    fn test_exact_contained_circles() {
        // One circle completely inside another
        let c1 = Circle::new(Point::new(0.0, 0.0), 3.0);
        let c2 = Circle::new(Point::new(0.0, 0.0), 1.0);
        let circles = vec![c1, c2];

        let exact = compute_overlaps_circles(&circles);
        let expected = c2.area(); // Intersection is the smaller circle

        let error = (exact - expected).abs() / expected;
        assert!(
            error < 0.001,
            "Contained circle intersection should be smaller circle area: exact={}, expected={}",
            exact,
            expected
        );
    }
}