Skip to content

Commit

Permalink
Move volume_wrap function out of volume.h
Browse files Browse the repository at this point in the history
Volume.h should only have very low level function.  Here I moved the
function directly in the wrap filter, since this is a function very
specific to this filter.

Also fixed the code style a bit: moved variable declarations at the top
of the function, and fixed compilation warning.
  • Loading branch information
guillaumechereau committed Mar 24, 2024
1 parent 2aab7e2 commit 072efa0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 71 deletions.
79 changes: 77 additions & 2 deletions src/filters/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,88 @@ typedef struct {
bool current_only;
} filter_wrap_t;

static void volume_wrap(volume_t *volume, int axis, int sign,
const int aabb[2][3])
{
int pos[3];
int buffer_pos[3];
int volume_pos[3];
int size[3];
uint8_t *buffer;
int i;
size_t buffer_offset;

if (aabb[1][axis] - aabb[0][axis] == 1) {
return;
}

size[0] = aabb[1][0] - aabb[0][0];
size[1] = aabb[1][1] - aabb[0][1];
size[2] = aabb[1][2] - aabb[0][2];

buffer = malloc(4 * size[0] * size[1] * size[2]);

for (pos[0] = 0; pos[0] < size[0]; pos[0]++) {
for (pos[1] = 0; pos[1] < size[1]; pos[1]++) {
for (pos[2] = 0; pos[2] < size[2]; pos[2]++) {
memcpy(buffer_pos, pos, sizeof(pos));
memcpy(volume_pos, pos, sizeof(pos));

for (i = 0; i < 3; i++) {
volume_pos[i] += aabb[0][i];
}

buffer_pos[axis] += sign;

if (buffer_pos[axis] < 0) {
buffer_pos[axis] += size[axis];
}

buffer_pos[axis] %= size[axis];

buffer_offset = 4 * (
buffer_pos[2] * size[0] * size[1] +
buffer_pos[1] * size[0] + buffer_pos[0]
);

volume_get_at(volume, NULL, volume_pos, &buffer[buffer_offset]);
}
}
}

for (pos[0] = 0; pos[0] < size[0]; pos[0]++) {
for (pos[1] = 0; pos[1] < size[1]; pos[1]++) {
for (pos[2] = 0; pos[2] < size[2]; pos[2]++) {
memcpy(volume_pos, pos, sizeof(pos));

for (i = 0; i < 3; i++) {
volume_pos[i] += aabb[0][i];
}

buffer_offset = 4 * (
pos[2] * size[0] * size[1] +
pos[1] * size[0] + pos[0]
);

volume_set_at(volume, NULL, volume_pos, &buffer[buffer_offset]);
}
}
}

free(buffer);
}

static bool wrap_box(int *out_axis, int *sign)
{
char buf[8];
bool ret = false;
static const char* AXIS_NAMES[] = {"X", "Y", "Z"};
int axis;
const char *AXIS_NAMES[] = {"X", "Y", "Z"};

*out_axis = 0;
*sign = 1;

for (int axis = 0; axis < 3; axis++) {
for (axis = 0; axis < 3; axis++) {
gui_row_begin(2);

snprintf(buf, sizeof(buf), "-%s", AXIS_NAMES[axis]);
Expand Down
68 changes: 0 additions & 68 deletions src/volume.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,71 +825,3 @@ void volume_get_global_stats(volume_global_stats_t *stats)
{
*stats = g_global_stats;
}

void volume_wrap(volume_t *volume, int axis, int sign, int aabb[2][3])
{
int pos[3];
int buffer_pos[3];
int volume_pos[3];

if (aabb[1][axis] - aabb[0][axis] == 1) {
return;
}

int size[3] = {
aabb[1][0] - aabb[0][0],
aabb[1][1] - aabb[0][1],
aabb[1][2] - aabb[0][2],
};

uint8_t *buffer = malloc(4 * size[0] * size[1] * size[2]);

for (pos[0] = 0; pos[0] < size[0]; pos[0]++) {
for (pos[1] = 0; pos[1] < size[1]; pos[1]++) {
for (pos[2] = 0; pos[2] < size[2]; pos[2]++) {
memcpy(buffer_pos, pos, sizeof(pos));
memcpy(volume_pos, pos, sizeof(pos));

for (int i = 0; i < 3; i++) {
volume_pos[i] += aabb[0][i];
}

buffer_pos[axis] += sign;

if (buffer_pos[axis] < 0) {
buffer_pos[axis] += size[axis];
}

buffer_pos[axis] %= size[axis];

size_t buffer_offset = 4 * (
buffer_pos[2] * size[0] * size[1] +
buffer_pos[1] * size[0] + buffer_pos[0]
);

volume_get_at(volume, NULL, volume_pos, &buffer[buffer_offset]);
}
}
}

for (pos[0] = 0; pos[0] < size[0]; pos[0]++) {
for (pos[1] = 0; pos[1] < size[1]; pos[1]++) {
for (pos[2] = 0; pos[2] < size[2]; pos[2]++) {
memcpy(volume_pos, pos, sizeof(pos));

for (int i = 0; i < 3; i++) {
volume_pos[i] += aabb[0][i];
}

size_t buffer_offset = 4 * (
pos[2] * size[0] * size[1] +
pos[1] * size[0] + pos[0]
);

volume_set_at(volume, NULL, volume_pos, &buffer[buffer_offset]);
}
}
}

free(buffer);
}
1 change: 0 additions & 1 deletion src/volume.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,5 @@ typedef struct {
} volume_global_stats_t;

void volume_get_global_stats(volume_global_stats_t *stats);
void volume_wrap(volume_t *volume, int axis, int sign, int aabb[2][3]);

#endif // VOLUME_H

0 comments on commit 072efa0

Please sign in to comment.