plotters-ternary 0.1.0

Publication-quality ternary diagrams, contours, filled bands, and scalar maps for Plotters
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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
use super::{Error, TernaryCartesian, TernaryGeometry, Tolerance};

/// An axis-aligned logical rectangle in the projected ternary plane.
///
/// The viewport is a clipping and zoom window. It has no drawing behaviour and
/// does not imply a visible frame or Cartesian axes.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TernaryViewport {
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
}

impl TernaryViewport {
    /// Construct a viewport using [`Tolerance::default`] for span validation.
    pub fn new(x_min: f64, x_max: f64, y_min: f64, y_max: f64) -> Result<Self, Error> {
        Self::new_with_tolerance(x_min, x_max, y_min, y_max, Tolerance::default())
    }

    /// Construct a viewport using an explicit minimum-span tolerance.
    pub fn new_with_tolerance(
        x_min: f64,
        x_max: f64,
        y_min: f64,
        y_max: f64,
        tolerance: Tolerance,
    ) -> Result<Self, Error> {
        tolerance.validate()?;
        if !x_min.is_finite() || !x_max.is_finite() || !y_min.is_finite() || !y_max.is_finite() {
            return Err(Error::NonFiniteViewportBounds {
                x_min,
                x_max,
                y_min,
                y_max,
            });
        }

        let width = x_max - x_min;
        let height = y_max - y_min;
        if !width.is_finite()
            || !height.is_finite()
            || width <= tolerance.absolute
            || height <= tolerance.absolute
        {
            return Err(Error::InvalidViewport {
                x_min,
                x_max,
                y_min,
                y_max,
                minimum_span: tolerance.absolute,
            });
        }

        Ok(Self {
            x_min,
            x_max,
            y_min,
            y_max,
        })
    }

    /// Return the tight axis-aligned bounds of the complete projected triangle.
    ///
    /// ```
    /// use plotters_ternary::{TernaryGeometry, TernaryViewport};
    ///
    /// let viewport = TernaryViewport::full(TernaryGeometry::default());
    /// assert_eq!(viewport.x_min(), 0.0);
    /// assert_eq!(viewport.x_max(), 1.0);
    /// ```
    pub fn full(geometry: TernaryGeometry) -> Self {
        let vertices = geometry.vertices();
        let x_min = vertices
            .iter()
            .map(|vertex| vertex.x)
            .fold(f64::INFINITY, f64::min);
        let x_max = vertices
            .iter()
            .map(|vertex| vertex.x)
            .fold(f64::NEG_INFINITY, f64::max);
        let y_min = vertices
            .iter()
            .map(|vertex| vertex.y)
            .fold(f64::INFINITY, f64::min);
        let y_max = vertices
            .iter()
            .map(|vertex| vertex.y)
            .fold(f64::NEG_INFINITY, f64::max);
        Self {
            x_min,
            x_max,
            y_min,
            y_max,
        }
    }

    /// Minimum logical X coordinate.
    pub const fn x_min(self) -> f64 {
        self.x_min
    }

    /// Maximum logical X coordinate.
    pub const fn x_max(self) -> f64 {
        self.x_max
    }

    /// Minimum logical Y coordinate.
    pub const fn y_min(self) -> f64 {
        self.y_min
    }

    /// Maximum logical Y coordinate.
    pub const fn y_max(self) -> f64 {
        self.y_max
    }

    /// Logical viewport width.
    pub const fn width(self) -> f64 {
        self.x_max - self.x_min
    }

    /// Logical viewport height.
    pub const fn height(self) -> f64 {
        self.y_max - self.y_min
    }

    /// Return whether a point is inside or on the viewport within tolerance.
    pub fn contains(
        self,
        cartesian: TernaryCartesian,
        tolerance: Tolerance,
    ) -> Result<bool, Error> {
        Ok(self.classify(cartesian, tolerance)? != ViewportPointLocation::Outside)
    }

    /// Classify a point against the viewport independently of triangle geometry.
    pub fn classify(
        self,
        cartesian: TernaryCartesian,
        tolerance: Tolerance,
    ) -> Result<ViewportPointLocation, Error> {
        tolerance.validate()?;
        if !cartesian.x.is_finite() || !cartesian.y.is_finite() {
            return Err(Error::NonFiniteCartesian {
                x: cartesian.x,
                y: cartesian.y,
            });
        }

        let outside = (cartesian.x < self.x_min && !tolerance.is_close(cartesian.x, self.x_min))
            || (cartesian.x > self.x_max && !tolerance.is_close(cartesian.x, self.x_max))
            || (cartesian.y < self.y_min && !tolerance.is_close(cartesian.y, self.y_min))
            || (cartesian.y > self.y_max && !tolerance.is_close(cartesian.y, self.y_max));
        if outside {
            return Ok(ViewportPointLocation::Outside);
        }

        if tolerance.is_close(cartesian.x, self.x_min)
            || tolerance.is_close(cartesian.x, self.x_max)
            || tolerance.is_close(cartesian.y, self.y_min)
            || tolerance.is_close(cartesian.y, self.y_max)
        {
            Ok(ViewportPointLocation::Boundary)
        } else {
            Ok(ViewportPointLocation::Inside)
        }
    }
}

