forked from qiaoxu123/RealTimePathPlanning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ofApp.cpp
205 lines (166 loc) · 4.99 KB
/
ofApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
#ifdef randomSeed
ofSeedRandom(randomSeed);
#endif // randomSeed
#ifdef CLK
auto start = std::chrono::steady_clock::now();
#endif // DEBUG
ofSetVerticalSync(true);
ofSetFrameRate(30);
ofSetWindowTitle("Dynamic-obstacles");
ofBackground(200,200,200,200);
myfont.loadFont("Roboto-Regular.ttf", 10);
//map = new Enviroment();
//car.setup();
ofVec2f w;
w.set(ofGetWidth() / 2, 0);
wall = new maze(w);
obstacles *ob = wall;
obst.push_back(ob);
w.set(ofGetWidth() / 2, 0.6*ofGetHeight());
wall = new maze(w);
ob = wall;
obst.push_back(ob);
w.set(ofGetWidth() / 4, 0.4*ofGetHeight());
wall = new maze(w, 60, 0.2*ofGetHeight());
ob = wall;
obst.push_back(ob);
w.set(0.75*ofGetWidth(), 0.4*ofGetHeight());
wall = new maze(w, 60, 0.2*ofGetHeight());
ob = wall;
obst.push_back(ob);
for (unsigned int i = 0; i < numberOfobst; i++)
{
obstacles *ob = new obstacles();
//OBST = new movingObst();
//obstacles *ob = OBST;
obst.push_back(ob);
}
//
OBST = new movingObst();
ob = OBST;
obst.push_back(ob);
cout << "Obst size: " << obst.size() << endl;
#ifdef randomSeed
std::cout << "RandomSeed:" << randomSeed << endl;
#endif
#ifdef CLK
auto end = std::chrono::steady_clock::now();
std::cout << std::endl << "Setup:" << std::chrono::duration<double, std::milli>(end - start).count() << " ms" << std::endl;
#endif // DEBUG
}
//--------------------------------------------------------------
void ofApp::update(){
if (!updateFlag) return;
#ifdef CLK
auto start = std::chrono::steady_clock::now();
#endif // DEBUG
#ifdef automatic
for (auto i : obst) {
i->move(obst);
//cout << "location: " << i->loc() << "Radius: " << i->rad() << endl;
//cout << i.getX() << " " << i.getY() << endl;
}
#endif // automatic
if (map!= NULL) map->update(car,obst);
#ifdef CLK
auto end = std::chrono::steady_clock::now();
/*std::cout << std::endl << "Update:" << std::chrono::duration<double, std::milli>(end - start).count() << " ms" << std::endl;*/
updateTime = std::chrono::duration<double, std::milli>(end - start).count();
#endif // DEBUG
}
//--------------------------------------------------------------
void ofApp::draw(){
#ifdef CLK
auto start = std::chrono::steady_clock::now();
#endif // DEBUG
for (auto i : obst) {
i->render();
}
if (map != NULL) map->render();
if (car!= NULL) car->render();
char fpsStr[255]; // an array of chars
ofSetColor({ 255,0,0 });
sprintf(fpsStr, "Frame rate: %d", int(ofGetFrameRate()));
myfont.drawString(fpsStr, ofGetWindowWidth() - 100, ofGetWindowHeight() - 25);
if (map != NULL) {
char numNode[255];
sprintf(numNode, "Number of nodes: %d", int(map->numofnode()));
myfont.drawString(numNode, ofGetWindowWidth() - 140, ofGetWindowHeight() - 10);
}
#ifdef CLK
auto end = std::chrono::steady_clock::now();
/*std::cout << std::endl << "Draw:" << std::chrono::duration<double, std::milli>(end - start).count() << " ms" << std::endl;*/
drawTime = std::chrono::duration<double, std::milli>(end - start).count();
char time[255];
sprintf(time, "Update rate: %f", updateTime);
myfont.drawString(time, ofGetWindowWidth() - 140, ofGetWindowHeight() - 755);
sprintf(time, "Draw rate: %f", drawTime);
myfont.drawString(time, ofGetWindowWidth() - 140, ofGetWindowHeight() - 740);
#endif // DEBUG
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 'p')
{
updateFlag = !updateFlag;
}
else if(key=='g')
{
map->grid = !map->grid;
}
else if (key == 'x') {
ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
img.save("screenshot.png");
}
#ifdef manual
OBST->move(key);
#endif // manual
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
ofVec2f loc;
loc.set(x, y);
if (button == 0) {
if (car != NULL) {
map->targetSet(loc);
}
}
else if (button == 2) {
car = new Robot(loc);
map = new Enviroment(car->getLocation());
}
else
{
}
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}