Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1667)

Side by Side Diff: src/core/SkPath.cpp

Issue 1517883002: fix SkPath::contains() for points on edge, conics (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: add comment Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkCubicClipper.cpp ('k') | tests/PathTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkBuffer.h" 8 #include "SkBuffer.h"
9 #include "SkCubicClipper.h"
9 #include "SkErrorInternals.h" 10 #include "SkErrorInternals.h"
10 #include "SkGeometry.h" 11 #include "SkGeometry.h"
11 #include "SkMath.h" 12 #include "SkMath.h"
12 #include "SkPathPriv.h" 13 #include "SkPathPriv.h"
13 #include "SkPathRef.h" 14 #include "SkPathRef.h"
14 #include "SkRRect.h" 15 #include "SkRRect.h"
15 16
16 //////////////////////////////////////////////////////////////////////////// 17 ////////////////////////////////////////////////////////////////////////////
17 18
18 /** 19 /**
(...skipping 2562 matching lines...) Expand 10 before | Expand all | Expand 10 after
2581 crossToDir(ymaxCross, dir); 2582 crossToDir(ymaxCross, dir);
2582 path.fFirstDirection = *dir; 2583 path.fFirstDirection = *dir;
2583 return true; 2584 return true;
2584 } else { 2585 } else {
2585 return false; 2586 return false;
2586 } 2587 }
2587 } 2588 }
2588 2589
2589 /////////////////////////////////////////////////////////////////////////////// 2590 ///////////////////////////////////////////////////////////////////////////////
2590 2591
2592 static bool between(SkScalar a, SkScalar b, SkScalar c) {
2593 SkASSERT(((a <= b && b <= c) || (a >= b && b >= c)) == ((a - b) * (c - b) <= 0)
2594 || (SkScalarNearlyZero(a) && SkScalarNearlyZero(b) && SkScalarNearly Zero(c)));
2595 return (a - b) * (c - b) <= 0;
2596 }
2597
2591 static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C, 2598 static SkScalar eval_cubic_coeff(SkScalar A, SkScalar B, SkScalar C,
2592 SkScalar D, SkScalar t) { 2599 SkScalar D, SkScalar t) {
2593 return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D); 2600 return SkScalarMulAdd(SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C), t, D);
2594 } 2601 }
2595 2602
2596 static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c 3, 2603 static SkScalar eval_cubic_pts(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c 3,
2597 SkScalar t) { 2604 SkScalar t) {
2598 SkScalar A = c3 + 3*(c1 - c2) - c0; 2605 SkScalar A = c3 + 3*(c1 - c2) - c0;
2599 SkScalar B = 3*(c2 - c1 - c1 + c0); 2606 SkScalar B = 3*(c2 - c1 - c1 + c0);
2600 SkScalar C = 3*(c1 - c0); 2607 SkScalar C = 3*(c1 - c0);
2601 SkScalar D = c0; 2608 SkScalar D = c0;
2602 return eval_cubic_coeff(A, B, C, D, t); 2609 return eval_cubic_coeff(A, B, C, D, t);
2603 } 2610 }
2604 2611
2605 /* Given 4 cubic points (either Xs or Ys), and a target X or Y, compute the
2606 t value such that cubic(t) = target
2607 */
2608 static void chopMonoCubicAt(SkScalar c0, SkScalar c1, SkScalar c2, SkScalar c3,
2609 SkScalar target, SkScalar* t) {
2610 // SkASSERT(c0 <= c1 && c1 <= c2 && c2 <= c3);
2611 SkASSERT(c0 < target && target < c3);
2612
2613 SkScalar D = c0 - target;
2614 SkScalar A = c3 + 3*(c1 - c2) - c0;
2615 SkScalar B = 3*(c2 - c1 - c1 + c0);
2616 SkScalar C = 3*(c1 - c0);
2617
2618 const SkScalar TOLERANCE = SK_Scalar1 / 4096;
2619 SkScalar minT = 0;
2620 SkScalar maxT = SK_Scalar1;
2621 SkScalar mid;
2622 int i;
2623 for (i = 0; i < 16; i++) {
2624 mid = SkScalarAve(minT, maxT);
2625 SkScalar delta = eval_cubic_coeff(A, B, C, D, mid);
2626 if (delta < 0) {
2627 minT = mid;
2628 delta = -delta;
2629 } else {
2630 maxT = mid;
2631 }
2632 if (delta < TOLERANCE) {
2633 break;
2634 }
2635 }
2636 *t = mid;
2637 }
2638
2639 template <size_t N> static void find_minmax(const SkPoint pts[], 2612 template <size_t N> static void find_minmax(const SkPoint pts[],
2640 SkScalar* minPtr, SkScalar* maxPtr) { 2613 SkScalar* minPtr, SkScalar* maxPtr) {
2641 SkScalar min, max; 2614 SkScalar min, max;
2642 min = max = pts[0].fX; 2615 min = max = pts[0].fX;
2643 for (size_t i = 1; i < N; ++i) { 2616 for (size_t i = 1; i < N; ++i) {
2644 min = SkMinScalar(min, pts[i].fX); 2617 min = SkMinScalar(min, pts[i].fX);
2645 max = SkMaxScalar(max, pts[i].fX); 2618 max = SkMaxScalar(max, pts[i].fX);
2646 } 2619 }
2647 *minPtr = min; 2620 *minPtr = min;
2648 *maxPtr = max; 2621 *maxPtr = max;
2649 } 2622 }
2650 2623
2651 static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { 2624 static int winding_mono_cubic(const SkPoint pts[], SkScalar x, SkScalar y, int* onCurveCount) {
2652 SkPoint storage[4]; 2625 if (!between(pts[0].fY, y, pts[3].fY)) {
2653
2654 int dir = 1;
2655 if (pts[0].fY > pts[3].fY) {
2656 storage[0] = pts[3];
2657 storage[1] = pts[2];
2658 storage[2] = pts[1];
2659 storage[3] = pts[0];
2660 pts = storage;
2661 dir = -1;
2662 }
2663 if (y < pts[0].fY || y >= pts[3].fY) {
2664 return 0; 2626 return 0;
2665 } 2627 }
2666 2628 if (y == pts[3].fY) {
2629 // if the cubic is a horizontal line, check if the point is on it
2630 // but don't check the last point, because that point is shared with the next curve
2631 if (pts[0].fY == pts[3].fY && between(pts[0].fX, x, pts[3].fX) && x != p ts[3].fX) {
2632 *onCurveCount += 1;
2633 }
2634 return 0;
2635 }
2636 int dir = pts[0].fY > pts[3].fY ? -1 : 1;
2667 // quickreject or quickaccept 2637 // quickreject or quickaccept
2668 SkScalar min, max; 2638 SkScalar min, max;
2669 find_minmax<4>(pts, &min, &max); 2639 find_minmax<4>(pts, &min, &max);
2670 if (x < min) { 2640 if (x < min) {
2671 return 0; 2641 return 0;
2672 } 2642 }
2673 if (x > max) { 2643 if (x > max) {
2674 return dir; 2644 return dir;
2675 } 2645 }
2676 2646
2677 // compute the actual x(t) value 2647 // compute the actual x(t) value
2678 SkScalar t; 2648 SkScalar t;
2679 chopMonoCubicAt(pts[0].fY, pts[1].fY, pts[2].fY, pts[3].fY, y, &t); 2649 SkAssertResult(SkCubicClipper::ChopMonoAtY(pts, y, &t));
2680 SkScalar xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t); 2650 SkScalar xt = eval_cubic_pts(pts[0].fX, pts[1].fX, pts[2].fX, pts[3].fX, t);
2651 if (SkScalarNearlyEqual(xt, x)) {
2652 if (x != pts[3].fX || y != pts[3].fY) { // don't test end points; they' re start points
2653 *onCurveCount += 1;
2654 }
2655 }
2681 return xt < x ? dir : 0; 2656 return xt < x ? dir : 0;
2682 } 2657 }
2683 2658
2684 static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y) { 2659 static int winding_cubic(const SkPoint pts[], SkScalar x, SkScalar y, int* onCur veCount) {
2685 SkPoint dst[10]; 2660 SkPoint dst[10];
2686 int n = SkChopCubicAtYExtrema(pts, dst); 2661 int n = SkChopCubicAtYExtrema(pts, dst);
2687 int w = 0; 2662 int w = 0;
2688 for (int i = 0; i <= n; ++i) { 2663 for (int i = 0; i <= n; ++i) {
2689 w += winding_mono_cubic(&dst[i * 3], x, y); 2664 w += winding_mono_cubic(&dst[i * 3], x, y, onCurveCount);
2690 } 2665 }
2691 return w; 2666 return w;
2692 } 2667 }
2693 2668
2694 static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y) { 2669 static double conic_eval_numerator(const SkScalar src[], SkScalar w, SkScalar t) {
2670 SkASSERT(src);
2671 SkASSERT(t >= 0 && t <= 1);
2672 SkScalar src2w = src[2] * w;
2673 SkScalar C = src[0];
2674 SkScalar A = src[4] - 2 * src2w + C;
2675 SkScalar B = 2 * (src2w - C);
2676 return (A * t + B) * t + C;
2677 }
2678
2679
2680 static double conic_eval_denominator(SkScalar w, SkScalar t) {
2681 SkScalar B = 2 * (w - 1);
2682 SkScalar C = 1;
2683 SkScalar A = -B;
2684 return (A * t + B) * t + C;
2685 }
2686
2687 static int winding_mono_conic(const SkConic& conic, SkScalar x, SkScalar y, int* onCurveCount) {
2688 const SkPoint* pts = conic.fPts;
2695 SkScalar y0 = pts[0].fY; 2689 SkScalar y0 = pts[0].fY;
2696 SkScalar y2 = pts[2].fY; 2690 SkScalar y2 = pts[2].fY;
2697 2691
2698 int dir = 1; 2692 int dir = 1;
2699 if (y0 > y2) { 2693 if (y0 > y2) {
2700 SkTSwap(y0, y2); 2694 SkTSwap(y0, y2);
2701 dir = -1; 2695 dir = -1;
2702 } 2696 }
2703 if (y < y0 || y >= y2) { 2697 if (y < y0 || y > y2) {
2698 return 0;
2699 }
2700 if (y == y2) {
2701 if (y0 == y2 && between(pts[0].fX, x, pts[2].fX) && x != pts[2].fX) { / / check horizontal
2702 *onCurveCount += 1;
2703 }
2704 return 0; 2704 return 0;
2705 } 2705 }
2706 2706
2707 SkScalar roots[2];
2708 SkScalar A = pts[2].fY;
2709 SkScalar B = pts[1].fY * conic.fW - y * conic.fW + y;
2710 SkScalar C = pts[0].fY;
2711 A += C - 2 * B; // A = a + c - 2*(b*w - yCept*w + yCept)
2712 B -= C; // B = b*w - w * yCept + yCept - a
2713 C -= y;
2714 int n = SkFindUnitQuadRoots(A, 2 * B, C, roots);
2715 SkASSERT(n <= 1);
2716 SkScalar xt;
2717 if (0 == n) {
2718 SkScalar mid = SkScalarAve(y0, y2);
2719 // Need [0] and [2] if dir == 1
2720 // and [2] and [0] if dir == -1
2721 xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX;
2722 } else {
2723 SkScalar t = roots[0];
2724 xt = conic_eval_numerator(&pts[0].fX, conic.fW, t) / conic_eval_denomina tor(conic.fW, t);
2725 }
2726 if (SkScalarNearlyEqual(xt, x)) {
2727 if (x != pts[2].fX || y != pts[2].fY) { // don't test end points; they' re start points
2728 *onCurveCount += 1;
2729 }
2730 }
2731 return xt < x ? dir : 0;
2732 }
2733
2734 static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) {
2735 // return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0;
2736 if (y0 == y1) {
2737 return true;
2738 }
2739 if (y0 < y1) {
2740 return y1 <= y2;
2741 } else {
2742 return y1 >= y2;
2743 }
2744 }
2745
2746 static int winding_conic(const SkPoint pts[], SkScalar x, SkScalar y, SkScalar w eight,
2747 int* onCurveCount) {
2748 SkConic conic(pts, weight);
2749 SkConic *c = &conic;
2750 SkConic chopped[2];
2751 int n = 0;
2752
2753 if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) {
2754 n = conic.chopAtYExtrema(chopped);
2755 c = chopped;
2756 }
2757 int w = winding_mono_conic(*c, x, y, onCurveCount);
2758 if (n > 0) {
2759 w += winding_mono_conic(chopped[1], x, y, onCurveCount);
2760 }
2761 return w;
2762 }
2763
2764 static int winding_mono_quad(const SkPoint pts[], SkScalar x, SkScalar y, int* o nCurveCount) {
2765 SkScalar y0 = pts[0].fY;
2766 SkScalar y2 = pts[2].fY;
2767
2768 int dir = 1;
2769 if (y0 > y2) {
2770 SkTSwap(y0, y2);
2771 dir = -1;
2772 }
2773 if (y < y0 || y > y2) {
2774 return 0;
2775 }
2776 if (y == y2) {
2777 if (y0 == y2 && between(pts[0].fX, x, pts[2].fX) && x != pts[2].fX) { / / check horizontal
2778 *onCurveCount += 1;
2779 }
2780 return 0;
2781 }
2707 // bounds check on X (not required. is it faster?) 2782 // bounds check on X (not required. is it faster?)
2708 #if 0 2783 #if 0
2709 if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) { 2784 if (pts[0].fX > x && pts[1].fX > x && pts[2].fX > x) {
2710 return 0; 2785 return 0;
2711 } 2786 }
2712 #endif 2787 #endif
2713 2788
2714 SkScalar roots[2]; 2789 SkScalar roots[2];
2715 int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY, 2790 int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY,
2716 2 * (pts[1].fY - pts[0].fY), 2791 2 * (pts[1].fY - pts[0].fY),
2717 pts[0].fY - y, 2792 pts[0].fY - y,
2718 roots); 2793 roots);
2719 SkASSERT(n <= 1); 2794 SkASSERT(n <= 1);
2720 SkScalar xt; 2795 SkScalar xt;
2721 if (0 == n) { 2796 if (0 == n) {
2722 SkScalar mid = SkScalarAve(y0, y2); 2797 SkScalar mid = SkScalarAve(y0, y2);
2723 // Need [0] and [2] if dir == 1 2798 // Need [0] and [2] if dir == 1
2724 // and [2] and [0] if dir == -1 2799 // and [2] and [0] if dir == -1
2725 xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX; 2800 xt = y < mid ? pts[1 - dir].fX : pts[dir - 1].fX;
2726 } else { 2801 } else {
2727 SkScalar t = roots[0]; 2802 SkScalar t = roots[0];
2728 SkScalar C = pts[0].fX; 2803 SkScalar C = pts[0].fX;
2729 SkScalar A = pts[2].fX - 2 * pts[1].fX + C; 2804 SkScalar A = pts[2].fX - 2 * pts[1].fX + C;
2730 SkScalar B = 2 * (pts[1].fX - C); 2805 SkScalar B = 2 * (pts[1].fX - C);
2731 xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C); 2806 xt = SkScalarMulAdd(SkScalarMulAdd(A, t, B), t, C);
2732 } 2807 }
2808 if (SkScalarNearlyEqual(xt, x)) {
2809 if (x != pts[2].fX || y != pts[2].fY) { // don't test end points; they' re start points
2810 *onCurveCount += 1;
2811 }
2812 }
2733 return xt < x ? dir : 0; 2813 return xt < x ? dir : 0;
2734 } 2814 }
2735 2815
2736 static bool is_mono_quad(SkScalar y0, SkScalar y1, SkScalar y2) { 2816 static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y, int* onCurv eCount) {
2737 // return SkScalarSignAsInt(y0 - y1) + SkScalarSignAsInt(y1 - y2) != 0;
2738 if (y0 == y1) {
2739 return true;
2740 }
2741 if (y0 < y1) {
2742 return y1 <= y2;
2743 } else {
2744 return y1 >= y2;
2745 }
2746 }
2747
2748 static int winding_quad(const SkPoint pts[], SkScalar x, SkScalar y) {
2749 SkPoint dst[5]; 2817 SkPoint dst[5];
2750 int n = 0; 2818 int n = 0;
2751 2819
2752 if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) { 2820 if (!is_mono_quad(pts[0].fY, pts[1].fY, pts[2].fY)) {
2753 n = SkChopQuadAtYExtrema(pts, dst); 2821 n = SkChopQuadAtYExtrema(pts, dst);
2754 pts = dst; 2822 pts = dst;
2755 } 2823 }
2756 int w = winding_mono_quad(pts, x, y); 2824 int w = winding_mono_quad(pts, x, y, onCurveCount);
2757 if (n > 0) { 2825 if (n > 0) {
2758 w += winding_mono_quad(&pts[2], x, y); 2826 w += winding_mono_quad(&pts[2], x, y, onCurveCount);
2759 } 2827 }
2760 return w; 2828 return w;
2761 } 2829 }
2762 2830
2763 static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y) { 2831 static int winding_line(const SkPoint pts[], SkScalar x, SkScalar y, int* onCurv eCount) {
2764 SkScalar x0 = pts[0].fX; 2832 SkScalar x0 = pts[0].fX;
2765 SkScalar y0 = pts[0].fY; 2833 SkScalar y0 = pts[0].fY;
2766 SkScalar x1 = pts[1].fX; 2834 SkScalar x1 = pts[1].fX;
2767 SkScalar y1 = pts[1].fY; 2835 SkScalar y1 = pts[1].fY;
2768 2836
2769 SkScalar dy = y1 - y0; 2837 SkScalar dy = y1 - y0;
2770 2838
2771 int dir = 1; 2839 int dir = 1;
2772 if (y0 > y1) { 2840 if (y0 > y1) {
2773 SkTSwap(y0, y1); 2841 SkTSwap(y0, y1);
2774 dir = -1; 2842 dir = -1;
2775 } 2843 }
2776 if (y < y0 || y >= y1) { 2844 if (y < y0 || y > y1) {
2777 return 0; 2845 return 0;
2778 } 2846 }
2847 if (y == y1) {
2848 if (y0 == y1 && between(x0, x, x1) && x != x1) { // check if on horizon tal line
2849 *onCurveCount += 1;
2850 }
2851 return 0;
2852 }
2853 SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) - SkScalarMul(dy, x - x 0);
2779 2854
2780 SkScalar cross = SkScalarMul(x1 - x0, y - pts[0].fY) - 2855 if (!cross) {
2781 SkScalarMul(dy, x - pts[0].fX); 2856 if (x != x1 || y != pts[1].fY) { // don't test end points since they're also start points
2782 2857 *onCurveCount += 1; // zero cross means the point is on the line
2783 if (SkScalarSignAsInt(cross) == dir) { 2858 }
2859 dir = 0;
2860 } else if (SkScalarSignAsInt(cross) == dir) {
2784 dir = 0; 2861 dir = 0;
2785 } 2862 }
2786 return dir; 2863 return dir;
2787 } 2864 }
2788 2865
2866 static void tangent_cubic(const SkPoint pts[], SkScalar x, SkScalar y,
2867 SkTDArray<SkVector>* tangents) {
2868 if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY)
2869 && !between(pts[2].fY, y, pts[3].fY)) {
2870 return;
2871 }
2872 if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX)
2873 && !between(pts[2].fX, x, pts[3].fX)) {
2874 return;
2875 }
2876 SkPoint dst[10];
2877 int n = SkChopCubicAtYExtrema(pts, dst);
2878 for (int i = 0; i <= n; ++i) {
2879 SkPoint* c = &dst[i * 3];
2880 SkScalar t;
2881 SkAssertResult(SkCubicClipper::ChopMonoAtY(c, y, &t));
2882 SkScalar xt = eval_cubic_pts(c[0].fX, c[1].fX, c[2].fX, c[3].fX, t);
2883 if (!SkScalarNearlyEqual(x, xt)) {
2884 continue;
2885 }
2886 SkVector tangent;
2887 SkEvalCubicAt(c, t, nullptr, &tangent, nullptr);
2888 tangents->push(tangent);
2889 }
2890 }
2891
2892 static void tangent_conic(const SkPoint pts[], SkScalar x, SkScalar y, SkScalar w,
2893 SkTDArray<SkVector>* tangents) {
2894 if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY)) {
2895 return;
2896 }
2897 if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX)) {
2898 return;
2899 }
2900 SkScalar roots[2];
2901 SkScalar A = pts[2].fY;
2902 SkScalar B = pts[1].fY * w - y * w + y;
2903 SkScalar C = pts[0].fY;
2904 A += C - 2 * B; // A = a + c - 2*(b*w - yCept*w + yCept)
2905 B -= C; // B = b*w - w * yCept + yCept - a
2906 C -= y;
2907 int n = SkFindUnitQuadRoots(A, 2 * B, C, roots);
2908 for (int index = 0; index < n; ++index) {
2909 SkScalar t = roots[index];
2910 SkScalar xt = conic_eval_numerator(&pts[0].fX, w, t) / conic_eval_denomi nator(w, t);
2911 if (!SkScalarNearlyEqual(x, xt)) {
2912 continue;
2913 }
2914 SkConic conic(pts, w);
2915 tangents->push(conic.evalTangentAt(t));
2916 }
2917 }
2918
2919 static void tangent_quad(const SkPoint pts[], SkScalar x, SkScalar y,
2920 SkTDArray<SkVector>* tangents) {
2921 if (!between(pts[0].fY, y, pts[1].fY) && !between(pts[1].fY, y, pts[2].fY)) {
2922 return;
2923 }
2924 if (!between(pts[0].fX, x, pts[1].fX) && !between(pts[1].fX, x, pts[2].fX)) {
2925 return;
2926 }
2927 SkScalar roots[2];
2928 int n = SkFindUnitQuadRoots(pts[0].fY - 2 * pts[1].fY + pts[2].fY,
2929 2 * (pts[1].fY - pts[0].fY),
2930 pts[0].fY - y,
2931 roots);
2932 for (int index = 0; index < n; ++index) {
2933 SkScalar t = roots[index];
2934 SkScalar C = pts[0].fX;
2935 SkScalar A = pts[2].fX - 2 * pts[1].fX + C;
2936 SkScalar B = 2 * (pts[1].fX - C);
2937 SkScalar xt = (A * t + B) * t + C;
2938 if (!SkScalarNearlyEqual(x, xt)) {
2939 continue;
2940 }
2941 tangents->push(SkEvalQuadTangentAt(pts, t));
2942 }
2943 }
2944
2945 static void tangent_line(const SkPoint pts[], SkScalar x, SkScalar y,
2946 SkTDArray<SkVector>* tangents) {
2947 SkScalar y0 = pts[0].fY;
2948 SkScalar y1 = pts[1].fY;
2949 if (!between(y0, y, y1)) {
2950 return;
2951 }
2952 SkScalar x0 = pts[0].fX;
2953 SkScalar x1 = pts[1].fX;
2954 if (!between(x0, x, x1)) {
2955 return;
2956 }
2957 SkScalar dx = x1 - x0;
2958 SkScalar dy = y1 - y0;
2959 if (!SkScalarNearlyEqual((x - x0) * dy, dx * (y - y0))) {
2960 return;
2961 }
2962 SkVector v;
2963 v.set(dx, dy);
2964 tangents->push(v);
2965 }
2966
2789 static bool contains_inclusive(const SkRect& r, SkScalar x, SkScalar y) { 2967 static bool contains_inclusive(const SkRect& r, SkScalar x, SkScalar y) {
2790 return r.fLeft <= x && x <= r.fRight && r.fTop <= y && y <= r.fBottom; 2968 return r.fLeft <= x && x <= r.fRight && r.fTop <= y && y <= r.fBottom;
2791 } 2969 }
2792 2970
2793 bool SkPath::contains(SkScalar x, SkScalar y) const { 2971 bool SkPath::contains(SkScalar x, SkScalar y) const {
2794 bool isInverse = this->isInverseFillType(); 2972 bool isInverse = this->isInverseFillType();
2795 if (this->isEmpty()) { 2973 if (this->isEmpty()) {
2796 return isInverse; 2974 return isInverse;
2797 } 2975 }
2798 2976
2799 if (!contains_inclusive(this->getBounds(), x, y)) { 2977 if (!contains_inclusive(this->getBounds(), x, y)) {
2800 return isInverse; 2978 return isInverse;
2801 } 2979 }
2802 2980
2803 SkPath::Iter iter(*this, true); 2981 SkPath::Iter iter(*this, true);
2804 bool done = false; 2982 bool done = false;
2805 int w = 0; 2983 int w = 0;
2984 int onCurveCount = 0;
2806 do { 2985 do {
2807 SkPoint pts[4]; 2986 SkPoint pts[4];
2808 switch (iter.next(pts, false)) { 2987 switch (iter.next(pts, false)) {
2809 case SkPath::kMove_Verb: 2988 case SkPath::kMove_Verb:
2810 case SkPath::kClose_Verb: 2989 case SkPath::kClose_Verb:
2811 break; 2990 break;
2812 case SkPath::kLine_Verb: 2991 case SkPath::kLine_Verb:
2813 w += winding_line(pts, x, y); 2992 w += winding_line(pts, x, y, &onCurveCount);
2814 break; 2993 break;
2815 case SkPath::kQuad_Verb: 2994 case SkPath::kQuad_Verb:
2816 w += winding_quad(pts, x, y); 2995 w += winding_quad(pts, x, y, &onCurveCount);
2817 break; 2996 break;
2818 case SkPath::kConic_Verb: 2997 case SkPath::kConic_Verb:
2819 SkASSERT(0); 2998 w += winding_conic(pts, x, y, iter.conicWeight(), &onCurveCount) ;
2820 break; 2999 break;
2821 case SkPath::kCubic_Verb: 3000 case SkPath::kCubic_Verb:
2822 w += winding_cubic(pts, x, y); 3001 w += winding_cubic(pts, x, y, &onCurveCount);
2823 break; 3002 break;
2824 case SkPath::kDone_Verb: 3003 case SkPath::kDone_Verb:
2825 done = true; 3004 done = true;
2826 break; 3005 break;
2827 } 3006 }
2828 } while (!done); 3007 } while (!done);
2829 3008 bool evenOddFill = SkPath::kEvenOdd_FillType == this->getFillType()
2830 switch (this->getFillType()) { 3009 || SkPath::kInverseEvenOdd_FillType == this->getFillType();
2831 case SkPath::kEvenOdd_FillType: 3010 if (evenOddFill) {
2832 case SkPath::kInverseEvenOdd_FillType: 3011 w &= 1;
2833 w &= 1;
2834 break;
2835 default:
2836 break;
2837 } 3012 }
2838 return SkToBool(w); 3013 if (w) {
3014 return !isInverse;
3015 }
3016 if (onCurveCount <= 1) {
3017 return SkToBool(onCurveCount) ^ isInverse;
3018 }
3019 if ((onCurveCount & 1) || evenOddFill) {
3020 return SkToBool(onCurveCount & 1) ^ isInverse;
3021 }
3022 // If the point touches an even number of curves, and the fill is winding, c heck for
3023 // coincidence. Count coincidence as places where the on curve points have i dentical tangents.
3024 iter.setPath(*this, true);
3025 SkTDArray<SkVector> tangents;
3026 do {
3027 SkPoint pts[4];
3028 int oldCount = tangents.count();
3029 switch (iter.next(pts, false)) {
3030 case SkPath::kMove_Verb:
3031 case SkPath::kClose_Verb:
3032 break;
3033 case SkPath::kLine_Verb:
3034 tangent_line(pts, x, y, &tangents);
3035 break;
3036 case SkPath::kQuad_Verb:
3037 tangent_quad(pts, x, y, &tangents);
3038 break;
3039 case SkPath::kConic_Verb:
3040 tangent_conic(pts, x, y, iter.conicWeight(), &tangents);
3041 break;
3042 case SkPath::kCubic_Verb:
3043 tangent_cubic(pts, x, y, &tangents);
3044 break;
3045 case SkPath::kDone_Verb:
3046 done = true;
3047 break;
3048 }
3049 if (tangents.count() > oldCount) {
3050 int last = tangents.count() - 1;
3051 const SkVector& tangent = tangents[last];
3052 if (SkScalarNearlyZero(tangent.lengthSqd())) {
3053 tangents.remove(last);
3054 } else {
3055 for (int index = 0; index < last; ++index) {
3056 const SkVector& test = tangents[index];
3057 if (SkScalarNearlyZero(test.cross(tangent))
3058 && SkScalarSignAsInt(tangent.fX - test.fX) <= 0
3059 && SkScalarSignAsInt(tangent.fY - test.fY) <= 0) {
3060 tangents.remove(last);
3061 tangents.removeShuffle(index);
3062 break;
3063 }
3064 }
3065 }
3066 }
3067 } while (!done);
3068 return SkToBool(tangents.count()) ^ isInverse;
2839 } 3069 }
2840 3070
2841 int SkPath::ConvertConicToQuads(const SkPoint& p0, const SkPoint& p1, const SkPo int& p2, 3071 int SkPath::ConvertConicToQuads(const SkPoint& p0, const SkPoint& p1, const SkPo int& p2,
2842 SkScalar w, SkPoint pts[], int pow2) { 3072 SkScalar w, SkPoint pts[], int pow2) {
2843 const SkConic conic(p0, p1, p2, w); 3073 const SkConic conic(p0, p1, p2, w);
2844 return conic.chopIntoQuadsPOW2(pts, pow2); 3074 return conic.chopIntoQuadsPOW2(pts, pow2);
2845 } 3075 }
OLDNEW
« no previous file with comments | « src/core/SkCubicClipper.cpp ('k') | tests/PathTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698