-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug.js
More file actions
379 lines (330 loc) · 11.5 KB
/
Copy pathdebug.js
File metadata and controls
379 lines (330 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"use strict";
var failCases = {},
style1 = { fillColor: null, strokeColor: new paper.Color(0,0,0) },
style2 = { strokeColor: new paper.Color(1,0,0), fillColor: new paper.Color(1,0,0, 0.4) },
style3 = { fillColor: null, strokeColor: new paper.Color(0,0,0, 0.4) },
style4 = { fillColor: new paper.Color(0,0,0, 0.4), strokeColor: null },
styleWindPath = { strokeColor: new paper.Color(0,0,0, 0.4) },
stylePoints = {fillColor: new paper.Color(0, 0, 0)},
styleHi = { strokeColor: new paper.Color(0,0,1), strokeWidth: 1.5 },
mouseTarget, boolTarget, tmpTarget,
oPath, bPath;
function makePath(v) {
return new paper.Path(new paper.Segment([v[0], v[1]], null, [v[2]-v[0], v[3]-v[1]]),
new paper.Segment([v[6], v[7]], [v[4]-v[6], v[5]-v[7]], null));
}
// window.onload = function(){
// // __options.nativeContains = true;
// // paper.install(window);
// paper.setup(document.getElementById("cvs"));
// mouseTarget =paper.project.layers[0];
// boolTarget = new paper.Layer();
// tmpTarget = new paper.Layer();
// var list = document.getElementById('list'), liEl, aEl;
// // loadJSON("out/intersection-fail.json", function(data){
// loadJSON("out/union-fail.json", function(data){
// for (var i = 0; i < data.cases.length; i++)
// failCases[data.cases[i].name] = data.cases[i];
// var names = Object.keys(failCases);
// for (var i = 0; i < names.length; i++) {
// aEl = document.createElement("A");
// aEl.appendChild(document.createTextNode(names[i]));
// liEl = document.createElement("LI");
// liEl.appendChild(aEl);
// list.appendChild(liEl);
// }
// list.addEventListener("click", listSelect, false);
// listSelect(null, document.querySelectorAll("#list > li")[0]);
// }, function(xhr){
// console.error(xhr);
// });
// };
// function listSelect(e, target){
// var thisEl = e ? e.target : target.childNodes[0],
// caseTag = thisEl.childNodes[0].nodeValue,
// activeEl = document.querySelectorAll("li.active"), el;
// for (el in activeEl)
// activeEl[el].className = "";
// thisEl.parentNode.className = "active";
// thisEl.parentNode.scrollIntoViewIfNeeded();
// doCase(failCases[caseTag]);
// }
// function doCase (fcase) {
// drawSlopes(null, null, true);
// mouseTarget.activate();
// var ch = mouseTarget.children.concat(boolTarget.children);
// for (var i = ch.length - 1; i >= 0; i--)
// ch[i].remove();
// boolTarget.activate();
// var op1 = getPath(fcase.op1);
// // markPoints(getSegments(op1), null, "#00f", 0.5);
// var op2 = getPath(fcase.op2);
// // markPoints(getSegments(op2), null, "#0f0", 0.5);
// op1.style = op2.style = style3;
// // var resOriginal = getPath(fcase.res);
// // resOriginal.style = style4;
// var fn = fcase.fn, res;
// if(fn === "subtract")
// res = op2[fn](op1);
// else
// res = op1[fn](op2);
// res.style = style2;
// tmpTarget.addChild(op1);
// tmpTarget.addChild(op2);
// var cp = op1[fn](op2, true);
// tmpTarget.addChild(cp);
// annotate2(cp, false, 0.1);
// oPath = cp;
// mouseTarget.activate();
// // mouseTarget.addChild(op1);
// // mouseTarget.addChild(op2);
// cp = op1[fn](op2, true);
// cp.style = style3;
// boolTarget.addChild(cp);
// annotate2(cp, false, 0.1);
// bPath = cp;
// op1.remove();
// op2.remove();
// paper.view.draw();
// }
function markWinding(p1, p2, fn) {
p1 = p1.clone();
p2 = p2.clone();
p1.style = p2.style = null;
p1.translate([300, 0]);
p2.translate([300, 0]);
var p = p1[fn](p2, true);
p.style = styleWindPath;
var crvs = p.getCurves();
if(!annotateLayer){
mainLayer = paper.project.layers[0];
annotateLayer = new paper.Layer();
}
testWinding(p, p1);
}
// make a Path or a CompoundPath using an SVG style encoded path data
var getPath = function(data) {
if (data === "")
return new paper.Path();
// Get the path data, and determine whether it is a compound path or a
// normal path based on the amount of moveTo commands inside it.
var path = data.match(/m/gi).length > 1
? new paper.CompoundPath()
: new paper.Path();
path.setPathData(data);
return path;
};
function getSegments(p) {
var paths = (p instanceof paper.CompoundPath)? p.children : [p], segments = [];
paths.reduce(function(seg, a){ Array.prototype.push.apply(seg, a.getSegments()); return seg; }, segments);
return segments;
}
// Scroll to zoom
window.onwheel = document.onwheel = scrollToZoom; // w3c, Gecko, IE9+
window.onmousewheel = document.onmousewheel = scrollToZoom; // Webkit, non-standard
function scrollToZoom(e) {
if(e.target.nodeName.toLowerCase() !== 'canvas')
return;
e.preventDefault();
var point = new paper.Point(e.clientX, e.clientY),
delta = (e.deltaY || (e.wheelDeltaY/3)) | 0,
modDelta = Math.abs(delta),
signDelta = delta < 0 ? -1 : 1,
scale = 1 + signDelta * map(modDelta, 0, 10, 0, 0.1);
mouseTarget.scale(scale, point);
boolTarget.scale(scale, point);
// tmpTarget.scale(scale, point);
if(window.annotateLayer){
annotateLayer.scale(scale, point);
monCrvs.length = 0;
annotate(pathMain, true);
}
paper.view.draw();
}
// Map val in range to domain
function map (val, minR, maxR, minD, maxD) {
val = val > maxR ? maxR : (val < minR ? minR : val);
return minD + (val - minR) * (maxD - minD) / (maxR - minR);
}
var moveTool = new paper.Tool(), currentItem;
moveTool.onMouseDown = function(event) {
// var hr = mouseTarget.hitTest(event.point, {stroke: true, fill: true});
var hr = bPath.hitTest(event.point, {stroke: true, fill: true});
// var hr = boolTarget.hitTest(event.point, {stroke: true, fill: true});
if(hr && hr.item){
currentItem = hr.item;
if(hr.location){
var crvs = hr.location.curve._path._parent.curves,
index = crvs.indexOf(hr.location.curve),
crv = oPath.curves[index];
window.__checkPoint = crv.getPointAt(0.5, true);
window.__crv = crv.getValues();
console.log(crv.getPointAt(0.5, true), crv.length, crv._getWinding());
// console.log(crv.getValues(), crv.length, crv._getWinding());
}
if(currentItem.parent && currentItem.parent instanceof CompoundPath){
currentItem = currentItem.parent;
}
}
}
moveTool.onMouseUp = function(event) {
currentItem = null;
}
moveTool.onMouseDrag = function(event) {
if(currentItem){
currentItem.translate(event.delta);
}
};
moveTool.onKeyDown = function(event) {
event.event.preventDefault();
switch(event.key){
case 'up':
var els = document.querySelectorAll("#list > li");
if(els && els.length){
for (var i = 0; i < els.length; i++)
if(els[i].className === 'active')
break;
if(i > 0)
listSelect(null, els[i-1]);
}
break;
case 'down':
var els = document.querySelectorAll("li.active + li");
if(els && els.length)
listSelect(null, els[0]);
break;
}
};
// ================================================================
// Timing and debug
function markPoint(pnt, t, c, r) {
if (!pnt) return;
if (pnt.point) pnt = pnt.point;
c = c || '#000';
r = r || 0.05;
var cir = new paper.Path.Circle(pnt, r);
cir.style.fillColor = c;
if (t !== undefined && t !== null) {
var text = new paper.PointText();
text.justification = 'center';
text.fillColor = c;
text.content = t;
text.fontSize = 0.5;
text.point = pnt.subtract([0, text.bounds.height]);
}
}
function markPoints(pnts, t, c, r) {
pnts.map(function(a){ markPoint(a, t, c, r) });
}
/**
* http://stackoverflow.com/questions/6875625/does-javascript-provide-a-high-resolution-timer
*/
if (window.performance && window.performance.now) {
console.log("Using high performance timer");
window.getTimestamp = function() { return window.performance.now(); };
} else {
if (window.performance && window.performance.webkitNow) {
console.log("Using webkit high performance timer");
window.getTimestamp = function() { return window.performance.webkitNow(); };
} else {
console.log("Using low performance timer");
window.getTimestamp = function() { return new Date().getTime(); };
}
}
function loadJSON(path, success, error)
{
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
if (success)
success(JSON.parse(xhr.responseText));
} else {
if (error)
error(xhr);
}
}
};
xhr.open("GET", path, true);
xhr.send();
}
function valsEq(v1, v2){
var eq = true;
for (var i = 0; i < 8; i++) {
eq = eq && (Math.abs(v1[i] - v2[i]) < 1);
}
return eq;
}
function annotate2(path, clear, r){
clear = true;
if (!path)
return;
r = r || 1;
var crvs = path.getCurves(), i, li,
segs = getSegments(path), p1,
crv, ncrv, p, p2, n, windLeft = path.isClockwise() ? -1 : 1;
// if(clear)
// for (i = mouseTarget.children.length - 1; i >= 0; i--)
// mouseTarget.children[i].remove();
// console.time("wind");
for (i = 0, li = crvs.length; i < li; i++) {
crv = crvs[i];
p1 = new paper.Path.Circle(crv.getSegment1().point, r);
p1.style = stylePoints;
// var crss = pathMain._getWinding(segs[i].getCurve().getSegment1().getPoint());
var crss = crvs[i]._getWinding();
// var crss = getWindingContribution(segs[i].getCurve(), crvs);
p = crv.getPoint(0.55);
n = crv.getNormal(0.5).normalize(windLeft);
p2 = p.add(n);
text(p2, crss);
// n = crv.getNormal(0.5).normalize(windLeft * -5);
// p2 = p.add(n).subtract([3.5, -3.5]);
// text(p2, (crss-1));
}
}
function pathFromCurve(crv) {
mouseTarget.activate();
var p = new paper.Path(crv.getSegment1(), crv.getSegment2());
boolTarget.activate();
return p;
}
function text(p, s, c){
var txt = new paper.PointText([0,0]);
txt.fillColor = c || new paper.Color(0,0,0,0.4);
txt.content = s;
txt.fontSize = 2;
txt.point = p.subtract(txt.bounds.center);
return txt;
}
function hilightCrv(c, dontClear){
window.__Crvs = window.__Crvs || [];
if (!dontClear)
for (var i = window.__Crvs.length - 1; i >= 0; i--) {
window.__Crvs[i].remove();
}
if(c) {
if(c[0]){
var v = c;
c = new paper.Curve(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
}
c = pathFromCurve(c);
c.style = styleHi;
window.__Crvs.push(c);
}
paper.view.draw();
}
function drawSlopes(pnt, vec, clear) {
window.__slopes = window.__slopes || [];
if (clear)
for (var i = window.__slopes.length - 1; i >= 0; i--) {
window.__slopes[i].remove();
}
if (pnt) {
var l = new paper.Path.Line(pnt, pnt.add(vec));
l.style = style1;
window.__slopes.push(l);
}
paper.view.draw();
}