-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.cpp
148 lines (134 loc) · 2.88 KB
/
matrix.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// $Revision: 1.1 $
// matrix.cpp
// (c) 2004-2022 Matthew Arcus
// MIT License
#include <math.h>
#include "matrix.h"
Matrix3::Matrix3()
{
Identity();
}
Matrix3::Matrix3(RotationAxis r, double theta)
{
switch (r) {
case XAxis:
{
RotX(theta);
break;
}
case YAxis:
{
RotY(theta);
break;
}
case ZAxis:
{
RotZ(theta);
break;
}
}
}
Matrix3::Matrix3(const Matrix3& m)
{
Copy(m);
}
void Matrix3::Copy(const Matrix3& m)
{
for (int i = 0; i < 9; i++) {
Set(i, m.Get(i));
}
identity = m.identity;
}
void Matrix3::Identity () {
int i;
for (i = 0; i < 9; i++) {
Set(i,0.0);
}
for (i = 0; i < 9; i+=4){
Set(i,1.0);
}
identity = true;
}
Matrix3 Matrix3::operator* (const Matrix3& m2) const
{
Matrix3 result;
result.Mult(*this, m2);
return result;
}
// Multiply m1 by m2 and put the result in this
void Matrix3::Mult (const Matrix3& m1, const Matrix3& m2)
{
if (m1.identity) {
if (m2.identity) {
Identity();
} else {
Copy(m2);
}
} else if (m2.identity) {
Copy(m1);
} else {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
double x = 0.0;
x += m1.Get(i,0) * m2.Get(0,j);
x += m1.Get(i,1) * m2.Get(1,j);
x += m1.Get(i,2) * m2.Get(2,j);
Set(i,j,x);
}
}
identity = false; // We could check the indices at this point
}
}
// Set m to be a rotation of theta about the x-axis
void Matrix3::RotX (double theta) {
if (theta == 0.0) {
Identity();
} else {
Set(0, 1.0); Set(1, 0.0); Set(2, 0.0);
Set(3, 0.0); Set(4, cos(theta)); Set(5, -sin(theta));
Set(6, 0.0); Set(7, sin(theta)); Set(8, cos(theta));
identity = false;
}
}
// Set m to be a rotation of theta about the y-axis
void Matrix3::RotY (double theta) {
if (theta == 0.0) {
Identity();
} else {
Set(0, cos(theta)); Set(1, 0.0); Set(2, -sin(theta));
Set(3, 0.0); Set(4, 1.0); Set(5, 0.0);
Set(6, sin(theta)); Set(7, 0.0); Set(8, cos(theta));
identity = false;
}
}
// Set m to be a rotation of theta about the z-axis
void Matrix3::RotZ (double theta) {
if (theta == 0.0) {
Identity();
} else {
Set(0, cos(theta)); Set(1, -sin(theta)); Set(2, 0.0);
Set(3, sin(theta)); Set(4, cos(theta)); Set(5, 0.0);
Set(6, 0.0); Set(7, 0.0); Set(8, 1.0);
identity = false;
}
}
void Matrix3::Apply (double& x, double& y, double& z) const
{
if (!identity) {
double x1 = Get(0) * x + Get(1) * y + Get(2) * z;
double y1 = Get(3) * x + Get(4) * y + Get(5) * z;
double z1 = Get(6) * x + Get(7) * y + Get(8) * z;
x = x1; y = y1; z = z1;
}
}
void Matrix3::ApplyLatLong (double& phi, double& lambda) const
{
if (!identity) {
double x = cos(lambda) * cos(phi);
double y = sin(lambda) * cos(phi);
double z = sin(phi);
Apply(x,y,z);
phi = asin(z);
lambda = atan2(y,x);
}
}