Geophysical Inversion and Modelling Library v1.5.4
Loading...
Searching...
No Matches
quaternion.h
1/******************************************************************************
2 * Copyright (C) 2008-2024 by the GIMLi development team *
3 * Carsten Rücker carsten@resistivity.net *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************/
18
19#ifndef _GIMLI_QUATERNION__H
20#define _GIMLI_QUATERNION__H
21
22#include <iostream>
23#include "pos.h"
24
25namespace GIMLI{
26
27template < class ValueType > class Quaternion;
28
29typedef Quaternion< double > RQuaternion;
30
31DLLEXPORT RMatrix getRotation(const Pos & src, const Pos & dest);
32
33template < class ValueType > class DLLEXPORT Quaternion{
34public:
35 Quaternion(ValueType w = 1.0, ValueType i = 0.0, ValueType j = 0.0, ValueType k = 0.0)
36 : re_(w){
37 im_[0] = i; im_[1] = j; im_[2] = k;
38 }
39
40 Quaternion(ValueType re, const Pos & im)
41 : re_(re), im_(im){ }
42
43 Quaternion(const Pos & xAxis, const Pos & yAxis,
44 const Pos & zAxis){
45 THROW_TO_IMPL
46 }
47
48 Quaternion(const Quaternion < ValueType > & q) { copy_(q); }
49
50 Quaternion & operator = (const Quaternion < ValueType > & q) {
51 if (this != & q){
52 copy_(q);
53 }
54 return *this;
55 }
56
57 const ValueType operator [] (const size_t i) const {
58 if (i == 0) return re_; else return im_[i - 1];
59 }
60
61 ValueType & operator [] (const size_t i){ if (i == 0) return re_; else return im_[i - 1]; }
62
63 Quaternion & operator /= (const ValueType & s) { re_ /= s; im_ /= s; return * this;}
64 Quaternion & operator *= (const ValueType & s) { re_ *= s; im_ /= s; return * this;}
65
66 void createFromAxisAngle(const Pos & axis, double angle){
67 //** The quaternion representing the rotation is
68 //** q = cos(a/2)+sin(a/2)*(x*i+y*j+z*k)
69 double ah = 0.5 * angle;
70 re_ = std::cos(ah);
71 im_ = axis * std::sin(ah);
72 }
73
74 template < class Matrix > void rotMatrix(Matrix & rot) const {
75 ValueType x = 2.0 * im_[0], y = 2.0 * im_[1], z = 2.0 * im_[2];
76
77 ValueType wx = x * re_, wy = y * re_, wz = z * re_;
78 ValueType xx = x * im_[0], xy = y * im_[0], xz = z * im_[0];
79 ValueType yy = y * im_[1], yz = z * im_[1], zz = z * im_[2];
80
81 rot[0][0] = 1.0 - (yy + zz);
82 rot[0][1] = xy - wz;
83 rot[0][2] = xz + wy;
84 rot[1][0] = xy + wz;
85 rot[1][1] = 1.0 - (xx + zz);
86 rot[1][2] = yz - wx;
87 rot[2][0] = xz - wy;
88 rot[2][1] = yz + wx;
89 rot[2][2] = 1.0 - (xx + yy);
90 }
91
92 inline ValueType norm() const { return re_ * re_ + im_.distSquared(); }
93
94 inline ValueType length() const { return std::sqrt(norm()); }
95
96 inline void normalise(){ *this /= length(); }
97
98 inline void setRe(const ValueType re){ re_ = re; }
99 inline ValueType re() const { return re_; }
100
101 inline void setIm(const Pos & im) { im_ = im; }
102 inline Pos im() const { return im_; }
103
104protected:
105
106 void copy_(const Quaternion < ValueType > & q){ re_ = q.re(); im_ = q.im(); }
107
108 ValueType re_;
109 Pos im_;
110};
111
112DLLEXPORT std::ostream & operator << (std::ostream & str, const RQuaternion & q);
113
114} // namespace GIMLI
115#endif
Simple row-based dense matrix based on Vector.
Definition matrix.h:324
3 dimensional vector
Definition pos.h:73
Definition quaternion.h:33
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
RVector y(const R3Vector &rv)
Definition pos.cpp:114
RVector x(const R3Vector &rv)
Definition pos.cpp:107
RVector z(const R3Vector &rv)
Definition pos.cpp:120