summaryrefslogtreecommitdiff
path: root/src/modules/flow/ff.c
blob: 98ac59783dae8aac20d86cab65f17d5a9b296812 (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
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "ff.h"
#include "v3f.h"

typedef struct ff_t {
	unsigned	size;
	ff_data_t	*fields[2];
	void		(*populator)(void *context, unsigned size, const ff_data_t *other, ff_data_t *field);
	void		*populator_context;
} ff_t;


/* populate the flow field specified by idx */
void ff_populate(ff_t *ff, unsigned idx)
{
	unsigned	other;

	assert(idx < 2);

	other = (idx + 1) % 2;

	ff->populator(ff->populator_context, ff->size, ff->fields[other], ff->fields[idx]);
}


ff_t * ff_free(ff_t *ff)
{
	if (ff) {
		for (int i = 0; i < 2; i++)
			free(ff->fields[i]);

		free(ff);
	}

	return NULL;
}


ff_t * ff_new(unsigned size, void (*populator)(void *context, unsigned size, const ff_data_t *other, ff_data_t *field), void *context)
{
	ff_t	*ff;

	ff = calloc(1, sizeof(ff_t));
	if (!ff)
		return NULL;

	for (int i = 0; i < 2; i++) {
		ff->fields[i] = calloc(size * size * size, sizeof(ff_data_t));
		if (!ff->fields[i])
			return ff_free(ff);
	}

	ff->size = size;
	ff->populator = populator;
	ff->populator_context = context;

	for (unsigned i = 0; i < 2; i++)
		ff_populate(ff, i);

	return ff;
}


static inline ff_data_t ff_sample(ff_data_t *field, size_t size, v3f_t *min, v3f_t *max, v3f_t *t)
{
	ff_data_t	*a, *b, *c, *d, *e, *f, *g, *h;
	size_t		ss = size * size;
	ff_data_t	res;

	a = &field[(size_t)min->x * ss + (size_t)max->y * size + (size_t)min->z];
	b = &field[(size_t)max->x * ss + (size_t)max->y * size + (size_t)min->z];
	c = &field[(size_t)min->x * ss + (size_t)min->y * size + (size_t)min->z];
	d = &field[(size_t)max->x * ss + (size_t)min->y * size + (size_t)min->z];
	e = &field[(size_t)min->x * ss + (size_t)max->y * size + (size_t)max->z];
	f = &field[(size_t)max->x * ss + (size_t)max->y * size + (size_t)max->z];
	g = &field[(size_t)min->x * ss + (size_t)min->y * size + (size_t)max->z];
	h = &field[(size_t)max->x * ss + (size_t)min->y * size + (size_t)max->z];

	res.direction = v3f_trilerp(&a->direction, &b->direction, &c->direction, &d->direction, &e->direction, &f->direction, &g->direction, &h->direction, t);
	res.color = v3f_trilerp(&a->color, &b->color, &c->color, &d->color, &e->color, &f->color, &g->color, &h->color, t);

	return res;
}


/* return an interpolated value from ff for the supplied coordinate */
/* coordinate must be in the range 0-1,0-1,0-1 */
/* w must be in the range 0-1 and determines how much of field 0 or 1 contributes to the result */
ff_data_t ff_get(ff_t *ff, v3f_t *coordinate, float w)
{
	v3f_t		scaled, min, max, t;
	ff_data_t	A, B, res;

	assert(w <= 1.f && w >= 0.f);
	assert(coordinate->x <= 1.f && coordinate->x >= 0.f);
	assert(coordinate->y <= 1.f && coordinate->y >= 0.f);
	assert(coordinate->z <= 1.f && coordinate->z >= 0.f);

	scaled = v3f_mult_scalar(coordinate, ff->size - 1);

	/* define the cube flanking the requested coordinate */
	min.x = floorf(scaled.x - 0.5f) + 0.5f;
	min.y = floorf(scaled.y - 0.5f) + 0.5f;
	min.z = floorf(scaled.z - 0.5f) + 0.5f;

	max.x = min.x + 1.0f;
	max.y = min.y + 1.0f;
	max.z = min.z + 1.0f;

	t.x = scaled.x - min.x;
	t.y = scaled.y - min.y;
	t.z = scaled.z - min.z;

	assert((size_t)min.x < ff->size);
	assert((size_t)min.x < ff->size);
	assert((size_t)min.y < ff->size);
	assert((size_t)max.x < ff->size);
	assert((size_t)max.x < ff->size);
	assert((size_t)max.y < ff->size);

	A = ff_sample(ff->fields[0], ff->size, &min, &max, &t);
	B = ff_sample(ff->fields[1], ff->size, &min, &max, &t);

	res.direction = v3f_nlerp(&A.direction, &B.direction, w);
	res.color = v3f_nlerp(&A.color, &B.color, w);

	return res;
}
© All Rights Reserved