// PoVRay 3.7 Scene File " ... .pov"
// author:  ...
// date:    ...
//--------------------------------------------------------------------------
#version 3.7;
global_settings{ assumed_gamma 1.0 }
//--------------------------------------------------------------------------
#include "colors.inc"
#include "textures.inc"
#include "glass.inc"
#include "metals.inc"
#include "golds.inc"
#include "stones.inc"
#include "woods.inc"
#include "shapes.inc"
#include "shapes2.inc"
#include "functions.inc"
#include "math.inc"
#include "transforms.inc"
#include "mrg_misc3.inc"
#include "platonic_solids.inc"

#declare lclock=clock+0;

//move test charge around  1-6,6-11 
    //rotate in screen plane with varying distance then corkscrew down

#declare test_r=vrotate(2.0*x, 370*trans((lclock-1)/5)*z); 
#declare test_r=vrotate(test_r, 360*trans((lclock-6)/5)*y); 

//fade in efield marker array 11-13
#declare t2F=trans((lclock-11)/2);


// rotate perspective  13-21

#declare cpos= vrotate (<.5, 2+2*sin(pi*trans((lclock-13)/8)), 7>, 180*trans((lclock-13)/8)*y);


//fade in efield lines phade out efied markers 21-23
#declare t4F=trans((lclock-21)/2); 

//and rotate perspective   23-31

#declare cpos= vrotate (cpos, 180*trans((lclock-23)/8)*y)-2*sin(pi*trans((lclock-23)/8))*y;








// perspective (default, not required) camera
camera {
  perspective
  location  cpos
  look_at   <0, 0,  0>
  right     -x*image_width/image_height  // aspect &RH coords
  // direction z                        // direction and zoom
  // angle 67                           // field (overides direction zoom)
}
// create a regular point light source
light_source {
  0*x                  // light's position (translated below)
  color rgb <1,1,1>    // light's color
  translate 5*<-20, 40, 20>
}


#declare p_texture= texture {pigment{color rgb <.7,.0,.0>} finish{Shiny}} ;
#declare test_texture= texture {pigment{color rgb .5*<1,.025,.10>} finish{Shiny}} ;
#declare n_texture= texture {pigment{color rgb <.0,.0,.7>} finish{Shiny}} ;
#declare Efieldlinetexture=texture{pigment{color rgb <1,.15,.1>} finish{Shiny}}; 
#declare Bfieldlinetexture=texture{pigment{color rgb <.1,.1,1>} finish{Luminous}}; 
#declare charge_radius=.12;
#declare test_charge_radius=.4*charge_radius;
 
//dipole
#declare q_dipole=.3;       // k=1
#declare l_dipole=1;
#declare nq=3;
#declare qq = array[nq];     //
#declare pq = array[nq];
#declare q1=.3; 

#declare pq[0]=<0,-.5*l_dipole,0>; 
#declare qq[0]=-2*q1;     //
#declare pq[1]=<l_dipole,l_dipole*tan(radians(52.2)),0>; 
#declare qq[1]=q1;     //
#declare pq[2]=<-l_dipole,l_dipole*tan(radians(52.2)),0>; 
#declare qq[2]=q1;     //
//charge
sphere { pq[1], charge_radius texture{p_texture}}   
sphere { pq[2], charge_radius texture{p_texture}}   
sphere { pq[0], 2*charge_radius texture{n_texture}}   

/**/



#declare E_field_out=<1,1,1>;
#macro E_field(_fieldpoint)
    #local _ic=0;
    #declare E_field_out=E_field_out*0;
    #while(_ic<nq) 
        #declare E_field_out=E_field_out+ qq[_ic]*(_fieldpoint-pq[_ic])/pow(vlength(_fieldpoint-pq[_ic]),3);
        #declare _ic=_ic+1;
    #end
   E_field_out     //return value
#end
 
 

//Make_Vector(vec,head_length,post_diameter,head_diameter)

#declare post_diameter=.2*test_charge_radius;
#declare head_diameter=3*post_diameter;
#declare head_length=3*head_diameter;



//testcharge
#declare E=E_field(test_r);
sphere { <0,0,0>, test_charge_radius texture{test_texture} translate test_r}  // end of sphere ----------------------------------- 
object{Make_Vector(E,head_length,post_diameter,head_diameter) translate test_r texture{Efieldlinetexture}}    

