aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/star_control/fvector.cpp
blob: 6ebd297a1f81ff7a43970deb94a1cd9238b46301 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "titanic/star_control/fvector.h"
#include "titanic/star_control/fpose.h"

#include "common/math.h"

namespace Titanic {

FVector FVector::swapComponents() const {
	return FVector(
		(ABS(_x - _y) < 0.00001 && ABS(_y - _z) < 0.00001 &&
			ABS(_x - _z) < 0.00001) ? -_y : _y,
		_z,
		_x
	);
}

FVector FVector::crossProduct(const FVector &src) const {
	return FVector(
		src._z * _y - _z * src._y,
		src._x * _z - _x * src._z,
		src._y * _x - _y * src._x
	);
}

void FVector::rotVectAxisY(float angleDeg) {
	float sinVal = sin(Common::deg2rad<double>(angleDeg));
	float cosVal = cos(Common::deg2rad<double>(angleDeg));
	float x = cosVal * _x - sinVal * _z;
	float z = cosVal * _z + sinVal * _x;

	_x = x;
	_z = z;
}

bool FVector::normalize(float & hyp) {
	hyp = sqrt(_x * _x + _y * _y + _z * _z);
	if (hyp==0) {
		return false;
	}

	_x *= 1.0 / hyp;
	_y *= 1.0 / hyp;
	_z *= 1.0 / hyp;
	return true;
}

FVector FVector::addAndNormalize(const FVector &v) const {
	FVector tempV(_x + v._x, _y + v._y, _z + v._z);

	float unusedScale = 0.0;
	if (!tempV.normalize(unusedScale)) {
		// Do the normalization, put the scale amount in unusedScale,
		// but if it is unsuccessful, crash
		assert(unusedScale);
	}

	return tempV;
}

FVector FVector::getAnglesAsVect() const {
	FVector vector = *this;
	FVector dest;

	if (!vector.normalize(dest._x)) {
		// Makes this vector have magnitude=1, put the scale amount in dest._x,
		// but if it is unsuccessful, crash
		assert(dest._x);
	}

	dest._y = acos(vector._y);	// radian distance/angle that this vector's y component is from the +y axis,
								// result is restricted to [0,pi]
	dest._z = atan2(vector._x,vector._z); // result is restricted to [-pi,pi]

	return dest;
}

float FVector::getDistance(const FVector &src) const {
	float xd = src._x - _x;
	float yd = src._y - _y;
	float zd = src._z - _z;

	return sqrt(xd * xd + yd * yd + zd * zd);
}

FVector FVector::matProdRowVect(const FPose &pose) const {
	FVector v;
	v._x = pose._row2._x * _y + pose._row3._x * _z + pose._row1._x * _x + pose._vector._x;
	v._y = pose._row2._y * _y + pose._row3._y * _z + pose._row1._y * _x + pose._vector._y;
	v._z = pose._row3._z * _z + pose._row2._z * _y + pose._row1._z * _x + pose._vector._z;
	return v;
}

FPose FVector::getFrameTransform(const FVector &v) {
	FPose matrix1, matrix2, matrix3, matrix4;

	FVector vector1 = getAnglesAsVect();
	matrix1.setRotationMatrix(X_AXIS, Common::rad2deg<double>(vector1._y));
	matrix2.setRotationMatrix(Y_AXIS, Common::rad2deg<double>(vector1._z));
	fposeProd(matrix1, matrix2, matrix3);
	matrix4 = matrix3.inverseTransform();

	vector1 = v.getAnglesAsVect();
	matrix1.setRotationMatrix(X_AXIS, Common::rad2deg<double>(vector1._y));
	matrix2.setRotationMatrix(Y_AXIS, Common::rad2deg<double>(vector1._z));
	fposeProd(matrix1, matrix2, matrix3);
	fposeProd(matrix4, matrix3, matrix1);

	return matrix1;
}

FPose FVector::formRotXY() const {
	FVector v1 = getAnglesAsVect();
	FPose m1, m2;
	m1.setRotationMatrix(X_AXIS, Common::rad2deg<double>(v1._y));
	m2.setRotationMatrix(Y_AXIS, Common::rad2deg<double>(v1._z));
	FPose m3;
	fposeProd(m1, m2, m3);
	return m3;
}

Common::String FVector::toString() const {
	return Common::String::format("(%.3f,%.3f,%.3f)", _x, _y, _z);
}

} // End of namespace Titanic