// Persistence of Vision Ray Tracer Scene Description File
// File: ?.pov
// Vers: 3.5
// Desc: Basic Scene Example
// Date: mm/dd/yy
// Auth: ?
//

#version 3.5;

#include "colors.inc"
#include "finish.inc"
#include "textures.inc"
#include "stones.inc"

global_settings {
  assumed_gamma 1.0
}

// ----------------------------------------

camera {
  location  <0.0, 3.5, -4.0>*4
  direction 1.5*z
  right     x*image_width/image_height
  look_at   <0.0, 0.0,  -1>
  angle 18
}


light_source {
  <0, 0, 0>            // light's position (translated below)
  color rgb <1, 1, 1>  // light's color
  translate <-30, 30, -30>
}  
#declare lclock=clock+0;
#declare g=.1;  //strength of gravity


     #declare r00=-4*z+1.5*x;   //impactor starting position
     #declare v00=.1*z;         //impactor starting velocity
     #declare Energy=.5*pow(vlength(v00),2)-g/vlength(r00);

     
     
#declare w_depth=1.1;
#declare w_width=.65;
#declare gstart=5;
#declare seperation=.9;
#declare surf_width=1.5*seperation;
#declare rstar=.4;
#declare rmin=.4;
#declare KEmax=Energy+g/(.98*rmin); //this is for color scaling
#declare spig=     pigment{Black}    //for rmax= .65
               //pigment{Yellow}   //for rmax= .8
               //pigment{Gray*.01} //for rmax= .5
               //pigment{White}    //for rmax= .65
#declare sfin=     finish{Luminous}    //Luminous except for Dull for rstar=.5
              
#declare fny = function(xp,zp) {min(1/sqrt(xp*xp+zp*zp+1e-6),1/rstar)} //isosurface
#declare fn2y = function(xp,zp) {min(1/sqrt(xp*xp+zp*zp+1e-6),1/(rstar-.1))} //isosurface
#declare fns = function(xp,yp,zp) {yp+fny(xp,zp)} //isosurface
// ----------------------------------------
isosurface {
  function { fns(x, y, z) }        // alternative declared function
  contained_by { box { -<2,3,4>, <2,1,2>} }  // container shape
  threshold 0.0                     // optional threshold value for isosurface [0.0]
  accuracy 0.01                      // accuracy of calculation [0.001]
 // max_gradient 6                      // maximum gradient the function can have [1.1]
   max_gradient 5                     // neg curve
  //evaluate 5, 1.2, 0.95             // evaluate the maximum gradient
  //max_trace 1                       // maybe increase for use in CSG [1]
  //all_intersections                 // alternative to 'max_trace'
  open                              // remove visible container surface
  texture{
    #declare k=5; 
          pigment{
            brick      
            color Yellow*.5          // color of mortar
            color rgb <.0,.0,.8>            // color of brick
            brick_size <1,1,1>   // size of brick    (optional)
            mortar     0.055       // width of mortar  (optional)
            translate y*.5
            scale <1,50,1>
            scale .5
          }
       //finish { ambient 0.2  specular 0.2  diffuse .6}
         finish{Dull}
 
  }
}
cylinder{0,1e-2*y,rstar
     pigment{spig} 
     translate -fny(rstar,0)*y
     finish{Luminous}
     no_shadow
}
sphere{0,rstar 
     pigment{spig} 
      finish{sfin}  //Dull for rstar = .5, Luminous for .65, .8
     no_shadow
     translate .2*y
}
#macro runge4(rold,vold,rnew,vnew,dt)

     #local k1x=vold*dt;
     #local k1v=-g*rold/pow(vlength(rold),3)*dt;
     
     #local k2x=(vold+.5*k1v)*dt;
     #local k2v=-g*(rold+.5*k1x)/pow(vlength(rold+.5*k1x),3)*dt;  
     
     #local k3x=(vold+.5*k2v)*dt;
     #local k3v=-g*(rold+.5*k2x)/pow(vlength(rold+.5*k2x),3)*dt;
     
     #local k4x=(vold+k3v)*dt;
     #local k4v=-g*(rold+k3x)/pow(vlength(rold+k3x),3)*dt;
                                                   
     #declare rnew=rold+(k1x+2*k2x+2*k3x+k4x)/6;
     #declare vnew=vold+(k1v+2*k2v+2*k3v+k4v)/6;

