# Color3Shells.slf
# add example of shell #14-#12 = "F"
# parametrization of all legal colorings
# add some check to see that on the 12-option shells, ANY proto-edge can be used.
##################################################################################

tclinit { 
  source SLIDEUI.tcl

  set winName .slfWINDOW
}

tclinit {
  proc CreateParamUI { parent name } {

    set subname "slf_[subst $name]"

    if { $parent == {} } {
      set root .$subname
    } elseif { $parent == "." } {
      set root .$subname
    } else {
      set root $parent.$subname
    }

    toplevel $root

    label $root.rowoTe -text "Outer Tetra Edges: 4 options:"
    scale $root.oTe -from 0 -to 3 -orient horizontal -resolution 1 -variable oTe
    global oTe
    set oTe 0

    label $root.rowoTt -text "Trunc. Tetra Edges: 12 options:"
    scale $root.oTt -from 0 -to 11 -orient horizontal -resolution 1 -variable oTt
    global oTt
    set oTt 0

    label $root.rowoTh -text "Tetra Half-Edges: 12 options:"
    scale $root.oTh -from 0 -to 11 -orient horizontal -resolution 1 -variable oTh
    global oTh
    set oTh 0

    label $root.rowoTp -text "TEST: Permutation of Proto-Edge:"
    scale $root.oTp -from 0 -to 11 -orient horizontal -resolution 1 -variable oTp
    global oTp
    set oTp 0

    grid $root.rowoTe    -row 1 -col 0 -padx 10 -pady 10 -sticky e,w
    grid $root.oTe       -row 1 -col 1
    grid $root.rowoTt    -row 2 -col 0 -padx 2 -pady 2 -sticky e,w
    grid $root.oTt       -row 2 -col 1
    grid $root.rowoTh    -row 3 -col 0 -padx 2 -pady 2 -sticky e,w
    grid $root.oTh       -row 3 -col 1
    
    grid $root.rowoTp    -row 4 -col 0 -padx 2 -pady 2 -sticky e,w
    grid $root.oTp       -row 4 -col 1

    wm geometry $root +0+20
  }

  CreateParamUI $winName param
}

### BASIC GEOMETRIC ELEMENTS ########################

sphere ball
 radius 0.7
 zslices 2
 thetaslices 4
endsphere

crosssection cs
  type circle
  radius 0.2
  slices 4
endcrosssection

######################### Some of the C-600 Points #########################

# POINTS
# SHELL 14
point v116 ( -5 -5 5 ) endpoint
point v117 ( 5 -5 -5 ) endpoint
point v118 ( -5 5 -5 ) endpoint
point v119 ( 5 5 5 ) endpoint

# SHELL 13
point v112 ( -3.37630689383837 -3.37630689383837 -3.376293 ) endpoint
point v113 ( 3.37630689383837 -3.37630689383837 3.376293 ) endpoint
point v114 ( -3.37630689383837 3.37630689383837 3.376293 ) endpoint
point v115 ( 3.37630689383837 3.37630689383837 -3.376293 ) endpoint

# SHELL 12
point v106 ( 0 -4 0 ) endpoint
point v107 ( -4 0 0 ) endpoint
point v108 ( 0 0 -4 ) endpoint
point v109 ( 0 0 4 ) endpoint
point v110 ( 4 0 0 ) endpoint
point v111 ( 0 4 0 ) endpoint

# SHELL 11
point v94 ( -2.6735608401714 -2.6735608401714 0.631144 ) endpoint
point v95 ( -0.631141592576895 -2.67355871885106 2.673552 ) endpoint
point v96 ( -2.67355871885106 -0.631141592576895 2.673552 ) endpoint
point v97 ( 0.631141592576896 -2.67355871885106 -2.673552 ) endpoint
point v98 ( -2.67355871885106 0.631141592576896 -2.673552 ) endpoint
point v99 ( 2.6735608401714 -2.6735608401714 -0.631144 ) endpoint
point v100 ( -2.6735608401714 2.6735608401714 -0.631144 ) endpoint
point v101 ( 2.67355871885106 -0.631141592576896 -2.673552 ) endpoint
point v102 ( -0.631141592576896 2.67355871885106 -2.673552 ) endpoint
point v103 ( 2.67355871885106 0.631141592576895 2.673552 ) endpoint
point v104 ( 0.631141592576895 2.67355871885106 2.673552 ) endpoint
point v105 ( 2.6735608401714 2.6735608401714 0.631144 ) endpoint

