home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / PLOTXYZ.DI$ / VIEWMTX.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  3.4 KB  |  107 lines

  1. function a=viewmtx(az,el,phi,target)
  2. %VIEWMTX Generate view transformation matrices.
  3. %    A=VIEWMTX(AZ,EL) returns the 4x4 orthographic transformation 
  4. %    matrix, A, used to project 3-D vectors onto the 2-D plot surface. 
  5. %    Uses the same definition for azumith and elevation as VIEW, in
  6. %    particular, AZ and EL must be in degrees.  Returns the same 
  7. %    transformation matrix as the commands
  8. %        VIEW(AZ,EL)
  9. %        A = VIEW
  10. %    but doesn't change the current VIEW.
  11. %
  12. %    A=VIEWMTX(AZ,EL,PHI) returns a 4x4 perspective transformation
  13. %    matrix used to project 3-D vectors onto the 2-D plot surface.
  14. %    PHI is the subtended view angle of the normalized plot cube 
  15. %    (in degrees) and controls the amount of perspective distortion:
  16. %        PHI =  0 degrees is orthographic projection
  17. %        PHI = 10 degrees is like a telephoto lens
  18. %        PHI = 25 degrees is like a normal lens
  19. %        PHI = 60 degrees is like a wide angle lens
  20. %    The matrix A can be used to set the view transformation using
  21. %    VIEW(A).
  22. %
  23. %    A=VIEWMTX(AZ,EL,PHI,XC) returns the perspective transformation
  24. %    matrix using XC as the target (or look-at) point within
  25. %    the normalized plot cube.  XC=[xc,yc,zc] specifies the 
  26. %    point (xc,yc,zc) in the plot cube.  The default value is
  27. %    the closest point in the plot cube,
  28. %
  29. %    XC = 0.5+sqrt(3)/2*[cos(EL)*sin(AZ),-cos(EL)*cos(AZ),sin(EL)].
  30. %
  31. %    See also VIEW.
  32.  
  33. %    Clay M. Thompson 5-1-91
  34. %    Revised 12-17-91, 3-10-92 by CMT
  35. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  36.  
  37. error(nargchk(2,4,nargin));
  38.  
  39. if nargin==2, phi = 0; end
  40. if nargin>2,
  41.    if phi>0,  d = sqrt(2)/2/tan(phi*pi/360); else, phi = 0; end
  42. end
  43.  
  44. % Make sure data is in the correct range.
  45. el = rem(rem(el+180,360)+360,360)-180; % Make sure -180 <= el <= 180
  46. if el>90,
  47.   el = 180-el;
  48.   az = az + 180;
  49. elseif el<-90,
  50.   el = -180-el;
  51.   az = az + 180;
  52. end
  53. az = rem(rem(az,360)+360,360); % Make sure 0 <= az <= 360
  54.  
  55. % Convert from degrees to radians.
  56. az = az*pi/180;
  57. el = el*pi/180;
  58.  
  59. if nargin>3,
  60.   target = target(:);    % Make sure its a vector.
  61.   if length(target)~=3
  62.     error('XC must be a 3-vector of [x,y,z] values.');
  63.   end
  64. else
  65.   target = 0.5 + sqrt(3)/2*[cos(el)*sin(az);-cos(el)*cos(az);sin(el)];
  66. end
  67.  
  68. % View transformation matrix:
  69. % Formed by composing two rotations:
  70. %   1) Rotate about the z axis -AZ radians
  71. %   2) Rotate about the x axis (EL-pi/2) radians
  72.  
  73. T = [ cos(az)           sin(az)           0       0
  74.      -sin(el)*sin(az)   sin(el)*cos(az)   cos(el) 0
  75.       cos(el)*sin(az)  -cos(el)*cos(az)   sin(el) 0
  76.       0                 0                 0       1 ];
  77.  
  78. if nargin==2 | phi==0, a = T; return, end % Return orthographic transformation.
  79.  
  80. f = d;    % Default focal length.
  81.  
  82. % Transformation to move origin of object coordinate system to TARGET
  83. O1 = [eye(4,3),T*[-target;1]];
  84.  
  85. % Perspective transformation
  86. P = [1 0 0 0;
  87.      0 1 0 0;
  88.      0 0 1 0;
  89.      0 0 -1/f d/f];
  90.  
  91. % The perspective transformation above works because the graphics
  92. % system divides by the homogenous length, w, before mapping to the screen.
  93. % If the homegeous vector is given by V = [x,y,z,w] then the transformed
  94. % point is U = [x/w y/w].
  95.  
  96. % Using f = d places the image plane through the origin of the object 
  97. % coordinate system, thus projecting the object onto this plane.  Note only
  98. % the x and y coordinates are used to draw the object in the graphics window.
  99.  
  100. % Form total transformation
  101. a=P*O1*T;
  102.  
  103.  
  104.  
  105.  
  106.  
  107.