Debian Codegraph @

src/common/route.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D 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.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "route.hpp"
17 #include "targetnodebase.hpp"
18
19 #include <algorithm>
20 #include <cassert>
21 #include <cmath>
22
23 Route::Route()
24 {
25 }
26
27 void Route::clear()
28 {
29 m_route.clear();
30 }
31
32 bool Route::push(TargetNodeBasePtr node)
33 {
34 node->setIndex(static_cast<int>(m_route.size()));
35 m_route.push_back(node);
36 return isClosed();
37 }
38
39 bool Route::isClosed() const
40 {
41 if (m_route.size() > 1)
42 {
43 const auto dx = std::abs(m_route[0]->location().x() - m_route.back()->location().x());
44 const auto dy = std::abs(m_route[0]->location().y() - m_route.back()->location().y());
45 const auto closingThreshold = 32;
46 return dx < closingThreshold && dy < closingThreshold;
47 }
48
49 return false;
50 }
51
52 size_t Route::numNodes() const
53 {
54 return static_cast<size_t>(m_route.size());
55 }
56
57 TargetNodeBasePtr Route::get(size_t index) const
58 {
59 assert(index < numNodes());
60 return m_route[index];
61 }
62
63 void Route::getAll(RouteVector & routeVector) const
64 {
65 routeVector = m_route;
66 }
67
68 void Route::buildFromVector(RouteVector & routeVector)
69 {
70 clear();
71
72 std::sort(routeVector.begin(), routeVector.end(),
73 [](const auto lhs, const auto rhs) {
74 return lhs->index() < rhs->index();
75 });
76
77 for (auto && node : routeVector)
78 {
79 if (node && node->index() >= 0)
80 {
81 push(node);
82 }
83 }
84 }
85
86 double Route::geometricLength() const
87 {
88 double result = 0;
89
90 if (m_route.size() > 1)
91 {
92 for (size_t i = 0; i < m_route.size() - 1; i++)
93 {
94 const auto dx = m_route[i]->location().x() - m_route[i + 1]->location().x();
95 const auto dy = m_route[i]->location().y() - m_route[i + 1]->location().y();
96 result += std::sqrt(dx * dx + dy * dy);
97 }
98
99 const auto dx = m_route[m_route.size() - 1]->location().x() - m_route[0]->location().x();
100 const auto dy = m_route[m_route.size() - 1]->location().y() - m_route[0]->location().y();
101 result += std::sqrt(dx * dx + dy * dy);
102 }
103
104 return result;
105 }
106
107 Route::RouteVector::iterator Route::begin()
108 {
109 return m_route.begin();
110 }
111
112 Route::RouteVector::iterator Route::end()
113 {
114 return m_route.end();
115 }
116
117 Route::RouteVector::const_iterator Route::cbegin() const
118 {
119 return m_route.cbegin();
120 }
121
122 Route::RouteVector::const_iterator Route::cend() const
123 {
124 return m_route.cend();
125 }