"Fossies" - the Fresh Open Source Software Archive

Member "asymptote-2.60/base/plain_prethree.asy" (6 Nov 2019, 6285 Bytes) of package /linux/misc/asymptote-2.60.src.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "plain_prethree.asy": 2.53_vs_2.54.

    1 // Critical definitions for transform3 needed by projection and picture.
    2 
    3 pair viewportmargin=settings.viewportmargin;
    4 
    5 typedef real[][] transform3;
    6 restricted transform3 identity4=identity(4);
    7 
    8 // A uniform 3D scaling.
    9 transform3 scale3(real s)
   10 {
   11   transform3 t=identity(4);
   12   t[0][0]=t[1][1]=t[2][2]=s;
   13   return t;
   14 }
   15 
   16 // Simultaneous 3D scalings in the x, y, and z directions.
   17 transform3 scale(real x, real y, real z)
   18 {
   19   transform3 t=identity(4);
   20   t[0][0]=x;
   21   t[1][1]=y;
   22   t[2][2]=z;
   23   return t;
   24 }
   25 
   26 transform3 shiftless(transform3 t)
   27 {
   28   transform3 T=copy(t);
   29   T[0][3]=T[1][3]=T[2][3]=0;
   30   return T;
   31 }
   32 
   33 real camerafactor=2;       // Factor used for camera adjustment.
   34 
   35 struct transformation {
   36   transform3 modelview;  // For orientation and positioning
   37   transform3 projection; // For 3D to 2D projection
   38   bool infinity;
   39   void operator init(transform3 modelview) {
   40     this.modelview=modelview;
   41     this.projection=identity4;
   42     infinity=true;
   43   }
   44   void operator init(transform3 modelview, transform3 projection) {
   45     this.modelview=modelview;
   46     this.projection=projection;
   47     infinity=false;
   48   }
   49   transform3 compute() {
   50     return infinity ? modelview : projection*modelview;
   51   }
   52   transformation copy() {
   53     transformation T=new transformation;
   54     T.modelview=copy(modelview);
   55     T.projection=copy(projection);
   56     T.infinity=infinity;
   57     return T;
   58   }
   59 }
   60 
   61 struct projection {
   62   transform3 t;         // projection*modelview (cached)
   63   bool infinity;
   64   bool absolute=false;
   65   triple camera;        // Position of camera.
   66   triple up;            // A vector that should be projected to direction (0,1).
   67   triple target;        // Point where camera is looking at.
   68   triple normal;        // Normal vector from target to projection plane.
   69   pair viewportshift;   // Fractional viewport shift.
   70   real zoom=1;          // Zoom factor.
   71   real angle;           // Lens angle (for perspective projection).
   72   bool showtarget=true; // Expand bounding volume to include target?
   73   typedef transformation projector(triple camera, triple up, triple target);
   74   projector projector;
   75   bool autoadjust=true; // Adjust camera to lie outside bounding volume?
   76   bool center=false;    // Center target within bounding volume?
   77   int ninterpolate;     // Used for projecting nurbs to 2D Bezier curves.
   78   bool bboxonly=true;   // Typeset label bounding box only.
   79   
   80   transformation T;
   81 
   82   void calculate() {
   83     T=projector(camera,up,target);
   84     t=T.compute();
   85     infinity=T.infinity;
   86     ninterpolate=infinity ? 1 : 16;
   87   }
   88 
   89   triple vector() {
   90     return camera-target;
   91   }
   92 
   93   void operator init(triple camera, triple up=(0,0,1), triple target=(0,0,0),
   94                      triple normal=camera-target,
   95                      real zoom=1, real angle=0, pair viewportshift=0,
   96                      bool showtarget=true, bool autoadjust=true,
   97                      bool center=false, projector projector) {
   98     this.camera=camera;
   99     this.up=up;
  100     this.target=target;
  101     this.normal=normal;
  102     this.zoom=zoom;
  103     this.angle=angle;
  104     this.viewportshift=viewportshift;
  105     this.showtarget=showtarget;
  106     this.autoadjust=autoadjust;
  107     this.center=center;
  108     this.projector=projector;
  109     calculate();
  110   }
  111 
  112   projection copy() {
  113     projection P=new projection;
  114     P.t=t;
  115     P.infinity=infinity;
  116     P.absolute=absolute;
  117     P.camera=camera;
  118     P.up=up;
  119     P.target=target;
  120     P.normal=normal;
  121     P.zoom=zoom;
  122     P.angle=angle;
  123     P.viewportshift=viewportshift;
  124     P.showtarget=showtarget;
  125     P.autoadjust=autoadjust;
  126     P.center=center;
  127     P.projector=projector;
  128     P.ninterpolate=ninterpolate;
  129     P.bboxonly=bboxonly;
  130     P.T=T.copy();
  131     return P;
  132   }
  133 
  134   // Return the maximum distance of box(m,M) from target.
  135   real distance(triple m, triple M) {
  136     triple[] c={m,(m.x,m.y,M.z),(m.x,M.y,m.z),(m.x,M.y,M.z),
  137                 (M.x,m.y,m.z),(M.x,m.y,M.z),(M.x,M.y,m.z),M};
  138     return max(abs(c-target));
  139   }
  140    
  141   
  142   // This is redefined here to make projection as self-contained as possible.
  143   static private real sqrtEpsilon = sqrt(realEpsilon);
  144 
  145   // Move the camera so that the box(m,M) rotated about target will always
  146   // lie in front of the clipping plane.
  147   bool adjust(triple m, triple M) {
  148     triple v=camera-target;
  149     real d=distance(m,M);
  150     static real lambda=camerafactor*(1-sqrtEpsilon);
  151     if(lambda*d >= abs(v)) {
  152       camera=target+camerafactor*d*unit(v);
  153       calculate();
  154       return true;
  155     }
  156     return false;
  157   }
  158 }
  159 
  160 projection currentprojection;
  161 
  162 struct light {
  163   real[][] diffuse;
  164   real[][] specular;
  165   pen background=nullpen; // Background color of the 3D canvas.
  166   real specularfactor;
  167   triple[] position; // Only directional lights are currently implemented.
  168 
  169   transform3 T=identity(4); // Transform to apply to normal vectors.
  170 
  171   bool on() {return position.length > 0;}
  172   
  173   void operator init(pen[] diffuse,
  174                      pen[] specular=diffuse, pen background=nullpen,
  175                      real specularfactor=1,
  176                      triple[] position) {
  177     int n=diffuse.length;
  178     assert(specular.length == n && position.length == n);
  179     
  180     this.diffuse=new real[n][];
  181     this.specular=new real[n][];
  182     this.background=background;
  183     this.position=new triple[n];
  184     for(int i=0; i < position.length; ++i) {
  185       this.diffuse[i]=rgba(diffuse[i]);
  186       this.specular[i]=rgba(specular[i]);
  187       this.position[i]=unit(position[i]);
  188     }
  189     this.specularfactor=specularfactor;
  190   }
  191 
  192   void operator init(pen diffuse=white, pen specular=diffuse,
  193                      pen background=nullpen, real specularfactor=1 ...triple[] position) {
  194     int n=position.length;
  195     operator init(array(n,diffuse),array(n,specular),
  196                   background,specularfactor,position);
  197   }
  198 
  199   void operator init(pen diffuse=white, pen specular=diffuse,
  200                      pen background=nullpen, real x, real y, real z) {
  201     operator init(diffuse,specular,background,(x,y,z));
  202   }
  203 
  204   void operator init(explicit light light) {
  205     diffuse=copy(light.diffuse);
  206     specular=copy(light.specular);
  207     background=light.background;
  208     specularfactor=light.specularfactor;
  209     position=copy(light.position);
  210   }
  211 
  212   real[] background() {return rgba(background == nullpen ? white : background);}
  213 }
  214 
  215 light currentlight;
  216