/// A point's location relative only to a logical viewport rectangle.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ViewportPointLocation {
    /// Strictly inside by more than the supplied tolerance.
    Inside,
    /// On or within tolerance of a viewport side.
    Boundary,
    /// Materially outside at least one viewport side.
    Outside,
}

/// A backend-independent allocated pixel rectangle.
///
/// `(x, y)` is the top-left corner. X increases rightward, Y increases
/// downward, and `width`/`height` are continuous coordinate extents.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct PixelRect {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
}

impl PixelRect {
    /// Construct a non-empty allocated pixel rectangle.
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Result<Self, Error> {
        let rectangle = Self {
            x,
            y,
            width,
            height,
        };
        rectangle.validate()?;
        Ok(rectangle)
    }

    /// Validate a rectangle, including one built through its public fields.
    pub fn validate(self) -> Result<(), Error> {
        if self.width == 0 || self.height == 0 {
            return Err(Error::InvalidPixelRect {
                x: self.x,
                y: self.y,
                width: self.width,
                height: self.height,
            });
        }
        Ok(())
    }
}

/// A floating-point pixel-space point used before backend rounding.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PixelPoint {
    pub x: f64,
    pub y: f64,
}

impl PixelPoint {
    pub const fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

/// Floating-point bounds actually occupied inside an allocated pixel rectangle.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PixelBounds {
    pub x_min: f64,
    pub x_max: f64,
    pub y_min: f64,
    pub y_max: f64,
}

impl PixelBounds {
    pub const fn width(self) -> f64 {
        self.x_max - self.x_min
    }

    pub const fn height(self) -> f64 {
        self.y_max - self.y_min
    }

    fn contains(self, point: PixelPoint) -> bool {
        point.x >= self.x_min
            && point.x <= self.x_max
            && point.y >= self.y_min
            && point.y <= self.y_max
    }
}

/// How a logical viewport uses an allocated pixel rectangle.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum ViewportFit {
    /// Use one scale for X and Y, potentially leaving unused pixel space.
    #[default]
    PreserveAspect,
    /// Scale X and Y independently to fill the allocation.
    Stretch,
}

/// Placement of aspect-fitted bounds when unused pixel space remains.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum ViewportAlignment {
    #[default]
    Center,
    Top,
    Bottom,
    Left,
    Right,
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

impl ViewportAlignment {
    const fn factors(self) -> (f64, f64) {
        match self {
            Self::Center => (0.5, 0.5),
            Self::Top => (0.5, 0.0),
            Self::Bottom => (0.5, 1.0),
            Self::Left => (0.0, 0.5),
            Self::Right => (1.0, 0.5),
            Self::TopLeft => (0.0, 0.0),
            Self::TopRight => (1.0, 0.0),
            Self::BottomLeft => (0.0, 1.0),
            Self::BottomRight => (1.0, 1.0),
        }
    }
}

/// A reversible mapping between a logical viewport and floating pixel space.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ViewportTransform {
    viewport: TernaryViewport,
    allocated: PixelRect,
    fitted: PixelBounds,
    fit: ViewportFit,
    alignment: ViewportAlignment,
}

