Snap.svg icon indicating copy to clipboard operation
Snap.svg copied to clipboard

isPointInside returning incorrect results on some paths

Open ratt opened this issue 10 years ago • 9 comments

We are experiencing an issue with isPointInside returning true when the X,Y axis not touching path, we have created a fiddle here https://jsfiddle.net/ratt/tnq0yy8u/6/ to illustrate. If you move the marker around inside the two paths, nothing should be logged to the console. However towards the top and bottom of the letter, the console will log "OUTSIDE" indicating the marker is touching the top/outside path.

We are using the x,y coordinates from the Drag().Move callback to perform the isPointInside test against.

Looking at the code i am assuming a bbox is being made of the marker and the arch on the path is causing the issue ? I am not sure how to resolve this.

ratt avatar Jul 06 '16 05:07 ratt

Just confirming this...I took a quick peek and couldn't get down to the code at fault, but reduced the svg a little to prove the issue.. It seems to have the problem with

http://svg.dabbles.info/ispointinsidetestx.html

Drag the orange circle over the path and to the left/above it as well, you will see it detects points outside. They key bit 'seems' to be the v8.0 bit if thats any use. Any larger than 8 and its ok, any smaller and the error creeps in.

ibrierley avatar Jul 16 '16 13:07 ibrierley

Found it... this is a fun one :stuck_out_tongue_winking_eye: could get tricky [EDIT] I'm almost there on this one, it's been a bit of a can of worms.

ThomasBrierley avatar Feb 03 '17 18:02 ThomasBrierley

So... On the surface this appears to be a small bug, but when building a more thorough test case it becomes clear there are many bugs in here. I've written up a detailed analysis for my own purposes (to keep me sane), I will post it here for reference along with the test case when I'm done finding things.

I want to outline what I think the broader problem with this set of bugs is:

I've been looking at this for a while and I think It's reasonable to say the current approach suffers from over generalisation... don't get me wrong, there is some really clever stuff in here - but ultimately the Point-In-Path test only needs to do a Ray-Casting Point-In-Polygon test on a polygonised sequence of cubic beziers.

The bugs are all in the form of well known ray-vertex counting problem. The procedure is broken up into many generalised pieces and has a number of levels of coarse and fine tests - because of this it is difficult to keep track of all the counting edge cases.

So while i'm tweaking all these little pieces, I'm concerned that even if I manage to fix everything for the Point-In-Path function - that i'll also be introducing subtle issues with other uses of the general functions (both internal and exposed).

I see two options:

  1. Apply all the tiny patches to fix the count errors for Ray-Casting and risk introducing other issues
  2. Write a separate and more holistic method for the Point-In-Path test geared to Ray-Casting

I've done most of No 1, but would be more happy with approach of No 2 (we could keep all the current functions from the original approach in tact)

I guess I'll need Dmitri to choose.

ThomasBrierley avatar Feb 23 '17 12:02 ThomasBrierley

Good work on looking at it whatever, I had a stab and was out of my depth on it. So I think the work you've done is really useful.

ibrierley avatar Feb 23 '17 12:02 ibrierley

Just because I like alternatives...

https://jsfiddle.net/t221uk4v/12/

Hacky I know, I'd rather the proper method be fixed, but whilst it currently doesn't, it could be an approach for some.

It's just using getElementAtPoint after drawing an invisible object and then removing (currently applies to an element, but it could just be a path thats drawn from a path string and removed). I'm sure there would be some issues, but for the initial test case raised here, I wonder if it could be useful.

ibrierley avatar Feb 23 '17 13:02 ibrierley

Hah, never knew about document.elementFromPoint good spot! It appears to be an old and well supported native function: https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint

I wonder what the state of support for SVG fragment DOMs are though? This might be why Dmitri chose to implement it himself at the time. If it is well supported now then it should be ok to just link this to the native function which will no doubt be substantially faster.

ThomasBrierley avatar Feb 23 '17 15:02 ThomasBrierley

I think it would depend on use case with the alternate one though. Would one want it to include a stroke-width for example, maybe some questions on transforms (if used on an element, but may want to avoid that). So I think a mathematical solution is still useful.

ibrierley avatar Feb 23 '17 15:02 ibrierley

Yes, actually I remember the native SVG intersection features have options for including or excluding parts of the rendered shape such as stroke, but I do not think these are programmatically available.

Another and more direct native method that could be used is (counter intuitively) the canvas isPointInPath method: https://www.w3schools.com/tags/canvas_ispointinpath.asp

This would require an offscreen canvas context... however it is still efficient because no actual rendering need take place (no stoke or fill commands etc).

  1. convert SVG path into cubic bezier list (already implemented in Snap)
  2. canvas beginPath command
  3. canvas bezierCurveTo command for each bezier segment (no need to polygonise - win)
  4. canvas isPointInPath command

In theory this should be very efficient, but will need testing to be sure... of course we are then yielding control to the implementors of canvas isPointInPath of browser devs. so should probably check to see how reliable those are before committing to this approach.

ThomasBrierley avatar Feb 23 '17 15:02 ThomasBrierley

This returns false instead of true Snap.path.isPointInside('M287 19 L317 19 L317 27 L287 27', 290, 25)

codermak avatar Apr 12 '17 06:04 codermak