//general vector drawing macro
#macro Make_Vector(vec,head_length,post_diameter,head_diameter)
  #local vec_length = vlength(vec);
  #if(vec_length>head_length)   //draw entire vector 
    #local vec_post = (vec_length-head_length)/vec_length*vec;
    union{
      cylinder{0,vec_post,post_diameter}
      cone{vec_post,head_diameter,vec,0}
    }
  #else                         //vec too small, draw cone tip for vec
    #local this_diam=vec_length*head_diameter/head_length;
    cone{0,this_diam,vec,0}
  #end
#end

// a "smoothly turning on" function
//(0 off to 1 on transitions)
//tt is "current time"
//tt0 is turn on time start
//dtt is turn on time duration
#declare transitor=function(tt,tt0,dtt) { .5*(1-cos(min(max(tt-tt0,0),dtt)/dtt*pi))}

#declare trans = function(tt) { (1-cos(pi*max(min(tt,1),0)))/2 }  //smoothed transition
#declare ltrans=function(tt) { max(min(tt,1),0) }                 //linear transition 


//
//#macro alt_azimuth(alt1,alt2,az1,az2)
//kinda like shape.inc's wedge, but only goes to radius 1
//3-D 
#macro alt_azimuth(alt1,alt2,az1,az2)
     #include "shapes.inc"  //watch those dependencies
     #local new_obj=box{-1,1};
     //look, ma, no error checking!
     //-90<=alt1<alt2<-90
     //0<=az1<az2<=360
     #if(alt1>-90.0)
          #if(alt1<0)
               #local new_obj=difference{
                    object{new_obj}
                    cone{0,0,-y,1/tan(radians(abs(alt1)))}
               };
          #else
               #if(alt1>0)
                #local new_obj=intersection{
                    object{new_obj}
                    cone{0,0,y,1/tan(radians(abs(alt1)))}
               };
               #else  //alt1 =  0, all above yz plane
                #local new_obj=intersection{
                    object{new_obj}
                    box{-x-z,1}
               };
               #end
          #end
     #end
     #if(alt2<90)     
          #if(alt2<0)
                #local new_obj=intersection{
                    object{new_obj}
                    cone{0,0,-y,1/tan(radians(abs(alt2)))}
               };
          #else
               #if(alt2>0)
                #local new_obj=difference{
                    object{new_obj}
                    cone{0,0,y,1/tan(radians(abs(alt2)))}
               };
               #else  //alt2=0, all below yz plane
                #local new_obj=intersection{
                    object{new_obj}
                    box{+x+z,-1}
               };
               #end
          #end   
     #end     
     intersection{
          object{new_obj}
          object{Wedge(az2-az1) rotate y*az1}
          bounded_by{ box{-1,1}}
     }          
#end

//reorient text to face the camera @cpos after scaling, placing text at tpos
#macro orient_text(tpos,cpos,txt,tscale)
  #local tcrr=cpos-tpos;
  #local talt=atan2(tcrr.y,vlength(tcrr-y*tcrr.y));
  #local taz=atan2(tcrr.x,-tcrr.z);
  #local tobj=
     text {
       ttf             
       "times.ttf", txt,.01,0 
          scale tscale
     };
  #local 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
     }
#end


//reorient a flat (originally in x-y plane) object to face the camera @cpos, placing object at opos
#macro orient_object(da_obj,opos,cpos)
  #local ocrr=cpos-opos;
  #local oalt=atan2(ocrr.y,vlength(ocrr-y*ocrr.y));
  #local oaz=atan2(ocrr.x,-ocrr.z);
     object{da_obj
          rotate x*degrees(oalt)                  //tilt up/down
          rotate -y*degrees(oaz)                  //face the camera
          translate opos                          //NOW to final resting place
     }
#end