impl ViewportTransform {
    /// Fit a logical viewport into an allocated pixel rectangle.
    ///
    /// ```
    /// use plotters_ternary::{
    ///     PixelRect, TernaryCartesian, TernaryViewport, ViewportAlignment,
    ///     ViewportFit, ViewportTransform,
    /// };
    ///
    /// let transform = ViewportTransform::new(
    ///     TernaryViewport::new(0.0, 2.0, 0.0, 1.0)?,
    ///     PixelRect::new(10, 20, 200, 100)?,
    ///     ViewportFit::PreserveAspect,
    ///     ViewportAlignment::Center,
    /// )?;
    /// let pixel = transform.logical_to_pixel(TernaryCartesian::new(0.0, 1.0))?;
    /// assert_eq!(pixel, plotters_ternary::PixelPoint::new(10.0, 20.0));
    /// # Ok::<(), plotters_ternary::Error>(())
    /// ```
    pub fn new(
        viewport: TernaryViewport,
        allocated: PixelRect,
        fit: ViewportFit,
        alignment: ViewportAlignment,
    ) -> Result<Self, Error> {
        allocated.validate()?;
        let allocated_width = f64::from(allocated.width);
        let allocated_height = f64::from(allocated.height);
        let allocated_x = f64::from(allocated.x);
        let allocated_y = f64::from(allocated.y);

        let (fitted_width, fitted_height) = match fit {
            ViewportFit::PreserveAspect => {
                let scale =
                    (allocated_width / viewport.width()).min(allocated_height / viewport.height());
                (viewport.width() * scale, viewport.height() * scale)
            }
            ViewportFit::Stretch => (allocated_width, allocated_height),
        };
        let (horizontal, vertical) = alignment.factors();
        let x_min = allocated_x + (allocated_width - fitted_width) * horizontal;
        let y_min = allocated_y + (allocated_height - fitted_height) * vertical;
        let fitted = PixelBounds {
            x_min,
            x_max: x_min + fitted_width,
            y_min,
            y_max: y_min + fitted_height,
        };

        Ok(Self {
            viewport,
            allocated,
            fitted,
            fit,
            alignment,
        })
    }

    pub const fn viewport(self) -> TernaryViewport {
        self.viewport
    }

    pub const fn allocated_pixel_rect(self) -> PixelRect {
        self.allocated
    }

    pub const fn fitted_pixel_bounds(self) -> PixelBounds {
        self.fitted
    }

    pub const fn fit(self) -> ViewportFit {
        self.fit
    }

    pub const fn alignment(self) -> ViewportAlignment {
        self.alignment
    }

    /// Map a finite logical point to floating pixel coordinates.
    pub fn logical_to_pixel(self, logical: TernaryCartesian) -> Result<PixelPoint, Error> {
        if !logical.x.is_finite() || !logical.y.is_finite() {
            return Err(Error::NonFiniteCartesian {
                x: logical.x,
                y: logical.y,
            });
        }
        let x_fraction = (logical.x - self.viewport.x_min) / self.viewport.width();
        let y_fraction = (self.viewport.y_max - logical.y) / self.viewport.height();
        Ok(PixelPoint::new(
            self.fitted.x_min + x_fraction * self.fitted.width(),
            self.fitted.y_min + y_fraction * self.fitted.height(),
        ))
    }

    /// Map a finite pixel point to logical coordinates, including points outside the fit.
    pub fn pixel_to_logical(self, pixel: PixelPoint) -> Result<TernaryCartesian, Error> {
        if !pixel.x.is_finite() || !pixel.y.is_finite() {
            return Err(Error::NonFinitePixelCoordinate {
                x: pixel.x,
                y: pixel.y,
            });
        }
        let x_fraction = (pixel.x - self.fitted.x_min) / self.fitted.width();
        let y_fraction = (pixel.y - self.fitted.y_min) / self.fitted.height();
        Ok(TernaryCartesian::new(
            self.viewport.x_min + x_fraction * self.viewport.width(),
            self.viewport.y_max - y_fraction * self.viewport.height(),
        ))
    }

