// raisedbed01.pov
// 2013 Aug 31 
// y is vertical, x is length, z is depth
#version 3.7 ;

// dimensions in inches
#declare len    = 96.0   ; // inside length of raised bed
#declare wid    = 36.0   ; // inside width of raised bed 
#declare npost  = 3      ; // number of posts per side
#declare nboard = 2      ; // boards high
#declare nhoop  = 8      ; // number of hoops

#declare dirtd  = -2.0   ; // dirt is 2 inches below height
#declare postt  = 3.5    ; // post thickness
#declare boardw = 9.5    ; // board width (10 inch dimensional)
#declare ledgew = 5.5    ; // ledge width (6 inch dimensional )
#declare lap    = 3.0    ; // ledge end overlap 
#declare ledger = 2.0    ; // ledge corner radius
#declare boardt = 1.5    ; // board thickness
#declare g      = 0.2    ; // drawn board gap (subtract from sides)
#declare postl  = 48     ; // post depth
#declare cabler = 0.15   ; // cable thickness/2
#declare cablee = 1.0    ; // cable end
#declare tl     = 8.0    ; // turnbuckle length
#declare tr     = 0.40   ; // turnbuckle radius
#declare br     = 0.20   ; // bolt radius
#declare bh     = 0.40   ; // bolt head radius
#declare boltl  = 4.0    ; // bolt length
#declare hr     = 3.5    ; // pipe hole radius
#declare por    = 3.4    ; // pipe outer radius
#declare pir    = 3.0    ; // pipe inner radius
#declare sandl  = -0.8   ; // sand depth
#declare hoopr  = 0.6    ; // hoop radius

#declare r2d= 45.0 / atan2( 1.0, 1.0 );
#declare WoodColor  = color red 0.40 green 0.30 blue 0.02;
#declare DirtColor  = color red 0.28 green 0.25 blue 0.19;

#declare height = nboard*(boardw+g)   ;
#declare holey  = postl-height        ;
#declare hooph  = hoopr+g             ;

#include "colors.inc"
global_settings { assumed_gamma 1.0 }

// camera and sun -------------------------
light_source { < -1000, 5000, -7000> color rgb 1.0 }
camera { angle 30.0  location <70,100,-200> look_at <0,20,0> sky y  }

// clocks ---------------------------------

#declare clk10 =  10.0*clock ;

#declare chole = clk10 - 0.0 ;
#declare cpipe = clk10 - 1.0 ;
#declare cpost = clk10 - 2.0 ;
#declare cside = clk10 - 3.0 ;
#declare cledg = clk10 - 4.0 ;
#declare cbolt = clk10 - 5.0 ;
#declare cdirt = clk10 - 6.0 ;
#declare choop = clk10 - 7.0 ;

#if ( chole > 1.0 ) #declare chole =  1.0 ; #end
#if ( cpipe > 1.0 ) #declare cpipe =  1.0 ; #end
#if ( cpost > 1.0 ) #declare cpost =  1.0 ; #end
#if ( cside > 1.0 ) #declare cside =  1.0 ; #end
#if ( cledg > 1.0 ) #declare cledg =  1.0 ; #end
#if ( cbolt > 1.0 ) #declare cbolt =  1.0 ; #end
#if ( cdirt > 1.0 ) #declare cdirt =  1.0 ; #end
#if ( choop > 1.0 ) #declare choop =  1.0 ; #end

#declare csand = cledg ;  // simultaneous with ledge

#declare cend  = cside ;
#declare ccabl = cside ;

// draw lawn with holes -----------------------------------
// sinking in

#declare z0 =  wid/2 + boardt + postt/2 ;
#declare y0 =  holey/6                  ;
#declare y1 =  y0-g - chole*holey/3     ;  // only part way
#declare dx =  (len-postt)/(npost-1)    ;

union {
   difference {
      box { < -2000, -1, -2000>, < 2000, 0, 2000> }
      union {
         #declare x0 = -(len-postt)/2.0          ;
         #while( x0 < len/2 )
            cylinder{ < x0, y0,  z0 >, < x0, y1,  z0 >, hr }
            cylinder{ < x0, y0, -z0 >, < x0, y1, -z0 >, hr }
            #declare x0 = x0 + dx ;
         #end  // post loop
      }
      finish { ambient 0.2 diffuse 0.7 } pigment { SpringGreen }
   }
   
