this version doesn’t hang for me… at least i couldn’t find settings to hang:
fn scatterPointsOnSpline sp dist tolerance:0.001 =
(
dist = dist as double
len = (curveLength sp) as double
tolerance = amax tolerance (len * 0.000001d0)
t = 0.0
d = 0.0
current = interpCurve3D sp 1 t pathParam:off
points = #(current)
while t < 1.0 and not esc_ do
(
t += (dist - d)/len
t = amin t 1.0
p = interpCurve3D sp 1 t pathParam:off
d = distance current p
if d >= dist - tolerance do
(
current = p
append points current
d = 0.0
)
)
points
)
/***************** TEST *****************/
(
delete helpers
c = random red green
t0 = timestamp()
h0 = heapfree
points = scatterPointsOnSpline $ 30.0 tolerance:0.0001
format "time:% heap:%" (timestamp() - t0) (h0 - heapfree)
dd = for k=1 to points.count collect
(
--point pos:points[k] wirecolor:c
if k > 1 then distance points[k-1] points[k] else dontcollect
)
--format " >> %\n" dd
format " >> %\n" #(dd.count, amin dd, amax dd)
)
we have to understand that most of MAX SDK functions are made in Float precision.
so there is no sense to ask tolerance to be smaller than ‘allowable error’
because of that i’ve added tolerance correction in case of very long splines. which limits tolerance by minimum as 0.000001 of spline length
now couple words about using fixed step in the algorithm.
i wanted to avoid it because there is no absolutely accurate way to define the step. Anyway it might be a situation that making a step we pass a ‘on distance’ point or might be more than one point inside a step.
in the second case we can’t use bi-search because you can find the second point instead of the first.