Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 4d752bfa2607d9ab561f38fac5b8849d549efabc (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
/*
   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);
}

/*
Axis const Axis::transpose()
{
	Axis` t;

	for (size_t i = 0; i < 3; i++)
		for (size_t j = 0; j < 3; j++)
			t.axis_vector[j][i] = 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]));
}

}