]> rtime.felk.cvut.cz Git - hubacji1/rrts.git/blob - src/rrtext8.cc
Refactor json input
[hubacji1/rrts.git] / src / rrtext8.cc
1 #include "rrtext.h"
2
3 RRTExt8::KdNode::KdNode(RRTNode *n)
4         : node_(n)
5         , left_(nullptr)
6         , right_(nullptr)
7 {
8 }
9
10 void RRTExt8::delete_kd_nodes(KdNode *n)
11 {
12         if (!n)
13                 return;
14         if (n->left() != nullptr)
15                 delete_kd_nodes(n->left());
16         if (n->right() != nullptr)
17                 delete_kd_nodes(n->right());
18         delete n;
19 }
20
21 void RRTExt8::store_node(RRTNode *n, KdNode *&r, int l)
22 {
23         if (r == nullptr)
24                 r = new KdNode(n);
25         else if (l % 3 == 0 && n->x() < r->node()->x())
26                 store_node(n, r->left(), l + 1);
27         else if (l % 3 == 0)
28                 store_node(n, r->right(), l + 1);
29         else if (l % 3 == 1 && n->y() < r->node()->y())
30                 store_node(n, r->left(), l + 1);
31         else if (l % 3 == 1)
32                 store_node(n, r->right(), l + 1);
33         else if (l % 3 == 0 && n->h() < r->node()->h())
34                 store_node(n, r->left(), l + 1);
35         else
36                 store_node(n, r->right(), l + 1);
37 }
38
39 void RRTExt8::nn(RRTNode *&n, RRTNode &t, KdNode *r, int l, double &d)
40 {
41         if (r == nullptr)
42                 return;
43         if (this->cost_search(*r->node(), t) < d) {
44                 n = r->node();
45                 d = this->cost_search(*r->node(), t);
46         }
47         if (l % 3 == 0 && t.x() < r->node()->x()) {
48                 nn(n, t, r->left(), l + 1, d);
49                 if (r->node()->x() - t.x() < d)
50                         nn(n, t, r->right(), l + 1, d);
51         } else if (l % 3 == 0) {
52                 nn(n, t, r->right(), l + 1, d);
53                 if (t.x() - r->node()->x() < d)
54                         nn(n, t, r->left(), l + 1, d);
55         } else if (l % 3 == 1 && t.y() < r->node()->y()) {
56                 nn(n, t, r->left(), l + 1, d);
57                 if (r->node()->y() - t.y() < d)
58                         nn(n, t, r->right(), l + 1, d);
59         } else if (l % 3 == 1) {
60                 nn(n, t, r->right(), l + 1, d);
61                 if (t.y() - r->node()->y() < d)
62                         nn(n, t, r->left(), l + 1, d);
63         } else if (l % 3 == 2 && t.h() < r->node()->h()) {
64                 nn(n, t, r->left(), l + 1, d);
65                 if (r->node()->h() - t.h() < d)
66                         nn(n, t, r->right(), l + 1, d);
67         } else {
68                 nn(n, t, r->right(), l + 1, d);
69                 if (t.h() - r->node()->h() < d)
70                         nn(n, t, r->left(), l + 1, d);
71         }
72 }
73
74 // API
75 void RRTExt8::init()
76 {
77 }
78
79 void RRTExt8::deinit()
80 {
81         this->delete_kd_nodes(this->kdroot_);
82 }
83
84 void RRTExt8::store_node(RRTNode n)
85 {
86         RRTS::store_node(n);
87         RRTNode *sn = &this->nodes().back();
88         this->store_node(sn, this->kdroot_, 0);
89 }
90
91 RRTNode *RRTExt8::nn(RRTNode &t)
92 {
93         RRTNode *n = &this->nodes().front();
94         double d = 9999;
95         this->nn(n, t, this->kdroot_, 0, d);
96         return n;
97 }
98
99 std::vector<RRTNode *> RRTExt8::nv(RRTNode &t)
100 {
101         std::vector<RRTNode *> nv;
102         return nv;
103 }