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 | |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 36 | // Test the behavior following a close not followed by a move. |
| 37 | path.reset(); |
| 38 | path.lineTo(SK_Scalar1, 0); |
| 39 | path.lineTo(SK_Scalar1, SK_Scalar1); |
| 40 | path.lineTo(0, SK_Scalar1); |
| 41 | path.close(); |
| 42 | path.lineTo(-SK_Scalar1, 0); |
| 43 | meas.setPath(&path, false); |
| 44 | length = meas.getLength(); |
| 45 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 4); |
| 46 | meas.nextContour(); |
| 47 | length = meas.getLength(); |
| 48 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 49 | SkPoint position; |
| 50 | SkVector tangent; |
| 51 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 52 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 53 | SkScalarNearlyEqual(position.fX, |
| 54 | -SK_ScalarHalf, |
| 55 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 56 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 57 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 58 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 59 | |
| 60 | // Test degenerate paths |
| 61 | path.reset(); |
| 62 | path.moveTo(0, 0); |
| 63 | path.lineTo(0, 0); |
| 64 | path.lineTo(SK_Scalar1, 0); |
| 65 | path.quadTo(SK_Scalar1, 0, SK_Scalar1, 0); |
| 66 | path.quadTo(SK_Scalar1, SK_Scalar1, SK_Scalar1, SK_Scalar1 * 2); |
| 67 | path.cubicTo(SK_Scalar1, SK_Scalar1 * 2, |
| 68 | SK_Scalar1, SK_Scalar1 * 2, |
| 69 | SK_Scalar1, SK_Scalar1 * 2); |
| 70 | path.cubicTo(SK_Scalar1*2, SK_Scalar1 * 2, |
| 71 | SK_Scalar1*3, SK_Scalar1 * 2, |
| 72 | SK_Scalar1*4, SK_Scalar1 * 2); |
| 73 | meas.setPath(&path, false); |
| 74 | length = meas.getLength(); |
| 75 | REPORTER_ASSERT(reporter, length == SK_Scalar1 * 6); |
| 76 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 77 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 78 | SkScalarNearlyEqual(position.fX, |
| 79 | SK_ScalarHalf, |
| 80 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 82 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 83 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 84 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_Scalar1 * 2.5f, &position, &tangent)); |
| 85 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 86 | SkScalarNearlyEqual(position.fX, SK_Scalar1, SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 87 | REPORTER_ASSERT(reporter, |
| 88 | SkScalarNearlyEqual(position.fY, SK_Scalar1 * 1.5f)); |
| 89 | REPORTER_ASSERT(reporter, tangent.fX == 0); |
| 90 | REPORTER_ASSERT(reporter, tangent.fY == SK_Scalar1); |
| 91 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_Scalar1 * 4.5f, &position, &tangent)); |
| 92 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 93 | SkScalarNearlyEqual(position.fX, |
| 94 | SkFloatToScalar(2.5f), |
| 95 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 97 | SkScalarNearlyEqual(position.fY, |
| 98 | SkFloatToScalar(2.0f), |
| 99 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 101 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 102 | |
| 103 | path.reset(); |
| 104 | path.moveTo(0, 0); |
| 105 | path.lineTo(SK_Scalar1, 0); |
| 106 | path.moveTo(SK_Scalar1, SK_Scalar1); |
| 107 | path.moveTo(SK_Scalar1 * 2, SK_Scalar1 * 2); |
| 108 | path.lineTo(SK_Scalar1, SK_Scalar1 * 2); |
| 109 | meas.setPath(&path, false); |
| 110 | length = meas.getLength(); |
| 111 | REPORTER_ASSERT(reporter, length == SK_Scalar1); |
| 112 | REPORTER_ASSERT(reporter, meas.getPosTan(SK_ScalarHalf, &position, &tangent)); |
| 113 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 114 | SkScalarNearlyEqual(position.fX, |
| 115 | SK_ScalarHalf, |
| 116 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 117 | REPORTER_ASSERT(reporter, position.fY == 0); |
| 118 | REPORTER_ASSERT(reporter, tangent.fX == SK_Scalar1); |
| 119 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
| 120 | meas.nextContour(); |
| 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 | SkFloatToScalar(1.5f), |
| 127 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 128 | REPORTER_ASSERT(reporter, |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 129 | SkScalarNearlyEqual(position.fY, |
| 130 | SkFloatToScalar(2.0f), |
| 131 | SkFloatToScalar(0.0001f))); |
schenney@chromium.org | 510c6b1 | 2012-01-12 20:04:06 +0000 | [diff] [blame] | 132 | REPORTER_ASSERT(reporter, tangent.fX == -SK_Scalar1); |
| 133 | REPORTER_ASSERT(reporter, tangent.fY == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | #include "TestClassDef.h" |
| 137 | DEFINE_TESTCLASS("PathMeasure", PathMeasureTestClass, TestPathMeasure) |