SkPathOpsQuad.h revision ed0935a28a29af7d3b16ac8d9365f291a335c6bd
1/*
2 * Copyright 2012 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 SkPathOpsQuad_DEFINED
9#define SkPathOpsQuad_DEFINED
10
11#include "SkPathOpsPoint.h"
12
13struct SkOpCurve;
14
15struct SkDQuadPair {
16    const SkDQuad& first() const { return (const SkDQuad&) pts[0]; }
17    const SkDQuad& second() const { return (const SkDQuad&) pts[2]; }
18    SkDPoint pts[5];
19};
20
21struct SkDQuad {
22    static const int kPointCount = 3;
23    static const int kPointLast = kPointCount - 1;
24    static const int kMaxIntersections = 4;
25
26    SkDPoint fPts[kPointCount];
27
28    bool collapsed() const {
29        return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2]);
30    }
31
32    bool controlsInside() const {
33        SkDVector v01 = fPts[0] - fPts[1];
34        SkDVector v02 = fPts[0] - fPts[2];
35        SkDVector v12 = fPts[1] - fPts[2];
36        return v02.dot(v01) > 0 && v02.dot(v12) > 0;
37    }
38
39    void debugInit() {
40        sk_bzero(fPts, sizeof(fPts));
41    }
42
43    SkDQuad flip() const {
44        SkDQuad result = {{fPts[2], fPts[1], fPts[0]}};
45        return result;
46    }
47
48    static bool IsConic() { return false; }
49
50    const SkDQuad& set(const SkPoint pts[kPointCount]) {
51        fPts[0] = pts[0];
52        fPts[1] = pts[1];
53        fPts[2] = pts[2];
54        return *this;
55    }
56
57    const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
58    SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
59
60    static int AddValidTs(double s[], int realRoots, double* t);
61    void align(int endIndex, SkDPoint* dstPt) const;
62    SkDQuadPair chopAt(double t) const;
63    SkDVector dxdyAtT(double t) const;
64    static int FindExtrema(const double src[], double tValue[1]);
65    bool hullIntersects(const SkDQuad& , bool* isLinear) const;
66    bool hullIntersects(const SkDConic& , bool* isLinear) const;
67    bool hullIntersects(const SkDCubic& , bool* isLinear) const;
68    bool isLinear(int startIndex, int endIndex) const;
69    bool monotonicInX() const;
70    bool monotonicInY() const;
71    void otherPts(int oddMan, const SkDPoint* endPt[2]) const;
72    SkDPoint ptAtT(double t) const;
73    static int RootsReal(double A, double B, double C, double t[2]);
74    static int RootsValidT(const double A, const double B, const double C, double s[2]);
75    static void SetABC(const double* quad, double* a, double* b, double* c);
76    SkDQuad subDivide(double t1, double t2) const;
77    static SkDQuad SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
78        SkDQuad quad;
79        quad.set(a);
80        return quad.subDivide(t1, t2);
81    }
82    SkDPoint subDivide(const SkDPoint& a, const SkDPoint& c, double t1, double t2) const;
83    static SkDPoint SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& c,
84                              double t1, double t2) {
85        SkDQuad quad;
86        quad.set(pts);
87        return quad.subDivide(a, c, t1, t2);
88    }
89
90    SkDCubic debugToCubic() const;
91    // utilities callable by the user from the debugger when the implementation code is linked in
92    void dump() const;
93    void dumpID(int id) const;
94    void dumpInner() const;
95
96private:
97//  static double Tangent(const double* quadratic, double t);  // uncalled
98};
99
100#endif
101