   difference {
      box { < -2000, -1, -2000>, < 2000, -100, 2000> }
      union {
         #declare x0 = -(len-postt)/2.0          ;
         #while( x0 < len/2 )
            cylinder{ < x0, y0,  z0 >, < x0, y1,  z0 >, hr }
            cylinder{ < x0, y0, -z0 >, < x0, y1, -z0 >, hr }
            #declare x0 = x0 + dx ;
         #end  // post loop
         }
      finish { ambient 0.2 diffuse 0.7 } pigment { DirtColor   }
   }
}

// draw pipes ------------------------------------------------
// sinking in

#if( cpipe > 0.0 )
   union {
   #declare z0 =  wid/2 + boardt + postt/2 ;
   #declare x0 = -(len-postt)/2.0          ;
   #declare dx =  (len-postt)/(npost-1)    ;
   #declare y0 =  postl-postl*cpipe        ;
   #declare y1 =  y0 - holey               ;
   #while( x0 < len/2 )
      difference {
         cylinder{ < x0, y0  ,  z0 >, < x0, y1  ,  z0 >, por }
         cylinder{ < x0, y0+g,  z0 >, < x0, y1-g,  z0 >, pir }
      }
      difference {
         cylinder{ < x0, y0  , -z0 >, < x0, y1  , -z0 >, por }
         cylinder{ < x0, y0+g, -z0 >, < x0, y1-g, -z0 >, pir }
      }
      #declare x0 = x0 + dx ;
   #end  // post loop
   finish { ambient 0.2 diffuse 0.7 } pigment { Gray10 } 
}
#end  // if cpipe

// draw posts -----------------------------
// sinking in
#if( cpost > 0.0 )
   #declare z0 =  wid/2  + boardt + g  ;
   #declare z1 =  z0     + postt       ;
   #declare y0 =  height - cpost*postl ;
   #declare y1 =  y0     + postl       ;
   #declare x0 = -len/2                ;
   #declare dx = (len-postt)/(npost-1) ;
   union {
      #while( x0 < len/2 )
         #declare x1 =  x0 + postt ;
         box{ < x0, y0,  z0 >, < x1,  y1,  z1 > }
         box{ < x0, y0, -z0 >, < x1,  y1, -z1 > }
         #declare x0 = x0 + dx ;
      #end  // post loop
     finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
   }
#end  // if cpost

// draw sand ------------------------------------------------
#if( csand > 0.0 )
   #declare x0 = len ;
   #declare z0 = wid ;
   #declare y0 = sandl - (1.0-csand)*holey ;
   #declare y1 = y0-1 ;
   box { < -x0, y0, -z0>, < x0, y1, z0>
      finish { ambient 0.2 diffuse 0.7 } pigment { Gray90 } }
#end // if csand

// draw end boards ------------------------
// appearing
#if( cend  > 0.0 )
   #declare  z0 =  wid/2 + boardt + postt ;
   #declare  z1 = -z0                     ;
   #declare  y0 =  0.0                    ;
   #declare  x0 =  len/2 + g              ;
   #declare  x1 =  x0 + boardt            ;
   #declare  yd =  boardw + g             ;
   union {
     #while( y0 < height*cend-g )
        #declare  y1 =  y0 + boardw            ;
        box { <  x0, y0, z0 >, <  x1, y1, z1 > }
        box { < -x0, y0, z0 >, < -x1, y1, z1 > }
        #declare y0 = y0 + yd ;
     #end
     finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
   }
#end  // if cend

