Isis 3 Programmer Reference
Quaternion.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include "Quaternion.h"
9 
10 #include <string>
11 #include <iostream>
12 #include <vector>
13 
14 #include <SpiceUsr.h>
15 #include <SpiceZfc.h>
16 #include <SpiceZmc.h>
17 
18 #include "IException.h"
19 #include "IString.h"
20 #include "NaifStatus.h"
21 
22 namespace Isis {
27  p_quaternion.resize(4);
28 
29  for(int i = 0; i < 4; i++) {
30  p_quaternion[i] = 0.;
31  }
32  }
33 
42  Quaternion::Quaternion(const std::vector<double> rotation) {
43  p_quaternion.resize(4);
44  Set(rotation);
45 
46  }
47 
50 
56  void Quaternion::Set(std::vector<double> rotation) {
57 
58  if(rotation.size() == 9) { // Matrix initialization
60  m2q_c(&rotation[0], &p_quaternion[0]);
62  }
63  else if(rotation.size() == 4) { //quaternion initialization
64  p_quaternion = rotation;
65  }
66  else {
67  std::string msg = "Input vector of unexpected size for matrix or quaternion";
68  throw IException(IException::Programmer, msg, _FILEINFO_);
69  }
70 
71  }
72 
73 
75  std::vector<double> Quaternion::ToMatrix() {
76  std::vector<double> matrix(9);
77  q2m_c(&p_quaternion[0], (SpiceDouble( *)[3]) &matrix[0]);
78  return matrix;
79  }
80 
81 
97  return *this;
98  }
99 
100 
118  std::vector<double> qout(4);
119 
120  qxq_c((SpiceDouble *) & (this->p_quaternion[0]),
121  (SpiceDouble *) & (quat.p_quaternion[0]),
122  (SpiceDouble *) & (qout[0]));
123  this->p_quaternion[0] = qout[0];
124  this->p_quaternion[1] = qout[1];
125  this->p_quaternion[2] = qout[2];
126  this->p_quaternion[3] = qout[3];
127  return *this;
128  }
129 
130 
148  Isis::Quaternion qout = *this;
149  qout *= quat;
150 
151  return qout;
152  }
153 
154 
155 
172  Quaternion Quaternion::operator*(const double &scalar) {
173  Isis::Quaternion qout = *this;
174 
175  double scalar2 = scalar * scalar;
176  double unitizer = 1 + qout.p_quaternion[0] * qout.p_quaternion[0] * (scalar2 - 1);
177 
178  if(unitizer > 0.) {
179  unitizer = sqrt(unitizer);
180  }
181  else {
182  std::string msg = "Unable to make quaternion a unit quaternion";
183  throw IException(IException::Programmer, msg, _FILEINFO_);
184  }
185 
186  qout.p_quaternion[0] = (qout.p_quaternion[0] * scalar) / unitizer;
187 
188  for(int i = 1; i < 4; i++) {
189  qout.p_quaternion[i] /= unitizer;
190  }
191  Polish(qout);
192 
193  return qout;
194  }
195 
198  Quaternion qout;
199  qout.p_quaternion[0] = p_quaternion[0];
200 
201  for(int i = 1; i < 4; i++) {
202  qout.p_quaternion[i] = -p_quaternion[i];
203  }
204  return qout;
205  }
206 
207 
208 
215  std::vector<double> Quaternion::Qxv(const std::vector<double> &vin) {
216  if(vin.size() != 3) {
217  std::string msg = "Unexpected vector size -- 3 expected";
218  throw IException(IException::Programmer, msg, _FILEINFO_);
219  }
220 
221  Quaternion qvin;
222  qvin.p_quaternion[0] = 0.;
223  qvin.p_quaternion[1] = vin[0];
224  qvin.p_quaternion[2] = vin[1];
225  qvin.p_quaternion[3] = vin[2];
226 
227  Quaternion qvout(p_quaternion);
228  Quaternion conj(p_quaternion);
229  qvout *= qvin;
230  qvout *= conj.Conjugate();
231  std::vector<double> vout(qvout.p_quaternion.begin() + 1, qvout.p_quaternion.end());
232 
233  return vout;
234  }
235 
236 
243 
244  if(quat.p_quaternion[0] < 0) {
245  quat.p_quaternion[0] = -quat.p_quaternion[0];
246  quat.p_quaternion[1] = -quat.p_quaternion[1];
247  quat.p_quaternion[2] = -quat.p_quaternion[2];
248  quat.p_quaternion[3] = -quat.p_quaternion[3];
249  }
250 
251  }
252 
253 
254 
260  std::vector<double> Quaternion::ToAngles(int axis3, int axis2, int axis1) {
261  std::vector<double> rotationMatrix = ToMatrix();
262  SpiceDouble ang1, ang2, ang3;
264  m2eul_c((SpiceDouble *) &rotationMatrix[0], axis3, axis2, axis1,
265  &ang3, &ang2, &ang1);
267  std::vector<double> angles;
268  angles.push_back(ang1);
269  angles.push_back(ang2);
270  angles.push_back(ang3);
271  return angles;
272  }
273 
274 }
275 // end namespace isis
Isis::Quaternion::operator*=
Quaternion & operator*=(const Quaternion &quat)
Multiply current Naif quaternion by another Naif quaternion, replacing the current quaternion.
Definition: Quaternion.cpp:117
Isis::Quaternion::ToMatrix
std::vector< double > ToMatrix()
Converts quaternion to 3x3 rotational matrix.
Definition: Quaternion.cpp:75
Isis::Quaternion::Qxv
std::vector< double > Qxv(const std::vector< double > &vin)
Multiply a vector by a quaternion (rotate the vector)
Definition: Quaternion.cpp:215
Isis::Quaternion::Quaternion
Quaternion()
Constructs an empty quaternion.
Definition: Quaternion.cpp:26
Isis::NaifStatus::CheckErrors
static void CheckErrors(bool resetNaif=true)
This method looks for any naif errors that might have occurred.
Definition: NaifStatus.cpp:28
Isis::Quaternion::operator=
Quaternion & operator=(const Quaternion &quat)
Assign value of quaternion class to another quaternion.
Definition: Quaternion.cpp:95
Isis::Quaternion::p_quaternion
std::vector< double > p_quaternion
Quaternion.
Definition: Quaternion.h:93
Isis::Quaternion::operator*
Quaternion operator*(const Quaternion &quat) const
Multiply two Naif quaternions to create a new quaternion.
Definition: Quaternion.cpp:147
Isis::Quaternion::~Quaternion
~Quaternion()
Destroys the Quaternion object.
Definition: Quaternion.cpp:49
Isis::Quaternion::Set
void Set(std::vector< double >)
Sets the quaternion value.
Definition: Quaternion.cpp:56
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::Quaternion::Polish
void Polish(Quaternion &quat)
Polish the quaternion – make the first component positive.
Definition: Quaternion.cpp:242
Isis::Quaternion
Provide operations for quaternion arithmetic.
Definition: Quaternion.h:36
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
Isis::Quaternion::Conjugate
Quaternion Conjugate()
Returns the conjugate of the quaternion.
Definition: Quaternion.cpp:197
Isis::Quaternion::ToAngles
std::vector< double > ToAngles(int axis3, int axis2, int axis1)
Return the camera angles (right ascension, declination, and twist) for the quaternion.
Definition: Quaternion.cpp:260
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16