    /// Map only pixel points inside the fitted bounds.
    pub fn pixel_to_logical_checked(
        self,
        pixel: PixelPoint,
    ) -> Result<Option<TernaryCartesian>, Error> {
        if !pixel.x.is_finite() || !pixel.y.is_finite() {
            return Err(Error::NonFinitePixelCoordinate {
                x: pixel.x,
                y: pixel.y,
            });
        }
        if self.fitted.contains(pixel) {
            self.pixel_to_logical(pixel).map(Some)
        } else {
            Ok(None)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EQUILATERAL_TRIANGLE_HEIGHT, TriangleOrientation, VertexOrder};

    const TOLERANCE: Tolerance = Tolerance {
        absolute: 1.0e-9,
        relative: 1.0e-9,
    };
    const EPSILON: f64 = 1.0e-10;

    fn assert_close(actual: f64, expected: f64) {
        assert!(
            (actual - expected).abs() < EPSILON,
            "{actual} != {expected}"
        );
    }

    fn test_viewport() -> TernaryViewport {
        TernaryViewport::new(0.0, 2.0, 0.0, 1.0).unwrap()
    }

    #[test]
    fn viewport_construction_and_full_bounds_cover_both_orientations() {
        let arbitrary = TernaryViewport::new(-2.0, 3.0, -4.0, 5.0).unwrap();
        assert_eq!(arbitrary.width(), 5.0);
        assert_eq!(arbitrary.height(), 9.0);

        let upward = TernaryViewport::full(TernaryGeometry::default());
        assert_eq!((upward.x_min(), upward.x_max()), (0.0, 1.0));
        assert_eq!(
            (upward.y_min(), upward.y_max()),
            (0.0, EQUILATERAL_TRIANGLE_HEIGHT)
        );

        let downward = TernaryViewport::full(TernaryGeometry::new(
            TriangleOrientation::Down,
            VertexOrder::default(),
        ));
        assert_eq!((downward.x_min(), downward.x_max()), (0.0, 1.0));
        assert_eq!(
            (downward.y_min(), downward.y_max()),
            (-EQUILATERAL_TRIANGLE_HEIGHT, 0.0)
        );

        assert!(TernaryViewport::new(10.0, 11.0, 10.0, 11.0).is_ok());
    }

    #[test]
    fn viewport_validation_rejects_reversed_degenerate_and_non_finite_bounds() {
        for result in [
            TernaryViewport::new(1.0, 0.0, 0.0, 1.0),
            TernaryViewport::new(0.0, 1.0, 1.0, 0.0),
            TernaryViewport::new(0.0, 0.0, 0.0, 1.0),
            TernaryViewport::new(0.0, 1.0, 0.0, 0.0),
            TernaryViewport::new_with_tolerance(0.0, 5.0e-10, 0.0, 1.0, TOLERANCE),
            TernaryViewport::new_with_tolerance(0.0, 1.0, 0.0, 5.0e-10, TOLERANCE),
        ] {
            assert!(matches!(result, Err(Error::InvalidViewport { .. })));
        }

        for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert!(matches!(
                TernaryViewport::new(value, 1.0, 0.0, 1.0),
                Err(Error::NonFiniteViewportBounds { .. })
            ));
        }
    }

    #[test]
    fn viewport_classification_is_independent_and_boundary_aware() {
        let viewport = TernaryViewport::new(0.2, 0.8, 0.1, 0.6).unwrap();
        assert_eq!(
            viewport
                .classify(TernaryCartesian::new(0.5, 0.3), TOLERANCE)
                .unwrap(),
            ViewportPointLocation::Inside
        );
        assert_eq!(
            viewport
                .classify(TernaryCartesian::new(0.2, 0.3), TOLERANCE)
                .unwrap(),
            ViewportPointLocation::Boundary
        );
        assert!(
            viewport
                .contains(TernaryCartesian::new(0.2 - 5.0e-10, 0.3), TOLERANCE)
                .unwrap()
        );
        assert!(
            !viewport
                .contains(TernaryCartesian::new(0.2 - 1.0e-5, 0.3), TOLERANCE)
                .unwrap()
        );
    }

    #[test]
    fn transform_maps_corners_centre_and_inverts_y() {
        let transform = ViewportTransform::new(
            test_viewport(),
            PixelRect::new(10, 20, 200, 100).unwrap(),
            ViewportFit::PreserveAspect,
            ViewportAlignment::Center,
        )
        .unwrap();
        let fitted = transform.fitted_pixel_bounds();
        assert_eq!(
            fitted,
            PixelBounds {
                x_min: 10.0,
                x_max: 210.0,
                y_min: 20.0,
                y_max: 120.0
            }
        );
        assert_eq!(
            transform
                .logical_to_pixel(TernaryCartesian::new(0.0, 1.0))
                .unwrap(),
            PixelPoint::new(10.0, 20.0)
        );
        assert_eq!(
            transform
                .logical_to_pixel(TernaryCartesian::new(2.0, 0.0))
                .unwrap(),
            PixelPoint::new(210.0, 120.0)
        );
        assert_eq!(
            transform
                .logical_to_pixel(TernaryCartesian::new(1.0, 0.5))
                .unwrap(),
            PixelPoint::new(110.0, 70.0)
        );
    }

    #[test]
    fn preserve_aspect_handles_wide_tall_portrait_landscape_and_offsets() {
        let cases = [
            (
                PixelRect::new(0, 0, 300, 100).unwrap(),
                PixelBounds {
                    x_min: 50.0,
                    x_max: 250.0,
                    y_min: 0.0,
                    y_max: 100.0,
                },
            ),
            (
                PixelRect::new(0, 0, 200, 200).unwrap(),
                PixelBounds {
                    x_min: 0.0,
                    x_max: 200.0,
                    y_min: 50.0,
                    y_max: 150.0,
                },
            ),
            (
                PixelRect::new(0, 0, 100, 300).unwrap(),
                PixelBounds {
                    x_min: 0.0,
                    x_max: 100.0,
                    y_min: 125.0,
                    y_max: 175.0,
                },
            ),
            (
                PixelRect::new(7, 11, 400, 100).unwrap(),
                PixelBounds {
                    x_min: 107.0,
                    x_max: 307.0,
                    y_min: 11.0,
                    y_max: 111.0,
                },
            ),
        ];
        for (allocated, expected) in cases {
            let transform = ViewportTransform::new(
                test_viewport(),
                allocated,
                ViewportFit::PreserveAspect,
                ViewportAlignment::Center,
            )
            .unwrap();
            assert_eq!(transform.fitted_pixel_bounds(), expected);
            assert_close(
                transform.fitted_pixel_bounds().width() / transform.fitted_pixel_bounds().height(),
                2.0,
            );
        }
    }

