-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmath_tools.h
142 lines (113 loc) · 4.87 KB
/
math_tools.h
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
#ifndef MATH_TOOLS_H
#define MATH_TOOLS_H
#include <iostream>
#include <cstdlib>
#include <cmath>
#define pi 3.14159265359
#define rand_factor 1.0 / RAND_MAX
struct int3
{
int3() {}
int3(int x, int y, int z) : x(x), y(y), z(z) {}
int3(int i) : x(i), y(i), z(i) {}
int3(const int* p) { x = p[0]; y = p[1]; z = p[2]; }
union { struct { int x, y, z; }; int data[3]; };
friend std::ostream& operator<<(std::ostream& os, const int3 &i);
};
struct float2
{
float2() {}
float2(float s) : x(s), y(s) {}
float2(float x, float y) : x(x), y(y) {}
float2(const float* p) { x = p[0]; y = p[1]; }
union { struct { float x; float y; }; float data[2]; };
};
struct float3
{
float3() {}
float3(float s) : x(s), y(s), z(s) {}
float3(float x, float y, float z) : x(x), y(y), z(z) {}
float3(const float* p) { x = p[0]; y = p[1]; z = p[2]; }
union { struct { float x; float y; float z; }; float data[3]; };
//float x, y, z;
inline float3 operator+(float s) const { return float3(s + x, s + y, s + z); }
inline float3 operator-(float s) const { return float3(s - x, s - y, s - z); }
inline float3 operator*(float s) const { return float3(s * x, s * y, s * z); }
inline float3 operator/(float s) const { return float3(x / s, y / s, z / s); }
inline float3 operator^(float s) const { return float3(std::pow(x, s), std::pow(y, s), std::pow(z, s)); }
inline float3 operator+(const float3& f) const { return float3(x + f.x, y + f.y, z + f.z); }
inline float3 operator-(const float3& f) const { return float3(x - f.x, y - f.y, z - f.z); }
inline float3 operator*(const float3& f) const { return float3(x * f.x, y * f.y, z * f.z); }
inline float3 operator/(const float3& f) const { return float3(x / f.x, y / f.y, z / f.z); }
inline float3& operator+=(const float3& f) { x += f.x; y += f.y; z += f.z; return *this; }
inline float3& operator-=(const float3& f) { x -= f.x; y -= f.y; z -= f.z; return *this; }
inline float3& operator*=(const float3& f) { x *= f.x; y *= f.y; z *= f.z; return *this; }
inline float3& operator/=(const float3& f) { x /= f.x; y /= f.y; z /= f.z; return *this; }
inline float3& operator+=(const float f) { x += f; y += f; z += f; return *this; }
inline float3& operator-=(const float f) { x -= f; y -= f; z -= f; return *this; }
inline float3& operator*=(const float f) { x *= f; y *= f; z *= f; return *this; }
inline float3& operator/=(const float f) { x /= f; y /= f; z /= f; return *this; }
inline float dot(const float3& f) const { return f.x * x + f.y * y + f.z * z; }
inline float3 cross(const float3& f) const { return float3(y * f.z - z * f.y, z * f.x - x * f.z, x * f.y - y * f.x); }
inline float squared_norm(void) const { return dot(*this); }
inline float norm(void) const { return std::sqrt(this->squared_norm()); }
inline void normalize(void) { float s = this->norm(); x /= s; y /= s; z /= s; }
inline float3 normalized(void) const { float s = this->norm(); return *this / s; }
friend std::ostream& operator<<(std::ostream& os, const float3 &f);
};
inline float3 max(float3 lhs, float3 rhs) { return float3(std::max(lhs.x, rhs.x), std::max(lhs.y, rhs.y), std::max(lhs.z, rhs.z)); }
inline float3 min(float3 lhs, float3 rhs) { return float3(std::min(lhs.x, rhs.x), std::min(lhs.y, rhs.y), std::min(lhs.z, rhs.z)); }
template<typename T>
inline void swap(T& a, T& b) { T t = a; a = b; b = t; }
struct mat3f
{
mat3f() {}
mat3f(float s) { for (int i = 0; i < 9; ++i) { data[i] = s; }; }
float data[9]; //row major
inline float& operator()(int i, int j)
{
return data[3 * i + j];
}
inline mat3f operator+(const mat3f& m) {
mat3f out;
for (int i = 0; i < 9; ++i)
out.data[i] = data[i] + m.data[i];
return out;
}
inline mat3f operator*(const mat3f& m) {
mat3f out;
for (int i = 0; i < 9; ++i)
out.data[i] = data[i] * m.data[i];
return out;
}
inline mat3f operator+(float s) {
mat3f out;
for (int i = 0; i < 9; ++i)
out.data[i] = data[i] + s;
return out;
}
inline mat3f operator*(float s) {
mat3f out;
for (int i = 0; i < 9; ++i)
out.data[i] = data[i] * s;
return out;
}
inline float3 dot(float3& f) {
float3 out(0.0f);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
out.data[i] += data[3 * i + j] * f.data[j];
return out;
}
// inline mat3f dot(const mat3f& m) {
// mat3f out(0.0f);
// for (int i = 0; i < 3; ++i)
// for (int j = 0; j < 3; ++j)
// for (int k = 0; k < 3; ++k)
// out.data[3 * i + j] = data[3 * i + k] * m.data[3 * k + j];
// return out;
// }
};
mat3f eye(void);
float wrap(const float f);
#endif