// array of field vectors
#if((t2F>0)&(t4F<1))
    #declare ds=.75;
    #declare smax=3 ;
    
    #declare snowx=-smax;
    #while (snowx<= smax)
        #declare snowy=-smax;
        #while (snowy<= smax)
            #declare snowz=-smax;
            #while (snowz<= smax)
                #declare snow=<snowx,snowy,snowz>;
                #if( (vlength(snow-pq[0])>ds)&(vlength(snow-pq[1])>ds)&(vlength(snow-pq[2])>ds))
                    #declare E=E_field(snow);
                    #declare Etexturefadein=texture{pigment{color rgbt <1,.15,.1,1-t2F+t4F>} finish{Shiny}}; 
    
                    object{Make_Vector(E,head_length,post_diameter,head_diameter) translate snow texture{Etexturefadein}}    
                    
                
                #end
                #declare snowz=snowz+ds;
            #end
            #declare snowy=snowy+ds;
        #end
        #declare snowx=snowx+ds;
    #end
#end

//field lines
#if(t4F>0)
    #declare Etexturefadein=texture{pigment{color rgbt <1,.15,.1,1-t4F>} finish{Shiny}}; 
    #macro draw_field_line(_fieldpoint)
        #local ds_fl=.01;
        #local s_fieldline=0;
        union{
        #while((vlength(s_fieldline-pq[0])>ds_fl)&(s_fieldline<14))
            #local Etmp=E_field(fieldline_point);
            #local new_fieldline_point=fieldline_point+ds_fl*Etmp/vlength(Etmp);
            sphere{fieldline_point,post_diameter }
            cylinder{fieldline_point,new_fieldline_point,post_diameter texture{Etexturefadein}}
            #if((abs(fieldline_point.y)<.5*ds_fl))
               object{Make_Vector(Etmp/vlength(Etmp)*head_length*1.01,head_length,post_diameter,head_diameter) translate fieldline_point} 
            #end
            #if((abs(iv-4.5)<.55)&(abs(s_fieldline-2)<.5*ds))
               object{Make_Vector(Etmp/vlength(Etmp)*head_length*1.01,head_length,post_diameter,head_diameter) texture{Etexturefadein} translate fieldline_point} /*  */
            #end
             
            #local fieldline_point=new_fieldline_point;
            #local s_fieldline=s_fieldline+ds_fl;
        #end
        }
    #end

    #declare ds=.25*charge_radius;
    #declare iv=0;
    #while(iv<20)
        #declare fieldline_point=pq[1]+ds*PDvertex[iv];
        object{ draw_field_line(fieldline_point)  texture{Etexturefadein}}
        #declare fieldline_point=pq[2]+ds*PDvertex[iv];
        object{ draw_field_line(fieldline_point)  texture{Etexturefadein}}
        #declare iv=iv+1;
    #end    




    #declare iv=4;
    #while(iv<=5)
        #declare sp=PDvertex[iv];    
        #declare fieldline_point=pq[0]+ds*(sp-2*sp.y*y);
        #declare s_fieldline=0;
        #while((s_fieldline<3)&(vlength(fieldline_point-pq[0])>ds))
            #declare E=E_field(fieldline_point);
            #declare new_fieldline_point=fieldline_point-ds*E/vlength(E);
            #declare Etexturefadein=texture{pigment{color rgbt <1,.15,.1,1-t4F>} finish{Shiny}}; 
            //#declare Etexturefadein=texture{pigment{color rgbt <1,.15,.1,0>} finish{Shiny}}; 
            
        
            sphere{fieldline_point,post_diameter texture{Etexturefadein}}
            cylinder{fieldline_point,new_fieldline_point,post_diameter texture{Etexturefadein}}
            
            #if((abs(fieldline_point.y)<.5*ds)&(vlength(E)>0))
               object{Make_Vector(E/vlength(E)*head_length*1.01,head_length,post_diameter,head_diameter) texture{Etexturefadein} translate fieldline_point} /*  */
            #end
            #if((abs(iv-4.5)<.55)&(abs(s_fieldline-1)<.5*ds))
               object{Make_Vector(E/vlength(E)*head_length*1.01,head_length,post_diameter,head_diameter) texture{Etexturefadein} translate fieldline_point} /*  */
            #end
             
            #declare fieldline_point=new_fieldline_point;
            #declare s_fieldline=s_fieldline+ds;
        #end
        #declare iv=iv+1;
    #end 



    
#end  
    
    
    
    