    #[test]
    fn every_alignment_places_unused_horizontal_and_vertical_space() {
        let alignments = [
            (ViewportAlignment::Center, 0.5, 0.5),
            (ViewportAlignment::Top, 0.5, 0.0),
            (ViewportAlignment::Bottom, 0.5, 1.0),
            (ViewportAlignment::Left, 0.0, 0.5),
            (ViewportAlignment::Right, 1.0, 0.5),
            (ViewportAlignment::TopLeft, 0.0, 0.0),
            (ViewportAlignment::TopRight, 1.0, 0.0),
            (ViewportAlignment::BottomLeft, 0.0, 1.0),
            (ViewportAlignment::BottomRight, 1.0, 1.0),
        ];
        for (alignment, horizontal, vertical) in alignments {
            let wide = ViewportTransform::new(
                test_viewport(),
                PixelRect::new(10, 20, 300, 100).unwrap(),
                ViewportFit::PreserveAspect,
                alignment,
            )
            .unwrap();
            assert_close(wide.fitted_pixel_bounds().x_min, 10.0 + 100.0 * horizontal);

            let tall = ViewportTransform::new(
                test_viewport(),
                PixelRect::new(10, 20, 200, 200).unwrap(),
                ViewportFit::PreserveAspect,
                alignment,
            )
            .unwrap();
            assert_close(tall.fitted_pixel_bounds().y_min, 20.0 + 100.0 * vertical);
        }
    }

    #[test]
    fn stretch_fills_allocation_and_round_trips_inside_and_outside_points() {
        let transform = ViewportTransform::new(
            test_viewport(),
            PixelRect::new(-20, 30, 300, 200).unwrap(),
            ViewportFit::Stretch,
            ViewportAlignment::BottomRight,
        )
        .unwrap();
        assert_eq!(
            transform.fitted_pixel_bounds(),
            PixelBounds {
                x_min: -20.0,
                x_max: 280.0,
                y_min: 30.0,
                y_max: 230.0
            }
        );

        for logical in [
            TernaryCartesian::new(0.25, 0.75),
            TernaryCartesian::new(1.0, 0.5),
            TernaryCartesian::new(2.5, -0.5),
        ] {
            let pixel = transform.logical_to_pixel(logical).unwrap();
            let recovered = transform.pixel_to_logical(pixel).unwrap();
            assert_close(recovered.x, logical.x);
            assert_close(recovered.y, logical.y);
        }

        let outside_pixel = PixelPoint::new(-100.0, -100.0);
        assert!(transform.pixel_to_logical(outside_pixel).is_ok());
        assert_eq!(
            transform.pixel_to_logical_checked(outside_pixel).unwrap(),
            None
        );
    }

    #[test]
    fn invalid_pixel_rectangles_and_non_finite_mapping_inputs_are_rejected() {
        assert!(matches!(
            PixelRect::new(0, 0, 0, 10),
            Err(Error::InvalidPixelRect { .. })
        ));
        assert!(matches!(
            ViewportTransform::new(
                test_viewport(),
                PixelRect {
                    x: 0,
                    y: 0,
                    width: 10,
                    height: 0
                },
                ViewportFit::PreserveAspect,
                ViewportAlignment::Center,
            ),
            Err(Error::InvalidPixelRect { .. })
        ));
        let transform = ViewportTransform::new(
            test_viewport(),
            PixelRect::new(0, 0, 100, 100).unwrap(),
            ViewportFit::PreserveAspect,
            ViewportAlignment::Center,
        )
        .unwrap();
        assert!(matches!(
            transform.logical_to_pixel(TernaryCartesian::new(f64::NAN, 0.0)),
            Err(Error::NonFiniteCartesian { .. })
        ));
        assert!(matches!(
            transform.pixel_to_logical(PixelPoint::new(f64::INFINITY, 0.0)),
            Err(Error::NonFinitePixelCoordinate { .. })
        ));
    }
}