/*
Antialias=on

Antialias_Threshold=0.3
Antialias_Depth=3
Input_File_Name=x_ray_compton_scattering.pov
Output_File_Name=x_ray_compton_scattering

Initial_Frame=1
Final_Frame=160
Initial_Clock=0
Final_Clock=8

Cyclic_Animation=off
Pause_when_Done=off
*/
#version 3.5;

#include "colors.inc"
#include "rand.inc"

global_settings {
  assumed_gamma 1.0
    ambient_light rgb <1,1,1> 

}

// ----------------------------------------
// create a regular point light source
light_source {
  0*x                  // light's position (translated below)
  color rgb <1,1,1>    // light's color
  translate <-20, 40, -20>
}

camera {
  location  12*<0.0, 0,-6.50>
  direction 1.5*z
  right     x*image_width/image_height
  look_at   <0.0, 0.0,  0.0>
}
#declare lclock=clock+0;
#declare linterpolater=function(xx,xa,xb,a,b){
        (xx-xa)*(b-a)/(xb-xa)+a
}
 //krypton
 
 
//building an approximate nucleus from a simple cubic array of nucleons  2, 8, 18, 8
#declare n_size=.8;     //"size" of nucleons ->sets scale in image
#declare nucleon=sphere{0, n_size*.6 };
#declare A=86;         //Atomic mass number
#declare Z=36;          //Atomic number
#declare frac=Z/A;
#declare n_rad=pow(3*A/(4*pi),.33333)*n_size;     //radius of nucleus
#declare Rn=n_rad;
//outer nuclear shell
#declare ncloud=1.5*n_rad;
sphere{ 0,ncloud
    texture {pigment {color rgbf <1,1,1,1>}}
    hollow
    interior {
        media {
            emission color rgbt <.7,.0,.0,.3>
            scattering {5}
            intervals 2 samples 32
            method 3
           // aa_threshold 0.3 aa_level 5
            density {spherical scale ncloud}
        }
    }
}

#declare Nsh = int(pi*n_rad/n_size);
#declare i=0;
#declare sr=-.62321;
union{
        #while(i<=Nsh)
                #declare qi=(i/Nsh*pi);
                #declare Ri=n_rad*sin(qi);
                #declare Ni=max(1,int(2*pi*Ri/n_size));
                #declare yi=n_rad*cos(qi);
                #declare j=1;
                        #while(j<=Ni)
                                object{nucleon translate yi*y+Ri*z rotate y*j*360/Ni
                                #declare sr=SRand(sr);
                                #if(abs(sr)<frac)
                                        texture {pigment { color rgbt <.9,.0,.0,0> } finish{ambient .40 diffuse .50}}
                                #else
                                        texture {pigment { color rgbt <.2,.2,.2,0> } finish{ambient .40 diffuse .50}}
                                #end
                                }
                                #declare j=j+1;
                        #end
                #declare i=i+1;
        #end 
        rotate x*90
}
//one in nuclear shell
union{
        #declare n_rad=n_rad-n_size;     //radius of nuclear shell
        #declare Nsh = int(pi*n_rad/n_size);
        #declare i=0;
        #while(i<=Nsh)
                #declare qi=(i/Nsh*pi);
                #declare Ri=n_rad*sin(qi);
                #declare Ni=max(1,int(2*pi*Ri/n_size));
                #declare yi=n_rad*cos(qi);
                #declare j=1;
                        #while(j<=Ni)
                                object{nucleon translate yi*y+Ri*z rotate y*j*360/Ni
                                #declare sr=SRand(sr);
                                #if(abs(sr)<frac)
                                        texture {pigment { color rgbt <.9,.0,.0,0> } finish{ambient .40 diffuse .50}}
                                #else
                                        texture {pigment { color rgbt <.2,.2,.2,0> } finish{ambient .40 diffuse .50}}
                                #end
                                }
                                #declare j=j+1;
                        #end
                #declare i=i+1;
        #end 
rotate x*90}

