/
IntersectionEntity.h
154 lines (134 loc) · 4.2 KB
/
IntersectionEntity.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef V2X_INTERSECTIONENTITY_H
#define V2X_INTERSECTIONENTITY_H
#include <vector>
#include <map>
#include <tuple>
#include <cstdint>
#include <iostream>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/VertexArray.hpp>
#include "asn_headers.h"
struct LaneNode
{
public:
int64_t x { 0 };
int64_t y { 0 };
uint64_t width { 0 };
int64_t lx { 0 };
int64_t ly { 0 };
int64_t rx { 0 };
int64_t ry { 0 };
sf::Vector2f dir { 0, 0};
std::vector<NodeAttributeXY_t> attributes;
bool is(NodeAttributeXY_t attr)
{
return std::find(attributes.begin(), attributes.end(), attr) != attributes.end();
}
sf::Vector2<int64_t> to_vec()
{
return sf::Vector2<int64_t>(x, y);
}
sf::Vector2<int64_t> right_to_vec()
{
return sf::Vector2<int64_t>(rx, ry);
}
sf::Vector2<int64_t> left_to_vec()
{
return sf::Vector2<int64_t>(lx, ly);
}
};
class Lane;
enum class TurnDirection
{
Unknown = 0,
Straight = 1,
Left = 2,
StraightAndLeft = 3,
Right = 4,
RightAndStraight = 5,
RightAndLeft = 6,
RightAndStraightAndLeft = 7,
};
class LaneConnection : public sf::Drawable
{
public:
static constexpr uint32_t BEZIER_SEGMENTS = 20;
static constexpr float BEZIER_CONTROL_LENGTH = 0.45f;
LaneConnection() = default;
Lane *to { nullptr };
Lane *from { nullptr };
SignalGroupID_t signal_group { 0 };
MovementPhaseState_t state { MovementPhaseState_unavailable };
TimeMark_t min_end_time { -1 };
TimeMark_t max_end_time { -1 };
TimeMark_t likely_time { -1 };
TimeIntervalConfidence_t confidence { -1 };
sf::VertexArray va {};
TurnDirection turn_direction { TurnDirection::Unknown };
void build_geometry(bool standalone);
void set_state(MovementPhaseState_t newstate);
void set_timing(TimeMark_t min, TimeMark_t max, TimeMark_t likely, TimeIntervalConfidence_t conf);
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
};
class IntersectionEntity;
class Lane : public sf::Drawable
{
private:
public:
~Lane() override;
Lane() = default;
Lane(Lane &&l) noexcept;
Lane &operator=(Lane &&l) noexcept;
LaneID_t id { -1 };
IntersectionEntity *intersection { nullptr };
LaneAttributes_t attr {};
std::vector<LaneNode> nodes;
std::vector<LaneConnection> connections;
sf::Drawable *ingress_arrow { nullptr };
sf::Drawable *egress_arrow { nullptr };
sf::Drawable *stopline { nullptr };
bool is_ingress { false };
bool is_egress { false };
bool can_turn_left { false };
bool can_turn_straight { false };
bool can_turn_right { false };
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
};
class Approach
{
public:
std::vector<Lane *> lanes;
};
class IntersectionEntity : public sf::Drawable
{
friend class Lane;
friend class LaneConnection;
private:
int64_t ref_x { 0 }, ref_y { 0 };
std::map<ApproachID_t, Approach> ingress_approaches;
std::map<LaneID_t, Lane> lanes;
const sf::Color lane_color { sf::Color(100, 100, 100) };
const sf::Color lane_outer_color { sf::Color(200, 200, 200) };
const sf::Color tram_lane_color { sf::Color(210, 180, 140) };
const sf::Color pedestrian_lane_color { sf::Color(135, 206, 235) };
const sf::Color bike_lane_color { sf::Color(250, 128, 114) };
sf::Drawable *ref { nullptr };
std::vector<sf::VertexArray> lane_geometries;
std::vector<sf::VertexArray> lane_outline_geometries;
std::vector<sf::VertexArray> lane_nodes;
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
public:
IntersectionEntity(int64_t ref_x, int64_t ref_y) : ref_x(ref_x), ref_y(ref_y) {}
sf::Vector2<int64_t> get_location();
void build_geometry(bool standalone);
void infer_data();
void add_lane(LaneID_t id, LaneAttributes &attr);
Lane &get_lane(LaneID_t id);
void add_lane_to_ingress_approach(ApproachID_t aid, LaneID_t lid);
void add_node(LaneID_t lane_id, int64_t x, int64_t y, uint64_t width, std::vector<NodeAttributeXY_t> &attributes);
void add_connection(LaneID_t start, LaneID_t end, const SignalGroupID_t *sg);
void set_signal_group_state(SignalGroupID_t id, MovementPhaseState_t state);
void set_signal_group_timing(SignalGroupID_t id, TimeMark_t min, TimeMark_t max, TimeMark_t likely, TimeIntervalConfidence_t conf);
void for_each_ingress_approach(std::function<void(Approach&)> callback);
};
#endif //V2X_INTERSECTIONENTITY_H