// draw cables ----------------------------
// appearing
#if( ccabl > 0.0 )
   #declare x0 = len/2 - postt - bh            ;
   #declare x1 = len/2 - (len-postt)/(npost-1) ;
   #declare x2 = -x1                           ;
   #declare x3 = -x0                           ;
   #declare yd = height / nboard               ;      
   #declare y0 = 0.5 * yd                      ;
   #declare z0 = wid/2 + boardt + postt/2      ;
   #declare z1 = wid/2 + boardt + postt + 2*g  ;
   #if( npost < 3 ) 
      #declare x1 = 1  ;
      #declare x2 = -1 ;
      #declare z1 = z0 ;
   #end
   #declare tx0 = len/2 + boardt + bh + g      ;
   #declare tx1 = x0                           ;
   #declare tx2 = tx1 - tl                     ;
   #declare tx3 = tx2 - tl                     ;
   #declare slp = (z1-z0)/(x1-x0)              ;
   #declare tz0 = z0                           ;
   #declare tz1 = z0                           ;
   #declare tz2 = z0 + slp*(tx2-x0)            ;
   #declare tz3 = z0 + slp*(tx3-x0)            ;
   #while(  y0 < height*ccabl )
      union { 
         #declare zm = -1 ;
         #while( zm < 2 ) 
            // cable
            cylinder { <  x0, y0, zm* z0>, <  x1, y0, zm* z1 >, cabler }
            cylinder { <  x1, y0, zm* z1>, <  x2, y0, zm* z1 >, cabler }
            cylinder { <  x2, y0, zm* z1>, <  x3, y0, zm* z0 >, cabler }
            // turnbuckle
            cylinder { < tx2, y0, zm*tz2>, < tx3, y0, zm*tz3 >, tr     }
            // bolts
            sphere { <  tx0, y0, zm*tz0>, bh }
            sphere { <  tx1, y0, zm*tz0>, bh }
            sphere { < -tx0, y0, zm*tz0>, bh }
            sphere { < -tx1, y0, zm*tz0>, bh }
            #declare zm = zm + 2 ;
         #end  // z loop 
         finish { ambient 0.5 diffuse 0.7 } pigment { Gray30 }
      }
      #declare y0 = y0 + yd ;
   #end  // y loop
#end  // if ccabl

// draw side boards -----------------------
// appearing
#if( cside > 0.0 )
   #declare  z0 =  wid/2                  ;
   #declare  z1 =  z0 + boardt            ;
   #declare  y0 =  0.0                    ;
   #declare  x0 =  len/2                  ;
   #declare  x1 =  -x0                    ;
   #declare  yd =  boardw + g             ;
   #declare  yy =  boardw*0.8             ;
   #while( y0 < height*cside-(g+yy) )
      #declare  y1 =  y0 + boardw         ;
      union {
         box { < x0, y0,  z0 >, < x1, y1,  z1 > }
         box { < x0, y0, -z0 >, < x1, y1, -z1 > }
         finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
      }
      #declare y0 = y0 + yd ;
   #end
#end  // if cside

// hoops object -----------------------------

#declare hz0 =  wid/2  + boardt + hoopr  ; //
#declare hy0 =  0                        ; // bottom of hoop posts
#declare hy1 =  hy0 +boardt + height + g ; // top of hoop posts
#declare hooph = hoopr + g               ; // hole through board

#declare allhoops = union {
   #declare hx0 =  hoopr-(len/2-postt)   ; // left side hoop position
   #declare hdx =  2*hx0/(1-nhoop)       ; // delta spacing
   #while( hx0 < len/2 )
      #declare hx1 = hx0 - 2*hoopr       ; //
      #declare hx2 = hx0 + 2*hoopr       ; //
      #declare hy2 = hy1 + hz0 + 2*hoopr ; //
      #declare hz1 = hz0 + 2*hoopr       ; //
      cylinder{ < hx0, hy0,  hz0 >, < hx0, hy1,  hz0 >, hoopr }
      cylinder{ < hx0, hy0, -hz0 >, < hx0, hy1, -hz0 >, hoopr }
      intersection {
         box  { < hx1, hy1,  hz1 >, < hx2, hy2, -hz1 >        }
         torus{ hz0, hoopr rotate 90*z translate <hx0, hy1, 0>}
       }
       #declare hx0 = hx0 + hdx ;
    #end  // post loop
   finish { ambient 0.5 diffuse 0.7 } pigment { White }
}

#declare hoophole = union {
   #declare hx0 =  hoopr-(len/2-postt)      ; // left side hoop position
   #declare hdx =  2*hx0/(1-nhoop)          ; // delta spacing
   #while( hx0 < len/2 )
      cylinder{  < hx0, hy0,  hz0 >, < hx0, hy1,  hz0 >, hooph }
      cylinder{  < hx0, hy0, -hz0 >, < hx0, hy1, -hz0 >, hooph }
      #declare hx0 = hx0 + hdx ;
   #end  // post loop
   finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor  }
}

