CalculiX  2.13
A Free Software Three-Dimensional Structural Finite Element Program
rotationvector.f File Reference

Go to the source code of this file.

Functions/Subroutines

subroutine rotationvector (a, v)
 

Function/Subroutine Documentation

◆ rotationvector()

subroutine rotationvector ( real*8, dimension(3,3), intent(in)  a,
real*8, dimension(3), intent(out)  v 
)
20 !
21 ! calculates rotation vector v from rotation matrix a
22 !
23  implicit none
24 !
25  real*8 a(3,3),q(4),v(3),theta,length,pi
26 !
27  intent(in) a
28 !
29  intent(out) v
30 !
31 ! based on: J.M.P. van Waveren, From Quaternion to Matrix and Back
32 ! February 27th 2005, Id Software, Inc.
33 ! and
34 ! Jay A. Farrel, Computation of the Quaternion from a Rotation
35 ! Matrix, November 30, 2015, University of California, Riverside
36 !
37  pi=4.d0*datan(1.d0)
38 !
39  if ((a(1,1)+a(2,2)+a(3,3)).gt.0.d0) then
40  q(1)=dsqrt(a(1,1)+a(2,2)+a(3,3)+1.d0)/2.d0
41  q(2)=(a(3,2)-a(2,3))/(4.d0*q(1))
42  q(3)=(a(1,3)-a(3,1))/(4.d0*q(1))
43  q(4)=(a(2,1)-a(1,2))/(4.d0*q(1))
44  else if (((a(1,1).gt.a(2,2)).and.(a(1,1).gt.a(3,3)))) then
45  q(2)=dsqrt(a(1,1)-a(2,2)-a(3,3)+1.d0)/2.d0
46  q(1)=(a(3,2)-a(2,3))/(4.d0*q(2))
47  q(3)=(a(1,2)+a(2,1))/(4.d0*q(2))
48  q(4)=(a(1,3)+a(3,1))/(4.d0*q(2))
49  else if (a(2,2).gt.a(3,3)) then
50  q(3)=dsqrt(-a(1,1)+a(2,2)-a(3,3)+1.d0)/2.d0
51  q(1)=(a(1,3)-a(3,1))/(4.d0*q(3))
52  q(2)=(a(1,2)+a(2,1))/(4.d0*q(3))
53  q(4)=(a(2,3)+a(3,2))/(4.d0*q(3))
54  else
55  q(4)=dsqrt(-a(1,1)-a(2,2)+a(3,3)+1.d0)/2.d0
56  q(1)=(a(2,1)-a(1,2))/(4.d0*q(4))
57  q(2)=(a(1,3)+a(3,1))/(4.d0*q(4))
58  q(3)=(a(2,3)+a(3,2))/(4.d0*q(4))
59  endif
60 !
61  length=dsqrt(q(2)*q(2)+q(3)*q(3)+q(4)*q(4))
62  theta=2.d0*acos(q(1))
63 !
64 ! if pi<theta<2*pi: reverse direction of rotation vector
65 ! and map angle in the range (0,pi)
66 !
67  if (theta.gt.pi) then
68  theta=2*pi-theta
69  q(2)=-q(2)
70  q(3)=-q(3)
71  q(4)=-q(4)
72  endif
73 !
74  if (length.ne.0) then
75  v(1)=theta*q(2)/length
76  v(2)=theta*q(3)/length
77  v(3)=theta*q(4)/length
78  else
79  v(1)=0.d0
80  v(2)=0.d0
81  v(3)=0.d0
82  endif
83 !
84 ! if theta=pi:
85 ! if x<0 -> change sign of rotation vector
86 ! elseif x=0 and y<0 -> change sign of rotation vector
87 ! elseif x=0 and y=0 and z<0 -> change sign of rotation vector
88 !
89  if (theta.eq.pi) then
90  if(v(1).lt.0.d0) then
91 ! +++ vs ---
92 ! ++- vs --+
93 ! +-+ vs -+-
94 ! +-- vs -++
95 ! +0+ vs -0-
96 ! ++0 vs --0
97 ! +0- vs -0+
98 ! +-0 vs -+0
99 ! +00 vs -00
100  v(1)=-v(1)
101  v(2)=-v(2)
102  v(3)=-v(3)
103  elseif(v(1).eq.0.d0) then
104  if(v(2).lt.0.d0) then
105 ! 0+- vs 0-+
106 ! 0+0 vs 0-0
107 ! 0++ vs 0--
108  v(2)=-v(2)
109  v(3)=-v(3)
110  elseif(v(2).eq.0.d0) then
111  if(v(3).lt.0.d0) then
112 ! 00+ vs 00-
113  v(3)=-v(3)
114  endif
115  endif
116  endif
117  endif
118 !
119 c write(*,*)'ROTATION VECTOR'
120 c write(*,*)v(1),v(2),v(3)
121 !
122  return
123 !
Hosted by OpenAircraft.com, (Michigan UAV, LLC)