/* -*- 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