POV-Ray : Newsgroups : povray.newusers : How do I add color to "sphere_sweep"? : Re: How do I add color to "sphere_sweep"? Server Time
6 Oct 2024 02:00:18 EDT (-0400)
  Re: How do I add color to "sphere_sweep"?  
From: Chris B
Date: 12 Aug 2009 09:26:13
Message: <4a82c2f5@news.povray.org>
"Mike Williams" <nos### [at] econymdemoncouk> wrote in message 
news:IDs### [at] econymdemoncouk...
> Wasn't it Alex who wrote:
>>* In the documentation, I cannot really find any info on the first
>>parameter of the spline function (-.25, 0.00, etc.). What does is do?
>>With sphere_sweep I don't need to define these values...
>
> You do if you're using cubic_spline mode of sphere_sweep.
>

I think Alex was referring to the 'val_1', 'val_2' parameters on the spline 
function. As Mike says, you will still need all of the same coordinates for 
points on the curve and for the control points for 'curved' curves, but the 
'val_n' settings are a feature specific to the spline function.

The best way I can explain it is that this value represents how far through 
the spline the point is and is usually defined so that the visible points 
lie from 0 to 1. Any additional control points that aren't on the curve 
(used at the start or end of a curve) can be defined using values less than 
0 or greater than 1, so the initial control point for the cubic spline was 
defined with a 'val_n' value of -0.25. You can more or less define whatever 
spacing you want with these values, but the example #while loop that I 
posted before assumed that the points were more or less evenly spaced 
distance-wise, so it simply incremented it's way along the spline using this 
value (The example below removes this assumption).

When you come to use a spline function you specify a number from 0 to 1 as a 
parameter and it returns the coordinate on the curve based on how the number 
you specify relates to the 'val_n' settings that the spline was defined 
with.

>
>>* I am also worried about efficiency, as I am modelling many objects of
>>this kind: let's say 10000 "hyperstreamtubes"
> Sphere_sweeps are inherently not very efficient, because the algorithm
> is quite complicated. You may well find that lots of simpler objects
> render faster. Lots of simpler objects will bound better when you have
> long strings like in that example.
>
> However, lots of simple objects will always give you rounded edges. If
> you want spiky edges like in that example, you might consider something
> like the SweepSpline macro
>
> See the bottom section of http://www.econym.demon.co.uk/isotut/more.htm
>

Mike's macro looks like it will give you by far the best performance, 
especially with the option to store the generated mesh to disk to cut down 
on parse times. The mesh2 object is very efficient and a uv_mapped color_map 
is likely to be the easiest way of using the N*3 color matrices that you 
mentioned. Using 1000 spheres for each of 10,000 "hyperstreamtubes" would 
probably be slow, particularly if your machine starts paging. Nevertheless 
I've pasted a short example using the "lots of spheres" approach below my 
signature in case this is of any interest.


Regards,
Chris B.



camera {location <-1, 1,-2> look_at <1,0.3,0>}
light_source {<1,10,-1> color rgb 1}

#declare PointCount = 6;
#declare SeparationRatio = 0.01;
#declare Segments = PointCount-1;

#declare Spline = spline {
  cubic_spline
  -0.25, <0   ,-1  ,0>
   0.0 , <0   , 0  ,0>
   0.18, <1.4 , 0.6,0>
   0.2 , <1.45, 0.6,0>
   0.4 , <1.5 , 0.6,0>
   0.6 , <1.55, 0.6,0>
   0.62, <1.6 , 0.6,0>
   0.8 , <2.5 , 0.5,0>
   1.0 , <3.5 , 0.5,0>
   1.25, <5   , 1  ,0>
}


#declare Colors = array [PointCount] {
  <1,0,0>, <1,1,0>,
  <0,1,0>, <0,1,1>,
  <0,0,1>, <1,0,1>
}


#declare Widths = array [PointCount] {
  0.02,0.01,0.08,0.02,0.005,0.02
}


// Loop through the spline
#declare Ctr = 0;
#declare Last_Point = Spline(-0.2);
#declare TargetSeparation = 0;
#while (Ctr < 1)
  // Work out distance from the previous point.
  #declare This_Point = Spline(Ctr);
  #declare Separation = vlength(This_Point-Last_Point);
  #if (Separation > TargetSeparation)
    // Work out relative distance between control point.
    #declare Proportions    = mod(Ctr*Segments,1);
    // Work out the color at this point
    #declare Previous_Color = Colors[floor(Ctr*Segments)];
    #declare Next_Color     = Colors[floor(Ctr*Segments)+1];
    #declare This_Color     = Previous_Color*(1-Proportions) + 
Next_Color*Proportions;
    // Work out the radius at this point
    #declare Previous_Size = Widths[floor(Ctr*Segments)];
    #declare Next_Size     = Widths[floor(Ctr*Segments)+1];
    #declare This_Size     = Previous_Size*(1-Proportions) + 
Next_Size*Proportions;
    // Add a sphere the right color and size
    sphere {
      This_Point,This_Size
      pigment { rgb This_Color}
    }
    #declare TargetSeparation = SeparationRatio*This_Size;
    #declare Last_Point = This_Point;
  #end
  #declare Ctr = Ctr + 0.0001;
#end


// Axes
cylinder {-10*x,10*x,0.01 pigment {rgb <1,0,1>}}
cylinder {-10*y,10*y,0.01 pigment {rgb <1,1,0>}}
cylinder {-10*z,10*z,0.01 pigment {rgb <0,1,1>}}


Post a reply to this message

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