WSF
WsfRouteNetwork.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11// ****************************************************************************
12// Updated by Infoscitex, a DCS Company.
13// ****************************************************************************
14
15#ifndef WSFROUTENETWORK_HPP
16#define WSFROUTENETWORK_HPP
17
18#include "wsf_export.h"
19
20#include <memory>
21#include <string>
22#include <vector>
23
24#include "UtGraph.hpp"
25class UtInput;
26#include "UtLineSegment.hpp"
27class UtScriptClass;
28class UtScriptTypes;
29#include "UtSpatialTree.hpp"
30#include "UtSphericalEarth.hpp"
31#include "UtVec3.hpp"
32#include "WsfObject.hpp"
33#include "WsfRoute.hpp"
35#include "WsfWaypoint.hpp"
36
37
40{
41public:
44
46 using WaypointAddr = std::pair<int, int>;
47 using WaypointAddrList = std::vector<WaypointAddr>;
48
51 {
52 public:
53 RoutePoint() = default;
54 RoutePoint(const WaypointAddr& aAddr, double aDistanceAlongSegment = 0)
55 : mWaypointAddr(aAddr)
56 , mDistanceAlongSegment(aDistanceAlongSegment)
57 {
58 }
59
60 int RouteIndex() const { return mWaypointAddr.first; }
61 int WaypointIndex() const { return mWaypointAddr.second; }
62
65
68 };
69
72 ~WsfRouteNetwork() override;
73
74 static std::unique_ptr<UtScriptClass> CreateScriptClass(const std::string& aClassName, UtScriptTypes* aScriptTypesPtr);
75
76 WsfRouteNetwork* Clone() const override;
77
78 bool ProcessInput(UtInput& aInput) override;
79
80 bool Initialize();
81
82 WSF_DEPRECATED bool Add(WsfRoute* aRoute);
83 bool Add(std::unique_ptr<WsfRoute> aRoutePtr);
84
85 bool AppendShortestPathOnNetwork(RoutePoint aPoint1, RoutePoint aPoint2, WsfRoute& aPath);
86
87 bool GenerateShortestPathBetweenWaypoints(const WsfWaypoint& aFromWaypoint,
88 const WsfWaypoint& aToWaypoint,
89 WsfRoute& aPath);
90
91 bool GeneratePathOffRouteToRoute(double aStartLat, double aStartLon, double aEndLat, double aEndLon, WsfRoute& aPath);
92
93 bool GeneratePathBetweenClosestWaypoints(double aStartLat, double aStartLon, double aEndLat, double aEndLon, WsfRoute& aPath);
94
95 bool GeneratePathBetweenClosestSegments(double aStartLat,
96 double aStartLon,
97 double aEndLat,
98 double aEndLon,
99 WsfRoute& aPath,
100 bool aLeaveRouteNetwork = true);
101
102 bool FindClosestWaypoint(double aLat, double aLon, WsfWaypoint& aWpt, double& aDistance);
103
104 bool FindClosestSegment(double aLat, double aLon, RoutePoint& aProjectedPoint, double& aDistanceToSegment);
105
106 unsigned int GetRouteCount() const { return static_cast<unsigned int>(mRoutes.size()); }
107
109 WsfRoute& operator[](unsigned int aIndex);
110
112 std::vector<const WsfRoute*> GetRoutes() const;
113
114 void Append(const RoutePoint& aPoint, WsfRoute& aRoute);
115 void AppendSubroute(const RoutePoint& aPoint1, const RoutePoint& aPoint2, WsfRoute& aRoute);
116 int GetAdjacentNodeIndices(const RoutePoint& aPoint, int* aIndices);
117
118 // The cost functor needs access to the 'GetRouteIndex' and 'GetNodeIndex' methods.
119 friend struct RouteCostFunc;
120
121 void BuildQuadtree();
122
123 static bool CompareWaypointAddrRoute(const WaypointAddr& aLhs, const WaypointAddr& aRhs)
124 {
125 return aLhs.first < aRhs.first;
126 }
127
130 {
132 : mSegmentNode(false)
133 , mId()
134 , mWaypoints(nullptr)
135 {
136 }
137
138 explicit RouteNode(WsfStringId aId, WaypointAddrList* aWptListPtr = nullptr)
139 : mSegmentNode(false)
140 , mId(aId)
141 , mWaypoints(aWptListPtr)
142 {
143 }
144
145 bool operator==(const RouteNode& aRhs) const { return (mId == aRhs.mId); }
146
147 bool operator!=(const RouteNode& aRhs) const { return (mId != aRhs.mId); }
148
149 bool operator<(const RouteNode& aRhs) const { return (mId < aRhs.mId); }
150 bool mSegmentNode; // Does this node represent a segment (and not just a point)
151 WsfStringId mId; // Nodes unique id.
152 WaypointAddrList* mWaypoints; // List of waypoints addresses which share this node
153 };
154
157 {
159 : mSrcNode()
160 , mDstNode()
161 , mDistance(0.0)
162 {
163 }
164
165 RouteEdge(const RouteNode& aSrcNode, const RouteNode& aDstNode)
166 : mSrcNode(aSrcNode)
167 , mDstNode(aDstNode)
168 , mDistance(0.0)
169 {
170 }
171
172 bool operator==(const RouteEdge& aRhs) const
173 {
174 return ((mSrcNode == aRhs.mSrcNode) && (mDstNode == aRhs.mDstNode));
175 }
176
179 double mDistance;
180 };
181
182 using RouteGraph = UtGraphT<RouteNode, RouteEdge, false, double>;
183
184 const RouteGraph& GetGraph() const { return mGraph; }
185
187 class RouteCostFunc : public RouteGraph::cost_func
188 {
189 public:
191 : mNetworkPtr(nullptr)
192 {
193 }
195 : mNetworkPtr(aNetworkPtr)
196 {
197 }
198
200 double operator()(const RouteEdge& aEdge, const RouteNode& /*aNode1*/, const RouteNode& /*aNode2*/) const override
201 {
202 return aEdge.mDistance;
203 }
204
206 double operator()(const RouteNode& aNode1, const RouteNode& aNode2) const override
207 {
208 double cost = 0.0;
209 WsfRouteNetwork::WaypointAddr wa1 = aNode1.mWaypoints->front();
210 WsfRouteNetwork::WaypointAddr wa2 = aNode2.mWaypoints->front();
211 if (wa1 != wa2)
212 {
213 // Get the WsfWaypoint objects that correspond to both nodes.
214
215 const WsfWaypoint& n1 = (*mNetworkPtr)[wa1.first][wa1.second];
216 const WsfWaypoint& n2 = (*mNetworkPtr)[wa2.first][wa2.second];
217
218 // Calculate the great circle distance between the two nodes.
219
220 double heading;
221 double distance;
222 UtSphericalEarth::GreatCircleHeadingAndDistance(n1.GetLat(), n1.GetLon(), n2.GetLat(), n2.GetLon(), heading, distance);
223 cost = distance;
224 }
225 return cost;
226 }
227
229 };
230
231protected:
232 // Copy constructor (for Clone());
233 WsfRouteNetwork(const WsfRouteNetwork& aSrc);
234
235private:
236 // this class sits in the quad-tree and provides query tests
237 class SegmentDatum : public UtQuadDatum
238 {
239 public:
240 SegmentDatum(const UtLineSegment& aSegment);
241 ~SegmentDatum() override;
242
243 bool IsPoint() const override { return false; }
244
245 bool IsWhollyContainedIn(const UtBoundingBox& aBox) const override;
246
247 bool IsPartiallyContainedIn(const UtBoundingBox& aBox) const override;
248
249 const UtBoundingBoxI<2> GetBoundingBox() const { return mBoundingBox; }
250
251 unsigned int mRouteIndex;
252 int mWaypointIndex;
253 UtLineSegment mSegment;
254 UtBoundingBoxI<2> mBoundingBox;
255 };
256
257 using Path = std::vector<RouteNode>;
258
259 bool FindShortestPath(WsfStringId aSrcNodeId, WsfStringId aDstNodeId, Path& aPath, double& aCost) const;
260
261
262 bool BuildRoute(const Path& aShortestPath, int aStartIndex, int aStopIndex, WsfRoute& aPath);
263
264 void AppendSubroute(const WsfRouteNetwork::RouteNode& aStartingWaypoint,
265 const WsfRouteNetwork::RouteNode& aEndingWaypoint,
266 WsfRoute& aPath);
267 void AppendSubroute(const WsfRoute& aSourceRoute, int aStartWptIndex, int aStopWptIndex, WsfRoute& aAppendRoute);
268
269 void InitializeShortestPath();
270
271 void TestNetwork();
272
273 std::vector<ut::CloneablePtr<WsfRoute>> mRoutes; // The routes in the network
274 std::vector<WsfWaypoint> mNwaypoint; // Waypoint location of node
275 // Graph representation needed for shortest path computations.
276
277 RouteGraph mGraph;
278 RouteCostFunc mCostFunc;
279
280 // The following data is needed for shortest path computations.
281 bool mShortestPathInitialized;
282 bool mVerbose;
283 bool mShowRoutes;
284
285 UtQuadTree* mTreePtr;
286 double mMaxLat;
287 double mMinLat;
288 double mMaxLon;
289 double mMinLon;
290 double mCenterLLA[3];
291 int mTreeDepth;
292
293 double mTransECEF[3][3];
294 double mRefECEF[3];
295
296 bool mInitialized;
297 int mNodeId;
298};
299
300#endif
UtStringId WsfStringId
WsfStringId – the same thing as UtStringId.
Definition WsfStringId.hpp:23
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
WsfObject()
This is the constructor for the WsfObject class.
Definition WsfObject.cpp:88
virtual WsfObject * Clone() const =0
virtual bool ProcessInput(UtInput &aInput)
Definition WsfObject.cpp:140
Definition WsfRouteNetworkTypes.hpp:22
A cost function used by the shortest_path algorithm provided by the RouteGraph class.
Definition WsfRouteNetwork.hpp:188
double operator()(const RouteEdge &aEdge, const RouteNode &, const RouteNode &) const override
Returns the precomputed cost along the edge.
Definition WsfRouteNetwork.hpp:200
double operator()(const RouteNode &aNode1, const RouteNode &aNode2) const override
Computes a lower-bound cost to move from aNode1 to aNode2 (a straight (curved) line).
Definition WsfRouteNetwork.hpp:206
RouteCostFunc(WsfRouteNetwork *aNetworkPtr)
Definition WsfRouteNetwork.hpp:194
WsfRouteNetwork * mNetworkPtr
Definition WsfRouteNetwork.hpp:228
RouteCostFunc()
Definition WsfRouteNetwork.hpp:190
Specifies a location along a route.
Definition WsfRouteNetwork.hpp:51
WaypointAddr mWaypointAddr
Waypoint and route index.
Definition WsfRouteNetwork.hpp:64
RoutePoint(const WaypointAddr &aAddr, double aDistanceAlongSegment=0)
Definition WsfRouteNetwork.hpp:54
int RouteIndex() const
Definition WsfRouteNetwork.hpp:60
double mDistanceAlongSegment
Distance from the waypoint addressed toward the next waypoint.
Definition WsfRouteNetwork.hpp:67
int WaypointIndex() const
Definition WsfRouteNetwork.hpp:61
UtGraphT< RouteNode, RouteEdge, false, double > RouteGraph
Definition WsfRouteNetwork.hpp:182
std::pair< int, int > WaypointAddr
A waypoint address. Minimal information required to look up a waypoint.
Definition WsfRouteNetwork.hpp:46
bool Initialize()
Definition WsfRouteNetwork.cpp:138
bool GeneratePathOffRouteToRoute(double aStartLat, double aStartLon, double aEndLat, double aEndLon, WsfRoute &aPath)
Definition WsfRouteNetwork.cpp:436
WsfRouteNetwork & operator=(const WsfRouteNetwork &)=delete
unsigned int GetRouteCount() const
Definition WsfRouteNetwork.hpp:106
std::vector< WaypointAddr > WaypointAddrList
Definition WsfRouteNetwork.hpp:47
bool FindClosestWaypoint(double aLat, double aLon, WsfWaypoint &aWpt, double &aDistance)
Returns the closest waypoint relative to the provided latitude and longitude.
Definition WsfRouteNetwork.cpp:651
WsfRouteNetwork()
Definition WsfRouteNetwork.cpp:47
bool GeneratePathBetweenClosestSegments(double aStartLat, double aStartLon, double aEndLat, double aEndLon, WsfRoute &aPath, bool aLeaveRouteNetwork=true)
Definition WsfRouteNetwork.cpp:539
const RouteGraph & GetGraph() const
Definition WsfRouteNetwork.hpp:184
bool FindClosestSegment(double aLat, double aLon, RoutePoint &aProjectedPoint, double &aDistanceToSegment)
Definition WsfRouteNetwork.cpp:582
bool AppendShortestPathOnNetwork(RoutePoint aPoint1, RoutePoint aPoint2, WsfRoute &aPath)
Definition WsfRouteNetwork.cpp:294
WsfStringId cSTOP_NODE_ID
Definition WsfRouteNetwork.hpp:43
bool GeneratePathBetweenClosestWaypoints(double aStartLat, double aStartLon, double aEndLat, double aEndLon, WsfRoute &aPath)
Definition WsfRouteNetwork.cpp:507
static bool CompareWaypointAddrRoute(const WaypointAddr &aLhs, const WaypointAddr &aRhs)
Definition WsfRouteNetwork.hpp:123
bool GenerateShortestPathBetweenWaypoints(const WsfWaypoint &aFromWaypoint, const WsfWaypoint &aToWaypoint, WsfRoute &aPath)
Definition WsfRouteNetwork.cpp:414
static std::unique_ptr< UtScriptClass > CreateScriptClass(const std::string &aClassName, UtScriptTypes *aScriptTypesPtr)
Definition WsfRouteNetwork.cpp:1285
WsfStringId cSTART_NODE_ID
Definition WsfRouteNetwork.hpp:42
void BuildQuadtree()
Definition WsfRouteNetwork.cpp:981
WSF_DEPRECATED bool Add(WsfRoute *aRoute)
Definition WsfRouteNetwork.cpp:205
A collection of WsfWaypoint objects that represent a path to be followed.
Definition WsfRoute.hpp:40
Definition WsfWaypoint.hpp:57
double GetLat() const
Definition WsfWaypoint.hpp:129
double GetLon() const
Definition WsfWaypoint.hpp:132
Represents an edge key for use with the RouteGraph class.
Definition WsfRouteNetwork.hpp:157
bool operator==(const RouteEdge &aRhs) const
Definition WsfRouteNetwork.hpp:172
RouteEdge()
Definition WsfRouteNetwork.hpp:158
double mDistance
Definition WsfRouteNetwork.hpp:179
RouteNode mSrcNode
Definition WsfRouteNetwork.hpp:177
RouteNode mDstNode
Definition WsfRouteNetwork.hpp:178
RouteEdge(const RouteNode &aSrcNode, const RouteNode &aDstNode)
Definition WsfRouteNetwork.hpp:165
Represents a node key for use with the RouteGraph class.
Definition WsfRouteNetwork.hpp:130
bool operator!=(const RouteNode &aRhs) const
Definition WsfRouteNetwork.hpp:147
bool operator==(const RouteNode &aRhs) const
Definition WsfRouteNetwork.hpp:145
RouteNode(WsfStringId aId, WaypointAddrList *aWptListPtr=nullptr)
Definition WsfRouteNetwork.hpp:138
bool operator<(const RouteNode &aRhs) const
Definition WsfRouteNetwork.hpp:149
WaypointAddrList * mWaypoints
Definition WsfRouteNetwork.hpp:152
RouteNode()
Definition WsfRouteNetwork.hpp:131
bool mSegmentNode
Definition WsfRouteNetwork.hpp:150
WsfStringId mId
Definition WsfRouteNetwork.hpp:151
Copyrights Multiple, All Rights Reserved