caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 | */ |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 7 | #include "Simplify.h" |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 8 | |
| 9 | namespace UnitTest { |
| 10 | |
| 11 | #include "EdgeWalker.cpp" |
| 12 | |
| 13 | } // end of UnitTest namespace |
| 14 | |
| 15 | #include "Intersection_Tests.h" |
| 16 | |
| 17 | SkPoint leftRight[][4] = { |
| 18 | // equal length |
| 19 | {{10, 10}, {10, 50}, {20, 10}, {20, 50}}, |
| 20 | {{10, 10}, {10, 50}, {10, 10}, {20, 50}}, |
| 21 | {{10, 10}, {10, 50}, {20, 10}, {10, 50}}, |
| 22 | // left top higher |
| 23 | {{10, 0}, {10, 50}, {20, 10}, {20, 50}}, |
| 24 | {{10, 0}, {10, 50}, {10, 10}, {20, 50}}, |
| 25 | {{10, 0}, {10, 50}, {20, 10}, {10, 50}}, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 26 | {{10, 0}, {10, 50}, {20, 10}, {10 + 0.000001f, 40}}, |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 27 | // left top lower |
| 28 | {{10, 20}, {10, 50}, {20, 10}, {20, 50}}, |
| 29 | {{10, 20}, {10, 50}, {10, 10}, {20, 50}}, |
| 30 | {{10, 20}, {10, 50}, {20, 10}, {10, 50}}, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 31 | {{10, 20}, {10, 50}, {20, 10}, {10 + 0.000001f, 40}}, |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 32 | {{10, 20}, {10, 50}, { 0, 0}, {50, 50}}, |
| 33 | // left bottom higher |
| 34 | {{10, 10}, {10, 40}, {20, 10}, {20, 50}}, |
| 35 | {{10, 10}, {10, 40}, {10, 10}, {20, 50}}, |
| 36 | {{10, 10}, {10, 40}, {20, 10}, {10, 50}}, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 37 | {{10, 10}, {10, 40}, {20, 10}, { 0 + 0.000001f, 70}}, |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 38 | // left bottom lower |
| 39 | {{10, 10}, {10, 60}, {20, 10}, {20, 50}}, |
| 40 | {{10, 10}, {10, 60}, {10, 10}, {20, 50}}, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 41 | {{10, 10}, {10, 60}, {20, 10}, {10 + 0.000001f, 50}}, |
| 42 | {{10, 10}, {10, 60}, {20, 10}, {10 + 0.000001f, 40}}, |
| 43 | {{10, 10}, {10, 60}, { 0, 0}, {20 + 0.000001f, 20}}, |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | size_t leftRightCount = sizeof(leftRight) / sizeof(leftRight[0]); |
| 47 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 48 | // older code that worked mostly |
| 49 | static bool operator_less_than(const UnitTest::ActiveEdge& lh, |
| 50 | const UnitTest::ActiveEdge& rh) { |
caryclark@google.com | 9f3e9a5 | 2012-12-10 12:50:53 +0000 | [diff] [blame] | 51 | if ((rh.fAbove.fY - lh.fAbove.fY > lh.fBelow.fY - rh.fAbove.fY |
| 52 | && lh.fBelow.fY < rh.fBelow.fY) |
| 53 | || (lh.fAbove.fY - rh.fAbove.fY < rh.fBelow.fY - lh.fAbove.fY |
| 54 | && rh.fBelow.fY < lh.fBelow.fY)) { |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 55 | const SkPoint& check = rh.fBelow.fY <= lh.fBelow.fY |
| 56 | && lh.fBelow != rh.fBelow ? rh.fBelow : |
| 57 | rh.fAbove; |
| 58 | return (check.fY - lh.fAbove.fY) * (lh.fBelow.fX - lh.fAbove.fX) |
| 59 | < (lh.fBelow.fY - lh.fAbove.fY) * (check.fX - lh.fAbove.fX); |
| 60 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 61 | const SkPoint& check = lh.fBelow.fY <= rh.fBelow.fY |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 62 | && lh.fBelow != rh.fBelow ? lh.fBelow : lh.fAbove; |
| 63 | return (rh.fBelow.fY - rh.fAbove.fY) * (check.fX - rh.fAbove.fX) |
| 64 | < (check.fY - rh.fAbove.fY) * (rh.fBelow.fX - rh.fAbove.fX); |
| 65 | } |
| 66 | |
| 67 | |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 68 | void ActiveEdge_Test() { |
| 69 | UnitTest::InEdge leftIn, rightIn; |
| 70 | UnitTest::ActiveEdge left, right; |
| 71 | left.fWorkEdge.fEdge = &leftIn; |
| 72 | right.fWorkEdge.fEdge = &rightIn; |
| 73 | for (size_t x = 0; x < leftRightCount; ++x) { |
| 74 | left.fAbove = leftRight[x][0]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 75 | left.fTangent = left.fBelow = leftRight[x][1]; |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 76 | right.fAbove = leftRight[x][2]; |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 77 | right.fTangent = right.fBelow = leftRight[x][3]; |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 78 | SkASSERT(left < right); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 79 | SkASSERT(operator_less_than(left, right)); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 80 | SkASSERT(!(right < left)); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 81 | SkASSERT(!operator_less_than(right, left)); |
caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame] | 82 | } |
| 83 | } |