File: vector3d.h

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (144 lines) | stat: -rw-r--r-- 3,100 bytes parent folder | download | duplicates (3)
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
#include <cxxtest/TestSuite.h>

#include "math/vector3d.h"

class Vector3dTestSuite : public CxxTest::TestSuite {
public:
	// Test Constructors
	void test_Vector3d() {
		Math::Vector3d v;

		TS_ASSERT(v.x() == 0.0f);
		TS_ASSERT(v.y() == 0.0f);
		TS_ASSERT(v.z() == 0.0f);

		Math::Vector3d v2(3, 2.2, 1);

		TS_ASSERT(v2.x() == 3.0f);
		TS_ASSERT(v2.y() == 2.2f);
		TS_ASSERT(v2.z() == 1.0f);

		Math::Vector3d v3(v2);

		TS_ASSERT(v3.x() == 3.0f);
		TS_ASSERT(v3.y() == 2.2f);
		TS_ASSERT(v3.z() == 1.0f);
	}

	void test_set() {
		Math::Vector3d v(2, 4, 6);
		v.set(1, 2, 3);

		TS_ASSERT(v.x() == 1.0f);
		TS_ASSERT(v.y() == 2.0f);
		TS_ASSERT(v.z() == 3.0f);
	}

	void test_crossProduct() {
		Math::Vector3d v1(1, 0, 0);
		Math::Vector3d v2(0, 1, 0);

		Math::Vector3d c12 = Math::Vector3d::crossProduct(v1, v2);
		Math::Vector3d c21 = Math::Vector3d::crossProduct(v2, v1);

		TS_ASSERT(c12 == Math::Vector3d(0, 0, 1));
		TS_ASSERT(c21 == Math::Vector3d(0, 0, -1));
	}

	void test_length() {
		Math::Vector3d v(1, 0, 1);
		TS_ASSERT_DELTA(v.length(), sqrt(2), 0.0001);

		Math::Vector3d v2(2, 2, 2);
		TS_ASSERT_DELTA(v2.length(), sqrt(12), 0.0001);

		// check static version too
		TS_ASSERT_DELTA(Math::Vector3d::length(v2), v2.length(), 0.0001);
	}

	void test_interpolate() {
		Math::Vector3d v1(1, 0, 2);
		Math::Vector3d v2(0, 5, 3);

		Math::Vector3d int1 = Math::Vector3d::interpolate(v1, v2, 0.1);
		TS_ASSERT_DELTA(int1.x(), 0.9, 0.0001);
		TS_ASSERT_DELTA(int1.y(), 0.5, 0.0001);
		TS_ASSERT_DELTA(int1.z(), 2.1, 0.0001);
	}

	/* all the below tests are for functions in Math::Vector, but add tests
	 * here as we can't directly instantiate that */

	void test_setValue() {
		Math::Vector3d v(3, 7, 9);

		v.setValue(0, 1);
		TS_ASSERT(v.x() == 1.0f);
		TS_ASSERT(v.y() == 7.0f);
		TS_ASSERT(v.z() == 9.0f);

		v.setValue(1, 3);
		v.setValue(2, 2);
		TS_ASSERT(v.x() == 1.0f);
		TS_ASSERT(v.y() == 3.0f);
		TS_ASSERT(v.z() == 2.0f);
	}

	void test_getValue() {
		Math::Vector3d v(5, 6, 7);

		TS_ASSERT(v.getValue(0) == 5.0f);
		TS_ASSERT(v.getValue(1) == 6.0f);
		TS_ASSERT(v.getValue(2) == 7.0f);
	}

	void test_dotProduct() {
		Math::Vector3d v1(1, 2, 3);
		Math::Vector3d v2(6, 5, 4);

		float result = v2.dotProduct(v1);

		TS_ASSERT_DELTA(result, 28.0f, 0.0001f);
	}

	void test_dotProductOrthogonal() {
		Math::Vector3d v1(6, 0, 0);
		Math::Vector3d v2(0, 9, 0);

		float result = v2.dotProduct(v1);

		TS_ASSERT_EQUALS(result, 0.0f);
	}

	void test_normalizeTrivial() {
		Math::Vector3d v(0, 1, 0);

		Math::Vector3d vn = v.getNormalized();

		v.normalize();

		TS_ASSERT_EQUALS(vn, v);
		TS_ASSERT(v.x() == 0);
		TS_ASSERT(v.y() == 1);
		TS_ASSERT(v.z() == 0);
	}

	void test_normalize() {
		Math::Vector3d v(2, 4, 6);

		Math::Vector3d vn = v.getNormalized();
		v.normalize();

		TS_ASSERT_EQUALS(vn, v);
		TS_ASSERT_DELTA(v.x(), 2 / sqrt(56), 0.0001);
		TS_ASSERT_DELTA(v.y(), 4 / sqrt(56), 0.0001);
		TS_ASSERT_DELTA(v.z(), 6 / sqrt(56), 0.0001);
	}

	void test_magnitude() {
		Math::Vector3d v(3, 2.5, 1);

		TS_ASSERT_DELTA(v.getMagnitude(), sqrt(16.25), 0.0001);
	}

};