# v_rotpl2ro

## PURPOSE

V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]

## SYNOPSIS

function r=v_rotpl2ro(u,v,t)

## DESCRIPTION

    V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]
     Inputs:

         U(n,1) and V(n,1) define a plane in n-dimensional space
         T is the rotation angle in radians from U towards V. If T
           is omitted it will default to the angle between U and V

     Outputs:

         R(n,n)   Rotation matrix

## CROSS-REFERENCE INFORMATION

This function calls:

- v_atan2sc V_ATAN2SC sin and cosine of atan(y/x) [S,C,R,T]=(Y,X)
- v_rotqr2ro ROTQR2RO converts a real quaternion to a 3x3 rotation matrix
- v_rotro2qr V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion

This function is called by:

## SOURCE CODE

    0001 function r=v_rotpl2ro(u,v,t)
    0002 %V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]
    0003 % Inputs:
    0004 %
    0005 %     U(n,1) and V(n,1) define a plane in n-dimensional space
    0006 %     T is the rotation angle in radians from U towards V. If T
    0007 %       is omitted it will default to the angle between U and V
    0008 %
    0009 % Outputs:
    0010 %
    0011 %     R(n,n)   Rotation matrix
    0012
    0013 %
    0014 %      Copyright (C) Mike Brookes 2007-2018
    0015 %      Version: $Id: v_rotpl2ro.m 10865 2018-09-21 17:22:45Z dmb $
    0016 %
    0017 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0018 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0019 %
    0020 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0021 %   This program is free software; you can redistribute it and/or modify
    0022 %   it under the terms of the GNU Lesser General Public License as published by
    0023 %   the Free Software Foundation; either version 3 of the License, or
    0024 %   (at your option) any later version.
    0025 %
    0026 %   This program is distributed in the hope that it will be useful,
    0027 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0028 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0029 %   GNU Lesser General Public License for more details.
    0030 %
    0031 %   You can obtain a copy of the GNU Lesser General Public License from
    0032 %   https://www.gnu.org/licenses/ .
    0033 %
    0034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0035
    0036 u=u(:);
    0037     n=length(u);
    0038 v=v(:);
    0039 l=sqrt(u'*u);
    0040 if l==0, error('input u is a zero vector'); end
    0041 u=u/l;      % normalize
    0042 q=v-v'*u*u;        % q is orthogonal to x
    0043 l=sqrt(q'*q);
    0044 if l==0          % u and v are colinear or v=zero
    0045     [m,i]=max(abs(u));
    0046     q=zeros(n,1);
    0047     q(1+mod(i(1),n))=1;  % choose next available dimension
    0048     q=q-q'*u*u;  % q is orthogonal to x
    0049     l=sqrt(q'*q);
    0050 end
    0051 q=q/l;          % normalize
    0052 if nargin<3
    0053     [s,c]=v_atan2sc(v'*q,v'*u);
    0054     r=eye(n)+(c-1)*(u*u'+q*q')+s*(q*u'-u*q');
    0055 else
    0056     r=eye(n)+(cos(t)-1)*(u*u'+q*q')+sin(t)*(q*u'-u*q');
    0057 end
    0058 if ~nargout && n==3
    0059     v_rotqr2ro(v_rotro2qr(r)); % plot a rotated cube
    0060 end
    0061