ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Node.h
Go to the documentation of this file.
1/**
2 * @file Node.h
3 * @brief Defines the Node class for quadtree spatial partitioning in 2D space.
4 *
5 * The Node class represents a node in a quadtree structure, designed to manage
6 * spatial partitioning of 2D entities like triangles. It supports subdivision,
7 * entity insertion, and bounding box management.
8 *
9 * @date 22/12/2024
10 * @version 1.0
11 * @author moepforfreedom, Coela Can't
12 */
13
14#pragma once
15
16#include "../../Physics/Utils/BoundingBox2D.h"
17#include "Triangle2D.h"
18
19/**
20 * @class Node
21 * @brief Represents a node in a quadtree structure for spatial partitioning.
22 */
23class Node {
24private:
25 static const uint16_t maxEntities = 8; ///< Maximum number of entities a node can hold before subdividing.
26 static const uint8_t maxDepth = 8; ///< Maximum depth of the quadtree.
27 uint16_t count = 0; ///< Current number of entities in the node.
28 uint16_t capacity = 0; ///< Capacity of entities allocated in the node.
29 Node* childNodes = nullptr; ///< Pointer to the child nodes of this node.
30 Triangle2D** entities = nullptr; ///< Array of entities (triangles) contained in the node.
31 BoundingBox2D bbox; ///< Bounding box defining the spatial area of this node.
32
33 /**
34 * @brief Creates child nodes for this node.
35 */
36 void CreateChildNodes();
37
38 /**
39 * @brief Distributes entities to child nodes after subdivision.
40 * @return The number of entities successfully distributed to child nodes.
41 */
42 uint16_t DistributeEntities();
43
44 /**
45 * @brief Determines whether the node should be subdivided.
46 * @param childEntitySum Total number of entities in child nodes.
47 * @return True if the node should subdivide, otherwise false.
48 */
49 bool ShouldSubdivide(uint16_t childEntitySum);
50
51public:
52 /**
53 * @brief Constructs a Node with specified bounds.
54 * @param min Minimum bounds of the node.
55 * @param max Maximum bounds of the node.
56 */
57 Node(const Vector2D& min, const Vector2D& max);
58
59 /**
60 * @brief Destructor for the Node class.
61 */
62 ~Node();
63
64 /**
65 * @brief Retrieves the bounding box of the node.
66 * @return Pointer to the BoundingBox2D object.
67 */
69
70 /**
71 * @brief Retrieves the child nodes of this node.
72 * @return Pointer to the array of child nodes.
73 */
75
76 /**
77 * @brief Retrieves the entities contained in this node.
78 * @return Pointer to the array of Triangle2D entities.
79 */
81
82 /**
83 * @brief Retrieves the current count of entities in the node.
84 * @return Number of entities in the node.
85 */
86 uint16_t GetCount();
87
88 /**
89 * @brief Expands the node's capacity to accommodate more entities.
90 * @param newCount New capacity for the node.
91 */
92 void Expand(uint16_t newCount);
93
94 /**
95 * @brief Inserts a triangle entity into the node.
96 * @param triangle Pointer to the Triangle2D entity to be inserted.
97 * @return True if the entity was successfully inserted, otherwise false.
98 */
99 bool Insert(Triangle2D* triangle);
100
101 /**
102 * @brief Subdivides the node into child nodes if needed.
103 * @param depth Current depth of the node in the quadtree.
104 */
105 void Subdivide(uint8_t depth = 0);
106
107 /**
108 * @brief Checks if the node is a leaf node (i.e., has no child nodes).
109 * @return True if the node is a leaf, otherwise false.
110 */
111 bool IsLeaf();
112};
Defines the Triangle2D class for representing and manipulating 2D triangles.
Represents a 2D axis-aligned bounding box.
Represents a node in a quadtree structure for spatial partitioning.
Definition Node.h:23
BoundingBox2D * GetBBox()
Retrieves the bounding box of the node.
Definition Node.cpp:14
void Expand(uint16_t newCount)
Expands the node's capacity to accommodate more entities.
Definition Node.cpp:30
bool ShouldSubdivide(uint16_t childEntitySum)
Determines whether the node should be subdivided.
Definition Node.cpp:98
Triangle2D ** GetEntities()
Retrieves the entities contained in this node.
Definition Node.cpp:22
Triangle2D ** entities
Array of entities (triangles) contained in the node.
Definition Node.h:30
uint16_t capacity
Capacity of entities allocated in the node.
Definition Node.h:28
static const uint8_t maxDepth
Maximum depth of the quadtree.
Definition Node.h:26
~Node()
Destructor for the Node class.
Definition Node.cpp:7
BoundingBox2D bbox
Bounding box defining the spatial area of this node.
Definition Node.h:31
bool IsLeaf()
Checks if the node is a leaf node (i.e., has no child nodes).
Definition Node.cpp:69
uint16_t GetCount()
Retrieves the current count of entities in the node.
Definition Node.cpp:26
bool Insert(Triangle2D *triangle)
Inserts a triangle entity into the node.
Definition Node.cpp:43
void Subdivide(uint8_t depth=0)
Subdivides the node into child nodes if needed.
Definition Node.cpp:54
Node * childNodes
Pointer to the child nodes of this node.
Definition Node.h:29
uint16_t DistributeEntities()
Distributes entities to child nodes after subdivision.
Definition Node.cpp:82
static const uint16_t maxEntities
Maximum number of entities a node can hold before subdividing.
Definition Node.h:25
void CreateChildNodes()
Creates child nodes for this node.
Definition Node.cpp:73
uint16_t count
Current number of entities in the node.
Definition Node.h:27
Node * GetChildNodes()
Retrieves the child nodes of this node.
Definition Node.cpp:18
Represents a 2D triangle with support for UV mapping, depth, and intersection testing.
Definition Triangle2D.h:25
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27