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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! Layout representation - the result of fitting a diagram specification.
use crate::geometry::diagram;
use crate::geometry::shapes::Circle;
use crate::geometry::traits::DiagramShape;
use crate::loss::LossType;
use crate::spec::{Combination, DiagramSpec};
use std::collections::{HashMap, HashSet};
/// Result of fitting a diagram specification to shapes.
///
/// The type parameter `S` determines which shape type was used (e.g., Circle, Ellipse).
/// Defaults to `Circle` for backward compatibility.
#[derive(Debug, Clone)]
pub struct Layout<S: DiagramShape = Circle> {
/// The fitted shapes (one per set, in the order of the original spec's set names).
///
/// Sets that were pruned during preprocessing (e.g. empty sets) are represented
/// by zero-parameter shapes at their original index.
pub(crate) shapes: Vec<S>,
/// Mapping from set names to shape indices.
set_to_shape: HashMap<String, usize>,
/// Original requested combination areas.
requested: HashMap<Combination, f64>,
/// Actual fitted combination areas (computed from shapes).
fitted: HashMap<Combination, f64>,
/// The loss function used during optimization (same objective is reported by `loss()`).
loss_type: LossType,
/// Final loss value (computed using `loss_type`).
loss: f64,
/// Number of iterations performed.
iterations: usize,
}
impl<S: DiagramShape + Copy + 'static> Layout<S> {
/// Creates a new layout from shapes and specification.
///
/// This computes the fitted areas and loss automatically. The `loss_type` determines
/// the objective reported by [`Layout::loss`] — it should match the objective the
/// fitter minimized so callers see a self-consistent value.
pub(crate) fn new(
shapes: Vec<S>,
set_to_shape: HashMap<String, usize>,
spec: &DiagramSpec,
iterations: usize,
loss_type: LossType,
) -> Self {
let requested = spec.exclusive_areas().clone();
let fitted = Self::compute_fitted_areas(&shapes, spec);
let loss = Self::compute_loss(&requested, &fitted, loss_type);
Layout {
shapes,
set_to_shape,
requested,
fitted,
loss_type,
loss,
iterations,
}
}
/// Get the fitted shapes.
pub fn shapes(&self) -> &[S] {
&self.shapes
}
/// Get the requested areas.
pub fn requested(&self) -> &HashMap<Combination, f64> {
&self.requested
}
/// Get the actual fitted areas.
pub fn fitted(&self) -> &HashMap<Combination, f64> {
&self.fitted
}
/// Get the final loss value, computed using the optimizer's objective ([`Layout::loss_type`]).
pub fn loss(&self) -> f64 {
self.loss
}
/// Get the loss function used by the optimizer.
pub fn loss_type(&self) -> LossType {
self.loss_type
}
/// Residuals per region: `requested - fitted`.
///
/// Includes every combination that appears in either requested or fitted areas.
pub fn residuals(&self) -> HashMap<Combination, f64> {
self.all_combinations()
.into_iter()
.map(|combo| {
let t = self.requested.get(&combo).copied().unwrap_or(0.0);
let f = self.fitted.get(&combo).copied().unwrap_or(0.0);
(combo, t - f)
})
.collect()
}
/// Per-region error: `|f_i / Σf - t_i / Σt|` for each combination.
///
/// Matches eulerr's `regionError` definition. Returns an empty map if either the
/// sum of fitted or the sum of requested areas is effectively zero.
pub fn region_error(&self) -> HashMap<Combination, f64> {
let sum_f: f64 = self.fitted.values().sum();
let sum_t: f64 = self.requested.values().sum();
if sum_f.abs() < 1e-10 || sum_t.abs() < 1e-10 {
return HashMap::new();
}
self.all_combinations()
.into_iter()
.map(|combo| {
let f = self.fitted.get(&combo).copied().unwrap_or(0.0);
let t = self.requested.get(&combo).copied().unwrap_or(0.0);
(combo, (f / sum_f - t / sum_t).abs())
})
.collect()
}
/// Diagnostic error: the maximum of [`Layout::region_error`] values.
///
/// Matches eulerr's `diagError` scalar (EulerAPE style).
pub fn diag_error(&self) -> f64 {
self.region_error()
.values()
.copied()
.fold(0.0_f64, f64::max)
}
/// venneuler-style stress metric (matches eulerr's `stress`):
/// `Σ(f - β·t)² / Σf²` where `β = Σ(f·t) / Σt²`.
///
/// Returns 0.0 if `Σt² < ε` or `Σf² < ε`.
pub fn stress(&self) -> f64 {
let combos = self.all_combinations();
let sum_ft: f64 = combos
.iter()
.map(|c| {
let f = self.fitted.get(c).copied().unwrap_or(0.0);
let t = self.requested.get(c).copied().unwrap_or(0.0);
f * t
})
.sum();
let sum_t2: f64 = self.requested.values().map(|&v| v * v).sum();
let sum_f2: f64 = self.fitted.values().map(|&v| v * v).sum();
if sum_t2 < 1e-20 || sum_f2 < 1e-20 {
return 0.0;
}
let beta = sum_ft / sum_t2;
let numerator: f64 = combos
.into_iter()
.map(|c| {
let f = self.fitted.get(&c).copied().unwrap_or(0.0);
let t = self.requested.get(&c).copied().unwrap_or(0.0);
(f - beta * t).powi(2)
})
.sum();
numerator / sum_f2
}
fn all_combinations(&self) -> Vec<Combination> {
let set: HashSet<Combination> = self
.requested
.keys()
.chain(self.fitted.keys())
.cloned()
.collect();
set.into_iter().collect()
}
/// Get the number of iterations.
pub fn iterations(&self) -> usize {
self.iterations
}
/// Get the shape for a specific set.
pub fn shape_for_set(&self, set_name: &str) -> Option<&S> {
self.set_to_shape
.get(set_name)
.map(|&idx| &self.shapes[idx])
}
/// Normalize the layout by rotating, centering, and packing disjoint clusters.
///
/// This modifies the layout in-place to:
/// 1. Rotate each cluster to a canonical orientation (first two shapes horizontal)
/// 2. Mirror clusters so the first shape is in the bottom-left
/// 3. Pack disjoint clusters together compactly
/// 4. Center the entire layout around the origin
///
/// # Arguments
///
/// * `padding_factor` - Padding between clusters as a fraction of total width
///
/// # Examples
///
/// ```
/// use eunoia::{DiagramSpecBuilder, Fitter};
/// use eunoia::geometry::shapes::Circle;
///
/// let spec = DiagramSpecBuilder::new()
/// .set("A", 10.0)
/// .set("B", 8.0)
/// .intersection(&["A", "B"], 2.0)
/// .build()
/// .unwrap();
///
/// let mut layout = Fitter::<Circle>::new(&spec).fit().unwrap();
/// layout.normalize(0.015);
/// ```
pub fn normalize(&mut self, padding_factor: f64)
where
S: Clone,
{
crate::fitter::normalize::normalize_layout(&mut self.shapes, padding_factor);
}
/// Decomposes the fitted shapes into polygons for each exclusive region.
///
/// This is useful for visualization where you want to fill each region
/// with a different color or pattern.
///
/// **Requires the `plotting` feature to be enabled.**
///
/// # Arguments
///
/// * `spec` - The diagram specification
/// * `n_vertices` - Number of vertices to use when converting shapes to polygons (e.g., 64)
///
/// # Examples
///
/// ```ignore
/// use eunoia::{DiagramSpecBuilder, Fitter};
/// use eunoia::geometry::shapes::Circle;
///
/// let spec = DiagramSpecBuilder::new()
/// .set("A", 10.0)
/// .set("B", 8.0)
/// .intersection(&["A", "B"], 2.0)
/// .build()
/// .unwrap();
///
/// let layout = Fitter::<Circle>::new(&spec).fit().unwrap();
/// let regions = layout.region_polygons(&spec, 64);
///
/// // Iterate over regions
/// for (combination, polygons) in regions.iter() {
/// println!("{}: {} polygons", combination, polygons.len());
/// }
/// ```
#[cfg(feature = "plotting")]
pub fn region_polygons(
&self,
spec: &DiagramSpec,
n_vertices: usize,
) -> crate::plotting::RegionPolygons
where
S: crate::geometry::traits::Polygonize,
{
let set_names = spec.set_names();
crate::plotting::decompose_regions(&self.shapes, set_names, spec, n_vertices)
}
/// Builds a [`PlotData`] bundle — region polygons, per-region and per-set
/// label anchors, and per-set outlines — in one call.
///
/// This is the recommended entry point for renderers and language
/// bindings: it computes everything a typical Euler-diagram drawing
/// routine needs from the fitted layout, with options for
/// polygonization resolution and label-anchor precision.
///
/// **Requires the `plotting` feature to be enabled.**
///
/// # Examples
///
/// ```
/// use eunoia::{DiagramSpecBuilder, Fitter, InputType};
/// use eunoia::geometry::shapes::Circle;
/// use eunoia::plotting::PlotOptions;
///
/// let spec = DiagramSpecBuilder::new()
/// .set("A", 5.0)
/// .set("B", 3.0)
/// .intersection(&["A", "B"], 1.0)
/// .input_type(InputType::Exclusive)
/// .build()
/// .unwrap();
///
/// let layout = Fitter::<Circle>::new(&spec).seed(42).fit().unwrap();
/// let plot = layout.plot_data(&spec, PlotOptions::default());
///
/// // Hand `plot.regions`, `plot.region_anchors`, `plot.set_anchors`,
/// // `plot.shape_outlines` to the renderer of your choice.
/// ```
///
/// [`PlotData`]: crate::plotting::PlotData
#[cfg(feature = "plotting")]
pub fn plot_data(
&self,
spec: &DiagramSpec,
options: crate::plotting::PlotOptions,
) -> crate::plotting::PlotData
where
S: crate::geometry::traits::Polygonize,
{
crate::plotting::build_plot_data(&self.shapes, spec, options)
}
/// Compute all combination areas from current shapes.
fn compute_fitted_areas(shapes: &[S], spec: &DiagramSpec) -> HashMap<Combination, f64> {
let set_names = spec.set_names();
// Use the shape-specific exact computation method
let exclusive_areas_by_mask = S::compute_exclusive_regions(shapes);
// Convert RegionMask to Combination
let mut exclusive_combos = HashMap::new();
for (mask, area) in exclusive_areas_by_mask {
if area > 1e-10 {
// Only include non-negligible areas
let indices = diagram::mask_to_indices(mask, shapes.len());
let combo_sets: Vec<&str> =
indices.iter().map(|&i| set_names[i].as_str()).collect();
if !combo_sets.is_empty() {
let combo = Combination::new(&combo_sets);
exclusive_combos.insert(combo, area);
}
}
}
exclusive_combos
}
/// Compute the loss between requested and fitted areas using the optimizer's loss type.
fn compute_loss(
requested: &HashMap<Combination, f64>,
fitted: &HashMap<Combination, f64>,
loss_type: LossType,
) -> f64 {
// LossType operates on RegionMask-keyed maps, but the mask encoding is only
// relevant within a single call. Assign each distinct combination to a unique
// mask so LossType sees the same pairing of fitted/requested values.
let mut combo_to_mask: HashMap<&Combination, diagram::RegionMask> = HashMap::new();
for combo in requested.keys().chain(fitted.keys()) {
let next = combo_to_mask.len();
combo_to_mask.entry(combo).or_insert(next);
}
let fitted_mask: HashMap<diagram::RegionMask, f64> =
fitted.iter().map(|(c, &v)| (combo_to_mask[c], v)).collect();
let requested_mask: HashMap<diagram::RegionMask, f64> = requested
.iter()
.map(|(c, &v)| (combo_to_mask[c], v))
.collect();
loss_type.compute(&fitted_mask, &requested_mask)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::spec::DiagramSpecBuilder;
#[test]
fn test_layout_creation() {
use crate::geometry::primitives::Point;
let spec = DiagramSpecBuilder::new()
.set("A", std::f64::consts::PI)
.build()
.unwrap();
let shapes = vec![Circle::new(Point::new(0.0, 0.0), 1.0)];
let mut set_to_shape = HashMap::new();
set_to_shape.insert("A".to_string(), 0);
let layout = Layout::new(shapes, set_to_shape, &spec, 0, LossType::sse());
assert_eq!(layout.shapes().len(), 1);
assert!(layout.loss() < 0.001); // Should be very close to π
}
#[test]
fn test_shape_for_set() {
use crate::geometry::primitives::Point;
let spec = DiagramSpecBuilder::new().set("A", 10.0).build().unwrap();
let shapes = vec![Circle::new(Point::new(1.0, 2.0), 3.0)];
let mut set_to_shape = HashMap::new();
set_to_shape.insert("A".to_string(), 0);
let layout = Layout::new(shapes, set_to_shape, &spec, 0, LossType::sse());
let circle = layout.shape_for_set("A").unwrap();
assert_eq!(circle.radius(), 3.0);
assert_eq!(circle.center().x(), 1.0);
assert_eq!(circle.center().y(), 2.0);
}
#[test]
fn test_ellipse_area_computation_uses_exact_method() {
use crate::geometry::primitives::Point;
use crate::geometry::shapes::Ellipse;
use crate::spec::InputType;
// Test case: Three disjoint ellipses should NOT report intersection areas
// This was the bug: Monte Carlo sampling was reporting spurious intersections
let spec = DiagramSpecBuilder::new()
.set("A", 2.9)
.set("B", 4.9)
.set("C", 1.0)
.input_type(InputType::Exclusive)
.build()
.unwrap();
// Create three disjoint ellipses (circles for simplicity)
let shapes = vec![
Ellipse::new(Point::new(-5.0, 0.0), 1.0, 1.0, 0.0), // A: left
Ellipse::new(Point::new(5.0, 0.0), 1.3, 1.3, 0.0), // B: right
Ellipse::new(Point::new(0.0, 5.0), 0.6, 0.6, 0.0), // C: top
];
let mut set_to_shape = HashMap::new();
set_to_shape.insert("A".to_string(), 0);
set_to_shape.insert("B".to_string(), 1);
set_to_shape.insert("C".to_string(), 2);
let layout = Layout::new(shapes, set_to_shape, &spec, 0, LossType::sse());
// Check fitted areas - there should be NO intersection areas
let ab_combo = Combination::new(&["A", "B"]);
let ac_combo = Combination::new(&["A", "C"]);
let bc_combo = Combination::new(&["B", "C"]);
let abc_combo = Combination::new(&["A", "B", "C"]);
let ab_area = layout.fitted().get(&ab_combo).copied().unwrap_or(0.0);
let ac_area = layout.fitted().get(&ac_combo).copied().unwrap_or(0.0);
let bc_area = layout.fitted().get(&bc_combo).copied().unwrap_or(0.0);
let abc_area = layout.fitted().get(&abc_combo).copied().unwrap_or(0.0);
// All intersection areas should be zero (or negligible) since shapes are disjoint
assert!(
ab_area < 1e-6,
"A&B should be ~0 for disjoint shapes, got {}",
ab_area
);
assert!(
ac_area < 1e-6,
"A&C should be ~0 for disjoint shapes, got {}",
ac_area
);
assert!(
bc_area < 1e-6,
"B&C should be ~0 for disjoint shapes, got {}",
bc_area
);
assert!(
abc_area < 1e-6,
"A&B&C should be ~0 for disjoint shapes, got {}",
abc_area
);
// Individual areas should match shape areas
let a_only = layout
.fitted()
.get(&Combination::new(&["A"]))
.copied()
.unwrap_or(0.0);
let expected_a = std::f64::consts::PI * 1.0 * 1.0;
assert!(
(a_only - expected_a).abs() < 0.01,
"A area should be ~π, got {}",
a_only
);
}
#[test]
fn test_empty_set_reinsertion() {
// A spec with one empty set (C = 0) should still produce a layout with an
// entry (zero shape) for C in its original position.
use crate::fitter::Fitter;
let spec = DiagramSpecBuilder::new()
.set("A", 10.0)
.set("B", 8.0)
.set("C", 0.0)
.intersection(&["A", "B"], 2.0)
.build()
.unwrap();
let layout = Fitter::<Circle>::new(&spec).seed(42).fit().unwrap();
// All 3 original sets should be represented.
assert_eq!(layout.shapes().len(), 3);
// C must be accessible by name and have zero area.
let c_shape = layout.shape_for_set("C").expect("C should be present");
assert_eq!(c_shape.radius(), 0.0);
// Surviving sets should have positive-area shapes.
let a_shape = layout.shape_for_set("A").expect("A should be present");
let b_shape = layout.shape_for_set("B").expect("B should be present");
assert!(a_shape.radius() > 0.0);
assert!(b_shape.radius() > 0.0);
}
#[test]
fn test_named_metric_accessors() {
use crate::fitter::Fitter;
// A simple 2-set overlap. The fitter should get close to exact for circles.
let spec = DiagramSpecBuilder::new()
.set("A", 5.0)
.set("B", 2.0)
.intersection(&["A", "B"], 1.0)
.build()
.unwrap();
let layout = Fitter::<Circle>::new(&spec).seed(42).fit().unwrap();
// residuals, region_error should have one entry per combination.
assert!(!layout.residuals().is_empty());
let region_err = layout.region_error();
assert!(!region_err.is_empty());
// diagError = max(regionError)
let max_region_err = region_err.values().copied().fold(0.0_f64, f64::max);
assert!((layout.diag_error() - max_region_err).abs() < 1e-12);
// stress is bounded [0, 1] for sensible inputs and finite.
let stress = layout.stress();
assert!(stress.is_finite());
assert!(stress >= 0.0);
}
#[test]
fn test_loss_reflects_loss_type() {
// Layout.loss() should match the LossType the optimizer minimized.
use crate::fitter::Fitter;
use crate::loss::LossType;
let spec = DiagramSpecBuilder::new()
.set("A", 5.0)
.set("B", 2.0)
.intersection(&["A", "B"], 1.0)
.build()
.unwrap();
let layout_sse = Fitter::<Circle>::new(&spec)
.seed(42)
.loss_type(LossType::sse())
.fit()
.unwrap();
let layout_rmse = Fitter::<Circle>::new(&spec)
.seed(42)
.loss_type(LossType::rmse())
.fit()
.unwrap();
assert_eq!(layout_sse.loss_type(), LossType::sse());
assert_eq!(layout_rmse.loss_type(), LossType::rmse());
// Given identical fits, RMSE = sqrt(SSE / n). Since both optimized with the
// same seed and the region structure is the same, at least the relationship
// sse >= rmse^2 * n_regions / 1 (approximately) should hold. Instead of
// asserting a tight relationship (the optimizer may take different paths),
// just check each loss is finite and non-negative.
assert!(layout_sse.loss().is_finite() && layout_sse.loss() >= 0.0);
assert!(layout_rmse.loss().is_finite() && layout_rmse.loss() >= 0.0);
}
}