Notifications
Clear all

[Closed] inMesh checker?

Hello,

I was trying to find a way of checking if a point is in a mesh. Something like Thinking particles’ “inMesh” operator. I did some tests with intersectRay but I had no luck cause intersectRay doesn’t give me any unique result when the ray hits the backside of a face.

What can I do??

Thanks in advance,
Nick

7 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

I see only one solution – cast rays and check back side intersection.
For closed and convex mesh it’s easy. Just cast only one ray in any direction
For other cases it’s might be complicated.

To get backside intersections use RayMeshGridIntersect and rayMeshGridIntersectOps interface…

IntersectRay should come back as undefined if it doesn’t hit a face, ie: meaning it went through a back-face. Here’s a little snipped I’ve used in the past that helped a bit… but this also assumes the pivot of the objects are both inside the mesh already… won’t work on L shaped objects where the pivot isn’t inside the mesh and such – you could add more checks… maybe shoot rays towards each face, and if 50+% of the faces return undefined then you can assume it’s inside the mesh or something…?


		 for i in testobjs do
		(
			for o in volobjs do
			(
				theRay = ray i.pos (o.pos- i.pos)
				Hits = (intersectRay o theRay)
				if Hits == undefined then
				(
					Format "% Missed %, possibly inside obj. 
" i.name o.name
				)
				else ()
			)
		)

Thanks both of you for your replies!

Till know in order to check if a face was backside… I was adding a shell modifier with 2 material IDs and I was keeping a snapshotAsMesh. After your post, denisT, about backside intersections and RayMeshGridIntersect, I did some reading but I didn’t found any option for backsided, but your post led me to the option of double sided! So that was the solution! Thanks

Even though I didn’t found any backsided option here is how I used the “doubleSided” option of RayMeshGridIntersect’s intersectRay.


myObj = $
pointPosition = [0,0,0]

/*CodeFrom RayMeshGridIntersect HELP*/
rm = RayMeshGridIntersect() --create an instance of the Reference Target 
rm.Initialize 10 --init. the voxel grid size to 10x10x10
rm.addNode myObj --add the sphere to the grid
rm.buildGrid() --build the grid data (collecting faces into the grid voxels)

/*My addons*/
theHitsCount = rm.intersectRay pointPosition [0,0,1] true --intersect
if theHitsCount > 0 AND ( mod (theHitsCount/2) 2) == 0.0 then "is inside" --check if the number is greater than 0 (which means it intersects), and then if the number is Even or Odd. If it's even it's inside, otherwise it's outside
else "is outside"
	
rm.free() --Clear the grid to run the script again

thanks again,
Nick

of course I meant using “doublesided” in my post. Backfaced intersection is intersection with faces whose normals point in the direction of the ray.

Yeap… But from what you’ve told I thought that MXS had an specific option to check if the itersection was backside.

Anyway thanks for your help. BTW I did some tests and the results were good… not great. I tried to decrease the voxel size and set the Initialize from 10 to 100 for more resolution… but I didn’t saw any difference. (the points where in random positions in space).

So I might have two options in my script… On fast (with voxels) and one slower (with mesh).

Cya!

1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

to “check” back or front side face hit you have to getClosestHit, using its index getHitNormal and make dot product of the normal and the ray direction…

if dot product > 0 then the normal points the same as the ray direction (backface hit), if < 0 then the normal points towards (frontface hit)…

Yes, that was my first thought, but after a small talk that I had with a friend developer, he told and the other way that I’ve posted. I thought that it’s quicker to take the result of the ray and just see if it is even or odd than retrieving info from the intersected’s face angle etc. Correct me if I’m wrong but was it faster for MXS to do some math than retrieve info and then do some math?

:EDIT:
I did some tests and my method returns errors… So I’ll move to yours! I’ll come back with the results! Thanks