4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 #if defined (_QUATERNION_H_) && defined (_VECTOR_H_)
26 template <typename T> int sgn(T val) {
33 template <typename T> T mag(T val) {
40 template <typename TYPE>
41 quaternion<TYPE>::quaternion() : m_quat(QUAT_SIZE)
45 template <typename TYPE>
46 quaternion<TYPE>::quaternion(const TYPE w, const TYPE x, const TYPE y, const TYPE z)
48 TYPE vec_data[QUAT_SIZE] = {w, x, y, z};
50 vect<TYPE> v(QUAT_SIZE, vec_data);
54 template <typename TYPE>
55 quaternion<TYPE>::quaternion(const vect<TYPE> v)
60 template <typename TYPE>
61 quaternion<TYPE>::quaternion(const quaternion<TYPE>& q)
66 template <typename TYPE>
67 quaternion<TYPE>::~quaternion()
71 template <typename TYPE>
72 quaternion<TYPE> quaternion<TYPE>::operator =(const quaternion<TYPE>& q)
79 template <typename TYPE>
80 void quaternion<TYPE>::quat_normalize()
85 w = m_quat.m_vec[0] * m_quat.m_vec[0];
86 x = m_quat.m_vec[1] * m_quat.m_vec[1];
87 y = m_quat.m_vec[2] * m_quat.m_vec[2];
88 z = m_quat.m_vec[3] * m_quat.m_vec[3];
90 val = sqrt(w + x + y + z);
92 m_quat = m_quat / val;
96 quaternion<T> operator *(const quaternion<T> q, const T val)
103 template <typename T>
104 quaternion<T> operator *(const quaternion<T> q1, const quaternion<T> q2)
110 w1 = q1.m_quat.m_vec[0];
111 x1 = q1.m_quat.m_vec[1];
112 y1 = q1.m_quat.m_vec[2];
113 z1 = q1.m_quat.m_vec[3];
115 w2 = q2.m_quat.m_vec[0];
116 x2 = q2.m_quat.m_vec[1];
117 y2 = q2.m_quat.m_vec[2];
118 z2 = q2.m_quat.m_vec[3];
120 x = x1*w2 + y1*z2 - z1*y2 + w1*x2;
121 y = -x1*z2 + y1*w2 + z1*x2 + w1*y2;
122 z = x1*y2 - y1*x2 + z1*w2 + w1*z2;
123 w = -x1*x2 - y1*y2 - z1*z2 + w1*w2;
125 quaternion<T> q(w, x, y, z);
130 template <typename T>
131 quaternion<T> operator +(const quaternion<T> q1, const quaternion<T> q2)
133 return (q1.m_quat + q2.m_quat);
138 quaternion<T> phase_correction(const quaternion<T> q1, const quaternion<T> q2)
141 w = mag(q1.m_quat.m_vec[0]) * sgn(q2.m_quat.m_vec[0]);
142 x = mag(q1.m_quat.m_vec[1]) * sgn(q2.m_quat.m_vec[1]);
143 y = mag(q1.m_quat.m_vec[2]) * sgn(q2.m_quat.m_vec[2]);
144 z = mag(q1.m_quat.m_vec[3]) * sgn(q2.m_quat.m_vec[3]);
146 quaternion<T> q(w, x, y, z);
150 #endif //_QUATERNION_H_