-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.hh
244 lines (195 loc) · 6.54 KB
/
util.hh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once
#include <cstdint>
#include <cmath>
#include <string>
#include <vector>
#include <array>
#include <thread>
#include <mutex>
#include <functional>
#include <atomic>
#include <unistd.h>
#include <assert.h>
#include "jansson/jansson.h"
typedef uint32_t uint;
#define FOR(I, N) for(int (I)=0; (I)<(N); (I)++)
#define FOR2(I, A, B) for(auto (I)=A; (I)<=(B); (I)++)
#define GLM_SWIZZLE
#define GLM_FORCE_RADIANS
#include "glm/glm.hpp"
#include "glm/gtc/constants.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "glm/gtc/noise.hpp"
#include "glm/gtc/quaternion.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/random.hpp"
#include "glm/gtx/fast_square_root.hpp"
#include "glm/gtx/intersect.hpp"
#include "glm/gtx/closest_point.hpp"
#include "glm/gtx/norm.hpp"
#include "glm/gtx/extented_min_max.hpp"
// ==========
struct Plucker
{
glm::vec3 d, m;
Plucker() {}
static Plucker points(glm::vec3 a, glm::vec3 b) { return Plucker(b - a, glm::cross(a, b)); }
static Plucker orig_dir(glm::vec3 orig, glm::vec3 dir) { return Plucker(dir, glm::cross(orig, dir)); }
private:
Plucker(glm::vec3 D, glm::vec3 M) : d(D), m(M) { }
};
inline float line_crossing(Plucker a, Plucker b) { return glm::dot(a.d, b.m) + glm::dot(b.d, a.m); }
inline bool opposite_sign_strict(double a, double b) { return a * b < 0; }
inline bool is_unit_length(glm::vec3 a) { return std::abs(glm::length2(a) - 1) <= 5e-7; }
inline glm::vec3 random_vec3() { return glm::vec3(glm::linearRand(0.0f, 1.0f), glm::linearRand(0.0f, 1.0f), glm::linearRand(0.0f, 1.0f)); }
template<typename T> T sqr(T a) { return a * a; }
inline float sqr(glm::vec3 a) { return glm::dot(a, a); }
inline int sqr(glm::ivec3 a) { return glm::dot(a, a); }
inline float distance2_point_and_line(glm::vec3 point, glm::vec3 orig, glm::vec3 dir)
{
assert(is_unit_length(dir));
return glm::distance2(point, orig + dir * glm::dot(point - orig, dir));
}
template<int size>
bool intersects_line_polygon(Plucker line, const Plucker edges[size])
{
FOR(i, size) if (line_crossing(line, edges[i]) > 0) return false;
return true;
}
// ==============
inline glm::mat3 rotate_x(float radians)
{
float c = cos(radians), s = sin(radians);
return glm::mat3(glm::vec3(1, 0, 0), glm::vec3(0, c, s), glm::vec3(0, -s, c));
}
inline glm::mat3 rotate_y(float radians)
{
float c = cos(radians), s = sin(radians);
return glm::mat3(glm::vec3(c, 0, -s), glm::vec3(0, 1, 0), glm::vec3(s, 0, c));
}
inline glm::mat3 rotate_z(float radians)
{
float c = cos(radians), s = sin(radians);
return glm::mat3(glm::vec3(c, s, 0), glm::vec3(-s, c, 0), glm::vec3(0, 0, 1));
}
// ==============
static const glm::ivec3 ix(1, 0, 0), iy(0, 1, 0), iz(0, 0, 1), ii(1, 1, 1);
static const glm::ivec3 ia[3] = { ix, iy, iz };
static const std::array<glm::ivec3, 6> face_dir = { -ix, ix, -iy, iy, -iz, iz };
inline bool between(glm::ivec3 a, glm::ivec3 b, glm::ivec3 c)
{
return a.x <= b.x && b.x <= c.x && a.y <= b.y && b.y <= c.y && a.z <= b.z && b.z <= c.z;
}
// ==============
inline const char* str(glm::ivec3 a)
{
char* result = nullptr;
asprintf(&result, "[%d %d %d]", a.x, a.y, a.z);
return result;
}
inline const char* str(glm::dvec3 a)
{
char* result = nullptr;
asprintf(&result, "[%lf %lf %lf]", a.x, a.y, a.z);
return result;
}
inline const char* str(glm::vec3 a)
{
char* result = nullptr;
asprintf(&result, "[%f %f %f]", a.x, a.y, a.z);
return result;
}
// ===============
float noise(glm::vec2 p, int octaves, float freqf, float ampf, bool turbulent);
float noise(glm::vec3 p, int octaves, float freqf, float ampf, bool turbulent);
// ===============
struct Sphere : public std::vector<glm::ivec3>
{
Sphere(int size)
{
FOR2(x, -size, size) FOR2(y, -size, size) FOR2(z, -size, size)
{
glm::ivec3 d(x, y, z);
if (glm::dot(d, d) <= size * size) push_back(d);
}
std::sort(begin(), end(), [](glm::ivec3 a, glm::ivec3 b) { return glm::length2(a) < glm::length2(b); });
}
};
// ================
struct Frustum
{
Frustum(const glm::mat4& matrix)
{
const float* m = glm::value_ptr(matrix);
glm::vec4 a(m[0], m[4], m[8], m[12]);
glm::vec4 b(m[1], m[5], m[9], m[13]);
glm::vec4 d(m[3], m[7], m[11], m[15]);
m_plane[0] = d - a; // left
m_plane[1] = d + a; // right
m_plane[2] = d + b; // bottom
m_plane[3] = d - b; // top
FOR(i, 4) m_plane[i] *= glm::fastInverseSqrt(sqr(m_plane[i].xyz()));
}
bool contains_point(glm::vec3 p) const
{
FOR(i, 4) if (glm::dot(p, m_plane[i].xyz()) + m_plane[i].w < 0) return false;
return true;
}
bool is_sphere_outside(glm::vec3 p, float radius) const
{
FOR(i, 4) if (glm::dot(p, m_plane[i].xyz()) + m_plane[i].w < -radius) return true;
return false;
}
private:
std::array<glm::vec4, 4> m_plane;
};
// ===============
inline int64_t rdtsc()
{
uint lo, hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return (static_cast<uint64_t>(hi) << 32) | lo;
}
struct Timestamp
{
Timestamp() : m_ticks(rdtsc()) { }
int64_t elapsed(Timestamp a = Timestamp()) { return a.m_ticks - m_ticks; }
double elapsed_ms(Timestamp a = Timestamp()) { return elapsed(a) * milisec_per_tick; }
static void init(glm::dvec3 a, glm::i64vec3 b, glm::dvec3 c, glm::i64vec3 d)
{
glm::dvec3 q = (c - a) / glm::dvec3(d - b);
if (q.x > q.y) std::swap(q.x, q.y);
if (q.y > q.z) std::swap(q.y, q.z);
if (q.x > q.y) std::swap(q.x, q.y);
milisec_per_tick = q.y * 1000;
}
static double milisec_per_tick;
private:
int64_t m_ticks;
};
// =================
#define CHECK2(A, B) do { if (!(A)) { fprintf(stderr, "%s failed at %s line %d. Errno %d (%s)\n", #A, __FILE__, __LINE__, errno, strerror(errno)); B; } } while(0);
#define CHECK(A) CHECK2(A, return false)
void __assert_rtn_format(const char* func, const char* file, int line, const char* cond, const char* fmt, ...);
#ifndef NDEBUG
#define assertf(C, fmt, ...) do { if (!(C)) __assert_rtn_format(__func__, __FILE__, __LINE__, #C, fmt, __VA_ARGS__); } while(0)
#else
#define assertf(C, fmt, ...)
#endif
#define release_assertf(C, fmt, ...) do { if (!(C)) __assert_rtn_format(__func__, __FILE__, __LINE__, #C, fmt, __VA_ARGS__); } while(0)
#define FAIL __assert_rtn_format(__func__, __FILE__, __LINE__, "false", "")
#define AutoLock(A) (A).lock(); Auto((A).unlock());
// ================
#include "city.h"
namespace std
{
template<>
struct hash<glm::ivec3>
{
typedef glm::ivec3 argument_type;
typedef std::size_t result_type;
static_assert(sizeof(glm::ivec3) == 12, "");
result_type operator()(argument_type const& s) const { return CityHash64(reinterpret_cast<const char*>(&s), sizeof(glm::ivec3)); }
};
}
static const glm::ivec3 x_bad_ivec3(0x80000000, 0x80000000, 0x80000000);