aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pathops/SkPathOpsBounds.h
blob: 7b9daa3671d4c0fc36efe54134bf780beeaf3b5a (plain)
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
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkPathOpBounds_DEFINED
#define SkPathOpBounds_DEFINED

#include "SkPathOpsRect.h"
#include "SkRect.h"

// SkPathOpsBounds, unlike SkRect, does not consider a line to be empty.
struct SkPathOpsBounds : public SkRect {
    static bool Intersects(const SkPathOpsBounds& a, const SkPathOpsBounds& b) {
        return AlmostLessOrEqualUlps(a.fLeft, b.fRight)
                && AlmostLessOrEqualUlps(b.fLeft, a.fRight)
                && AlmostLessOrEqualUlps(a.fTop, b.fBottom)
                && AlmostLessOrEqualUlps(b.fTop, a.fBottom);
    }

   // Note that add(), unlike SkRect::join() or SkRect::growToInclude()
   // does not treat the bounds of horizontal and vertical lines as
   // empty rectangles.
    void add(SkScalar left, SkScalar top, SkScalar right, SkScalar bottom) {
        if (left < fLeft) fLeft = left;
        if (top < fTop) fTop = top;
        if (right > fRight) fRight = right;
        if (bottom > fBottom) fBottom = bottom;
    }

    void add(const SkPathOpsBounds& toAdd) {
        add(toAdd.fLeft, toAdd.fTop, toAdd.fRight, toAdd.fBottom);
    }

    void add(const SkDPoint& pt) {
        if (pt.fX < fLeft) fLeft = SkDoubleToScalar(pt.fX);
        if (pt.fY < fTop) fTop = SkDoubleToScalar(pt.fY);
        if (pt.fX > fRight) fRight = SkDoubleToScalar(pt.fX);
        if (pt.fY > fBottom) fBottom = SkDoubleToScalar(pt.fY);
    }

    bool almostContains(const SkPoint& pt) {
        return AlmostLessOrEqualUlps(fLeft, pt.fX)
                && AlmostLessOrEqualUlps(pt.fX, fRight)
                && AlmostLessOrEqualUlps(fTop, pt.fY)
                && AlmostLessOrEqualUlps(pt.fY, fBottom);
    }

    // unlike isEmpty(), this permits lines, but not points
    // FIXME: unused for now
    bool isReallyEmpty() const {
        // use !<= instead of > to detect NaN values
        return !(fLeft <= fRight) || !(fTop <= fBottom)
                || (fLeft == fRight && fTop == fBottom);
    }

    void setPointBounds(const SkDPoint& pt) {
        fLeft = fRight = SkDoubleToScalar(pt.fX);
        fTop = fBottom = SkDoubleToScalar(pt.fY);
    }

    typedef SkRect INHERITED;
};

#endif