Isis 3 Programmer Reference
Quaternion.cpp
Go to the documentation of this file.
1 
24 #include "Quaternion.h"
25 
26 #include <string>
27 #include <iostream>
28 #include <vector>
29 
30 #include <SpiceUsr.h>
31 #include <SpiceZfc.h>
32 #include <SpiceZmc.h>
33 
34 #include "IException.h"
35 #include "IString.h"
36 #include "NaifStatus.h"
37 
38 namespace Isis {
43  p_quaternion.resize(4);
44 
45  for(int i = 0; i < 4; i++) {
46  p_quaternion[i] = 0.;
47  }
48  }
49 
58  Quaternion::Quaternion(const std::vector<double> rotation) {
59  p_quaternion.resize(4);
60  Set(rotation);
61 
62  }
63 
66 
72  void Quaternion::Set(std::vector<double> rotation) {
73 
74  if(rotation.size() == 9) { // Matrix initialization
76  m2q_c(&rotation[0], &p_quaternion[0]);
78  }
79  else if(rotation.size() == 4) { //quaternion initialization
80  p_quaternion = rotation;
81  }
82  else {
83  std::string msg = "Input vector of unexpected size for matrix or quaternion";
85  }
86 
87  }
88 
89 
91  std::vector<double> Quaternion::ToMatrix() {
92  std::vector<double> matrix(9);
93  q2m_c(&p_quaternion[0], (SpiceDouble( *)[3]) &matrix[0]);
94  return matrix;
95  }
96 
97 
112  p_quaternion = quat.p_quaternion;
113  return *this;
114  }
115 
116 
134  std::vector<double> qout(4);
135 
136  qxq_c((SpiceDouble *) & (this->p_quaternion[0]),
137  (SpiceDouble *) & (quat.p_quaternion[0]),
138  (SpiceDouble *) & (qout[0]));
139  this->p_quaternion[0] = qout[0];
140  this->p_quaternion[1] = qout[1];
141  this->p_quaternion[2] = qout[2];
142  this->p_quaternion[3] = qout[3];
143  return *this;
144  }
145 
146 
164  Isis::Quaternion qout = *this;
165  qout *= quat;
166 
167  return qout;
168  }
169 
170 
171 
188  Quaternion Quaternion::operator*(const double &scalar) {
189  Isis::Quaternion qout = *this;
190 
191  double scalar2 = scalar * scalar;
192  double unitizer = 1 + qout.p_quaternion[0] * qout.p_quaternion[0] * (scalar2 - 1);
193 
194  if(unitizer > 0.) {
195  unitizer = sqrt(unitizer);
196  }
197  else {
198  std::string msg = "Unable to make quaternion a unit quaternion";
200  }
201 
202  qout.p_quaternion[0] = (qout.p_quaternion[0] * scalar) / unitizer;
203 
204  for(int i = 1; i < 4; i++) {
205  qout.p_quaternion[i] /= unitizer;
206  }
207  Polish(qout);
208 
209  return qout;
210  }
211 
214  Quaternion qout;
215  qout.p_quaternion[0] = p_quaternion[0];
216 
217  for(int i = 1; i < 4; i++) {
218  qout.p_quaternion[i] = -p_quaternion[i];
219  }
220  return qout;
221  }
222 
223 
224 
231  std::vector<double> Quaternion::Qxv(const std::vector<double> &vin) {
232  if(vin.size() != 3) {
233  std::string msg = "Unexpected vector size -- 3 expected";
235  }
236 
237  Quaternion qvin;
238  qvin.p_quaternion[0] = 0.;
239  qvin.p_quaternion[1] = vin[0];
240  qvin.p_quaternion[2] = vin[1];
241  qvin.p_quaternion[3] = vin[2];
242 
243  Quaternion qvout(p_quaternion);
244  Quaternion conj(p_quaternion);
245  qvout *= qvin;
246  qvout *= conj.Conjugate();
247  std::vector<double> vout(qvout.p_quaternion.begin() + 1, qvout.p_quaternion.end());
248 
249  return vout;
250  }
251 
252 
259 
260  if(quat.p_quaternion[0] < 0) {
261  quat.p_quaternion[0] = -quat.p_quaternion[0];
262  quat.p_quaternion[1] = -quat.p_quaternion[1];
263  quat.p_quaternion[2] = -quat.p_quaternion[2];
264  quat.p_quaternion[3] = -quat.p_quaternion[3];
265  }
266 
267  }
268 
269 
270 
276  std::vector<double> Quaternion::ToAngles(int axis3, int axis2, int axis1) {
277  std::vector<double> rotationMatrix = ToMatrix();
278  SpiceDouble ang1, ang2, ang3;
280  m2eul_c((SpiceDouble *) &rotationMatrix[0], axis3, axis2, axis1,
281  &ang3, &ang2, &ang1);
283  std::vector<double> angles;
284  angles.push_back(ang1);
285  angles.push_back(ang2);
286  angles.push_back(ang3);
287  return angles;
288  }
289 
290 }
291 // end namespace isis
Quaternion & operator=(const Quaternion &quat)
Assign value of quaternion class to another quaternion.
Definition: Quaternion.cpp:111
Provide operations for quaternion arithmetic.
Definition: Quaternion.h:52
void Set(std::vector< double >)
Sets the quaternion value.
Definition: Quaternion.cpp:72
std::vector< double > p_quaternion
Quaternion.
Definition: Quaternion.h:109
Quaternion operator*(const Quaternion &quat) const
Multiply two Naif quaternions to create a new quaternion.
Definition: Quaternion.cpp:163
void Polish(Quaternion &quat)
Polish the quaternion – make the first component positive.
Definition: Quaternion.cpp:258
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
Quaternion & operator*=(const Quaternion &quat)
Multiply current Naif quaternion by another Naif quaternion, replacing the current quaternion...
Definition: Quaternion.cpp:133
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
Quaternion Conjugate()
Returns the conjugate of the quaternion.
Definition: Quaternion.cpp:213
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:276
std::vector< double > ToMatrix()
Converts quaternion to 3x3 rotational matrix.
Definition: Quaternion.cpp:91
std::vector< double > Qxv(const std::vector< double > &vin)
Multiply a vector by a quaternion (rotate the vector)
Definition: Quaternion.cpp:231
static void CheckErrors(bool resetNaif=true)
This method looks for any naif errors that might have occurred.
Definition: NaifStatus.cpp:43
Isis exception class.
Definition: IException.h:107
Quaternion()
Constructs an empty quaternion.
Definition: Quaternion.cpp:42
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
~Quaternion()
Destroys the Quaternion object.
Definition: Quaternion.cpp:65