//krypton 2, 8, 18, 8
//electron orbits and occupancy
#declare res = array[7];
#declare nes = array[7];
#declare enorb = 1.2*Rn;
#declare res[1]= 2*enorb;  //K shell
#declare nes[1]= 2;
#declare res[2]= 3*enorb;  //L shell
#declare nes[2]= 8;
#declare res[3]= 4*enorb;  //M shell
#declare nes[3]= 18;
#declare res[4]= 5*enorb;  //N shell
#declare nes[4]= 8;
#declare res[5]= 6*enorb;  //O shell
#declare nes[5]= 12;
#declare res[6]= 7*enorb;  //P shell
#declare nes[6]= 2;
#declare e_size = n_size*.75;
#declare electron=sphere{0, e_size texture {pigment { color rgbt <.2,.2,.9,0> } finish{ambient .80 diffuse .50}}};
#declare ecloud=res[4];
//skip one electron    
//target
#declare i_target=4;  //which shell
#declare n_target=7;  //which electron
#declare y_target=res[i_target]*cos((n_target-1)/nes[i_target]*2*pi);
#declare x_target=res[i_target]*sin((n_target-1)/nes[i_target]*2*pi);

//photon
#declare photon_wave=function(x) {
        (cos(5*x) )*
    exp(-.09*abs((x+3.5)*(x+3.5)))
        }
#declare photon_wave2=function(x) {
        (cos(3*x) )*
    exp(-.09*abs((x+3.5)*(x+3.5)))
        }
#declare wstart=-2.5*pi;
#declare wend =0;
#declare wthick=.15;
#declare wnseg=50;
#declare wxnew=wstart;
#declare wynew=photon_wave(wxnew);
#declare wdx=(wend-wstart)/wnseg;
#declare phsize=enorb*.3;
//photon location
#declare x_0=-14*enorb;
#declare c=abs(x_0)/3;//3sec in, need more for e out sec out
#declare xp=x_0+c*lclock;
#declare Qp=30;

#declare pcount=0;
#declare photon=union{
     sphere_sweep {
       cubic_spline
       wnseg+1,
                #while (pcount<wnseg+1)
                        #declare wxnew=pcount*wdx+wstart;
                        #declare wynew=photon_wave(wxnew);
                        <wxnew,wynew,0>,wthick
                        #declare pcount=pcount+1;
                #end
     }
                cone{(wxnew-1*wdx)*x,4*wthick,(wxnew+1.15)*x,0 scale <1,1,wthick>}
                           
                scale phsize*<1,1,1>
                translate xp*x 
} 
#declare pcount=0;
#declare photon2=union{
     sphere_sweep {
       cubic_spline
       wnseg+1,
                #while (pcount<wnseg+1)
                        #declare wxnew=pcount*wdx+wstart;
                        #declare wynew=photon_wave2(wxnew);
                        <wxnew,wynew,0>,wthick
                        #declare pcount=pcount+1;
                #end
     }
                cone{(wxnew-1*wdx)*x,4*wthick,(wxnew+1.15)*x,0 scale <1,1,wthick>}
                           
                scale phsize*<1,1,1>
                translate xp*x 
} 

intersection{
     object{photon   }
     cylinder {0,-15*enorb*x,phsize*1.2}        
                texture {pigment { color rgbt <1,1,0,0> } finish{ambient 1 diffuse 1}}
       translate x_target*x +y_target*y-z
}


intersection{
     object{photon2   }
     cylinder {0,20*enorb*x,phsize*1.2}        
                texture {pigment { color rgbt <1,.0,0,0> } finish{ambient 1 diffuse 1}}
     rotate -z*Qp
       translate x_target*x +y_target*y-z
}

//now scatter that target electron
object{ electron translate x_target*x+y_target*y+vrotate(.5*c*max(0,lclock-3)*x,2*Qp*z)}



#declare i_s=1;
union{
        #while(i_s<=4)
                torus{res[i_s],e_size/3
                        texture {pigment { color rgbt <.2,.2,.9,.5> } finish{ambient .40 diffuse .50}}
                        rotate x*90}
                #declare i_e=1;
                #while(i_e<=nes[i_s])
                        #declare edraw=1;
                        #if(i_s=i_target) #if(i_e=n_target)
                                #declare edraw=0;
                        #end #end
                        #if(edraw=1)
                                object{ electron translate res[i_s]*y rotate (i_e-1)/nes[i_s]*360*(-z)}
                        #end

                        #declare i_e=i_e+1;
                #end
                
                #declare i_s=i_s+1;
        #end
}

sphere{ 0,ecloud
    texture {pigment {color rgbf <1,1,1,1>}}
    hollow
    interior {
        media {
            emission color rgbt <.0,.0,.02,.9>
            scattering {2}
            intervals 3 samples 32
            method 3
           // aa_threshold 0.3 aa_level 5
            density {spherical scale ecloud*1.2}
        }
    }
}
/**/