12.4. Reading information from the tree
Once you have built a BSP tree, you can retrieve information from any node. The node gives you free access to its fields :
class TCODBsp {
public :
int x,y,w,h; //
int position; // position of splitting
bool horizontal; // horizontal splitting ?
uint8 level; // level in the tree
...
}
typedef struct {
int x,y,w,h;
int position;
bool horizontal;
uint8 level;
...
} TCOD_bsp_t;
class TCODBsp
{
public int x { get; set; }
public int y { get; set; }
public int h { get; set; }
public int w { get; set; }
public int position { get; set; }
public bool horizontal { get; set; }
public byte level { get; set; }
}
Parameter | Description |
---|---|
x,y,w,h | Rectangular region covered by this node. |
position | If this node is not a leaf, splitting position. |
horizontal | If this node is not a leaf, splitting orientation. |
level | Level in the BSP tree (0 for the root, 1 for the root's sons, ...). |
Navigate in the tree
You can navigate from a node to its sons or its parent using one of those functions. Each function returns NULL if the corresponding node does not exists (if the node is not splitted for getLeft and getRight, and if the node is the root node for getFather).
TCODBsp *TCODBsp::getLeft() const
TCODBsp *TCODBsp::getRight() const
TCODBsp *TCODBsp::getFather() const
TCOD_bsp_t * TCOD_bsp_left(TCOD_bsp_t *node)
TCOD_bsp_t * TCOD_bsp_right(TCOD_bsp_t *node)
TCOD_bsp_t * TCOD_bsp_father(TCOD_bsp_t *node)
bsp_left(node)
bsp_right(node)
bsp_father(node)
TCODBsp TCODBsp::getLeft()
TCODBsp TCODBsp::getRight()
TCODBsp TCODBsp::getFather()
Parameter | Description |
---|---|
node | In the C version, the node reference. |
Checking if a node is a leaf
You can know if a node is a leaf (not splitted, no sons) with this function :
bool TCODBsp::isLeaf() const
bool TCOD_bsp_is_leaf(TCOD_bsp_t *node)
bsp_is_leaf(node)
bool TCODBsp::isLeaf()
Check if a cell is inside a node
You can check if a map cell is inside a node.
bool TCODBsp::contains(int cx, int cy) const
bool TCOD_bsp_contains(TCOD_bsp_t *node, int cx, int cy)
bsp_contains(node, cx, cy)
bool TCODBsp::contains(int x, int y)
Parameter | Description |
---|---|
node | In the C version, the node reference. |
cx,cy | Map cell coordinates. |
Getting the node containing a cell
You can search the tree for the smallest node containing a map cell. If the cell is outside the tree, the function returns NULL :
TCODBsp *TCODBsp::findNode(int cx, int cy)
TCOD_bsp_t * TCOD_bsp_find_node(TCOD_bsp_t *node, int cx, int cy)
bsp_find_node(node, cx, cy)
TCODBsp TCODBsp::findNode(int x, int y)
Parameter | Description |
---|---|
node | In the C version, the node reference. |
cx,cy | Map cell coordinates. |