                       // Persistence of Vision Ray Tracer Scene Description File
// File: ?.pov
// Vers: 3.5
// Desc: scence template
// Date: 10/06/04
// Auth: mrg
//

#version 3.5;

#include "colors.inc"
#include "rand.inc"
#include "math.inc"
#include "../../mrg_misc3.inc"
#include "golds.inc"
#include "metals.inc"
// some standard finishes
#include "finish.inc"




global_settings {
  assumed_gamma 1.0
  ambient_light color rgb <1,1,1> 
  max_trace_level 7
}
background { color rgb 0 }
#declare lclock=clock+0;


#declare cpos2=<1,2.0, -9.0>*1.6;
#declare cpos1=<11,5, -8.0>*1.05;

 
#declare T1=32.5;
//#declare cpos=cpos1*(1-trans(lclock-12))+cpos2*trans(lclock-12);
#declare cpos=vrotate(cpos1,-trans((lclock-T1)/2)*35*(.125*x-1.35*y));
camera {
  location cpos 
//  location  <33,0, 0>*3
  right     x*image_width/image_height
  look_at   3*z
  angle 40
}
/*
#declare T_off=.6;                   //amount of  current off time
#declare T_rot=12;                   //time for a full rotation = 1 cycle
#declare loopa=360/T_rot*lclock-90;  //rotation angle
#declare T_p5=T_rot/2;              //time for 1/2 rotation (=battery cycle)

#declare n_rot=int(lclock/T_rot);     //number of rotations
#declare t_int=lclock-n_rot*T_rot; // amount of time "into" the current rotation
#declare tofL= n_rot+T_p5*(ltrans((t_int-T_off/2)/(T_p5-T_off))-ltrans((t_int-T_off/2-T_p5)/(T_p5-T_off)));

#declare n_tp5=int(lclock/T_p5);     //number of elapsed half cycles
#declare tp5_int=lclock-n_tp5*T_p5; // amount of time "into" the current half cycle
#declare tofB= n_tp5+(T_p5-T_off)*ltrans((tp5_int-T_off/2)/(T_p5-T_off));
*/
#declare tc = lclock;
#declare Tp5=6;  //time for half rotation
#declare dT=.3;  //dead time per half cycle
//battery loop
#declare Nbc=int(tc/Tp5);  //number of half cycles  for the battery
#declare t_bon=Nbc*(Tp5-dT)+min(tc-Nbc*Tp5,Tp5-dT);
//rotor loop
#declare Nfc=int(.5*tc/Tp5);  //number of full cycles
#declare t_lon=tc-Nfc*2*Tp5;  // how far into the current cycle
#declare t_lon=max(0,min(t_lon,Tp5-dT))-max(0,min(t_lon-Tp5,Tp5-dT));



#declare cdraw=0;   //is the current on NOW?
#if((tc-Nbc*Tp5)<(Tp5-dT))
     #declare cdraw=1;
#end
     
#declare rot_phase=.5*dT/Tp5*180;     
#declare loopa=180/Tp5*tc-90+rot_phase;  //rotation angle     
     
#declare cylrad = .15;

//battery
union{
     difference{
          cylinder{-x,x,.5}
          cylinder{-1.2*x,1.2*x,.45}
           pigment{Blue*.05+Gray*.2}
     }
     union{
          cylinder{-.95*x,.95*x,.45 }
          cylinder{.95*x,1.10*x,.15 }
          texture{T_Chrome_3B}
     }
     intersection{
          union{
          box{-.15*x-.025*y,.15*x+.025*y-.5*z translate -.4*z+.8*x}
          box{-.15*y-.025*x,.15*y+.025*x-.5*z translate -.4*z+.8*x}
          box{-.15*x-.025*y,.15*x+.025*y-.5*z translate -.4*z-.8*x}
          /*  
               text {  ttf  "timesbd.ttf", "Duragizer", 2.5, 0 
                    scale .32
                    translate -.85*z -.7*x-.1*y  
               }
               text {  ttf  "symbol.ttf", "-", 2.5, 0 
                    scale .45
                    translate -.85*z -.95*x-.1*y  
               }
               text {  ttf  "symbol.ttf", "+", 2.5, 0 
                    scale .45
                    translate -.85*z +.75*x-.1*y  
               }
          */     
          }
          cylinder{-x,x,.501}
          pigment{Black}  no_shadow no_reflection

     }
     translate -2*y
}
#declare wire_texture=texture{ 
     pigment{color rgbt  <.7,.7,.7,.9>} 
     finish{Shiny}

};
#declare charge_texture=texture{
     pigment{color rgb <.75,.1,.0>}
     finish {Luminous}
};

