blob: 16c0cfb7b52aa0d6af87c2de4d305cac64f2474d (
plain)
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
|
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <daw/logging.h>
#include <daw/utils.h>
/* These should all be in some external facing module "tools" */
f32 lerp(f32 dt, f32 a, f32 b) { return (a * (1.0f - dt)) + (b * dt); }
i32 int_lerp(f32 dt, i32 a, i32 b) {
return (i32)((f32)a * (1.0f - dt)) + (i32)((f32)b * dt);
}
u32 hash(char* str) {
u32 sum = 0;
while (*str != '\0') {
sum ^= (u32)(*str) * 0xdeece66d + 0xb;
str++;
}
return sum;
}
/* Populates dstmap
* on success: return pointer to dstmap
* on failure: return NULL */
i32* kernmap(const void* map, i32* dstmap, const ivec2 mapsize,
predicate_t* predicate) {
const usize w = (usize)mapsize[0];
const usize h = (usize)mapsize[1];
i32 mask[w * h];
if (w * h < 1) return NULL;
for (usize i = 0; i < w * h; i++) {
mask[i] = predicate((void*)((u64)map + sizeof(i32) * i)) ? 1 : 0;
}
for (usize y = 1; y < h - 1; y++) {
for (usize x = 1; x < w - 1; x++) {
const usize global_idx = (y * w) + x;
const usize offs = global_idx - w - 1;
i32 _sum = 0;
i32 shift = 0;
/* We go in the following order */
/* ....|0|1|2|....*/
/* ....|3|4|5|....*/
/* ....|6|7|8|....*/
/* Where `4` is in the center, MASK_C */
for (usize yy = offs; yy <= offs + w + w; yy += w) {
for (usize xx = yy; xx < yy + 3; xx++) {
_sum = _sum | (mask[xx] << shift++);
}
}
dstmap[global_idx] = _sum;
}
}
return dstmap;
}
/* Returns an index from the given weights. */
i32 pick_from_sample(const i32* weights, i32 len) {
if (len <= 0) return 0;
/* Cumulative sum */
i32 cumweights[len];
i32 sum = 0;
for (i32 i = 0; i < len; i++) {
sum += weights[i];
cumweights[i] = sum;
}
if (sum == 0) return 0;
i32 pick = rand() % sum;
for (i32 i = 0; i < len; i++) {
if (pick < cumweights[i]) return i;
}
return -1;
}
|