Skip to content

Commit f3dd3f2

Browse files
committed
changed obstacleRects and obstacleHash to shared_ptr for safer memory management
1 parent 5d7ce02 commit f3dd3f2

22 files changed

+37
-37
lines changed

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"*hpp*hpp*hpp*hppinitializer_list": "cpp",
4545
"unordered_map": "cpp",
4646
"unordered_set": "cpp",
47-
"*hpp*hppcomplex": "cpp"
47+
"*hpp*hppcomplex": "cpp",
48+
"*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hpp*hppcstdarg": "cpp",
49+
"*hrandom": "cpp",
50+
"random": "cpp"
4851
}
4952
}

SConstruct

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
21
libs= ['m', 'GL', 'GLU', 'glfw',
32
'X11', 'Xrandr', 'Xinerama', 'Xi', 'Xxf86vm', 'Xcursor',
43
'pthread', 'dl', 'boost_system']
54

65

76
env=Environment(CXXFLAGS='-Wall -march=native -std=c++14', LIBS=libs)
7+
88
debug = ARGUMENTS.get('debug', 0)
99
if int(debug):
1010
env.Append(CCFLAGS = '-g')

src/Waldo.hpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class Waldo {
1111
private:
1212
int width;
1313
int height;
14-
int mapArea;
15-
std::vector<std::vector<bool>> *obstacleHash;
16-
std::vector<std::shared_ptr<Rect>> *obstacleRects;
14+
std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash;
15+
std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects;
1716
double maxTravel = 1;
1817
std::thread replanThread;
1918
int velocityHistorySize = 0;
@@ -69,7 +68,7 @@ class Waldo {
6968
unsigned int importance = 0;
7069
double distanceToUav = 0.0;
7170

72-
Waldo(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, int width, int height,
71+
Waldo(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, int width, int height,
7372
int velocityHistorySize) {
7473
this->obstacleHash = obstacleHash;
7574
this->obstacleRects = obstacleRects;

src/generatePaths.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int main(int argc, char* argv[]) {
7777
bool usePseudoRandom = true;
7878

7979
AStar* planner = NULL;
80-
vector<shared_ptr<Rect>> obstacleRects;
80+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects;
8181
vector<vector<bool>> obstacleHash;
8282

8383
for (int mapNum = 0; mapNum < 1000; mapNum++) {

src/generateTruth.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ int main(int argc, char* argv[]) {
8484
options.parse(argc, argv);
8585

8686
for (int mapNum = 0; mapNum < numMaps; mapNum++) {
87-
vector<shared_ptr<Rect>> obstacleRects;
87+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects;
8888
generateObstacleRects(width, height, 10, obstacleRects, OBSTACLE_PADDING);
8989

9090
vector<vector<bool>> obstacleHash(height, vector<bool>(width, false));

src/main.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void drawWaldos(vector<unique_ptr<Waldo>>& waldos) {
3838
}
3939
}
4040

41-
void display(const shared_ptr<RrtNode> root, const shared_ptr<RrtNode>& endNode, deque<Coord>& bestPath, vector<shared_ptr<Rect>>* obstacleRects,
42-
vector<unique_ptr<Waldo>>& waldos, bool shouldDrawTree) {
41+
void display(const shared_ptr<RrtNode> root, const shared_ptr<RrtNode>& endNode, deque<Coord>& bestPath,
42+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, vector<unique_ptr<Waldo>>& waldos, bool shouldDrawTree) {
4343
// drawPoint(root->coord, 10, HSL(25, 1.0, 0.5));
4444
drawSolidCircle(root->coord, VIEW_RADIUS, HSL(25, 1.0, 0.5), 0.2);
4545
drawHollowCircle(root->coord, VIEW_RADIUS, HSL(25, 1.0, 0.5));
@@ -93,23 +93,21 @@ int main(int argc, char* argv[]) {
9393

9494
auto window = initWindow(isFullscreen, monitorNum, width, height);
9595

96-
vector<shared_ptr<Rect>> obstacleRects;
97-
generateObstacleRects(width, height, numObstacles, obstacleRects, OBSTACLE_PADDING);
96+
auto obstacleRects = generateObstacleRects(width, height, numObstacles, OBSTACLE_PADDING);
9897

99-
vector<vector<bool>> obstacleHash(height, vector<bool>(width, false));
100-
generateObstacleHash(obstacleRects, obstacleHash);
98+
auto obstacleHash = generateObstacleHash(width, height, obstacleRects);
10199

102100
vector<unique_ptr<Waldo>> waldos;
103101

104102
for (int w = 0; w < numWaldos; w++) {
105-
waldos.push_back(make_unique<Waldo>(&obstacleHash, &obstacleRects, width, height, waldoHistorySize));
103+
waldos.push_back(make_unique<Waldo>(obstacleHash, obstacleRects, width, height, waldoHistorySize));
106104
}
107105

108106
SamplingPlanner* planner;
109107
if (useFmt) {
110-
planner = new OnlineFmtStar(&obstacleHash, &obstacleRects, 6, width, height, !useHalton, nullptr, pruneRadius);
108+
planner = new OnlineFmtStar(obstacleHash, obstacleRects, 6, width, height, !useHalton, nullptr, pruneRadius);
111109
} else {
112-
planner = new OnlineRrtStar(&obstacleHash, &obstacleRects, 6, width, height, !useHalton, nullptr, pruneRadius);
110+
planner = new OnlineRrtStar(obstacleHash, obstacleRects, 6, width, height, !useHalton, nullptr, pruneRadius);
113111
}
114112
// AStar* planner = new AStar(&obstacleHash, &obstacleRects, width, height, usePseudoRandom);
115113
// PrmStar* planner = new PrmStar(&obstacleHash, &obstacleRects, width, height, usePseudoRandom, GraphType::Grid);
@@ -133,7 +131,7 @@ int main(int argc, char* argv[]) {
133131
auto moveInterval = 1.0 / 30.0;
134132
auto score = 0.0;
135133

136-
auto remainderCallback = [&obstacleHash, &planner, &waldos, &replanInterval, &moveInterval, &trialLength, &startTime, &lastReplan, &lastMove,
134+
auto remainderCallback = [obstacleHash, &planner, &waldos, &replanInterval, &moveInterval, &trialLength, &startTime, &lastReplan, &lastMove,
137135
width, height, voteCellSize, &score, window, &lastPrint]() {
138136
auto currentTime = glfwGetTime();
139137

@@ -151,7 +149,7 @@ int main(int argc, char* argv[]) {
151149
waldo->distanceToUav = euclideanDistance(planner->root->coord, waldo->coord());
152150

153151
if (waldo->importance > 0 && waldo->distanceToUav < VIEW_RADIUS) {
154-
if (!lineIntersectsObstacles(waldo->coord(), planner->root->coord, &obstacleHash, width, height)) {
152+
if (!lineIntersectsObstacles(waldo->coord(), planner->root->coord, obstacleHash, width, height)) {
155153
score += waldo->importance;
156154
vector<Coord> predictedCoords{
157155
// waldo->predictFutureFromRandWalk(60),

src/planning/AStar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct PriorityQueue {
2828
/////////////////////////////
2929
////////////////////////////
3030

31-
AStar::AStar(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, int width, int height, bool usePseudoRandom,
31+
AStar::AStar(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, int width, int height, bool usePseudoRandom,
3232
bool initialize)
3333
: Planner(obstacleHash, obstacleRects, width, height, usePseudoRandom) {
3434
this->name = "astar_visibility";

src/planning/AStar.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AStar : public Planner {
1515
int dy;
1616

1717
public:
18-
AStar(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, int width, int height,
18+
AStar(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, int width, int height,
1919
bool usePseudoRandom, bool initialize = true);
2020
~AStar();
2121
virtual void moveStart(double dx, double dy);

src/planning/OnlineFmtStar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
using namespace std;
88

9-
OnlineFmtStar::OnlineFmtStar(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, double maxSegment, int width, int height,
9+
OnlineFmtStar::OnlineFmtStar(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, double maxSegment, int width, int height,
1010
bool usePseudoRandom, shared_ptr<Coord> start, double pruneRadius, double percentCoverage)
1111
: SamplingPlanner(obstacleHash, obstacleRects, maxSegment, width, height, usePseudoRandom, start, pruneRadius, percentCoverage) {
1212
this->name = "ofmtstar";

src/planning/OnlineFmtStar.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class OnlineFmtStar : public SamplingPlanner {
1515
void sampleAndAdd();
1616

1717
public:
18-
OnlineFmtStar(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, double maxSegment, int width,
18+
OnlineFmtStar(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, double maxSegment, int width,
1919
int height, bool usePseudoRandom, std::shared_ptr<Coord> start, double pruneRadius = 5, double percentCoverage = 0.02);
2020

2121
bool isDoneBuilding();

src/planning/OnlineRrtStar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
using namespace std;
1313

14-
OnlineRrtStar::OnlineRrtStar(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, double maxSegment, int width, int height,
14+
OnlineRrtStar::OnlineRrtStar(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, double maxSegment, int width, int height,
1515
bool usePseudoRandom, shared_ptr<Coord> start, double pruneRadius, double percentCoverage)
1616
: SamplingPlanner(obstacleHash, obstacleRects, maxSegment, width, height, usePseudoRandom, start, pruneRadius, percentCoverage) {
1717
this->name = "orrtstar";

src/planning/OnlineRrtStar.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class OnlineRrtStar : public SamplingPlanner {
99
void sampleAndAdd();
1010

1111
public:
12-
OnlineRrtStar(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, double maxSegment, int width,
12+
OnlineRrtStar(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, double maxSegment, int width,
1313
int height, bool usePseudoRandom, std::shared_ptr<Coord> start, double pruneRadius = 5, double percentCoverage = 0.02);
1414

1515
bool isDoneBuilding();

src/planning/Planner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
using namespace std;
1717

18-
Planner::Planner(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, int width, int height, bool usePseudoRandom)
18+
Planner::Planner(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, int width, int height, bool usePseudoRandom)
1919
: haltonX(19), haltonY(31), rtree() {
2020
// srand(time(NULL)); // initialize the random number generator so it happens
2121

src/planning/Planner.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Planner {
2525
int height;
2626
double mapArea;
2727
double obstacleArea;
28-
std::vector<std::vector<bool>> *obstacleHash;
28+
std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash;
2929
double maxTravel = 1.5;
3030
std::unordered_map<Coord, double> unseenAreaMap;
3131

@@ -53,15 +53,15 @@ class Planner {
5353
public:
5454
std::shared_ptr<RrtNode> root;
5555
std::shared_ptr<RrtNode> endNode;
56-
std::vector<std::shared_ptr<Rect>> *obstacleRects;
56+
std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects;
5757
std::deque<Coord> bestPath;
5858
bool usePseudoRandom;
5959
std::string name = "unset";
6060

6161
const double distanceK = 1.0;
6262
double unseenAreaK = 100.0;
6363

64-
Planner(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, int width, int height,
64+
Planner(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, int width, int height,
6565
bool usePseudoRandom);
6666
virtual ~Planner();
6767
virtual void moveStart(double dx, double dy);

src/planning/PrmStar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using namespace std;
66

7-
PrmStar::PrmStar(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, int width, int height, bool usePseudoRandom,
7+
PrmStar::PrmStar(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, int width, int height, bool usePseudoRandom,
88
GraphType graphType)
99
: AStar(obstacleHash, obstacleRects, width, height, usePseudoRandom, false) {
1010
if (graphType == GraphType::Random) {

src/planning/PrmStar.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PrmStar : public AStar {
1414
GraphType graphType;
1515

1616
public:
17-
PrmStar(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, int width, int height,
17+
PrmStar(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, int width, int height,
1818
bool usePseudoRandom, GraphType graphType);
1919
~PrmStar();
2020

src/planning/SamplingPlanner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
using namespace std;
1313

14-
SamplingPlanner::SamplingPlanner(vector<vector<bool>> *obstacleHash, vector<shared_ptr<Rect>> *obstacleRects, double maxSegment, int width,
14+
SamplingPlanner::SamplingPlanner(shared_ptr<vector<vector<bool>>> obstacleHash, shared_ptr<vector<shared_ptr<Rect>>> obstacleRects, double maxSegment, int width,
1515
int height, bool usePseudoRandom, shared_ptr<Coord> start, double pruneRadius, double percentCoverage)
1616
: Planner(obstacleHash, obstacleRects, width, height, usePseudoRandom) {
1717
this->maxSegment = maxSegment;

src/planning/SamplingPlanner.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SamplingPlanner : public Planner {
3030
int nodeAddThreshold;
3131
long numNodes = 0;
3232

33-
SamplingPlanner(std::vector<std::vector<bool>> *obstacleHash, std::vector<std::shared_ptr<Rect>> *obstacleRects, double maxSegment, int width,
33+
SamplingPlanner(std::shared_ptr<std::vector<std::vector<bool>>> obstacleHash, std::shared_ptr<std::vector<std::shared_ptr<Rect>>> obstacleRects, double maxSegment, int width,
3434
int height, bool usePseudoRandom, std::shared_ptr<Coord> start, double pruneRadius, double percentCoverage);
3535
// virtual void sample() = 0;
3636
virtual bool isDoneBuilding() = 0;

src/simulate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(int argc, char* argv[]) {
8585
options.parse(argc, argv);
8686

8787
for (int mapNum = 0; mapNum < numMaps; mapNum++) {
88-
vector<shared_ptr<Rect>> obstacleRects;
88+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects;
8989
// generateObstacleRects(width, height, 10, obstacleRects, OBSTACLE_PADDING);
9090
readMap(string_format("data/map%d.json", mapNum), width, height, obstacleRects);
9191

src/simulate_tree.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(int argc, char* argv[]) {
8585
options.parse(argc, argv);
8686

8787
for (int mapNum = 0; mapNum < numMaps; mapNum++) {
88-
vector<shared_ptr<Rect>> obstacleRects;
88+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects;
8989
// generateObstacleRects(width, height, 10, obstacleRects, OBSTACLE_PADDING);
9090
readMap(string_format("data/map%d.json", mapNum), width, height, obstacleRects);
9191
auto allPaths = readAllPaths(string_format("data/map%dpaths.json", mapNum));

src/threeMain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ int main(int argc, char* argv[]) {
370370
ratio = width / (float)height;
371371
initDisplay(width, height, ratio);
372372

373-
vector<shared_ptr<Rect>> obstacleRects;
373+
shared_ptr<vector<shared_ptr<Rect>>> obstacleRects;
374374
generateObstacleRects(width, height, 10, obstacleRects);
375375

376376
vector<vector<bool>> obstacleHash(height, vector<bool>(width, false));

0 commit comments

Comments
 (0)