aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pathops/SkPathOpsCurve.cpp
blob: 651e64a908e66c260e31df7338bddbe3602792f2 (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
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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkPathOpsBounds.h"
#include "SkPathOpsRect.h"
#include "SkPathOpsCurve.h"

SkDPoint SkDCurve::conicTop(const SkPoint curve[3], SkScalar curveWeight, 
        double startT, double endT, double* topT) {
    SkDPoint topPt = fConic[0];
    *topT = startT;
    if (topPt.fY > fConic[2].fY || (topPt.fY == fConic[2].fY && topPt.fX > fConic[2].fX)) {
        *topT = endT;
        topPt = fConic[2];
    }
    if (!fConic.monotonicInY()) {
        double extremeT;
        if (SkDConic::FindExtrema(&fConic.fPts.fPts[0].fY, fConic.fWeight, &extremeT)) {
            SkDConic dCurve;
            dCurve.set(curve, curveWeight);
            extremeT = startT + (endT - startT) * extremeT;
            SkDPoint test = dCurve.ptAtT(extremeT);
            if (topPt.fY > test.fY || (topPt.fY == test.fY && topPt.fX > test.fX)) {
                *topT = extremeT;
                topPt = test;
            }
        }
    }
    return topPt;
}

SkDPoint SkDCurve::cubicTop(const SkPoint curve[4], SkScalar ,
        double startT, double endT, double* topT) {
    SkDPoint topPt = fCubic[0];
    *topT = startT;
    if (topPt.fY > fCubic[3].fY || (topPt.fY == fCubic[3].fY && topPt.fX > fCubic[3].fX)) {
        *topT = endT;
        topPt = fCubic[3];
    }
    double extremeTs[2];
    if (!fCubic.monotonicInY()) {
        int roots = SkDCubic::FindExtrema(&fCubic.fPts[0].fY, extremeTs);
        SkDCubic dCurve;
        dCurve.set(curve);
        for (int index = 0; index < roots; ++index) {
            double t = startT + (endT - startT) * extremeTs[index];
            SkDPoint mid = dCurve.ptAtT(t);
            if (topPt.fY > mid.fY || (topPt.fY == mid.fY && topPt.fX > mid.fX)) {
                *topT = t;
                topPt = mid;
            }
        }
    }
    return topPt;
}

SkDPoint SkDCurve::lineTop(const SkPoint[2], SkScalar , double startT, double endT, double* topT) {
    SkDPoint topPt = fLine[0];
    *topT = startT;
    if (topPt.fY > fLine[1].fY || (topPt.fY == fLine[1].fY && topPt.fX > fLine[1].fX)) {
        *topT = endT;
        topPt = fLine[1];
    }
    return topPt;
}

SkDPoint SkDCurve::quadTop(const SkPoint curve[3], SkScalar ,
        double startT, double endT, double* topT) {
    SkDPoint topPt = fQuad[0];
    *topT = startT;
    if (topPt.fY > fQuad[2].fY || (topPt.fY == fQuad[2].fY && topPt.fX > fQuad[2].fX)) {
        *topT = endT;
        topPt = fQuad[2];
    }
    if (!fQuad.monotonicInY()) {
        double extremeT;
        if (SkDQuad::FindExtrema(&fQuad.fPts[0].fY, &extremeT)) {
            SkDQuad dCurve;
            dCurve.set(curve);
            extremeT = startT + (endT - startT) * extremeT;
            SkDPoint test = dCurve.ptAtT(extremeT);
            if (topPt.fY > test.fY || (topPt.fY == test.fY && topPt.fX > test.fX)) {
                *topT = extremeT;
                topPt = test;
            }
        }
    }
    return topPt;
}

SkDPoint (SkDCurve::* const Top[])(const SkPoint curve[], SkScalar curveWeight,
        double tStart, double tEnd, double* topT) = {
    NULL,
    &SkDCurve::lineTop,
    &SkDCurve::quadTop,
    &SkDCurve::conicTop,
    &SkDCurve::cubicTop
};

void SkDCurve::setConicBounds(const SkPoint curve[3], SkScalar curveWeight,
        double tStart, double tEnd, SkPathOpsBounds* bounds) {
    SkDConic dCurve;
    dCurve.set(curve, curveWeight);
    SkDRect dRect;
    dRect.setBounds(dCurve, fConic, tStart, tEnd);
    bounds->set(SkDoubleToScalar(dRect.fLeft), SkDoubleToScalar(dRect.fTop),
            SkDoubleToScalar(dRect.fRight), SkDoubleToScalar(dRect.fBottom));
}

void SkDCurve::setCubicBounds(const SkPoint curve[4], SkScalar , 
        double tStart, double tEnd, SkPathOpsBounds* bounds) {
    SkDCubic dCurve;
    dCurve.set(curve);
    SkDRect dRect;
    dRect.setBounds(dCurve, fCubic, tStart, tEnd);
    bounds->set(SkDoubleToScalar(dRect.fLeft), SkDoubleToScalar(dRect.fTop),
            SkDoubleToScalar(dRect.fRight), SkDoubleToScalar(dRect.fBottom));
}

void SkDCurve::setLineBounds(const SkPoint[2], SkScalar , 
        double , double , SkPathOpsBounds* bounds) {
    bounds->setPointBounds(fLine[0]);
    bounds->add(fLine[1]);
}

void SkDCurve::setQuadBounds(const SkPoint curve[3], SkScalar ,
        double tStart, double tEnd, SkPathOpsBounds* bounds) {
    SkDQuad dCurve;
    dCurve.set(curve);
    SkDRect dRect;
    dRect.setBounds(dCurve, fQuad, tStart, tEnd);
    bounds->set(SkDoubleToScalar(dRect.fLeft), SkDoubleToScalar(dRect.fTop),
            SkDoubleToScalar(dRect.fRight), SkDoubleToScalar(dRect.fBottom));
}

void (SkDCurve::* const SetBounds[])(const SkPoint curve[], SkScalar curveWeight,
        double tStart, double tEnd, SkPathOpsBounds* bounds) = {
    NULL,
    &SkDCurve::setLineBounds,
    &SkDCurve::setQuadBounds,
    &SkDCurve::setConicBounds,
    &SkDCurve::setCubicBounds
};