
/*
This file has been "tuned" for the loop of charge configuration
in terms of placing the labels

Antialias=on

Antialias_Threshold=0.3
Antialias_Depth=3
Input_File_Name=efield_builder_loop.pov

Initial_Frame=1
Final_Frame=120
Initial_Clock=0
Final_Clock=1

Cyclic_Animation=off
Pause_when_Done=off

+FC6
*/
  global_settings {
  assumed_gamma 1
  ambient_light rgb <1,1,1> 
  }
#declare ltime =2*clock+.3;  //twice along, once to show geometry, once to add up for integral
#declare cpos=<-7, 3 , -8>*2.0;
//field point
#declare fp = <0,0,-2.0>;

//      Declare parameterized line of charge here for 0<s<1  //
/*line of charge
#declare l_x = function(s) { 0 }
#declare l_y = function(s) { -1+2*s }
#declare l_z = function(s) { 0 }
*/
/*ring of charge*/
#declare l_x = function(s) { cos(2*pi*s) }
#declare l_y = function(s) { sin(2*pi*s) }
#declare l_z = function(s) { 0 }


#declare l_thick = .05;        //thickness of line
#declare ds=1.0/60.;            //length of section per add
#declare nds=4;                 //number of subsections (closer numerical integral/smoothness of curve
#declare dds=ds/nds;

#if(ltime< 1.0)  //illustrate piece ds at s=t
        #declare s1=ltime;
        #declare p1=<l_x(s1), l_y(s1), l_z(s1)>;
        #declare counter=0;
        union{
                #while(counter<nds)           
                        #declare s2 = s1+dds;
                        #declare p2=<l_x(s2), l_y(s2), l_z(s2)>;
                        cylinder { p1, p2,l_thick}     
                        #declare p1=p2;
                        #declare s1=s2;
                        #declare counter=counter+1;                
                #end
        texture {pigment { color rgbt <.8,.0,.0,0> } finish{ambient 1 diffuse 1}}}
        #declare rc=<l_x(ltime+ds/2.0), l_y(ltime+ds/2.0), l_z(ltime+ds/2.0)>;
          #declare tpos=1.4*rc;
          #declare txt="dq";
          #declare tcrr=cpos-tpos;
          #declare talt=atan2(tcrr.y,vlength(tcrr-y*tcrr.y));
          #declare taz=atan2(tcrr.x,-tcrr.z);
          #declare tobj=
          text {
            ttf             
            "times.ttf", txt,.01,0 
               scale <1,1,.1>*.3
          };
          #declare obj_box=max_extent(tobj)-min_extent(tobj);
          object{tobj
               translate -(obj_box.x*x+obj_box.y*y)/2  //center of text at origin,
                                                       //rather than lower left hand corener      
               rotate x*degrees(talt)                  //tilt up/down
               rotate -y*degrees(taz)                  //face the camera
               translate tpos                          //final resting place
               texture {pigment { color rgbt <.8,.0,.0,0> } finish{ambient 1 diffuse 1}}
          }

        //dq to  field point line
        cylinder{rc, fp,l_thick/3   
                texture {pigment { color rgbt <.6,.6,.6,.7> } finish{ambient 1 diffuse 1}}}
          #declare tpos=(.7*rc+.5*fp);
          #declare txt="r";
          #declare tcrr=cpos-tpos;
          #declare talt=atan2(tcrr.y,vlength(tcrr-y*tcrr.y));
          #declare taz=atan2(tcrr.x,-tcrr.z);
          #declare tobj=
          text {
            ttf             
            "times.ttf", txt,.01,0 
               scale <1,1,.1>*.3
          };
          #declare obj_box=max_extent(tobj)-min_extent(tobj);
          object{tobj
               translate -(obj_box.x*x+obj_box.y*y)/2  //center of text at origin,
                                                       //rather than lower left hand corener      
               rotate x*degrees(talt)                  //tilt up/down
               rotate -y*degrees(taz)                  //face the camera
               translate tpos                          //final resting place
               texture {pigment { color rgbt <.6,.6,.6,0> } finish{ambient 1 diffuse 1}}
          }

        //dE vector
        #declare dr=vlength(fp-rc);
        #declare E_v=(fp-rc)/(dr*dr*dr)*3;
        cylinder{fp, fp+.7*E_v,l_thick/3   
                texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}}
        cone{fp+.7*E_v,l_thick, fp+E_v,0   
                texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}}
          
          #declare tpos=-2.5*z-.25*y;
          #declare txt="dE";
          #declare tcrr=cpos-tpos;
          #declare talt=atan2(tcrr.y,vlength(tcrr-y*tcrr.y));
          #declare taz=atan2(tcrr.x,-tcrr.z);
          #declare tobj=
          text {
            ttf             
            "times.ttf", txt,.01,0 
               scale <1,1,.1>*.3
          };
          #declare obj_box=max_extent(tobj)-min_extent(tobj);
          object{tobj
               translate -(obj_box.x*x+obj_box.y*y)/2  //center of text at origin,
                                                       //rather than lower left hand corener      
               rotate x*degrees(talt)                  //tilt up/down
               rotate -y*degrees(taz)                  //face the camera
               translate tpos                          //final resting place
               texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}
          }

        
        
        // hint of total distribution
        #declare s1=0;
        #declare p1=<l_x(s1), l_y(s1), l_z(s1)>;

        union{
                #while(s1<=1)           
                        #declare s2 = s1+dds;
                        #declare p2=<l_x(s2), l_y(s2), l_z(s2)>;
                        cylinder { p1, p2,l_thick}     
                        #declare p1=p2;
                        #declare s1=s2;
                        //sneak in a little numerical integration here
                        #declare rc=<l_x(s1), l_y(s1), l_z(s1)>;
                        #declare dr=vlength(fp-rc);
                        #declare E_v=E_v+(fp-rc)/(dr*dr*dr)*.1;
                #end
                texture {pigment { color rgbt <.5,.0,.0,.95> } finish{ambient 1 diffuse 1}}}

