blob: 7630618a3823cf117064ad66a125e373bd25047d [file] [log] [blame]
Chris Dalton8610e9c2019-05-09 11:07:10 -06001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrOctoBounds_DEFINED
9#define GrOctoBounds_DEFINED
10
11#include "include/core/SkRect.h"
12#include <functional>
13
14/**
15 * This class is composed of two bounding boxes: one in device space, and one in a 45-degree rotated
16 * space.
17 *
18 * The 45-degree bounding box resides in "| 1 -1 | * coords" space.
19 * | 1 1 |
20 *
21 * The intersection of these two boxes defines the bounding octagon of a shape.
22 *
23 * Furthermore, both bounding boxes are fully tightened. This means we can blindly find the
24 * intersections between each diagonal and its vertical and horizontal neighbors, and be left with
25 * 8 points that define a convex (possibly degenerate) octagon.
26 */
27class GrOctoBounds {
28public:
29 void set(const SkRect& bounds, const SkRect& bounds45) {
30 fBounds = bounds;
31 fBounds45 = bounds45;
32 SkDEBUGCODE(this->validateBoundsAreTight());
33 }
34
35 const SkRect& bounds() const { return fBounds; }
36 float left() const { return fBounds.left(); }
37 float top() const { return fBounds.top(); }
38 float right() const { return fBounds.right(); }
39 float bottom() const { return fBounds.bottom(); }
40
41
42 // The 45-degree bounding box resides in "| 1 -1 | * coords" space.
43 // | 1 1 |
44 const SkRect& bounds45() const { return fBounds45; }
45 float left45() const { return fBounds45.left(); }
46 float top45() const { return fBounds45.top(); }
47 float right45() const { return fBounds45.right(); }
48 float bottom45() const { return fBounds45.bottom(); }
49
50 void roundOut(SkIRect* out) const {
51 // The octagon is the intersection of fBounds and fBounds45 (see the comment at the start of
52 // the class). The octagon's bounding box is therefore just fBounds. And the integer
53 // bounding box can be found by simply rounding out fBounds.
54 fBounds.roundOut(out);
55 }
56
57 GrOctoBounds makeOffset(float dx, float dy) const {
58 GrOctoBounds offset;
59 offset.setOffset(*this, dx, dy);
60 return offset;
61 }
62
63 void setOffset(const GrOctoBounds& octoBounds, float dx, float dy) {
64 fBounds = octoBounds.fBounds.makeOffset(dx, dy);
65 fBounds45 = octoBounds.fBounds45.makeOffset(dx - dy, dx + dy);
66 SkDEBUGCODE(this->validateBoundsAreTight());
67 }
68
69 void outset(float radius) {
70 fBounds.outset(radius, radius);
71 fBounds45.outset(radius*SK_ScalarSqrt2, radius*SK_ScalarSqrt2);
72 SkDEBUGCODE(this->validateBoundsAreTight());
73 }
74
75 // The 45-degree bounding box resides in "| 1 -1 | * coords" space.
76 // | 1 1 |
77 //
78 // i.e., | x45 | = | x - y |
79 // | y45 | = | x + y |
80 //
81 // These methods transform points between device space and 45-degree space.
82 constexpr static float Get_x45(float x, float y) { return x - y; }
83 constexpr static float Get_y45(float x, float y) { return x + y; }
84 constexpr static float Get_x(float x45, float y45) { return (x45 + y45) * .5f; }
85 constexpr static float Get_y(float x45, float y45) { return (y45 - x45) * .5f; }
86
87#ifdef SK_DEBUG
88 void validateBoundsAreTight() const;
89 void validateBoundsAreTight(const std::function<void(
90 bool cond, const char* file, int line, const char* code)>& validateFn) const;
91#endif
92
93private:
94 SkRect fBounds;
95 SkRect fBounds45;
96};
97
98#endif