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