group balls
# SHELL 14 -- Outermost vertices for reference
instance ball  surface S14  translate ( -5 -5 5 ) endinstance
instance ball  surface S14  translate ( 5 -5 -5 ) endinstance
instance ball  surface S14  translate ( -5 5 -5 ) endinstance
instance ball  surface S14  translate ( 5 5 5 )  endinstance
endgroup

######################### OUTER TETRAHEDRON #########################

# POLYLINES
polyline pl307 pointlist ( v116 v117 ) endpolyline ##
polyline pl309 pointlist ( v116 v118 ) endpolyline ##
polyline pl311 pointlist ( v116 v119 ) endpolyline ##
polyline pl415 pointlist ( v117 v118 ) endpolyline ##
polyline pl417 pointlist ( v117 v119 ) endpolyline ##
polyline pl464 pointlist ( v118 v119 ) endpolyline ##

group edges
# outermost shell -- TETRAHEDRON
instance pl307 	surface SA endinstance
instance pl309 	surface SA endinstance
instance pl311 	surface SA endinstance
instance pl415 	surface SA endinstance
instance pl417 	surface SA endinstance
instance pl464 	surface SA endinstance
endgroup

# Prototype TETRA EDGE on the +X-face
polyline tetraline
  pointlist (v117 v119)
endpolyline
sweep tetraedge
  path tetraline  endpath
  crosssection cs  begincap 1  endcap 1  endcrosssection
endsweep

group protopath1
  instance tetraedge 
    rotate(1 0 0)({expr $oTe * 90})  rotate(0 0 1)({expr $oTe * 90})  
  endinstance
endgroup


######################## TRUNC TETRA #######################

polyline pl80 pointlist ( v94 v95 ) endpolyline
polyline pl82 pointlist ( v94 v96 ) endpolyline
polyline pl170 pointlist ( v95 v96 ) endpolyline
polyline pl283 pointlist ( v97 v99 ) endpolyline
polyline pl287 pointlist ( v97 v101 ) endpolyline
polyline pl322 pointlist ( v98 v100 ) endpolyline
polyline pl323 pointlist ( v98 v102 ) endpolyline
polyline pl367 pointlist ( v99 v101 ) endpolyline
polyline pl508 pointlist ( v100 v102 ) endpolyline
polyline pl623 pointlist ( v103 v104 ) endpolyline
polyline pl626 pointlist ( v103 v105 ) endpolyline
polyline pl653 pointlist ( v104 v105 ) endpolyline

group edges2
# -- Truncated Tetra
instance pl80 	surface SB endinstance
instance pl82 	surface SB endinstance
instance pl170 	surface SB endinstance
instance pl283 	surface SB endinstance
instance pl287 	surface SB endinstance
instance pl322 	surface SB endinstance
instance pl323 	surface SB endinstance
instance pl367 	surface SB endinstance
instance pl508 	surface SB endinstance
instance pl623 	surface SB endinstance
instance pl626 	surface SB endinstance
instance pl653 	surface SB endinstance
endgroup

# Prototype TRUNC TETRA EDGE on X-side edge of {1 1 1} triangle.
polyline truncline
  pointlist (v103 v105)
endpolyline
sweep trunctetra
  path truncline  endpath
  crosssection cs  begincap 1  endcap 1  endcrosssection
endsweep

