-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.hpp
86 lines (71 loc) · 2.92 KB
/
vector.hpp
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
#pragma once
#include <iostream>
#include <cmath>
#include <algorithm>
// we use custom Vector3f and Vector2f instead of Eigen because of the speed
class Vector3f {
public:
float x, y, z;
Vector3f() : x(0), y(0), z(0) {}
Vector3f(float xx) : x(xx), y(xx), z(xx) {}
Vector3f(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
Vector3f operator * (const float &r) const { return Vector3f(x * r, y * r, z * r); }
Vector3f operator / (const float &r) const { return Vector3f(x / r, y / r, z / r); }
float norm() const {return std::sqrt(x * x + y * y + z * z);}
Vector3f normalized() {
float n = std::sqrt(x * x + y * y + z * z);
return Vector3f(x / n, y / n, z / n);
}
Vector3f operator * (const Vector3f &v) const { return Vector3f(x * v.x, y * v.y, z * v.z); }
Vector3f operator - (const Vector3f &v) const { return Vector3f(x - v.x, y - v.y, z - v.z); }
Vector3f operator + (const Vector3f &v) const { return Vector3f(x + v.x, y + v.y, z + v.z); }
Vector3f operator - () const { return Vector3f(-x, -y, -z); }
Vector3f& operator += (const Vector3f &v) { x += v.x, y += v.y, z += v.z; return *this; }
friend Vector3f operator * (const float &r, const Vector3f &v)
{ return Vector3f(v.x * r, v.y * r, v.z * r); }
friend std::ostream & operator << (std::ostream &os, const Vector3f &v)
{ return os << v.x << ", " << v.y << ", " << v.z; }
double operator[](int index) const;
double& operator[](int index);
static Vector3f Min(const Vector3f &p1, const Vector3f &p2) {
return Vector3f(std::min(p1.x, p2.x), std::min(p1.y, p2.y),
std::min(p1.z, p2.z));
}
static Vector3f Max(const Vector3f &p1, const Vector3f &p2) {
return Vector3f(std::max(p1.x, p2.x), std::max(p1.y, p2.y),
std::max(p1.z, p2.z));
}
};
inline double Vector3f::operator[](int index) const {
return (&x)[index];
}
class Vector2f {
public:
Vector2f() : x(0), y(0) {}
Vector2f(float xx) : x(xx), y(xx) {}
Vector2f(float xx, float yy) : x(xx), y(yy) {}
Vector2f operator * (const float &r) const { return Vector2f(x * r, y * r); }
Vector2f operator + (const Vector2f &v) const { return Vector2f(x + v.x, y + v.y); }
float x, y;
};
inline Vector3f lerp(const Vector3f &a, const Vector3f& b, const float &t)
{ return a * (1 - t) + b * t; }
inline Vector3f normalize(const Vector3f &v)
{
float mag2 = v.x * v.x + v.y * v.y + v.z * v.z;
if (mag2 > 0) {
float invMag = 1 / sqrtf(mag2);
return Vector3f(v.x * invMag, v.y * invMag, v.z * invMag);
}
return v;
}
inline float dotProduct(const Vector3f &a, const Vector3f &b)
{ return a.x * b.x + a.y * b.y + a.z * b.z; }
inline Vector3f crossProduct(const Vector3f &a, const Vector3f &b)
{
return Vector3f(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}