POV-Ray : Newsgroups : povray.advanced-users : <no subject> : Re: <no subject> Server Time
25 Apr 2024 16:12:28 EDT (-0400)
  Re: <no subject>  
From: Bald Eagle
Date: 14 Sep 2018 14:00:07
Message: <web.5b9bf6c26f92e856c437ac910@news.povray.org>
The wet and juicy version.

I prefer the bologna descriptor.

The offender is
#declare _Y = function (Angle, _M) {min(max(0, Angle * _M), _M)}

where "Angle" goes from 0 to 1.



#######################################################################

#version version;

// floating point comparison debug



#include "colors.inc"

light_source {
 <5, 10, -20>
 color White
 fade_distance 20
 fade_power 2
}

camera {
    location  <0, 2, -35>
    look_at   <0, 0, -10>

    right x*image_width/image_height
    up y
}

//###############################################

#macro Scalar (_Value, _P, _N)
 #local _Result = str(_Value, 0, _P);
 #if(_N) #local _Result = concat (_Result, " \n") #end
 _Result
#end

//###############################################

#macro Vector (_Value, _P, _N)
 #local _Result = concat ("<", vstr(3, _Value, ", ", 0, _P), ">");
 #if(_N) #local _Result = concat (_Result, " \n") #end
 _Result
#end

//###############################################
//------------------------------------------------------------------------------
#declare Round = function (Value) {ceil(Value - 0.5)}
//------------------------------------------------------------------------------
// Fast NChooseM
#declare FastNCM = function (n, k) {prod(i, k+1, n, i) / prod(i, 1, n-k, i)}

#declare PowerFn = function(s, p) { select(p, pow(s, p), 1, pow(s, p)) }
//------------------------------------------------------------------------------
#declare Bernstein = function (Deg, K, S)
{FastNCM(Deg, K) * PowerFn(S, K) * PowerFn (1 - S, Deg - K)}

//------------------------------------------------------------------------------
#macro Point (VG, UG, Uc, Vc)   // called during interpolation loop due to using
Current [V][U] array
 //#debug concat(Scalar(VG, 0, 0), ", ", Scalar(UG, 0, 1), "\n")
#local Degree = 3;
 #local P =
 #for (j, 0, 3)
  #for (i, 0, 3)
  //#debug concat(Scalar(VG, 0, 0), ", ", Scalar(UG, 0, 0), ", ", Scalar(i, 0,
0), ", ", Scalar(j, 0, 1), "\n")
  Bernstein(Degree, i, Vc) * Bernstein(Degree, j, Uc) * StitchedArray
[VG][UG][i][j] +
  #end
 #end
 0;
 P
#end
//------------------------------------------------------------------------------

#declare Curve = true;

#declare R1 = 10;   //
#declare R2 = 4;   //
#declare Sum = R1+R2;  //

#declare U1 = 0;   // small radius
#declare U2 = 2*pi;   //
#declare V1 = 0;   // large radius
#declare V2 = 2*pi;   //
#declare r0 = R1;
#declare r1 = R2;
//#declare Phi = U1;
#declare   Phi_inc = (2*pi/360)*60;
#declare Theta_inc = (2*pi/360)*20;


#debug "Creating arrays... \n"

// Create an array to hold all of the Bezier patches to cover the surface
#declare Usize = Round ((U2-U1)/Phi_inc)+1;
#declare Vsize = Round((V2-V1)/Theta_inc);
#debug concat ("Usize = ", str(Usize, 0, 3), ",   Vsize = ", str(Vsize, 0, 3),
"\n")


#declare UU = Usize-1;
#declare VV = Vsize-1;

#debug concat ("UU = ", str(UU, 0, 3), ",   VV = ", str(VV, 0, 3), "\n")

#declare UArray = array [Usize+1];
#declare VArray = array [Vsize+1];

#debug concat ("UArray extends from 0 to ", str(dimension_size(UArray,1)-1, 0,
3), " \n")
#debug concat ("VArray extends from 0 to ", str(dimension_size(VArray,1)-1, 0,
3), " \n")

#declare StitchedArray = array[Vsize][Usize];

#for (U, 0, UU)
 #for (V, 0, VV)
  #local StitchedArray [V][U] = array[4][4];
 #end

#end

#debug concat ("StitchedArray is ", str(dimension_size(StitchedArray, 1)-1, 0,
3), " x ", str(dimension_size(StitchedArray, 2)-1, 0, 3), " elements \n\n")

//----------------------------------------------------


#declare Ustep = (U2-U1)/(UU);
#declare Vstep = (V2-V1)/(VV);