group protopath2
  instance trunctetra 
    rotate(0 0 1)({expr 90*( $oTt + $oTt / 6 )}) 
    rotate(1 -1 0)({expr -35.26*( pow(-1, ( $oTt + $oTt / 6 )) -1) }) 
    rotate(1 1 1)({expr 60*( $oTt + $oTt / 6 )}) 
  endinstance
    ### The idea here is to have a single slider that produces all the 12 possible legal constellations.
    ### Basically this parameter walks around the tetrahedron in 60 degree steps around the 111-axis.
    ### Thus we have to laternate between pyramid edges and base-edges.
    ### To distinguish between the two we need to flip the edge through 90 degree rotations,
    ### and in addition also tilt it through 70.5 degrees to aling it with the pyramid edges.
    ### 12 steps of that visits all tetra edges twice with opposite orientations.
    
    ### The second half-edge is then generated from that position by a flip either around the x- or y-axis.
    ### To pick exactly one non-redundant set of half-edge combinations we need to know when to flip x or y.
    ### An inspection of the behavior showed that an x-flip for steps "0" thru "5", combined with
    ### a y-flip for steps "7" thru "12" would cover all combinations; step "6" would be left out.
    ### This can be achieved with the integer division by 6 which adds +1 from step 6 on, and also
    ### shifts from the x- to the y-flip at the right moment.

  instance trunctetra 
    rotate(0 0 1)({expr 90*( $oTt + $oTt / 6 )}) 
    rotate(1 -1 0)({expr -35.26*( pow(-1, ( $oTt + $oTt / 6 )) -1) }) 
    rotate(1 1 1)({expr 60*( $oTt + $oTt / 6 )}) 
    rotate(1 0 0)({expr 90*( 1 + pow(-1, $oTt / 6) ) })    rotate(0 1 0)({expr 90*( pow(-1, $oTt / 6) -1) }) 
  endinstance
endgroup

################  TETRA HALF-EDGES  #######################

polyline pl159 pointlist ( v106 v116 ) endpolyline
polyline pl162 pointlist ( v106 v117 ) endpolyline
polyline pl245 pointlist ( v107 v116 ) endpolyline
polyline pl248 pointlist ( v107 v118 ) endpolyline
polyline pl308 pointlist ( v116 v109 ) endpolyline
polyline pl414 pointlist ( v117 v108 ) endpolyline
polyline pl418 pointlist ( v117 v110 ) endpolyline
polyline pl433 pointlist ( v108 v118 ) endpolyline
polyline pl441 pointlist ( v109 v119 ) endpolyline
polyline pl467 pointlist ( v118 v111 ) endpolyline
polyline pl558 pointlist ( v119 v110 ) endpolyline
polyline pl562 pointlist ( v119 v111 ) endpolyline

group edges3
# SHELL "F"
instance pl159 	surface SF endinstance
instance pl162 	surface SF endinstance
instance pl245 	surface SF endinstance
instance pl248 	surface SF endinstance
instance pl308 	surface SF endinstance
instance pl414 	surface SF endinstance
instance pl418 	surface SF endinstance
instance pl433 	surface SF endinstance
instance pl441 	surface SF endinstance
instance pl467 	surface SF endinstance
instance pl558 	surface SF endinstance
instance pl562 	surface SF endinstance
endgroup

# Prototype TETRA HALF-EDGE underneath tetra edge on X-face.
polyline halfline
  pointlist (v119 v110)
endpolyline
sweep halfedge
  path halfline  endpath
  crosssection cs  begincap 1  endcap 1  endcrosssection
endsweep

group halfedgeP
  instance halfedge 
    rotate(0 0 1)({expr 90*( $oTp + $oTp / 6 )}) 
    rotate(1 -1 0)({expr -35.26*( pow(-1, ( $oTp + $oTp / 6 )) -1) }) 
    rotate(1 1 1)({expr 60*( $oTp + $oTp / 6 )}) 
  endinstance
endgroup

group protopath3
  instance halfedgeP 
    rotate(0 0 1)({expr 90*( $oTh + $oTh / 6 )}) 
    rotate(1 -1 0)({expr -35.26*( pow(-1, ( $oTh + $oTh / 6 )) -1) }) 
    rotate(1 1 1)({expr 60*( $oTh + $oTh / 6 )}) 
  endinstance
  instance halfedgeP 
    rotate(0 0 1)({expr 90*( $oTh + $oTh / 6 )}) 
    rotate(1 -1 0)({expr -35.26*( pow(-1, ( $oTh + $oTh / 6 )) -1) }) 
    rotate(1 1 1)({expr 60*( $oTh + $oTh / 6 )}) 
    rotate(1 0 0)({expr 90*( 1 + pow(-1, $oTh / 6) ) })    rotate(0 1 0)({expr 90*( pow(-1, $oTh / 6) -1) }) 
  endinstance 
