Easy_rider
 
Loading...
Searching...
No Matches
Graph.h
Go to the documentation of this file.
1
9#ifndef GRAPH_H
10#define GRAPH_H
11
12#include <algorithm>
13#include <concepts>
14#include <utility>
15#include <vector>
16
25template <typename T>
26concept NodeConcept = requires(const T &n) {
27 { n.getX() } -> std::same_as<int>;
28 { n.getY() } -> std::same_as<int>;
29 { n.getPosition() } -> std::same_as<std::pair<int, int>>;
30};
31
41template <typename U, typename T>
42concept EdgeConcept = requires(const U &e) {
43 { e.getFrom() } -> std::same_as<T>;
44 { e.getTo() } -> std::same_as<T>;
45 { e.getLength() } -> std::same_as<double>;
46 { e.getMaxSpeed() } -> std::same_as<int>;
47};
48
56template <NodeConcept T, typename U>
57 requires EdgeConcept<U, T>
58class Graph {
59public:
63 Graph() = default;
64
69 void addNode(const T &node) { nodes_.push_back(node); }
70
75 void addEdge(const U &edge) { edges_.push_back(edge); }
76
81 enum class AddEdgeResult {
82 Success,
84 Crosses
85 };
86
98 auto f = edge.getFrom().getPosition();
99 auto t = edge.getTo().getPosition();
100
101 if (isDuplicate(f, t))
103
104 if (crossesAnyEdge(f, t))
106
107 edges_.push_back(edge);
109 }
110
115 std::vector<T> getNodes() const { return nodes_; }
116
121 std::vector<U> getEdges() const { return edges_; }
122
123private:
124 std::vector<T> nodes_;
125 std::vector<U> edges_;
138 static long long orient(const std::pair<int, int> &A,
139 const std::pair<int, int> &B,
140 const std::pair<int, int> &C) {
141 return static_cast<long long>(B.first - A.first) * (C.second - A.second) -
142 static_cast<long long>(B.second - A.second) * (C.first - A.first);
143 }
144
153 static bool onSegment(const std::pair<int, int> &A,
154 const std::pair<int, int> &B,
155 const std::pair<int, int> &C) {
156 return std::min(A.first, B.first) <= C.first &&
157 C.first <= std::max(A.first, B.first) &&
158 std::min(A.second, B.second) <= C.second &&
159 C.second <= std::max(A.second, B.second);
160 }
161
170 bool isDuplicate(const std::pair<int, int> &f,
171 const std::pair<int, int> &t) const {
172 return std::any_of(edges_.begin(), edges_.end(), [&](auto const &e) {
173 auto p = e.getFrom().getPosition();
174 auto q = e.getTo().getPosition();
175 return (p == f) && (q == t);
176 });
177 }
178
190 bool segmentCrosses(const std::pair<int, int> &f,
191 const std::pair<int, int> &t,
192 const std::pair<int, int> &q1,
193 const std::pair<int, int> &q2) const {
194 if (q1 == f || q1 == t || q2 == f || q2 == t)
195 return false;
196
197 long long o1 = orient(f, t, q1);
198 long long o2 = orient(f, t, q2);
199 long long o3 = orient(q1, q2, f);
200 long long o4 = orient(q1, q2, t);
201
202 if (((o1 > 0 && o2 < 0) || (o1 < 0 && o2 > 0)) &&
203 ((o3 > 0 && o4 < 0) || (o3 < 0 && o4 > 0)))
204 return true;
205
206 if (o1 == 0 && onSegment(f, t, q1))
207 return true;
208 if (o2 == 0 && onSegment(f, t, q2))
209 return true;
210 if (o3 == 0 && onSegment(q1, q2, f))
211 return true;
212 if (o4 == 0 && onSegment(q1, q2, t))
213 return true;
214
215 return false;
216 }
217
225 bool crossesAnyEdge(const std::pair<int, int> &f,
226 const std::pair<int, int> &t) const {
227 return std::any_of(edges_.begin(), edges_.end(), [&](auto const &e) {
228 auto q1 = e.getFrom().getPosition();
229 auto q2 = e.getTo().getPosition();
230 return segmentCrosses(f, t, q1, q2);
231 });
232 }
233};
234
235#endif // GRAPH_H
A templated graph storing nodes of type T and edges of type U.
Definition Graph.h:58
void addNode(const T &node)
Add a node to the graph.
Definition Graph.h:69
Graph()=default
Default-constructs an empty graph.
std::vector< U > getEdges() const
Retrieve all edges in the graph.
Definition Graph.h:121
void addEdge(const U &edge)
Add an edge to the graph without checking for duplicates.
Definition Graph.h:75
AddEdgeResult addEdgeIfNotExists(const U &edge)
Attempt to add a directed edge, rejecting duplicates or crossings.
Definition Graph.h:97
std::vector< T > getNodes() const
Retrieve all nodes in the graph.
Definition Graph.h:115
AddEdgeResult
Result of attempting to insert an edge with checks.
Definition Graph.h:81
Concept that an Edge type must satisfy, given a Node type T.
Definition Graph.h:42
Concept that a Node type must satisfy.
Definition Graph.h:26