DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Point-in-polygon code or trick

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

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

prl

I'm hunting for some code or DC trick to test whether a point in 3d lies within a closed planar polygon also in 3d, specifically a DesignCAD plane?  Anyone been there already with DesignCAD? 


Bob P

A macro is going to be pretty complicated.  A more conventional test could be through using points on the polygon to define a working plane, then checking the delta-z from a polygon point to the new point.

prl

Thanks Bob.

The point inside or outside of a polygon does seem to be challenge, especially if the polygon is shaped like a "C" or something even more zig-zags.

What you mention, to determine whether a 3d point is above or below a 3d plane, that one I have. 


For a few years now I've been using some code to help fix some of the surface normal issues in DesignCAD when exporting.  I have much of this working but this piece of code is still missing.  There are some fortran and C, C++ versions of doing it, I'm just lazy.

MarkX

For a point inside the plane ...

Draw a line from the edge of the plane to the point. Then use the extend tool to extend the point to another line on the opposite side of the plane and use line snap "K" and attempt to snap it there. If the point was not on the plane, you will not be able to snap exactly to the far side.

Sort of works. Problem is, if you don't have precision cranked up, even points on the plane may fail the test.

Good luck,
Mark

prl

Quote from: MarkX on December 07, 2012, 08:34:55 AM
If the point was not on the plane, you will not be able to snap exactly to the far side.

Thanks Mark.  Determining if a 3d point is in the 3d plane (not above or below), that routine I have in basiccad.  If anyone needs that, let me know.

The routine I'm looking for is for determining if the 3d point is inside the closed planar points or outside (within the same plane).

Example:  the planar shape is the outside shape of a Star of David.  This "star" is a plane positioned randomly in 3d space.  I now have a test point in 3d space that is known to be in the same plane as the "star".  I'm looking to test if this 3d point is inside the plane of the "star" or outside.

prl

#5
Mark,

Pondering your approach some more, I think you are describing a manual ray casting solution.  And this should work if I count the number of intersections using a vector test ray.  Let me think about this and see if I can't change the concept into basiccad code.

Thanks

MarkX

#6
Quote from: prl on December 07, 2012, 08:46:39 AM
Quote from: MarkX on December 07, 2012, 08:34:55 AM
If the point was not on the plane, you will not be able to snap exactly to the far side.

Thanks Mark.  Determining if a 3d point is in the 3d plane (not above or below), that routine I have in basiccad.  If anyone needs that, let me know.

The routine I'm looking for is for determining if the 3d point is inside the closed planar points or outside (within the same plane).

Example:  the planar shape is the outside shape of a Star of David.  This "star" is a plane positioned randomly in 3d space.  I now have a test point in 3d space that is known to be in the same plane as the "star".  I'm looking to test if this 3d point is inside the plane of the "star" or outside.
Programmatically ...
For a shape like the Start of David, if contiguous,  run a test. Draw a line from the point arbitrarily long (but longer than the longest dimension of your object. Trim against the shape. Measure the line. Do in all four directions. If the line is not trimmed (length remains same) in any one of the directions, then it is outside the planar shape. Of course, this won't work for convoluted U shapes or spirals, but should work for basic shapes.

Method #2. Create a named point-mark at the desired point. Use polygon selection to select everything inside the planar shape. Look to see if named object is selected. (Edit: Need to do this from the equivalent of a front view of the planar object. But should work on all contiguous shapes, even if U shaped).

Mark


Bob P

#7
This will only work if your polygon is a real plane and the point is over/under the plane...

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.

MarkX

Method #3: Draw a line from any point known to be on the plane to the target point. Then increase the line length to something arbitrarily long. Trim the line against the planar shape. If the trim succeeds, the target point is inside the planar shape. Once again, won't work with irregular (U shape) planes.

Dr PR

#9
prl,

This is an excellent 3D geometry problem. The question you must answer is what does "inside" mean, in geometrical terms?

The worst case I can think of is a spiral polygon. If you draw a line in the plane of the polygon that cuts through the polygon, it may pass "in" and "out" of the polygon many times. What constitutes "in" and "out?"

I think this has to do with the number of intersections between the line and the edges of the polygon. Working from the starting point and extending to infinity, if a line cuts the edge of the polygon an odd number of times (1, 3, 5, ...) the starting point is inside the polygon. If it cuts the edge of the polygon an even number of times (0, 2, 4, 6, ...) the starting point is outside the polygon. See the attached picture.

So, if you draw a line that is in the plane of the polygon and longer than the width of the polygon and count the intersections with the line and the edge of the polygon ("Line Plane" will probably work) you will know if the starting point is inside or outside the polygon.

There is one possible problem. A line from a starting point outside a polygon may be tangent to an edge of the polygon, or pass through a single point at the tip of a polygon - each event will produce an intersection count of 1. So to be certain you would need to try two or more lines. If any line produces an even number of intersections the point is outside the polygon.

There may be an easier way, but I can't think of it right now. The catch is that the line must extend beyong the extremety of all parts of a complex polygon.

Phil
DesignCAD user since 1987

prl

#10
Phil and Mark,

Thanks for the feedback.   I may try a formal correct math approach doing the intersecting counting and also maybe a few DC trick methods as suggested by Mark in methods 2 and 3.  Looking for speed and the ability to handle polygons of any complex shape.

One of my initial trick methods rolling around in my mind is to draw a small polygon at the test point, then use the polygon add or subtract method to see if it adds or subtracts area from the polygon area of the DesignCAD surface (plane).  This method obviously relies on the assumption that the DesignCAD area command actually computes the correct area for a planar polygon positioned arbitrarily in 3d space. 

MarkX

Quote from: prl on December 07, 2012, 12:51:29 PM
One of my initial trick methods rolling around in my mind is to draw a small polygon at the test point, then use the polygon add or subtract method to see if it adds or subtracts area from the polygon area of the DesignCAD surface (plane).  This method obviously relies on the assumption that the DesignCAD area command actually computes the correct area for a planar polygon positioned arbitrarily in 3d space.
There's also some error built in -- what if the point is just outside, but on the edge of the main plane?

If that's acceptable, then maybe you could turn both the plane and the point into a solid, and use the Interference Check tool (never used it, so don't know if it really works).

Have fun,
Mark

prl

Quote from: MarkX on December 07, 2012, 01:21:42 PM
There's also some error built in -- what if the point is just outside, but on the edge of the main plane?

You're correct.  I would need to check the distance of the point relative to all the edges and make sure the polygon I draw is less than this.  This math I have.

Quote from: MarkX on December 07, 2012, 01:21:42 PM
. . . maybe you could turn both the plane and the point into a solid, and use the Interference Check tool (never used it, so don't know if it really works).

You're starting to think like Lar. ;D 

Dr PR

I tried placing a pointmark at the test position and then defining both the pointmark and the plane to be solids. After that I used the "Solid Intersect" function to see if the pointmark and plane have anything in common. No joy - DesignCAD doesn't buy the idea that a pointmark is a solid.

Phil
DesignCAD user since 1987

MarkX

Quote from: Dr PR on December 07, 2012, 04:31:20 PM
I tried placing a pointmark at the test position and then defining both the pointmark and the plane to be solids. After that I used the "Solid Intersect" function to see if the pointmark and plane have anything in common. No joy - DesignCAD doesn't buy the idea that a pointmark is a solid.

Phil
I was thinking more about defining a tiny sphere around the pointmark. Remember, one of PRL's approaches allowed for a little overlap error.

Mark