Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_quaternion.cxx
Go to the documentation of this file.
1 #include <math.h>
2 #include <stdlib.h>
3 #include "a_quaternion.h"
4 
5 //---------------------------------------------------------------------------
6 double a_quaternion::norm() const
7 {
8  return r_*r_+i_*i_+j_*j_+k_*k_;
9 }
10 //---------------------------------------------------------------------------
11 double a_quaternion::abs() const
12 {
13  return sqrt(norm());
14 }
15 //---------------------------------------------------------------------------
17 {
18  r_ = -r_;
19  i_ = -i_;
20  j_ = -j_;
21  k_ = -k_;
22  return *this;
23 }
24 //---------------------------------------------------------------------------
26 {
27  i_ = -i_;
28  j_ = -j_;
29  k_ = -k_;
30  return *this;
31 }
32 //---------------------------------------------------------------------------
34 {
35  double temp = 2.*r_;
36  r_ = r_*r_-(i_*i_+j_*j_+k_*k_);
37  i_ *= temp;
38  j_ *= temp;
39  k_ *= temp;
40  return *this;
41 }
42 //---------------------------------------------------------------------------
44 {
45  double temp = norm();
46  r_ /= temp;
47  i_ /= -temp;
48  j_ /= -temp;
49  k_ /= -temp;
50  return (*this);
51 }
52 //---------------------------------------------------------------------------
54 {
55  r_ += b.r_; i_ += b.i_; j_ += b.j_; k_ += b.k_; return *this;
56 }
57 //---------------------------------------------------------------------------
59 {
60  a_quaternion a = *this;
61  return a += b;
62 }
63 //---------------------------------------------------------------------------
65 {
66  r_ -= b.r_; i_ -= b.i_; j_ -= b.j_; k_ -= b.k_; return *this;
67 }
68 //---------------------------------------------------------------------------
70 {
71  a_quaternion a = *this;
72  return a -= b;
73 }
74 //---------------------------------------------------------------------------
76 {
77  r_ *= b; i_ *= b; j_ *= b; k_ *= b; return *this;
78 }
79 //---------------------------------------------------------------------------
81 {
82  a_quaternion a = *this;
83  return a *= b;
84 }
85 //---------------------------------------------------------------------------
87 {
88  r_ /= b; i_ /= b; j_ /= b; k_ /= b; return *this;
89 }
90 //---------------------------------------------------------------------------
92 {
93  a_quaternion a = *this;
94  return a /= b;
95 }
96 //---------------------------------------------------------------------------
98 {
99  double t0 = (k_-j_)*(b.j_-b.k_);
100  double t1 = (r_+i_)*(b.r_+b.i_);
101  double t2 = (r_-i_)*(b.j_+b.k_);
102  double t3 = (k_+j_)*(b.r_-b.i_);
103  double t4 = (k_-i_)*(b.i_-b.j_);
104  double t5 = (k_+i_)*(b.i_+b.j_);
105  double t6 = (r_+j_)*(b.r_-b.k_);
106  double t7 = (r_-j_)*(b.r_+b.k_);
107  double t8 = t5+t6+t7;
108  double t9 = (t4+t8)/2.;
109  r_ = t0+t9-t5;
110  i_ = t1+t9-t8;
111  j_ = t2+t9-t7;
112  k_ = t3+t9-t6;
113  return *this;
114 }
115 //---------------------------------------------------------------------------
117 {
118  a_quaternion a = *this;
119  return a *= b;
120 }
121 //---------------------------------------------------------------------------
123 {
124  a_quaternion temp = b;
125  return (*this)*=(temp.invert());
126 }
127 //---------------------------------------------------------------------------
129 {
130  a_quaternion a = *this;
131  return a /= b;
132 }
133 //---------------------------------------------------------------------------
134 bool operator==(const a_quaternion& a, const a_quaternion& b)
135 {
136  return (a.r() == b.r() && a.i() == b.i() && a.j() == b.j() && a.k() == b.k());
137 }
138 //---------------------------------------------------------------------------
139 bool operator!=(const a_quaternion& a, const a_quaternion& b)
140 {
141  return !(a==b);
142 }
143 //---------------------------------------------------------------------------
145 {
146  return x.negate();
147 }
148 //---------------------------------------------------------------------------
150 {
151  std::cerr << "sqrt(const a_quaternion&) undefined because a_quaternion::sqrt() undefined." << std::endl;
152  exit(1); // Should be derived from member function.
153 }
154 //---------------------------------------------------------------------------
155 std::ostream& operator<<(std::ostream& o, const a_quaternion& x)
156 {
157  return o << x.r_ << " " << x.i_ << " " << x.j_ << " " << x.k_;
158 }
159 //---------------------------------------------------------------------------
161 {
162  a_quaternion z = x;
163  return z += y; // derived operator
164 }
165 //---------------------------------------------------------------------------
167 {
168  a_quaternion z = x;
169  return z -= y; // derived operator
170 }
171 //---------------------------------------------------------------------------
172 a_quaternion operator*(const a_quaternion& x, const double y)
173 {
174  a_quaternion z = x;
175  return z *= y; // derived operator
176 }
177 //---------------------------------------------------------------------------
179 {
180  a_quaternion z = x;
181  return z *= y; // derived operator
182 }
183 //---------------------------------------------------------------------------
185 {
186  a_quaternion z = x;
187  return z /= y; // derived operator
188 }
a_quaternion sqrt(const a_quaternion &x)
std::ostream & operator<<(std::ostream &o, const a_quaternion &x)
a_quaternion operator+(const a_quaternion &x, const a_quaternion &y)
a_quaternion operator/(const a_quaternion &x, const a_quaternion &y)
bool operator==(const a_quaternion &a, const a_quaternion &b)
a_quaternion operator*(const a_quaternion &x, const double y)
a_quaternion operator-(a_quaternion &x)
bool operator!=(const a_quaternion &a, const a_quaternion &b)
double k() const
Definition: a_quaternion.h:30
a_quaternion & conjugate()
double norm() const
Definition: a_quaternion.cxx:6
double j() const
Definition: a_quaternion.h:29
a_quaternion operator/(const double b)
a_quaternion & invert()
a_quaternion operator-(const a_quaternion &b)
double abs() const
a_quaternion operator+(const a_quaternion &b)
a_quaternion & square()
a_quaternion & operator*=(const double b)
a_quaternion & negate()
a_quaternion & operator-=(const a_quaternion &b)
a_quaternion operator*(const double b)
double r() const
Definition: a_quaternion.h:27
double i() const
Definition: a_quaternion.h:28
a_quaternion & operator/=(const double b)
a_quaternion & operator+=(const a_quaternion &b)