-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsp.h
More file actions
91 lines (74 loc) · 2.84 KB
/
Copy pathbsp.h
File metadata and controls
91 lines (74 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* R_BSP.h - A Lightweight Binary Space Partitioning (BSP) Layout Manager like bspwm
*/
#ifndef R_BSP_H
#define R_BSP_H
#include <stdbool.h>
#include <stddef.h>
#include "arena.h"
/* --------------------------------------------------------------------------
* Macros & Utilities
* -------------------------------------------------------------------------- */
#define RECT_UNWRAP(r) (r).x, (r).y, (r).w, (r).h
#define RECT2RECTANGLE(rect) \
(Rectangle) { RECT_UNWRAP(rect) }
/* --------------------------------------------------------------------------
* Data Structures
* -------------------------------------------------------------------------- */
typedef struct Rect
{
float x, y, w, h;
} Rect;
typedef struct BSPNode BSPNode;
typedef struct
{
Rect area;
} Axes;
struct BSPNode
{
Rect area; /**< Physical bounds calculated during bsp_set_area */
BSPNode *a; /**< Left or Top child */
BSPNode *b; /**< Right or Bottom child */
float split_ratio; /**< 0.0 to 1.0 (relative to parent size) */
bool vertical; /**< True for Left|Right, False for Top/Bottom */
void *user_data; /**< Generic pointer for user-defined data (e.g., Plot objects) */
};
typedef struct
{
float margin; /**< Uniform spacing between nodes and screen borders */
BSPNode *root; /**< Root of the layout tree */
Arena *pool; /**< Arena memory for fast, zero-fragmentation node allocation */
} BSPContext;
/* --------------------------------------------------------------------------
* Public API
* -------------------------------------------------------------------------- */
/**
* Initializes the BSP system and allocates an initial root node.
* @param margin Space (in pixels) between elements.
* @param pool_size Bytes to allocate for the internal Arena pool.
*/
BSPContext *bsp_init(float margin, size_t pool_size);
/**
* Performs a single split on a leaf node.
* @return The parent node that was just split.
*/
BSPNode *bsp_split(BSPContext *ctx, BSPNode *node, float split_ratio, bool vertical);
/**
* Divides a node into multiple segments using integer weights (e.g., {1, 2, 1}).
* * @param count Number of segments to create.
* @param weights Array of relative proportions.
* @param vertical If true, splits into columns; if false, splits into rows.
* @param out_leaves (Optional) An array of size [count] to store pointers to the resulting leaf nodes.
*/
void bsp_split_multi(BSPContext *ctx, BSPNode *node, int count, int *weights, bool vertical, BSPNode **out_leaves);
/**
* Recursively calculates the physical Rect areas for the entire tree.
* Call this whenever the window resizes or the tree structure changes.
* returns whether any dimension has 0
*/
void bsp_set_area(BSPContext *ctx, Rect total_area);
/**
* Debug utility to print the tree structure to the console.
*/
void bsp_print(BSPNode *node);
#endif // R_BSP_H