Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 1 | describe('PathKit\'s Pathops', function() { |
Kevin Lubick | 556350d | 2018-10-12 15:21:17 -0400 | [diff] [blame] | 2 | // see https://fiddle.skia.org/c/@discrete_path |
| 3 | function drawStar(X=128, Y=128, R=116) { |
| 4 | let p = PathKit.NewPath(); |
| 5 | p.moveTo(X + R, Y); |
| 6 | for (let i = 1; i < 8; i++) { |
| 7 | let a = 2.6927937 * i; |
| 8 | p.lineTo(X + R * Math.cos(a), Y + R * Math.sin(a)); |
| 9 | } |
| 10 | p.closePath(); |
| 11 | return p; |
| 12 | } |
| 13 | |
| 14 | it('pathops_simplify', function(done) { |
| 15 | function setup(ctx) { |
| 16 | ctx.path = drawStar(); |
| 17 | } |
| 18 | |
| 19 | function test(ctx) { |
| 20 | let path = ctx.path.copy().simplify(); |
| 21 | path.delete(); |
| 22 | } |
| 23 | |
| 24 | function teardown(ctx) { |
| 25 | ctx.path.delete(); |
| 26 | } |
| 27 | |
| 28 | LoadPathKit.then(() => { |
| 29 | benchmarkAndReport('pathops_simplify', setup, test, teardown).then(() => { |
| 30 | done(); |
| 31 | }).catch(reportError(done)); |
| 32 | }); |
| 33 | }); |
| 34 | |
| 35 | it('pathops_diff', function(done) { |
| 36 | function setup(ctx) { |
| 37 | // Values chosen abitrarily to have some overlap and some not. |
| 38 | ctx.path1 = drawStar(X=120, Y=120); |
| 39 | ctx.path2 = drawStar(X=140, Y=145); |
| 40 | } |
| 41 | |
| 42 | function test(ctx) { |
| 43 | let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.DIFFERENCE); |
| 44 | path.delete(); |
| 45 | } |
| 46 | |
| 47 | function teardown(ctx) { |
| 48 | ctx.path1.delete(); |
| 49 | ctx.path2.delete(); |
| 50 | } |
| 51 | |
| 52 | LoadPathKit.then(() => { |
| 53 | benchmarkAndReport('pathops_diff', setup, test, teardown).then(() => { |
| 54 | done(); |
| 55 | }).catch(reportError(done)); |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | it('pathops_intersect', function(done) { |
| 60 | function setup(ctx) { |
| 61 | // Values chosen abitrarily to have some overlap and some not. |
| 62 | ctx.path1 = drawStar(X=120, Y=120); |
| 63 | ctx.path2 = drawStar(X=140, Y=145); |
| 64 | } |
| 65 | |
| 66 | function test(ctx) { |
| 67 | let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.INTERSECT); |
| 68 | path.delete(); |
| 69 | } |
| 70 | |
| 71 | function teardown(ctx) { |
| 72 | ctx.path1.delete(); |
| 73 | ctx.path2.delete(); |
| 74 | } |
| 75 | |
| 76 | LoadPathKit.then(() => { |
| 77 | benchmarkAndReport('pathops_intersect', setup, test, teardown).then(() => { |
| 78 | done(); |
| 79 | }).catch(reportError(done)); |
| 80 | }); |
| 81 | }); |
| 82 | |
| 83 | it('pathops_union', function(done) { |
| 84 | function setup(ctx) { |
| 85 | // Values chosen abitrarily to have some overlap and some not. |
| 86 | ctx.path1 = drawStar(X=120, Y=120); |
| 87 | ctx.path2 = drawStar(X=140, Y=145); |
| 88 | } |
| 89 | |
| 90 | function test(ctx) { |
| 91 | let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.UNION); |
| 92 | path.delete(); |
| 93 | } |
| 94 | |
| 95 | function teardown(ctx) { |
| 96 | ctx.path1.delete(); |
| 97 | ctx.path2.delete(); |
| 98 | } |
| 99 | |
| 100 | LoadPathKit.then(() => { |
| 101 | benchmarkAndReport('pathops_union', setup, test, teardown).then(() => { |
| 102 | done(); |
| 103 | }).catch(reportError(done)); |
| 104 | }); |
| 105 | }); |
| 106 | |
| 107 | it('pathops_xor', function(done) { |
| 108 | function setup(ctx) { |
| 109 | // Values chosen abitrarily to have some overlap and some not. |
| 110 | ctx.path1 = drawStar(X=120, Y=120); |
| 111 | ctx.path2 = drawStar(X=140, Y=145); |
| 112 | } |
| 113 | |
| 114 | function test(ctx) { |
| 115 | let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.XOR); |
| 116 | path.delete(); |
| 117 | } |
| 118 | |
| 119 | function teardown(ctx) { |
| 120 | ctx.path1.delete(); |
| 121 | ctx.path2.delete(); |
| 122 | } |
| 123 | |
| 124 | LoadPathKit.then(() => { |
| 125 | benchmarkAndReport('pathops_xor', setup, test, teardown).then(() => { |
| 126 | done(); |
| 127 | }).catch(reportError(done)); |
| 128 | }); |
| 129 | }); |
| 130 | |
| 131 | it('pathops_reverse_diff', function(done) { |
| 132 | function setup(ctx) { |
| 133 | // Values chosen abitrarily to have some overlap and some not. |
| 134 | ctx.path1 = drawStar(X=120, Y=120); |
| 135 | ctx.path2 = drawStar(X=140, Y=145); |
| 136 | } |
| 137 | |
| 138 | function test(ctx) { |
| 139 | let path = PathKit.MakeFromOp(ctx.path1, ctx.path2, PathKit.PathOp.REVERSE_DIFFERENCE); |
| 140 | path.delete(); |
| 141 | } |
| 142 | |
| 143 | function teardown(ctx) { |
| 144 | ctx.path1.delete(); |
| 145 | ctx.path2.delete(); |
| 146 | } |
| 147 | |
| 148 | LoadPathKit.then(() => { |
| 149 | benchmarkAndReport('pathops_reverse_diff', setup, test, teardown).then(() => { |
| 150 | done(); |
| 151 | }).catch(reportError(done)); |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | }); |