endgroup


###############################################################
# VERTEX COLORS
surface S14 color (0.5 0.5 0.5) endsurface

# EDGE COLORS -- INTRASHELLS
surface SA color (0.2 0.2 0.2) endsurface
surface SB color (0.2 0.4 0.2) endsurface
surface SF color (0.4 0.2 0.2) endsurface

# COLORS for PATH COPIES
surface R color (1.0 0.2 0.0) endsurface #= prototype
surface Y color (1.0 1.0 0.0) endsurface
surface C color (0.0 1.0 1.0) endsurface
surface B color (0 0 1.0) endsurface
surface G color (0 1 0) endsurface
surface M color (0.8 0.0 1.0) endsurface

group assembly
  instance balls  endinstance
  instance edges  endinstance
  instance protopath1  surface R  endinstance
  instance protopath1  surface G  rotate(1 1 1)(120)  endinstance
  instance protopath1  surface B  rotate(1 1 1)(-120) endinstance
  instance protopath1  surface C  rotate(0 0 1)(180)  endinstance
  instance protopath1  surface M  rotate(0 0 1)(180) rotate(1 1 1)(120) endinstance
  instance protopath1  surface Y  rotate(0 0 1)(180) rotate(1 1 1)(-120) endinstance   
### The above defines the global transformation that transforms the PROTOPATH into 6 colored copies:
### RED is the PROTOTYPE; CYAN lies opposite of it.
### Then 2 more copies are made of this pair by rotating around the {1 1 1} axis.

  instance edges2      endinstance
  instance protopath2  surface R  endinstance
  instance protopath2  surface G  rotate(1 1 1)(120)  endinstance
  instance protopath2  surface B  rotate(1 1 1)(-120) endinstance
  instance protopath2  surface C  rotate(0 0 1)(180)  endinstance
  instance protopath2  surface M  rotate(0 0 1)(180) rotate(1 1 1)(120) endinstance
  instance protopath2  surface Y  rotate(0 0 1)(180) rotate(1 1 1)(-120) endinstance   

  instance edges3      endinstance
  instance protopath3  surface R  endinstance
  instance protopath3  surface G  rotate(1 1 1)(120)  endinstance
  instance protopath3  surface B  rotate(1 1 1)(-120) endinstance
  instance protopath3  surface C  rotate(0 0 1)(180)  endinstance
  instance protopath3  surface M  rotate(0 0 1)(180) rotate(1 1 1)(120) endinstance
  instance protopath3  surface Y  rotate(0 0 1)(180) rotate(1 1 1)(-120) endinstance   
endgroup

################ ILLUMINATION, VIEWING, RENDERING ###################################
group World
  instance assembly
    scale (0.02 0.02 0.02)
  endinstance
endgroup

camera cam
  #projection SLF_PARALLEL
  projection SLF_PERSPECTIVE
  frustum (-0.1 -0.1 -2) (0.1 0.1 -0.1)
endcamera

group gCamera
  instance cam
    id instCam
    translate (0.0 0.0 1.0)
  endinstance
endgroup

#### LIGHT
light lite
  type SLF_DIRECTIONAL
endlight

group gLight0
  instance lite
    id instLite0
    lookat
      eye (1.0 1.0 1.0)
      target (0.0 0.0 0.0)
      up (0.0 1.0 0.0)
    endlookat
  endinstance
endgroup

group gLight1
  instance lite
    id instLite1
    lookat
      eye (-1.0 -1.0 0.0)
      target (0.0 0.0 0.0)
      up (0.0 1.0 0.0)
    endlookat
  endinstance
endgroup

light lite2
  type SLF_AMBIENT
  color (0.3 0.3 0.3)
endlight

group gLight2
  instance lite2
    id instLite2
  endinstance
endgroup

#### RENDER

window WINDOW
  background (1 1 1)
endwindow

viewport VIEWPORT WINDOW
  origin (0.0 0.0)
  size (1.0 1.0)
endviewport

render VIEWPORT gCamera.instCam.cam World
  light gLight0.instLite0.lite
  light gLight1.instLite1.lite
  light gLight2.instLite2.lite2
endrender

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