From 2b410eee24e97d7385664dd1cbd62c0e8de24bd9 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Sat, 22 Nov 2025 17:12:33 +0100 Subject: [PATCH] fixed it!!!! --- .gitignore | 1 + build | 10 ++++++++- src/SomaSolve.c | 29 ++++++++++++-------------- src/SomaSolve.h | 4 ++-- src/VoxelSpace.c | 54 +++++++++++++++++++++--------------------------- src/VoxelSpace.h | 46 ++++++++++++++++++++--------------------- src/main.c | 14 ++++++------- src/tests.c | 22 ++++++++++---------- 8 files changed, 90 insertions(+), 90 deletions(-) diff --git a/.gitignore b/.gitignore index 2bbba39..12cce9b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .cache .vscode /target +perf.data diff --git a/build b/build index 63daf03..fbc481b 100755 --- a/build +++ b/build @@ -1,6 +1,14 @@ #!/bin/bash LIB_INCLUDE="-lglfw -lGL -lm" -gcc -g -g2 -DOS_LINUX=1 -DDJSTDLIB_DEBUG=1 -xc -std=c99 ./src/main.c -o ./target/somaesque $LIB_INCLUDE + +echo [Building target] +if [ $DEBUG ]; then + time clang -O0 -g -g2 -DOS_LINUX=1 -DDJSTDLIB_DEBUG=1 -xc -std=c99 ./src/main.c -o ./target/somaesque $LIB_INCLUDE +else + time clang -O2 -DOS_LINUX=1 -xc -std=c99 ./src/main.c -o ./target/somaesque $LIB_INCLUDE +fi +echo [Target built] ./target/somaesque + diff --git a/src/SomaSolve.c b/src/SomaSolve.c index 8006f24..3e0f1f8 100644 --- a/src/SomaSolve.c +++ b/src/SomaSolve.c @@ -74,8 +74,6 @@ struct Solver { SomaSolutionList *solutions; }; -uint64 STD_SOMA[] = { 23ul, 30ul, 15ul, 1043ul, 24594ul, 12306ul, 11ul }; - /* void backtrack_solve_iter(std::vector *polycube_input, std::vector *offsets) { size_t num_inputs = offsets->size() - 1; @@ -161,7 +159,7 @@ SomaSolutionList getSolutionRotations(Arena *arena, SomaSolution *solution, int result.data[i] = PushList(arena, SomaSolution, solution->length); } for (int piece_i = 0; piece_i < solution->length; piece_i++) { - Space space = { + VoxelSpace space = { solution->data[piece_i], dims[0], dims[1], @@ -222,15 +220,15 @@ uint64 factorial(int n) { return result; } -SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { +SomaSolutionList solve(VoxelSpaceReprList reprsInput, int dims[3]) { Arena *arena = arenaAlloc(Megabytes(64)); Arena *permsArena = arenaAlloc(Megabytes(128)); - OffsetList offsets = PushList(arena, OffsetList, reprs_in_count + 1); + OffsetList offsets = PushList(arena, OffsetList, reprsInput.length + 1); VoxelSpaceReprList polycubes = PushList(arena, VoxelSpaceReprList, 0); - Space empty_voxel_space = { + VoxelSpace emptyVoxelSpace = { 0, dims[0], dims[1], @@ -242,10 +240,10 @@ SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { uint64 possibleCombos = 0; { - Space space = empty_voxel_space; - space.space = reprs_in[0]; - cullEmptySpace(&space); - VoxelSpaceReprList positions = getAllPositionsInPrism(permsArena, &space, dims); + VoxelSpace voxelSpace = emptyVoxelSpace; + voxelSpace.space = reprsInput.data[0]; + cullEmptySpace(&voxelSpace); + VoxelSpaceReprList positions = getAllPositionsInPrism(permsArena, &voxelSpace, dims); possibleCombos += positions.length; VoxelSpaceReprList_underlying *insertion = PushArray(arena, uint64, positions.capacity); polycubes.capacity += positions.capacity; @@ -253,10 +251,10 @@ SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { memcpy(insertion, positions.data, positions.capacity * ListElementSize(VoxelSpaceReprList)); }; - for (size_t i = 1; i < reprs_in_count; i++) { + for (size_t i = 1; i < reprsInput.length; i++) { AppendList(&offsets, polycubes.capacity); - Space space = empty_voxel_space; - space.space = reprs_in[i]; + VoxelSpace space = emptyVoxelSpace; + space.space = reprsInput.data[i]; cullEmptySpace(&space); VoxelSpaceReprList perms = getAllPermutationsInPrism(permsArena, &space, dims); possibleCombos *= perms.length; @@ -269,7 +267,7 @@ SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { AppendList(&offsets, polycubes.length); SomaSolutionList solutions = PushList(permsArena, SomaSolutionList, (size_t)floor(sqrt(possibleCombos))); - AppendList(&solutions, PushFullList(permsArena, SomaSolution, reprs_in_count)); + AppendList(&solutions, PushFullList(permsArena, SomaSolution, reprsInput.length)); Solver solver = { &polycubes, @@ -282,12 +280,11 @@ SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_count, int dims[3]) { return filterUnique(permsArena, solver.solutions, dims); } - void interactiveCmdLineSolveSoma() { //get_dims_input(dims); //std::cout << '\n'; //std::vector reprs = get_reprs_input(dims[0]*dims[1]*dims[2]); print("Great. Calculating solutions...\n"); - SomaSolutionList solutions = solve(STD_SOMA, ArrayCount(STD_SOMA), (int[]){ 3, 3, 3 }); + SomaSolutionList solutions = solve(AsList(VoxelSpaceReprList, STD_SOMA), (int[]){ 3, 3, 3 }); print("%zu solutions found.\n", solutions.length); } diff --git a/src/SomaSolve.h b/src/SomaSolve.h index 0d9f437..67c5a10 100644 --- a/src/SomaSolve.h +++ b/src/SomaSolve.h @@ -1,9 +1,9 @@ #include "VoxelSpace.h" #include "lib/djstdlib/core.h" -extern uint64 STD_SOMA[]; +#define STD_SOMA { 23ul, 30ul, 15ul, 1043ul, 24594ul, 12306ul, 11ul } typedef VoxelSpaceReprList SomaSolution; DefineList(SomaSolution, SomaSolution); -SomaSolutionList solve(uint64 *reprs_in, uint32 reprs_in_length, int dims[3]); +SomaSolutionList solve(VoxelSpaceReprList reprs_in, int dims[3]); void interactive_cmd_line_solve_soma(); diff --git a/src/VoxelSpace.c b/src/VoxelSpace.c index 886803c..a0d5591 100644 --- a/src/VoxelSpace.c +++ b/src/VoxelSpace.c @@ -10,7 +10,7 @@ int index(int dim_y, int dim_z, int x, int y, int z) { // │ 0, 0, -1 │ * │ y │ = │-z │ // │ 0, 1, 0 │ │ z │ │ y │ // └ ┘ └ ┘ └ ┘ -int newIndexRotX(Space *space, int x, int y, int z) { +int newIndexRotX(VoxelSpace *space, int x, int y, int z) { return space->dim_z * space->dim_y * x + space->dim_y * (space->dim_z - 1 - z) + y; } @@ -19,7 +19,7 @@ int newIndexRotX(Space *space, int x, int y, int z) { // │ 0, 1, 0 │ * │ y │ = │-y │ // │ -1, 0, 0 │ │ z │ │ x │ // └ ┘ └ ┘ └ ┘ -int newIndexRotY(Space *space, int x, int y, int z) { +int newIndexRotY(VoxelSpace *space, int x, int y, int z) { return space->dim_y * space->dim_x * z + space->dim_x * y + (space->dim_x - 1 - x); } @@ -28,7 +28,7 @@ int newIndexRotY(Space *space, int x, int y, int z) { // │ 1, 0, 0 │ * │ y │ = │ x │ // │ 0, 0, 1 │ │ z │ │ z │ // └ ┘ └ ┘ └ ┘ -int newIndexRotZ(Space *space, int x, int y, int z) { +int newIndexRotZ(VoxelSpace *space, int x, int y, int z) { return space->dim_x * space->dim_z * (space->dim_y - 1 - y) + space->dim_z * x + z; } @@ -50,18 +50,12 @@ inline bool collides(uint64 a, uint64 b) { return (a | b) != (a ^ b); } -/* -bool collides(Space *a, Space *b) { - return (a->space | b->space) != (a->space ^ b->space); -} -*/ - -bool filledAt(Space *space, int x, int y, int z) { +inline bool filledAt(VoxelSpace *space, int x, int y, int z) { uint64 mask = 1ull << (space->dim_y * space->dim_z * x + space->dim_z * y + z); return (space->space & mask) != 0ull; } -Extrema getExtrema(Space *space) { +Extrema getExtrema(VoxelSpace *space) { Extrema extrema = { 0, space->dim_x, @@ -89,7 +83,7 @@ Extrema getExtrema(Space *space) { return extrema; } -void cullEmptySpace(Space *space) { +void cullEmptySpace(VoxelSpace *space) { Extrema extrema = getExtrema(space); int space_index = 0; uint64 newSpace = 0ull; @@ -109,7 +103,7 @@ void cullEmptySpace(Space *space) { space->space = newSpace; } -void rotate90X(Space *space) { +void rotate90X(VoxelSpace *space) { uint64 new_space = 0; for (int x = 0; x < space->dim_x; x++) { for (int y = 0; y < space->dim_y; y++) { @@ -126,7 +120,7 @@ void rotate90X(Space *space) { space->space = new_space; } -void rotate90Y(Space *space) { +void rotate90Y(VoxelSpace *space) { uint64 new_space = 0; for (int x = 0; x < space->dim_x; x++) { for (int y = 0; y < space->dim_y; y++) { @@ -143,7 +137,7 @@ void rotate90Y(Space *space) { space->space = new_space; } -void rotate90Z(Space *space) { +void rotate90Z(VoxelSpace *space) { uint64 new_space = 0; for (int x = 0; x < space->dim_x; x++) { for (int y = 0; y < space->dim_y; y++) { @@ -160,15 +154,15 @@ void rotate90Z(Space *space) { space->space = new_space; } -bool isMatch(Space *a, Space *b) { - return a->space == b->space +bool isMatch(VoxelSpace *a, VoxelSpace *b) { + return a->space == b->space && a->dim_x == b->dim_x && a->dim_y == b->dim_y && a->dim_z == b->dim_z; } -void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, Space* spaceToSpin) { - Space spins[4] = {}; +void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, VoxelSpace* spaceToSpin) { + VoxelSpace spins[4] = {}; spins[0] = *spaceToSpin; for (int i = 0; i < 3; i++) { spins[i + 1] = spins[i]; @@ -188,17 +182,17 @@ void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, Space* spaceToSpin) { } } -void pushXAxisSpins(Arena *arena, VoxelSpaceList *existingSpaces, Space* spaceToSpin) { - Space refSpace = *spaceToSpin; +void pushXAxisSpins(Arena *arena, VoxelSpaceList *existingSpaces, VoxelSpace* spaceToSpin) { + VoxelSpace refSpace = *spaceToSpin; for (int i = 0; i < 4; i++) { rotate90X(&refSpace); AppendList(existingSpaces, refSpace); } } -VoxelSpaceList getUniqueRotations(Arena *arena, Space *space) { +VoxelSpaceList getUniqueRotations(Arena *arena, VoxelSpace *space) { VoxelSpaceList rotations = PushList(arena, VoxelSpaceList, 24); - Space refSpace = *space; + VoxelSpace refSpace = *space; cullEmptySpace(&refSpace); pushNewUniqueSpins(&rotations, &refSpace); rotate90Y(&refSpace); @@ -217,9 +211,9 @@ VoxelSpaceList getUniqueRotations(Arena *arena, Space *space) { return rotations; } -VoxelSpaceList getAllRotations(Arena *arena, Space *space) { +VoxelSpaceList getAllRotations(Arena *arena, VoxelSpace *space) { VoxelSpaceList rotations = PushList(arena, VoxelSpaceList, 24); - Space refSpace = *space; + VoxelSpace refSpace = *space; pushXAxisSpins(arena, &rotations, &refSpace); rotate90Y(&refSpace); pushXAxisSpins(arena, &rotations, &refSpace); @@ -235,7 +229,7 @@ VoxelSpaceList getAllRotations(Arena *arena, Space *space) { return rotations; } -VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]) { +VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, VoxelSpace *space, int prism_dims[3]) { if (space->dim_x > prism_dims[0] || space->dim_y > prism_dims[1] || space->dim_z > prism_dims[2]) { return (VoxelSpaceReprList)EmptyList(); } @@ -272,7 +266,7 @@ VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, Space *space, int prism_ return result; } -VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]) { +VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, VoxelSpace *space, int prism_dims[3]) { Scratch temp = scratchStart(&arena, 1); VoxelSpaceList rotations = getUniqueRotations(temp.arena, space); @@ -299,17 +293,17 @@ int size(uint64 space) { return size; } -bool spaceValueAt(Space *space, int x, int y, int z) { +bool spaceValueAt(VoxelSpace *space, int x, int y, int z) { uint64 mask = 1 << (space->dim_y * space->dim_z * x + space->dim_z * y + z); return (space->space & mask) != 0; } -void spaceToggle(Space *space, int x, int y, int z) { +void spaceToggle(VoxelSpace *space, int x, int y, int z) { uint64 mask = 1 << (space->dim_y * space->dim_z * x + space->dim_z * y + z); space->space ^= mask; } -void spaceSet(Space *space, bool val, int x, int y, int z) { +void spaceSet(VoxelSpace *space, bool val, int x, int y, int z) { uint64 mask = 1 << (space->dim_y * space->dim_z * x + space->dim_z * y + z); if (val) { space->space |= mask; diff --git a/src/VoxelSpace.h b/src/VoxelSpace.h index 21c24bb..f14e0d1 100644 --- a/src/VoxelSpace.h +++ b/src/VoxelSpace.h @@ -15,62 +15,62 @@ struct Extrema { int zMin; }; -typedef struct Space Space; -struct Space { +typedef struct VoxelSpace VoxelSpace; +struct VoxelSpace { uint64 space; int dim_x; int dim_y; int dim_z; }; -DefineList(Space, VoxelSpace); +DefineList(VoxelSpace, VoxelSpace); DefineList(uint64, VoxelSpaceRepr); -int newIndexRotX(Space *space, int x, int y, int z); +int newIndexRotX(VoxelSpace *space, int x, int y, int z); -int newIndexRotY(Space *space, int x, int y, int z); +int newIndexRotY(VoxelSpace *space, int x, int y, int z); -int newIndexRotZ(Space *space, int x, int y, int z); +int newIndexRotZ(VoxelSpace *space, int x, int y, int z); uint64 toggle(uint64 space, int index); uint64 set(uint64 space, int index, bool val); -//bool collides(Space *a, Space *b); +//bool collides(VoxelSpace *a, VoxelSpace *b); bool collides(uint64 a, uint64 b); -Space add(Space *a, Space *b); +VoxelSpace add(VoxelSpace *a, VoxelSpace *b); -bool filledAt(Space *space, int x, int y, int z); +bool filledAt(VoxelSpace *space, int x, int y, int z); -Extrema getExtrema(Space *space); +Extrema getExtrema(VoxelSpace *space); -void cullEmptySpace(Space *space); +void cullEmptySpace(VoxelSpace *space); -bool isMatch(Space *a, Space *b); +bool isMatch(VoxelSpace *a, VoxelSpace *b); -void rotate90X(Space *space); +void rotate90X(VoxelSpace *space); -void rotate90Y(Space *space); +void rotate90Y(VoxelSpace *space); -void rotate90Z(Space *space); +void rotate90Z(VoxelSpace *space); -void pushNewUniqueSpins(VoxelSpaceList *existingSpaces, Space* spaceToSpin); +void pushNewUniqueSpins(VoxelSpaceList *existingVoxelSpaces, VoxelSpace* spaceToSpin); -VoxelSpaceList getUniqueRotations(Arena *arena, Space *space); +VoxelSpaceList getUniqueRotations(Arena *arena, VoxelSpace *space); -VoxelSpaceList getAllRotations(Arena *arena, Space *space); +VoxelSpaceList getAllRotations(Arena *arena, VoxelSpace *space); -VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, Space *space, int prism_dims[3]); +VoxelSpaceReprList getAllPositionsInPrism(Arena *arena, VoxelSpace *space, int prism_dims[3]); -VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, Space *space, int prism_dims[3]); +VoxelSpaceReprList getAllPermutationsInPrism(Arena *arena, VoxelSpace *space, int prism_dims[3]); int size(uint64 space); -bool spaceValueAt(Space *space, int x, int y, int z); +bool spaceValueAt(VoxelSpace *space, int x, int y, int z); -void spaceToggle(Space *space, int x, int y, int z); +void spaceToggle(VoxelSpace *space, int x, int y, int z); -void spaceSet(Space *space, bool val, int x, int y, int z); +void spaceSet(VoxelSpace *space, bool val, int x, int y, int z); #endif diff --git a/src/main.c b/src/main.c index 382f942..3e76725 100644 --- a/src/main.c +++ b/src/main.c @@ -79,7 +79,7 @@ struct Frame { typedef struct Polycube Polycube; struct Polycube { uint32 entityHandle; - Space repr; + VoxelSpace repr; RLVector4 color; }; DefineList(Polycube, Polycube); @@ -145,7 +145,7 @@ struct Renderer { typedef struct PolycubeInput PolycubeInput; struct PolycubeInput { - Space repr; + VoxelSpace repr; RLVector4 color; }; DefineList(PolycubeInput, PolycubeInput); @@ -371,7 +371,7 @@ void processInput(Soma *soma) { } } -Polycube createPolycubeFromRepr(Arena *arena, Scene *s, Space *repr, RLVector4 color) { +Polycube createPolycubeFromRepr(Arena *arena, Scene *s, VoxelSpace *repr, RLVector4 color) { uint32 polycubeMainEntityHandle = createEntity(arena, s); Entity *polycubeMainEntity = getEntity(s, polycubeMainEntityHandle); for (int x = 0; x < repr->dim_x; x++) { @@ -673,8 +673,9 @@ int mainGfx() { cubeMesh = createMesh("./assets/models/cube.obj"); wallTex = createTexture("./assets/textures/brick-wall.jpg"); - for (EachInArray(STD_SOMA, i)) { - Space voxelSpace = { STD_SOMA[i], 3, 3, 3 }; + VoxelSpaceReprList stdSoma = AsList(VoxelSpaceReprList, STD_SOMA); + for (EachIn(stdSoma, i)) { + VoxelSpace voxelSpace = { stdSoma.data[i], 3, 3, 3 }; RLVector4 color = colorFromIndex(i); PolycubeInput input = (PolycubeInput){ voxelSpace, color }; AppendList(&soma.state.polycubeInput, input); @@ -713,7 +714,7 @@ int mainGfx() { Polycube *currentPolycube = &soma.state.polycubes.data[soma.state.currentPolycube]; PolycubeInput *pinput = &soma.state.polycubeInput.data[soma.state.currentPolycube]; removeEntity(soma.scene, currentPolycube->entityHandle); - Space culledRepr = pinput->repr; + VoxelSpace culledRepr = pinput->repr; cullEmptySpace(&culledRepr); Polycube newPolycube = createPolycubeFromRepr(arena, soma.scene, &culledRepr, pinput->color); SceneGraphNode *graphNode = getSceneGraphNodeForEntity(soma.scene, newPolycube.entityHandle); @@ -758,7 +759,6 @@ int mainGfx() { int main() { initialiseDjStdCore(); - test(); return mainCmd(); } diff --git a/src/tests.c b/src/tests.c index 25e94a0..b87f8db 100644 --- a/src/tests.c +++ b/src/tests.c @@ -11,7 +11,7 @@ DefineList(MismatchData, MismatchData); void test() { { - Space space = {}; + VoxelSpace space = {}; space.space=23ull; space.dim_x=3; space.dim_y=3; @@ -50,7 +50,7 @@ void test() { }; { - Space space1 = {}; + VoxelSpace space1 = {}; space1.space=172ull; space1.dim_x=3; space1.dim_y=3; @@ -68,14 +68,14 @@ void test() { { Arena *arena = arenaAlloc(Megabytes(64)); - Space space1 = {}; + VoxelSpace space1 = {}; space1.space=30ull; space1.dim_x=3; space1.dim_y=3; space1.dim_z=3; VoxelSpaceList rotations = getUniqueRotations(arena, &space1); - Space expected_rots[] = { + VoxelSpace expected_rots[] = { { 30ull, 1, 2, 3 }, { 45ull, 1, 3, 2 }, { 30ull, 3, 2, 1 }, @@ -104,7 +104,7 @@ void test() { { Arena *arena = arenaAlloc(Megabytes(64)); - Space space1 = {}; + VoxelSpace space1 = {}; space1.space=30ul; space1.dim_x=3; space1.dim_y=3; @@ -199,19 +199,19 @@ void test() { } { - Space space1 = {}; + VoxelSpace space1 = {}; space1.space=30ull; space1.dim_x=3; space1.dim_y=3; space1.dim_z=3; - Space space2 = {}; + VoxelSpace space2 = {}; space2.space=30ull; space2.dim_x=3; space2.dim_y=3; space2.dim_z=3; - Space space3 = {}; + VoxelSpace space3 = {}; space3.space=30ull; space3.dim_x=3; space3.dim_y=3; @@ -230,7 +230,7 @@ void test() { int dims[] = {3, 3, 3}; - Space space1 = {}; + VoxelSpace space1 = {}; space1.space=30ull; space1.dim_x=3; space1.dim_y=3; @@ -246,7 +246,7 @@ void test() { 62914560ull, }; - Space space2 = {}; + VoxelSpace space2 = {}; space2.space=23ul; space2.dim_x=3; space2.dim_y=3; @@ -262,7 +262,7 @@ void test() { 48234496ull, }; - Space space3 = {}; + VoxelSpace space3 = {}; space3.space=15ull; space3.dim_x=3; space3.dim_y=3;