Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: e6442eb5d382ed6a2e4ae3aca25cbbefd266be2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
   math/axis.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

// project headers
#include "math/axis.h"
#include "math/mathlib.h"

namespace math
{

Axis::Axis()
{
	clear();
}

Axis::Axis(const Axis & other) {
	assign(other);
}

void Axis::clear()
{
	axis_vector[0] = Vector3f(1.0f, 0.0f, 0.0f);
	axis_vector[1] = Vector3f(0.0f, 1.0f, 0.0f);
	axis_vector[2] = Vector3f(0.0f, 0.0f, 1.0f);
}

void Axis::assign(const Axis & other) {
	for (size_t i=0; i < 3; i++) {
		axis_vector[i].assign(other.axis_vector[i]);
	}
}

Axis & Axis::operator=(const Axis & other) {
	assign(other);
	return *this;
}

// change heading, rotate around Z-axis (positive is left)
void Axis::change_direction(const float angle) {
	float cosa = cosf(angle * M_PI / 180.0f);
	float sina = sinf(angle * M_PI / 180.0f);

	Vector3f forward = axis_vector[0] * cosa + axis_vector[1] * sina;
	Vector3f left = axis_vector[1] *cosa - axis_vector[0] * sina;
	
	axis_vector[0].assign(forward);
	axis_vector[1].assign(left);
}

// change pitch, rotate around negative Y-axis (positive is up)
void Axis::change_pitch(const float angle) {
	float cosa = cosf(angle * M_PI / 180.0f);
	float sina = sinf(angle * M_PI / 180.0f);

	Vector3f forward = axis_vector[0] * cosa + axis_vector[2] * sina;
	Vector3f up = axis_vector[2] * cosa - axis_vector[0] * sina;

	axis_vector[0].assign(forward);
	axis_vector[2].assign(up);
}

// change roll, rotate around forward vector (positive is left)
void Axis::change_roll(const float angle) {
	float cosa = cosf(angle * M_PI / 180.0f);
	float sina = sinf(angle * M_PI / 180.0f);

	Vector3f forward = axis_vector[2] * cosa + axis_vector[1] * sina;
	Vector3f up = axis_vector[1] * cosa - axis_vector[2] * sina;

	axis_vector[2].assign(forward);
	axis_vector[1].assign(up);
}

// perform a rotation about an arbitrary axis
/* notes:
	http://mathworld.wolfram.com/RotationFormula.html
*/
void Axis::rotate(Vector3f const &normal, float angle)
{
	float cosa = cosf(angle);
	float sina = sinf(angle);

	for (size_t i =0; i < 3; i++) {
		axis_vector[i] = 
			axis_vector[i]*cosa + 
			normal * dotproduct(normal, axis_vector[i]) * (1 - cosa) + 
			crossproduct(axis_vector[i], normal)*sina;
	}
}

Axis Axis::transpose() const
{
	Axis t;

	for (size_t i = 0; i < 3; i++)
		for (size_t j = 0; j < 3; j++)
			t.axis_vector[i][j] = axis_vector[j][i];
	return t;
}

// write an axis to a std::ostream
std::ostream &operator<<(std::ostream & os, Axis const & axis)
{
	os << axis.forward() << " ";
	os << axis.left() << " ";
	os << axis.up();
	return os;
}

// read an axis from a std::istream
std::istream &operator>>(std::istream & is, Axis & axis)
{
	is >> axis[0];
	is >> axis[1];
	is >> axis[2];
	return is;
}

// local-to-global coordinates
Vector3f operator*(Axis const &axis, Vector3f const &vector)
{
	return (Vector3f(vector[0] * axis[0] + vector[1] * axis[1] + vector[2] * axis[2]));
}

}