Skip to content

Commit 895aa16

Browse files
committed
fix clipping polyline containing vertices that fails to be projected (i.e. for SIN, TAN proj)
1 parent 9201aff commit 895aa16

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

src/js/View.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,12 +2224,11 @@ export let View = (function () {
22242224
if (!footprint.source || !footprint.source.tooSmallFootprint) {
22252225
const originLineWidth = footprint.getLineWidth();
22262226
let spreadedLineWidth = (originLineWidth || 1) + 3;
2227-
2227+
22282228
footprint.setLineWidth(spreadedLineWidth);
22292229
if (footprint.isShowing && footprint.isInStroke(ctx, this, x * window.devicePixelRatio, y * window.devicePixelRatio)) {
22302230
closests.push(footprint);
22312231
}
2232-
22332232
footprint.setLineWidth(originLineWidth);
22342233
}
22352234
})

src/js/shapes/Circle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ export let Circle = (function() {
317317
};
318318

319319
Circle.prototype.isInStroke = function(ctx, view, x, y) {
320-
this.draw(ctx, view, true);
320+
if (!this.draw(ctx, view, true)) {
321+
return false;
322+
}
321323
return ctx.isPointInStroke(x, y);
322324
};
323325

src/js/shapes/Ellipse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export let Ellipse = (function() {
337337
};
338338

339339
Ellipse.prototype.isInStroke = function(ctx, view, x, y) {
340-
if (!this.draw(ctx, view, true, true)) {
340+
if (!this.draw(ctx, view, true)) {
341341
return false;
342342
}
343343

src/js/shapes/Polyline.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export let Polyline = (function() {
232232
return false;
233233
}
234234

235+
235236
noSmallCheck = noSmallCheck===true || false;
236237
noStroke = noStroke===true || false;
237238

@@ -269,28 +270,37 @@ export let Polyline = (function() {
269270
let ymin = Number.POSITIVE_INFINITY
270271
let ymax = Number.NEGATIVE_INFINITY;
271272

273+
274+
275+
let behind = true;
272276
for (var k=0; k<len; k++) {
273277
var xyview = view.aladin.world2pix(this.raDecArray[k][0], this.raDecArray[k][1]);
278+
274279
if (!xyview) {
275-
return false;
280+
xyView.push(undefined);
281+
} else {
282+
behind = false;
283+
let [x, y] = xyview
284+
xyView.push({x, y});
285+
286+
xmin = Math.min(xmin, x);
287+
ymin = Math.min(ymin, y);
288+
xmax = Math.max(xmax, x);
289+
ymax = Math.max(ymax, y);
276290
}
277-
278-
xyView.push({x: xyview[0], y: xyview[1]});
279-
280-
xmin = Math.min(xmin, xyview[0]);
281-
ymin = Math.min(ymin, xyview[1]);
282-
xmax = Math.max(xmax, xyview[0]);
283-
ymax = Math.max(ymax, xyview[1]);
284291
}
285292

293+
if (behind)
294+
return false;
295+
286296
// 2. do not draw the polygon if it lies outside the view
287297
if (xmax < 0 || xmin > view.width || ymax < 0 || ymin > view.height) {
288298
return false;
289299
}
290300

291301
// do not draw neither if the polygone does not lie inside lineWidth
292302
if (!noSmallCheck) {
293-
this.isTooSmall = (xmax - xmin) < this.lineWidth || (ymax - ymin) < this.lineWidth;
303+
this.isTooSmall = (xmax - xmin) < this.lineWidth && (ymax - ymin) < this.lineWidth;
294304

295305
if (this.isTooSmall) {
296306
return false;
@@ -302,6 +312,10 @@ export let Polyline = (function() {
302312

303313
if (view.projection === ProjectionEnum.SIN) {
304314
drawLine = (v0, v1) => {
315+
if (v0 === undefined || v1 === undefined) {
316+
return false;
317+
}
318+
305319
const l = {x1: v0.x, y1: v0.y, x2: v1.x, y2: v1.y};
306320

307321
if (Polyline.isInsideView(l.x1, l.y1, l.x2, l.y2, view.width, view.height)) {
@@ -311,6 +325,9 @@ export let Polyline = (function() {
311325

312326
if (this.closed && this.fill) {
313327
fillPoly = (v0, v1, index) => {
328+
if (v0 === undefined || v1 === undefined)
329+
return false;
330+
314331
const l = {x1: v0.x, y1: v0.y, x2: v1.x, y2: v1.y};
315332

316333
if (index === 0) {
@@ -391,7 +408,6 @@ export let Polyline = (function() {
391408
v1 = v1 + 1;
392409
}
393410

394-
//ctx.globalAlpha = 1;
395411
ctx.save();
396412
ctx.fillStyle = this.fillColor;
397413
ctx.globalAlpha = this.opacity;
@@ -409,30 +425,41 @@ export let Polyline = (function() {
409425
for (var j = 0; j < this.raDecArray.length; j++) {
410426
var xy = view.aladin.world2pix(this.raDecArray[j][0], this.raDecArray[j][1]);
411427
if (!xy) {
412-
return false;
428+
pointXY.push(undefined)
429+
} else {
430+
pointXY.push({
431+
x: xy[0],
432+
y: xy[1]
433+
});
413434
}
414-
pointXY.push({
415-
x: xy[0],
416-
y: xy[1]
417-
});
418435
}
419436

420437
const lastPointIdx = pointXY.length - 1;
421438
for (var l = 0; l < lastPointIdx; l++) {
422-
const line = {x1: pointXY[l].x, y1: pointXY[l].y, x2: pointXY[l + 1].x, y2: pointXY[l + 1].y}; // new segment
423-
_drawLine(line, ctx, true);
439+
let v1 = pointXY[l];
440+
let v2 = pointXY[l + 1];
424441

425-
if (ctx.isPointInStroke(x, y)) { // x,y is on line?
426-
return true;
442+
if (v1 && v2) {
443+
const line = {x1: v1.x, y1: v1.y, x2: v2.x, y2: v2.y}; // new segment
444+
_drawLine(line, ctx, true);
445+
446+
if (ctx.isPointInStroke(x, y)) { // x, y is on line?
447+
return true;
448+
}
427449
}
428450
}
429451

430452
if(this.closed) {
431-
const line = {x1: pointXY[lastPointIdx].x, y1: pointXY[lastPointIdx].y, x2: pointXY[0].x, y2: pointXY[0].y}; // new segment
432-
_drawLine(line, ctx, true);
453+
let v1 = pointXY[lastPointIdx];
454+
let v2 = pointXY[0];
433455

434-
if (ctx.isPointInStroke(x, y)) { // x,y is on line?
435-
return true;
456+
if (v1 && v2) {
457+
const line = {x1: v1.x, y1: v1.y, x2: v2.x, y2: v2.y}; // new segment
458+
_drawLine(line, ctx, true);
459+
460+
if (ctx.isPointInStroke(x, y)) { // x,y is on line?
461+
return true;
462+
}
436463
}
437464
}
438465

0 commit comments

Comments
 (0)