2 This file is part of I am car.
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
29 RRTNode::RRTNode(float x, float y):
37 RRTNode::RRTNode(float x, float y, float h):
40 h_((h > 2 * M_PI) ? h - 2 * M_PI : h),
45 RRTNode::RRTNode(float x, float y, float h, float t):
48 h_((h > 2 * M_PI) ? h - 2 * M_PI : h),
53 RRTNode::RRTNode(float x, float y, float h, float t, float s):
56 h_((h > 2 * M_PI) ? h - 2 * M_PI : h),
62 float RRTNode::x() const
67 float RRTNode::y() const
72 float RRTNode::h() const
82 float RRTNode::t() const
87 float RRTNode::s() const
92 RRTNode *RRTNode::rs() const
97 float RRTNode::ccost() const
102 float RRTNode::dcost() const
107 float RRTNode::ocost() const
112 char RRTNode::tree() const
117 std::vector<RRTNode *> &RRTNode::children()
119 return this->children_;
122 RRTNode *RRTNode::parent() const
124 return this->parent_;
127 bool RRTNode::visited()
129 return this->visited_;
133 void RRTNode::h(float ch)
138 void RRTNode::t(float ct)
143 void RRTNode::s(float cs)
148 void RRTNode::rs(RRTNode *rs)
153 bool RRTNode::add_child(RRTNode *node, float cost)
155 return this->add_child(node, cost, 1);
158 bool RRTNode::add_child(RRTNode *node, float cost, float time)
161 node->ccost(this->ccost() + cost);
163 node->t(this->t() + time);
164 this->children_.push_back(node);
168 bool RRTNode::rem_child(RRTNode *node)
171 for (auto ch: this->children_) {
173 this->children_.erase(this->children_.begin() + i);
181 float RRTNode::ccost(float cost)
183 return this->ccost_ = cost;
186 float RRTNode::dcost(float cost)
188 return this->dcost_ = cost;
191 float RRTNode::ocost(float cost)
193 return this->ocost_ = cost;
196 char RRTNode::tree(char t)
198 return this->tree_ = t;
201 bool RRTNode::remove_parent()
203 this->parent_ = nullptr;
207 bool RRTNode::parent(RRTNode *parent)
209 if (parent == nullptr)
211 this->parent_ = parent;
215 bool RRTNode::visit(bool v)
217 return this->visited_ = v;
221 bool RRTNode::comp_ccost(RRTNode *n1, RRTNode *n2)
223 return n1->ccost() < n2->ccost();
226 float RRTNode::update_ccost()
228 std::vector<RRTNode *> s; // DFS stack
229 std::vector<RRTNode *> r; // reset visited_
232 while (s.size() > 0) {
237 for (auto ch: tmp->children()) {
238 ch->ccost(tmp->ccost() + ch->dcost());
248 bool RRTNode::visit()
250 if (this->visited_) {
253 this->visited_ = true;
257 bool RRTNode::inFront(RRTNode *n)
259 float nx = cos(this->h());
260 float ny = sin(this->h());
261 float dx = n->x() - this->x();
262 float dy = n->y() - this->y();
263 float dot = nx * dx + ny * dy;
270 init_(new RRTNode()),
274 RRTEdge::RRTEdge(RRTNode *init, RRTNode *goal):
279 RRTNode *RRTEdge::init() const
284 RRTNode *RRTEdge::goal() const
289 RRTNode *RRTEdge::intersect(RRTEdge *e, bool segment)
291 // see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
292 float x1 = this->init()->x();
293 float y1 = this->init()->y();
294 float x2 = this->goal()->x();
295 float y2 = this->goal()->y();
296 float x3 = e->init()->x();
297 float y3 = e->init()->y();
298 float x4 = e->goal()->x();
299 float y4 = e->goal()->y();
300 float deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
304 float px = (x1 * y2 - y1 * x2) * (x3 - x4) -
305 (x1 - x2) * (x3 * y4 - y3 * x4);
307 float py = (x1 * y2 - y1 * x2) * (y3 - y4) -
308 (y1 - y2) * (x3 * y4 - y3 * x4);
310 return new RRTNode(px, py, 0);
312 float t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
314 float u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
317 if (t < 0 || t > 1 || u < 0 || u > 1)
319 return new RRTNode(x1 + t * (x2 - x1), y1 + t * (y2 - y1), 0);