summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Software/Vec3.h
blob: a5857ffbd67e9202a33e6cb656188cd96a6312f3 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <cmath>
#include <cstdlib>

#include "Common/ChunkFile.h"

class Vec3
{
public:
	float x, y, z;

	Vec3()
	{
	}

	explicit Vec3(float f)
	{
		x = y = z = f;
	}

	explicit Vec3(const float *f)
	{
		x = f[0];
		y = f[1];
		z = f[2];
	}

	Vec3(const float _x, const float _y, const float _z)
	{
		x = _x;
		y = _y;
		z = _z;
	}

	void set(const float _x, const float _y, const float _z)
	{
		x = _x;
		y = _y;
		z = _z;
	}

	Vec3 operator+(const Vec3 &other) const
	{
		return Vec3(x + other.x, y + other.y, z + other.z);
	}

	void operator+=(const Vec3 &other)
	{
		x += other.x;
		y += other.y;
		z += other.z;
	}

	Vec3 operator-(const Vec3 &v) const
	{
		return Vec3(x - v.x, y - v.y, z - v.z);
	}

	void operator-=(const Vec3 &other)
	{
		x -= other.x;
		y -= other.y;
		z -= other.z;
	}

	Vec3 operator-() const
	{
		return Vec3(-x, -y, -z);
	}

	Vec3 operator*(const float f) const
	{
		return Vec3(x * f, y * f, z * f);
	}

	Vec3 operator/(const float f) const
	{
		float invf = (1.0f / f);
		return Vec3(x * invf, y * invf, z * invf);
	}

	void operator/=(const float f)
	{
		*this = *this / f;
	}

	float operator*(const Vec3 &other) const
	{
		return (x * other.x) +
		       (y * other.y) +
		       (z * other.z);
	}

	void operator*=(const float f)
	{
		*this = *this * f;
	}

	Vec3 ScaledBy(const Vec3 &other) const
	{
		return Vec3(x * other.x, y * other.y, z * other.z);
	}

	Vec3 operator%(const Vec3 &v) const
	{
		return Vec3((y * v.z) - (z * v.y),
		            (z * v.x) - (x * v.z),
		            (x * v.y) - (y * v.x));
	}

	float Length2() const
	{
		return (x * x) + (y * y) + (z * z);
	}

	float Length() const
	{
		return sqrtf(Length2());
	}

	float Distance2To(Vec3 &other)
	{
		return (other - (*this)).Length2();
	}

	Vec3 Normalized() const
	{
		return (*this) / Length();
	}

	void Normalize()
	{
		(*this) /= Length();
	}

	float &operator[](int i)
	{
		return *((&x) + i);
	}

	float operator[](const int i) const
	{
		return *((&x) + i);
	}

	bool operator==(const Vec3 &other) const
	{
		if (x == other.x && y == other.y && z == other.z)
			return true;
		else
			return false;
	}

	void SetZero()
	{
		memset((void*)this, 0, sizeof(float) * 3);
	}

	void DoState(PointerWrap &p)
	{
		p.Do(x);
		p.Do(y);
		p.Do(z);
	}
};