// top bolts object -----------------------------

#declare allbolts = union {
   #declare bz0 =  wid/2  + boardt + postt/2    ;
   #declare by0 =  height + boardt + g   ;
   #declare by1 =  by0    - boltl        ;
   #declare bx0 = -(len-postt)/2.0       ;
   #declare bdx =  (len-postt)/(npost-1) ;
      #while( bx0 < len/2 )
         sphere  { < bx0, by0,  bz0 >,                      bh }
         cylinder{ < bx0, by0,  bz0 >, < bx0,  by1,  bz0 >, br }
         sphere  { < bx0, by0, -bz0 >,                      bh }
         cylinder{ < bx0, by0, -bz0 >, < bx0,  by1, -bz0 >, br }
      #declare bx0 = bx0 + bdx ;
   #end  // post loop
   finish { ambient 0.5 diffuse 0.7 } pigment { Gray30 }
}

// draw ledge boards ------------------------
//  descend from top
#if( cledg > 0.0 )
   #declare  z0 =  wid/2                ;
   #declare  z1 =  z0     + ledgew      ;
   #declare  y0 =  height *(2.0-cledg)  ;
   #declare  y1 =  y0     + boardt      ;
   #declare  x0 =  len/2 + boardt + lap ;
   #declare  x1 = -x0                   ;
   #declare lx0 =  x0 + 1.0             ;
   #declare lx1 =  x0 + g - ledger      ;
   #declare ly0 =  y0 - 1.0             ;
   #declare ly1 =  y1 + 1.0             ;
   #declare lz0 =  z1 + 1.0             ;
   #declare lz1 =  z1 + g - ledger      ;
   difference {
      union {
        box{ < x0, y0,  z0 >,  <x1, y1,  z1> }
        box{ < x0, y0, -z0 >,  <x1, y1, -z1> }
      }
      difference {                 // rounded corners
         union {
           // negative block, corners
           box      { <  lx0, ly0,  lz0 >, <  lx1, ly1,  lz1 >         }
           box      { < -lx0, ly0,  lz0 >, < -lx1, ly1,  lz1 >         }
           box      { <  lx0, ly0, -lz0 >, <  lx1, ly1, -lz1 >         }
           box      { < -lx0, ly0, -lz0 >, < -lx1, ly1, -lz1 >         }
           // negative block, hoop and bolt holes
           #if( cbolt > 0.0 ) 
              object { hoophole
                 finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
              }
              object { allbolts
                 finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
              }
           #end 
         }
         union {  // double negative cylinder
           cylinder { <  lx1, ly0,  lz1 >, <  lx1, ly1,  lz1 >, ledger }
           cylinder { < -lx1, ly0,  lz1 >, < -lx1, ly1,  lz1 >, ledger }
           cylinder { <  lx1, ly0, -lz1 >, <  lx1, ly1, -lz1 >, ledger }
           cylinder { < -lx1, ly0, -lz1 >, < -lx1, ly1, -lz1 >, ledger }
         }
      }
      finish { ambient 0.5 diffuse 0.7 } pigment { WoodColor }
   }
#end  // if cledg

// draw top bolts -----------------------------
// sinking in
#if( cbolt > 0.0 )
   #declare byd =  (1.0-cbolt)*1.6*boltl ;
   object{ allbolts translate byd*y }
#end  // if cbolt

// dirt ---------------------------------------------------
// filling
#if( cdirt > 0.0 )
  #declare dy1 = cdirt*height + dirtd;
  box{ <-len/2, dy1 , -wid/2 >, < len/2, -1.0 ,  wid/2 >
       finish { ambient 0.1 diffuse 0.7 } pigment { DirtColor }
  }
#end  // if cdirt

// draw hoops -----------------------------
// sinking in
#if( choop > 0.0 ) 
   #declare hpy =  1.2*height*(1-choop) ;
   object { allhoops translate < 0, hpy, 0 > }
#end  // if hoop

