Newer
Older
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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2017 Fraunhofer ESK
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Karsten Roscher <karsten.roscher@esk.fraunhofer.de>
*/
#include "traci-node-utils.h"
#include "traci-vehicle-adapter.h"
#include "traci-exception.h"
#include <ns3/assert.h>
namespace
{
ns3::Ptr<ns3::traci::Vehicle>
GetVehicleOrThrow (ns3::Ptr<ns3::Node> node)
{
if (!node)
{
throw ns3::traci::NodeRequestError ("Node must not be null");
}
ns3::Ptr<ns3::TraCiVehicleAdapter> traciAdapter =
node->GetObject<ns3::TraCiVehicleAdapter> ();
if (!traciAdapter)
{
throw ns3::traci::NodeRequestError ("TraCiVehicleAdapter is missing");
}
ns3::Ptr<ns3::traci::Vehicle> vehicle = traciAdapter->GetVehicle ();
if (!vehicle)
{
throw ns3::traci::NodeRequestError("TraCiVehicleAdapter has no vehicle set");
}
return vehicle;
}
} // namespace
namespace ns3
{
namespace traci
{
Position2D
GetPosition (Ptr<Node> node)
{
NS_ASSERT_MSG (node, "Node must not be null");
Ptr<traci::Vehicle> vehicle = GetVehicleOrThrow (node);
NS_ASSERT (vehicle);
return vehicle->GetPosition2d ();
}
double
GetRoadDistance (Ptr<Node> from, Ptr<Node> to)
{
NS_ASSERT_MSG (from, "Node 'from' must not be null");
NS_ASSERT_MSG (to, "Node 'to' must not be null");
Ptr<traci::Vehicle> vehicleFrom = GetVehicleOrThrow (from);
Ptr<traci::Vehicle> vehicleTo = GetVehicleOrThrow (to);
Ptr<TraCi> traci = vehicleFrom->GetTraCi ();
NS_ASSERT_MSG (traci, "TraCi not available from vehicle");
return traci->GetDistance (vehicleFrom->GetPosition2d (), vehicleTo->GetPosition2d (), true);
}
double
GetMinRoadDistance (Ptr<Node> a, Ptr<Node> b)
{
NS_ASSERT_MSG (a, "Node 'a' must not be null");
NS_ASSERT_MSG (b, "Node 'b' must not be null");
Ptr<traci::Vehicle> vehicleA = GetVehicleOrThrow (a);
Ptr<traci::Vehicle> vehicleB = GetVehicleOrThrow (b);
Position2D posA = vehicleA->GetPosition2d ();
Position2D posB = vehicleB->GetPosition2d ();
Ptr<TraCi> traci = vehicleA->GetTraCi ();
NS_ASSERT_MSG (traci, "TraCi not available from vehicle");
return std::min (traci->GetDistance (posA, posB, true),
traci->GetDistance (posB, posA, true));
}
} // namespace traci
} // namespace ns3