ruchitinfushion
Hi friends,
    This days i have fever of 3D Maths,Trying to understand building blocks of 3D programming.And i have decided to solve vectors and matrices problems inside Maya using python to sharp my mind.Below is first problem which i want to solve.

=>Point In Polygon

Aim= Determine
whether a given point in the 3D space lies inside, outside, or on the boundary of a Polygon.

Now i need help for
*Basic principle behind this problem.
*Most Accurate and Fastest algorithm to solve this problem.

pshipkov
Point cannot be inside or outside of a polygon. It can lie on it or be on one of its sides (based on the polygon's normal vector).

Here is how you do it with simple words:

Polygons are composed of triangles. If your polygon is planar you take the first triangle and build transformation matrix from its points.
This is how we get orthogonal vectors from the 3 points of the triangle
v1 = p3-p1
v2 = p2-p1
v1 = normalize(v1 ^ v2)
v3 = normalize(v1 ^ v2)
v2 = normalize(v1 ^ v3)
These vectors will be used to build a matrix below. They define its orientation. But we will need and the positional data for that matrix. You can use one of the points of the triangle for that or find the barycenter. In the most real world cases barycenter is the way to go. Here is how you find it:
v4 = p1*0.3333+p2*0.3333+p3*0.3333
Build a matrix from these vectors, different applications may have different ordering of the xform matrix but i assume we are talking Maya here
              | v1.x v1.y v1.z 0 |
matrix =| v2.x v2.y v2.z 0 |
              | v3.x v3.y v3.z 0 |
              | v4.x v4.y v4.z 1 |

Because we are talking Maya here the triangles of mesh objects usually come with normals already associated to them. I like to assemble the matrix in a way that its Y axis coincides with the normal vector of the triangle.

You can think of that matrix representing the triangle as transform object in Maya. You can think of the point you have as another transform object.
Now to figure out on which side of the triangle (polygon) this point lies you have to "parent" it under that imaginary transform object defined by the matrix. And then check if the local "Y" position of the point is positive or negative. If positive the point is "outside" (using your wording) of the polygon,  otherwise it is on the opposite side, or "inside".

                                      *           <-point

  y
  |                                               <- matrix

  |                      
  z_____ x           |
                          |                        <-triangle's normal vector

                          |
---------------------------------------- <-triangle from the side

To find the coordinates of the point in the local space of the matrix you have to multiply its coordinates by the inverse matrix:
v = p * inverseMatrix
if v.y > 0 point is on the side defined by the triangle's normal
if v.y < 0 point is on the other side
if v.y == 0 point lies on the plane defined by the triangle.


Now if your polygon is non-planar things get more interesting. You will have to find the closest point on the surface of the polygon to your point. Then figure out which of the polygon's triangles this point is inside and run the test above for that triangle. While you can do that all by yourself this brute force method is too slow for real tools. You will need some sort of accelerated lookup method - octree or kdtree. Luckily Maya provides such. Take a look at the MMeshIntersector class.

It goes like this:
MPointOnMesh pomi;
MMeshIntersector meshIntersector;
meshIntersector.create(oMyMeshGeo);
meshIntersector.getClosestPoint(pMyPoint, pomi, 99999999.9);
pomi.triangleIndex(); // triangle ID

Let me know if something is not clear.
Reply 0 0
ruchitinfushion
@PShipkov
     Thanx for reply in detail..See this link Fill a mesh of particles in Maya
instead of Filling a mesh with particles i want to query a given world position of voxel is inside of polygon or outside of polygon?
  if it is inside then setFluidAttr density to 1 else 0.Like what you did in "Scatter and fluid grid2" example.
               Over Google i found algorithm of ray-casting but people said it's slow.One more good link i found is

Determining if a point lies on the interior of a polygon

i hope i will solve this problem soon with my little bit of understanding in vector math.Thanx again

Reply 0 0
pshipkov
Doing it for voxels is not much different than for points, especially if you test at the center of the voxels.
For more precise solution you can test for each of the 8 corners of given voxel and if one of them is outisde of your object you skip it (the voxel).
Reply 0 0
ruchitinfushion
Here is First step=>
Visualize Normal vector of given face.




import math
import maya.cmds as mc
import maya.mel as mm
#Find Normal Vector
A = mc.xform('pPlane1.vtx[0]', q=True, ws=True, t=True)
B = mc.xform('pPlane1.vtx[1]', q=True, ws=True, t=True)
C = mc.xform('pPlane1.vtx[3]', q=True, ws=True, t=True)
# Cross product
nX = ( A[2] - B[2] ) * ( C[1] - B[1] ) - ( A[1] - B[1] ) * ( C[2] - B[2] )
nY = ( A[0] - B[0] ) * ( C[2] - B[2] ) - ( A[2] - B[2] ) * ( C[0] - B[0] )
nZ = ( A[1] - B[1] ) * ( C[0] - B[0] ) - ( A[0] - B[0] ) * ( C[1] - B[1] )

NormalLength = math.sqrt( ( nX * nX ) + ( nY * nY ) + ( nZ * nZ ))

NormalX = nX / NormalLength
NormalY = nY / NormalLength
NormalZ = nZ / NormalLength

mc.setAttr('curveShape1.controlPoints[1].xValue', NormalX)
mc.setAttr('curveShape1.controlPoints[1].yValue', NormalY)
mc.setAttr('curveShape1.controlPoints[1].zValue', NormalZ)

Reply 0 0
ruchitinfushion
Step 2=>
Visualize closest Face to query point.


See attachment for scene file.
I hope i am on right track.
Reply 0 0
JeremyR
I thought the dot product would tell you whether  point or vector was in front of or behind a face?
Reply 0 0
ruchitinfushion
Step 3=>
Visualize Dot Product Of Vector


#import module
import cmath
import maya.cmds as mc

#Define Vector

A = mc.xform('curve1.cv[1]', q=True, ws=True, t=True)
B = mc.xform('curve2.cv[1]', q=True, ws=True, t=True)
C = mc.xform('curve3.cv[1]', q=True, ws=True, t=True)
D = mc.xform('curve4.cv[1]', q=True, ws=True, t=True)

#The Dot Product

#(A*B)
dotAB = (A[0]*B[0] + A[1]*B[1] + A[2]*B[2])
#(A*C)
dotAC = (A[0]*C[0] + A[1]*C[1] + A[2]*C[2])
#(A*D)
dotAD = (A[0]*D[0] + A[1]*D[1] + A[2]*D[2])

Result
dotAB = 0.0
dotAC = 54.000000000000028
dotAD = -45.000000000000014

conclusion:
if dotProduct > 0 then point is outside
if dotProduct = 0 then point lie on surface
if dotProduct <0 then point is inside

is this true?? Reply
Reply 0 0
pshipkov
I am not fallowing you here. Which of the 4 points define the plane.
Reply 0 0
ruchitinfushion
Assume point A is normal vector of plane.
Reply 0 0
pshipkov
Do you mean the vertical line that ends with point A ?
And where is the plane - the horizontal one that ends with point B ?
Reply 0 0
ruchitinfushion


A = Normal vector of plane
And also plane is closest face for B,C & D.
Reply 0 0
ruchitinfushion
Reply Please
Reply 0 0
pshipkov

Hi, sorry for the late reply.
I am trying to make sense of what you are doing and kind of cannot.
So you have that plane defined by points BCD and A is the normal vector, correct ?
If that's the case what you are trying to prove with that ?
What is the unknown variable you are trying to find ?

Reply 0 0
ruchitinfushion
No No No..i am trying to show you something else.
Let assume there is one polygon face which is closest face for three points (B,C & D) in space and "A" is normal vector of that face.After doing dot product of AB,AC & AD i got result 0,54 & -45 respectively.
Now by looking at result can we say that ??
Point B is on the surface.
Point C is outside surface(in normal direction)
Point D is inside surface(in opp of normal direction)

Because
Dot produst of AB = 0
Dot produst of AC > 0
Dot produst of AB < 0


Reply 0 0
Reply