[Closed] RayMeshGridIntersect returning weird values
t=$sphere001
if myRMGI !=undefined then free myRGMI
for o in selection do
(
v1 = normalize((getknotpoint o 1 2)-(getknotpoint o 1 1))
(
id=1
myRMGI = raymeshgridintersect()
myRMGI.initialize 10
-- the raymeshgridinterface doesn't care if the added nodes aren't editable meshes
-- as long as the nodes evaluate to an editable mesh
myRMGI.addnode t
myRMGI.buildgrid()
-- test for intersection with target
hcount = myRMGI.intersectray t.pos v1 true --false means don't check backfaces
ir = intersectray t (ray t.pos v1)
print hcount
-- get the closest hit
if hcount > 0 then
(
for hit = 1 to hcount do
(
--local firsthit = rm.getClosestHit ()
hitface = myRMGI.gethitface hit
hitnorm = myRMGI.getHitNormal hit
hitdist = myRMGI.getHitDist hit
hitPos = myRMGI.getHitBary hit
p=point pos:hitpos
format "%
" #(t,hitface,hitnorm,hitdist,hitPos)
)
)
)
)
Create a sphere at 0,0,0
draw a couple of lines that intersect it
run this code. The returned values are not what I expected nor can I make any sense out of them. What am I missing here?
Thanks!
Looks like I have a fundamental misunderstanding of raymesh intersect. I am trying to find the intersection of a spline to a mesh, i think raymeshintersect only works with two meshes.
hitPos = myRMGI.getHitBary hit
does not return the hit position it returns the barycentricposition within the hit face based on the face verts
to get the ray hit position, compute it from the initial ray and hit distance
Looks like I have a fundamental misunderstanding of raymesh intersect. I am trying to find the intersection of a spline to a mesh, i think raymeshintersect only works with two meshes.
it works with rays, segments, spheres and boxes against a single mesh.
Problem in your case is you need to use the knot pos as your ray position not the t.pos. Currently you are searching from the Teapot position looking down the spline, when you need to be looking down the spline from knot to knot.
-Eric
YEP! Thank you Eric and Klunk! It’s working great now. I am aware of the redundantly redundant placement of the dummy.
for h in objects where superclassof h == helper do delete h
d = dummy pos:(getknotpoint $Line001 1 1)
if myRMGI !=undefined then free myRGMI
v1 = normalize((getknotpoint $Line001 1 2)-(getknotpoint $Line001 1 1))
t = $sphere001
(
id=1
myRMGI = raymeshgridintersect()
myRMGI.initialize 10
myRMGI.addnode t
myRMGI.buildgrid()
hcount = myRMGI.intersectray d.pos v1 true --false means don't check backfaces
if hcount > 0 then
(
for hit = 1 to hcount do
(
--local firsthit = rm.getClosestHit ()
hitface = myRMGI.gethitface hit
hitnorm = myRMGI.getHitNormal hit
hitdist = myRMGI.getHitDist hit
hitPos = d.pos+(v1)*hitdist
p=point pos:hitpos
format "%
" #(hitface,hitnorm,hitdist,hitPos)
)
)
)
I take that back. raymeshgridintersect is blowing my memory out. I am sending it about 700 splines to check against a target and my memory usage skyrockets when it enters the function.
skyrockets = 2.2GB usage to 24GB usage in a few seconds.
you are probably doing something wrong… i use raymeshgridintersect on 100,000+ poly terrain within a viewport redraw call back to project into the terrain so you can track the terrain in walkthrough mode. Runs at about 300 fps so after a minute or so thats approx 20,000 ray tests against the voxel grid and I don’t get any memory spikes.
It looks like the problem was caused by not clearing the array returned by the function.
I did two things and there is no more issue. I declared the array of hits as a global array, and cleared the array with arr=#() after each return. <—(my hunch is that was it)
Now there is no more issue.