#!/usr/bin/env tclsh # hcs2b.tcl # Hilbert curve sphere_sweep to box. set cfg(self) [file rootname [file tail $argv0]] set cfg(vers) 0.1.0 # ----------------------------------------------------------------------------- proc readParseSphere {lnumVar fp} { global cfg upvar $lnumVar lnum incr lnum if {3 != [scan [gets $fp] { <%g, %g, %g>, HCR} x y z]} { puts stderr [format {%s: oops, [scan] failed in line %d.} $cfg(self) $lnum] exit 1 } return [list $x $y $z] } proc outBox {s1 s2 hcr ln} { global cfg if {[lindex $s1 0] == [lindex $s2 0]} { if {[lindex $s1 2] > [lindex $s2 2]} {set a $s2 ; set b $s1} \ else {set a $s1 ; set b $s2} } \ elseif {[lindex $s1 2] == [lindex $s2 2]} { if {[lindex $s1 0] > [lindex $s2 0]} {set a $s2 ; set b $s1} \ else {set a $s1 ; set b $s2} } \ else { puts stderr [format {%s: oops, bad data in line %d.} $cfg(self) $ln] exit 1 } puts [format { box {<%g, %g, %g> - %g, <%g, %g, %g> + %g}} \ [lindex $a 0] [lindex $a 1] [lindex $a 2] $hcr \ [lindex $b 0] [lindex $b 1] [lindex $b 2] $hcr] return } # -------------------------------------------------------------------- # input from pipe unless given a file name. if {!$argc} { set fp stdin } \ elseif {1 == $argc} { if {[catch {set fp [open [lindex $argv 0] r]} $what]} { puts stderr [format {%s: oops, file open failed: %s} $cfg(self) $what] exit 1 } } \ else { puts stderr [format {usage: %s [include]} $cfg(self)] exit 1 } # skip to hardwired HCR value, scan, store. set lnum 0 while {0 <= [gets $fp line]} { incr lnum if {[regexp {^#local HCR = [[:digit:]\.]+;$} $line]} { break } } if {[eof $fp]} { puts stderr [format {%s: oops, unexpected EOF at line %d.} $cfg(self) $lnum] exit 1 } \ elseif {1 != [scan [string range $line 13 end-1] %g rt(hcr)]} { puts stderr [format {%s: oops, [scan] failed in line %d.} $cfg(self) $lnum] exit 1 } # skip to 'spline' marker, read following #spheres. scan, store. while {0 <= [gets $fp line]} { incr lnum if {[regexp {^ linear_spline$} $line]} { gets $fp line incr lnum break } } if {[eof $fp]} { puts stderr [format {%s: oops, unexpected EOF at line %d.} $cfg(self) $lnum] exit 1 } \ elseif {1 != [scan [string range $line 2 end-1] %d rt(n)]} { puts stderr [format {%s: oops, [scan] failed in line %d.} $cfg(self) $lnum] exit 1 } # now read spheres, each consecutive pair becomes a box. # wrap in a 'union {}' within an 'object {}'. incr rt(n) -1 puts -nonewline "object \{\n union \{\n" set s1 [readParseSphere lnum $fp] for {set i 0} {$rt(n) != $i} {incr i} { set s2 [readParseSphere lnum $fp] outBox $s1 $s2 $rt(hcr) $lnum set s1 $s2 } puts -nonewline " \}\n\}\n" # done with file, and program. close $fp exit 0