Skip to content

Commit

Permalink
woking on test bsp tree
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAlabar committed Feb 7, 2025
1 parent b88df3b commit 9465c89
Show file tree
Hide file tree
Showing 5 changed files with 472 additions and 22 deletions.
Binary file added cub3D
Binary file not shown.
3 changes: 3 additions & 0 deletions includes/bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,7 @@ t_fixed32 evaluate_seed_quality(t_bsp_line **lines, int count,
unsigned int find_best_seed(t_bsp_line **lines, int count, int depth);
void init_count_data(t_count_data *count);

t_bsp_node *init_node(t_bsp_line **lines, int num_lines);
void free_bsp_node(t_bsp_node *node);

#endif
44 changes: 36 additions & 8 deletions srcs/engine/bsp/bsp_cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,41 @@
* Free all lines stored in a node
* Safe to call with NULL pointers
*/
static void free_node_lines(t_bsp_line **lines, int num_lines)
static void free_node_lines(t_bsp_line **lines, int num_lines,
t_bsp_line *partition)
{
int i;

if (!lines)
return ;
i = -1;
while (++i < num_lines)
i = 0;
while (i < num_lines)
{
if (lines[i])
if (lines[i] && lines[i] != partition)
{
free(lines[i]);
lines[i] = NULL;
}
i++;
}
free(lines);
}

static void free_node_content(t_bsp_node *node)
{
if (!node)
return ;
if (node->lines)
{
free_node_lines(node->lines, node->num_lines, node->partition);
node->lines = NULL;
}
if (node->partition)
{
free(node->partition);
node->partition = NULL;
}
}
/*
** Recursively free a BSP node and all its children
** Safe to call with NULL pointer
Expand All @@ -41,12 +61,16 @@ void free_bsp_node(t_bsp_node *node)
if (!node)
return ;
if (node->front)
{
free_bsp_node(node->front);
node->front = NULL;
}
if (node->back)
{
free_bsp_node(node->back);
if (node->partition)
free(node->partition);
free_node_lines(node->lines, node->num_lines);
node->back = NULL;
}
free_node_content(node);
free(node);
}

Expand Down Expand Up @@ -142,6 +166,10 @@ void free_bsp_tree(t_bsp_tree *tree)
{
if (!tree)
return ;
free_bsp_node(tree->root);
if (tree->root)
{
free_bsp_node(tree->root);
tree->root = NULL;
}
free(tree);
}
45 changes: 45 additions & 0 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,13 +555,58 @@ static void test_line_splitting(void)
ft_printf("\n=== End of Line Splitting Tests ===\n");
}

static void test_bsp_tree_construction(void)
{
t_doom_map *map;
t_bsp_tree *tree;
t_bsp_line **lines;
int num_lines;

ft_printf("\n=== Testing BSP Tree Construction ===\n");

map = create_complex_test_map();
if (!map)
{
ft_printf("Failed to create test map\n");
return ;
}

ft_printf("Input map:\n");
print_map_details(map);

if (!extract_map_lines(map, &lines, &num_lines))
{
ft_printf("Failed to extract lines\n");
free(map);
return ;
}

tree = init_bsp_build(map);
if (!tree)
{
ft_printf("Failed to create BSP tree\n");
free(map);
return ;
}

ft_printf("\nBSP Tree structure:\n");
print_bsp_tree(tree);

ft_printf("\nCleaning up...\n");
free_bsp_tree(tree);
free(map);

ft_printf("=== End of BSP Tree Construction Test ===\n");
}

int main(void)
{
test_map_line_extraction();
test_line_classification();
test_partition_selection();
test_point_classification();
test_line_splitting();
test_bsp_tree_construction();

return (0);
}
Loading

0 comments on commit 9465c89

Please # to comment.