#debug concat ("UStep = U2 (", Scalar (U2, 5, 0), ") - U1 (", Scalar (U1, 5, 0),
") / Usize (", Scalar (Usize, 5, 0), ") = ", Scalar (Ustep, 5, 1))
#debug concat ("VStep = V2 (", Scalar (V2, 5, 0), ") - V1 (", Scalar (V1, 5, 0),
") / Vsize (", Scalar (Vsize, 5, 0), ") = ", Scalar (Vstep, 5, 1))

#debug "\n\n"

#for (U, 0, UU)     // must be UU because SitchedArray is only that big
 #declare UArray [U] = <U, max(0, U*Ustep), min((U+1)*Ustep, U2)>;
 //#debug concat ("UArray [", Scalar (U, 0, 0), "] = ", Vector (UArray [U], 5,
1))
#end

#for (V, 0, VV) // must be VV because SitchedArray is only that big
 #declare VArray [V] = <V, max(0, V*Vstep), min((V+1)*Vstep, V2)>;
 //#debug concat ("VArray [", Scalar (V, 0, 0), "] = ", Vector (VArray [V], 5,
1))
#end

//----------------------------------------------------

// Determines the Bezier patch in the 2D array that the function coordinates
fall on
#macro ThisPatch (XVal, YVal)
    #local Mult = 1000;

    //#local _Patch = <0, 0>;
    #local _Patch = array [6];

    #for (U, 0, Usize)
        #debug concat ("Testing Y = ", Scalar (YVal, 3, 0), " vs ", Vector
(UArray [U], 3, 0), " \n")
        #if ( int(YVal*Mult) >= int(UArray [U].y*Mult) & int(YVal*Mult) <=
int(UArray [U].z*Mult) )

        #local _Patch [0] = UArray [U].x;
        #local _Patch [1] = UArray [U].y;
        #local _Patch [2] = UArray [U].z;
        #end
    #end

    #ifndef (_Patch [0]) #debug "_Patch [0] U not defined \n" #end

    #for (V, 0, Vsize)
    #debug concat ("Testing X = ", Scalar (XVal, 3, 0), " vs ", Vector (VArray
[V], 3, 0), " \n")
        #if (XVal >= VArray [V].y & XVal <= VArray [V].z)

        #local _Patch [3] = VArray [V].x;
        #local _Patch [4] = VArray [V].y;
        #local _Patch [5] = VArray [V].z;
        #end
    #end

    #ifndef (_Patch [3]) #debug "_Patch [3] V not defined \n" #end

    _Patch
#end

//-----------------------------------------------------------
#macro Vector4 (_Value, _P, _N)
 #local _Result = concat ("<", vstr(4, _Value, ", ", 0, _P), ">");
 #if(_N) #local _Result = concat (_Result, " \n") #end
 _Result
#end
//-----------------------------------------------------------

// Determines where on individual Bezier patch function coordinates are
#macro PatchUV (XVal, YVal)
    #local Range = ThisPatch (XVal, YVal);
    #local ThisU = (YVal - Range [1]) / (Range [2]- Range [1]);
    #local ThisV = (XVal - Range [4]) / (Range [5]- Range [4]);
    #local _Result = <Range [3], Range [0], ThisV, ThisU>;
    //#debug Vector4 (_Result, 3, 1)
 _Result

#end


#if (Curve)

    #declare N = 5;
 #declare _X = function (_N, Angle, _M) {min (_N*mod(Angle, 1/_N) * _M, _M)}
 #declare _Y = function (Angle, _M) {min(max(0, Angle * _M), _M)}


 #declare F = 1.0000001; // LastPoint multiplier to avoid [most] degenerate
cylinders
 #for (Theta, 0, 1, 0.001)

  #debug concat ("_X = ", Scalar (_X(N, Theta, Vsize), 5, 0), ", _Y = ", Scalar
(_Y(Theta, Usize), 5, 1))

  #local UV = PatchUV ( _X(N, Theta, Vsize), _Y(Theta, Usize) );
  // this converts the points to 'flat' UV, but now they need to be converted to
the interpolated Bezier coordinates
  #local CurrentPoint = Point (UV.x, UV.y, UV.z, UV.t);
  //#debug Vector (Point, 3, 1)
  //#debug Vector (UV, 3, 1)
  sphere {CurrentPoint Radius pigment {White*Theta}}
  #if (Theta > 0)
   cylinder {LastPoint*F, CurrentPoint, Radius texture {CurveTex} }
  #end
  #local LastPoint = CurrentPoint;
 #end

#end


Post a reply to this message

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