Point-in-polygon code or trick

Started by prl, December 07, 2012, 06:47:22 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

prl

Quote from: MarkX on December 07, 2012, 05:34:20 PM
I was thinking more about defining a tiny sphere around the pointmark. Remember, one of PRL's approaches allowed for a little overlap error.

Wow, you guys gave me an idea which is a variation of this.

1.  Draw a line from the test point to one of the polygon corners.  (All test points are already in the plane of the polygon so the new line is also in the plane of the polygon).

2.  Use the perpendicular plane command and create a small plane at the test point using this new line.  This little test plane will be normal to the main plane.  It sticks out on each side.

3a.  Now define each plane as a solid and do a solid interference check.  If interference then the test point is within the polygon boundaries.

or doing a slightly different trick

3b.  use the surface intersection command on the little plane and the main plane.  Check to see if a line was created (entity was added).  If yes, then the test point is within the polygon boundaries.


Dr PR

prl,

3b should work so long as the line from the test point goes to the nearest point on the polygon and not to a point far across the polygon.

I've never tried the interference test on objects that weren't actually true solids (with volume).

Phil
DesignCAD user since 1987

prl

#17
Quote from: Dr PR on December 07, 2012, 09:09:04 PM
I've never tried the interference test on objects that weren't actually true solids (with volume).

For 3a looks like you are right.  At first I thought this worked but now I get false positives.

Manually method 3b seems to work.  I'm using a test plane of .1".  For the test point, doesn't seem to care which vertex I draw the line too.  Only dead zone  is I need to check the test point location relative to the edges. Stay at least half the distance of the test plane away otherwise I could have a test point outside the boundaries but still have a bit of the test plane inside the boundaries giving a false positive.

prl

#18
Quote from: Bob P on December 07, 2012, 10:39:20 AM
Try to draw a line like this:
>Set a point at the proposed location
>Planesnap
>Enter
If the line creation fails, the point is on the plane.  Otherwise, you'll get a line whose other end falls on the plane.

Bob, this is a clever approach too.  If I set a point outside the boundaries and do a planesnap, I would expect the point to move somewhere else.  If it is already on the surface, I would expect my test point to be in the same location before and after the planesnap.  Wonder if the planesnap has a physical snap range or pixel range that would cause it not to work giving me a false positive.  Need to test method.

Bob P

I hate the way planesnap works!  If the beginning point is "outside" the plane's projection, planesnap goes to an edge of the plane.  I think it should snap to a point on the virtual plane defined by a selection, or points.  It's a Basicad project on my list.  I think somewhere around I have a page printed out on how to mathematically define a plane.  Then it's just a matter of creating a normal that intersects the original point.  There was a matrix solution.  Fairly complex, as I recall.  Three simultaneous equations that had to be merged and simplified. 

The frustrating thing is, I suspect that the developers already have the mathematical solution hiding in the program's source code.

prl

#20
Bob, I'm making some decent progress using your approach.

Quote from: Bob P on December 08, 2012, 03:30:10 AM
. . . I think somewhere around I have a page printed out on how to mathematically define a plane.  Then it's just a matter of creating a normal that intersects the original point.

I probably have that coded somewhere.  You want to set three points to define a mathematical plane (aka a real plane, not a DC surface), and generate a  line (normal) between your 4th point and that mathematical plane, yes?  The normal line may not necessarily intersect the DC surface but it does the mathematical plane defined by the DC surface.

FWIW:  Always disliked the terminology DC uses for surfaces.

MarkX

Quote from: prl on December 08, 2012, 02:12:27 AM
Quote from: Bob P on December 07, 2012, 10:39:20 AM
Try to draw a line like this:
>Set a point at the proposed location
>Planesnap
>Enter
If the line creation fails, the point is on the plane.  Otherwise, you'll get a line whose other end falls on the plane.

Bob, this is a clever approach too.  If I set a point outside the boundaries and do a planesnap, I would expect the point to move somewhere else.  If it is already on the surface, I would expect my test point to be in the same location before and after the planesnap.  Wonder if the planesnap has a physical snap range or pixel range that would cause it not to work giving me a false positive.  Need to test method.

In my quick test, the plane snap always found the plane, even if the starting point was outside the plane. Or maybe (most likely) I'm  not implementing the test correctly. Perhaps it depends on what view you use?

Mark

prl

#22
Well here is a testing macro that seems to work.  It uses the planesnap approach.  I'm surprised it is so brief.

'program:  pop.d3m  (point on plane)

'12/8/2012
'experimenting with determining if a test point (x1, y1, z1) is on a DC surface

precision 15

sys(1116) = 0 
pf = 0.000000000001  'point fuzz

layer(1)=14
here:
  setpoint "Set a test point, enter to end loop" 1
 
  if sys(1)=0 then End
 
  pointval x1 y1 z1 1
  sys(1)=0
   
  'determine if point is on surface   
  >MoveCursor
  {
  <planesnap [x1, y1, z1]
  }
 
  getxy  xn, yn, zn
 
  dx = abs(x1 - xn)
  dy = abs(y1 - yn)
  dz = abs(z1 - zn)
 
  if dx<pf and dy<pf and dz<pf then
     Message "INSIDE plane"
  else
     Message "OUTSIDE plane"
  endif
goto here

End


Also attached is a sample test drawing that goes with this test macro.

To simulate a routine that generates test points, I ran the macro and gravity snapped to the arrow heads.  That feeds the macro the test point (x1,y1,z1) data.

Note this code isn't intended to check non-coplanar points or points directly on the boundary. Since other routines will generate the test point, those conditions will already be satisfied.

I appreciate the great ideas.



Bob P

Quote from: MarkX
In my quick test, the plane snap always found the plane, even if the starting point was outside the plane. Or maybe (most likely) I'm  not implementing the test correctly. Perhaps it depends on what view you use?
Mark

My method only works when the checking point falls within a projection perpendicular to the plane.  That's why I complained about how planesnap works.  Using it, if the original point falls outside that volume, planesnap will snap to the nearest edge of the area (and therefore blow up my test).