#else
        #declare lt2=ltime-1.0;
        #declare s1=0;
        #declare p1=<l_x(s1), l_y(s1), l_z(s1)>;
        #declare E_v = <0,0,0>;
        union{
                #while(s1<=lt2)           
                        #declare s2 = s1+dds;
                        #declare p2=<l_x(s2), l_y(s2), l_z(s2)>;
                        cylinder { p1, p2,l_thick}     
                        #declare p1=p2;
                        #declare s1=s2;
                        //sneak in a little numerical integration here
                        #declare rc=<l_x(s1), l_y(s1), l_z(s1)>;
                        #declare dr=vlength(fp-rc);
                        #declare E_v=E_v+(fp-rc)/(dr*dr*dr)*.08;
                #end
          texture {pigment { color rgbt <.8,.0,.0,0> } finish{ambient 1 diffuse 1}}
        }
         cylinder{fp, fp+.9*E_v,l_thick/3   
                 texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}}
         cone{fp+.9*E_v,l_thick, fp+E_v,0   
                 texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}}

               #declare tpos=<0,.25,fp.z+E_v.z-.25>;
          #declare txt="Enet";
          #declare tcrr=cpos-tpos;
          #declare talt=atan2(tcrr.y,vlength(tcrr-y*tcrr.y));
          #declare taz=atan2(tcrr.x,-tcrr.z);
          #declare tobj=
          text {
            ttf             
            "times.ttf", txt,.01,0 
               scale <1,1,.1>*.3
          };
          #declare obj_box=max_extent(tobj)-min_extent(tobj);
          object{tobj
               translate -(obj_box.x*x+obj_box.y*y)/2  //center of text at origin,
                                                       //rather than lower left hand corener      
               rotate x*degrees(talt)                  //tilt up/down
               rotate -y*degrees(taz)                  //face the camera
               translate tpos                          //final resting place
               texture {pigment { color rgbt <.0,.0,.9,0> } finish{ambient 1 diffuse 1}}
          }

#end


#declare camera_scale=10; 
camera {
    location cpos
    look_at  fp
    angle 20
}

//axis
cylinder { -2*x, 2*x,l_thick/3 texture {pigment { color rgbt <.1,.1,.1,.5> } finish{ambient 1 diffuse 1}}}     
cylinder { -2*y, 2*y,l_thick/3 texture {pigment { color rgbt <.1,.1,.1,.5> } finish{ambient 1 diffuse 1}}}     
cylinder { z, -4*z,l_thick/3 texture {pigment { color rgbt <.1,.1,.1,.5> } finish{ambient 1 diffuse 1}}}     

//field point

sphere{fp,l_thick texture {pigment { color rgbt <.9,.9,.9,.0> } finish{ambient 1 diffuse 1}}}