Home | History | Annotate | Download | only in pathops
      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 SkPathOpsCubic_DEFINED
      9 #define SkPathOpsCubic_DEFINED
     10 
     11 #include "SkPath.h"
     12 #include "SkPathOpsPoint.h"
     13 
     14 struct SkDCubicPair {
     15     const SkDCubic& first() const { return (const SkDCubic&) pts[0]; }
     16     const SkDCubic& second() const { return (const SkDCubic&) pts[3]; }
     17     SkDPoint pts[7];
     18 };
     19 
     20 struct SkDCubic {
     21     static const int kPointCount = 4;
     22     static const int kPointLast = kPointCount - 1;
     23     static const int kMaxIntersections = 9;
     24 
     25     enum SearchAxis {
     26         kXAxis,
     27         kYAxis
     28     };
     29 
     30     bool collapsed() const {
     31         return fPts[0].approximatelyEqual(fPts[1]) && fPts[0].approximatelyEqual(fPts[2])
     32                 && fPts[0].approximatelyEqual(fPts[3]);
     33     }
     34 
     35     bool controlsInside() const {
     36         SkDVector v01 = fPts[0] - fPts[1];
     37         SkDVector v02 = fPts[0] - fPts[2];
     38         SkDVector v03 = fPts[0] - fPts[3];
     39         SkDVector v13 = fPts[1] - fPts[3];
     40         SkDVector v23 = fPts[2] - fPts[3];
     41         return v03.dot(v01) > 0 && v03.dot(v02) > 0 && v03.dot(v13) > 0 && v03.dot(v23) > 0;
     42     }
     43 
     44     static bool IsConic() { return false; }
     45 
     46     const SkDPoint& operator[](int n) const { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
     47     SkDPoint& operator[](int n) { SkASSERT(n >= 0 && n < kPointCount); return fPts[n]; }
     48 
     49     void align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const;
     50     double binarySearch(double min, double max, double axisIntercept, SearchAxis xAxis) const;
     51     double calcPrecision() const;
     52     SkDCubicPair chopAt(double t) const;
     53     static void Coefficients(const double* cubic, double* A, double* B, double* C, double* D);
     54     static bool ComplexBreak(const SkPoint pts[4], SkScalar* t);
     55     int convexHull(char order[kPointCount]) const;
     56 
     57     void debugInit() {
     58         sk_bzero(fPts, sizeof(fPts));
     59     }
     60 
     61     void dump() const;  // callable from the debugger when the implementation code is linked in
     62     void dumpID(int id) const;
     63     void dumpInner() const;
     64     SkDVector dxdyAtT(double t) const;
     65     bool endsAreExtremaInXOrY() const;
     66     static int FindExtrema(const double src[], double tValue[2]);
     67     int findInflections(double tValues[2]) const;
     68 
     69     static int FindInflections(const SkPoint a[kPointCount], double tValues[2]) {
     70         SkDCubic cubic;
     71         return cubic.set(a).findInflections(tValues);
     72     }
     73 
     74     int findMaxCurvature(double tValues[]) const;
     75     bool hullIntersects(const SkDCubic& c2, bool* isLinear) const;
     76     bool hullIntersects(const SkDConic& c, bool* isLinear) const;
     77     bool hullIntersects(const SkDQuad& c2, bool* isLinear) const;
     78     bool hullIntersects(const SkDPoint* pts, int ptCount, bool* isLinear) const;
     79     bool isLinear(int startIndex, int endIndex) const;
     80     bool monotonicInX() const;
     81     bool monotonicInY() const;
     82     void otherPts(int index, const SkDPoint* o1Pts[kPointCount - 1]) const;
     83     SkDPoint ptAtT(double t) const;
     84     static int RootsReal(double A, double B, double C, double D, double t[3]);
     85     static int RootsValidT(const double A, const double B, const double C, double D, double s[3]);
     86 
     87     int searchRoots(double extremes[6], int extrema, double axisIntercept,
     88                     SearchAxis xAxis, double* validRoots) const;
     89 
     90     /**
     91      *  Return the number of valid roots (0 < root < 1) for this cubic intersecting the
     92      *  specified horizontal line.
     93      */
     94     int horizontalIntersect(double yIntercept, double roots[3]) const;
     95     /**
     96      *  Return the number of valid roots (0 < root < 1) for this cubic intersecting the
     97      *  specified vertical line.
     98      */
     99     int verticalIntersect(double xIntercept, double roots[3]) const;
    100 
    101     const SkDCubic& set(const SkPoint pts[kPointCount]) {
    102         fPts[0] = pts[0];
    103         fPts[1] = pts[1];
    104         fPts[2] = pts[2];
    105         fPts[3] = pts[3];
    106         return *this;
    107     }
    108 
    109     SkDCubic subDivide(double t1, double t2) const;
    110 
    111     static SkDCubic SubDivide(const SkPoint a[kPointCount], double t1, double t2) {
    112         SkDCubic cubic;
    113         return cubic.set(a).subDivide(t1, t2);
    114     }
    115 
    116     void subDivide(const SkDPoint& a, const SkDPoint& d, double t1, double t2, SkDPoint p[2]) const;
    117 
    118     static void SubDivide(const SkPoint pts[kPointCount], const SkDPoint& a, const SkDPoint& d, double t1,
    119                           double t2, SkDPoint p[2]) {
    120         SkDCubic cubic;
    121         cubic.set(pts).subDivide(a, d, t1, t2, p);
    122     }
    123 
    124     double top(const SkDCubic& dCurve, double startT, double endT, SkDPoint*topPt) const;
    125     SkDQuad toQuad() const;
    126 
    127     static const int gPrecisionUnit;
    128 
    129     SkDPoint fPts[kPointCount];
    130 };
    131 
    132 /* Given the set [0, 1, 2, 3], and two of the four members, compute an XOR mask
    133    that computes the other two. Note that:
    134 
    135    one ^ two == 3 for (0, 3), (1, 2)
    136    one ^ two <  3 for (0, 1), (0, 2), (1, 3), (2, 3)
    137    3 - (one ^ two) is either 0, 1, or 2
    138    1 >> (3 - (one ^ two)) is either 0 or 1
    139 thus:
    140    returned == 2 for (0, 3), (1, 2)
    141    returned == 3 for (0, 1), (0, 2), (1, 3), (2, 3)
    142 given that:
    143    (0, 3) ^ 2 -> (2, 1)  (1, 2) ^ 2 -> (3, 0)
    144    (0, 1) ^ 3 -> (3, 2)  (0, 2) ^ 3 -> (3, 1)  (1, 3) ^ 3 -> (2, 0)  (2, 3) ^ 3 -> (1, 0)
    145 */
    146 inline int other_two(int one, int two) {
    147     return 1 >> (3 - (one ^ two)) ^ 3;
    148 }
    149 
    150 #endif
    151