Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/dynamicvoronoi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ DynamicVoronoi::~DynamicVoronoi() {
}

void DynamicVoronoi::initializeEmpty(int _sizeX, int _sizeY, bool initGridMap) {
sizeX = _sizeX;
sizeY = _sizeY;
if (data) {
for (int x=0; x<sizeX; x++) delete[] data[x];
delete[] data;
}
data = new dataCell*[sizeX];
for (int x=0; x<sizeX; x++) data[x] = new dataCell[sizeY];

if (initGridMap) {
if (gridMap) {
for (int x=0; x<sizeX; x++) delete[] gridMap[x];
delete[] gridMap;
}
}

sizeX = _sizeX;
sizeY = _sizeY;

data = new dataCell*[sizeX];
for (int x=0; x<sizeX; x++) data[x] = new dataCell[sizeY];

if (initGridMap) {
gridMap = new bool*[sizeX];
for (int x=0; x<sizeX; x++) gridMap[x] = new bool[sizeY];
}
Expand All @@ -60,6 +64,12 @@ void DynamicVoronoi::initializeEmpty(int _sizeX, int _sizeY, bool initGridMap) {
}

void DynamicVoronoi::initializeMap(int _sizeX, int _sizeY, bool** _gridMap) {
// release memory used by existing gridMap
if (gridMap && gridMap != _gridMap) {
for (int x=0; x<sizeX; x++) delete[] gridMap[x];
delete[] gridMap;
}

gridMap = _gridMap;
initializeEmpty(_sizeX, _sizeY, false);

Expand Down
35 changes: 35 additions & 0 deletions tests/test_voronoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "../include/dynamicvoronoi.h"
#include <iostream>

using namespace HybridAStar;

int main() {
DynamicVoronoi voronoi;

// Create a 10x10 map
int width = 10;
int height = 10;
bool** map1 = new bool*[width];
for (int i = 0; i < width; ++i) {
map1[i] = new bool[height]();
}

// Initialize map
std::cout << "Initializing 10x10 map..." << std::endl;
voronoi.initializeMap(width, height, map1);

// Create a 20x20 map
int new_width = 20;
int new_height = 20;
bool** map2 = new bool*[new_width];
for (int i = 0; i < new_width; ++i) {
map2[i] = new bool[new_height]();
}

// Re-initialize map (which should free previous map resources or cause bug with current code)
std::cout << "Re-initializing with 20x20 map..." << std::endl;
voronoi.initializeMap(new_width, new_height, map2);

std::cout << "Done!" << std::endl;
return 0;
}