#end

     #declare r0=r00;
     #declare v0=v00;

     #declare r1=r0;
     #declare v1=v0;
     #declare d_t =.005;


     #declare Dti=.05;
     #declare tt=0;
     #declare ti=0;
#declare cspline =
  spline {
   linear_spline
      0.0 , <0.0,0.0,1>
      0.3 , <.0,1,0.5>
      0.6 , <.5,1,0.0>
      1.0 , <1,0.0,0.0>
  }   
  #declare old=r00;
#while (tt<=lclock)
     runge4(r0,v0,r1,v1,d_t) 
     #declare r0=r1;
     #declare v0=v1;
   
     #declare ti=ti+d_t;
     #declare tt=tt+d_t;
          #declare KE=Energy+g/vlength(r1);
     #if(ti>=Dti)
          #declare dy=-fn2y(r1.x,r1.z)*y;
          #declare kcolor=cspline(KE/KEmax);
          #declare new=r1+dy;
          cylinder {old,new, .075   
               pigment {color rgb kcolor } 
               no_shadow  no_reflection 
               finish{Luminous}
          }
          #declare old=new;
          #declare ti=0;
          #if(vlength(r1)<rstar)
               #declare tt=lclock;
          #end     
     #end     
     
#end
          sphere {0 , .075   
                texture{T_Stone31 scale  .5}
               translate r1+.2*y
               no_shadow  no_reflection
          }

union{
     cylinder { 0,min(KE,KEmax)/KEmax*y,.3
        pigment {
         gradient y
         color_map {
           [0.0 rgb <0.0,0.0,1>]
           [0.3 rgb <.0,1,0.5>]
           [0.6 rgb <.5,1,0.0>]
           [1.0 rgb <1,0.0,0.0>]
         }
       }
       finish{Luminous}
       scale<1,1,.01>
          translate 2.1*z-.4*y
     }
     
     // create a TrueType text shape
     text {   ttf  "crystal.ttf",   "Impactor", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z-1.4*x+.22*y
     }
     text {   ttf  "crystal.ttf",   "Kinetic ", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z-1.4*x
     }
     text {   ttf  "crystal.ttf",   "Energy", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z-1.4*x-.22*y
     }
     translate -.4*x
}  
#declare rstart=2;
#declare ymax=g/rstar;
#declare dy=.2/40;
#declare ynow=0;
#declare ynext=g/rstart;
#declare rnow=rstart;
#declare rnext=rstart; 
union{
     #while(ynext<=ymax)
          cone{ynow*y,rnow,ynext*y,rnext
          }                 
          #declare ynow=ynext;
          #declare ynext=ynext+dy;
          #declare rnow=g/ynow;
          #declare rnext=g/ynext; 
     #end
     pigment{ color rgb <.0,.0,.8>  }
     scale -2*<.1,1,.0001>
  finish{Dull}
     translate 2.1*z+.2*y+.5*x
} 
union{
     #while(ynext<=1*g/.3)
          cone{ynow*y,rnow,ynext*y,rnext
          }                 
          #declare ynow=ynext;
          #declare ynext=ynext+dy;
          #declare rnow=g/ynow;
          #declare rnext=g/ynext; 
     #end
     pigment{ spig }
     scale -2*<.1,1,.0001>
  finish{sfin}
     translate 2.1*z+.2*y+.5*x
} 

     // create a TrueType text shape
     text {   ttf  "crystal.ttf",   "Gravitational", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z+1.0*x+.22*y
     }
     text {   ttf  "crystal.ttf",   "Well", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z+1.0*x+0*y
     }
     text {   ttf  "crystal.ttf",   "Depth ", .02,  0  
          pigment{color Gray}
          scale .25
          translate 2*z+1.0*x-.22*y
     }
