-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
83 lines (72 loc) · 1.96 KB
/
vector.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
#include "vector.h"
Vec3f Vec3f::reflect(const Vec3f &I, const Vec3f &J)
{
return I - J*2.f*(I.dot(J));
}
float Vec3f::norm() {
return std::sqrt(this->x * this->x +
this->y * this->y +
this->z * this->z);
}
float Vec3f::dot(const Vec3f & other) const {
#ifdef USE_VU
VecComponents mulResult;
Vec3f &nonConstThis = const_cast<Vec3f &>(*this);
asm __volatile__ (
"lqc2 vf1, 0(%1)\n"
"lqc2 vf2, 0(%2)\n"
"vmul.xyzw vf3, vf1, vf2\n"
"sqc2 vf3, 0(%0)\n"
: "=r" (mulResult) : "r" (nonConstThis.vector), "r" (other.vector));
return mulResult.x + mulResult.y + mulResult.z;
#endif
return this->x * other.x +
this->y * other.y +
this->z * other.z;
}
Vec3f Vec3f::operator*(float k) const
{
return Vec3f(this->x * k,
this->y * k,
this->z * k);
}
Vec3f &Vec3f::operator=(const Vec3f & other)
{
vector.x = other.vector.x;
vector.y = other.vector.y;
vector.z = other.vector.z;
vector.w = other.vector.w;
return *this;
}
Vec3f Vec3f::normalize() {
return (*this) * (1/this->norm());
}
float Vec3f::operator[](int index)
{
if (index == 0) return vector.x;
if (index == 1) return vector.y;
if (index == 2) return vector.z;
if (index == 3) return vector.w;
return -0.0f;
}
#ifdef _EE
uint32_t Vec3f::asGsColor()
{
unsigned char r = static_cast<unsigned char>(255.0f * std::max(.0f, std::min(1.f, x)));
unsigned char g = static_cast<unsigned char>(255.0f * std::max(.0f, std::min(1.f, y)));
unsigned char b = static_cast<unsigned char>(255.0f * std::max(.0f, std::min(1.f, z)));
return GS_SETREG_RGBA(r, g, b, 0xFF);
}
#endif
Vec3f operator+(const Vec3f& a, const Vec3f& b)
{
return Vec3f(a.x + b.x,
a.y + b.y,
a.z + b.z);
}
Vec3f operator-(const Vec3f& a, const Vec3f& b)
{
return Vec3f(a.x - b.x,
a.y - b.y,
a.z - b.z);
}