Format more consistently with the rest of Skia.
authorturk@google.com <turk@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 4 Mar 2009 01:33:35 +0000 (01:33 +0000)
committerturk@google.com <turk@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Wed, 4 Mar 2009 01:33:35 +0000 (01:33 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@106 2bbb7eff-a529-9590-31e7-b0007b416f81

src/core/SkCubicClipper.cpp
src/core/SkCubicClipper.h
tests/ClipCubicTest.cpp

index d11eb99..6e47513 100644 (file)
@@ -77,7 +77,7 @@ static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) {
 #else  // BISECTION    // Linear convergence, typically 16 iterations.
 
     // Check that the endpoints straddle zero.
-    SkScalar tNeg, tPos;    // Parameter where the function is negative and positive, respectively.
+    SkScalar tNeg, tPos;    // Negative and positive function parameters.
     if (ycrv[0] < 0) {
         if (ycrv[3] < 0)
             return false;
@@ -92,7 +92,7 @@ static bool chopMonoCubicAtY(SkPoint pts[4], SkScalar y, SkScalar* t) {
         *t = 0;
         return true;
     }
-    
+
     const SkScalar tol = SK_Scalar1 / 65536;  // 1 for fixed, 1e-5 for float.
     int iters = 0;
     do {
@@ -139,10 +139,10 @@ bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
     if (dst[3].fY <= ctop || dst[0].fY >= cbot) {
         return false;
     }
-    
+
     SkScalar t;
     SkPoint tmp[7]; // for SkChopCubicAt
-    
+
     // are we partially above
     if (dst[0].fY < ctop && chopMonoCubicAtY(dst, ctop, &t)) {
         SkChopCubicAt(dst, tmp, t);
@@ -150,7 +150,7 @@ bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
         dst[1] = tmp[4];
         dst[2] = tmp[5];
     }
-    
+
     // are we partially below
     if (dst[3].fY > cbot && chopMonoCubicAtY(dst, cbot, &t)) {
         SkChopCubicAt(dst, tmp, t);
@@ -158,7 +158,7 @@ bool SkCubicClipper::clipCubic(const SkPoint srcPts[4], SkPoint dst[4]) {
         dst[2] = tmp[2];
         dst[3] = tmp[3];
     }
-    
+
     if (reverse) {
         SkTSwap<SkPoint>(dst[0], dst[3]);
         SkTSwap<SkPoint>(dst[1], dst[2]);
index 546abae..9a45b31 100644 (file)
 
 /** This class is initialized with a clip rectangle, and then can be fed cubics,
     which must already be monotonic in Y.
+
     In the future, it might return a series of segments, allowing it to clip
     also in X, to ensure that all segments fit in a finite coordinate system.
  */
 class SkCubicClipper {
 public:
     SkCubicClipper();
-    
+
     void setClip(const SkIRect& clip);
 
     bool clipCubic(const SkPoint src[4], SkPoint dst[4]);
index 0cd1d85..e9820f6 100644 (file)
@@ -11,138 +11,130 @@ static void PrintCurve(const char *name, const SkPoint crv[4]) {
             crv[1].fX, crv[1].fY,
             crv[2].fX, crv[2].fY,
             crv[3].fX, crv[3].fY);
-                     
+
 }
 
 
-static bool CurvesAreEqual(
-  const SkPoint c0[4], const SkPoint c1[4], float tol) {
-      for (int i = 0; i < 4; i++) {
-          if (SkScalarAbs(c0[i].fX - c1[i].fX) > SkFloatToScalar(tol) ||
-              SkScalarAbs(c0[i].fY - c1[i].fY) > SkFloatToScalar(tol)
-          ) {
-              PrintCurve("c0", c0);
-              PrintCurve("c1", c1);
-              return false;
-          }
-      }
-      return true;
+static bool CurvesAreEqual(const SkPoint c0[4],
+                           const SkPoint c1[4],
+                           float tol) {
+    for (int i = 0; i < 4; i++) {
+        if (SkScalarAbs(c0[i].fX - c1[i].fX) > SkFloatToScalar(tol) ||
+            SkScalarAbs(c0[i].fY - c1[i].fY) > SkFloatToScalar(tol)
+        ) {
+            PrintCurve("c0", c0);
+            PrintCurve("c1", c1);
+            return false;
+        }
+    }
+    return true;
 }
 
 
-static SkPoint* SetCurve(
-    float x0, float y0,
-    float x1, float y1,
-    float x2, float y2,
-    float x3, float y3,
-    SkPoint crv[4]
-) {
+static SkPoint* SetCurve(float x0, float y0,
+                         float x1, float y1,
+                         float x2, float y2,
+                         float x3, float y3,
+                         SkPoint crv[4]) {
     crv[0].fX = SkFloatToScalar(x0);   crv[0].fY = SkFloatToScalar(y0);
     crv[1].fX = SkFloatToScalar(x1);   crv[1].fY = SkFloatToScalar(y1);
     crv[2].fX = SkFloatToScalar(x2);   crv[2].fY = SkFloatToScalar(y2);
     crv[3].fX = SkFloatToScalar(x3);   crv[3].fY = SkFloatToScalar(y3);
     return crv;
 }
-    
+
 
 static void TestCubicClipping(skiatest::Reporter* reporter) {
-  static SkPoint crv[4] = {
-    { SkFloatToScalar(0), SkFloatToScalar(0)  },
-    { SkFloatToScalar(2), SkFloatToScalar(3)  },
-    { SkFloatToScalar(1), SkFloatToScalar(10) },
-    { SkFloatToScalar(4), SkFloatToScalar(12) }
-  };
-  
-  SkCubicClipper clipper;
-  SkPoint clipped[4], shouldbe[4];
-  SkIRect clipRect;
-  bool success;
-  const float tol = 1e-4;
-
-  // Test no clip, with plenty of room.
-  clipRect.set(-2, -2, 6, 14);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0, 0, 2, 3, 1, 10, 4, 12, shouldbe
-  ), tol));
-
-  // Test no clip, touching first point.
-  clipRect.set(-2, 0, 6, 14);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0, 0, 2, 3, 1, 10, 4, 12, shouldbe
-  ), tol));
-
-  // Test no clip, touching last point.
-  clipRect.set(-2, -2, 6, 12);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0, 0, 2, 3, 1, 10, 4, 12, shouldbe
-  ), tol));
-
-  // Test all clip.
-  clipRect.set(-2, 14, 6, 20);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == false);
-
-  // Test clip at 1.
-  clipRect.set(-2, 1, 6, 14);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0.5126125216, 1,
-      1.841195941,  4.337081432,
-      1.297019958,  10.19801331,
-      4,            12,
-      shouldbe
-  ), tol));
-
-  // Test clip at 2.
-  clipRect.set(-2, 2, 6, 14);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      00.8412352204, 2,
-      1.767683744,   5.400758266,
-      1.55052948,    10.36701965,
-      4,             12,
-      shouldbe
-  ), tol));
-
-  // Test clip at 11.
-  clipRect.set(-2, -2, 6, 11);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0,           0,
-      1.742904663, 2.614356995,
-      1.207521796, 8.266430855,
-      3.026495695, 11,
-      shouldbe
-  ), tol));
-
-  // Test clip at 10.
-  clipRect.set(-2, -2, 6, 10);
-  clipper.setClip(clipRect);
-  success = clipper.clipCubic(crv, clipped);
-  REPORTER_ASSERT(reporter, success == true);
-  REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
-      0,           0,
-      1.551193237, 2.326789856,
-      1.297736168, 7.059780121,
-      2.505550385, 10,
-      shouldbe
-  ), tol));
+    static SkPoint crv[4] = {
+        { SkFloatToScalar(0), SkFloatToScalar(0)  },
+        { SkFloatToScalar(2), SkFloatToScalar(3)  },
+        { SkFloatToScalar(1), SkFloatToScalar(10) },
+        { SkFloatToScalar(4), SkFloatToScalar(12) }
+    };
+
+    SkCubicClipper clipper;
+    SkPoint clipped[4], shouldbe[4];
+    SkIRect clipRect;
+    bool success;
+    const float tol = 1e-4;
+
+    // Test no clip, with plenty of room.
+    clipRect.set(-2, -2, 6, 14);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
+
+    // Test no clip, touching first point.
+    clipRect.set(-2, 0, 6, 14);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
+
+    // Test no clip, touching last point.
+    clipRect.set(-2, -2, 6, 12);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
+
+    // Test all clip.
+    clipRect.set(-2, 14, 6, 20);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == false);
+
+    // Test clip at 1.
+    clipRect.set(-2, 1, 6, 14);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0.5126125216, 1,
+        1.841195941,  4.337081432,
+        1.297019958,  10.19801331,
+        4,            12,
+        shouldbe), tol));
+
+    // Test clip at 2.
+    clipRect.set(-2, 2, 6, 14);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        00.8412352204, 2,
+        1.767683744,   5.400758266,
+        1.55052948,    10.36701965,
+        4,             12,
+        shouldbe), tol));
+
+    // Test clip at 11.
+    clipRect.set(-2, -2, 6, 11);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0,           0,
+        1.742904663, 2.614356995,
+        1.207521796, 8.266430855,
+        3.026495695, 11,
+        shouldbe), tol));
+
+    // Test clip at 10.
+    clipRect.set(-2, -2, 6, 10);
+    clipper.setClip(clipRect);
+    success = clipper.clipCubic(crv, clipped);
+    REPORTER_ASSERT(reporter, success == true);
+    REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
+        0,           0,
+        1.551193237, 2.326789856,
+        1.297736168, 7.059780121,
+        2.505550385, 10,
+        shouldbe), tol));
 }