/
common.h
174 lines (138 loc) · 5.25 KB
/
common.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
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
171
172
173
174
#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <math.h>
#include <assert.h>
using std::cerr;
using std::cout;
using std::endl;
#define SAFE_DELETE(x) if((x)) { delete (x); (x) = NULL; }
#define MAX(x, y) (x) > (y) ? (x) : (y)
#define MIN(x, y) (x) < (y) ? (x) : (y)
#define PI 3.14159265358979323846
#define DRAW_GLQUAD \
glBegin(GL_QUADS); \
glTexCoord2f(0.0f,1.0f); \
glVertex2f(0.0f,0.0f); \
glTexCoord2f(1.0f,1.0f); \
glVertex2f(w,0.0f); \
glTexCoord2f(1.0f,0.0f); \
glVertex2f(w,h); \
glTexCoord2f(0.0f,0.0f); \
glVertex2f(0.0f,h); \
glEnd(); \
struct float3 {
float3(float v0 = 0, float v1 = 0, float v2 = 0) : x(v0), y(v1), z(v2) { }
float3(float *data) { x = data[0]; y = data[1]; z = data[2]; }
static inline float3 zero() { return float3(0,0,0); }
static inline float3 one() { return float3(1,1,1); }
#define VECOP_PCW(op) { x op rhs.x; y op rhs.y; z op rhs.z; return *this; }
#define VECOP_SCA(op) { x op rhs; y op rhs ; z op rhs ; return *this; }
inline float3& operator = (const float3& rhs) VECOP_PCW( =) /// equality assignment
inline float3& operator += (const float3& rhs) VECOP_PCW(+=) /// piecewise addition operator
inline float3& operator -= (const float3& rhs) VECOP_PCW(-=) /// piecewise subtraction operator
inline float3& operator *= (const float3& rhs) VECOP_PCW(*=) /// piecewise mult operator
inline float3& operator /= (const float3& rhs) VECOP_PCW(/=) /// piecewise div operator
inline float3 operator + (const float3& rhs) const { return float3(*this) += rhs; } /// piecewise addition
inline float3 operator - (const float3& rhs) const { return float3(*this) -= rhs; } /// piecewise subtraction
inline float3 operator * (const float3& rhs) const { return float3(*this) *= rhs; } /// piecewise mult
inline float3 operator / (const float3& rhs) const { return float3(*this) /= rhs; } /// piecewise div
inline float3& operator += (const float rhs) VECOP_SCA(+=) /// scalar addition operator
inline float3& operator -= (const float rhs) VECOP_SCA(-=) /// scalar subtraction operator
inline float3& operator *= (const float rhs) VECOP_SCA(*=) /// scalar multiplication operator
inline float3& operator /= (const float rhs) VECOP_SCA(/=) /// scalar division operator
inline float3 operator + (const float rhs) const { return float3(*this) += rhs; } /// piecewise addition
inline float3 operator - (const float rhs) const { return float3(*this) -= rhs; } /// piecewise subtraction
inline float3 operator * (const float rhs) const { return float3(*this) *= rhs; } /// piecewise multiplication
inline float3 operator / (const float rhs) const { return float3(*this) /= rhs; } /// piecewise multiplication
#undef VECOP_PCW
#undef VECOP_SCA
inline float dot(const float3 &rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
inline float normalize() {
float m = getMagnitude();
x /= m, y /= m, z /= m;
return m;
}
inline float3 getNormalized() {
float m = getMagnitude();
return float3(x / m, y / m, z / m);
}
inline float getMagnitude() const {
return sqrt(getMagnitude2());
}
inline float getMagnitude2() const {
return x * x + y * y + z * z;
}
inline float getDistance(const float3 &rhs) const {
return sqrt(getDistance2(rhs));
}
inline float getDistance2(const float3 &rhs) const {
return (rhs.x - x) * (rhs.x - x) + (rhs.y - y) * (rhs.y - y) +
(rhs.z - z) * (rhs.z - z);
}
inline float3 cross(const float3& rhs) const {
return float3(data[1] * rhs.data[2] - data[2] * rhs.data[1],
data[2] * rhs.data[0] - data[0] * rhs.data[2],
data[0] * rhs.data[1] - data[1] * rhs.data[0]);
}
/// rotate around y axis
inline float3 rotateY(float theta) {
return float3(z*sinf(theta)+x*cosf(theta),
y,
z*cosf(theta)-x*sinf(theta));
}
/// rotate around x axis
inline float3 rotateX(float theta) {
return float3(x,
y*cosf(theta)-z*sinf(theta),
y*sinf(theta)+z*cosf(theta));
}
inline bool operator==(const float3 &rhs) {
return (x == rhs.x && y == rhs.y && z == rhs.z);
}
inline bool operator!=(const float3 &rhs) {
return (x != rhs.x || y != rhs.y || z != rhs.z);
}
union {
struct {
float x, y, z;
};
struct {
float r, g, b;
};
float data[3];
};
};
inline static float dot(const float3 &v1, const float3 &v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
inline float3 operator*(const float scale, const float3 &rhs) {
return float3(rhs.x * scale, rhs.y * scale, rhs.z * scale);
}
inline float3 operator/(const float scale, const float3 &rhs) {
return float3( scale / rhs.x , scale / rhs.y , scale / rhs.z);
}
inline float3 operator-(const float3 &rhs) {
return float3(-rhs.x, -rhs.y, -rhs.z);
}
inline std::ostream& operator<<(std::ostream& os, const float3& f) {
os <<"[";
for (unsigned i = 0; i < 3; ++i) {
os << f.data[i] << ",";
}
os << "]";
return os;
}
struct float2 {
union {
struct {
float x, y;
};
float data[2];
};
float2(float v0 = 0, float v1 = 0) : x(v0), y(v1){ }
float2(float *data) { x = data[0]; y = data[1]; }
};
#endif