POV-Ray : Newsgroups : povray.general : SDL tricks, handy code snippets, Frequently used code, and good practices Server Time
25 Nov 2024 04:41:00 EST (-0500)
  SDL tricks, handy code snippets, Frequently used code, and good practices (Message 1 to 1 of 1)  
From: Bald Eagle
Subject: SDL tricks, handy code snippets, Frequently used code, and good practices
Date: 28 Feb 2016 23:05:01
Message: <web.56d3c26e30ea59d5e7df57c0@news.povray.org>
Hi Folks,
periodically I get the sense that there's a lot of little bits and pieces of
specialized knowledge and things peculiar to POV-Ray that aren't widely known,
aren't obvious, and might not be formally documented or easily searchable /
found.

I was hoping people could chime in with anything that they know about.
What I was specifically hoping people would share were:
-Methods that are specific to SDL and would make writing scenes easier, more
readable, and easier to modify ("control panels", parameterization), as well as
other little POV-Ray "factoids"
-Handy code snippets for performing slightly modified tasks (special loops,
home-brew alternative to the official POV tools, etc.)
-Frequently used code structures that you use for writing your SDL, but that
others might not be familiar with, or has special customizations that you're
worked out over time.
-Methods of coding that really help speed up writing, avoid common errors, aid
in debugging code, aid readability, etc.
-Any good coding practices that formally schooled folks might take for granted,
but that self-taught people could certainly benefit from.
-Tips on writing/using good macros, functions, splines, nested loops, arrays,
and other heavy-lifting items.
-Useful "tests" that often have to be coded by hand - "does A intersect B"?,
methods of generating certain special number values/ranges, and other things
like that.

I know, lots of stuff, but I'm curious, and this thread would be a good
condensed repository.
I'd request that any commentary, etc. get posted in a separate thread with a
link to the message you're referencing, in order to keep this thread short and
"clean".   Thanks   :)

----------------------------------------------------------------------
One of the things I've found useful is to rig my Include Files so that I can
render them as a stand-alone scene, or use them as an include file without any
modification.

#####################
At beginning of file:
#####################
#ifdef (SDL)
     // do nothing
#else
     // setup scene file for independent rendering
     global_settings { }
     #include "debug.inc"
     #declare Feet = 12;
     Camera_Position, Camera_look_at, Camera_Angle
     light_source { }
     sky_sphere{ }
     etc........................................................
#end  // end SDL check

##############################################################
At end of file, do stuff to render what's in the include file:
##############################################################

#ifndef (SDL)
 object { }
#else
 // do nothing
#end

----------------------------------------------------------------------

"Verbose" flag
For certain macros, routines, or even whole SDL files, I'll have a master or a
series of flags to turn on messaging to the debug stream.  That way I can turn
it on or off, and it doesn't take rooting through all the code to comment-out /
uncomment the debugging statements.

#if (Verbose = true)
     #debug concat ("Variable = ", str(Var, 3, 1), "\n")
#end

Great to add to certain include files to keep track of what's going on.

----------------------------------------------------------------------
Random "flipping"
great for varying the direction of something - a shift to the left or right, top
or bottom, behind or in front of.   Text, objects, wooden flooring planks, etc.

#declare Direction = seed (123);
#if (rand (Direction) > 0.5)
     #declare Sign = 1;
#else
     #declare Sign = -1;
#end

[do something with a (variable * Sign)]

----------------------------------------------------------------------
Alternative to Sphere Sweep
* Using loop structure with special 1st item

Can use many fewer points and still get a nice smooth continuous curve.
Lots faster to parse.

First define a spline
Start with a sphere, since there no place to draw "from", "to"
Store old position
Draw cylinder from old position to new position.
Place a sphere at new position to fill in any "joints"