//commutator radius
#declare ccro=.5; 
#declare ccri=.3;
#declare wire_radius=.08; 
//wires
merge{
     cylinder{-2.05*x,2.05*x,wire_radius translate -2*y}
     cylinder{-2.05*y,0.05*y,wire_radius translate 2*x }
     cylinder{-2.05*y,0.05*y,wire_radius translate -2*x }
     
     difference{
          cylinder{-2.05*x,2.05*x,wire_radius  }
          union{
               box{-.125*(z+y)-(ccro+.1)*x,+.125*(z+y)+(ccro+.1)*x }   //brushes
               cylinder{-z,z,ccro }
          }     
     }
          texture{wire_texture}
}

//battery path
#declare bpath = array[6];
#declare bpath[0]=-(ccro+.05)*x;
#declare bpath[1]=-2*x;
#declare bpath[2]=-2*x-2*y;
#declare bpath[3]=2*x-2*y;
#declare bpath[4]=2*x;
#declare bpath[5]=+(ccro+.05)*x;

//brushes
     difference{
          box{-wire_radius*(z+y)-(ccro+.15)*x,+wire_radius*(z+y)+(ccro+.15)*x }   //brushes
          cylinder{-z,z,ccro }
                    texture{T_Brass_4E}

     }
          
     //D-contacts
union{     
     difference{
          cylinder{-.125*z,.125*z,ccro }
          union{
               cylinder{-.3*z,.3*z,ccri }
               box{-.15*x-z-1.2*ccro*y,+.15*x+z+1.2*ccro*y }
          }
          texture{T_Brass_4E}
     } 


//loop
#declare loopl=5.0;
merge{
               cylinder{0,2.05*z,wire_radius translate x*(ccri+wire_radius)}
               cylinder{0,2.05*z,wire_radius translate -x*(ccri+wire_radius)} 
               cylinder{-x*(ccri+.5*wire_radius),-(loopl/2+.05)*x,wire_radius translate 2*z}
               cylinder{x*(ccri+.5*wire_radius),(loopl/2+.05)*x,wire_radius translate 2*z}
               cylinder{-.05*z,loopl*z,wire_radius translate 2*z+loopl/2*x}
               cylinder{-.05*z,loopl*z,wire_radius translate 2*z-loopl/2*x}
               cylinder{-(loopl/2+.05)*x,(loopl/2+.05)*x,wire_radius translate (loopl+2)*z}
               sphere{2*z,wire_radius  translate x*(ccri+wire_radius)}
               sphere{2*z,wire_radius  translate -x*(ccri+wire_radius)}
               texture{wire_texture}
     }
     rotate loopa*z
}
//splines for charge paths
#declare lpath = array[8];
#declare lpath[0]=vrotate(x*(ccri+wire_radius)+.05*z,loopa*z);
#declare lpath[1]=vrotate(x*(ccri+wire_radius)+2*z,loopa*z);
#declare lpath[2]=vrotate(2*z+loopl/2*x,loopa*z);
#declare lpath[3]=vrotate(2*z+loopl/2*x+loopl*z,loopa*z);
#declare lpath[4]=vrotate(2*z-loopl/2*x+loopl*z,loopa*z);
#declare lpath[5]=vrotate(2*z-loopl/2*x,loopa*z);
#declare lpath[6]=vrotate(-x*(ccri+wire_radius)+2*z,loopa*z);
#declare lpath[7]=vrotate(-x*(ccri+wire_radius)+.05*z,loopa*z);

#declare foo1 = function {
     spline {
          linear_spline
          0,bpath[0]
          #declare sc=1;
          #declare plb=0;  //plb is (accumulated) path distance along battery loop
          #while (sc<6)
               #declare plb=plb+vlength(bpath[sc]-bpath[sc-1]);
               plb,bpath[sc]
               #declare sc=sc+1;

          #end
     }
}

