2012-03-09 22:44:36 -04:00
|
|
|
/// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
|
|
|
|
/*
|
|
|
|
* vector3.cpp
|
|
|
|
* Copyright (C) Andrew Tridgell 2012
|
|
|
|
*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AP_Math.h"
|
|
|
|
|
|
|
|
#define HALF_SQRT_2 0.70710678118654757
|
|
|
|
|
|
|
|
// rotate a vector by a standard rotation, attempting
|
|
|
|
// to use the minimum number of floating point operations
|
|
|
|
template <typename T>
|
|
|
|
void Vector3<T>::rotate(enum Rotation rotation)
|
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
switch (rotation) {
|
|
|
|
case ROTATION_NONE:
|
2012-03-11 09:17:05 -03:00
|
|
|
case ROTATION_MAX:
|
2012-03-09 22:44:36 -04:00
|
|
|
return;
|
|
|
|
case ROTATION_YAW_45: {
|
|
|
|
tmp = HALF_SQRT_2*(x - y);
|
|
|
|
y = HALF_SQRT_2*(x + y);
|
|
|
|
x = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_YAW_90: {
|
|
|
|
tmp = x; x = -y; y = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_YAW_135: {
|
|
|
|
tmp = -HALF_SQRT_2*(x + y);
|
|
|
|
y = HALF_SQRT_2*(x - y);
|
|
|
|
x = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_YAW_180:
|
|
|
|
x = -x; y = -y;
|
|
|
|
return;
|
|
|
|
case ROTATION_YAW_225: {
|
|
|
|
tmp = HALF_SQRT_2*(y - x);
|
|
|
|
y = -HALF_SQRT_2*(x + y);
|
|
|
|
x = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_YAW_270: {
|
|
|
|
tmp = x; x = y; y = -tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_YAW_315: {
|
|
|
|
tmp = HALF_SQRT_2*(x + y);
|
|
|
|
y = HALF_SQRT_2*(y - x);
|
|
|
|
x = tmp;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180: {
|
|
|
|
y = -y; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_45: {
|
|
|
|
tmp = HALF_SQRT_2*(x + y);
|
|
|
|
y = HALF_SQRT_2*(x - y);
|
|
|
|
x = tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_90: {
|
|
|
|
tmp = x; x = y; y = tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_135: {
|
|
|
|
tmp = HALF_SQRT_2*(y - x);
|
2012-08-17 03:20:14 -03:00
|
|
|
y = HALF_SQRT_2*(y + x);
|
2012-03-09 22:44:36 -04:00
|
|
|
x = tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_PITCH_180: {
|
|
|
|
x = -x; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_225: {
|
|
|
|
tmp = -HALF_SQRT_2*(x + y);
|
2012-08-17 03:20:14 -03:00
|
|
|
y = HALF_SQRT_2*(y - x);
|
2012-03-09 22:44:36 -04:00
|
|
|
x = tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_270: {
|
|
|
|
tmp = x; x = -y; y = -tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case ROTATION_ROLL_180_YAW_315: {
|
|
|
|
tmp = HALF_SQRT_2*(x - y);
|
2012-08-17 03:20:14 -03:00
|
|
|
y = -HALF_SQRT_2*(x + y);
|
2012-03-09 22:44:36 -04:00
|
|
|
x = tmp; z = -z;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 00:59:06 -03:00
|
|
|
// vector cross product
|
2012-03-22 07:12:36 -03:00
|
|
|
template <typename T>
|
|
|
|
Vector3<T> Vector3<T>::operator %(const Vector3<T> &v) const
|
|
|
|
{
|
|
|
|
Vector3<T> temp(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dot product
|
|
|
|
template <typename T>
|
|
|
|
T Vector3<T>::operator *(const Vector3<T> &v) const
|
2012-08-17 03:20:14 -03:00
|
|
|
{
|
|
|
|
return x*v.x + y*v.y + z*v.z;
|
|
|
|
}
|
2012-03-22 07:12:36 -03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
float Vector3<T>::length(void) const
|
|
|
|
{
|
|
|
|
return (T)sqrt(*this * *this);
|
|
|
|
}
|
|
|
|
|
2012-03-09 22:44:36 -04:00
|
|
|
// only define for signed numbers
|
|
|
|
template void Vector3<float>::rotate(enum Rotation);
|
2012-03-22 07:12:36 -03:00
|
|
|
template float Vector3<float>::length(void) const;
|
|
|
|
template Vector3<float> Vector3<float>::operator %(const Vector3<float> &v) const;
|
|
|
|
template float Vector3<float>::operator *(const Vector3<float> &v) const;
|