blob: f89598ad473808b7eb44ad4fb2c30f297583bb17 [file] [log] [blame]
caryclark@google.com818b0cc2013-04-08 11:50:46 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "PathOpsTestCommon.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +00008#include "SkPathOpsBounds.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +00009#include "SkPathOpsCubic.h"
caryclark@google.com8d0a5242013-07-16 16:11:16 +000010#include "SkPathOpsLine.h"
11#include "SkPathOpsQuad.h"
12#include "SkPathOpsTriangle.h"
caryclark@google.com818b0cc2013-04-08 11:50:46 +000013
caryclark@google.comd892bd82013-06-17 14:10:36 +000014void CubicToQuads(const SkDCubic& cubic, double precision, SkTArray<SkDQuad, true>& quads) {
15 SkTArray<double, true> ts;
caryclark@google.com818b0cc2013-04-08 11:50:46 +000016 cubic.toQuadraticTs(precision, &ts);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000017 if (ts.count() <= 0) {
caryclark@google.com818b0cc2013-04-08 11:50:46 +000018 SkDQuad quad = cubic.toQuad();
caryclark@google.comd892bd82013-06-17 14:10:36 +000019 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000020 return;
21 }
22 double tStart = 0;
23 for (int i1 = 0; i1 <= ts.count(); ++i1) {
24 const double tEnd = i1 < ts.count() ? ts[i1] : 1;
25 SkDCubic part = cubic.subDivide(tStart, tEnd);
26 SkDQuad quad = part.toQuad();
caryclark@google.comd892bd82013-06-17 14:10:36 +000027 quads.push_back(quad);
caryclark@google.com818b0cc2013-04-08 11:50:46 +000028 tStart = tEnd;
29 }
30}
caryclark@google.com8d0a5242013-07-16 16:11:16 +000031
32static bool SkDoubleIsNaN(double x) {
33 return x != x;
34}
35
36bool ValidBounds(const SkPathOpsBounds& bounds) {
37 if (SkScalarIsNaN(bounds.fLeft)) {
38 return false;
39 }
40 if (SkScalarIsNaN(bounds.fTop)) {
41 return false;
42 }
43 if (SkScalarIsNaN(bounds.fRight)) {
44 return false;
45 }
46 return !SkScalarIsNaN(bounds.fBottom);
47}
48
49bool ValidCubic(const SkDCubic& cubic) {
50 for (int index = 0; index < 4; ++index) {
51 if (!ValidPoint(cubic[index])) {
52 return false;
53 }
54 }
55 return true;
56}
57
58bool ValidLine(const SkDLine& line) {
59 for (int index = 0; index < 2; ++index) {
60 if (!ValidPoint(line[index])) {
61 return false;
62 }
63 }
64 return true;
65}
66
67bool ValidPoint(const SkDPoint& pt) {
68 if (SkDoubleIsNaN(pt.fX)) {
69 return false;
70 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +000071 return !SkDoubleIsNaN(pt.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +000072}
73
74bool ValidPoints(const SkPoint* pts, int count) {
75 for (int index = 0; index < count; ++index) {
76 if (SkScalarIsNaN(pts[index].fX)) {
77 return false;
78 }
79 if (SkScalarIsNaN(pts[index].fY)) {
80 return false;
81 }
82 }
83 return true;
84}
85
86bool ValidQuad(const SkDQuad& quad) {
87 for (int index = 0; index < 3; ++index) {
88 if (!ValidPoint(quad[index])) {
89 return false;
90 }
91 }
92 return true;
93}
94
95bool ValidTriangle(const SkDTriangle& triangle) {
96 for (int index = 0; index < 3; ++index) {
97 if (!ValidPoint(triangle.fPts[index])) {
98 return false;
99 }
100 }
101 return true;
102}
103
104bool ValidVector(const SkDVector& v) {
105 if (SkDoubleIsNaN(v.fX)) {
106 return false;
107 }
skia.committer@gmail.comeebe6f42013-07-17 07:01:13 +0000108 return !SkDoubleIsNaN(v.fY);
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000109}