ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

math.c (26586B)


      1 /* See LICENSE for license details. */
      2 #include "external/cephes.c"
      3 
      4 function void
      5 fill_kronecker_sub_matrix_f16(f16 *out, i32 out_stride, f16 scale, const f16 *b, iv2 b_dim)
      6 {
      7 	for (i32 i = 0; i < b_dim.y; i++) {
      8 		for (i32 j = 0; j < b_dim.x; j += 4, b += 4) {
      9 			out[j + 0] = scale * b[0];
     10 			out[j + 1] = scale * b[1];
     11 			out[j + 2] = scale * b[2];
     12 			out[j + 3] = scale * b[3];
     13 		}
     14 		out += out_stride;
     15 	}
     16 }
     17 
     18 /* NOTE: this won't check for valid space/etc and assumes row major order */
     19 function void
     20 kronecker_product_f16(f16 *out, const f16 *a, iv2 a_dim, const f16 *b, iv2 b_dim)
     21 {
     22 	iv2 out_dim = {{a_dim.x * b_dim.x, a_dim.y * b_dim.y}};
     23 	assert(out_dim.y % 4 == 0);
     24 	for (i32 i = 0; i < a_dim.y; i++) {
     25 		f16 *vout = out;
     26 		for (i32 j = 0; j < a_dim.x; j++, a++) {
     27 			fill_kronecker_sub_matrix_f16(vout, out_dim.y, *a, b, b_dim);
     28 			vout += b_dim.y;
     29 		}
     30 		out += out_dim.y * b_dim.x;
     31 	}
     32 }
     33 
     34 /* NOTE/TODO: to support even more hadamard sizes use the Paley construction */
     35 function f16 *
     36 make_hadamard_transpose(Arena *arena, i32 dim, b32 row_major)
     37 {
     38 	read_only f16 hadamard_12_12_transpose[] = {
     39 		1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
     40 		1, -1, -1,  1, -1, -1, -1,  1,  1,  1, -1,  1,
     41 		1,  1, -1, -1,  1, -1, -1, -1,  1,  1,  1, -1,
     42 		1, -1,  1, -1, -1,  1, -1, -1, -1,  1,  1,  1,
     43 		1,  1, -1,  1, -1, -1,  1, -1, -1, -1,  1,  1,
     44 		1,  1,  1, -1,  1, -1, -1,  1, -1, -1, -1,  1,
     45 		1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1, -1,
     46 		1, -1,  1,  1,  1, -1,  1, -1, -1,  1, -1, -1,
     47 		1, -1, -1,  1,  1,  1, -1,  1, -1, -1,  1, -1,
     48 		1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1,  1,
     49 		1,  1, -1, -1, -1,  1,  1,  1, -1,  1, -1, -1,
     50 		1, -1,  1, -1, -1, -1,  1,  1,  1, -1,  1, -1,
     51 	};
     52 
     53 	read_only f16 hadamard_20_20_transpose[] = {
     54 		1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
     55 		1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1,
     56 		1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1,
     57 		1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,
     58 		1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,
     59 		1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1,
     60 		1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1,
     61 		1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1,
     62 		1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1,
     63 		1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,
     64 		1, -1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1,
     65 		1,  1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,
     66 		1, -1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1,
     67 		1,  1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,
     68 		1,  1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,
     69 		1,  1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,
     70 		1,  1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,
     71 		1, -1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1,
     72 		1, -1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1,
     73 		1,  1, -1, -1,  1,  1, -1, -1, -1, -1,  1, -1,  1, -1,  1,  1,  1,  1, -1, -1,
     74 	};
     75 
     76 	f16 *result = 0;
     77 
     78 	i32 order          = dim;
     79 	b32 power_of_2     = IsPowerOfTwo(dim);
     80 	b32 multiple_of_12 = dim % 12 == 0;
     81 	b32 multiple_of_20 = dim % 20 == 0;
     82 	i64 elements       = dim * dim;
     83 
     84 	i32 base_dim = 0;
     85 	if (power_of_2) {
     86 		base_dim  = dim;
     87 	} else if (multiple_of_20 && IsPowerOfTwo(dim / 20)) {
     88 		base_dim  = 20;
     89 		dim      /= 20;
     90 	} else if (multiple_of_12 && IsPowerOfTwo(dim / 12)) {
     91 		base_dim  = 12;
     92 		dim      /= 12;
     93 	}
     94 
     95 	if (power_of_2 && base_dim) {
     96 		result = push_array(arena, f16, elements);
     97 
     98 		Temp scratch = temp_begin(arena);
     99 		f16 *m = dim == base_dim ? result : push_array(arena, f16, elements);
    100 
    101 		#define IND(i, j) ((i) * dim + (j))
    102 		m[0] = 1;
    103 		for (i32 k = 1; k < dim; k *= 2) {
    104 			for (i32 i = 0; i < k; i++) {
    105 				for (i32 j = 0; j < k; j++) {
    106 					f16 val = m[IND(i, j)];
    107 					m[IND(i + k, j)]     =  val;
    108 					m[IND(i, j + k)]     =  val;
    109 					m[IND(i + k, j + k)] = -val;
    110 				}
    111 			}
    112 		}
    113 		#undef IND
    114 
    115 		const f16 *m2 = 0;
    116 		iv2 m2_dim;
    117 		switch (base_dim) {
    118 		case 12:{ m2 = hadamard_12_12_transpose; m2_dim = (iv2){{12, 12}}; }break;
    119 		case 20:{ m2 = hadamard_20_20_transpose; m2_dim = (iv2){{20, 20}}; }break;
    120 		}
    121 		if (m2) kronecker_product_f16(result, m, (iv2){{dim, dim}}, m2, m2_dim);
    122 
    123 		temp_end(scratch);
    124 	}
    125 
    126 	if (result && row_major) {
    127 		for (i32 r = 0; r < order; r++)
    128 			for (i32 c = 0; c < order; c++)
    129 				swap(result[r * order + c], result[c * order + r]);
    130 	}
    131 
    132 	return result;
    133 }
    134 
    135 function b32
    136 u128_equal(u128 a, u128 b)
    137 {
    138 	b32 result = a.U64[0] == b.U64[0] && a.U64[1] == b.U64[1];
    139 	return result;
    140 }
    141 
    142 function RangeU64
    143 subrange_n_from_n_m_count(u64 n, u64 n_count, u64 m)
    144 {
    145 	assert(n < n_count);
    146 
    147 	u64 per_lane            = m / n_count;
    148 	u64 leftover            = m - per_lane * n_count;
    149 	u64 leftovers_before_n  = Min(leftover, n);
    150 	u64 base_index          = n * per_lane + leftovers_before_n;
    151 	u64 one_past_last_index = base_index + per_lane + ((n < leftover) ? 1 : 0);
    152 
    153 	RangeU64 result = {base_index, one_past_last_index};
    154 	return result;
    155 }
    156 
    157 function i32
    158 iv3_dimension(iv3 points)
    159 {
    160 	i32 result = (points.x > 1) + (points.y > 1) + (points.z > 1);
    161 	return result;
    162 }
    163 
    164 function bv3
    165 iv3_equal(iv3 a, iv3 b)
    166 {
    167 	bv3 result;
    168 	result.x = a.x == b.x;
    169 	result.y = a.y == b.y;
    170 	result.z = a.z == b.z;
    171 	return result;
    172 }
    173 
    174 function b32
    175 bv3_all(bv3 a)
    176 {
    177 	b32 result = a.x != 0 && a.y != 0 && a.z != 0;
    178 	return result;
    179 }
    180 
    181 function b32
    182 bv3_any(bv3 a)
    183 {
    184 	b32 result = a.x != 0 || a.y != 0 || a.z != 0;
    185 	return result;
    186 }
    187 
    188 function v2
    189 clamp_v2_rect(v2 v, Rect r)
    190 {
    191 	v2 result = v;
    192 	result.x = Clamp(v.x, r.pos.x, r.pos.x + r.size.x);
    193 	result.y = Clamp(v.y, r.pos.y, r.pos.y + r.size.y);
    194 	return result;
    195 }
    196 
    197 function v2
    198 v2_from_iv2(iv2 v)
    199 {
    200 	v2 result;
    201 	result.E[0] = (f32)v.E[0];
    202 	result.E[1] = (f32)v.E[1];
    203 	return result;
    204 }
    205 
    206 function v2
    207 v2_abs(v2 a)
    208 {
    209 	v2 result;
    210 	result.x = Abs(a.x);
    211 	result.y = Abs(a.y);
    212 	return result;
    213 }
    214 
    215 function v2
    216 v2_scale(v2 a, f32 scale)
    217 {
    218 	v2 result;
    219 	result.x = a.x * scale;
    220 	result.y = a.y * scale;
    221 	return result;
    222 }
    223 
    224 function v2
    225 v2_add(v2 a, v2 b)
    226 {
    227 	v2 result;
    228 	result.x = a.x + b.x;
    229 	result.y = a.y + b.y;
    230 	return result;
    231 }
    232 
    233 function v2
    234 v2_sub(v2 a, v2 b)
    235 {
    236 	v2 result = v2_add(a, v2_scale(b, -1.0f));
    237 	return result;
    238 }
    239 
    240 function v2
    241 v2_mul(v2 a, v2 b)
    242 {
    243 	v2 result;
    244 	result.x = a.x * b.x;
    245 	result.y = a.y * b.y;
    246 	return result;
    247 }
    248 
    249 function v2
    250 v2_div(v2 a, v2 b)
    251 {
    252 	v2 result;
    253 	result.x = a.x / b.x;
    254 	result.y = a.y / b.y;
    255 	return result;
    256 }
    257 
    258 function v2
    259 v2_floor(v2 a)
    260 {
    261 	v2 result;
    262 	result.x = (f32)((i32)a.x);
    263 	result.y = (f32)((i32)a.y);
    264 	return result;
    265 }
    266 
    267 function f32
    268 v2_magnitude_squared(v2 a)
    269 {
    270 	f32 result = a.x * a.x + a.y * a.y;
    271 	return result;
    272 }
    273 
    274 function f32
    275 v2_magnitude(v2 a)
    276 {
    277 	f32 result = sqrt_f32(a.x * a.x + a.y * a.y);
    278 	return result;
    279 }
    280 
    281 function v3
    282 cross(v3 a, v3 b)
    283 {
    284 	v3 result;
    285 	result.x = a.y * b.z - a.z * b.y;
    286 	result.y = a.z * b.x - a.x * b.z;
    287 	result.z = a.x * b.y - a.y * b.x;
    288 	return result;
    289 }
    290 
    291 function v3
    292 v3_from_iv3(iv3 v)
    293 {
    294 	v3 result;
    295 	result.E[0] = (f32)v.E[0];
    296 	result.E[1] = (f32)v.E[1];
    297 	result.E[2] = (f32)v.E[2];
    298 	return result;
    299 }
    300 
    301 function v3
    302 v3_abs(v3 a)
    303 {
    304 	v3 result;
    305 	result.x = Abs(a.x);
    306 	result.y = Abs(a.y);
    307 	result.z = Abs(a.z);
    308 	return result;
    309 }
    310 
    311 function v3
    312 v3_scale(v3 a, f32 scale)
    313 {
    314 	v3 result;
    315 	result.x = scale * a.x;
    316 	result.y = scale * a.y;
    317 	result.z = scale * a.z;
    318 	return result;
    319 }
    320 
    321 function v3
    322 v3_add(v3 a, v3 b)
    323 {
    324 	v3 result;
    325 	result.x = a.x + b.x;
    326 	result.y = a.y + b.y;
    327 	result.z = a.z + b.z;
    328 	return result;
    329 }
    330 
    331 function v3
    332 v3_sub(v3 a, v3 b)
    333 {
    334 	v3 result = v3_add(a, v3_scale(b, -1.0f));
    335 	return result;
    336 }
    337 
    338 function v3
    339 v3_div(v3 a, v3 b)
    340 {
    341 	v3 result;
    342 	result.x = a.x / b.x;
    343 	result.y = a.y / b.y;
    344 	result.z = a.z / b.z;
    345 	return result;
    346 }
    347 
    348 function f32
    349 v3_dot(v3 a, v3 b)
    350 {
    351 	f32 result = a.x * b.x + a.y * b.y + a.z * b.z;
    352 	return result;
    353 }
    354 
    355 function f32
    356 v3_magnitude_squared(v3 a)
    357 {
    358 	f32 result = v3_dot(a, a);
    359 	return result;
    360 }
    361 
    362 function f32
    363 v3_magnitude(v3 a)
    364 {
    365 	f32 result = sqrt_f32(v3_dot(a, a));
    366 	return result;
    367 }
    368 
    369 function v3
    370 v3_normalize(v3 a)
    371 {
    372 	v3 result = v3_scale(a, 1.0f / v3_magnitude(a));
    373 	return result;
    374 }
    375 
    376 function v4
    377 v4_scale(v4 a, f32 scale)
    378 {
    379 	v4 result;
    380 	result.x = scale * a.x;
    381 	result.y = scale * a.y;
    382 	result.z = scale * a.z;
    383 	result.w = scale * a.w;
    384 	return result;
    385 }
    386 
    387 function v4
    388 v4_add(v4 a, v4 b)
    389 {
    390 	v4 result;
    391 	result.x = a.x + b.x;
    392 	result.y = a.y + b.y;
    393 	result.z = a.z + b.z;
    394 	result.w = a.w + b.w;
    395 	return result;
    396 }
    397 
    398 function v4
    399 v4_sub(v4 a, v4 b)
    400 {
    401 	v4 result = v4_add(a, v4_scale(b, -1));
    402 	return result;
    403 }
    404 
    405 function f32
    406 v4_dot(v4 a, v4 b)
    407 {
    408 	f32 result = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
    409 	return result;
    410 }
    411 
    412 function v4
    413 v4_lerp(v4 a, v4 b, f32 t)
    414 {
    415 	v4 result = v4_add(a, v4_scale(v4_sub(b, a), t));
    416 	return result;
    417 }
    418 
    419 function b32
    420 m4_equal(m4 a, m4 b)
    421 {
    422 	b32 result = 1;
    423 	for EachElement(a.E, it)
    424 		result &= f32_equal(a.E[it], b.E[it]);
    425 	return result;
    426 }
    427 
    428 #define m4_identity() \
    429 	(m4){.E = { \
    430 		1, 0, 0, 0, \
    431 		0, 1, 0, 0, \
    432 		0, 0, 1, 0, \
    433 		0, 0, 0, 1, \
    434 	}}
    435 
    436 function v4
    437 m4_row(m4 a, u32 row)
    438 {
    439 	v4 result;
    440 	result.E[0] = a.c[0].E[row];
    441 	result.E[1] = a.c[1].E[row];
    442 	result.E[2] = a.c[2].E[row];
    443 	result.E[3] = a.c[3].E[row];
    444 	return result;
    445 }
    446 
    447 function m4
    448 m4_mul(m4 a, m4 b)
    449 {
    450 	m4 result;
    451 	for (u32 i = 0; i < 4; i++) {
    452 		for (u32 j = 0; j < 4; j++) {
    453 			result.c[i].E[j] = v4_dot(m4_row(a, j), b.c[i]);
    454 		}
    455 	}
    456 	return result;
    457 }
    458 
    459 /* NOTE(rnp): based on:
    460  * https://web.archive.org/web/20131215123403/ftp://download.intel.com/design/PentiumIII/sml/24504301.pdf
    461  * TODO(rnp): redo with SIMD as given in the link (but need to rewrite for column-major)
    462  */
    463 function m4
    464 m4_inverse(m4 m)
    465 {
    466 	m4 result;
    467 	result.E[ 0] =  m.E[5] * m.E[10] * m.E[15] - m.E[5] * m.E[11] * m.E[14] - m.E[9] * m.E[6] * m.E[15] + m.E[9] * m.E[7] * m.E[14] + m.E[13] * m.E[6] * m.E[11] - m.E[13] * m.E[7] * m.E[10];
    468 	result.E[ 4] = -m.E[4] * m.E[10] * m.E[15] + m.E[4] * m.E[11] * m.E[14] + m.E[8] * m.E[6] * m.E[15] - m.E[8] * m.E[7] * m.E[14] - m.E[12] * m.E[6] * m.E[11] + m.E[12] * m.E[7] * m.E[10];
    469 	result.E[ 8] =  m.E[4] * m.E[ 9] * m.E[15] - m.E[4] * m.E[11] * m.E[13] - m.E[8] * m.E[5] * m.E[15] + m.E[8] * m.E[7] * m.E[13] + m.E[12] * m.E[5] * m.E[11] - m.E[12] * m.E[7] * m.E[ 9];
    470 	result.E[12] = -m.E[4] * m.E[ 9] * m.E[14] + m.E[4] * m.E[10] * m.E[13] + m.E[8] * m.E[5] * m.E[14] - m.E[8] * m.E[6] * m.E[13] - m.E[12] * m.E[5] * m.E[10] + m.E[12] * m.E[6] * m.E[ 9];
    471 	result.E[ 1] = -m.E[1] * m.E[10] * m.E[15] + m.E[1] * m.E[11] * m.E[14] + m.E[9] * m.E[2] * m.E[15] - m.E[9] * m.E[3] * m.E[14] - m.E[13] * m.E[2] * m.E[11] + m.E[13] * m.E[3] * m.E[10];
    472 	result.E[ 5] =  m.E[0] * m.E[10] * m.E[15] - m.E[0] * m.E[11] * m.E[14] - m.E[8] * m.E[2] * m.E[15] + m.E[8] * m.E[3] * m.E[14] + m.E[12] * m.E[2] * m.E[11] - m.E[12] * m.E[3] * m.E[10];
    473 	result.E[ 9] = -m.E[0] * m.E[ 9] * m.E[15] + m.E[0] * m.E[11] * m.E[13] + m.E[8] * m.E[1] * m.E[15] - m.E[8] * m.E[3] * m.E[13] - m.E[12] * m.E[1] * m.E[11] + m.E[12] * m.E[3] * m.E[ 9];
    474 	result.E[13] =  m.E[0] * m.E[ 9] * m.E[14] - m.E[0] * m.E[10] * m.E[13] - m.E[8] * m.E[1] * m.E[14] + m.E[8] * m.E[2] * m.E[13] + m.E[12] * m.E[1] * m.E[10] - m.E[12] * m.E[2] * m.E[ 9];
    475 	result.E[ 2] =  m.E[1] * m.E[ 6] * m.E[15] - m.E[1] * m.E[ 7] * m.E[14] - m.E[5] * m.E[2] * m.E[15] + m.E[5] * m.E[3] * m.E[14] + m.E[13] * m.E[2] * m.E[ 7] - m.E[13] * m.E[3] * m.E[ 6];
    476 	result.E[ 6] = -m.E[0] * m.E[ 6] * m.E[15] + m.E[0] * m.E[ 7] * m.E[14] + m.E[4] * m.E[2] * m.E[15] - m.E[4] * m.E[3] * m.E[14] - m.E[12] * m.E[2] * m.E[ 7] + m.E[12] * m.E[3] * m.E[ 6];
    477 	result.E[10] =  m.E[0] * m.E[ 5] * m.E[15] - m.E[0] * m.E[ 7] * m.E[13] - m.E[4] * m.E[1] * m.E[15] + m.E[4] * m.E[3] * m.E[13] + m.E[12] * m.E[1] * m.E[ 7] - m.E[12] * m.E[3] * m.E[ 5];
    478 	result.E[14] = -m.E[0] * m.E[ 5] * m.E[14] + m.E[0] * m.E[ 6] * m.E[13] + m.E[4] * m.E[1] * m.E[14] - m.E[4] * m.E[2] * m.E[13] - m.E[12] * m.E[1] * m.E[ 6] + m.E[12] * m.E[2] * m.E[ 5];
    479 	result.E[ 3] = -m.E[1] * m.E[ 6] * m.E[11] + m.E[1] * m.E[ 7] * m.E[10] + m.E[5] * m.E[2] * m.E[11] - m.E[5] * m.E[3] * m.E[10] - m.E[ 9] * m.E[2] * m.E[ 7] + m.E[ 9] * m.E[3] * m.E[ 6];
    480 	result.E[ 7] =  m.E[0] * m.E[ 6] * m.E[11] - m.E[0] * m.E[ 7] * m.E[10] - m.E[4] * m.E[2] * m.E[11] + m.E[4] * m.E[3] * m.E[10] + m.E[ 8] * m.E[2] * m.E[ 7] - m.E[ 8] * m.E[3] * m.E[ 6];
    481 	result.E[11] = -m.E[0] * m.E[ 5] * m.E[11] + m.E[0] * m.E[ 7] * m.E[ 9] + m.E[4] * m.E[1] * m.E[11] - m.E[4] * m.E[3] * m.E[ 9] - m.E[ 8] * m.E[1] * m.E[ 7] + m.E[ 8] * m.E[3] * m.E[ 5];
    482 	result.E[15] =  m.E[0] * m.E[ 5] * m.E[10] - m.E[0] * m.E[ 6] * m.E[ 9] - m.E[4] * m.E[1] * m.E[10] + m.E[4] * m.E[2] * m.E[ 9] + m.E[ 8] * m.E[1] * m.E[ 6] - m.E[ 8] * m.E[2] * m.E[ 5];
    483 
    484 	f32 determinant = m.E[0] * result.E[0] + m.E[1] * result.E[4] + m.E[2] * result.E[8] + m.E[3] * result.E[12];
    485 	determinant = 1.0f / determinant;
    486 	for(i32 i = 0; i < 16; i++)
    487 		result.E[i] *= determinant;
    488 	return result;
    489 }
    490 
    491 function m4
    492 m4_translation(v3 delta)
    493 {
    494 	m4 result;
    495 	result.c[0] = (v4){{1, 0, 0, 0}};
    496 	result.c[1] = (v4){{0, 1, 0, 0}};
    497 	result.c[2] = (v4){{0, 0, 1, 0}};
    498 	result.c[3] = (v4){{delta.x, delta.y, delta.z, 1}};
    499 	return result;
    500 }
    501 
    502 function m4
    503 m4_scale(v3 scale)
    504 {
    505 	m4 result;
    506 	result.c[0] = (v4){{scale.x, 0,       0,       0}};
    507 	result.c[1] = (v4){{0,       scale.y, 0,       0}};
    508 	result.c[2] = (v4){{0,       0,       scale.z, 0}};
    509 	result.c[3] = (v4){{0,       0,       0,       1}};
    510 	return result;
    511 }
    512 
    513 function m4
    514 m4_rotation_about_axis(v3 axis, f32 turns)
    515 {
    516 	assert(f32_equal(v3_magnitude_squared(axis), 1.0f));
    517 	f32 sa  = sin_f32(turns * 2 * PI);
    518 	f32 ca  = cos_f32(turns * 2 * PI);
    519 	f32 mca = 1.0f - ca;
    520 
    521 	f32 x = axis.x, x2 = x * x;
    522 	f32 y = axis.y, y2 = y * y;
    523 	f32 z = axis.z, z2 = z * z;
    524 
    525 	m4 result;
    526 	result.c[0] = (v4){{ca + mca * x2,        mca * x * y - sa * z, mca * x * z + sa * y, 0}};
    527 	result.c[1] = (v4){{mca * x * y + sa * z, ca + mca * y2,        mca * y * z - sa * x, 0}};
    528 	result.c[2] = (v4){{mca * x * z - sa * y, mca * y * z + sa * x, ca + mca * z2,        0}};
    529 	result.c[3] = (v4){{0, 0, 0, 1}};
    530 	return result;
    531 }
    532 
    533 function m4
    534 m4_rotation_about_y(f32 turns)
    535 {
    536 	m4 result = m4_rotation_about_axis((v3){.y = 1.0f}, turns);
    537 	return result;
    538 }
    539 
    540 function m4
    541 y_aligned_volume_transform(v3 extent, v3 translation, f32 rotation_turns)
    542 {
    543 	m4 T = m4_translation(translation);
    544 	m4 R = m4_rotation_about_axis((v3){.y = 1.0f}, rotation_turns);
    545 	m4 S = m4_scale(extent);
    546 	m4 result = m4_mul(T, m4_mul(R, S));
    547 	return result;
    548 }
    549 
    550 function v4
    551 m4_mul_v4(m4 a, v4 v)
    552 {
    553 	v4 result;
    554 	result.x = v4_dot(m4_row(a, 0), v);
    555 	result.y = v4_dot(m4_row(a, 1), v);
    556 	result.z = v4_dot(m4_row(a, 2), v);
    557 	result.w = v4_dot(m4_row(a, 3), v);
    558 	return result;
    559 }
    560 
    561 function v3
    562 m4_mul_v3(m4 a, v3 v)
    563 {
    564 	v3 result = m4_mul_v4(a, (v4){{v.x, v.y, v.z, 1.0f}}).xyz;
    565 	return result;
    566 }
    567 
    568 function v2
    569 rect_uv(v2 p, Rect r)
    570 {
    571 	v2 result = v2_div(v2_sub(p, r.pos), r.size);
    572 	return result;
    573 }
    574 
    575 function v2
    576 rect_uv_ndc(v2 p, Rect r)
    577 {
    578 	v2 uv     = rect_uv(p, r);
    579 	v2 result = v2_sub(v2_scale(uv, 2.f), (v2){{1.f, 1.f}});
    580 	return result;
    581 }
    582 
    583 function Rect
    584 rect_intersect(Rect a, Rect b)
    585 {
    586 	v2 ae = v2_add(a.pos, a.size);
    587 	v2 be = v2_add(b.pos, b.size);
    588 
    589 	Rect result   = {0};
    590 	result.pos.x  = Max(a.pos.x, b.pos.x);
    591 	result.pos.y  = Max(a.pos.y, b.pos.y);
    592 	result.size.x = Min(ae.x, be.x) - result.pos.x;
    593 	result.size.y = Min(ae.y, be.y) - result.pos.y;
    594 	return result;
    595 }
    596 
    597 function Rect
    598 rect_squish_centered(Rect a, v2 pct)
    599 {
    600 	v2 delta_size = v2_mul(a.size, pct);
    601 	Rect result;
    602 	result.pos  = v2_add(a.pos,  v2_scale(delta_size, 0.5f));
    603 	result.size = v2_add(a.size, v2_scale(delta_size, -1.f));
    604 	return result;
    605 }
    606 
    607 function Rect
    608 rect_shrink_centered(Rect a, v2 px)
    609 {
    610 	Rect result;
    611 	result.pos  = v2_add(a.pos,  v2_scale(px, 0.5f));
    612 	result.size = v2_add(a.size, v2_scale(px, -1.f));
    613 	return result;
    614 }
    615 
    616 function m4
    617 orthographic_projection(f32 n, f32 f, f32 t, f32 r)
    618 {
    619 	m4 result;
    620 	f32 a = -2 / (f - n);
    621 	f32 b = - (f + n) / (f - n);
    622 	result.c[0] = (v4){{1 / r, 0,     0,  0}};
    623 	result.c[1] = (v4){{0,     1 / t, 0,  0}};
    624 	result.c[2] = (v4){{0,     0,     a,  0}};
    625 	result.c[3] = (v4){{0,     0,     b,  1}};
    626 	return result;
    627 }
    628 
    629 function m4
    630 perspective_projection(f32 n, f32 f, f32 fov, f32 aspect)
    631 {
    632 	m4 result;
    633 	f32 t = n * tan_f32(fov / 2.0f);
    634 	f32 r = t * aspect;
    635 	f32 a = -(f + n) / (f - n);
    636 	f32 b = -2 * f * n / (f - n);
    637 	result.c[0] = (v4){{n / r, 0,     0,  0}};
    638 	result.c[1] = (v4){{0,     n / t, 0,  0}};
    639 	result.c[2] = (v4){{0,     0,     a, -1}};
    640 	result.c[3] = (v4){{0,     0,     b,  0}};
    641 	return result;
    642 }
    643 
    644 function m4
    645 camera_look_at(v3 camera, v3 point)
    646 {
    647 	v3 orthogonal = {{0, 1.0f, 0}};
    648 	v3 normal     = v3_normalize(v3_sub(camera, point));
    649 	v3 right      = cross(orthogonal, normal);
    650 	v3 up         = cross(normal,     right);
    651 
    652 	v3 translate;
    653 	camera      = v3_sub((v3){0}, camera);
    654 	translate.x = v3_dot(camera, right);
    655 	translate.y = v3_dot(camera, up);
    656 	translate.z = v3_dot(camera, normal);
    657 
    658 	m4 result;
    659 	result.c[0] = (v4){{right.x,     up.x,        normal.x,    0}};
    660 	result.c[1] = (v4){{right.y,     up.y,        normal.y,    0}};
    661 	result.c[2] = (v4){{right.z,     up.z,        normal.z,    0}};
    662 	result.c[3] = (v4){{translate.x, translate.y, translate.z, 1}};
    663 	return result;
    664 }
    665 
    666 /* NOTE(rnp): adapted from "Essential Mathematics for Games and Interactive Applications" (Verth, Bishop) */
    667 function f32
    668 obb_raycast(m4 obb_orientation, v3 obb_size, v3 obb_center, ray r)
    669 {
    670 	v3 p = v3_sub(obb_center, r.origin);
    671 	v3 X = obb_orientation.c[0].xyz;
    672 	v3 Y = obb_orientation.c[1].xyz;
    673 	v3 Z = obb_orientation.c[2].xyz;
    674 
    675 	/* NOTE(rnp): projects direction vector onto OBB axis */
    676 	v3 f;
    677 	f.x = v3_dot(X, r.direction);
    678 	f.y = v3_dot(Y, r.direction);
    679 	f.z = v3_dot(Z, r.direction);
    680 
    681 	/* NOTE(rnp): projects relative vector onto OBB axis */
    682 	v3 e;
    683 	e.x = v3_dot(X, p);
    684 	e.y = v3_dot(Y, p);
    685 	e.z = v3_dot(Z, p);
    686 
    687 	f32 result = 0;
    688 	f32 t[6] = {0};
    689 	for (i32 i = 0; i < 3; i++) {
    690 		if (f32_equal(f.E[i], 0)) {
    691 			if (-e.E[i] - obb_size.E[i] > 0 || -e.E[i] + obb_size.E[i] < 0)
    692 				result = -1.0f;
    693 			f.E[i] = F32_EPSILON;
    694 		}
    695 		t[i * 2 + 0] = (e.E[i] + obb_size.E[i]) / f.E[i];
    696 		t[i * 2 + 1] = (e.E[i] - obb_size.E[i]) / f.E[i];
    697 	}
    698 
    699 	if (result != -1) {
    700 		f32 tmin = Max(Max(Min(t[0], t[1]), Min(t[2], t[3])), Min(t[4], t[5]));
    701 		f32 tmax = Min(Min(Max(t[0], t[1]), Max(t[2], t[3])), Max(t[4], t[5]));
    702 		if (tmax >= 0 && tmin <= tmax) {
    703 			result = tmin > 0 ? tmin : tmax;
    704 		} else {
    705 			result = -1;
    706 		}
    707 	}
    708 
    709 	return result;
    710 }
    711 
    712 function f32
    713 complex_filter_first_moment(v2 *filter, i32 length, f32 sampling_frequency)
    714 {
    715 	f32 n = 0, d = 0;
    716 	for (i32 i = 0; i < length; i++) {
    717 		f32 t = v2_magnitude_squared(filter[i]);
    718 		n += (f32)i * t;
    719 		d += t;
    720 	}
    721 	f32 result = n / d / sampling_frequency;
    722 	return result;
    723 }
    724 
    725 function f32
    726 real_filter_first_moment(f32 *filter, i32 length, f32 sampling_frequency)
    727 {
    728 	f32 n = 0, d = 0;
    729 	for (i32 i = 0; i < length; i++) {
    730 		f32 t = filter[i] * filter[i];
    731 		n += (f32)i * t;
    732 		d += t;
    733 	}
    734 	f32 result = n / d / sampling_frequency;
    735 	return result;
    736 }
    737 
    738 function f32
    739 tukey_window(f32 t, f32 tapering)
    740 {
    741 	f32 r = tapering;
    742 	f32 result = 1;
    743 	if (t < r / 2)      result = 0.5f * (1 + cos_f32(2 * PI * (t - r / 2)     / r));
    744 	if (t >= 1 - r / 2) result = 0.5f * (1 + cos_f32(2 * PI * (t - 1 + r / 2) / r));
    745 	return result;
    746 }
    747 
    748 /* NOTE(rnp): adapted from "Discrete Time Signal Processing" (Oppenheim) */
    749 function f32 *
    750 kaiser_low_pass_filter(Arena *arena, f32 cutoff_frequency, f32 sampling_frequency, f32 beta, i32 length)
    751 {
    752 	f32 *result = push_array(arena, f32, length);
    753 	f32 wc      = 2 * PI * cutoff_frequency / sampling_frequency;
    754 	f32 a       = (f32)length / 2.0f;
    755 	f32 pi_i0_b = PI * (f32)cephes_i0(beta);
    756 
    757 	for (i32 n = 0; n < length; n++) {
    758 		f32 t       = (f32)n - a;
    759 		f32 impulse = !f32_equal(t, 0) ? sin_f32(wc * t) / t : wc;
    760 		t           = t / a;
    761 		f32 window  = (f32)cephes_i0(beta * sqrt_f32(1 - t * t)) / pi_i0_b;
    762 		result[n]   = impulse * window;
    763 	}
    764 
    765 	return result;
    766 }
    767 
    768 function f32 *
    769 rf_chirp(Arena *arena, f32 min_frequency, f32 max_frequency, f32 sampling_frequency,
    770          i32 length, b32 reverse)
    771 {
    772 	f32 *result = push_array(arena, f32, length);
    773 	for (i32 i = 0; i < length; i++) {
    774 		i32 index = reverse? length - 1 - i : i;
    775 		f32 fc    = min_frequency + (f32)i * (max_frequency - min_frequency) / (2 * (f32)length);
    776 		f32 arg   = 2 * PI * fc * (f32)i / sampling_frequency;
    777 		result[index] = sin_f32(arg) * tukey_window((f32)i / (f32)length, 0.2f);
    778 	}
    779 	return result;
    780 }
    781 
    782 function v2 *
    783 baseband_chirp(Arena *arena, f32 min_frequency, f32 max_frequency, f32 sampling_frequency,
    784                i32 length, b32 reverse, f32 scale)
    785 {
    786 	v2 *result    = push_array(arena, v2, length);
    787 	f32 conjugate = reverse ? -1 : 1;
    788 	for (i32 i = 0; i < length; i++) {
    789 		i32 index = reverse? length - 1 - i : i;
    790 		f32 fc    = min_frequency + (f32)i * (max_frequency - min_frequency) / (2 * (f32)length);
    791 		f32 arg   = 2 * PI * fc * (f32)i / sampling_frequency;
    792 		v2 sample = {{scale * cos_f32(arg), conjugate * scale * sin_f32(arg)}};
    793 		result[index] = v2_scale(sample, tukey_window((f32)i / (f32)length, 0.2f));
    794 	}
    795 	return result;
    796 }
    797 
    798 function iv3
    799 das_output_dimension(iv3 points)
    800 {
    801 	iv3 result;
    802 	result.x = Max(points.x, 1);
    803 	result.y = Max(points.y, 1);
    804 	result.z = Max(points.z, 1);
    805 
    806 	switch (iv3_dimension(result)) {
    807 	case 1:{
    808 		if (result.y > 1) result.x = result.y;
    809 		if (result.z > 1) result.x = result.z;
    810 		result.y = result.z = 1;
    811 	}break;
    812 
    813 	case 2:{
    814 		if (result.x > 1) {
    815 			if (result.z > 1) result.y = result.z;
    816 		} else {
    817 			result.x = result.z;
    818 		}
    819 		result.z = 1;
    820 	}break;
    821 
    822 	case 3:{}break;
    823 
    824 	InvalidDefaultCase;
    825 	}
    826 
    827 	return result;
    828 }
    829 
    830 function m4
    831 das_transform_1d(v3 p1, v3 p2)
    832 {
    833 	v3 extent = v3_sub(p2, p1);
    834 	m4 result = {
    835 		.c[0] = (v4){{extent.x, extent.y, extent.z, 0.0f}},
    836 		.c[1] = (v4){{0.0f, 0.0f, 0.0f, 0.0f}},
    837 		.c[2] = (v4){{0.0f, 0.0f, 0.0f, 0.0f}},
    838 		.c[3] = (v4){{p1.x, p1.y, p1.z, 1.0f}},
    839 	};
    840 	return result;
    841 }
    842 
    843 function m4
    844 das_transform_2d_with_normal(v3 normal, v2 min_coordinate, v2 max_coordinate, f32 offset)
    845 {
    846 	v3 U = {{0, 1.0f, 0}};
    847 	if (f32_equal(v3_dot(U, normal), 1.0f))
    848 		U = (v3){{1.0f, 0, 0}};
    849 
    850 	v3 N = normal;
    851 	v3 V = cross(U, N);
    852 
    853 	v3 min = v3_add(v3_scale(U, min_coordinate.x), v3_scale(V, min_coordinate.y));
    854 	v3 max = v3_add(v3_scale(U, max_coordinate.x), v3_scale(V, max_coordinate.y));
    855 
    856 	v3 extent = v3_sub(max, min);
    857 	U = v3_scale(U, v3_dot(U, extent));
    858 	V = v3_scale(V, v3_dot(V, extent));
    859 
    860 	v3 t = v3_add(v3_scale(N, offset), min);
    861 
    862 	m4 result;
    863 	result.c[0] = (v4){{U.x,  U.y,  U.z,  0.0f}};
    864 	result.c[1] = (v4){{V.x,  V.y,  V.z,  0.0f}};
    865 	result.c[2] = (v4){{N.x,  N.y,  N.z,  0.0f}};
    866 	result.c[3] = (v4){{t.x,  t.y,  t.z,  1.0f}};
    867 
    868 	return result;
    869 }
    870 
    871 function m4
    872 das_transform_2d_xz(v2 min_coordinate, v2 max_coordinate, f32 y_off)
    873 {
    874 	m4 result = das_transform_2d_with_normal((v3){.y = 1.0f}, min_coordinate, max_coordinate, y_off);
    875 	return result;
    876 }
    877 
    878 function m4
    879 das_transform_2d_yz(v2 min_coordinate, v2 max_coordinate, f32 x_off)
    880 {
    881 	// NOTE(rnp): flip so that region extends in correct direction
    882 	m4 result = das_transform_2d_with_normal((v3){.x = -1.0f}, min_coordinate, max_coordinate, x_off);
    883 	return result;
    884 }
    885 
    886 function m4
    887 das_transform_2d_xy(v2 min_coordinate, v2 max_coordinate, f32 z_off)
    888 {
    889 	m4 result = das_transform_2d_with_normal((v3){.z = 1.0f}, min_coordinate, max_coordinate, z_off);
    890 	return result;
    891 }
    892 
    893 function m4
    894 das_transform_3d(v3 min_coordinate, v3 max_coordinate)
    895 {
    896 	v3 extent = v3_sub(max_coordinate, min_coordinate);
    897 	m4 result;
    898 	result.c[0] = (v4){{extent.x,         0.0f,             0.0f,             0.0f}};
    899 	result.c[1] = (v4){{0.0f,             extent.y,         0.0f,             0.0f}};
    900 	result.c[2] = (v4){{0.0f,             0.0f,             extent.z,         0.0f}};
    901 	result.c[3] = (v4){{min_coordinate.x, min_coordinate.y, min_coordinate.z, 1.0f}};
    902 	return result;
    903 }
    904 
    905 function m4
    906 das_transform(v3 min_coordinate, v3 max_coordinate, iv3 *points)
    907 {
    908 	m4 result;
    909 
    910 	*points = das_output_dimension(*points);
    911 
    912 	switch (iv3_dimension(*points)) {
    913 	case 1:{result = das_transform_1d(      min_coordinate,     max_coordinate);    }break;
    914 	case 2:{result = das_transform_2d_xz(XY(min_coordinate), XY(max_coordinate), 0);}break;
    915 	case 3:{result = das_transform_3d(      min_coordinate,     max_coordinate);    }break;
    916 	}
    917 
    918 	return result;
    919 }
    920 
    921 function v3
    922 plane_normal_from_transform(m4 transform)
    923 {
    924 	v3 U = v3_normalize(transform.c[0].xyz);
    925 	v3 V = v3_normalize(transform.c[1].xyz);
    926 	v3 result  = cross(V, U);
    927 	return result;
    928 }
    929 
    930 function f32
    931 plane_offset_from_transform(m4 transform)
    932 {
    933 	f32 result = v3_dot(plane_normal_from_transform(transform), transform.c[3].xyz);
    934 	return result;
    935 }
    936 
    937 function void
    938 plane_corners_from_transform(m4 transform, v2 *min, v2 *max)
    939 {
    940 	v3 U = v3_normalize(transform.c[0].xyz);
    941 	v3 V = v3_normalize(transform.c[1].xyz);
    942 
    943 	v3 min_3d = m4_mul_v3(transform, (v3){{0.f, 0.f, 0.f}});
    944 	v3 max_3d = m4_mul_v3(transform, (v3){{1.f, 1.f, 1.f}});
    945 
    946 	if (min) *min = (v2){{v3_dot(U, min_3d), v3_dot(V, min_3d)}};
    947 	if (max) *max = (v2){{v3_dot(U, max_3d), v3_dot(V, max_3d)}};
    948 }
    949 
    950 function v2
    951 plane_uv(v3 point, v3 U, v3 V)
    952 {
    953 	v2 result;
    954 	result.x = v3_dot(U, point) / v3_dot(U, U);
    955 	result.y = v3_dot(V, point) / v3_dot(V, V);
    956 	return result;
    957 }
    958 
    959 function v4
    960 hsv_to_rgb(v4 hsv)
    961 {
    962 	/* f(k(n))   = V - V*S*max(0, min(k, min(4 - k, 1)))
    963 	 * k(n)      = fmod((n + H * 6), 6)
    964 	 * (R, G, B) = (f(n = 5), f(n = 3), f(n = 1))
    965 	 */
    966 	alignas(16) f32 nval[4] = {5.0f, 3.0f, 1.0f, 0.0f};
    967 	f32x4 n   = load_f32x4(nval);
    968 	f32x4 H   = dup_f32x4(hsv.x);
    969 	f32x4 S   = dup_f32x4(hsv.y);
    970 	f32x4 V   = dup_f32x4(hsv.z);
    971 	f32x4 six = dup_f32x4(6);
    972 
    973 	f32x4 t   = add_f32x4(n, mul_f32x4(six, H));
    974 	f32x4 rem = floor_f32x4(div_f32x4(t, six));
    975 	f32x4 k   = sub_f32x4(t, mul_f32x4(rem, six));
    976 
    977 	t = min_f32x4(sub_f32x4(dup_f32x4(4), k), dup_f32x4(1));
    978 	t = max_f32x4(dup_f32x4(0), min_f32x4(k, t));
    979 	t = mul_f32x4(t, mul_f32x4(S, V));
    980 
    981 	v4 rgba;
    982 	store_f32x4(rgba.E, sub_f32x4(V, t));
    983 	rgba.a = hsv.a;
    984 	return rgba;
    985 }
    986 
    987 function f32
    988 ease_in_out_cubic(f32 t)
    989 {
    990 	f32 result;
    991 	if (t < 0.5f) {
    992 		result = 4.0f * t * t * t;
    993 	} else {
    994 		t      = -2.0f * t + 2.0f;
    995 		result =  1.0f - t * t * t / 2.0f;
    996 	}
    997 	return result;
    998 }
    999 
   1000 function f32
   1001 ease_in_out_quartic(f32 t)
   1002 {
   1003 	f32 result;
   1004 	if (t < 0.5f) {
   1005 		result = 8.0f * t * t * t * t;
   1006 	} else {
   1007 		t      = -2.0f * t + 2.0f;
   1008 		result =  1.0f - t * t * t * t / 2.0f;
   1009 	}
   1010 	return result;
   1011 }