2012-04-26 21:01:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2012-05-18 20:50:33 +00:00
|
|
|
#include "Simplify.h"
|
2012-04-26 21:01:06 +00:00
|
|
|
|
|
|
|
#undef SkASSERT
|
|
|
|
#define SkASSERT(cond) while (!(cond)) { sk_throw(); }
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
// Terminology:
|
|
|
|
// A Path contains one of more Contours
|
|
|
|
// A Contour is made up of Segment array
|
2012-05-18 20:50:33 +00:00
|
|
|
// A Segment is described by a Verb and a Point array with 2, 3, or 4 points
|
|
|
|
// A Verb is one of Line, Quad(ratic), or Cubic
|
2012-05-07 20:49:36 +00:00
|
|
|
// A Segment contains a Span array
|
|
|
|
// A Span is describes a portion of a Segment using starting and ending T
|
|
|
|
// T values range from 0 to 1, where 0 is the first Point in the Segment
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
// FIXME: remove once debugging is complete
|
|
|
|
#if 0 // set to 1 for no debugging whatsoever
|
|
|
|
|
|
|
|
//const bool gxRunTestsInOneThread = false;
|
|
|
|
|
|
|
|
#define DEBUG_ADD_INTERSECTING_TS 0
|
|
|
|
#define DEBUG_BRIDGE 0
|
2012-07-02 20:27:02 +00:00
|
|
|
#define DEBUG_CROSS 0
|
2012-04-26 21:01:06 +00:00
|
|
|
#define DEBUG_DUMP 0
|
2012-05-23 18:09:25 +00:00
|
|
|
#define DEBUG_PATH_CONSTRUCTION 0
|
2012-07-12 12:52:50 +00:00
|
|
|
#define DEBUG_ACTIVE_SPANS 0
|
2012-07-02 20:27:02 +00:00
|
|
|
#define DEBUG_WINDING 0
|
2012-05-23 18:09:25 +00:00
|
|
|
#define DEBUG_UNUSED 0 // set to expose unused functions
|
2012-07-02 20:27:02 +00:00
|
|
|
#define DEBUG_MARK_DONE 0
|
2012-04-26 21:01:06 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
//const bool gRunTestsInOneThread = true;
|
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
#define DEBUG_ADD_INTERSECTING_TS 0
|
2012-04-26 21:01:06 +00:00
|
|
|
#define DEBUG_BRIDGE 1
|
2012-07-02 20:27:02 +00:00
|
|
|
#define DEBUG_CROSS 1
|
2012-04-26 21:01:06 +00:00
|
|
|
#define DEBUG_DUMP 1
|
2012-05-23 18:09:25 +00:00
|
|
|
#define DEBUG_PATH_CONSTRUCTION 1
|
2012-07-12 21:05:13 +00:00
|
|
|
#define DEBUG_ACTIVE_SPANS 01
|
|
|
|
#define DEBUG_WINDING 01
|
2012-05-23 18:09:25 +00:00
|
|
|
#define DEBUG_UNUSED 0 // set to expose unused functions
|
2012-07-12 21:05:13 +00:00
|
|
|
#define DEBUG_MARK_DONE 01
|
2012-04-26 21:01:06 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-12 12:52:50 +00:00
|
|
|
#if DEBUG_ACTIVE_SPANS && !DEBUG_DUMP
|
|
|
|
#undef DEBUG_DUMP
|
|
|
|
#define DEBUG_DUMP 1
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
#if DEBUG_DUMP
|
|
|
|
static const char* kLVerbStr[] = {"", "line", "quad", "cubic"};
|
2012-05-23 18:09:25 +00:00
|
|
|
// static const char* kUVerbStr[] = {"", "Line", "Quad", "Cubic"};
|
2012-04-26 21:01:06 +00:00
|
|
|
static int gContourID;
|
|
|
|
static int gSegmentID;
|
|
|
|
#endif
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
#ifndef DEBUG_TEST
|
|
|
|
#define DEBUG_TEST 0
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
static int LineIntersect(const SkPoint a[2], const SkPoint b[2],
|
|
|
|
Intersections& intersections) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
|
|
|
|
return intersect(aLine, bLine, intersections.fT[0], intersections.fT[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int QuadLineIntersect(const SkPoint a[3], const SkPoint b[2],
|
|
|
|
Intersections& intersections) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
|
|
|
|
intersect(aQuad, bLine, intersections);
|
|
|
|
return intersections.fUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CubicLineIntersect(const SkPoint a[2], const SkPoint b[3],
|
|
|
|
Intersections& intersections) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
const _Line bLine = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}};
|
|
|
|
return intersect(aCubic, bLine, intersections.fT[0], intersections.fT[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int QuadIntersect(const SkPoint a[3], const SkPoint b[3],
|
|
|
|
Intersections& intersections) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
const Quadratic bQuad = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY}};
|
|
|
|
intersect(aQuad, bQuad, intersections);
|
|
|
|
return intersections.fUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int CubicIntersect(const SkPoint a[4], const SkPoint b[4],
|
|
|
|
Intersections& intersections) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
const Cubic bCubic = {{b[0].fX, b[0].fY}, {b[1].fX, b[1].fY}, {b[2].fX, b[2].fY},
|
|
|
|
{b[3].fX, b[3].fY}};
|
|
|
|
intersect(aCubic, bCubic, intersections);
|
|
|
|
return intersections.fUsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int HLineIntersect(const SkPoint a[2], SkScalar left, SkScalar right,
|
|
|
|
SkScalar y, bool flipped, Intersections& intersections) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
return horizontalIntersect(aLine, left, right, y, flipped, intersections);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int HQuadIntersect(const SkPoint a[3], SkScalar left, SkScalar right,
|
|
|
|
SkScalar y, bool flipped, Intersections& intersections) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
return horizontalIntersect(aQuad, left, right, y, flipped, intersections);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int HCubicIntersect(const SkPoint a[4], SkScalar left, SkScalar right,
|
|
|
|
SkScalar y, bool flipped, Intersections& intersections) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
return horizontalIntersect(aCubic, left, right, y, flipped, intersections);
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
static int VLineIntersect(const SkPoint a[2], SkScalar top, SkScalar bottom,
|
|
|
|
SkScalar x, bool flipped, Intersections& intersections) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
return verticalIntersect(aLine, top, bottom, x, flipped, intersections);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int VQuadIntersect(const SkPoint a[3], SkScalar top, SkScalar bottom,
|
|
|
|
SkScalar x, bool flipped, Intersections& intersections) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
return verticalIntersect(aQuad, top, bottom, x, flipped, intersections);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int VCubicIntersect(const SkPoint a[4], SkScalar top, SkScalar bottom,
|
|
|
|
SkScalar x, bool flipped, Intersections& intersections) {
|
2012-04-26 21:01:06 +00:00
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
2012-07-02 20:27:02 +00:00
|
|
|
return verticalIntersect(aCubic, top, bottom, x, flipped, intersections);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
static int (* const VSegmentIntersect[])(const SkPoint [], SkScalar ,
|
|
|
|
SkScalar , SkScalar , bool , Intersections& ) = {
|
|
|
|
NULL,
|
|
|
|
VLineIntersect,
|
|
|
|
VQuadIntersect,
|
|
|
|
VCubicIntersect
|
|
|
|
};
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
static void LineXYAtT(const SkPoint a[2], double t, SkPoint* out) {
|
|
|
|
const _Line line = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
double x, y;
|
|
|
|
xy_at_t(line, t, x, y);
|
|
|
|
out->fX = SkDoubleToScalar(x);
|
|
|
|
out->fY = SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void QuadXYAtT(const SkPoint a[3], double t, SkPoint* out) {
|
|
|
|
const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
double x, y;
|
|
|
|
xy_at_t(quad, t, x, y);
|
|
|
|
out->fX = SkDoubleToScalar(x);
|
|
|
|
out->fY = SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CubicXYAtT(const SkPoint a[4], double t, SkPoint* out) {
|
|
|
|
const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
double x, y;
|
|
|
|
xy_at_t(cubic, t, x, y);
|
|
|
|
out->fX = SkDoubleToScalar(x);
|
|
|
|
out->fY = SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void (* const SegmentXYAtT[])(const SkPoint [], double , SkPoint* ) = {
|
|
|
|
NULL,
|
|
|
|
LineXYAtT,
|
|
|
|
QuadXYAtT,
|
|
|
|
CubicXYAtT
|
|
|
|
};
|
|
|
|
|
|
|
|
static SkScalar LineXAtT(const SkPoint a[2], double t) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
double x;
|
|
|
|
xy_at_t(aLine, t, x, *(double*) 0);
|
|
|
|
return SkDoubleToScalar(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar QuadXAtT(const SkPoint a[3], double t) {
|
|
|
|
const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
double x;
|
|
|
|
xy_at_t(quad, t, x, *(double*) 0);
|
|
|
|
return SkDoubleToScalar(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar CubicXAtT(const SkPoint a[4], double t) {
|
|
|
|
const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
double x;
|
|
|
|
xy_at_t(cubic, t, x, *(double*) 0);
|
|
|
|
return SkDoubleToScalar(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar (* const SegmentXAtT[])(const SkPoint [], double ) = {
|
|
|
|
NULL,
|
|
|
|
LineXAtT,
|
|
|
|
QuadXAtT,
|
|
|
|
CubicXAtT
|
|
|
|
};
|
|
|
|
|
|
|
|
static SkScalar LineYAtT(const SkPoint a[2], double t) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
double y;
|
|
|
|
xy_at_t(aLine, t, *(double*) 0, y);
|
|
|
|
return SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar QuadYAtT(const SkPoint a[3], double t) {
|
|
|
|
const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
double y;
|
|
|
|
xy_at_t(quad, t, *(double*) 0, y);
|
|
|
|
return SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar CubicYAtT(const SkPoint a[4], double t) {
|
|
|
|
const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
double y;
|
|
|
|
xy_at_t(cubic, t, *(double*) 0, y);
|
|
|
|
return SkDoubleToScalar(y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar (* const SegmentYAtT[])(const SkPoint [], double ) = {
|
|
|
|
NULL,
|
|
|
|
LineYAtT,
|
|
|
|
QuadYAtT,
|
|
|
|
CubicYAtT
|
|
|
|
};
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
static SkScalar LineDXAtT(const SkPoint a[2], double ) {
|
|
|
|
return a[1].fX - a[0].fX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar QuadDXAtT(const SkPoint a[3], double t) {
|
|
|
|
const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY}};
|
|
|
|
double x;
|
|
|
|
dxdy_at_t(quad, t, x, *(double*) 0);
|
|
|
|
return SkDoubleToScalar(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar CubicDXAtT(const SkPoint a[4], double t) {
|
|
|
|
const Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}, {a[2].fX, a[2].fY},
|
|
|
|
{a[3].fX, a[3].fY}};
|
|
|
|
double x;
|
|
|
|
dxdy_at_t(cubic, t, x, *(double*) 0);
|
|
|
|
return SkDoubleToScalar(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar (* const SegmentDXAtT[])(const SkPoint [], double ) = {
|
|
|
|
NULL,
|
|
|
|
LineDXAtT,
|
|
|
|
QuadDXAtT,
|
|
|
|
CubicDXAtT
|
|
|
|
};
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
static void LineSubDivide(const SkPoint a[2], double startT, double endT,
|
|
|
|
SkPoint sub[2]) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
_Line dst;
|
|
|
|
sub_divide(aLine, startT, endT, dst);
|
|
|
|
sub[0].fX = SkDoubleToScalar(dst[0].x);
|
|
|
|
sub[0].fY = SkDoubleToScalar(dst[0].y);
|
|
|
|
sub[1].fX = SkDoubleToScalar(dst[1].x);
|
|
|
|
sub[1].fY = SkDoubleToScalar(dst[1].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void QuadSubDivide(const SkPoint a[3], double startT, double endT,
|
|
|
|
SkPoint sub[3]) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}};
|
|
|
|
Quadratic dst;
|
|
|
|
sub_divide(aQuad, startT, endT, dst);
|
|
|
|
sub[0].fX = SkDoubleToScalar(dst[0].x);
|
|
|
|
sub[0].fY = SkDoubleToScalar(dst[0].y);
|
|
|
|
sub[1].fX = SkDoubleToScalar(dst[1].x);
|
|
|
|
sub[1].fY = SkDoubleToScalar(dst[1].y);
|
|
|
|
sub[2].fX = SkDoubleToScalar(dst[2].x);
|
|
|
|
sub[2].fY = SkDoubleToScalar(dst[2].y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CubicSubDivide(const SkPoint a[4], double startT, double endT,
|
|
|
|
SkPoint sub[4]) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
|
|
|
|
Cubic dst;
|
|
|
|
sub_divide(aCubic, startT, endT, dst);
|
|
|
|
sub[0].fX = SkDoubleToScalar(dst[0].x);
|
|
|
|
sub[0].fY = SkDoubleToScalar(dst[0].y);
|
|
|
|
sub[1].fX = SkDoubleToScalar(dst[1].x);
|
|
|
|
sub[1].fY = SkDoubleToScalar(dst[1].y);
|
|
|
|
sub[2].fX = SkDoubleToScalar(dst[2].x);
|
|
|
|
sub[2].fY = SkDoubleToScalar(dst[2].y);
|
|
|
|
sub[3].fX = SkDoubleToScalar(dst[3].x);
|
|
|
|
sub[3].fY = SkDoubleToScalar(dst[3].y);
|
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
static void (* const SegmentSubDivide[])(const SkPoint [], double , double ,
|
|
|
|
SkPoint []) = {
|
|
|
|
NULL,
|
|
|
|
LineSubDivide,
|
|
|
|
QuadSubDivide,
|
|
|
|
CubicSubDivide
|
|
|
|
};
|
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
#if DEBUG_UNUSED
|
2012-04-26 21:01:06 +00:00
|
|
|
static void QuadSubBounds(const SkPoint a[3], double startT, double endT,
|
|
|
|
SkRect& bounds) {
|
|
|
|
SkPoint dst[3];
|
|
|
|
QuadSubDivide(a, startT, endT, dst);
|
|
|
|
bounds.fLeft = bounds.fRight = dst[0].fX;
|
|
|
|
bounds.fTop = bounds.fBottom = dst[0].fY;
|
|
|
|
for (int index = 1; index < 3; ++index) {
|
|
|
|
bounds.growToInclude(dst[index].fX, dst[index].fY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CubicSubBounds(const SkPoint a[4], double startT, double endT,
|
|
|
|
SkRect& bounds) {
|
|
|
|
SkPoint dst[4];
|
|
|
|
CubicSubDivide(a, startT, endT, dst);
|
|
|
|
bounds.fLeft = bounds.fRight = dst[0].fX;
|
|
|
|
bounds.fTop = bounds.fBottom = dst[0].fY;
|
|
|
|
for (int index = 1; index < 4; ++index) {
|
|
|
|
bounds.growToInclude(dst[index].fX, dst[index].fY);
|
|
|
|
}
|
|
|
|
}
|
2012-05-23 18:09:25 +00:00
|
|
|
#endif
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
static SkPath::Verb QuadReduceOrder(const SkPoint a[3],
|
2012-04-26 21:01:06 +00:00
|
|
|
SkTDArray<SkPoint>& reducePts) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}};
|
|
|
|
Quadratic dst;
|
|
|
|
int order = reduceOrder(aQuad, dst);
|
2012-05-18 20:50:33 +00:00
|
|
|
if (order == 3) {
|
|
|
|
return SkPath::kQuad_Verb;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
for (int index = 0; index < order; ++index) {
|
|
|
|
SkPoint* pt = reducePts.append();
|
|
|
|
pt->fX = SkDoubleToScalar(dst[index].x);
|
|
|
|
pt->fY = SkDoubleToScalar(dst[index].y);
|
|
|
|
}
|
|
|
|
return (SkPath::Verb) (order - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkPath::Verb CubicReduceOrder(const SkPoint a[4],
|
|
|
|
SkTDArray<SkPoint>& reducePts) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
|
|
|
|
Cubic dst;
|
|
|
|
int order = reduceOrder(aCubic, dst, kReduceOrder_QuadraticsAllowed);
|
2012-05-18 20:50:33 +00:00
|
|
|
if (order == 4) {
|
|
|
|
return SkPath::kCubic_Verb;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
for (int index = 0; index < order; ++index) {
|
|
|
|
SkPoint* pt = reducePts.append();
|
|
|
|
pt->fX = SkDoubleToScalar(dst[index].x);
|
|
|
|
pt->fY = SkDoubleToScalar(dst[index].y);
|
|
|
|
}
|
|
|
|
return (SkPath::Verb) (order - 1);
|
|
|
|
}
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
static bool QuadIsLinear(const SkPoint a[3]) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}};
|
|
|
|
return isLinear(aQuad, 0, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CubicIsLinear(const SkPoint a[4]) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
|
|
|
|
return isLinear(aCubic, 0, 3);
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
static SkScalar LineLeftMost(const SkPoint a[2], double startT, double endT) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
double x[2];
|
|
|
|
xy_at_t(aLine, startT, x[0], *(double*) 0);
|
2012-05-31 13:13:11 +00:00
|
|
|
xy_at_t(aLine, endT, x[1], *(double*) 0);
|
|
|
|
return SkMinScalar((float) x[0], (float) x[1]);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar QuadLeftMost(const SkPoint a[3], double startT, double endT) {
|
|
|
|
const Quadratic aQuad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}};
|
2012-05-18 20:50:33 +00:00
|
|
|
return (float) leftMostT(aQuad, startT, endT);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar CubicLeftMost(const SkPoint a[4], double startT, double endT) {
|
|
|
|
const Cubic aCubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
|
2012-05-18 20:50:33 +00:00
|
|
|
return (float) leftMostT(aCubic, startT, endT);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SkScalar (* const SegmentLeftMost[])(const SkPoint [], double , double) = {
|
|
|
|
NULL,
|
|
|
|
LineLeftMost,
|
|
|
|
QuadLeftMost,
|
|
|
|
CubicLeftMost
|
|
|
|
};
|
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
#if DEBUG_UNUSED
|
2012-04-26 21:01:06 +00:00
|
|
|
static bool IsCoincident(const SkPoint a[2], const SkPoint& above,
|
|
|
|
const SkPoint& below) {
|
|
|
|
const _Line aLine = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY}};
|
|
|
|
const _Line bLine = {{above.fX, above.fY}, {below.fX, below.fY}};
|
|
|
|
return implicit_matches_ulps(aLine, bLine, 32);
|
|
|
|
}
|
2012-05-23 18:09:25 +00:00
|
|
|
#endif
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
class Segment;
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
// sorting angles
|
|
|
|
// given angles of {dx dy ddx ddy dddx dddy} sort them
|
|
|
|
class Angle {
|
|
|
|
public:
|
2012-05-18 20:50:33 +00:00
|
|
|
// FIXME: this is bogus for quads and cubics
|
|
|
|
// if the quads and cubics' line from end pt to ctrl pt are coincident,
|
|
|
|
// there's no obvious way to determine the curve ordering from the
|
|
|
|
// derivatives alone. In particular, if one quadratic's coincident tangent
|
|
|
|
// is longer than the other curve, the final control point can place the
|
|
|
|
// longer curve on either side of the shorter one.
|
|
|
|
// Using Bezier curve focus http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf
|
|
|
|
// may provide some help, but nothing has been figured out yet.
|
2012-05-07 20:49:36 +00:00
|
|
|
bool operator<(const Angle& rh) const {
|
2012-05-18 20:50:33 +00:00
|
|
|
if ((fDy < 0) ^ (rh.fDy < 0)) {
|
|
|
|
return fDy < 0;
|
|
|
|
}
|
|
|
|
if (fDy == 0 && rh.fDy == 0 && fDx != rh.fDx) {
|
|
|
|
return fDx < rh.fDx;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
SkScalar cmp = fDx * rh.fDy - rh.fDx * fDy;
|
2012-05-07 20:49:36 +00:00
|
|
|
if (cmp) {
|
|
|
|
return cmp < 0;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
if ((fDDy < 0) ^ (rh.fDDy < 0)) {
|
|
|
|
return fDDy < 0;
|
|
|
|
}
|
|
|
|
if (fDDy == 0 && rh.fDDy == 0 && fDDx != rh.fDDx) {
|
|
|
|
return fDDx < rh.fDDx;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
cmp = fDDx * rh.fDDy - rh.fDDx * fDDy;
|
2012-05-07 20:49:36 +00:00
|
|
|
if (cmp) {
|
|
|
|
return cmp < 0;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
if ((fDDDy < 0) ^ (rh.fDDDy < 0)) {
|
|
|
|
return fDDDy < 0;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
if (fDDDy == 0 && rh.fDDDy == 0) {
|
|
|
|
return fDDDx < rh.fDDDx;
|
|
|
|
}
|
|
|
|
return fDDDx * rh.fDDDy < rh.fDDDx * fDDDy;
|
|
|
|
}
|
|
|
|
|
|
|
|
int end() const {
|
|
|
|
return fEnd;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
|
|
|
|
2012-06-07 21:09:20 +00:00
|
|
|
bool isHorizontal() const {
|
|
|
|
return fDy == 0 && fDDy == 0 && fDDDy == 0;
|
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
// since all angles share a point, this needs to know which point
|
|
|
|
// is the common origin, i.e., whether the center is at pts[0] or pts[verb]
|
|
|
|
// practically, this should only be called by addAngle
|
2012-07-02 20:27:02 +00:00
|
|
|
void set(const SkPoint* pts, SkPath::Verb verb, const Segment* segment,
|
2012-06-01 17:44:28 +00:00
|
|
|
int start, int end) {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkASSERT(start != end);
|
|
|
|
fSegment = segment;
|
|
|
|
fStart = start;
|
|
|
|
fEnd = end;
|
|
|
|
fDx = pts[1].fX - pts[0].fX; // b - a
|
|
|
|
fDy = pts[1].fY - pts[0].fY;
|
2012-05-07 20:49:36 +00:00
|
|
|
if (verb == SkPath::kLine_Verb) {
|
2012-05-18 20:50:33 +00:00
|
|
|
fDDx = fDDy = fDDDx = fDDDy = 0;
|
2012-05-07 20:49:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
fDDx = pts[2].fX - pts[1].fX - fDx; // a - 2b + c
|
|
|
|
fDDy = pts[2].fY - pts[1].fY - fDy;
|
2012-05-07 20:49:36 +00:00
|
|
|
if (verb == SkPath::kQuad_Verb) {
|
2012-05-18 20:50:33 +00:00
|
|
|
fDDDx = fDDDy = 0;
|
2012-05-07 20:49:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
fDDDx = pts[3].fX + 3 * (pts[1].fX - pts[2].fX) - pts[0].fX;
|
|
|
|
fDDDy = pts[3].fY + 3 * (pts[1].fY - pts[2].fY) - pts[0].fY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// noncoincident quads/cubics may have the same initial angle
|
|
|
|
// as lines, so must sort by derivatives as well
|
|
|
|
// if flatness turns out to be a reasonable way to sort, use the below:
|
2012-05-22 17:01:14 +00:00
|
|
|
void setFlat(const SkPoint* pts, SkPath::Verb verb, Segment* segment,
|
2012-06-01 17:44:28 +00:00
|
|
|
int start, int end) {
|
2012-05-18 20:50:33 +00:00
|
|
|
fSegment = segment;
|
|
|
|
fStart = start;
|
|
|
|
fEnd = end;
|
|
|
|
fDx = pts[1].fX - pts[0].fX; // b - a
|
|
|
|
fDy = pts[1].fY - pts[0].fY;
|
|
|
|
if (verb == SkPath::kLine_Verb) {
|
|
|
|
fDDx = fDDy = fDDDx = fDDDy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (verb == SkPath::kQuad_Verb) {
|
|
|
|
int uplsX = FloatAsInt(pts[2].fX - pts[1].fY - fDx);
|
|
|
|
int uplsY = FloatAsInt(pts[2].fY - pts[1].fY - fDy);
|
|
|
|
int larger = std::max(abs(uplsX), abs(uplsY));
|
|
|
|
int shift = 0;
|
|
|
|
double flatT;
|
|
|
|
SkPoint ddPt; // FIXME: get rid of copy (change fDD_ to point)
|
|
|
|
LineParameters implicitLine;
|
|
|
|
_Line tangent = {{pts[0].fX, pts[0].fY}, {pts[1].fX, pts[1].fY}};
|
|
|
|
implicitLine.lineEndPoints(tangent);
|
|
|
|
implicitLine.normalize();
|
|
|
|
while (larger > UlpsEpsilon * 1024) {
|
|
|
|
larger >>= 2;
|
|
|
|
++shift;
|
|
|
|
flatT = 0.5 / (1 << shift);
|
|
|
|
QuadXYAtT(pts, flatT, &ddPt);
|
|
|
|
_Point _pt = {ddPt.fX, ddPt.fY};
|
|
|
|
double distance = implicitLine.pointDistance(_pt);
|
|
|
|
if (approximately_zero(distance)) {
|
|
|
|
SkDebugf("%s ulps too small %1.9g\n", __FUNCTION__, distance);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flatT = 0.5 / (1 << shift);
|
|
|
|
QuadXYAtT(pts, flatT, &ddPt);
|
|
|
|
fDDx = ddPt.fX - pts[0].fX;
|
|
|
|
fDDy = ddPt.fY - pts[0].fY;
|
|
|
|
SkASSERT(fDDx != 0 || fDDy != 0);
|
|
|
|
fDDDx = fDDDy = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkASSERT(0); // FIXME: add cubic case
|
|
|
|
}
|
|
|
|
|
2012-05-22 17:01:14 +00:00
|
|
|
Segment* segment() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return const_cast<Segment*>(fSegment);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int sign() const {
|
2012-05-31 13:13:11 +00:00
|
|
|
return SkSign32(fStart - fEnd);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
int start() const {
|
|
|
|
return fStart;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-05-18 20:50:33 +00:00
|
|
|
SkScalar fDx;
|
|
|
|
SkScalar fDy;
|
|
|
|
SkScalar fDDx;
|
|
|
|
SkScalar fDDy;
|
|
|
|
SkScalar fDDDx;
|
|
|
|
SkScalar fDDDy;
|
2012-07-02 20:27:02 +00:00
|
|
|
const Segment* fSegment;
|
2012-05-18 20:50:33 +00:00
|
|
|
int fStart;
|
|
|
|
int fEnd;
|
2012-05-07 20:49:36 +00:00
|
|
|
};
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
static void sortAngles(SkTDArray<Angle>& angles, SkTDArray<Angle*>& angleList) {
|
|
|
|
int angleCount = angles.count();
|
|
|
|
int angleIndex;
|
|
|
|
angleList.setReserve(angleCount);
|
|
|
|
for (angleIndex = 0; angleIndex < angleCount; ++angleIndex) {
|
|
|
|
*angleList.append() = &angles[angleIndex];
|
|
|
|
}
|
|
|
|
QSort<Angle>(angleList.begin(), angleList.end() - 1);
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// Bounds, unlike Rect, does not consider a line to be empty.
|
2012-04-26 21:01:06 +00:00
|
|
|
struct Bounds : public SkRect {
|
|
|
|
static bool Intersects(const Bounds& a, const Bounds& b) {
|
|
|
|
return a.fLeft <= b.fRight && b.fLeft <= a.fRight &&
|
|
|
|
a.fTop <= b.fBottom && b.fTop <= a.fBottom;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
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 Bounds& toAdd) {
|
|
|
|
add(toAdd.fLeft, toAdd.fTop, toAdd.fRight, toAdd.fBottom);
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
bool isEmpty() {
|
|
|
|
return fLeft > fRight || fTop > fBottom
|
|
|
|
|| fLeft == fRight && fTop == fBottom
|
|
|
|
|| isnan(fLeft) || isnan(fRight)
|
|
|
|
|| isnan(fTop) || isnan(fBottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setCubicBounds(const SkPoint a[4]) {
|
|
|
|
_Rect dRect;
|
|
|
|
Cubic cubic = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}, {a[3].fX, a[3].fY}};
|
|
|
|
dRect.setBounds(cubic);
|
2012-05-18 20:50:33 +00:00
|
|
|
set((float) dRect.left, (float) dRect.top, (float) dRect.right,
|
|
|
|
(float) dRect.bottom);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setQuadBounds(const SkPoint a[3]) {
|
|
|
|
const Quadratic quad = {{a[0].fX, a[0].fY}, {a[1].fX, a[1].fY},
|
|
|
|
{a[2].fX, a[2].fY}};
|
|
|
|
_Rect dRect;
|
|
|
|
dRect.setBounds(quad);
|
2012-05-18 20:50:33 +00:00
|
|
|
set((float) dRect.left, (float) dRect.top, (float) dRect.right,
|
|
|
|
(float) dRect.bottom);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
struct Span {
|
2012-04-30 19:38:50 +00:00
|
|
|
Segment* fOther;
|
2012-07-02 20:27:02 +00:00
|
|
|
mutable SkPoint const* fPt; // lazily computed as needed
|
|
|
|
double fT;
|
2012-05-18 20:50:33 +00:00
|
|
|
double fOtherT; // value at fOther[fOtherIndex].fT
|
|
|
|
int fOtherIndex; // can't be used during intersection
|
2012-07-02 20:27:02 +00:00
|
|
|
int fWindSum; // accumulated from contours surrounding this one
|
|
|
|
int fWindValue; // 0 == canceled; 1 == normal; >1 == coincident
|
2012-05-23 18:09:25 +00:00
|
|
|
bool fDone; // if set, this span to next higher T has been processed
|
2012-04-26 21:01:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Segment {
|
|
|
|
public:
|
|
|
|
Segment() {
|
|
|
|
#if DEBUG_DUMP
|
|
|
|
fID = ++gSegmentID;
|
|
|
|
#endif
|
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
|
2012-07-12 19:29:45 +00:00
|
|
|
bool activeAngle(int index, int& done, SkTDArray<Angle>& angles) const {
|
|
|
|
if (activeAngleInner(index, done, angles)) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
double referenceT = fTs[index].fT;
|
|
|
|
int lesser = index;
|
|
|
|
while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
|
2012-07-12 19:29:45 +00:00
|
|
|
if (activeAngleOther(lesser, done, angles)) {
|
2012-07-11 17:52:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do {
|
2012-07-12 19:29:45 +00:00
|
|
|
if (activeAngleOther(index, done, angles)) {
|
2012-07-11 17:52:32 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-12 19:29:45 +00:00
|
|
|
bool activeAngleOther(int index, int& done, SkTDArray<Angle>& angles) const {
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* span = &fTs[index];
|
|
|
|
Segment* other = span->fOther;
|
|
|
|
int oIndex = span->fOtherIndex;
|
2012-07-12 19:29:45 +00:00
|
|
|
return other->activeAngleInner(oIndex, done, angles);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool activeAngleInner(int index, int& done, SkTDArray<Angle>& angles) const {
|
|
|
|
int next = nextSpan(index, 1);
|
|
|
|
if (next > 0) {
|
|
|
|
const Span& upSpan = fTs[index];
|
2012-07-12 21:05:13 +00:00
|
|
|
if (upSpan.fWindValue) {
|
|
|
|
addAngle(angles, index, next);
|
|
|
|
if (upSpan.fDone) {
|
|
|
|
done++;
|
|
|
|
} else if (upSpan.fWindSum != SK_MinS32) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-12 19:29:45 +00:00
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
2012-07-12 19:29:45 +00:00
|
|
|
int prev = nextSpan(index, -1);
|
2012-07-11 17:52:32 +00:00
|
|
|
// edge leading into junction
|
2012-07-12 19:29:45 +00:00
|
|
|
if (prev >= 0) {
|
|
|
|
const Span& downSpan = fTs[prev];
|
2012-07-12 21:05:13 +00:00
|
|
|
if (downSpan.fWindValue) {
|
|
|
|
addAngle(angles, index, prev);
|
|
|
|
if (downSpan.fDone) {
|
|
|
|
done++;
|
|
|
|
} else if (downSpan.fWindSum != SK_MinS32) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-12 19:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
SkScalar activeTop() const {
|
|
|
|
SkASSERT(!done());
|
|
|
|
int count = fTs.count();
|
|
|
|
SkScalar result = SK_ScalarMax;
|
|
|
|
bool lastDone = true;
|
|
|
|
for (int index = 0; index < count; ++index) {
|
|
|
|
bool done = fTs[index].fDone;
|
|
|
|
if (!done || !lastDone) {
|
|
|
|
SkScalar y = yAtT(index);
|
|
|
|
if (result > y) {
|
|
|
|
result = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastDone = done;
|
|
|
|
}
|
|
|
|
SkASSERT(result < SK_ScalarMax);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addAngle(SkTDArray<Angle>& angles, int start, int end) const {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkASSERT(start != end);
|
|
|
|
SkPoint edge[4];
|
|
|
|
(*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
|
|
|
|
Angle* angle = angles.append();
|
2012-06-01 17:44:28 +00:00
|
|
|
angle->set(edge, fVerb, this, start, end);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
void addCubic(const SkPoint pts[4]) {
|
|
|
|
init(pts, SkPath::kCubic_Verb);
|
2012-04-26 21:01:06 +00:00
|
|
|
fBounds.setCubicBounds(pts);
|
|
|
|
}
|
|
|
|
|
2012-06-07 21:09:20 +00:00
|
|
|
// FIXME: this needs to defer add for aligned consecutive line segments
|
2012-07-02 20:27:02 +00:00
|
|
|
SkPoint addCurveTo(int start, int end, SkPath& path, bool active) {
|
2012-05-22 17:01:14 +00:00
|
|
|
SkPoint edge[4];
|
2012-07-02 20:27:02 +00:00
|
|
|
// OPTIMIZE? if not active, skip remainder and return xy_at_t(end)
|
2012-05-22 17:01:14 +00:00
|
|
|
(*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
|
2012-07-02 20:27:02 +00:00
|
|
|
if (active) {
|
2012-05-23 18:09:25 +00:00
|
|
|
#if DEBUG_PATH_CONSTRUCTION
|
2012-07-02 20:27:02 +00:00
|
|
|
SkDebugf("%s %s (%1.9g,%1.9g)", __FUNCTION__,
|
|
|
|
kLVerbStr[fVerb], edge[1].fX, edge[1].fY);
|
|
|
|
if (fVerb > 1) {
|
|
|
|
SkDebugf(" (%1.9g,%1.9g)", edge[2].fX, edge[2].fY);
|
|
|
|
}
|
|
|
|
if (fVerb > 2) {
|
|
|
|
SkDebugf(" (%1.9g,%1.9g)", edge[3].fX, edge[3].fY);
|
|
|
|
}
|
|
|
|
SkDebugf("\n");
|
2012-05-23 18:09:25 +00:00
|
|
|
#endif
|
2012-07-02 20:27:02 +00:00
|
|
|
switch (fVerb) {
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
path.lineTo(edge[1].fX, edge[1].fY);
|
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
|
|
|
path.quadTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY);
|
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
path.cubicTo(edge[1].fX, edge[1].fY, edge[2].fX, edge[2].fY,
|
|
|
|
edge[3].fX, edge[3].fY);
|
|
|
|
break;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
2012-06-07 21:09:20 +00:00
|
|
|
return edge[fVerb];
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
void addLine(const SkPoint pts[2]) {
|
|
|
|
init(pts, SkPath::kLine_Verb);
|
2012-04-26 21:01:06 +00:00
|
|
|
fBounds.set(pts, 2);
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
const SkPoint& addMoveTo(int tIndex, SkPath& path, bool active) {
|
|
|
|
const SkPoint& pt = xyAtT(tIndex);
|
|
|
|
if (active) {
|
2012-05-23 18:09:25 +00:00
|
|
|
#if DEBUG_PATH_CONSTRUCTION
|
2012-07-02 20:27:02 +00:00
|
|
|
SkDebugf("%s (%1.9g,%1.9g)\n", __FUNCTION__, pt.fX, pt.fY);
|
2012-05-23 18:09:25 +00:00
|
|
|
#endif
|
2012-07-02 20:27:02 +00:00
|
|
|
path.moveTo(pt.fX, pt.fY);
|
|
|
|
}
|
2012-06-07 21:09:20 +00:00
|
|
|
return pt;
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
// add 2 to edge or out of range values to get T extremes
|
2012-05-18 20:50:33 +00:00
|
|
|
void addOtherT(int index, double otherT, int otherIndex) {
|
|
|
|
Span& span = fTs[index];
|
|
|
|
span.fOtherT = otherT;
|
|
|
|
span.fOtherIndex = otherIndex;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
void addQuad(const SkPoint pts[3]) {
|
|
|
|
init(pts, SkPath::kQuad_Verb);
|
2012-04-26 21:01:06 +00:00
|
|
|
fBounds.setQuadBounds(pts);
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
|
|
|
|
// Defer all coincident edge processing until
|
|
|
|
// after normal intersections have been computed
|
|
|
|
|
|
|
|
// no need to be tricky; insert in normal T order
|
|
|
|
// resolve overlapping ts when considering coincidence later
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// add non-coincident intersection. Resulting edges are sorted in T.
|
|
|
|
int addT(double newT, Segment* other) {
|
2012-05-07 20:49:36 +00:00
|
|
|
// FIXME: in the pathological case where there is a ton of intercepts,
|
|
|
|
// binary search?
|
|
|
|
int insertedAt = -1;
|
|
|
|
size_t tCount = fTs.count();
|
2012-07-02 20:27:02 +00:00
|
|
|
for (size_t index = 0; index < tCount; ++index) {
|
2012-05-07 20:49:36 +00:00
|
|
|
// OPTIMIZATION: if there are three or more identical Ts, then
|
|
|
|
// the fourth and following could be further insertion-sorted so
|
|
|
|
// that all the edges are clockwise or counterclockwise.
|
|
|
|
// This could later limit segment tests to the two adjacent
|
|
|
|
// neighbors, although it doesn't help with determining which
|
|
|
|
// circular direction to go in.
|
2012-07-02 20:27:02 +00:00
|
|
|
if (newT < fTs[index].fT) {
|
|
|
|
insertedAt = index;
|
|
|
|
break;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
Span* span;
|
|
|
|
if (insertedAt >= 0) {
|
|
|
|
span = fTs.insert(insertedAt);
|
|
|
|
} else {
|
|
|
|
insertedAt = tCount;
|
|
|
|
span = fTs.append();
|
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
span->fT = newT;
|
2012-07-02 20:27:02 +00:00
|
|
|
span->fOther = other;
|
2012-06-01 17:44:28 +00:00
|
|
|
span->fPt = NULL;
|
2012-07-02 20:27:02 +00:00
|
|
|
span->fWindSum = SK_MinS32;
|
|
|
|
span->fWindValue = 1;
|
|
|
|
if ((span->fDone = newT == 1)) {
|
2012-05-23 18:09:25 +00:00
|
|
|
++fDoneSpans;
|
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
return insertedAt;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// set spans from start to end to decrement by one
|
|
|
|
// note this walks other backwards
|
|
|
|
// FIMXE: there's probably an edge case that can be constructed where
|
|
|
|
// two span in one segment are separated by float epsilon on one span but
|
|
|
|
// not the other, if one segment is very small. For this
|
|
|
|
// case the counts asserted below may or may not be enough to separate the
|
|
|
|
// spans. Even if the counts work out, what if the spanw aren't correctly
|
|
|
|
// sorted? It feels better in such a case to match the span's other span
|
|
|
|
// pointer since both coincident segments must contain the same spans.
|
|
|
|
void addTCancel(double startT, double endT, Segment& other,
|
|
|
|
double oStartT, double oEndT) {
|
|
|
|
SkASSERT(endT - startT >= FLT_EPSILON);
|
|
|
|
SkASSERT(oEndT - oStartT >= FLT_EPSILON);
|
|
|
|
int index = 0;
|
|
|
|
while (startT - fTs[index].fT >= FLT_EPSILON) {
|
|
|
|
++index;
|
|
|
|
}
|
2012-07-03 19:53:30 +00:00
|
|
|
int oIndex = other.fTs.count();
|
2012-07-02 20:27:02 +00:00
|
|
|
while (other.fTs[--oIndex].fT - oEndT > -FLT_EPSILON)
|
|
|
|
;
|
|
|
|
Span* test = &fTs[index];
|
|
|
|
Span* oTest = &other.fTs[oIndex];
|
|
|
|
do {
|
|
|
|
bool decrement = test->fWindValue && oTest->fWindValue;
|
|
|
|
Span* end = test;
|
|
|
|
do {
|
|
|
|
if (decrement) {
|
2012-07-03 19:53:30 +00:00
|
|
|
SkASSERT(end->fWindValue > 0);
|
2012-07-02 20:27:02 +00:00
|
|
|
if (--(end->fWindValue) == 0) {
|
|
|
|
end->fDone = true;
|
|
|
|
++fDoneSpans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end = &fTs[++index];
|
|
|
|
} while (end->fT - test->fT < FLT_EPSILON);
|
|
|
|
Span* oTestStart = oTest;
|
|
|
|
do {
|
|
|
|
if (decrement) {
|
2012-07-03 19:53:30 +00:00
|
|
|
SkASSERT(oTestStart->fWindValue > 0);
|
2012-07-02 20:27:02 +00:00
|
|
|
if (--(oTestStart->fWindValue) == 0) {
|
|
|
|
oTestStart->fDone = true;
|
|
|
|
++other.fDoneSpans;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!oIndex) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
oTestStart = &other.fTs[--oIndex];
|
|
|
|
} while (oTest->fT - oTestStart->fT < FLT_EPSILON);
|
|
|
|
test = end;
|
|
|
|
oTest = oTestStart;
|
|
|
|
} while (test->fT < endT - FLT_EPSILON);
|
|
|
|
SkASSERT(!oIndex || oTest->fT <= oStartT - FLT_EPSILON);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set spans from start to end to increment the greater by one and decrement
|
|
|
|
// the lesser
|
|
|
|
void addTCoincident(double startT, double endT, Segment& other,
|
|
|
|
double oStartT, double oEndT) {
|
|
|
|
SkASSERT(endT - startT >= FLT_EPSILON);
|
|
|
|
SkASSERT(oEndT - oStartT >= FLT_EPSILON);
|
|
|
|
int index = 0;
|
|
|
|
while (startT - fTs[index].fT >= FLT_EPSILON) {
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
int oIndex = 0;
|
|
|
|
while (oStartT - other.fTs[oIndex].fT >= FLT_EPSILON) {
|
|
|
|
++oIndex;
|
|
|
|
}
|
|
|
|
Span* test = &fTs[index];
|
|
|
|
Span* oTest = &other.fTs[oIndex];
|
|
|
|
SkTDArray<double> outsideTs;
|
|
|
|
SkTDArray<double> oOutsideTs;
|
|
|
|
do {
|
2012-07-03 19:53:30 +00:00
|
|
|
bool transfer = test->fWindValue && oTest->fWindValue;
|
2012-07-02 20:27:02 +00:00
|
|
|
bool decrementOther = test->fWindValue >= oTest->fWindValue;
|
|
|
|
Span* end = test;
|
|
|
|
double startT = end->fT;
|
|
|
|
double oStartT = oTest->fT;
|
|
|
|
do {
|
2012-07-03 19:53:30 +00:00
|
|
|
if (transfer) {
|
|
|
|
if (decrementOther) {
|
|
|
|
++(end->fWindValue);
|
|
|
|
} else {
|
|
|
|
SkASSERT(end->fWindValue > 0);
|
|
|
|
if (--(end->fWindValue) == 0) {
|
|
|
|
end->fDone = true;
|
|
|
|
++fDoneSpans;
|
|
|
|
int outCount = outsideTs.count();
|
|
|
|
if (outCount == 0 || end->fT - outsideTs[outCount - 2]
|
|
|
|
>= FLT_EPSILON) {
|
|
|
|
*outsideTs.append() = end->fT;
|
|
|
|
*outsideTs.append() = oStartT;
|
|
|
|
}
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end = &fTs[++index];
|
|
|
|
} while (end->fT - test->fT < FLT_EPSILON);
|
|
|
|
Span* oEnd = oTest;
|
|
|
|
do {
|
2012-07-03 19:53:30 +00:00
|
|
|
if (transfer) {
|
|
|
|
if (decrementOther) {
|
|
|
|
SkASSERT(oEnd->fWindValue > 0);
|
|
|
|
if (--(oEnd->fWindValue) == 0) {
|
|
|
|
oEnd->fDone = true;
|
|
|
|
++other.fDoneSpans;
|
|
|
|
int oOutCount = oOutsideTs.count();
|
|
|
|
if (oOutCount == 0 || oEnd->fT
|
|
|
|
- oOutsideTs[oOutCount - 2] >= FLT_EPSILON) {
|
|
|
|
*oOutsideTs.append() = oEnd->fT;
|
|
|
|
*oOutsideTs.append() = startT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++(oEnd->fWindValue);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
oEnd = &other.fTs[++oIndex];
|
|
|
|
} while (oEnd->fT - oTest->fT < FLT_EPSILON);
|
|
|
|
test = end;
|
|
|
|
oTest = oEnd;
|
|
|
|
} while (test->fT < endT - FLT_EPSILON);
|
|
|
|
SkASSERT(oTest->fT < oEndT + FLT_EPSILON);
|
|
|
|
SkASSERT(oTest->fT > oEndT - FLT_EPSILON);
|
|
|
|
if (!done() && outsideTs.count()) {
|
2012-07-03 19:53:30 +00:00
|
|
|
addTOutsides(outsideTs, other, oEndT);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
if (!other.done() && oOutsideTs.count()) {
|
2012-07-03 19:53:30 +00:00
|
|
|
other.addTOutsides(oOutsideTs, *this, endT);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:53:30 +00:00
|
|
|
void addTOutsides(const SkTDArray<double>& outsideTs, Segment& other,
|
2012-07-02 20:27:02 +00:00
|
|
|
double otherEnd) {
|
|
|
|
int count = outsideTs.count();
|
|
|
|
double endT = 0;
|
|
|
|
int endSpan = 0;
|
|
|
|
for (int index = 0; index < count; index += 2) {
|
|
|
|
double t = outsideTs[index];
|
|
|
|
double otherT = outsideTs[index + 1];
|
|
|
|
if (t > 1 - FLT_EPSILON) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (t - endT > FLT_EPSILON) {
|
2012-07-11 17:52:32 +00:00
|
|
|
endSpan = addTDonePair(t, other, otherT);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
endT = fTs[++endSpan].fT;
|
|
|
|
} while (endT - t < FLT_EPSILON);
|
|
|
|
}
|
|
|
|
addTPair(endT, other, otherEnd);
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
// match the other.fWindValue to its mates
|
|
|
|
int addTDonePair(double t, Segment& other, double otherT) {
|
|
|
|
int insertedAt = addTPair(t, other, otherT);
|
|
|
|
Span& end = fTs[insertedAt];
|
|
|
|
SkASSERT(end.fWindValue == 1);
|
|
|
|
end.fWindValue = 0;
|
|
|
|
end.fDone = true;
|
|
|
|
++fDoneSpans;
|
|
|
|
Span& otherEnd = other.fTs[end.fOtherIndex];
|
|
|
|
Span* match = NULL;
|
|
|
|
if (end.fOtherIndex > 0) {
|
|
|
|
match = &other.fTs[end.fOtherIndex - 1];
|
|
|
|
}
|
|
|
|
if (!match || match->fT < otherT) {
|
|
|
|
match = &other.fTs[end.fOtherIndex + 1];
|
|
|
|
}
|
|
|
|
otherEnd.fWindValue = match->fWindValue;
|
|
|
|
return insertedAt;
|
|
|
|
}
|
|
|
|
|
2012-07-03 19:53:30 +00:00
|
|
|
int addTPair(double t, Segment& other, double otherT) {
|
|
|
|
int insertedAt = addT(t, &other);
|
|
|
|
int otherInsertedAt = other.addT(otherT, this);
|
2012-07-02 20:27:02 +00:00
|
|
|
addOtherT(insertedAt, otherT, otherInsertedAt);
|
2012-07-03 19:53:30 +00:00
|
|
|
other.addOtherT(otherInsertedAt, t, insertedAt);
|
2012-07-02 20:27:02 +00:00
|
|
|
return insertedAt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addTwoAngles(int start, int end, SkTDArray<Angle>& angles) const {
|
2012-05-18 20:50:33 +00:00
|
|
|
// add edge leading into junction
|
2012-07-02 20:27:02 +00:00
|
|
|
if (fTs[SkMin32(end, start)].fWindValue > 0) {
|
|
|
|
addAngle(angles, end, start);
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
// add edge leading away from junction
|
2012-05-31 13:13:11 +00:00
|
|
|
int step = SkSign32(end - start);
|
2012-06-01 17:44:28 +00:00
|
|
|
int tIndex = nextSpan(end, step);
|
2012-07-02 20:27:02 +00:00
|
|
|
if (tIndex >= 0 && fTs[SkMin32(end, tIndex)].fWindValue > 0) {
|
2012-06-01 17:44:28 +00:00
|
|
|
addAngle(angles, end, tIndex);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
const Bounds& bounds() const {
|
|
|
|
return fBounds;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-06-01 17:44:28 +00:00
|
|
|
void buildAngles(int index, SkTDArray<Angle>& angles) const {
|
|
|
|
double referenceT = fTs[index].fT;
|
|
|
|
int lesser = index;
|
2012-07-02 20:27:02 +00:00
|
|
|
while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
|
2012-06-01 17:44:28 +00:00
|
|
|
buildAnglesInner(lesser, angles);
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
do {
|
2012-06-01 17:44:28 +00:00
|
|
|
buildAnglesInner(index, angles);
|
2012-07-02 20:27:02 +00:00
|
|
|
} while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
|
|
|
|
void buildAnglesInner(int index, SkTDArray<Angle>& angles) const {
|
|
|
|
Span* span = &fTs[index];
|
|
|
|
Segment* other = span->fOther;
|
|
|
|
// if there is only one live crossing, and no coincidence, continue
|
|
|
|
// in the same direction
|
|
|
|
// if there is coincidence, the only choice may be to reverse direction
|
|
|
|
// find edge on either side of intersection
|
|
|
|
int oIndex = span->fOtherIndex;
|
|
|
|
// if done == -1, prior span has already been processed
|
|
|
|
int step = 1;
|
2012-07-02 20:27:02 +00:00
|
|
|
int next = other->nextSpan(oIndex, step);
|
2012-06-01 17:44:28 +00:00
|
|
|
if (next < 0) {
|
|
|
|
step = -step;
|
2012-07-02 20:27:02 +00:00
|
|
|
next = other->nextSpan(oIndex, step);
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
|
|
|
// add candidate into and away from junction
|
|
|
|
other->addTwoAngles(next, oIndex, angles);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cancels(const Segment& other) const {
|
2012-07-03 19:53:30 +00:00
|
|
|
SkASSERT(fVerb == SkPath::kLine_Verb);
|
|
|
|
SkASSERT(other.fVerb == SkPath::kLine_Verb);
|
|
|
|
SkPoint dxy = fPts[0] - fPts[1];
|
|
|
|
SkPoint odxy = other.fPts[0] - other.fPts[1];
|
|
|
|
return dxy.fX * odxy.fX < 0 || dxy.fY * odxy.fY < 0;
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
// figure out if the segment's ascending T goes clockwise or not
|
|
|
|
// not enough context to write this as shown
|
|
|
|
// instead, add all segments meeting at the top
|
|
|
|
// sort them using buildAngleList
|
|
|
|
// find the first in the sort
|
|
|
|
// see if ascendingT goes to top
|
2012-05-23 18:09:25 +00:00
|
|
|
bool clockwise(int /* tIndex */) const {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkASSERT(0); // incomplete
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
int crossedSpan(const SkPoint& basePt, SkScalar& bestY, double& hitT) const {
|
|
|
|
int bestT = -1;
|
|
|
|
SkScalar top = bounds().fTop;
|
|
|
|
SkScalar bottom = bounds().fBottom;
|
2012-07-12 21:05:13 +00:00
|
|
|
int end = 0;
|
2012-07-02 20:27:02 +00:00
|
|
|
do {
|
2012-07-12 21:05:13 +00:00
|
|
|
int start = end;
|
2012-07-02 20:27:02 +00:00
|
|
|
end = nextSpan(start, 1);
|
|
|
|
SkPoint edge[4];
|
|
|
|
// OPTIMIZE: wrap this so that if start==0 end==fTCount-1 we can
|
|
|
|
// work with the original data directly
|
|
|
|
(*SegmentSubDivide[fVerb])(fPts, fTs[start].fT, fTs[end].fT, edge);
|
2012-07-11 17:52:32 +00:00
|
|
|
// intersect ray starting at basePt with edge
|
2012-07-02 20:27:02 +00:00
|
|
|
Intersections intersections;
|
|
|
|
int pts = (*VSegmentIntersect[fVerb])(edge, top, bottom, basePt.fX,
|
|
|
|
false, intersections);
|
|
|
|
if (pts == 0) {
|
|
|
|
continue;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
if (pts > 1 && fVerb == SkPath::kLine_Verb) {
|
|
|
|
// if the intersection is edge on, wait for another one
|
|
|
|
continue;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
SkASSERT(pts == 1); // FIXME: more code required to disambiguate
|
|
|
|
SkPoint pt;
|
|
|
|
double foundT = intersections.fT[0][0];
|
|
|
|
(*SegmentXYAtT[fVerb])(fPts, foundT, &pt);
|
|
|
|
if (bestY < pt.fY) {
|
|
|
|
bestY = pt.fY;
|
|
|
|
bestT = foundT < 1 ? start : end;
|
|
|
|
hitT = foundT;
|
|
|
|
}
|
|
|
|
} while (fTs[end].fT != 1);
|
|
|
|
return bestT;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
bool done() const {
|
2012-05-22 21:12:00 +00:00
|
|
|
SkASSERT(fDoneSpans <= fTs.count());
|
|
|
|
return fDoneSpans == fTs.count();
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// so the span needs to contain the pairing info found here
|
|
|
|
// this should include the winding computed for the edge, and
|
|
|
|
// what edge it connects to, and whether it is discarded
|
|
|
|
// (maybe discarded == abs(winding) > 1) ?
|
|
|
|
// only need derivatives for duration of sorting, add a new struct
|
|
|
|
// for pairings, remove extra spans that have zero length and
|
|
|
|
// reference an unused other
|
|
|
|
// for coincident, the last span on the other may be marked done
|
|
|
|
// (always?)
|
|
|
|
|
|
|
|
// if loop is exhausted, contour may be closed.
|
|
|
|
// FIXME: pass in close point so we can check for closure
|
|
|
|
|
|
|
|
// given a segment, and a sense of where 'inside' is, return the next
|
|
|
|
// segment. If this segment has an intersection, or ends in multiple
|
|
|
|
// segments, find the mate that continues the outside.
|
|
|
|
// note that if there are multiples, but no coincidence, we can limit
|
|
|
|
// choices to connections in the correct direction
|
|
|
|
|
|
|
|
// mark found segments as done
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
// start is the index of the beginning T of this edge
|
|
|
|
// it is guaranteed to have an end which describes a non-zero length (?)
|
|
|
|
// winding -1 means ccw, 1 means cw
|
2012-07-02 20:27:02 +00:00
|
|
|
// firstFind allows coincident edges to be treated differently
|
2012-07-11 17:52:32 +00:00
|
|
|
Segment* findNext(SkTDArray<Span*>& chase, int winding, const int startIndex,
|
|
|
|
const int endIndex,
|
|
|
|
int& nextStart, int& nextEnd, int& flipped, bool firstFind) {
|
2012-05-22 17:01:14 +00:00
|
|
|
SkASSERT(startIndex != endIndex);
|
2012-05-07 20:49:36 +00:00
|
|
|
int count = fTs.count();
|
2012-05-22 17:01:14 +00:00
|
|
|
SkASSERT(startIndex < endIndex ? startIndex < count - 1
|
|
|
|
: startIndex > 0);
|
2012-05-31 13:13:11 +00:00
|
|
|
int step = SkSign32(endIndex - startIndex);
|
2012-07-02 20:27:02 +00:00
|
|
|
int end = nextSpan(startIndex, step);
|
2012-05-22 17:01:14 +00:00
|
|
|
SkASSERT(end >= 0);
|
2012-05-18 20:50:33 +00:00
|
|
|
Span* endSpan = &fTs[end];
|
|
|
|
Segment* other;
|
2012-07-02 20:27:02 +00:00
|
|
|
if (isSimple(end)) {
|
2012-05-22 21:12:00 +00:00
|
|
|
// mark the smaller of startIndex, endIndex done, and all adjacent
|
|
|
|
// spans with the same T value (but not 'other' spans)
|
2012-05-31 13:13:11 +00:00
|
|
|
markDone(SkMin32(startIndex, endIndex), winding);
|
2012-05-18 20:50:33 +00:00
|
|
|
other = endSpan->fOther;
|
2012-05-31 13:13:11 +00:00
|
|
|
nextStart = endSpan->fOtherIndex;
|
|
|
|
nextEnd = nextStart + step;
|
|
|
|
SkASSERT(step < 0 ? nextEnd >= 0 : nextEnd < other->fTs.count());
|
2012-05-07 20:49:36 +00:00
|
|
|
return other;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
// more than one viable candidate -- measure angles to find best
|
2012-05-07 20:49:36 +00:00
|
|
|
SkTDArray<Angle> angles;
|
2012-05-22 21:12:00 +00:00
|
|
|
SkASSERT(startIndex - endIndex != 0);
|
|
|
|
SkASSERT((startIndex - endIndex < 0) ^ (step < 0));
|
2012-06-01 17:44:28 +00:00
|
|
|
addTwoAngles(startIndex, end, angles);
|
|
|
|
buildAngles(end, angles);
|
2012-05-18 20:50:33 +00:00
|
|
|
SkTDArray<Angle*> sorted;
|
|
|
|
sortAngles(angles, sorted);
|
|
|
|
int angleCount = angles.count();
|
2012-07-11 17:52:32 +00:00
|
|
|
int firstIndex = findStartingEdge(sorted, startIndex, end);
|
|
|
|
|
2012-05-22 17:01:14 +00:00
|
|
|
SkASSERT(firstIndex >= 0);
|
2012-05-31 13:13:11 +00:00
|
|
|
int startWinding = winding;
|
2012-07-02 20:27:02 +00:00
|
|
|
int nextIndex = firstIndex + 1;
|
|
|
|
int lastIndex = firstIndex != 0 ? firstIndex : angleCount;
|
|
|
|
const Angle* foundAngle = NULL;
|
|
|
|
// iterate through the angle, and compute everyone's winding
|
|
|
|
bool firstEdge = true;
|
2012-05-07 20:49:36 +00:00
|
|
|
do {
|
2012-07-02 20:27:02 +00:00
|
|
|
if (nextIndex == angleCount) {
|
2012-05-18 20:50:33 +00:00
|
|
|
nextIndex = 0;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
const Angle* nextAngle = sorted[nextIndex];
|
2012-05-31 13:13:11 +00:00
|
|
|
int maxWinding = winding;
|
2012-07-02 20:27:02 +00:00
|
|
|
Segment* nextSegment = nextAngle->segment();
|
|
|
|
int windValue = nextSegment->windValue(nextAngle);
|
|
|
|
SkASSERT(windValue > 0);
|
|
|
|
winding -= nextAngle->sign() * windValue;
|
2012-07-11 17:52:32 +00:00
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s maxWinding=%d winding=%d\n", __FUNCTION__, maxWinding,
|
|
|
|
winding);
|
|
|
|
#endif
|
|
|
|
if (maxWinding * winding < 0) {
|
|
|
|
flipped = -flipped;
|
|
|
|
SkDebugf("flipped sign %d %d\n", maxWinding, winding);
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
firstEdge = false;
|
2012-06-07 21:09:20 +00:00
|
|
|
if (!winding) {
|
2012-07-02 20:27:02 +00:00
|
|
|
if (!foundAngle) {
|
2012-07-12 21:05:13 +00:00
|
|
|
#if 0
|
|
|
|
nextAngle->segment()->markWinding(
|
|
|
|
SkMin32(nextAngle->start(), nextAngle->end()), maxWinding);
|
|
|
|
#endif
|
2012-07-02 20:27:02 +00:00
|
|
|
foundAngle = nextAngle;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
continue;
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
if (nextSegment->done()) {
|
2012-07-11 17:52:32 +00:00
|
|
|
continue;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
|
|
|
// if the winding is non-zero, nextAngle does not connect to
|
|
|
|
// current chain. If we haven't done so already, mark the angle
|
|
|
|
// as done, record the winding value, and mark connected unambiguous
|
|
|
|
// segments as well.
|
2012-07-11 17:52:32 +00:00
|
|
|
if (nextSegment->windSum(nextAngle) == SK_MinS32) {
|
2012-06-01 17:44:28 +00:00
|
|
|
if (abs(maxWinding) < abs(winding)) {
|
|
|
|
maxWinding = winding;
|
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* last;
|
2012-07-02 20:27:02 +00:00
|
|
|
if (foundAngle) {
|
2012-07-11 17:52:32 +00:00
|
|
|
last = nextSegment->markAndChaseWinding(nextAngle, maxWinding);
|
2012-07-02 20:27:02 +00:00
|
|
|
} else {
|
2012-07-11 17:52:32 +00:00
|
|
|
last = nextSegment->markAndChaseDone(nextAngle, maxWinding);
|
|
|
|
}
|
|
|
|
if (last) {
|
|
|
|
*chase.append() = last;
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
} while (++nextIndex != lastIndex);
|
2012-07-11 17:52:32 +00:00
|
|
|
sorted[firstIndex]->segment()->
|
|
|
|
markDone(SkMin32(startIndex, endIndex), startWinding);
|
2012-07-02 20:27:02 +00:00
|
|
|
if (!foundAngle) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nextStart = foundAngle->start();
|
|
|
|
nextEnd = foundAngle->end();
|
|
|
|
return foundAngle->segment();
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
|
|
|
|
int findStartingEdge(SkTDArray<Angle*>& sorted, int start, int end) {
|
|
|
|
int angleCount = sorted.count();
|
|
|
|
int firstIndex = -1;
|
|
|
|
for (int angleIndex = 0; angleIndex < angleCount; ++angleIndex) {
|
|
|
|
const Angle* angle = sorted[angleIndex];
|
|
|
|
if (angle->segment() == this && angle->start() == end &&
|
|
|
|
angle->end() == start) {
|
|
|
|
firstIndex = angleIndex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return firstIndex;
|
|
|
|
}
|
|
|
|
|
2012-05-22 17:01:14 +00:00
|
|
|
// FIXME: this is tricky code; needs its own unit test
|
2012-05-23 18:09:25 +00:00
|
|
|
void findTooCloseToCall(int /* winding */ ) { // FIXME: winding should be considered
|
2012-05-07 20:49:36 +00:00
|
|
|
int count = fTs.count();
|
|
|
|
if (count < 3) { // require t=0, x, 1 at minimum
|
|
|
|
return;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
int matchIndex = 0;
|
2012-05-07 20:49:36 +00:00
|
|
|
int moCount;
|
|
|
|
Span* match;
|
|
|
|
Segment* mOther;
|
|
|
|
do {
|
|
|
|
match = &fTs[matchIndex];
|
|
|
|
mOther = match->fOther;
|
|
|
|
moCount = mOther->fTs.count();
|
2012-05-22 17:01:14 +00:00
|
|
|
if (moCount >= 3) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (++matchIndex >= count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (true); // require t=0, x, 1 at minimum
|
2012-05-07 20:49:36 +00:00
|
|
|
// OPTIMIZATION: defer matchPt until qualifying toCount is found?
|
2012-06-01 17:44:28 +00:00
|
|
|
const SkPoint* matchPt = &xyAtT(match);
|
2012-04-30 19:38:50 +00:00
|
|
|
// look for a pair of nearby T values that map to the same (x,y) value
|
|
|
|
// if found, see if the pair of other segments share a common point. If
|
|
|
|
// so, the span from here to there is coincident.
|
2012-05-07 20:49:36 +00:00
|
|
|
for (int index = matchIndex + 1; index < count; ++index) {
|
|
|
|
Span* test = &fTs[index];
|
2012-07-02 20:27:02 +00:00
|
|
|
if (test->fDone) {
|
2012-05-31 13:13:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
Segment* tOther = test->fOther;
|
|
|
|
int toCount = tOther->fTs.count();
|
|
|
|
if (toCount < 3) { // require t=0, x, 1 at minimum
|
2012-04-30 19:38:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
const SkPoint* testPt = &xyAtT(test);
|
|
|
|
if (*matchPt != *testPt) {
|
2012-04-30 19:38:50 +00:00
|
|
|
matchIndex = index;
|
2012-05-07 20:49:36 +00:00
|
|
|
moCount = toCount;
|
|
|
|
match = test;
|
|
|
|
mOther = tOther;
|
2012-04-30 19:38:50 +00:00
|
|
|
matchPt = testPt;
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
int moStart = -1;
|
|
|
|
int moEnd = -1;
|
|
|
|
double moStartT, moEndT;
|
2012-04-30 19:38:50 +00:00
|
|
|
for (int moIndex = 0; moIndex < moCount; ++moIndex) {
|
2012-05-07 20:49:36 +00:00
|
|
|
Span& moSpan = mOther->fTs[moIndex];
|
2012-07-02 20:27:02 +00:00
|
|
|
if (moSpan.fDone) {
|
2012-05-31 13:13:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
if (moSpan.fOther == this) {
|
|
|
|
if (moSpan.fOtherT == match->fT) {
|
|
|
|
moStart = moIndex;
|
2012-05-22 17:01:14 +00:00
|
|
|
moStartT = moSpan.fT;
|
2012-05-07 20:49:36 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
if (moSpan.fOther == tOther) {
|
|
|
|
SkASSERT(moEnd == -1);
|
|
|
|
moEnd = moIndex;
|
|
|
|
moEndT = moSpan.fT;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
}
|
|
|
|
if (moStart < 0 || moEnd < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// FIXME: if moStartT, moEndT are initialized to NaN, can skip this test
|
|
|
|
if (moStartT == moEndT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int toStart = -1;
|
|
|
|
int toEnd = -1;
|
|
|
|
double toStartT, toEndT;
|
|
|
|
for (int toIndex = 0; toIndex < toCount; ++toIndex) {
|
|
|
|
Span& toSpan = tOther->fTs[toIndex];
|
|
|
|
if (toSpan.fOther == this) {
|
|
|
|
if (toSpan.fOtherT == test->fT) {
|
|
|
|
toStart = toIndex;
|
|
|
|
toStartT = toSpan.fT;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
continue;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
if (toSpan.fOther == mOther && toSpan.fOtherT == moEndT) {
|
|
|
|
SkASSERT(toEnd == -1);
|
|
|
|
toEnd = toIndex;
|
|
|
|
toEndT = toSpan.fT;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
// FIXME: if toStartT, toEndT are initialized to NaN, can skip this test
|
|
|
|
if (toStart <= 0 || toEnd <= 0) {
|
2012-04-26 21:01:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
if (toStartT == toEndT) {
|
2012-04-26 21:01:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
// test to see if the segment between there and here is linear
|
|
|
|
if (!mOther->isLinear(moStart, moEnd)
|
|
|
|
|| !tOther->isLinear(toStart, toEnd)) {
|
|
|
|
continue;
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
// FIXME: defer implementation until the rest works
|
|
|
|
// this may share code with regular coincident detection
|
|
|
|
SkASSERT(0);
|
|
|
|
#if 0
|
|
|
|
if (flipped) {
|
|
|
|
mOther->addTCancel(moStart, moEnd, tOther, tStart, tEnd);
|
|
|
|
} else {
|
|
|
|
mOther->addTCoincident(moStart, moEnd, tOther, tStart, tEnd);
|
|
|
|
}
|
|
|
|
#endif
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 19:38:50 +00:00
|
|
|
// OPTIMIZATION : for a pair of lines, can we compute points at T (cached)
|
|
|
|
// and use more concise logic like the old edge walker code?
|
|
|
|
// FIXME: this needs to deal with coincident edges
|
2012-05-22 17:01:14 +00:00
|
|
|
Segment* findTop(int& tIndex, int& endIndex) {
|
2012-04-26 21:01:06 +00:00
|
|
|
// iterate through T intersections and return topmost
|
|
|
|
// topmost tangent from y-min to first pt is closer to horizontal
|
2012-06-07 21:09:20 +00:00
|
|
|
SkASSERT(!done());
|
|
|
|
int firstT;
|
|
|
|
int lastT;
|
|
|
|
SkPoint topPt;
|
|
|
|
topPt.fY = SK_ScalarMax;
|
2012-04-26 21:01:06 +00:00
|
|
|
int count = fTs.count();
|
2012-07-02 20:27:02 +00:00
|
|
|
// see if either end is not done since we want smaller Y of the pair
|
2012-06-07 21:09:20 +00:00
|
|
|
bool lastDone = true;
|
|
|
|
for (int index = 0; index < count; ++index) {
|
2012-05-07 20:49:36 +00:00
|
|
|
const Span& span = fTs[index];
|
2012-06-07 21:09:20 +00:00
|
|
|
if (!span.fDone || !lastDone) {
|
|
|
|
const SkPoint& intercept = xyAtT(&span);
|
|
|
|
if (topPt.fY > intercept.fY || (topPt.fY == intercept.fY
|
|
|
|
&& topPt.fX > intercept.fX)) {
|
|
|
|
topPt = intercept;
|
2012-07-02 20:27:02 +00:00
|
|
|
firstT = lastT = index;
|
2012-06-07 21:09:20 +00:00
|
|
|
} else if (topPt == intercept) {
|
|
|
|
lastT = index;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-06-07 21:09:20 +00:00
|
|
|
lastDone = span.fDone;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
// sort the edges to find the leftmost
|
2012-06-01 17:44:28 +00:00
|
|
|
int step = 1;
|
|
|
|
int end = nextSpan(firstT, step);
|
2012-05-18 20:50:33 +00:00
|
|
|
if (end == -1) {
|
2012-06-01 17:44:28 +00:00
|
|
|
step = -1;
|
|
|
|
end = nextSpan(firstT, step);
|
2012-05-23 18:09:25 +00:00
|
|
|
SkASSERT(end != -1);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
// if the topmost T is not on end, or is three-way or more, find left
|
2012-04-30 19:38:50 +00:00
|
|
|
// look for left-ness from tLeft to firstT (matching y of other)
|
2012-05-18 20:50:33 +00:00
|
|
|
SkTDArray<Angle> angles;
|
|
|
|
SkASSERT(firstT - end != 0);
|
2012-07-02 20:27:02 +00:00
|
|
|
addTwoAngles(end, firstT, angles);
|
2012-06-01 17:44:28 +00:00
|
|
|
buildAngles(firstT, angles);
|
2012-05-18 20:50:33 +00:00
|
|
|
SkTDArray<Angle*> sorted;
|
|
|
|
sortAngles(angles, sorted);
|
2012-06-07 21:09:20 +00:00
|
|
|
// skip edges that have already been processed
|
|
|
|
firstT = -1;
|
|
|
|
Segment* leftSegment;
|
|
|
|
do {
|
|
|
|
const Angle* angle = sorted[++firstT];
|
|
|
|
leftSegment = angle->segment();
|
|
|
|
tIndex = angle->end();
|
|
|
|
endIndex = angle->start();
|
|
|
|
} while (leftSegment->fTs[SkMin32(tIndex, endIndex)].fDone);
|
2012-05-18 20:50:33 +00:00
|
|
|
return leftSegment;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
// FIXME: not crazy about this
|
|
|
|
// when the intersections are performed, the other index is into an
|
|
|
|
// incomplete array. as the array grows, the indices become incorrect
|
|
|
|
// while the following fixes the indices up again, it isn't smart about
|
|
|
|
// skipping segments whose indices are already correct
|
|
|
|
// assuming we leave the code that wrote the index in the first place
|
|
|
|
void fixOtherTIndex() {
|
|
|
|
int iCount = fTs.count();
|
|
|
|
for (int i = 0; i < iCount; ++i) {
|
|
|
|
Span& iSpan = fTs[i];
|
|
|
|
double oT = iSpan.fOtherT;
|
|
|
|
Segment* other = iSpan.fOther;
|
|
|
|
int oCount = other->fTs.count();
|
|
|
|
for (int o = 0; o < oCount; ++o) {
|
|
|
|
Span& oSpan = other->fTs[o];
|
|
|
|
if (oT == oSpan.fT && this == oSpan.fOther) {
|
|
|
|
iSpan.fOtherIndex = o;
|
2012-07-02 20:27:02 +00:00
|
|
|
break;
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 13:13:11 +00:00
|
|
|
// OPTIMIZATION: uses tail recursion. Unwise?
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* innerChaseDone(int index, int step, int winding) {
|
2012-07-02 20:27:02 +00:00
|
|
|
int end = nextSpan(index, step);
|
2012-07-12 19:29:45 +00:00
|
|
|
SkASSERT(end >= 0);
|
|
|
|
if (multipleSpans(end)) {
|
|
|
|
return &fTs[end];
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
const Span& endSpan = fTs[end];
|
|
|
|
Segment* other = endSpan.fOther;
|
|
|
|
index = endSpan.fOtherIndex;
|
|
|
|
int otherEnd = other->nextSpan(index, step);
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* last = other->innerChaseDone(index, step, winding);
|
2012-07-02 20:27:02 +00:00
|
|
|
other->markDone(SkMin32(index, otherEnd), winding);
|
2012-07-11 17:52:32 +00:00
|
|
|
return last;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* innerChaseWinding(int index, int step, int winding) {
|
2012-06-01 17:44:28 +00:00
|
|
|
int end = nextSpan(index, step);
|
2012-07-12 19:29:45 +00:00
|
|
|
SkASSERT(end >= 0);
|
|
|
|
if (multipleSpans(end)) {
|
|
|
|
return &fTs[end];
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-06-07 21:09:20 +00:00
|
|
|
const Span& endSpan = fTs[end];
|
|
|
|
Segment* other = endSpan.fOther;
|
|
|
|
index = endSpan.fOtherIndex;
|
2012-06-01 17:44:28 +00:00
|
|
|
int otherEnd = other->nextSpan(index, step);
|
2012-07-02 20:27:02 +00:00
|
|
|
int min = SkMin32(index, otherEnd);
|
|
|
|
if (other->fTs[min].fWindSum != SK_MinS32) {
|
|
|
|
SkASSERT(other->fTs[index].fWindSum == winding);
|
2012-07-11 17:52:32 +00:00
|
|
|
return NULL;
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* last = other->innerChaseWinding(index, step, winding);
|
2012-07-02 20:27:02 +00:00
|
|
|
other->markWinding(min, winding);
|
2012-07-11 17:52:32 +00:00
|
|
|
return last;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
void init(const SkPoint pts[], SkPath::Verb verb) {
|
|
|
|
fPts = pts;
|
|
|
|
fVerb = verb;
|
2012-05-22 21:12:00 +00:00
|
|
|
fDoneSpans = 0;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool intersected() const {
|
|
|
|
return fTs.count() > 0;
|
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
|
|
|
|
bool isLinear(int start, int end) const {
|
|
|
|
if (fVerb == SkPath::kLine_Verb) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (fVerb == SkPath::kQuad_Verb) {
|
|
|
|
SkPoint qPart[3];
|
|
|
|
QuadSubDivide(fPts, fTs[start].fT, fTs[end].fT, qPart);
|
|
|
|
return QuadIsLinear(qPart);
|
|
|
|
} else {
|
|
|
|
SkASSERT(fVerb == SkPath::kCubic_Verb);
|
|
|
|
SkPoint cPart[4];
|
|
|
|
CubicSubDivide(fPts, fTs[start].fT, fTs[end].fT, cPart);
|
|
|
|
return CubicIsLinear(cPart);
|
|
|
|
}
|
|
|
|
}
|
2012-07-03 19:53:30 +00:00
|
|
|
|
|
|
|
// OPTIMIZE: successive calls could start were the last leaves off
|
|
|
|
// or calls could specialize to walk forwards or backwards
|
|
|
|
bool isMissing(double startT) const {
|
|
|
|
size_t tCount = fTs.count();
|
|
|
|
for (size_t index = 0; index < tCount; ++index) {
|
|
|
|
if (fabs(startT - fTs[index].fT) < FLT_EPSILON) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
bool isSimple(int end) const {
|
2012-06-01 17:44:28 +00:00
|
|
|
int count = fTs.count();
|
|
|
|
if (count == 2) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
double t = fTs[end].fT;
|
|
|
|
if (t < FLT_EPSILON) {
|
|
|
|
return fTs[1].fT >= FLT_EPSILON;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
if (t > 1 - FLT_EPSILON) {
|
|
|
|
return fTs[count - 2].fT <= 1 - FLT_EPSILON;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
return false;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
|
|
|
|
bool isHorizontal() const {
|
|
|
|
return fBounds.fTop == fBounds.fBottom;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
bool isVertical() const {
|
|
|
|
return fBounds.fLeft == fBounds.fRight;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkScalar leftMost(int start, int end) const {
|
|
|
|
return (*SegmentLeftMost[fVerb])(fPts, fTs[start].fT, fTs[end].fT);
|
|
|
|
}
|
2012-05-22 21:12:00 +00:00
|
|
|
|
2012-05-31 13:13:11 +00:00
|
|
|
// this span is excluded by the winding rule -- chase the ends
|
|
|
|
// as long as they are unambiguous to mark connections as done
|
|
|
|
// and give them the same winding value
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* markAndChaseDone(const Angle* angle, int winding) {
|
2012-05-31 13:13:11 +00:00
|
|
|
int index = angle->start();
|
|
|
|
int endIndex = angle->end();
|
|
|
|
int step = SkSign32(endIndex - index);
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* last = innerChaseDone(index, step, winding);
|
2012-05-31 13:13:11 +00:00
|
|
|
markDone(SkMin32(index, endIndex), winding);
|
2012-07-11 17:52:32 +00:00
|
|
|
return last;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* markAndChaseWinding(const Angle* angle, int winding) {
|
2012-07-02 20:27:02 +00:00
|
|
|
int index = angle->start();
|
|
|
|
int endIndex = angle->end();
|
|
|
|
int min = SkMin32(index, endIndex);
|
|
|
|
int step = SkSign32(endIndex - index);
|
2012-07-11 17:52:32 +00:00
|
|
|
Span* last = innerChaseWinding(index, step, winding);
|
2012-07-02 20:27:02 +00:00
|
|
|
markWinding(min, winding);
|
2012-07-11 17:52:32 +00:00
|
|
|
return last;
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 13:13:11 +00:00
|
|
|
// FIXME: this should also mark spans with equal (x,y)
|
2012-07-02 20:27:02 +00:00
|
|
|
// This may be called when the segment is already marked done. While this
|
|
|
|
// wastes time, it shouldn't do any more than spin through the T spans.
|
|
|
|
// OPTIMIZATION: abort on first done found (assuming that this code is
|
|
|
|
// always called to mark segments done).
|
2012-05-31 13:13:11 +00:00
|
|
|
void markDone(int index, int winding) {
|
2012-07-02 20:27:02 +00:00
|
|
|
// SkASSERT(!done());
|
2012-05-22 21:12:00 +00:00
|
|
|
double referenceT = fTs[index].fT;
|
|
|
|
int lesser = index;
|
2012-07-02 20:27:02 +00:00
|
|
|
while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
|
2012-05-31 13:13:11 +00:00
|
|
|
Span& span = fTs[lesser];
|
2012-06-07 21:09:20 +00:00
|
|
|
if (span.fDone) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
#if DEBUG_MARK_DONE
|
|
|
|
const SkPoint& pt = xyAtT(&span);
|
|
|
|
SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
|
|
|
|
__FUNCTION__, fID, lesser, span.fT, pt.fX, pt.fY, winding);
|
|
|
|
#endif
|
2012-06-07 21:09:20 +00:00
|
|
|
span.fDone = true;
|
2012-07-02 20:27:02 +00:00
|
|
|
SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
|
|
|
|
span.fWindSum = winding;
|
2012-05-23 18:09:25 +00:00
|
|
|
fDoneSpans++;
|
2012-05-22 21:12:00 +00:00
|
|
|
}
|
|
|
|
do {
|
2012-05-31 13:13:11 +00:00
|
|
|
Span& span = fTs[index];
|
2012-07-02 20:27:02 +00:00
|
|
|
// SkASSERT(!span.fDone);
|
2012-06-07 21:09:20 +00:00
|
|
|
if (span.fDone) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
#if DEBUG_MARK_DONE
|
|
|
|
const SkPoint& pt = xyAtT(&span);
|
|
|
|
SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
|
|
|
|
__FUNCTION__, fID, index, span.fT, pt.fX, pt.fY, winding);
|
|
|
|
#endif
|
2012-05-31 13:13:11 +00:00
|
|
|
span.fDone = true;
|
2012-07-02 20:27:02 +00:00
|
|
|
SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
|
|
|
|
span.fWindSum = winding;
|
2012-05-23 18:09:25 +00:00
|
|
|
fDoneSpans++;
|
2012-07-02 20:27:02 +00:00
|
|
|
} while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
|
|
|
|
}
|
|
|
|
|
|
|
|
void markWinding(int index, int winding) {
|
|
|
|
SkASSERT(!done());
|
|
|
|
double referenceT = fTs[index].fT;
|
|
|
|
int lesser = index;
|
|
|
|
while (--lesser >= 0 && referenceT - fTs[lesser].fT < FLT_EPSILON) {
|
|
|
|
Span& span = fTs[lesser];
|
|
|
|
if (span.fDone) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkASSERT(span.fWindValue == 1 || winding == 0);
|
|
|
|
SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
|
|
|
|
#if DEBUG_MARK_DONE
|
|
|
|
const SkPoint& pt = xyAtT(&span);
|
|
|
|
SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
|
|
|
|
__FUNCTION__, fID, lesser, span.fT, pt.fX, pt.fY, winding);
|
|
|
|
#endif
|
|
|
|
span.fWindSum = winding;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
Span& span = fTs[index];
|
|
|
|
// SkASSERT(!span.fDone || span.fCoincident);
|
|
|
|
if (span.fDone) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkASSERT(span.fWindValue == 1 || winding == 0);
|
|
|
|
SkASSERT(span.fWindSum == SK_MinS32 || span.fWindSum == winding);
|
|
|
|
#if DEBUG_MARK_DONE
|
|
|
|
const SkPoint& pt = xyAtT(&span);
|
|
|
|
SkDebugf("%s segment=%d index=%d t=%1.9g pt=(%1.9g,%1.9g) wind=%d\n",
|
|
|
|
__FUNCTION__, fID, index, span.fT, pt.fX, pt.fY, winding);
|
|
|
|
#endif
|
|
|
|
span.fWindSum = winding;
|
|
|
|
} while (++index < fTs.count() && fTs[index].fT - referenceT < FLT_EPSILON);
|
2012-05-22 21:12:00 +00:00
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-07-12 19:29:45 +00:00
|
|
|
// return span if when chasing, two or more radiating spans are not done
|
|
|
|
// OPTIMIZATION: ? multiple spans is detected when there is only one valid
|
|
|
|
// candidate and the remaining spans have windValue == 0 (canceled by
|
|
|
|
// coincidence). The coincident edges could either be removed altogether,
|
|
|
|
// or this code could be more complicated in detecting this case. Worth it?
|
|
|
|
bool multipleSpans(int end) const {
|
|
|
|
return end > 0 && end < fTs.count() - 1;
|
2012-06-07 21:09:20 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:44:28 +00:00
|
|
|
// This has callers for two different situations: one establishes the end
|
|
|
|
// of the current span, and one establishes the beginning of the next span
|
|
|
|
// (thus the name). When this is looking for the end of the current span,
|
|
|
|
// coincidence is found when the beginning Ts contain -step and the end
|
|
|
|
// contains step. When it is looking for the beginning of the next, the
|
|
|
|
// first Ts found can be ignored and the last Ts should contain -step.
|
2012-07-02 20:27:02 +00:00
|
|
|
// OPTIMIZATION: probably should split into two functions
|
2012-06-01 17:44:28 +00:00
|
|
|
int nextSpan(int from, int step) const {
|
2012-05-31 13:13:11 +00:00
|
|
|
const Span& fromSpan = fTs[from];
|
|
|
|
int count = fTs.count();
|
|
|
|
int to = from;
|
|
|
|
while (step > 0 ? ++to < count : --to >= 0) {
|
|
|
|
const Span& span = fTs[to];
|
2012-07-02 20:27:02 +00:00
|
|
|
if ((step > 0 ? span.fT - fromSpan.fT : fromSpan.fT - span.fT) < FLT_EPSILON) {
|
2012-05-31 13:13:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
const SkPoint* pts() const {
|
|
|
|
return fPts;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void reset() {
|
2012-05-18 20:50:33 +00:00
|
|
|
init(NULL, (SkPath::Verb) -1);
|
2012-04-26 21:01:06 +00:00
|
|
|
fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
|
|
|
|
fTs.reset();
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:44:28 +00:00
|
|
|
// OPTIMIZATION: mark as debugging only if used solely by tests
|
|
|
|
const Span& span(int tIndex) const {
|
|
|
|
return fTs[tIndex];
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
|
|
|
|
int spanSign(int startIndex, int endIndex) const {
|
|
|
|
return startIndex < endIndex ? -fTs[startIndex].fWindValue :
|
|
|
|
fTs[endIndex].fWindValue;
|
|
|
|
}
|
2012-06-01 17:44:28 +00:00
|
|
|
|
2012-05-22 17:01:14 +00:00
|
|
|
// OPTIMIZATION: mark as debugging only if used solely by tests
|
2012-04-26 21:01:06 +00:00
|
|
|
double t(int tIndex) const {
|
|
|
|
return fTs[tIndex].fT;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
|
|
|
|
void updatePts(const SkPoint pts[]) {
|
|
|
|
fPts = pts;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkPath::Verb verb() const {
|
|
|
|
return fVerb;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
int windSum(int tIndex) const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return fTs[tIndex].fWindSum;
|
|
|
|
}
|
2012-05-31 13:13:11 +00:00
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
int windSum(const Angle* angle) const {
|
2012-05-31 13:13:11 +00:00
|
|
|
int start = angle->start();
|
|
|
|
int end = angle->end();
|
|
|
|
int index = SkMin32(start, end);
|
2012-07-11 17:52:32 +00:00
|
|
|
return windSum(index);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int windValue(int tIndex) const {
|
|
|
|
return fTs[tIndex].fWindValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int windValue(const Angle* angle) const {
|
|
|
|
int start = angle->start();
|
|
|
|
int end = angle->end();
|
|
|
|
int index = SkMin32(start, end);
|
|
|
|
return windValue(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar xAtT(const Span* span) const {
|
|
|
|
return xyAtT(span).fX;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
const SkPoint& xyAtT(int index) const {
|
|
|
|
return xyAtT(&fTs[index]);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 17:44:28 +00:00
|
|
|
const SkPoint& xyAtT(const Span* span) const {
|
|
|
|
if (!span->fPt) {
|
|
|
|
if (span->fT == 0) {
|
|
|
|
span->fPt = &fPts[0];
|
|
|
|
} else if (span->fT == 1) {
|
|
|
|
span->fPt = &fPts[fVerb];
|
|
|
|
} else {
|
|
|
|
SkPoint* pt = fIntersections.append();
|
|
|
|
(*SegmentXYAtT[fVerb])(fPts, span->fT, pt);
|
|
|
|
span->fPt = pt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *span->fPt;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
|
|
|
|
SkScalar yAtT(int index) const {
|
|
|
|
return yAtT(&fTs[index]);
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
SkScalar yAtT(const Span* span) const {
|
|
|
|
return xyAtT(span).fY;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
#if DEBUG_DUMP
|
|
|
|
void dump() const {
|
|
|
|
const char className[] = "Segment";
|
|
|
|
const int tab = 4;
|
|
|
|
for (int i = 0; i < fTs.count(); ++i) {
|
|
|
|
SkPoint out;
|
|
|
|
(*SegmentXYAtT[fVerb])(fPts, t(i), &out);
|
|
|
|
SkDebugf("%*s [%d] %s.fTs[%d]=%1.9g (%1.9g,%1.9g) other=%d"
|
2012-07-02 20:27:02 +00:00
|
|
|
" otherT=%1.9g windSum=%d\n",
|
2012-04-26 21:01:06 +00:00
|
|
|
tab + sizeof(className), className, fID,
|
|
|
|
kLVerbStr[fVerb], i, fTs[i].fT, out.fX, out.fY,
|
2012-07-02 20:27:02 +00:00
|
|
|
fTs[i].fOther->fID, fTs[i].fOtherT, fTs[i].fWindSum);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
SkDebugf("%*s [%d] fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)",
|
2012-04-26 21:01:06 +00:00
|
|
|
tab + sizeof(className), className, fID,
|
2012-05-07 20:49:36 +00:00
|
|
|
fBounds.fLeft, fBounds.fTop, fBounds.fRight, fBounds.fBottom);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-12 12:52:50 +00:00
|
|
|
#if DEBUG_ACTIVE_SPANS
|
|
|
|
void debugShowActiveSpans(int contourID, int segmentIndex) {
|
|
|
|
if (done()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < fTs.count(); ++i) {
|
|
|
|
if (fTs[i].fDone) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkDebugf("%s contour=%d segment=%d (%d)", __FUNCTION__, contourID,
|
|
|
|
segmentIndex, fID);
|
|
|
|
SkDebugf(" (%1.9g,%1.9g", fPts[0].fX, fPts[0].fY);
|
|
|
|
for (int vIndex = 1; vIndex <= fVerb; ++vIndex) {
|
|
|
|
SkDebugf(" %1.9g,%1.9g", fPts[vIndex].fX, fPts[vIndex].fY);
|
|
|
|
}
|
|
|
|
const Span* span = &fTs[i];
|
|
|
|
SkDebugf(") fT=%d (%1.9g) (%1.9g,%1.9g)", i, fTs[i].fT,
|
|
|
|
xAtT(span), yAtT(i));
|
|
|
|
const Segment* other = fTs[i].fOther;
|
|
|
|
SkDebugf(" other=%d otherT=%1.9g otherIndex=%d", other->fID,
|
|
|
|
fTs[i].fOtherT, fTs[i].fOtherIndex);
|
|
|
|
SkDebugf(" windSum=%d windValue=%d\n", fTs[i].fWindSum,
|
|
|
|
fTs[i].fWindValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
private:
|
|
|
|
const SkPoint* fPts;
|
|
|
|
SkPath::Verb fVerb;
|
|
|
|
Bounds fBounds;
|
2012-05-07 20:49:36 +00:00
|
|
|
SkTDArray<Span> fTs; // two or more (always includes t=0 t=1)
|
2012-06-01 17:44:28 +00:00
|
|
|
// OPTIMIZATION:if intersections array is a pointer, the it could only
|
|
|
|
// be allocated as needed instead of always initialized -- though maybe
|
|
|
|
// the initialization is lightweight enough that it hardly matters
|
|
|
|
mutable SkTDArray<SkPoint> fIntersections;
|
2012-05-22 21:12:00 +00:00
|
|
|
int fDoneSpans; // used for quick check that segment is finished
|
2012-04-26 21:01:06 +00:00
|
|
|
#if DEBUG_DUMP
|
|
|
|
int fID;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2012-07-03 19:53:30 +00:00
|
|
|
class Contour;
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
struct Coincidence {
|
2012-07-03 19:53:30 +00:00
|
|
|
Contour* fContours[2];
|
|
|
|
int fSegments[2];
|
2012-07-02 20:27:02 +00:00
|
|
|
double fTs[2][2];
|
|
|
|
};
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
class Contour {
|
|
|
|
public:
|
|
|
|
Contour() {
|
|
|
|
reset();
|
|
|
|
#if DEBUG_DUMP
|
|
|
|
fID = ++gContourID;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Contour& rh) const {
|
|
|
|
return fBounds.fTop == rh.fBounds.fTop
|
|
|
|
? fBounds.fLeft < rh.fBounds.fLeft
|
|
|
|
: fBounds.fTop < rh.fBounds.fTop;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
void addCoincident(int index, Contour* other, int otherIndex,
|
|
|
|
const Intersections& ts, bool swap) {
|
|
|
|
Coincidence& coincidence = *fCoincidences.append();
|
2012-07-03 19:53:30 +00:00
|
|
|
coincidence.fContours[0] = this;
|
|
|
|
coincidence.fContours[1] = other;
|
|
|
|
coincidence.fSegments[0] = index;
|
|
|
|
coincidence.fSegments[1] = otherIndex;
|
2012-07-02 20:27:02 +00:00
|
|
|
coincidence.fTs[swap][0] = ts.fT[0][0];
|
|
|
|
coincidence.fTs[swap][1] = ts.fT[0][1];
|
|
|
|
coincidence.fTs[!swap][0] = ts.fT[1][0];
|
|
|
|
coincidence.fTs[!swap][1] = ts.fT[1][1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void addCross(const Contour* crosser) {
|
|
|
|
#ifdef DEBUG_CROSS
|
|
|
|
for (int index = 0; index < fCrosses.count(); ++index) {
|
|
|
|
SkASSERT(fCrosses[index] != crosser);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*fCrosses.append() = crosser;
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void addCubic(const SkPoint pts[4]) {
|
|
|
|
fSegments.push_back().addCubic(pts);
|
|
|
|
fContainsCurves = true;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
int addLine(const SkPoint pts[2]) {
|
2012-04-26 21:01:06 +00:00
|
|
|
fSegments.push_back().addLine(pts);
|
2012-05-18 20:50:33 +00:00
|
|
|
return fSegments.count();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
|
|
|
|
void addOtherT(int segIndex, int tIndex, double otherT, int otherIndex) {
|
|
|
|
fSegments[segIndex].addOtherT(tIndex, otherT, otherIndex);
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
int addQuad(const SkPoint pts[3]) {
|
2012-04-26 21:01:06 +00:00
|
|
|
fSegments.push_back().addQuad(pts);
|
|
|
|
fContainsCurves = true;
|
2012-05-18 20:50:33 +00:00
|
|
|
return fSegments.count();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
int addT(int segIndex, double newT, Contour* other, int otherIndex) {
|
|
|
|
containsIntercepts();
|
|
|
|
return fSegments[segIndex].addT(newT, &other->fSegments[otherIndex]);
|
|
|
|
}
|
|
|
|
|
2012-04-30 19:38:50 +00:00
|
|
|
const Bounds& bounds() const {
|
2012-04-26 21:01:06 +00:00
|
|
|
return fBounds;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void complete() {
|
|
|
|
setBounds();
|
|
|
|
fContainsIntercepts = false;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void containsIntercepts() {
|
|
|
|
fContainsIntercepts = true;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
const Segment* crossedSegment(const SkPoint& basePt, SkScalar& bestY,
|
|
|
|
int &tIndex, double& hitT) {
|
|
|
|
int segmentCount = fSegments.count();
|
|
|
|
const Segment* bestSegment = NULL;
|
|
|
|
for (int test = 0; test < segmentCount; ++test) {
|
|
|
|
Segment* testSegment = &fSegments[test];
|
|
|
|
const SkRect& bounds = testSegment->bounds();
|
|
|
|
if (bounds.fTop < bestY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bounds.fTop > basePt.fY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bounds.fLeft > basePt.fX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bounds.fRight < basePt.fX) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
double testHitT;
|
|
|
|
int testT = testSegment->crossedSpan(basePt, bestY, testHitT);
|
|
|
|
if (testT >= 0) {
|
|
|
|
bestSegment = testSegment;
|
|
|
|
tIndex = testT;
|
|
|
|
hitT = testHitT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestSegment;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool crosses(const Contour* crosser) const {
|
|
|
|
if (this == crosser) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (int index = 0; index < fCrosses.count(); ++index) {
|
|
|
|
if (fCrosses[index] == crosser) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-30 19:38:50 +00:00
|
|
|
void findTooCloseToCall(int winding) {
|
|
|
|
int segmentCount = fSegments.count();
|
|
|
|
for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
|
|
|
|
fSegments[sIndex].findTooCloseToCall(winding);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
void fixOtherTIndex() {
|
|
|
|
int segmentCount = fSegments.count();
|
|
|
|
for (int sIndex = 0; sIndex < segmentCount; ++sIndex) {
|
|
|
|
fSegments[sIndex].fixOtherTIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void reset() {
|
|
|
|
fSegments.reset();
|
|
|
|
fBounds.set(SK_ScalarMax, SK_ScalarMax, SK_ScalarMax, SK_ScalarMax);
|
2012-05-07 20:49:36 +00:00
|
|
|
fContainsCurves = fContainsIntercepts = false;
|
2012-07-03 14:30:08 +00:00
|
|
|
fWindingSum = SK_MinS32;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-03 19:53:30 +00:00
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
void resolveCoincidence(int winding) {
|
|
|
|
int count = fCoincidences.count();
|
|
|
|
for (int index = 0; index < count; ++index) {
|
|
|
|
Coincidence& coincidence = fCoincidences[index];
|
2012-07-03 19:53:30 +00:00
|
|
|
Contour* thisContour = coincidence.fContours[0];
|
|
|
|
Contour* otherContour = coincidence.fContours[1];
|
|
|
|
int thisIndex = coincidence.fSegments[0];
|
|
|
|
int otherIndex = coincidence.fSegments[1];
|
|
|
|
Segment& thisOne = thisContour->fSegments[thisIndex];
|
|
|
|
Segment& other = otherContour->fSegments[otherIndex];
|
2012-07-02 20:27:02 +00:00
|
|
|
double startT = coincidence.fTs[0][0];
|
|
|
|
double endT = coincidence.fTs[0][1];
|
|
|
|
if (startT > endT) {
|
|
|
|
SkTSwap<double>(startT, endT);
|
|
|
|
}
|
|
|
|
SkASSERT(endT - startT >= FLT_EPSILON);
|
|
|
|
double oStartT = coincidence.fTs[1][0];
|
|
|
|
double oEndT = coincidence.fTs[1][1];
|
|
|
|
if (oStartT > oEndT) {
|
|
|
|
SkTSwap<double>(oStartT, oEndT);
|
|
|
|
}
|
|
|
|
SkASSERT(oEndT - oStartT >= FLT_EPSILON);
|
2012-07-03 19:53:30 +00:00
|
|
|
if (winding > 0 || thisOne.cancels(other)) {
|
|
|
|
// make sure startT and endT have t entries
|
|
|
|
if (thisOne.isMissing(startT) || other.isMissing(oEndT)) {
|
|
|
|
thisOne.addTPair(startT, other, oEndT);
|
|
|
|
}
|
|
|
|
if (thisOne.isMissing(endT) || other.isMissing(oStartT)) {
|
|
|
|
other.addTPair(oStartT, thisOne, endT);
|
|
|
|
}
|
|
|
|
thisOne.addTCancel(startT, endT, other, oStartT, oEndT);
|
2012-07-02 20:27:02 +00:00
|
|
|
} else {
|
2012-07-03 19:53:30 +00:00
|
|
|
if (thisOne.isMissing(startT) || other.isMissing(oStartT)) {
|
|
|
|
thisOne.addTPair(startT, other, oStartT);
|
|
|
|
}
|
|
|
|
if (thisOne.isMissing(endT) || other.isMissing(oEndT)) {
|
|
|
|
other.addTPair(oEndT, thisOne, endT);
|
|
|
|
}
|
|
|
|
thisOne.addTCoincident(startT, endT, other, oStartT, oEndT);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkTArray<Segment>& segments() {
|
|
|
|
return fSegments;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWinding(int winding) {
|
|
|
|
SkASSERT(fWindingSum < 0);
|
|
|
|
fWindingSum = winding;
|
|
|
|
}
|
|
|
|
|
2012-05-07 20:49:36 +00:00
|
|
|
// OPTIMIZATION: feel pretty uneasy about this. It seems like once again
|
|
|
|
// we need to sort and walk edges in y, but that on the surface opens the
|
|
|
|
// same can of worms as before. But then, this is a rough sort based on
|
|
|
|
// segments' top, and not a true sort, so it could be ameniable to regular
|
|
|
|
// sorting instead of linear searching. Still feel like I'm missing something
|
2012-07-02 20:27:02 +00:00
|
|
|
Segment* topSegment(SkScalar& bestY) {
|
2012-05-07 20:49:36 +00:00
|
|
|
int segmentCount = fSegments.count();
|
|
|
|
SkASSERT(segmentCount > 0);
|
|
|
|
int best = -1;
|
|
|
|
Segment* bestSegment = NULL;
|
|
|
|
while (++best < segmentCount) {
|
|
|
|
Segment* testSegment = &fSegments[best];
|
|
|
|
if (testSegment->done()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bestSegment = testSegment;
|
|
|
|
break;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
if (!bestSegment) {
|
|
|
|
return NULL;
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
SkScalar bestTop = bestSegment->activeTop();
|
2012-05-07 20:49:36 +00:00
|
|
|
for (int test = best + 1; test < segmentCount; ++test) {
|
|
|
|
Segment* testSegment = &fSegments[test];
|
|
|
|
if (testSegment->done()) {
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
if (testSegment->bounds().fTop > bestTop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkScalar testTop = testSegment->activeTop();
|
2012-05-07 20:49:36 +00:00
|
|
|
if (bestTop > testTop) {
|
|
|
|
bestTop = testTop;
|
|
|
|
bestSegment = testSegment;
|
|
|
|
}
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
bestY = bestTop;
|
2012-05-07 20:49:36 +00:00
|
|
|
return bestSegment;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
int updateSegment(int index, const SkPoint* pts) {
|
|
|
|
Segment& segment = fSegments[index];
|
|
|
|
segment.updatePts(pts);
|
|
|
|
return segment.verb() + 1;
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
int windSum() {
|
2012-07-02 20:27:02 +00:00
|
|
|
if (fWindingSum >= 0) {
|
|
|
|
return fWindingSum;
|
|
|
|
}
|
|
|
|
// check peers
|
|
|
|
int count = fCrosses.count();
|
|
|
|
for (int index = 0; index < count; ++index) {
|
|
|
|
const Contour* crosser = fCrosses[index];
|
|
|
|
if (0 <= crosser->fWindingSum) {
|
|
|
|
fWindingSum = crosser->fWindingSum;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fWindingSum;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_TEST
|
|
|
|
SkTArray<Segment>& debugSegments() {
|
|
|
|
return fSegments;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
#if DEBUG_DUMP
|
|
|
|
void dump() {
|
|
|
|
int i;
|
|
|
|
const char className[] = "Contour";
|
|
|
|
const int tab = 4;
|
|
|
|
SkDebugf("%s %p (contour=%d)\n", className, this, fID);
|
|
|
|
for (i = 0; i < fSegments.count(); ++i) {
|
|
|
|
SkDebugf("%*s.fSegments[%d]:\n", tab + sizeof(className),
|
|
|
|
className, i);
|
|
|
|
fSegments[i].dump();
|
|
|
|
}
|
|
|
|
SkDebugf("%*s.fBounds=(l:%1.9g, t:%1.9g r:%1.9g, b:%1.9g)\n",
|
|
|
|
tab + sizeof(className), className,
|
|
|
|
fBounds.fLeft, fBounds.fTop,
|
|
|
|
fBounds.fRight, fBounds.fBottom);
|
|
|
|
SkDebugf("%*s.fContainsIntercepts=%d\n", tab + sizeof(className),
|
|
|
|
className, fContainsIntercepts);
|
|
|
|
SkDebugf("%*s.fContainsCurves=%d\n", tab + sizeof(className),
|
|
|
|
className, fContainsCurves);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-07-12 12:52:50 +00:00
|
|
|
#if DEBUG_ACTIVE_SPANS
|
|
|
|
void debugShowActiveSpans() {
|
|
|
|
for (int index = 0; index < fSegments.count(); ++index) {
|
|
|
|
fSegments[index].debugShowActiveSpans(fID, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
protected:
|
|
|
|
void setBounds() {
|
|
|
|
int count = fSegments.count();
|
|
|
|
if (count == 0) {
|
|
|
|
SkDebugf("%s empty contour\n", __FUNCTION__);
|
|
|
|
SkASSERT(0);
|
|
|
|
// FIXME: delete empty contour?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fBounds = fSegments.front().bounds();
|
|
|
|
for (int index = 1; index < count; ++index) {
|
2012-07-02 20:27:02 +00:00
|
|
|
fBounds.add(fSegments[index].bounds());
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
private:
|
2012-07-02 20:27:02 +00:00
|
|
|
SkTArray<Segment> fSegments;
|
|
|
|
SkTDArray<Coincidence> fCoincidences;
|
|
|
|
SkTDArray<const Contour*> fCrosses;
|
2012-04-26 21:01:06 +00:00
|
|
|
Bounds fBounds;
|
|
|
|
bool fContainsIntercepts;
|
|
|
|
bool fContainsCurves;
|
2012-07-02 20:27:02 +00:00
|
|
|
int fWindingSum; // initial winding number outside
|
2012-04-26 21:01:06 +00:00
|
|
|
#if DEBUG_DUMP
|
|
|
|
int fID;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
class EdgeBuilder {
|
|
|
|
public:
|
|
|
|
|
2012-04-30 19:38:50 +00:00
|
|
|
EdgeBuilder(const SkPath& path, SkTArray<Contour>& contours)
|
2012-04-26 21:01:06 +00:00
|
|
|
: fPath(path)
|
|
|
|
, fCurrentContour(NULL)
|
|
|
|
, fContours(contours)
|
|
|
|
{
|
|
|
|
#if DEBUG_DUMP
|
|
|
|
gContourID = 0;
|
|
|
|
gSegmentID = 0;
|
|
|
|
#endif
|
|
|
|
walk();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
void complete() {
|
2012-07-02 20:27:02 +00:00
|
|
|
if (fCurrentContour && fCurrentContour->segments().count()) {
|
2012-04-26 21:01:06 +00:00
|
|
|
fCurrentContour->complete();
|
|
|
|
fCurrentContour = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void walk() {
|
|
|
|
// FIXME:remove once we can access path pts directly
|
|
|
|
SkPath::RawIter iter(fPath); // FIXME: access path directly when allowed
|
|
|
|
SkPoint pts[4];
|
|
|
|
SkPath::Verb verb;
|
|
|
|
do {
|
|
|
|
verb = iter.next(pts);
|
|
|
|
*fPathVerbs.append() = verb;
|
|
|
|
if (verb == SkPath::kMove_Verb) {
|
|
|
|
*fPathPts.append() = pts[0];
|
|
|
|
} else if (verb >= SkPath::kLine_Verb && verb <= SkPath::kCubic_Verb) {
|
|
|
|
fPathPts.append(verb, &pts[1]);
|
|
|
|
}
|
|
|
|
} while (verb != SkPath::kDone_Verb);
|
|
|
|
// FIXME: end of section to remove once path pts are accessed directly
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkPath::Verb reducedVerb;
|
|
|
|
uint8_t* verbPtr = fPathVerbs.begin();
|
|
|
|
const SkPoint* pointsPtr = fPathPts.begin();
|
2012-05-18 20:50:33 +00:00
|
|
|
const SkPoint* finalCurveStart = NULL;
|
|
|
|
const SkPoint* finalCurveEnd = NULL;
|
2012-04-26 21:01:06 +00:00
|
|
|
while ((verb = (SkPath::Verb) *verbPtr++) != SkPath::kDone_Verb) {
|
|
|
|
switch (verb) {
|
|
|
|
case SkPath::kMove_Verb:
|
|
|
|
complete();
|
2012-05-18 20:50:33 +00:00
|
|
|
if (!fCurrentContour) {
|
|
|
|
fCurrentContour = fContours.push_back_n(1);
|
|
|
|
finalCurveEnd = pointsPtr++;
|
|
|
|
*fExtra.append() = -1; // start new contour
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
continue;
|
|
|
|
case SkPath::kLine_Verb:
|
|
|
|
// skip degenerate points
|
|
|
|
if (pointsPtr[-1].fX != pointsPtr[0].fX
|
|
|
|
|| pointsPtr[-1].fY != pointsPtr[0].fY) {
|
|
|
|
fCurrentContour->addLine(&pointsPtr[-1]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SkPath::kQuad_Verb:
|
2012-05-18 20:50:33 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
reducedVerb = QuadReduceOrder(&pointsPtr[-1], fReducePts);
|
|
|
|
if (reducedVerb == 0) {
|
|
|
|
break; // skip degenerate points
|
|
|
|
}
|
|
|
|
if (reducedVerb == 1) {
|
2012-05-18 20:50:33 +00:00
|
|
|
*fExtra.append() =
|
|
|
|
fCurrentContour->addLine(fReducePts.end() - 2);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
fCurrentContour->addQuad(&pointsPtr[-1]);
|
|
|
|
break;
|
|
|
|
case SkPath::kCubic_Verb:
|
|
|
|
reducedVerb = CubicReduceOrder(&pointsPtr[-1], fReducePts);
|
|
|
|
if (reducedVerb == 0) {
|
|
|
|
break; // skip degenerate points
|
|
|
|
}
|
|
|
|
if (reducedVerb == 1) {
|
2012-05-18 20:50:33 +00:00
|
|
|
*fExtra.append() =
|
|
|
|
fCurrentContour->addLine(fReducePts.end() - 2);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reducedVerb == 2) {
|
2012-05-18 20:50:33 +00:00
|
|
|
*fExtra.append() =
|
|
|
|
fCurrentContour->addQuad(fReducePts.end() - 3);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
fCurrentContour->addCubic(&pointsPtr[-1]);
|
|
|
|
break;
|
|
|
|
case SkPath::kClose_Verb:
|
|
|
|
SkASSERT(fCurrentContour);
|
2012-05-18 20:50:33 +00:00
|
|
|
if (finalCurveStart && finalCurveEnd
|
|
|
|
&& *finalCurveStart != *finalCurveEnd) {
|
|
|
|
*fReducePts.append() = *finalCurveStart;
|
|
|
|
*fReducePts.append() = *finalCurveEnd;
|
|
|
|
*fExtra.append() =
|
|
|
|
fCurrentContour->addLine(fReducePts.end() - 2);
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
complete();
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
SkDEBUGFAIL("bad verb");
|
|
|
|
return;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
finalCurveStart = &pointsPtr[verb - 1];
|
2012-04-26 21:01:06 +00:00
|
|
|
pointsPtr += verb;
|
|
|
|
SkASSERT(fCurrentContour);
|
|
|
|
}
|
|
|
|
complete();
|
2012-07-02 20:27:02 +00:00
|
|
|
if (fCurrentContour && !fCurrentContour->segments().count()) {
|
2012-04-26 21:01:06 +00:00
|
|
|
fContours.pop_back();
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
// correct pointers in contours since fReducePts may have moved as it grew
|
|
|
|
int cIndex = 0;
|
|
|
|
int extraCount = fExtra.count();
|
2012-07-02 20:27:02 +00:00
|
|
|
SkASSERT(extraCount == 0 || fExtra[0] == -1);
|
2012-05-18 20:50:33 +00:00
|
|
|
int eIndex = 0;
|
|
|
|
int rIndex = 0;
|
|
|
|
while (++eIndex < extraCount) {
|
|
|
|
int offset = fExtra[eIndex];
|
|
|
|
if (offset < 0) {
|
2012-07-02 20:27:02 +00:00
|
|
|
++cIndex;
|
2012-05-18 20:50:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
fCurrentContour = &fContours[cIndex];
|
|
|
|
rIndex += fCurrentContour->updateSegment(offset - 1,
|
|
|
|
&fReducePts[rIndex]);
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
|
|
|
fExtra.reset(); // we're done with this
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const SkPath& fPath;
|
|
|
|
SkTDArray<SkPoint> fPathPts; // FIXME: point directly to path pts instead
|
2012-04-30 19:38:50 +00:00
|
|
|
SkTDArray<uint8_t> fPathVerbs; // FIXME: remove
|
2012-04-26 21:01:06 +00:00
|
|
|
Contour* fCurrentContour;
|
|
|
|
SkTArray<Contour>& fContours;
|
|
|
|
SkTDArray<SkPoint> fReducePts; // segments created on the fly
|
2012-05-18 20:50:33 +00:00
|
|
|
SkTDArray<int> fExtra; // -1 marks new contour, > 0 offsets into contour
|
2012-04-26 21:01:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Work {
|
|
|
|
public:
|
|
|
|
enum SegmentType {
|
|
|
|
kHorizontalLine_Segment = -1,
|
|
|
|
kVerticalLine_Segment = 0,
|
|
|
|
kLine_Segment = SkPath::kLine_Verb,
|
|
|
|
kQuad_Segment = SkPath::kQuad_Verb,
|
|
|
|
kCubic_Segment = SkPath::kCubic_Verb,
|
|
|
|
};
|
2012-07-02 20:27:02 +00:00
|
|
|
|
|
|
|
void addCoincident(Work& other, const Intersections& ts, bool swap) {
|
|
|
|
fContour->addCoincident(fIndex, other.fContour, other.fIndex, ts, swap);
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
// FIXME: does it make sense to write otherIndex now if we're going to
|
|
|
|
// fix it up later?
|
|
|
|
void addOtherT(int index, double otherT, int otherIndex) {
|
2012-07-02 20:27:02 +00:00
|
|
|
fContour->addOtherT(fIndex, index, otherT, otherIndex);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
// Avoid collapsing t values that are close to the same since
|
|
|
|
// we walk ts to describe consecutive intersections. Since a pair of ts can
|
|
|
|
// be nearly equal, any problems caused by this should be taken care
|
|
|
|
// of later.
|
|
|
|
// On the edge or out of range values are negative; add 2 to get end
|
2012-07-02 20:27:02 +00:00
|
|
|
int addT(double newT, const Work& other) {
|
|
|
|
return fContour->addT(fIndex, newT, other.fContour, other.fIndex);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
bool advance() {
|
|
|
|
return ++fIndex < fLast;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkScalar bottom() const {
|
|
|
|
return bounds().fBottom;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
const Bounds& bounds() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return fContour->segments()[fIndex].bounds();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const SkPoint* cubic() const {
|
|
|
|
return fCubic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(Contour* contour) {
|
|
|
|
fContour = contour;
|
|
|
|
fIndex = 0;
|
2012-07-02 20:27:02 +00:00
|
|
|
fLast = contour->segments().count();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-03 14:30:08 +00:00
|
|
|
|
|
|
|
bool isAdjacent(const Work& next) {
|
|
|
|
return fContour == next.fContour && fIndex + 1 == next.fIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFirstLast(const Work& next) {
|
|
|
|
return fContour == next.fContour && fIndex == 0
|
|
|
|
&& next.fIndex == fLast - 1;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
|
|
|
|
SkScalar left() const {
|
|
|
|
return bounds().fLeft;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
void promoteToCubic() {
|
|
|
|
fCubic[0] = pts()[0];
|
|
|
|
fCubic[2] = pts()[1];
|
|
|
|
fCubic[3] = pts()[2];
|
|
|
|
fCubic[1].fX = (fCubic[0].fX + fCubic[2].fX * 2) / 3;
|
|
|
|
fCubic[1].fY = (fCubic[0].fY + fCubic[2].fY * 2) / 3;
|
|
|
|
fCubic[2].fX = (fCubic[3].fX + fCubic[2].fX * 2) / 3;
|
|
|
|
fCubic[2].fY = (fCubic[3].fY + fCubic[2].fY * 2) / 3;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
const SkPoint* pts() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return fContour->segments()[fIndex].pts();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar right() const {
|
|
|
|
return bounds().fRight;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
ptrdiff_t segmentIndex() const {
|
|
|
|
return fIndex;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SegmentType segmentType() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
const Segment& segment = fContour->segments()[fIndex];
|
2012-04-26 21:01:06 +00:00
|
|
|
SegmentType type = (SegmentType) segment.verb();
|
|
|
|
if (type != kLine_Segment) {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
if (segment.isHorizontal()) {
|
|
|
|
return kHorizontalLine_Segment;
|
|
|
|
}
|
|
|
|
if (segment.isVertical()) {
|
|
|
|
return kVerticalLine_Segment;
|
|
|
|
}
|
|
|
|
return kLine_Segment;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
bool startAfter(const Work& after) {
|
|
|
|
fIndex = after.fIndex;
|
|
|
|
return advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar top() const {
|
|
|
|
return bounds().fTop;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkPath::Verb verb() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return fContour->segments()[fIndex].verb();
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
SkScalar x() const {
|
|
|
|
return bounds().fLeft;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool xFlipped() const {
|
|
|
|
return x() != pts()[0].fX;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkScalar y() const {
|
|
|
|
return bounds().fTop;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool yFlipped() const {
|
2012-07-02 20:27:02 +00:00
|
|
|
return y() != pts()[0].fY;
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Contour* fContour;
|
|
|
|
SkPoint fCubic[4];
|
|
|
|
int fIndex;
|
|
|
|
int fLast;
|
|
|
|
};
|
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
#if DEBUG_ADD_INTERSECTING_TS
|
2012-04-26 21:01:06 +00:00
|
|
|
static void debugShowLineIntersection(int pts, const Work& wt,
|
|
|
|
const Work& wn, const double wtTs[2], const double wnTs[2]) {
|
|
|
|
if (!pts) {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkDebugf("%s no intersect (%1.9g,%1.9g %1.9g,%1.9g) (%1.9g,%1.9g %1.9g,%1.9g)\n",
|
|
|
|
__FUNCTION__, wt.pts()[0].fX, wt.pts()[0].fY,
|
|
|
|
wt.pts()[1].fX, wt.pts()[1].fY, wn.pts()[0].fX, wn.pts()[0].fY,
|
|
|
|
wn.pts()[1].fX, wn.pts()[1].fY);
|
2012-04-26 21:01:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkPoint wtOutPt, wnOutPt;
|
|
|
|
LineXYAtT(wt.pts(), wtTs[0], &wtOutPt);
|
|
|
|
LineXYAtT(wn.pts(), wnTs[0], &wnOutPt);
|
2012-05-18 20:50:33 +00:00
|
|
|
SkDebugf("%s wtTs[0]=%g (%g,%g, %g,%g) (%g,%g)",
|
2012-04-26 21:01:06 +00:00
|
|
|
__FUNCTION__,
|
|
|
|
wtTs[0], wt.pts()[0].fX, wt.pts()[0].fY,
|
|
|
|
wt.pts()[1].fX, wt.pts()[1].fY, wtOutPt.fX, wtOutPt.fY);
|
|
|
|
if (pts == 2) {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkDebugf(" wtTs[1]=%g", wtTs[1]);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-03 19:53:30 +00:00
|
|
|
SkDebugf(" wnTs[0]=%g (%g,%g, %g,%g) (%g,%g)",
|
2012-04-26 21:01:06 +00:00
|
|
|
wnTs[0], wn.pts()[0].fX, wn.pts()[0].fY,
|
|
|
|
wn.pts()[1].fX, wn.pts()[1].fY, wnOutPt.fX, wnOutPt.fY);
|
|
|
|
if (pts == 2) {
|
2012-05-18 20:50:33 +00:00
|
|
|
SkDebugf(" wnTs[1]=%g", wnTs[1]);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-07-03 19:53:30 +00:00
|
|
|
SkDebugf("\n");
|
|
|
|
}
|
2012-05-23 18:09:25 +00:00
|
|
|
#else
|
|
|
|
static void debugShowLineIntersection(int , const Work& ,
|
|
|
|
const Work& , const double [2], const double [2]) {
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
2012-05-23 18:09:25 +00:00
|
|
|
#endif
|
2012-04-26 21:01:06 +00:00
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
static bool addIntersectTs(Contour* test, Contour* next) {
|
2012-05-18 20:50:33 +00:00
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
if (test != next) {
|
|
|
|
if (test->bounds().fBottom < next->bounds().fTop) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!Bounds::Intersects(test->bounds(), next->bounds())) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
Work wt;
|
2012-04-26 21:01:06 +00:00
|
|
|
wt.init(test);
|
2012-07-02 20:27:02 +00:00
|
|
|
bool foundCommonContour = test == next;
|
2012-04-26 21:01:06 +00:00
|
|
|
do {
|
2012-05-18 20:50:33 +00:00
|
|
|
Work wn;
|
|
|
|
wn.init(next);
|
2012-04-26 21:01:06 +00:00
|
|
|
if (test == next && !wn.startAfter(wt)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (!Bounds::Intersects(wt.bounds(), wn.bounds())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int pts;
|
|
|
|
Intersections ts;
|
|
|
|
bool swap = false;
|
|
|
|
switch (wt.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
swap = true;
|
|
|
|
switch (wn.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
case Work::kLine_Segment: {
|
|
|
|
pts = HLineIntersect(wn.pts(), wt.left(),
|
|
|
|
wt.right(), wt.y(), wt.xFlipped(), ts);
|
2012-05-18 20:50:33 +00:00
|
|
|
debugShowLineIntersection(pts, wt, wn,
|
|
|
|
ts.fT[1], ts.fT[0]);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kQuad_Segment: {
|
|
|
|
pts = HQuadIntersect(wn.pts(), wt.left(),
|
|
|
|
wt.right(), wt.y(), wt.xFlipped(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kCubic_Segment: {
|
|
|
|
pts = HCubicIntersect(wn.pts(), wt.left(),
|
|
|
|
wt.right(), wt.y(), wt.xFlipped(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
swap = true;
|
|
|
|
switch (wn.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
case Work::kLine_Segment: {
|
|
|
|
pts = VLineIntersect(wn.pts(), wt.top(),
|
|
|
|
wt.bottom(), wt.x(), wt.yFlipped(), ts);
|
2012-05-18 20:50:33 +00:00
|
|
|
debugShowLineIntersection(pts, wt, wn,
|
|
|
|
ts.fT[1], ts.fT[0]);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kQuad_Segment: {
|
|
|
|
pts = VQuadIntersect(wn.pts(), wt.top(),
|
|
|
|
wt.bottom(), wt.x(), wt.yFlipped(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kCubic_Segment: {
|
|
|
|
pts = VCubicIntersect(wn.pts(), wt.top(),
|
|
|
|
wt.bottom(), wt.x(), wt.yFlipped(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Work::kLine_Segment:
|
|
|
|
switch (wn.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
pts = HLineIntersect(wt.pts(), wn.left(),
|
|
|
|
wn.right(), wn.y(), wn.xFlipped(), ts);
|
2012-05-18 20:50:33 +00:00
|
|
|
debugShowLineIntersection(pts, wt, wn,
|
|
|
|
ts.fT[1], ts.fT[0]);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
pts = VLineIntersect(wt.pts(), wn.top(),
|
|
|
|
wn.bottom(), wn.x(), wn.yFlipped(), ts);
|
2012-05-18 20:50:33 +00:00
|
|
|
debugShowLineIntersection(pts, wt, wn,
|
|
|
|
ts.fT[1], ts.fT[0]);
|
2012-04-26 21:01:06 +00:00
|
|
|
break;
|
|
|
|
case Work::kLine_Segment: {
|
|
|
|
pts = LineIntersect(wt.pts(), wn.pts(), ts);
|
|
|
|
debugShowLineIntersection(pts, wt, wn,
|
|
|
|
ts.fT[1], ts.fT[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kQuad_Segment: {
|
|
|
|
swap = true;
|
|
|
|
pts = QuadLineIntersect(wn.pts(), wt.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kCubic_Segment: {
|
|
|
|
swap = true;
|
|
|
|
pts = CubicLineIntersect(wn.pts(), wt.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Work::kQuad_Segment:
|
|
|
|
switch (wn.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
pts = HQuadIntersect(wt.pts(), wn.left(),
|
|
|
|
wn.right(), wn.y(), wn.xFlipped(), ts);
|
|
|
|
break;
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
pts = VQuadIntersect(wt.pts(), wn.top(),
|
|
|
|
wn.bottom(), wn.x(), wn.yFlipped(), ts);
|
|
|
|
break;
|
|
|
|
case Work::kLine_Segment: {
|
|
|
|
pts = QuadLineIntersect(wt.pts(), wn.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kQuad_Segment: {
|
|
|
|
pts = QuadIntersect(wt.pts(), wn.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kCubic_Segment: {
|
|
|
|
wt.promoteToCubic();
|
|
|
|
pts = CubicIntersect(wt.cubic(), wn.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Work::kCubic_Segment:
|
|
|
|
switch (wn.segmentType()) {
|
|
|
|
case Work::kHorizontalLine_Segment:
|
|
|
|
pts = HCubicIntersect(wt.pts(), wn.left(),
|
|
|
|
wn.right(), wn.y(), wn.xFlipped(), ts);
|
|
|
|
break;
|
|
|
|
case Work::kVerticalLine_Segment:
|
|
|
|
pts = VCubicIntersect(wt.pts(), wn.top(),
|
|
|
|
wn.bottom(), wn.x(), wn.yFlipped(), ts);
|
|
|
|
break;
|
|
|
|
case Work::kLine_Segment: {
|
|
|
|
pts = CubicLineIntersect(wt.pts(), wn.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kQuad_Segment: {
|
|
|
|
wn.promoteToCubic();
|
|
|
|
pts = CubicIntersect(wt.pts(), wn.cubic(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Work::kCubic_Segment: {
|
|
|
|
pts = CubicIntersect(wt.pts(), wn.pts(), ts);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SkASSERT(0);
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
if (!foundCommonContour && pts > 0) {
|
|
|
|
test->addCross(next);
|
|
|
|
next->addCross(test);
|
|
|
|
foundCommonContour = true;
|
|
|
|
}
|
2012-04-26 21:01:06 +00:00
|
|
|
// in addition to recording T values, record matching segment
|
2012-06-01 17:44:28 +00:00
|
|
|
if (pts == 2 && wn.segmentType() <= Work::kLine_Segment
|
|
|
|
&& wt.segmentType() <= Work::kLine_Segment) {
|
2012-07-02 20:27:02 +00:00
|
|
|
wt.addCoincident(wn, ts, swap);
|
|
|
|
continue;
|
2012-06-01 17:44:28 +00:00
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
for (int pt = 0; pt < pts; ++pt) {
|
2012-04-26 21:01:06 +00:00
|
|
|
SkASSERT(ts.fT[0][pt] >= 0 && ts.fT[0][pt] <= 1);
|
|
|
|
SkASSERT(ts.fT[1][pt] >= 0 && ts.fT[1][pt] <= 1);
|
2012-07-02 20:27:02 +00:00
|
|
|
int testTAt = wt.addT(ts.fT[swap][pt], wn);
|
|
|
|
int nextTAt = wn.addT(ts.fT[!swap][pt], wt);
|
2012-05-18 20:50:33 +00:00
|
|
|
wt.addOtherT(testTAt, ts.fT[!swap][pt], nextTAt);
|
|
|
|
wn.addOtherT(nextTAt, ts.fT[swap][pt], testTAt);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
} while (wn.advance());
|
|
|
|
} while (wt.advance());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// resolve any coincident pairs found while intersecting, and
|
2012-04-30 19:38:50 +00:00
|
|
|
// see if coincidence is formed by clipping non-concident segments
|
|
|
|
static void coincidenceCheck(SkTDArray<Contour*>& contourList, int winding) {
|
|
|
|
int contourCount = contourList.count();
|
2012-07-02 20:27:02 +00:00
|
|
|
for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
|
|
|
|
Contour* contour = contourList[cIndex];
|
2012-07-11 17:52:32 +00:00
|
|
|
contour->findTooCloseToCall(winding);
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
2012-06-01 18:20:10 +00:00
|
|
|
for (int cIndex = 0; cIndex < contourCount; ++cIndex) {
|
2012-04-30 19:38:50 +00:00
|
|
|
Contour* contour = contourList[cIndex];
|
2012-07-11 17:52:32 +00:00
|
|
|
contour->resolveCoincidence(winding);
|
2012-04-30 19:38:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-02 20:27:02 +00:00
|
|
|
// project a ray from the top of the contour up and see if it hits anything
|
|
|
|
// note: when we compute line intersections, we keep track of whether
|
|
|
|
// two contours touch, so we need only look at contours not touching this one.
|
|
|
|
// OPTIMIZATION: sort contourList vertically to avoid linear walk
|
|
|
|
static int innerContourCheck(SkTDArray<Contour*>& contourList,
|
|
|
|
Contour* baseContour, const SkPoint& basePt) {
|
|
|
|
int contourCount = contourList.count();
|
|
|
|
int winding = 0;
|
|
|
|
SkScalar bestY = SK_ScalarMin;
|
|
|
|
for (int cTest = 0; cTest < contourCount; ++cTest) {
|
|
|
|
Contour* contour = contourList[cTest];
|
|
|
|
if (basePt.fY < contour->bounds().fTop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (bestY > contour->bounds().fBottom) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (baseContour->crosses(contour)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int tIndex;
|
|
|
|
double tHit;
|
|
|
|
const Segment* test = contour->crossedSegment(basePt, bestY, tIndex,
|
|
|
|
tHit);
|
|
|
|
if (!test) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If the ray hit the end of a span, we need to construct the wheel of
|
|
|
|
// angles to find the span closest to the ray -- even if there are just
|
|
|
|
// two spokes on the wheel.
|
|
|
|
if (tHit == test->t(tIndex)) {
|
|
|
|
SkTDArray<Angle> angles;
|
|
|
|
int end = test->nextSpan(tIndex, 1);
|
|
|
|
if (end < 0) {
|
|
|
|
end = test->nextSpan(tIndex, -1);
|
|
|
|
}
|
|
|
|
test->addTwoAngles(tIndex, end, angles);
|
|
|
|
// test->buildAnglesInner(tIndex, angles);
|
|
|
|
test->buildAngles(tIndex, angles);
|
|
|
|
SkTDArray<Angle*> sorted;
|
|
|
|
sortAngles(angles, sorted);
|
|
|
|
const Angle* angle = sorted[0];
|
|
|
|
test = angle->segment();
|
|
|
|
SkScalar testDx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit);
|
|
|
|
if (testDx == 0) {
|
|
|
|
angle = *(sorted.end() - 1);
|
|
|
|
test = angle->segment();
|
|
|
|
SkASSERT((*SegmentDXAtT[test->verb()])(test->pts(), tHit) != 0);
|
|
|
|
}
|
|
|
|
tIndex = angle->start(); // lesser Y
|
2012-07-11 17:52:32 +00:00
|
|
|
winding = test->windSum(SkMin32(tIndex, angle->end()));
|
2012-07-02 20:27:02 +00:00
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s 1 winding=%d\n", __FUNCTION__, winding);
|
|
|
|
#endif
|
|
|
|
} else {
|
2012-07-11 17:52:32 +00:00
|
|
|
winding = test->windSum(tIndex);
|
2012-07-02 20:27:02 +00:00
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s 2 winding=%d\n", __FUNCTION__, winding);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// see if a + change in T results in a +/- change in X (compute x'(T))
|
|
|
|
SkScalar dx = (*SegmentDXAtT[test->verb()])(test->pts(), tHit);
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s dx=%1.9g\n", __FUNCTION__, dx);
|
|
|
|
#endif
|
|
|
|
SkASSERT(dx != 0);
|
|
|
|
if (winding * dx > 0) { // if same signs, result is negative
|
|
|
|
winding += dx > 0 ? -1 : 1;
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s 3 winding=%d\n", __FUNCTION__, winding);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
baseContour->setWinding(winding);
|
|
|
|
return winding;
|
|
|
|
}
|
2012-05-18 20:50:33 +00:00
|
|
|
|
|
|
|
// OPTIMIZATION: not crazy about linear search here to find top active y.
|
|
|
|
// seems like we should break down and do the sort, or maybe sort each
|
|
|
|
// contours' segments?
|
|
|
|
// Once the segment array is built, there's no reason I can think of not to
|
|
|
|
// sort it in Y. hmmm
|
2012-07-02 20:27:02 +00:00
|
|
|
// FIXME: return the contour found to pass to inner contour check
|
2012-05-18 20:50:33 +00:00
|
|
|
static Segment* findTopContour(SkTDArray<Contour*>& contourList,
|
2012-07-02 20:27:02 +00:00
|
|
|
Contour*& topContour) {
|
|
|
|
int contourCount = contourList.count();
|
2012-05-18 20:50:33 +00:00
|
|
|
int cIndex = 0;
|
|
|
|
Segment* topStart;
|
2012-07-02 20:27:02 +00:00
|
|
|
SkScalar bestY = SK_ScalarMax;
|
|
|
|
Contour* contour;
|
2012-05-18 20:50:33 +00:00
|
|
|
do {
|
2012-07-02 20:27:02 +00:00
|
|
|
contour = contourList[cIndex];
|
|
|
|
topStart = contour->topSegment(bestY);
|
2012-05-18 20:50:33 +00:00
|
|
|
} while (!topStart && ++cIndex < contourCount);
|
|
|
|
if (!topStart) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
topContour = contour;
|
|
|
|
while (++cIndex < contourCount) {
|
|
|
|
contour = contourList[cIndex];
|
|
|
|
if (bestY < contour->bounds().fTop) {
|
2012-05-18 20:50:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
SkScalar testY = SK_ScalarMax;
|
|
|
|
Segment* test = contour->topSegment(testY);
|
|
|
|
if (!test || bestY <= testY) {
|
|
|
|
continue;
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
2012-07-02 20:27:02 +00:00
|
|
|
topContour = contour;
|
|
|
|
topStart = test;
|
|
|
|
bestY = testY;
|
2012-05-18 20:50:33 +00:00
|
|
|
}
|
|
|
|
return topStart;
|
|
|
|
}
|
|
|
|
|
2012-07-11 17:52:32 +00:00
|
|
|
static Segment* findChase(SkTDArray<Span*>& chase, int& tIndex, int& endIndex) {
|
|
|
|
while (chase.count()) {
|
2012-07-12 19:29:45 +00:00
|
|
|
Span* span = chase[chase.count() - 1];
|
2012-07-11 17:52:32 +00:00
|
|
|
const Span& backPtr = span->fOther->span(span->fOtherIndex);
|
|
|
|
Segment* segment = backPtr.fOther;
|
|
|
|
tIndex = backPtr.fOtherIndex;
|
2012-07-12 19:29:45 +00:00
|
|
|
SkTDArray<Angle> angles;
|
|
|
|
int done = 0;
|
|
|
|
if (segment->activeAngle(tIndex, done, angles)) {
|
|
|
|
Angle* last = angles.end() - 1;
|
|
|
|
tIndex = last->start();
|
|
|
|
endIndex = last->end();
|
|
|
|
return last->segment();
|
|
|
|
}
|
|
|
|
if (done == angles.count()) {
|
|
|
|
chase.pop(&span);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkTDArray<Angle*> sorted;
|
|
|
|
sortAngles(angles, sorted);
|
|
|
|
// find first angle, initialize winding to computed fWindSum
|
|
|
|
int firstIndex = -1;
|
|
|
|
const Angle* angle;
|
|
|
|
int winding;
|
|
|
|
do {
|
|
|
|
angle = sorted[++firstIndex];
|
|
|
|
winding = angle->segment()->windSum(angle);
|
|
|
|
} while (winding == SK_MinS32);
|
|
|
|
int firstSign = angle->sign();
|
|
|
|
if (firstSign * winding > 0) {
|
|
|
|
winding -= firstSign;
|
|
|
|
}
|
2012-07-12 21:05:13 +00:00
|
|
|
// SkDebugf("%s firstSign=%d\n", __FUNCTION__, firstSign);
|
2012-07-12 19:29:45 +00:00
|
|
|
// we care about first sign and whether wind sum indicates this
|
|
|
|
// edge is inside or outside. Maybe need to pass span winding
|
|
|
|
// or first winding or something into this function?
|
|
|
|
// advance to first undone angle, then return it and winding
|
|
|
|
// (to set whether edges are active or not)
|
|
|
|
int nextIndex = firstIndex + 1;
|
|
|
|
int angleCount = sorted.count();
|
|
|
|
int lastIndex = firstIndex != 0 ? firstIndex : angleCount;
|
|
|
|
do {
|
|
|
|
SkASSERT(nextIndex != firstIndex);
|
|
|
|
if (nextIndex == angleCount) {
|
|
|
|
nextIndex = 0;
|
|
|
|
}
|
|
|
|
const Angle* angle = sorted[nextIndex];
|
|
|
|
segment = angle->segment();
|
|
|
|
int windValue = segment->windValue(angle);
|
|
|
|
SkASSERT(windValue > 0);
|
|
|
|
int maxWinding = winding;
|
|
|
|
winding -= angle->sign() * windValue;
|
|
|
|
if (maxWinding * winding < 0) {
|
|
|
|
SkDebugf("%s flipped sign %d %d\n", __FUNCTION__, maxWinding, winding);
|
|
|
|
}
|
|
|
|
tIndex = angle->start();
|
|
|
|
endIndex = angle->end();
|
|
|
|
int lesser = SkMin32(tIndex, endIndex);
|
|
|
|
const Span& nextSpan = segment->span(lesser);
|
|
|
|
if (!nextSpan.fDone) {
|
|
|
|
// FIXME: this be wrong. assign startWinding if edge is in
|
|
|
|
// same direction. If the direction is opposite, winding to
|
|
|
|
// assign is flipped sign or +/- 1?
|
|
|
|
if (abs(maxWinding) < abs(winding)) {
|
|
|
|
maxWinding = winding;
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
2012-07-12 19:29:45 +00:00
|
|
|
segment->markWinding(lesser, maxWinding);
|
|
|
|
break;
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
2012-07-12 19:29:45 +00:00
|
|
|
} while (++nextIndex != lastIndex);
|
|
|
|
return segment;
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-12 12:52:50 +00:00
|
|
|
#if DEBUG_ACTIVE_SPANS
|
|
|
|
static void debugShowActiveSpans(SkTDArray<Contour*>& contourList) {
|
|
|
|
for (int index = 0; index < contourList.count(); ++ index) {
|
|
|
|
contourList[index]->debugShowActiveSpans();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-04-26 21:01:06 +00:00
|
|
|
// Each segment may have an inside or an outside. Segments contained within
|
|
|
|
// winding may have insides on either side, and form a contour that should be
|
|
|
|
// ignored. Segments that are coincident with opposing direction segments may
|
|
|
|
// have outsides on either side, and should also disappear.
|
|
|
|
// 'Normal' segments will have one inside and one outside. Subsequent connections
|
|
|
|
// when winding should follow the intersection direction. If more than one edge
|
|
|
|
// is an option, choose first edge that continues the inside.
|
2012-05-18 20:50:33 +00:00
|
|
|
// since we start with leftmost top edge, we'll traverse through a
|
|
|
|
// smaller angle counterclockwise to get to the next edge.
|
2012-05-22 17:01:14 +00:00
|
|
|
static void bridge(SkTDArray<Contour*>& contourList, SkPath& simple) {
|
2012-07-02 20:27:02 +00:00
|
|
|
bool firstContour = true;
|
2012-05-07 20:49:36 +00:00
|
|
|
do {
|
2012-07-02 20:27:02 +00:00
|
|
|
Contour* topContour;
|
|
|
|
Segment* topStart = findTopContour(contourList, topContour);
|
2012-05-07 20:49:36 +00:00
|
|
|
if (!topStart) {
|
|
|
|
break;
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
2012-05-07 20:49:36 +00:00
|
|
|
// Start at the top. Above the top is outside, below is inside.
|
2012-05-31 13:13:11 +00:00
|
|
|
// follow edges to intersection by changing the index by direction.
|
|
|
|
int index, endIndex;
|
2012-06-07 21:09:20 +00:00
|
|
|
Segment* current = topStart->findTop(index, endIndex);
|
2012-07-11 17:52:32 +00:00
|
|
|
int contourWinding;
|
|
|
|
if (firstContour) {
|
|
|
|
contourWinding = 0;
|
|
|
|
firstContour = false;
|
|
|
|
} else {
|
|
|
|
const SkPoint& topPoint = current->xyAtT(endIndex);
|
|
|
|
contourWinding = innerContourCheck(contourList, topContour, topPoint);
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s contourWinding=%d\n", __FUNCTION__, contourWinding);
|
|
|
|
#endif
|
2012-07-02 20:27:02 +00:00
|
|
|
}
|
2012-06-07 21:09:20 +00:00
|
|
|
SkPoint lastPt;
|
2012-07-02 20:27:02 +00:00
|
|
|
bool firstTime = true;
|
2012-07-11 17:52:32 +00:00
|
|
|
int winding = contourWinding;
|
2012-07-02 20:27:02 +00:00
|
|
|
int spanWinding = current->spanSign(index, endIndex);
|
2012-07-11 17:52:32 +00:00
|
|
|
// int firstWinding = contourWinding + spanWinding;
|
|
|
|
// FIXME: needs work. While it works in limited situations, it does
|
|
|
|
// not always compute winding correctly. Active should be removed and instead
|
|
|
|
// the initial winding should be correctly passed in so that if the
|
|
|
|
// inner contour is wound the same way, it never finds an accumulated
|
|
|
|
// winding of zero. Inside 'find next', we need to look for transitions
|
|
|
|
// other than zero when resolving sorted angles.
|
|
|
|
SkTDArray<Span*> chaseArray;
|
2012-05-18 20:50:33 +00:00
|
|
|
do {
|
2012-07-11 17:52:32 +00:00
|
|
|
bool active = winding * spanWinding <= 0;
|
|
|
|
const SkPoint* firstPt = NULL;
|
|
|
|
do {
|
|
|
|
SkASSERT(!current->done());
|
|
|
|
int nextStart, nextEnd, flipped = 1;
|
|
|
|
Segment* next = current->findNext(chaseArray,
|
|
|
|
winding + spanWinding, index,
|
|
|
|
endIndex, nextStart, nextEnd, flipped, firstTime);
|
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!firstPt) {
|
|
|
|
firstPt = ¤t->addMoveTo(index, simple, active);
|
|
|
|
}
|
|
|
|
lastPt = current->addCurveTo(index, endIndex, simple, active);
|
|
|
|
current = next;
|
|
|
|
index = nextStart;
|
|
|
|
endIndex = nextEnd;
|
|
|
|
spanWinding = SkSign32(spanWinding) * flipped * next->windValue(
|
|
|
|
SkMin32(nextStart, nextEnd));
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s spanWinding=%d\n", __FUNCTION__, spanWinding);
|
|
|
|
#endif
|
|
|
|
firstTime = false;
|
|
|
|
} while (*firstPt != lastPt && (active || !current->done()));
|
|
|
|
if (firstPt && active) {
|
|
|
|
#if DEBUG_PATH_CONSTRUCTION
|
|
|
|
SkDebugf("%s close\n", __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
simple.close();
|
|
|
|
}
|
|
|
|
current = findChase(chaseArray, index, endIndex);
|
2012-07-12 12:52:50 +00:00
|
|
|
#if DEBUG_ACTIVE_SPANS
|
|
|
|
debugShowActiveSpans(contourList);
|
|
|
|
#endif
|
2012-07-11 17:52:32 +00:00
|
|
|
if (!current) {
|
2012-05-31 13:13:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
int lesser = SkMin32(index, endIndex);
|
|
|
|
spanWinding = current->windSum(lesser);
|
|
|
|
int spanValue = current->windValue(lesser);
|
|
|
|
SkASSERT(spanWinding != SK_MinS32);
|
|
|
|
int spanSign = current->spanSign(index, endIndex);
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s spanWinding=%d spanSign=%d winding=%d spanValue=%d\n",
|
|
|
|
__FUNCTION__, spanWinding, spanSign, winding, spanValue);
|
|
|
|
#endif
|
|
|
|
if (spanWinding * spanSign < 0) {
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s spanWinding * spanSign < 0\n", __FUNCTION__);
|
|
|
|
#endif
|
2012-07-12 19:29:45 +00:00
|
|
|
// SkTSwap<int>(index, endIndex);
|
2012-07-11 17:52:32 +00:00
|
|
|
}
|
|
|
|
if (abs(spanWinding) > spanValue) {
|
|
|
|
#if DEBUG_WINDING
|
|
|
|
SkDebugf("%s abs(spanWinding) > spanValue\n", __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
winding = spanWinding;
|
|
|
|
spanWinding = spanValue * SkSign32(spanWinding);
|
|
|
|
winding -= spanWinding;
|
2012-05-31 13:13:11 +00:00
|
|
|
}
|
2012-07-11 17:52:32 +00:00
|
|
|
} while (true);
|
2012-05-22 17:01:14 +00:00
|
|
|
} while (true);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2012-05-18 20:50:33 +00:00
|
|
|
static void fixOtherTIndex(SkTDArray<Contour*>& contourList) {
|
|
|
|
int contourCount = contourList.count();
|
|
|
|
for (int cTest = 0; cTest < contourCount; ++cTest) {
|
|
|
|
Contour* contour = contourList[cTest];
|
|
|
|
contour->fixOtherTIndex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 17:01:14 +00:00
|
|
|
static void makeContourList(SkTArray<Contour>& contours,
|
2012-04-26 21:01:06 +00:00
|
|
|
SkTDArray<Contour*>& list) {
|
2012-04-30 19:38:50 +00:00
|
|
|
int count = contours.count();
|
2012-04-26 21:01:06 +00:00
|
|
|
if (count == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-30 19:38:50 +00:00
|
|
|
for (int index = 0; index < count; ++index) {
|
2012-04-26 21:01:06 +00:00
|
|
|
*list.append() = &contours[index];
|
|
|
|
}
|
|
|
|
QSort<Contour>(list.begin(), list.end() - 1);
|
|
|
|
}
|
|
|
|
|
2012-05-23 18:09:25 +00:00
|
|
|
void simplifyx(const SkPath& path, SkPath& simple) {
|
2012-04-26 21:01:06 +00:00
|
|
|
// returns 1 for evenodd, -1 for winding, regardless of inverse-ness
|
2012-04-30 19:38:50 +00:00
|
|
|
int winding = (path.getFillType() & 1) ? 1 : -1;
|
2012-04-26 21:01:06 +00:00
|
|
|
simple.reset();
|
|
|
|
simple.setFillType(SkPath::kEvenOdd_FillType);
|
|
|
|
|
|
|
|
// turn path into list of segments
|
|
|
|
SkTArray<Contour> contours;
|
|
|
|
// FIXME: add self-intersecting cubics' T values to segment
|
|
|
|
EdgeBuilder builder(path, contours);
|
|
|
|
SkTDArray<Contour*> contourList;
|
2012-05-22 17:01:14 +00:00
|
|
|
makeContourList(contours, contourList);
|
2012-04-26 21:01:06 +00:00
|
|
|
Contour** currentPtr = contourList.begin();
|
|
|
|
if (!currentPtr) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-22 17:01:14 +00:00
|
|
|
Contour** listEnd = contourList.end();
|
2012-04-26 21:01:06 +00:00
|
|
|
// find all intersections between segments
|
|
|
|
do {
|
|
|
|
Contour** nextPtr = currentPtr;
|
|
|
|
Contour* current = *currentPtr++;
|
|
|
|
Contour* next;
|
|
|
|
do {
|
|
|
|
next = *nextPtr++;
|
2012-05-23 18:09:25 +00:00
|
|
|
} while (addIntersectTs(current, next) && nextPtr != listEnd);
|
2012-05-22 17:01:14 +00:00
|
|
|
} while (currentPtr != listEnd);
|
2012-04-30 19:38:50 +00:00
|
|
|
// eat through coincident edges
|
|
|
|
coincidenceCheck(contourList, winding);
|
2012-07-03 14:30:08 +00:00
|
|
|
fixOtherTIndex(contourList);
|
2012-04-26 21:01:06 +00:00
|
|
|
// construct closed contours
|
2012-05-22 17:01:14 +00:00
|
|
|
bridge(contourList, simple);
|
2012-04-26 21:01:06 +00:00
|
|
|
}
|
|
|
|
|