reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkPath.h" |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 3 | #include "SkParse.h" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 4 | #include "SkSize.h" |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 5 | |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 6 | static void check_convexity(skiatest::Reporter* reporter, const SkPath& path, |
| 7 | SkPath::Convexity expected) { |
| 8 | SkPath::Convexity c = SkPath::ComputeConvexity(path); |
| 9 | REPORTER_ASSERT(reporter, c == expected); |
| 10 | } |
| 11 | |
| 12 | static void test_convexity2(skiatest::Reporter* reporter) { |
| 13 | SkPath pt; |
| 14 | pt.moveTo(0, 0); |
| 15 | pt.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 16 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 17 | |
| 18 | SkPath line; |
| 19 | line.moveTo(12, 20); |
| 20 | line.lineTo(-12, -20); |
| 21 | line.close(); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 22 | check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 23 | |
| 24 | SkPath triLeft; |
| 25 | triLeft.moveTo(0, 0); |
| 26 | triLeft.lineTo(1, 0); |
| 27 | triLeft.lineTo(1, 1); |
| 28 | triLeft.close(); |
| 29 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
| 30 | |
| 31 | SkPath triRight; |
| 32 | triRight.moveTo(0, 0); |
| 33 | triRight.lineTo(-1, 0); |
| 34 | triRight.lineTo(1, 1); |
| 35 | triRight.close(); |
| 36 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
| 37 | |
| 38 | SkPath square; |
| 39 | square.moveTo(0, 0); |
| 40 | square.lineTo(1, 0); |
| 41 | square.lineTo(1, 1); |
| 42 | square.lineTo(0, 1); |
| 43 | square.close(); |
| 44 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
| 45 | |
| 46 | SkPath redundantSquare; |
| 47 | redundantSquare.moveTo(0, 0); |
| 48 | redundantSquare.lineTo(0, 0); |
| 49 | redundantSquare.lineTo(0, 0); |
| 50 | redundantSquare.lineTo(1, 0); |
| 51 | redundantSquare.lineTo(1, 0); |
| 52 | redundantSquare.lineTo(1, 0); |
| 53 | redundantSquare.lineTo(1, 1); |
| 54 | redundantSquare.lineTo(1, 1); |
| 55 | redundantSquare.lineTo(1, 1); |
| 56 | redundantSquare.lineTo(0, 1); |
| 57 | redundantSquare.lineTo(0, 1); |
| 58 | redundantSquare.lineTo(0, 1); |
| 59 | redundantSquare.close(); |
| 60 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
| 61 | |
| 62 | SkPath bowTie; |
| 63 | bowTie.moveTo(0, 0); |
| 64 | bowTie.lineTo(0, 0); |
| 65 | bowTie.lineTo(0, 0); |
| 66 | bowTie.lineTo(1, 1); |
| 67 | bowTie.lineTo(1, 1); |
| 68 | bowTie.lineTo(1, 1); |
| 69 | bowTie.lineTo(1, 0); |
| 70 | bowTie.lineTo(1, 0); |
| 71 | bowTie.lineTo(1, 0); |
| 72 | bowTie.lineTo(0, 1); |
| 73 | bowTie.lineTo(0, 1); |
| 74 | bowTie.lineTo(0, 1); |
| 75 | bowTie.close(); |
| 76 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
| 77 | |
| 78 | SkPath spiral; |
| 79 | spiral.moveTo(0, 0); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame^] | 80 | spiral.lineTo(100, 0); |
| 81 | spiral.lineTo(100, 100); |
| 82 | spiral.lineTo(0, 100); |
| 83 | spiral.lineTo(0, 50); |
| 84 | spiral.lineTo(50, 50); |
| 85 | spiral.lineTo(50, 75); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 86 | spiral.close(); |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 87 | check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 88 | |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame^] | 89 | // TODO(reed): We evaluate this path as concave for SK_SCALAR_IS_FLOAT, |
| 90 | // but convex for SK_SCALAR_IS_FIXED. |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 91 | SkPath dent; |
| 92 | dent.moveTo(0, 0); |
epoger@google.com | 2047f00 | 2011-05-17 17:36:59 +0000 | [diff] [blame^] | 93 | dent.lineTo(100, 100); |
| 94 | dent.lineTo(0, 100); |
| 95 | dent.lineTo(-50, 200); |
| 96 | dent.lineTo(-200, 100); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 97 | dent.close(); |
| 98 | check_convexity(reporter, dent, SkPath::kConcave_Convexity); |
| 99 | } |
| 100 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 101 | static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p, |
| 102 | const SkRect& bounds) { |
| 103 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 104 | REPORTER_ASSERT(reporter, p.getBounds() == bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 105 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 106 | SkPath p2(p); |
| 107 | REPORTER_ASSERT(reporter, p2.isConvex()); |
| 108 | REPORTER_ASSERT(reporter, p2.getBounds() == bounds); |
| 109 | |
| 110 | SkPath other; |
| 111 | other.swap(p2); |
| 112 | REPORTER_ASSERT(reporter, other.isConvex()); |
| 113 | REPORTER_ASSERT(reporter, other.getBounds() == bounds); |
| 114 | } |
| 115 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 116 | static void setFromString(SkPath* path, const char str[]) { |
| 117 | bool first = true; |
| 118 | while (str) { |
| 119 | SkScalar x, y; |
| 120 | str = SkParse::FindScalar(str, &x); |
| 121 | if (NULL == str) { |
| 122 | break; |
| 123 | } |
| 124 | str = SkParse::FindScalar(str, &y); |
| 125 | SkASSERT(str); |
| 126 | if (first) { |
| 127 | path->moveTo(x, y); |
| 128 | first = false; |
| 129 | } else { |
| 130 | path->lineTo(x, y); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void test_convexity(skiatest::Reporter* reporter) { |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 136 | static const SkPath::Convexity C = SkPath::kConcave_Convexity; |
| 137 | static const SkPath::Convexity V = SkPath::kConvex_Convexity; |
| 138 | |
| 139 | SkPath path; |
| 140 | |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 141 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 142 | path.addCircle(0, 0, 10); |
| 143 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 144 | path.addCircle(0, 0, 10); // 2nd circle |
| 145 | REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path)); |
| 146 | path.reset(); |
| 147 | path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction); |
| 148 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 149 | path.reset(); |
| 150 | path.addRect(0, 0, 10, 10, SkPath::kCW_Direction); |
| 151 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 152 | |
| 153 | static const struct { |
| 154 | const char* fPathStr; |
| 155 | SkPath::Convexity fExpectedConvexity; |
| 156 | } gRec[] = { |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 157 | { "", SkPath::kConvex_Convexity }, |
| 158 | { "0 0", SkPath::kConvex_Convexity }, |
| 159 | { "0 0 10 10", SkPath::kConvex_Convexity }, |
reed@google.com | 85b6e39 | 2011-05-15 20:25:17 +0000 | [diff] [blame] | 160 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kConcave_Convexity }, |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 161 | { "0 0 10 10 10 20", SkPath::kConvex_Convexity }, |
| 162 | { "0 0 10 10 10 0", SkPath::kConvex_Convexity }, |
| 163 | { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity }, |
| 164 | { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity }, |
| 165 | }; |
| 166 | |
| 167 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 168 | SkPath path; |
| 169 | setFromString(&path, gRec[i].fPathStr); |
| 170 | SkPath::Convexity c = SkPath::ComputeConvexity(path); |
| 171 | REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void TestPath(skiatest::Reporter* reporter); |
| 176 | void TestPath(skiatest::Reporter* reporter) { |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 177 | { |
| 178 | SkSize size; |
| 179 | size.fWidth = 3.4f; |
| 180 | size.width(); |
| 181 | size = SkSize::Make(3,4); |
| 182 | SkISize isize = SkISize::Make(3,4); |
| 183 | } |
| 184 | |
| 185 | SkTSize<SkScalar>::Make(3,4); |
| 186 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 187 | SkPath p, p2; |
| 188 | SkRect bounds, bounds2; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 189 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 190 | REPORTER_ASSERT(reporter, p.isEmpty()); |
reed@google.com | b54455e | 2011-05-16 14:16:04 +0000 | [diff] [blame] | 191 | REPORTER_ASSERT(reporter, p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 192 | REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType); |
| 193 | REPORTER_ASSERT(reporter, !p.isInverseFillType()); |
| 194 | REPORTER_ASSERT(reporter, p == p2); |
| 195 | REPORTER_ASSERT(reporter, !(p != p2)); |
| 196 | |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 197 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 198 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 199 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 200 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 201 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 202 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 203 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 204 | p.reset(); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 205 | p.addOval(bounds); |
| 206 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 207 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 208 | p.reset(); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 209 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 210 | check_convex_bounds(reporter, p, bounds); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 211 | |
| 212 | REPORTER_ASSERT(reporter, p != p2); |
| 213 | REPORTER_ASSERT(reporter, !(p == p2)); |
| 214 | |
| 215 | // does getPoints return the right result |
| 216 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4); |
| 217 | SkPoint pts[4]; |
| 218 | int count = p.getPoints(pts, 4); |
| 219 | REPORTER_ASSERT(reporter, count == 4); |
| 220 | bounds2.set(pts, 4); |
| 221 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 222 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 223 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 224 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 225 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 226 | |
| 227 | #if 0 // isRect needs to be implemented |
| 228 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
| 229 | bounds.setEmpty(); |
| 230 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 231 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 232 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 233 | // now force p to not be a rect |
| 234 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 235 | p.addRect(bounds); |
| 236 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
| 237 | #endif |
| 238 | |
| 239 | SkPoint pt; |
| 240 | |
| 241 | p.moveTo(SK_Scalar1, 0); |
| 242 | p.getLastPt(&pt); |
| 243 | REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 244 | |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 245 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame] | 246 | test_convexity2(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | #include "TestClassDef.h" |
| 250 | DEFINE_TESTCLASS("Path", PathTestClass, TestPath) |