]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrtext2.cc
d98d7cb0c817a0b545418d5a01dbeda889aa8471
[hubacji1/rrts.git] / src / rrtext2.cc
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 #include <cassert>
8 #include "rrtext.hh"
9 #define CUTE_C2_IMPLEMENTATION
10 #include "cute_c2.h"
11
12 namespace rrts {
13
14 bool
15 RRTExt2::collide(RRTNode const& n)
16 {
17         this->c2x_bc_.p.x = n.x();
18         this->c2x_bc_.p.y = n.y();
19         this->c2x_bc_.r.c = cos(n.h());
20         this->c2x_bc_.r.s = sin(n.h());
21         for (auto& o: this->c2_obstacles_) {
22                 if (c2PolytoPoly(&this->c2_bc_, &this->c2x_bc_, &o, NULL)) {
23                         return true;
24                 }
25         }
26         return false;
27 }
28
29 bool
30 RRTExt2::collide_steered()
31 {
32         unsigned int i = 0;
33         for (auto& n: this->steered_) {
34                 if (this->collide(n)) {
35                         break;
36                 }
37                 i++;
38         }
39         this->steered_.erase(this->steered_.begin() + i, this->steered_.end());
40         return this->steered_.size() == 0;
41 }
42
43 RRTExt2::RRTExt2() : RRTS()
44 {
45         this->c2_bc_.count = 4;
46         this->c2_bc_.verts[0].x = this->bc_.lfx();
47         this->c2_bc_.verts[0].y = this->bc_.lfy();
48         this->c2_bc_.verts[1].x = this->bc_.lrx();
49         this->c2_bc_.verts[1].y = this->bc_.lry();
50         this->c2_bc_.verts[2].x = this->bc_.rrx();
51         this->c2_bc_.verts[2].y = this->bc_.rry();
52         this->c2_bc_.verts[3].x = this->bc_.rfx();
53         this->c2_bc_.verts[3].y = this->bc_.rfy();
54 }
55
56 Json::Value
57 RRTExt2::json() const
58 {
59         return RRTS::json();
60 }
61
62 void
63 RRTExt2::json(Json::Value jvi)
64 {
65         RRTS::json(jvi);
66         if (jvi["obst"] == Json::nullValue) {
67                 return;
68         }
69         for (auto& o: jvi["obst"]) {
70                 assert(o.size() < C2_MAX_POLYGON_VERTS);
71                 c2Poly c2tmp;
72                 c2tmp.count = o.size();
73                 unsigned int i = 0;
74                 for (auto& c: o) {
75                         c2tmp.verts[i].x = c[0].asDouble();
76                         c2tmp.verts[i].y = c[1].asDouble();
77                         i++;
78                 }
79                 this->c2_obstacles_.push_back(c2tmp);
80         }
81 }
82
83 } // namespace rrts