# v_rotlu2ro

## PURPOSE

V_ROTLU2RO converts look and up directions to a rotation matrix

## SYNOPSIS

function r=v_rotlu2ro(l,u)

## DESCRIPTION

    V_ROTLU2RO converts look and up directions to a rotation matrix
      Inputs:  L(3,...)    Vector specifying look direction (need not be a unit vector)
               U(3,...)    Vector specifying up direction. Default is u=[0 0 1]'
                           unless l is a multiple of this, in which case u=[0 1 0]'.

     Outputs:  R(3,3,...)  Equivalent rotation matrix

     The rotation maps the look direction to the negative z-axis and the up direction
     to lie in the y-z plane with a positive y component. That is, R*L=a*[0 0 -1]' and
     R*U=b*[0 1 c] for postive constants a and b. After applying this rotation to an object,
     the 2-D data obtained by omitting the z-component represents an orthographic projection
     performed by a camera looking in the direction L.

## CROSS-REFERENCE INFORMATION

This function calls:

- v_rotro2qr V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion

This function is called by:

## SOURCE CODE

    0001 function r=v_rotlu2ro(l,u)
    0002 %V_ROTLU2RO converts look and up directions to a rotation matrix
    0003 %  Inputs:  L(3,...)    Vector specifying look direction (need not be a unit vector)
    0004 %           U(3,...)    Vector specifying up direction. Default is u=[0 0 1]'
    0005 %                       unless l is a multiple of this, in which case u=[0 1 0]'.
    0006 %
    0007 % Outputs:  R(3,3,...)  Equivalent rotation matrix
    0008 %
    0009 % The rotation maps the look direction to the negative z-axis and the up direction
    0010 % to lie in the y-z plane with a positive y component. That is, R*L=a*[0 0 -1]' and
    0011 % R*U=b*[0 1 c] for postive constants a and b. After applying this rotation to an object,
    0012 % the 2-D data obtained by omitting the z-component represents an orthographic projection
    0013 % performed by a camera looking in the direction L.
    0014
    0015 %      Copyright (C) Mike Brookes 2023
    0016 %      Version: $Id: v_rotlu2ro.m 10865 2018-09-21 17:22:45Z dmb $
    0017 %
    0018 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0019 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0020 %
    0021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0022 %   This program is free software; you can redistribute it and/or modify
    0023 %   it under the terms of the GNU Lesser General Public License as published by
    0024 %   the Free Software Foundation; either version 3 of the License, or
    0025 %   (at your option) any later version.
    0026 %
    0027 %   This program is distributed in the hope that it will be useful,
    0028 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0029 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0030 %   GNU Lesser General Public License for more details.
    0031 %
    0032 %   You can obtain a copy of the GNU Lesser General Public License from
    0033 %   https://www.gnu.org/licenses/ .
    0034 %
    0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0036 persistent mk
    0037 if isempty(mk)
    0038     mk=cat(3,repmat([-1;1;-1],1,3),repmat([1;-1;-1],1,3),repmat([1;1;1],1,3),repmat([-1;-1;1],1,3));
    0039 end
    0040 sz=size(l);
    0041 l=reshape(l,3,[]);          % make 2-dimensional
    0042 n=size(l,2);                % number of rotation matrices to generate
    0043 if n==1
    0044     r=zeros(3,3);
    0045 else
    0046     r=zeros([3 3 sz(2:end)]);
    0047 end
    0048 if nargin<2
    0049     u=repmat([0;0;1],1,n);
    0050     u(2,:)=u(2,:)+(l(1,:)==0 & l(2,:)==0);
    0051 end
    0052 for i=1:n
    0053     [q,t]=qr([l(:,i) u(:,i)]);
    0054     rx=[cross(q(:,2),q(:,1)) q(:,2) q(:,1)]';
    0055     r(:,:,i)=rx.*mk(:,:,2*(rx(3,:)*l(:,i)<0)+(rx(2,:)*u(:,i)<0)+1);
    0056 end
    0057 if ~nargout
    0058     v_rotro2qr(r(:,:,1)); % plot a cube
    0059     set(gca,'CameraPosition',[0 0 1]);
    0060 end