epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 8 | #include "Test.h" |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 9 | #include "TestClassDef.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 10 | #include "SkPathMeasure.h" |
| 11 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 12 | static void test_small_segment3() { |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 13 | SkPath path; |
| 14 | const SkPoint pts[] = { |
| 15 | { 0, 0 }, |
| 16 | { 100000000000.0f, 100000000000.0f }, { 0, 0 }, { 10, 10 }, |
| 17 | { 10, 10 }, { 0, 0 }, { 10, 10 } |
| 18 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 20 | path.moveTo(pts[0]); |
bsalomon@google.com | 2fc2359 | 2012-10-03 19:10:31 +0000 | [diff] [blame] | 21 | for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 3) { |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 22 | path.cubicTo(pts[i], pts[i + 1], pts[i + 2]); |
| 23 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 25 | SkPathMeasure meas(path, false); |
| 26 | meas.getLength(); |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 27 | } |
| 28 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 29 | static void test_small_segment2() { |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 30 | SkPath path; |
| 31 | const SkPoint pts[] = { |
| 32 | { 0, 0 }, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 33 | { 100000000000.0f, 100000000000.0f }, { 0, 0 }, |
| 34 | { 10, 10 }, { 0, 0 }, |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 35 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 36 | |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 37 | path.moveTo(pts[0]); |
| 38 | for (size_t i = 1; i < SK_ARRAY_COUNT(pts); i += 2) { |
| 39 | path.quadTo(pts[i], pts[i + 1]); |
| 40 | } |
| 41 | SkPathMeasure meas(path, false); |
| 42 | meas.getLength(); |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 43 | } |
| 44 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 45 | static void test_small_segment() { |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 46 | SkPath path; |
| 47 | const SkPoint pts[] = { |
| 48 | { 100000, 100000}, |
| 49 | // big jump between these points, makes a big segment |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 50 | { 1.0005f, 0.9999f }, |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 51 | // tiny (non-zero) jump between these points |
robertphillips@google.com | c6ce750 | 2012-05-08 13:15:37 +0000 | [diff] [blame] | 52 | { SK_Scalar1, SK_Scalar1 }, |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 53 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 55 | path.moveTo(pts[0]); |
| 56 | for (size_t i = 1; i < SK_ARRAY_COUNT(pts); ++i) { |
| 57 | path.lineTo(pts[i]); |
| 58 | } |
| 59 | SkPathMeasure meas(path, false); |
| 60 | |
| 61 | /* this would assert (before a fix) because we added a segment with |
| 62 | the same length as the prev segment, due to the follow (bad) pattern |
| 63 | |
| 64 | d = distance(pts[0], pts[1]); |
| 65 | distance += d; |
| 66 | seg->fDistance = distance; |
reed@google.com | ded4414 | 2012-04-27 20:22:07 +0000 | [diff] [blame] | 67 | |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 68 | SkASSERT(d > 0); // TRUE |
| 69 | SkASSERT(seg->fDistance > prevSeg->fDistance); // FALSE |
| 70 | |
| 71 | This 2nd assert failes because (distance += d) didn't affect distance |
| 72 | because distance >>> d. |
| 73 | */ |
| 74 | meas.getLength(); |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 75 | } |
| 76 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 77 | DEF_TEST(PathMeasure, reporter) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 78 | SkPath path; |
| 79 | |
| 80 | path.moveTo(0, 0); |
| 81 | path.lineTo(SK_Scalar1, 0); |
| 82 | path.lineTo(SK_Scalar1, SK_Scalar1); |
| 83 | path.lineTo(0, SK_Scalar1); |
| 84 | |
| 85 | SkPathMeasure meas(path, true); |
| 86 | SkScalar length = meas.getLength(); |
| 87 | SkASSERT(length == SK_Scalar1*4); |
| 88 | |
| 89 | path.reset(); |
| 90 | path.moveTo(0, 0); |
| 91 | path.lineTo(SK_Scalar1*3, SK_Scalar1*4); |
| 92 | meas.setPath(&path, false); |
| 93 | length = meas.getLength(); |
| 94 | REPORTER_ASSERT(reporter, length == SK_Scalar1*5); |
| 95 | |
| 96 | path.reset(); |
| 97 | path.addCircle(0, 0, SK_Scalar1); |
| 98 | meas.setPath(&path, true); |
| 99 | length = meas.getLength(); |
| 100 | // SkDebugf("circle arc-length = %g\n", length); |
| 101 | |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 102 | // Test the behavior following a close not followed by a move. |
| 103 | path.reset(); |
| 104 | path.lineTo(SK_Scalar1, 0); |
| 105 | path.lineTo(SK_Scalar1, SK_Scalar1); |
| 106 | path.lineTo(0, SK_Scalar1); |
| 107 | path.close(); |
| 108 | path.lineTo(-SK_Scalar1, 0); |
| 109 | meas.setPath(&path, false); |
| 110 | length = meas.getLength(); |
| 111 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 4); |
| 112 | meas.nextContour(); |
| 113 | length = meas.getLength(); |
| 114 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 115 | SkPoint position; |
| 116 | SkVector tangent; |
| 117 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 118 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 119 | SkScalarNearlyEqual(position.fX, |
| 120 | -SK_ScalarHalf, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 121 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 122 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 123 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 124 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 125 | |
| 126 | // Test degenerate paths |
| 127 | path.reset(); |
| 128 | path.moveTo(0, 0); |
| 129 | path.lineTo(0, 0); |
| 130 | path.lineTo(SK_Scalar1, 0); |
| 131 | path.quadTo(SK_Scalar1, 0, SK_Scalar1, 0); |
| 132 | path.quadTo(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1 * 2); |
| 133 | path.cubicTo(SK_Scalar1, SK_Scalar1 * 2, |
| 134 | SK_Scalar1, SK_Scalar1 * 2, |
| 135 | SK_Scalar1, SK_Scalar1 * 2); |
| 136 | path.cubicTo(SK_Scalar1*2, SK_Scalar1 * 2, |
| 137 | SK_Scalar1*3, SK_Scalar1 * 2, |
| 138 | SK_Scalar1*4, SK_Scalar1 * 2); |
| 139 | meas.setPath(&path, false); |
| 140 | length = meas.getLength(); |
| 141 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 6); |
| 142 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 143 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 144 | SkScalarNearlyEqual(position.fX, |
| 145 | SK_ScalarHalf, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 146 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 147 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 148 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 149 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, meas.getPosTan(2.5f, &position, &tangent)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 151 | REPORTER_ASSERT(reporter, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 152 | SkScalarNearlyEqual(position.fX, SK_Scalar1, 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 153 | REPORTER_ASSERT(reporter, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 154 | SkScalarNearlyEqual(position.fY, 1.5f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 155 | REPORTER_ASSERT(reporter, tangent.fX == 0); |
| 156 | REPORTER_ASSERT(reporter, tangent.fY == SK_Scalar1); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 157 | REPORTER_ASSERT(reporter, meas.getPosTan(4.5f, &position, &tangent)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 158 | REPORTER_ASSERT(reporter, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 159 | SkScalarNearlyEqual(position.fX, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 160 | 2.5f, |
| 161 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 162 | REPORTER_ASSERT(reporter, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 163 | SkScalarNearlyEqual(position.fY, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 164 | 2.0f, |
| 165 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 166 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 167 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 168 | |
| 169 | path.reset(); |
| 170 | path.moveTo(0, 0); |
| 171 | path.lineTo(SK_Scalar1, 0); |
| 172 | path.moveTo(SK_Scalar1, SK_Scalar1); |
| 173 | path.moveTo(SK_Scalar1 * 2, SK_Scalar1 * 2); |
| 174 | path.lineTo(SK_Scalar1, SK_Scalar1 * 2); |
| 175 | meas.setPath(&path, false); |
| 176 | length = meas.getLength(); |
| 177 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 178 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 179 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 180 | SkScalarNearlyEqual(position.fX, |
| 181 | SK_ScalarHalf, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 182 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 183 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 184 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 185 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 186 | meas.nextContour(); |
| 187 | length = meas.getLength(); |
| 188 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 189 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 190 | REPORTER_ASSERT(reporter, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 191 | SkScalarNearlyEqual(position.fX, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 192 | 1.5f, |
| 193 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 194 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 195 | SkScalarNearlyEqual(position.fY, |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 196 | 2.0f, |
| 197 | 0.0001f)); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 198 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 199 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
reed@google.com | fab1ddd | 2012-04-20 15:10:32 +0000 | [diff] [blame] | 200 | |
sugoi@google.com | 54f0d1b | 2013-02-27 19:17:41 +0000 | [diff] [blame] | 201 | test_small_segment(); |
| 202 | test_small_segment2(); |
| 203 | test_small_segment3(); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 204 | } |