Saturday, January 21, 2017

Orbital Aero Model: Euler Class

This is one of a series of posts for how orientations and rotations are handled in the orbital aero model (original post in series). This Euler class ties in with the Vector, Quaternion, and Matrix classes also posted.

Euler.h


#pragma once
#include <math.h>

class Vector;
class Quaternion;
class Matrix;

class Euler
{
private:
Matrix getXRotationMatrix() const;
Matrix getYRotationMatrix() const;
Matrix getZRotationMatrix() const;

public:
// Coordinate system:
// X forward
// Y to the right
// Z down

double psi; // rotate about z (yaw)
double theta; // rotate about y (pitch)
double phi; // rotate about x (roll)

// Create a set of euler angles.
Euler();

// Create euler angles with given values for roll, pitch, and yaw.
Euler(const double newPsi, const double newTheta, const double newPhi);

// Destructor
~Euler();

// Scales the euler angles by a given value.
Euler operator * (const double valueToMultiply) const;

// Clear the euler angles to all 0.
void clear();

// Return if any of the euler angles are non-zero.
bool isNonZero() const;

// Get the transformation matrix for these euler angles.
Matrix transformationMatrix() const;
};

Euler.cpp


#include "Matrix.h"
#include "Vector.h"
#include "Euler.h"

// Create a set of euler angles.
Euler::Euler()
{
this->psi = 0.0;
this->theta = 0.0;
this->phi = 0.0;
return;
}

// Create euler angles with given values for roll, pitch, and yaw.
Euler::Euler(const double newPsi, const double newTheta, const double newPhi)
{
this->psi = newPsi;
this->theta = newTheta;
this->phi = newPhi;
return;
}

// Destructor
Euler::~Euler()
{
}

// Scales the euler angles by a given value.
Euler Euler::operator * (const double valueToMultiply) const
{
return Euler(this->psi * valueToMultiply, this->theta * valueToMultiply, this->phi * valueToMultiply);
}

// Clear the euler angles to all 0.
void Euler::clear()
{
this->psi = 0.0;
this->theta = 0.0;
this->phi = 0.0;
}

// Return if any of the euler angles are non-zero.
bool Euler::isNonZero() const
{
if (this->psi != 0.0 || this->theta != 0.0 || this->phi != 0.0)
{
return true;
}
else
{
return false;
}
}

// Get the transformation matrix for these euler angles.
Matrix Euler::transformationMatrix() const
{
Matrix xRotationMatrix = getXRotationMatrix();
Matrix yRotationMatrix = getYRotationMatrix();
Matrix zRotationMatrix = getZRotationMatrix();
return Matrix(xRotationMatrix * yRotationMatrix * zRotationMatrix);
}

// Create a x-axis rotation matrix.
Matrix Euler::getXRotationMatrix() const
{
double cosRotation = cos(this->phi);
double sinRotation = sin(this->phi);

Matrix xRotationaMatrix;
xRotationaMatrix.value[0][0] = 1.0;
xRotationaMatrix.value[0][1] = 0.0;
xRotationaMatrix.value[0][2] = 0.0;
xRotationaMatrix.value[1][0] = 0.0;
xRotationaMatrix.value[1][1] = cosRotation;
xRotationaMatrix.value[1][2] = sinRotation;
xRotationaMatrix.value[2][0] = 0.0;
xRotationaMatrix.value[2][1] = -sinRotation;
xRotationaMatrix.value[2][2] = cosRotation;

return xRotationaMatrix;
}

// Create a y-axis rotation matrix.
Matrix Euler::getYRotationMatrix() const
{
double cosRotation = cos(this->theta);
double sinRotation = sin(this->theta);

Matrix yRotationaMatrix;
yRotationaMatrix.value[0][0] = cosRotation;
yRotationaMatrix.value[0][1] = 0.0;
yRotationaMatrix.value[0][2] = -sinRotation;
yRotationaMatrix.value[1][0] = 0.0;
yRotationaMatrix.value[1][1] = 1.0;
yRotationaMatrix.value[1][2] = 0.0;
yRotationaMatrix.value[2][0] = sinRotation;
yRotationaMatrix.value[2][1] = 0.0;
yRotationaMatrix.value[2][2] = cosRotation;

return yRotationaMatrix;
}

// Create a z-axis rotation matrix.
Matrix Euler::getZRotationMatrix() const
{
double cosRotation = cos(this->psi);
double sinRotation = sin(this->psi);

Matrix zRotationaMatrix;
zRotationaMatrix.value[0][0] = cosRotation;
zRotationaMatrix.value[0][1] = sinRotation;
zRotationaMatrix.value[0][2] = 0.0;
zRotationaMatrix.value[1][0] = -sinRotation;
zRotationaMatrix.value[1][1] = cosRotation;
zRotationaMatrix.value[1][2] = 0.0;
zRotationaMatrix.value[2][0] = 0.0;
zRotationaMatrix.value[2][1] = 0.0;
zRotationaMatrix.value[2][2] = 1.0;

return zRotationaMatrix;
}


Copyright (c) 2017 Clinton Kam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

No comments:

Post a Comment