# An example of a helper script written in TCL. It would be better to move the first # three proceedures to another TCL file. In that way the principle part of the script, # the "while" loop, would be more prominent. # Malcolm Kesson July 2005 # This line is necessary if the script is run on OSX or Linux fconfigure stdout -translation binary #----------------------------------------------------------- # A trivial procedure for halving a value #----------------------------------------------------------- proc half { in } { return [expr $in / 2] } #----------------------------------------------------------- # Procedure for generating points on a Hilbert curve #----------------------------------------------------------- proc hilbert { x0 y0 xis xjs yis yjs n } { if { $n <= 0 } { set Y 0 # write the xyz coordinates puts "[expr $x0 + ($xis + $yis) / 2] $Y [expr $y0 + ($xjs + $yjs) / 2]" } else { hilbert $x0 $y0 [half $yis] [half $yjs] [half $xis] [half $xjs] [expr $n - 1] hilbert [expr $x0 + [half $xis]] [expr $y0 + $xjs/2] [half $xis] [half $xjs] [half $yis] [half $yjs] [expr $n - 1] hilbert [expr $x0 + $xis/2 + $yis/2] [expr $y0 + $xjs/2 + $yjs/2] [half $xis] [half $xjs] [half $yis] [half $yjs] [expr $n - 1] hilbert [expr $x0 + $xis/2 + $yis] [expr $y0 + $xjs/2 + $yjs] [half -$yis] [half -$yjs] [half -$xis] [half -$xjs] [expr $n - 1] } } #----------------------------------------------------------- # Called by the while loop each time prman requests a curve #----------------------------------------------------------- proc makeCurve { iterations thickness } { # Calculate the number of curve CV's set npoints [expr pow(4, $iterations)] set npoints [expr int($npoints)] puts "Basis \"b-spline\" 1 \"b-spline\" 1" puts "Curves \"cubic\" \[$npoints\] \"nonperiodic\"" puts "\"P\" \[" puts [hilbert -0.5 -0.5 1.0 0.0 0.0 1.0 $iterations] puts "\] \"constantwidth\" \[$thickness\]" } #----------------------------------------------------------- # As soon as this helper script is called by prman it enters # the "while" loop and waits for incoming requests for curves. Remember, # even if the RIB file that calls this script supplies NO parameters the # the script will, infact, receive one item of data - the number of pixels # "covered" by the bounding box specified by the "Procedural" RIB statement. # For example, this script might be called by a RIB file as, # Procedural "RunProgram" ["tclsh C:/cutter/hilbert.tcl" ""] [-1 1 -1 1 -1 1] # Notice the empty data string. Infact, the script does require two inputs. See # the sample rib file contained in the current directory. #----------------------------------------------------------- while { [gets stdin buffer] != -1 } { set pixels [lindex $buffer 0] set iterations [lindex $buffer 1] set thickness [lindex $buffer 2] #puts stderr "$pixels" # <-- uncomment this to show the number of pixels puts "TransformBegin" makeCurve $iterations $thickness puts "TransformEnd" puts "\377" flush stdout }