ProtoTracer  1.0
Real-time 3D rendering and animation engine
Loading...
Searching...
No Matches
Node.cpp
Go to the documentation of this file.
1#include "Node.h"
2
3#include <Arduino.h>
4
5Node::Node(const Vector2D& min, const Vector2D& max) : bbox(min, max) {}
6
8 if (entities) delete[] entities;
9
10 if (childNodes) delete[] childNodes;
11}
12
13
15 return &bbox;
16}
17
21
25
26uint16_t Node::GetCount() {
27 return count;
28}
29
30void Node::Expand(uint16_t newCount) {
31 Triangle2D** tmp = entities;
32 entities = new Triangle2D*[newCount];
33
34 for (uint16_t i = 0; i < newCount; ++i) {
35 if (i < count) entities[i] = tmp[i];
36 else entities[i] = nullptr;
37 }
38
39 delete[] tmp;
40 capacity = newCount;
41}
42
43bool Node::Insert(Triangle2D* triangle) {
44 if (!triangle->DidIntersect(&bbox)) return false;
45
47
48 entities[count] = triangle;
49 ++count;
50
51 return true;
52}
53
54void Node::Subdivide(uint8_t depth) {
55 if (depth == maxDepth) return;
56
58
60 for (uint8_t i = 0; i < 4; ++i) {
61 //Subdivide if child has greater entity count than max
62 if (childNodes[i].count > maxEntities) childNodes[i].Subdivide(depth + 1);
63 }
64 }
65
66 count = 0;//subdivided, so entities are removed
67}
68
70 return !childNodes;
71}
72
74 childNodes = new Node[4]{
75 Node(bbox.GetMinimum(), bbox.GetCenter()),
76 Node(Vector2D(bbox.GetCenter().X, bbox.GetMinimum().Y), Vector2D(bbox.GetMaximum().X, bbox.GetCenter().Y)),
77 Node(Vector2D(bbox.GetMinimum().X, bbox.GetCenter().Y), Vector2D(bbox.GetCenter().X, bbox.GetMaximum().Y)),
78 Node(bbox.GetCenter(), bbox.GetMaximum())
79 };
80}
81
83 uint16_t entityCount = 0;
84
85 //Fills child nodes with valid entities
86 for (uint8_t i = 0; i < 4; ++i) {
87 for (uint16_t j = 0; j < count; ++j) {
88 entityCount += childNodes[i].Insert(entities[j]);
89 }
90 }
91
92 delete[] entities;//Delete current nodes reference to entities
93 entities = nullptr;
94
95 return entityCount;
96}
97
98bool Node::ShouldSubdivide(uint16_t childEntitySum){
99 //Entities divided to have significantly less node than parent
100 return childEntitySum / 2 < count;//childEntitySum / 4 < count / 2 - average amount of child entities / half of count
101}
Defines the Node class for quadtree spatial partitioning in 2D space.
Represents a 2D axis-aligned bounding box.
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
Node(const Vector2D &min, const Vector2D &max)
Constructs a Node with specified bounds.
Definition Node.cpp:5
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
bool DidIntersect(const float &x, const float &y, float &u, float &v, float &w)
Checks if a point intersects the triangle using barycentric coordinates.
Represents a 2D vector (X, Y) and provides methods for vector arithmetic.
Definition Vector2D.h:27