#declare foo2 = function {
     spline {
          linear_spline
          0,lpath[0]
          #declare sc=1;
          #declare pll=0; //pll is (accumulated) path distance along rotating loop
          #while (sc<8)
               #declare pll=pll+vlength(lpath[sc]-lpath[sc-1]);
               pll,lpath[sc]
               #declare sc=sc+1;

          #end
     }
} 
#declare ds=.25;  //charge spacing
#declare vc=.5;   //velocity of charges

#declare cposs=0;
#while (cposs<plb)
     #declare thispos=mod(cposs+vc*t_bon,plb);
     //#declare thispos=thispos-int(thispos/plb)*plb;
     sphere{foo1(thispos),.055 
          texture{charge_texture}
          no_shadow no_reflection
     }

     #declare cposs=cposs+ds;
#end
#declare cposs=0;
#while (cposs<pll)
     #declare thispos=mod(cposs+vc*t_lon,pll);
     //#declare thispos=thispos-int(thispos/pll)*pll;
     sphere{foo2(thispos),.055 
          texture{charge_texture}
          no_shadow no_reflection
     }
     #declare cposs=cposs+ds;
#end
//things to draw while the current is on
#if(cdraw>.5)  

     //draw the current
     #declare testc=0;
     #while(testc<5)
          cylinder{ bpath[testc], bpath[testc+1],.02 pigment{color rgb <.6,.6,.6>}}
     
          #declare testc=testc+1;
     #end
     
     #declare testc=0;
     #while(testc<7)
          cylinder{ lpath[testc], lpath[testc+1],.02 pigment{color rgb <.6,.6,.6>}}
     
          #declare testc=testc+1;
     #end
     //draw the force markers
     //Make_Vector(vec,head_length,post_diameter,head_diameter)
     #declare testa=loopa-int(loopa/360)*360;
     #declare idir=-1;
     #if (abs(testa-180)<90)
          #declare idir=+1;
     #end     
     #declare hl=.3;
     #declare pd =hl/5;
     #declare hd= pd*2;
     #declare fmax=1.5;
     #declare side_F=fmax*sin(radians(loopa))*idir;
     object{Make_Vector(side_F*z,hl,pd,hd) pigment{color Orange} translate (2+loopl)*z}
     object{Make_Vector(-side_F*z,hl,pd,hd) pigment{color Orange} translate (2)*z}
     object{Make_Vector(-idir*fmax*y,hl,pd,hd) pigment{color Orange} translate (2+loopl/2)*z+vrotate(loopl/2*x,testa*z)}
     object{Make_Vector(idir*fmax*y,hl,pd,hd) pigment{color Orange} translate (2+loopl/2)*z+vrotate(-loopl/2*x,testa*z)}
     object{orient_text(-idir*(fmax+.3)*y+(2+loopl/2)*z+vrotate(loopl/2*x,testa*z),cpos,"F",.6)
          pigment{color Orange}
               finish{ambient .5 diffuse 0}
          
     } 
     object{orient_text(idir*(fmax+.3)*y+(2+loopl/2)*z+vrotate(-loopl/2*x,testa*z),cpos,"F",.6)
          pigment{color Orange}
               finish{ambient .5 diffuse 0}
          
     } 
     
     
#end
//bfield
#declare bline1=merge{
     cylinder{-20*x,20*x,.03 }
     cone{-.35*x,0,0,.15 translate 4*x}
     cone{-.35*x,0,0,.15 translate -4*x}
     pigment{color rgbt Blue*1.4+<.3,.3,.3,.7>}
          finish{ambient .5 diffuse 0}  no_shadow no_reflection
};
#declare dbs=1.75;
#declare nb_side=1;
#declare nbcz=-nb_side;
#while (nbcz<=nb_side)
     #declare nbcy=-nb_side;
     #while (nbcy<=nb_side)
          object{bline1 translate (nbcz*z+nbcy*y)*dbs+(2+loopl/2)*z}
          #declare nbcy=nbcy+1;
     #end
     #declare nbcz=nbcz+1;
#end
object{orient_text((2+loopl/2)*z+dbs*y-4*x+.6*y,cpos,"B",.6)
     pigment{color rgbt Blue*1.4+<.3,.3,.3,.0>}
          finish{ambient .5 diffuse 0}
     
} 
light_source {
  <0, 0, 0>            // light's position (translated below)
  color rgb <1, 1, 1>  // light's color
  translate <30, 30, -30>//place it to the left (neg x), above (pos y) and behind (neg z) vantage point
}
/**/
// ----------------------------------------


