| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkPathMeasure.h" |
| 10 | |
| 11 | static void TestPathMeasure(skiatest::Reporter* reporter) { |
| 12 | SkPath path; |
| 13 | |
| 14 | path.moveTo(0, 0); |
| 15 | path.lineTo(SK_Scalar1, 0); |
| 16 | path.lineTo(SK_Scalar1, SK_Scalar1); |
| 17 | path.lineTo(0, SK_Scalar1); |
| 18 | |
| 19 | SkPathMeasure meas(path, true); |
| 20 | SkScalar length = meas.getLength(); |
| 21 | SkASSERT(length == SK_Scalar1*4); |
| 22 | |
| 23 | path.reset(); |
| 24 | path.moveTo(0, 0); |
| 25 | path.lineTo(SK_Scalar1*3, SK_Scalar1*4); |
| 26 | meas.setPath(&path, false); |
| 27 | length = meas.getLength(); |
| 28 | REPORTER_ASSERT(reporter, length == SK_Scalar1*5); |
| 29 | |
| 30 | path.reset(); |
| 31 | path.addCircle(0, 0, SK_Scalar1); |
| 32 | meas.setPath(&path, true); |
| 33 | length = meas.getLength(); |
| 34 | // SkDebugf("circle arc-length = %g\n", length); |
| 35 | |
| 36 | for (int i = 0; i < 8; i++) { |
| 37 | SkScalar d = length * i / 8; |
| 38 | SkPoint p; |
| 39 | SkVector v; |
| 40 | meas.getPosTan(d, &p, &v); |
| 41 | #if 0 |
| 42 | SkDebugf("circle arc-length=%g, pos[%g %g] tan[%g %g]\n", |
| 43 | d, p.fX, p.fY, v.fX, v.fY); |
| 44 | #endif |
| 45 | } |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 46 | |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 47 | // Test the behavior following a close not followed by a move. |
| 48 | path.reset(); |
| 49 | path.lineTo(SK_Scalar1, 0); |
| 50 | path.lineTo(SK_Scalar1, SK_Scalar1); |
| 51 | path.lineTo(0, SK_Scalar1); |
| 52 | path.close(); |
| 53 | path.lineTo(-SK_Scalar1, 0); |
| 54 | meas.setPath(&path, false); |
| 55 | length = meas.getLength(); |
| 56 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 4); |
| 57 | meas.nextContour(); |
| 58 | length = meas.getLength(); |
| 59 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 60 | SkPoint position; |
| 61 | SkVector tangent; |
| 62 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 63 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 64 | SkScalarNearlyEqual(position.fX, |
| 65 | -SK_ScalarHalf, |
| 66 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 68 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 69 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 70 | |
| 71 | // Test degenerate paths |
| 72 | path.reset(); |
| 73 | path.moveTo(0, 0); |
| 74 | path.lineTo(0, 0); |
| 75 | path.lineTo(SK_Scalar1, 0); |
| 76 | path.quadTo(SK_Scalar1, 0, SK_Scalar1, 0); |
| 77 | path.quadTo(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1 * 2); |
| 78 | path.cubicTo(SK_Scalar1, SK_Scalar1 * 2, |
| 79 | SK_Scalar1, SK_Scalar1 * 2, |
| 80 | SK_Scalar1, SK_Scalar1 * 2); |
| 81 | path.cubicTo(SK_Scalar1*2, SK_Scalar1 * 2, |
| 82 | SK_Scalar1*3, SK_Scalar1 * 2, |
| 83 | SK_Scalar1*4, SK_Scalar1 * 2); |
| 84 | meas.setPath(&path, false); |
| 85 | length = meas.getLength(); |
| 86 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 6); |
| 87 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 88 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 89 | SkScalarNearlyEqual(position.fX, |
| 90 | SK_ScalarHalf, |
| 91 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 92 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 93 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 94 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 95 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_Scalar1 * 2.5f, &position, &tangent)); |
| 96 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 97 | SkScalarNearlyEqual(position.fX, SK_Scalar1, SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, |
| 99 | SkScalarNearlyEqual(position.fY, SK_Scalar1 * 1.5f)); |
| 100 | REPORTER_ASSERT(reporter, tangent.fX == 0); |
| 101 | REPORTER_ASSERT(reporter, tangent.fY == SK_Scalar1); |
| 102 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_Scalar1 * 4.5f, &position, &tangent)); |
| 103 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 104 | SkScalarNearlyEqual(position.fX, |
| 105 | SkFloatToScalar(2.5f), |
| 106 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 107 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 108 | SkScalarNearlyEqual(position.fY, |
| 109 | SkFloatToScalar(2.0f), |
| 110 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 111 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 112 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 113 | |
| 114 | path.reset(); |
| 115 | path.moveTo(0, 0); |
| 116 | path.lineTo(SK_Scalar1, 0); |
| 117 | path.moveTo(SK_Scalar1, SK_Scalar1); |
| 118 | path.moveTo(SK_Scalar1 * 2, SK_Scalar1 * 2); |
| 119 | path.lineTo(SK_Scalar1, SK_Scalar1 * 2); |
| 120 | meas.setPath(&path, false); |
| 121 | length = meas.getLength(); |
| 122 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 123 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 124 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 125 | SkScalarNearlyEqual(position.fX, |
| 126 | SK_ScalarHalf, |
| 127 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 129 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 130 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 131 | meas.nextContour(); |
| 132 | length = meas.getLength(); |
| 133 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 134 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 135 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 136 | SkScalarNearlyEqual(position.fX, |
| 137 | SkFloatToScalar(1.5f), |
| 138 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 139 | REPORTER_ASSERT(reporter, |
| robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame^] | 140 | SkScalarNearlyEqual(position.fY, |
| 141 | SkFloatToScalar(2.0f), |
| 142 | SkFloatToScalar(0.0001f))); |
| schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 143 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 144 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | #include "TestClassDef.h" |
| 148 | DEFINE_TESTCLASS("PathMeasure", PathMeasureTestClass, TestPathMeasure) |