Here is my Quadrant Class:
#ifndef QUADRANT_H
#define QUADRANT_H
#include "Point.h"
#include "Boundary.h"
struct Quadrant : Boundary
{
Quadrant* firstchild = nullptr;
Point* firstPoint = nullptr;
int count = -1;
bool subdivided;
Quadrant(const Boundary& AABB);
void subdivide(std::vector<Quadrant>& m_quadrantStorage);
};
#endif
Here is my Quadrant class CPP:
#include "Quadrant.h"
#include <iostream>
Quadrant::Quadrant(const Boundary& AABB)
:Boundary(AABB), subdivided(false)
{
}
void Quadrant::subdivide(std::vector<Quadrant>& m_quadrantStorage)
{
sf::Vector2f newhalfsize = halfsize / float(2);
Boundary NEB{ sf::Vector2f { centre.x + newhalfsize.x, centre.y - newhalfsize.y }, newhalfsize };
Boundary SEB{ sf::Vector2f { centre.x + newhalfsize.x, centre.y + newhalfsize.y }, newhalfsize };
Boundary SWB{ sf::Vector2f { centre.x - newhalfsize.x, centre.y + newhalfsize.y }, newhalfsize };
Boundary NWB{ sf::Vector2f { centre.x - newhalfsize.x, centre.y - newhalfsize.y }, newhalfsize };
//SIZE OF m_quadrantStorage is 1 AT THIS LINE.
m_quadrantStorage.emplace_back(NEB); //SIZE OF m_quadrantStorage is 2 AT THIS LINE.
firstchild = &m_quadrantStorage.at(1);
m_quadrantStorage.emplace_back(SWB);
m_quadrantStorage.emplace_back(SEB);
m_quadrantStorage.emplace_back(NWB);
//First child is a pointer to the second element in m_quadrantStorage
std::cout << "FIRST CHILD ADDRESS:" << firstchild << std::endl;
std::cout << &m_quadrantStorage[1] << std::endl;
}
Here is my Quadtree class header:
#ifndef QUADTREE_H
#define QUADTREE_H
#include "Quadrant.h"
#include "Point.h"
#include <vector>
class Quadtree
{
public:
Quadtree(sf::RenderWindow* winptr);
void display() const;
void debug();
std::vector<Quadrant> m_quadrantStorage;
private:
sf::RenderWindow* m_winPtr = nullptr;
std::vector<Quadrant*> findleaves(const Boundary& rect);
bool insert(Point* point);
};
#endif
Here is my Quadtree class cpp:
#include "Quadtree.h"
#include "Globals.h"
#include <iostream>
Quadtree::Quadtree(sf::RenderWindow* winptr)
:m_winPtr(winptr)
{ m_quadrantStorage.emplace_back(Boundary{ sf::Vector2f{0.0f,0.0f}, sf::Vector2f{WIDTH / 2,HEIGHT / 2} });
}
void Quadtree::display() const
{
for (const auto& q : m_quadrantStorage)
{
m_winPtr->draw(q);
}
}
std::vector<Quadrant*> Quadtree::findleaves(const Boundary& rect)
{
std::vector<Quadrant*> leaves;
std::vector<Quadrant*> to_process;
to_process.push_back(&m_quadrantStorage[0]);
while (to_process.size() > 0)
{
Quadrant* node = to_process[to_process.size() - 1];
to_process.pop_back();
if (node->count != -1)
{
leaves.push_back(node);
}
else
{
for (int i = 0; i < 4; i++)
{
if (rect.intersects<Quadrant>(*(node + i)))
{
to_process.push_back(node + i);
}
}
}
}
return leaves;
}
void Quadtree::debug()
{
m_quadrantStorage[0].subdivide(m_quadrantStorage);
//std::cout << "{" << (m_quadrantStorage[0].firstchild)->centre.x << "," << (m_quadrantStorage[0].firstchild)->centre.y << "}" << std::endl;
//std::vector<Quadrant*> quadz = findleaves(Boundary{ sf::Vector2f{0,0}, sf::Vector2f{WIDTH / 2,HEIGHT / 2} });
//std::cout << quadz.size() << std::endl;
}
Ok, I have seen from the comments that DDDDDD is indicative of freed heap in debug mode. But I have switched to release and the memory locations are not the same.
After pushing a quadrant object to an external vector which was taken by reference, and saving the memory location of the newly pushed object in a variable, the memory location of the actual object in the vector, and the one in the pointer are not the same.
I get the pointer's memory address to be DDDDDDD and the memory address of the object to be valid. I dont understand what is going on.