From d1d7cfef7d2f1035e46b386579b20fdbd7240288 Mon Sep 17 00:00:00 2001 From: Randy McShandy Date: Thu, 7 Dec 2023 23:11:12 -0600 Subject: [PATCH] Some simplifications and additions but still desperately needs more threads or somethings --- structs.h | 24 ++++++++++++++++++++++ utils.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 8 ++++++++ 3 files changed, 92 insertions(+) create mode 100644 structs.h create mode 100644 utils.c create mode 100644 utils.h diff --git a/structs.h b/structs.h new file mode 100644 index 0000000..c10ed09 --- /dev/null +++ b/structs.h @@ -0,0 +1,24 @@ +#define GRID_X 256 +#define GRID_Y 256 + +typedef struct +{ + float v[3][3]; + +} Mat3; + +typedef struct +{ + float a; + float b; +} FVec2; + +typedef struct +{ + float diff_a; + float diff_b; + float feed; + float kill; + float delta_t; +} RD_Opts; + diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..b20db76 --- /dev/null +++ b/utils.c @@ -0,0 +1,60 @@ +#include "structs.h" + +FVec2 grid[GRID_X][GRID_Y]; +FVec2 grid_prime[GRID_X][GRID_Y]; + +float convolve(Mat3 kernel, Mat3 source) +{ + float result = 0.0f; + + // Convolve kernel at grid[x,y] + for (int ix = 0; ix < 3; ix++) + { + for (int iy = 0; iy < 3; iy++) + { + result += (kernel.v[ix][iy] * source.v[ix][iy]); + } + } + + return result; +} + +float rd_a_prime(RD_Opts opts, int x, int y, Mat3 kernel, float A, float B) +{ + float a_prime = 1.0f; + // Use species A in the convolution, b_prime will need B + // For now we won't iterate over edge rows and columns to avoid special case vomit + Mat3 source = + { + .v = + { + {grid[x-1][y-1].a, grid[x][y-1].a, grid[x+1][y-1].a}, + {grid[x-1][y+0].a, grid[x][y+0].a, grid[x+1][y+0].a}, + {grid[x-1][y+1].a, grid[x][y+1].a, grid[x+1][y+1].a}, + } + }; + + a_prime = A + (opts.diff_a*(convolve(kernel, source)) - (A*B*B) + opts.feed*(1.0f-A)) * opts.delta_t; + + return a_prime; +} + +float rd_b_prime(RD_Opts opts, int x, int y, Mat3 kernel, float A, float B) +{ + float b_prime = 1.0f; + // Use species A in the convolution, b_prime will need B + Mat3 source = + { + .v = + { + {grid[x-1][y-1].b, grid[x][y-1].b, grid[x+1][y-1].b}, + {grid[x-1][y+0].b, grid[x][y+0].b, grid[x+1][y+0].b}, + {grid[x-1][y+1].b, grid[x][y+1].b, grid[x+1][y+1].b}, + } + }; + + b_prime = B + (opts.diff_b*(convolve(kernel, source)) + A*(B*B) - (opts.kill + opts.feed)*B) * opts.delta_t; + + return b_prime; +} + diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..8121a81 --- /dev/null +++ b/utils.h @@ -0,0 +1,8 @@ + +float convolve(Mat3 kernel, Mat3 source); +float rd_a_prime(RD_Opts opts, int x, int y, Mat3 kernel, float A, float B); +float rd_b_prime(RD_Opts opts, int x, int y, Mat3 kernel, float A, float B); + +extern FVec2 grid[GRID_X][GRID_Y]; +extern FVec2 grid_prime[GRID_X][GRID_Y]; + -- 2.49.0