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 float RRTNode::ccost() const
97 float RRTNode::dcost() const
102 float RRTNode::ocost() const
107 char RRTNode::tree() const
112 std::vector<RRTNode *> &RRTNode::children()
114 return this->children_;
117 RRTNode *RRTNode::parent() const
119 return this->parent_;
122 bool RRTNode::visited()
124 return this->visited_;
128 void RRTNode::h(float ch)
133 void RRTNode::t(float ct)
138 void RRTNode::s(float cs)
143 bool RRTNode::add_child(RRTNode *node, float cost)
145 return this->add_child(node, cost, 1);
148 bool RRTNode::add_child(RRTNode *node, float cost, float time)
151 node->ccost(this->ccost() + cost);
153 node->t(this->t() + time);
154 this->children_.push_back(node);
158 bool RRTNode::rem_child(RRTNode *node)
161 for (auto ch: this->children_) {
163 this->children_.erase(this->children_.begin() + i);
171 float RRTNode::ccost(float cost)
173 return this->ccost_ = cost;
176 float RRTNode::dcost(float cost)
178 return this->dcost_ = cost;
181 float RRTNode::ocost(float cost)
183 return this->ocost_ = cost;
186 char RRTNode::tree(char t)
188 return this->tree_ = t;
191 bool RRTNode::remove_parent()
193 this->parent_ = nullptr;
197 bool RRTNode::parent(RRTNode *parent)
199 if (parent == nullptr)
201 this->parent_ = parent;
205 bool RRTNode::visit(bool v)
207 return this->visited_ = v;
211 bool RRTNode::comp_ccost(RRTNode *n1, RRTNode *n2)
213 return n1->ccost() < n2->ccost();
216 float RRTNode::update_ccost()
218 std::vector<RRTNode *> s; // DFS stack
219 std::vector<RRTNode *> r; // reset visited_
222 while (s.size() > 0) {
227 for (auto ch: tmp->children()) {
228 ch->ccost(tmp->ccost() + ch->dcost());
238 bool RRTNode::visit()
240 if (this->visited_) {
243 this->visited_ = true;
248 init_(new RRTNode()),
252 RRTEdge::RRTEdge(RRTNode *init, RRTNode *goal):
257 RRTNode *RRTEdge::init() const
262 RRTNode *RRTEdge::goal() const
267 RRTNode *RRTEdge::intersect(RRTEdge *e, bool segment)
269 // see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
270 float x1 = this->init()->x();
271 float y1 = this->init()->y();
272 float x2 = this->goal()->x();
273 float y2 = this->goal()->y();
274 float x3 = e->init()->x();
275 float y3 = e->init()->y();
276 float x4 = e->goal()->x();
277 float y4 = e->goal()->y();
278 float deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
282 float px = (x1 * y2 - y1 * x2) * (x3 - x4) -
283 (x1 - x2) * (x3 * y4 - y3 * x4);
285 float py = (x1 * y2 - y1 * x2) * (y3 - y4) -
286 (y1 - y2) * (x3 * y4 - y3 * x4);
288 return new RRTNode(px, py, 0);
290 float t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4);
292 float u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3);
295 if (t < 0 || t > 1 || u < 0 || u > 1)
297 return new RRTNode(x1 + t * (x2 - x1), y1 + t * (y2 - y1), 0);