#declare LoopStep = 0.01;
#declare Nr = 0;              // start
#declare EndNr = 1+LoopStep;  // end
#declare OldPosition = <0, 0, 0>;
#while   (Nr < EndNr)
     #declare Position = Spline(Nr);
     #if (Nr = 0)
            sphere{ <Column*XStep, Row*YStep, 0>, LineWidth pigment {Color}
translate <Position.x*Sign, Position.y, 0>}
            //#debug concat ("Spline: ", vstr(3, Position, ", ", 0, 1), "\n" )
      #else
            sphere{ <Column*XStep, Row*YStep, 0>, LineWidth pigment {Color}
translate <Position.x*Sign, Position.y, 0>}
            cylinder { <(Column*XStep)+(OldPosition.x*Sign),
((Row*YStep)+OldPosition.y), 0>, <(Column*XStep)+(Position.x*Sign),
((Row*YStep)+Position.y), 0>, LineWidth pigment {Color}}
       #end

       #declare OldPosition = Position;
       #declare Nr = Nr + LoopStep;
#end // end of loop

----------------------------------------------------------------------
"Tracing" with a spline

I used Visio to trace something with curved line segments.  I can read the
length of the individual segments.  So I just added that length to the sum of
the previous spline point, and then used the last sum and pasted to define the
total spline length.   Long, laborious, and tedious, but simple and easy to keep
track of what you're doing.

#declare TotalSplineLength =
(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2+12+12.3+23.5);
#declare Porcupine = spline {
    cubic_spline
    -24.6, <12, 170, 0>,                   //control point
    0.00, <36.5, 172.6, 0>,                       //start point
    25.3, <56.5, 157.1, 0>,
    (25.3+38), <54, 195.1, 0>,
    (25.3+38+36.1), <67.5, 161.6, 0>,
    (25.3+38+36.1+27.9), <79, 187.1, 0>,
    (25.3+38+36.1+27.9+25.7), <75.5, 212.6, 0>,
    (25.3+38+36.1+27.9+25.7+49), <87.5, 165.1, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1), <109, 219.1, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5), <106.5, 167.6, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5), <137, 217.6, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3), <120.5, 161.6, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9), <167.5, 190.1, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1), <131, 150.1, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8), <170.5,
169.1, 0>,
    //(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2), <159,
146.6, 0>,
    (25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6),
<141.5, 137.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5),
<173.5, 147.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2),
<144, 126.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2),
<171.5, 116.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4),
<150.5, 112.1>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8),
<157.5, 100.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3),
<166.5, 97.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6),
<167.5, 93.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9),
<144, 88.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5),
<125.5, 90.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4)
,
<120, 85.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5),
<108.5, 80.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10),
<98.5, 80.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6),
<98.1, 86.7, 0>

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5),
<101.5, 89.7, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5),
<100.5, 94.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2),
<85.5, 96.7, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2),
<78.1, 91.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9),
<69.9, 81.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4),
<57.5, 81.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2),
<54.1, 85.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9),
<58.5, 89.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6),
<66.9, 97.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7),
<63.5, 115.5, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2),
<45.5, 112.5, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2),
<38.1, 128.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2+12),
<41.9, 139.5, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2+12+12.3),
<46, 151.1, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2+12+12.3+23.5),
<36.5, 172.6, 0>,

(25.3+38+36.1+27.9+25.7+49+58.1+51.5+58.5+58.3+54.9+54.1+43.8+25.2+19.6+33.5+36.2+29.2+21.4+13.8+9.3+4.6+23.9+18.5+7.4+
12.5+10+6.6+4.5+4.5+15.2+9.2+12.9+12.4+5.2+5.9+11.6+18.7+18.2+17.2+12+12.3+23.5+24.6),
<13, 180, 0>   // Control Point

 }

----------------------------------------------------------------------
From Official POV stuff:
----------------------------------------------------------------------
(*) View_POV_Include_Stack
Many include files check for this variable, and if defined, output a message to
the Debug Stream.
For example:
#ifdef(View_POV_Include_Stack)
   #debug "including arrays.inc\n"
#end

----------------------------------------------------------------------
Avoid problems when using code from older versions of POV-Ray in newer versions.

#ifndef (ARRAYS_INC_TEMP)  //  checks to see if this is already declared
#declare ARRAYS_INC_TEMP = version; // "stores" version you declared in your SDL
#version 3.5;  // switches version number

[ rest of file ]

#version ARRAYS_INC_TEMP;  // reverts to version you declared in your SDL
#end

----------------------------------------------------------------------

More to follow as I dig up bits of my own code and ponder more on the Tao of POV
and write the SDL of no SDL...


Post a reply to this message

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