-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuaternion_type.f90
169 lines (126 loc) · 3.64 KB
/
Quaternion_type.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
!Quaternion type, required mathematical operations and conversions fom quaternions to vectors
!and vice versa.
module Quaternion_Math
implicit none
type trig_type
integer :: i(3)
end type trig_type
type quaternion
double precision :: q(4)
end type quaternion
interface operator(+)
module procedure q_add
end interface operator(+)
interface operator(-)
module procedure q_sub
end interface operator(-)
interface operator(*)
module procedure q_multi
end interface operator(*)
interface operator (/)
module procedure q_div
end interface operator (/)
interface assignment(=)
module procedure q_eq
end interface assignment(=)
interface operator (.len.)
module procedure q_len
end interface operator (.len.)
interface operator(.vtoq.)
module procedure q_v2q
end interface operator(.vtoq.)
interface operator(.qtov.)
module procedure q_q2v
end interface operator(.qtov.)
contains
function conjugate(q1) result(q_conj)
implicit none
type(quaternion),intent(in) :: q1
type(quaternion) :: q_conj
integer :: i
q_conj = q1
do i=2,4
q_conj%q(i) = -1*q1%q(i)
end do
end function conjugate
function q_add(q1,q2) result(q_out)
implicit none
type(quaternion), intent(in) :: q1,q2
type(quaternion) :: q_out
q_out%q(1) = q1%q(1) + q2%q(1)
q_out%q(2) = q1%q(2) + q2%q(2)
q_out%q(3) = q1%q(3) + q2%q(3)
q_out%q(4) = q1%q(4) + q2%q(4)
end function q_add
function q_sub(q1,q2) result(q_out)
implicit none
type(quaternion), intent(in) :: q1,q2
type(quaternion) :: q_out
q_out%q(1) = q1%q(1) - q2%q(1)
q_out%q(2) = q1%q(2) - q2%q(2)
q_out%q(3) = q1%q(3) - q2%q(3)
q_out%q(4) = q1%q(4) - q2%q(4)
end function q_sub
function q_prod(q1,q2) result(q_out)
implicit none
type(quaternion), intent(in) :: q1,q2
type(quaternion) :: q_out
q_out%q(1) = q1%q(1)*q2%q(1) - q1%q(2)*q2%q(2) &
- q1%q(3)*q2%q(3) - q1%q(4)*q2%q(4)
q_out%q(2) = q1%q(1)*q2%q(2) + q1%q(2)*q2%q(1) &
+ q1%q(3)*q2%q(4) - q1%q(4)*q2%q(3)
q_out%q(3) = q1%q(1)*q2%q(3) - q1%q(2)*q2%q(4) &
+ q1%q(3)*q2%q(1) + q1%q(4)*q2%q(2)
q_out%q(4) = q1%q(1)*q2%q(4) + q1%q(2)*q2%q(3) &
- q1%q(3)*q2%q(2) + q1%q(4)*q2%q(1)
end function q_prod
type(quaternion) function q_multi(q1,x)
implicit none
type(quaternion),intent(in) :: q1
double precision,intent(in) :: x
integer :: i
do i=1,4
q_multi%q(i) = q1%q(i)*x
end do
end function q_multi
type(quaternion) function q_div(q1,x)
implicit none
type(quaternion),intent(in) :: q1
double precision,intent(in) :: x
integer :: i
do i=1,4
q_div%q(i) = q1%q(i)/x
end do
end function q_div
double precision function q_len(q1)
implicit none
type(quaternion),intent(in) :: q1
q_len = (q1%q(1)**2 + q1%q(2)**2 + q1%q(3)**2 + q1%q(4)**2)**0.5
end function q_len
subroutine q_eq(q1,x)
implicit none
type(quaternion),intent(out) :: q1
double precision,intent(in) :: x
integer :: i
do i=1,4
q1%q(i) = x
end do
end subroutine q_eq
function q_v2q(v1) result(q1)
implicit none
type(quaternion) :: q1
double precision,dimension(3),intent(in) :: v1
q1%q(1) = 0
q1%q(2) = v1(1)
q1%q(3) = v1(2)
q1%q(4) = v1(3)
end function q_v2q
function q_q2v(q1) result(v1)
implicit none
double precision,dimension(3) :: v1
type(quaternion),intent(in) :: q1
v1(1) = q1%q(2)
v1(2) = q1%q(3)
v1(3) = q1%q(4)
end function q_q2v
end module Quaternion_Math