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(); |
| 16 | // check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
| 17 | check_convexity(reporter, pt, SkPath::kUnknown_Convexity); |
| 18 | |
| 19 | SkPath line; |
| 20 | line.moveTo(12, 20); |
| 21 | line.lineTo(-12, -20); |
| 22 | line.close(); |
| 23 | // check_convexity(reporter, pt, SkPath::kConvex_Convexity); |
| 24 | check_convexity(reporter, pt, SkPath::kUnknown_Convexity); |
| 25 | |
| 26 | SkPath triLeft; |
| 27 | triLeft.moveTo(0, 0); |
| 28 | triLeft.lineTo(1, 0); |
| 29 | triLeft.lineTo(1, 1); |
| 30 | triLeft.close(); |
| 31 | check_convexity(reporter, triLeft, SkPath::kConvex_Convexity); |
| 32 | |
| 33 | SkPath triRight; |
| 34 | triRight.moveTo(0, 0); |
| 35 | triRight.lineTo(-1, 0); |
| 36 | triRight.lineTo(1, 1); |
| 37 | triRight.close(); |
| 38 | check_convexity(reporter, triRight, SkPath::kConvex_Convexity); |
| 39 | |
| 40 | SkPath square; |
| 41 | square.moveTo(0, 0); |
| 42 | square.lineTo(1, 0); |
| 43 | square.lineTo(1, 1); |
| 44 | square.lineTo(0, 1); |
| 45 | square.close(); |
| 46 | check_convexity(reporter, square, SkPath::kConvex_Convexity); |
| 47 | |
| 48 | SkPath redundantSquare; |
| 49 | redundantSquare.moveTo(0, 0); |
| 50 | redundantSquare.lineTo(0, 0); |
| 51 | redundantSquare.lineTo(0, 0); |
| 52 | redundantSquare.lineTo(1, 0); |
| 53 | redundantSquare.lineTo(1, 0); |
| 54 | redundantSquare.lineTo(1, 0); |
| 55 | redundantSquare.lineTo(1, 1); |
| 56 | redundantSquare.lineTo(1, 1); |
| 57 | redundantSquare.lineTo(1, 1); |
| 58 | redundantSquare.lineTo(0, 1); |
| 59 | redundantSquare.lineTo(0, 1); |
| 60 | redundantSquare.lineTo(0, 1); |
| 61 | redundantSquare.close(); |
| 62 | check_convexity(reporter, redundantSquare, SkPath::kConvex_Convexity); |
| 63 | |
| 64 | SkPath bowTie; |
| 65 | bowTie.moveTo(0, 0); |
| 66 | bowTie.lineTo(0, 0); |
| 67 | bowTie.lineTo(0, 0); |
| 68 | bowTie.lineTo(1, 1); |
| 69 | bowTie.lineTo(1, 1); |
| 70 | bowTie.lineTo(1, 1); |
| 71 | bowTie.lineTo(1, 0); |
| 72 | bowTie.lineTo(1, 0); |
| 73 | bowTie.lineTo(1, 0); |
| 74 | bowTie.lineTo(0, 1); |
| 75 | bowTie.lineTo(0, 1); |
| 76 | bowTie.lineTo(0, 1); |
| 77 | bowTie.close(); |
| 78 | check_convexity(reporter, bowTie, SkPath::kConcave_Convexity); |
| 79 | |
| 80 | SkPath spiral; |
| 81 | spiral.moveTo(0, 0); |
| 82 | spiral.lineTo(1, 0); |
| 83 | spiral.lineTo(1, 1); |
| 84 | spiral.lineTo(0, 1); |
| 85 | spiral.lineTo(0,.5); |
| 86 | spiral.lineTo(.5,.5); |
| 87 | spiral.lineTo(.5,.75); |
| 88 | spiral.close(); |
| 89 | // check_convexity(reporter, spiral, SkPath::kConcave_Convexity); |
| 90 | |
| 91 | SkPath dent; |
| 92 | dent.moveTo(0, 0); |
| 93 | dent.lineTo(1, 1); |
| 94 | dent.lineTo(0, 1); |
| 95 | dent.lineTo(-.5,2); |
| 96 | dent.lineTo(-2, 1); |
| 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) { |
| 136 | static const SkPath::Convexity U = SkPath::kUnknown_Convexity; |
| 137 | static const SkPath::Convexity C = SkPath::kConcave_Convexity; |
| 138 | static const SkPath::Convexity V = SkPath::kConvex_Convexity; |
| 139 | |
| 140 | SkPath path; |
| 141 | |
| 142 | REPORTER_ASSERT(reporter, U == SkPath::ComputeConvexity(path)); |
| 143 | path.addCircle(0, 0, 10); |
| 144 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 145 | path.addCircle(0, 0, 10); // 2nd circle |
| 146 | REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path)); |
| 147 | path.reset(); |
| 148 | path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction); |
| 149 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 150 | path.reset(); |
| 151 | path.addRect(0, 0, 10, 10, SkPath::kCW_Direction); |
| 152 | REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path)); |
| 153 | |
| 154 | static const struct { |
| 155 | const char* fPathStr; |
| 156 | SkPath::Convexity fExpectedConvexity; |
| 157 | } gRec[] = { |
| 158 | { "0 0", SkPath::kUnknown_Convexity }, |
| 159 | { "0 0 10 10", SkPath::kUnknown_Convexity }, |
| 160 | { "0 0 10 10 20 20 0 0 10 10", SkPath::kUnknown_Convexity }, |
| 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@android.com | 6b82d1a | 2009-06-03 02:35:01 +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 | |
| 201 | p.setIsConvex(false); |
| 202 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 203 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 204 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 205 | p.reset(); |
| 206 | p.setIsConvex(false); |
| 207 | p.addOval(bounds); |
| 208 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 209 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 210 | p.reset(); |
| 211 | p.setIsConvex(false); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 212 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 213 | check_convex_bounds(reporter, p, bounds); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 214 | |
| 215 | REPORTER_ASSERT(reporter, p != p2); |
| 216 | REPORTER_ASSERT(reporter, !(p == p2)); |
| 217 | |
| 218 | // does getPoints return the right result |
| 219 | REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4); |
| 220 | SkPoint pts[4]; |
| 221 | int count = p.getPoints(pts, 4); |
| 222 | REPORTER_ASSERT(reporter, count == 4); |
| 223 | bounds2.set(pts, 4); |
| 224 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 225 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 226 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 227 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 228 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 229 | |
| 230 | #if 0 // isRect needs to be implemented |
| 231 | REPORTER_ASSERT(reporter, p.isRect(NULL)); |
| 232 | bounds.setEmpty(); |
| 233 | REPORTER_ASSERT(reporter, p.isRect(&bounds2)); |
| 234 | REPORTER_ASSERT(reporter, bounds == bounds2); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 235 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 236 | // now force p to not be a rect |
| 237 | bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2); |
| 238 | p.addRect(bounds); |
| 239 | REPORTER_ASSERT(reporter, !p.isRect(NULL)); |
| 240 | #endif |
| 241 | |
| 242 | SkPoint pt; |
| 243 | |
| 244 | p.moveTo(SK_Scalar1, 0); |
| 245 | p.getLastPt(&pt); |
| 246 | REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 247 | |
| 248 | // check that reset and rewind clear the convex hint back to false |
| 249 | p.setIsConvex(false); |
| 250 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 251 | p.setIsConvex(true); |
| 252 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 253 | p.reset(); |
| 254 | REPORTER_ASSERT(reporter, !p.isConvex()); |
| 255 | p.setIsConvex(true); |
| 256 | REPORTER_ASSERT(reporter, p.isConvex()); |
| 257 | p.rewind(); |
| 258 | REPORTER_ASSERT(reporter, !p.isConvex()); |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame] | 259 | |
| 260 | test_convexity(reporter); |
reed@google.com | 7c42481 | 2011-05-15 04:38:34 +0000 | [diff] [blame^] | 261 | test_convexity2(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | #include "TestClassDef.h" |
| 265 | DEFINE_TESTCLASS("Path", PathTestClass, TestPath) |