POV-Ray : Newsgroups : povray.general : evenly spaced points on splines : evenly spaced points on splines Server Time
30 Jul 2024 22:23:57 EDT (-0400)
  evenly spaced points on splines  
From: stevenvh
Date: 11 Apr 2008 08:00:00
Message: <web.47ff52a1bf1d652c5245d3740@news.povray.org>
Hi,
when I recently started working with (natural) splines I was somewhat
disappointed to see that the generated points weren't evenly spaced.
This is not a problem when modeling a garden hose, where the constituting
spheres should be packed close to get a smooth surface, but it cab be a bit of
a nuisance when placing a series of disjunct objects along the spline's path.
So I tried to get my objects evenly spaced. Here's the idea:

Suppose you want to place 100 objects evenly along the spline's path.
You create an array of 10000 elements, say a few orders of magnitude more than
the number of points you're interested in. Calculate 10000 points on the
spline, and the distance from the previous point. In the array you store the
accumulated distances. So the n-th entry holds the path length from start to
point n.
When the array is filled you also know the total path length L, suppose L equals
12.34. Now find in the array the partials lengths of 12.34 / 100, 2 * 12.34 /
100, 3 * 12.34 / 100, etc.
Store the indices of those values in a new array, a lookup table
which translates between a position on the spline and the parameter POV-Ray's
spline function wants to give you the point at that position.
That's it. Questions, comments, ideas, constructive criticism and alternate
solutions are welcome.

My suggestion is to add this functionality to spline in a future version of
POV-Ray.
Code below is just a quick-and-dirty test program, no optimization, no nothing.

Steven

***** Code starts here **************************************************

/******************************************************************************
/
/   Evenly spaced points on spline
/
/   Steven Van Hulle
/   POV-Ray v3.6
/   2008-04-11
/
******************************************************************************/

#include "colors.inc"
#include "transforms.inc"

#default { finish{ ambient 0.7 } }
background { Gray85 }
light_source { <-30, 30, -30>  color White }

camera {
   location  <0.0, 1, -5.0>
   direction 3.0*z
   right     x * image_width / image_height
   look_at   <0.0, 0.7, 0.0>
}


#macro Sqr(n) n*n #end
#macro Distance(A, B) sqrt(Sqr(A.x - B.x) + Sqr(A.y - B.y) + Sqr(A.z - B.z))
#end


#declare Path_Spline = spline {
   natural_spline
   -0.25, <-2,    0,   0>
    0.00, <-1,    0,   0>
    0.25, <-0.15, 0.5, 0>
    0.50, < 0,    1.5, 0>
    0.75, < 0.75, 0.5, 0>
    1.00, < 1,    0,   0>
    1.25, < 2,    0,   0>
};

#declare Path_Spline2 = spline {
   natural_spline
   -0.25, <-2.1,    0, 0>
    0.00, <-1.1,    0, 0>
    0.25, <-0.25, 0.5, 0>
    0.50, <-0.1,  1.5, 0>
    0.75, < 0.65, 0.5, 0>
    1.00, < 0.9,    0, 0>
    1.25, < 1.9,    0, 0>
};


#declare npoints = 100;
#declare resol = 100;
#declare dist = array[npoints * resol + 1];
#declare points = array[npoints + 1];

#declare dist[0] = 0;
#declare prevpt = Path_Spline(0);
#local ii = 1;
#while (ii <= npoints * resol)
    #declare newpt = Path_Spline(ii/(npoints * resol));
    #declare dist[ii] = dist[ii-1] + Distance(prevpt, newpt);;
    #declare prevpt = newpt;
    #declare ii = ii + 1;
#end

#local Ltot = dist[npoints * resol];
#declare points[0] = 0;
#local p = 1;
#local jj = 0;
#while (p <= npoints)
    #declare targetval = Ltot * p / npoints;
    #while ((dist[jj] < targetval) & (jj < npoints * resol))
        #declare jj = jj + 1;
    #end
    #declare points[p] = jj;
    sphere {Path_Spline(jj/(npoints * resol)), 0.005 pigment{Blue}}

    #declare p = p + 1;
#end


// show common spline
#local C = 0;
#while (C <= 1)
   sphere{Path_Spline2(C),0.005 pigment {Red}}
   #local C=C+0.01;
#end


text {
    ttf "arial.ttf" "POV-Ray spline"
 .001, 0 pigment { Red }  scale 0.05  translate <-1, 1.4, 0>
}
text {
    ttf "arial.ttf" "Evenly spaced spline"
 .001, 0 pigment { Blue } scale 0.05  translate <-1, 1.33, 0>
}

***** Code ends here ****************************************************


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.