WSF
WsfPathFinder.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//* PURPOSE:
16//* This class is used to find a path from A to B in a 2D environment with
17//* weighted nodes. It uses the UtGraph class to build a set of nodes that
18//* hold the weights for each square in a lat/lon grid. Squares are measured
19//* in arc degrees rather than meters because the class needs to work on a
20//* spherical earth.
21//*
22//* AUTHOR
23//* Michael Williams
24//*
25//* NOTES:
26//* None
27//***************************************************************************
28
29#ifndef WSFPATHFINDER_HPP
30#define WSFPATHFINDER_HPP
31
32#include "wsf_export.h"
33
34#include <limits>
35#include <list>
36#include <map>
37#include <vector>
38
39class pf_cost_func;
40#include "UtGraph.hpp"
41class UtInput;
42class UtInputBlock;
43class WsfDraw;
44#include "WsfGeoPoint.hpp"
45#include "WsfObject.hpp"
46#include "WsfObjectTypeList.hpp"
47class WsfPlatform;
48class WsfRoute;
49#include "WsfRouteComputer.hpp"
51class WsfSimulation;
52class WsfZone;
53
54// PFNode and PFEdge have to be defined out here because they're used for the
55// definition of WsfPathFinder, an inheritor of a templated UtGraph class.
56
58{
59public:
60 WsfPFNode(long aX, long aY, float aBaseWeight, WsfGeoPoint& aLoc);
61
62 bool operator==(const WsfPFNode& aRhs) const;
63
64 bool operator!=(const WsfPFNode& aRhs) const;
65
66 bool operator<(const WsfPFNode& aRhs) const;
67
68 void setMarked(bool b);
69 bool getMarked();
70 void AddNeighbor(WsfPFNode* aNodePtr);
71
72 std::vector<WsfPFNode*> mNeighbors;
73 long mX;
74 long mY;
76 float mBaseWeight; // mutable because we need to modify it through const iterators
77 mutable double mWeight;
78 bool mMarkedPt; // points are marked as significant or insignificant by the RQT reduction algorithm
81};
82
85
87{
88 WsfPFEdge(WsfPFNode* aSrcNodePtr, WsfPFNode* aDstNodePtr)
89 : mSrcNodePtr(aSrcNodePtr)
90 , mDstNodePtr(aDstNodePtr)
91 {
92 mLength = aSrcNodePtr->mLoc.GetDistanceFrom(aDstNodePtr->mLoc);
93 }
94
95 bool operator==(const WsfPFEdge& aRhs) const
96 {
97 return ((mSrcNodePtr == aRhs.mSrcNodePtr) && (mDstNodePtr == aRhs.mDstNodePtr));
98 }
99
102 double mLength;
103};
104
107 public UtGraphT<WsfPFNode, WsfPFEdge, false, double>,
108 public WsfRouteComputer
109{
110public:
111 using PFGraph = UtGraphT<WsfPFNode, WsfPFEdge, false, double>;
112
113 WsfPathFinder(WsfScenario* aScenarioPtr, WsfGeoPoint& aUpperLeftPtr, WsfGeoPoint& aLowerRightPtr, double aGridSizeDegrees);
114 WsfPathFinder(WsfScenario* aScenarioPtr);
115 ~WsfPathFinder() override;
116
117 virtual bool Initialize(WsfSimulation* aSimulationPtr);
118
119 bool ProcessInput(UtInput& aInput) override;
120
121 const char* GetScriptClassName() const override;
122
123 // Add aZonePtr with weight aWeight. Higher weights mean stronger avoidance.
124 // Does not allow duplicates.
125 void AddZone(WsfZone* aZonePtr, double aWeight);
126
127 // Remove aZonePtr if it exists.
128 void RemoveZone(WsfZone* aZonePtr);
129
130 void RecalculateWeights();
131
132 virtual bool FindPath(const WsfGeoPoint& aStartPtr, WsfGeoPoint& aEndPtr, WsfRoute& aRoute, double& aCost);
133
134 virtual const WsfPFNode* GetClosestNode(const WsfGeoPoint& aPointPtr);
135
136 void print() const;
137 node_iterator GetGrid(long aX, long aY);
138 node_iterator GetGridAbsolute(unsigned long idx);
139 void SetGrid(unsigned long aX, unsigned long aY, node_iterator aIterPtr);
140 void SetGridAbsolute(unsigned long aX, node_iterator aIterPtr);
141
142 void DebugDrawGrid();
143 virtual void DebugDrawZones();
144
145 WsfGeoPoint GetNodeLocation(int nodeIndex);
146
147 long GetXSize() { return mXSize; }
148 long GetYSize() { return mYSize; }
149 bool GetConsiderNode(const WsfPFNode& aNode);
150 bool GetConsiderNode(const WsfGeoPoint& aGeoPoint);
151 int GetNumberOfZones();
155 WsfGeoPoint* FindClosestPointOnEdge(double aSimTime, const WsfGeoPoint& aGeoPoint);
156 virtual WsfGeoPoint* FindClosestValidPoint(double aSimTime, const WsfGeoPoint& aGeoPoint);
157 WsfGeoPoint* FindClosestPointInQuadrant(const WsfGeoPoint& aGeoPoint, int aQuadrant);
158 WsfZone* GetZoneByName(const std::string& aZoneName);
159
160 bool ComputeFindPath(WsfMover& aMover, WsfRoute& aRoute) override;
161 bool ComputeSetRoute(WsfMover& aMover, WsfRoute& aRoute, int& aInitialPointIndex) override;
162 bool ComputeUpdateRoute(WsfMover& aMover, WsfRoute& aRoute) override;
163 bool ComputeExtrapolate(WsfMover& aMover, WsfRoute& aRoute) override;
164
165 bool ReturnToRoute(WsfMover& aMover, WsfRoute& aRoute) override;
166 virtual WsfGeoPoint GetRandomLocation();
167 virtual void SetDebugEnabled(bool aDebugEnabled) { mDebugEnabled = aDebugEnabled; }
168 bool DebugEnabled() const { return mDebugEnabled; }
170
171protected:
172 bool ContainsZone(WsfZone* aZonePtr);
173 bool ContainsZone(const std::string& aZoneName);
174
175 bool PointIsInGridBounds(int x, int y);
176
177 // void LoadGridInfo();
178 // void GeneratePathFinderLight();
179 bool IsDuplicateEdge(node_iterator nodeIter, const WsfPFNode& aNode2, const WsfPFEdge& tmpEdge);
180 bool shortest_path(const_node_iterator aSrcNodeIter,
181 const_node_iterator aDstNodeIter,
182 NodeList& aPath,
183 double& aCost,
184 const cost_func* aCostFuncPtr = nullptr) const override;
185
188 std::list<WsfZone*> mZones;
189 std::map<WsfZone*, double> mZoneWeights;
190
193
195
196 long mXSize;
197 long mYSize;
198
199 std::vector<node_iterator> mGrid;
200 bool mInitialized; // only true when mUpperLeft,
201 // mLowerRight, and mGridSizeDegrees
202 // have been set and mGrid has been
203 // allocated
204
207
208 class pf_cost_func : public PFGraph::cost_func
209 {
210 public:
211 double operator()(const WsfPFEdge& aEdge, const WsfPFNode& aNode1, const WsfPFNode& aNode2) const override
212 {
213 double cost = aEdge.mLength * aNode2.mWeight;
214 return cost;
215 }
216
217 double operator()(const WsfPFNode& aNode1, const WsfPFNode& aNode2) const override
218 {
219 double estimate = aNode2.mLoc.GetDistanceFrom(aNode1.mLoc);
220 return estimate;
221 }
222
223 // adding consider_node function to the cost_func. This will flag a node to be ignored during path-finding.
224 bool consider_node(const WsfPFNode& aNode1) const override
225 {
226 // NO_FLY_ZONES are marked as DBL_MAX and won't be considered in path-finding
227 return (aNode1.mWeight == std::numeric_limits<double>::max() ? false : true);
228 }
229 };
230};
231
232class WsfPathFinderTypes : public WsfObjectTypeList<WsfPathFinder>
233{
234public:
236 std::unique_ptr<WsfPathFinder> ProcessPathFinderInput(UtInput& aInput);
237 virtual LoadResult LoadInstance(UtInput& aInput);
238 void ProcessBlock(UtInputBlock& aInputBlock, WsfPathFinder* aPFPtr, bool init);
239 bool ProcessInput(UtInput& aInput) override;
240
241 // static WsfPathFinder* FindPathFinder(const std::string& aName);
242
243 // static bool AddPathFinder(const std::string& aTypeName,
244 // WsfPathFinder* aTypePtr);
245 // static void ClearTypes();
246
247 // static WsfObjectTypeList<WsfPathFinder> mTypeList; //!< The list of pathfinders
248};
249
253{
254public:
257 void Add(std::unique_ptr<WsfPathFinder> aPathFinderPtr);
258 WsfPathFinder* Find(const std::string& aPathFinderName) const;
259 bool Initialize(WsfSimulation* aSimulationPtr);
260
261private:
263 using PathFinderMap = std::map<std::string, std::unique_ptr<WsfPathFinder>>;
264
265 WsfSimulation* mSimulationPtr;
266 PathFinderMap mPathFinders;
267};
268
271{
272public:
273 WsfScriptPathFinderClass(const std::string& aClassName, UtScriptTypes* aScriptTypesPtr);
274
275 UT_DECLARE_SCRIPT_METHOD(FindClosestPointOnEdge);
276 UT_DECLARE_SCRIPT_METHOD(FindClosestValidPoint);
279 UT_DECLARE_SCRIPT_METHOD(SetDebugEnabled);
283};
284
285#endif
bool operator!=(int aId, const WsfStringInt &aRhs)
Not equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:75
bool operator==(int aId, const WsfStringInt &aRhs)
Equal relational operator for the case of an integer on the LHS and a StringId on the RHS.
Definition WsfStringId.hpp:69
#define WSF_EXPORT
Definition WsfXIO_Export.hpp:35
Provides the capability to draw shapes into the WSF replay file.
Definition WsfDraw.hpp:35
Definition WsfGeoPoint.hpp:29
double GetDistanceFrom(double aLocationWCS[3]) const
Definition WsfGeoPoint.cpp:151
Definition WsfMover.hpp:37
WsfObjectTypeList(WsfScenario &aScenario, unsigned int aFlags, const std::string &aBlockName)
Definition WsfObjectTypeList.hpp:76
Definition WsfPathFinder.hpp:58
std::vector< WsfPFNode * > mNeighbors
Definition WsfPathFinder.hpp:72
float mNormalAngle
Definition WsfPathFinder.hpp:75
WsfGeoPoint mLoc
Definition WsfPathFinder.hpp:79
bool mMarkedPt
Definition WsfPathFinder.hpp:78
void setMarked(bool b)
Definition WsfPathFinder.cpp:1333
WsfPFNode(long aX, long aY, float aBaseWeight, WsfGeoPoint &aLoc)
Definition WsfPathFinder.cpp:1301
bool operator<(const WsfPFNode &aRhs) const
Definition WsfPathFinder.cpp:1328
int mNetworkIndex
Definition WsfPathFinder.hpp:80
long mY
Definition WsfPathFinder.hpp:74
long mX
Definition WsfPathFinder.hpp:73
bool getMarked()
Definition WsfPathFinder.cpp:1338
float mBaseWeight
Definition WsfPathFinder.hpp:76
void AddNeighbor(WsfPFNode *aNodePtr)
Definition WsfPathFinder.cpp:1343
double mWeight
Definition WsfPathFinder.hpp:77
WsfPathFinder * Find(const std::string &aPathFinderName) const
Definition WsfPathFinder.cpp:1465
bool Initialize(WsfSimulation *aSimulationPtr)
Definition WsfPathFinder.cpp:1484
void Add(std::unique_ptr< WsfPathFinder > aPathFinderPtr)
Definition WsfPathFinder.cpp:1475
WsfPathFinderList()
Definition WsfPathFinder.cpp:1459
~WsfPathFinderList()=default
WsfPathFinderTypes(WsfScenario &aScenario)
Definition WsfPathFinder.cpp:1353
std::unique_ptr< WsfPathFinder > ProcessPathFinderInput(UtInput &aInput)
Definition WsfPathFinder.cpp:1358
virtual LoadResult LoadInstance(UtInput &aInput)
Create an instance of a 'type object' of this class.
Definition WsfPathFinder.cpp:1398
void ProcessBlock(UtInputBlock &aInputBlock, WsfPathFinder *aPFPtr, bool init)
Definition WsfPathFinder.cpp:1432
bool ProcessInput(UtInput &aInput) override
Definition WsfPathFinder.cpp:1454
Definition WsfPathFinder.hpp:209
double operator()(const WsfPFNode &aNode1, const WsfPFNode &aNode2) const override
Definition WsfPathFinder.hpp:217
bool consider_node(const WsfPFNode &aNode1) const override
Definition WsfPathFinder.hpp:224
double operator()(const WsfPFEdge &aEdge, const WsfPFNode &aNode1, const WsfPFNode &aNode2) const override
Definition WsfPathFinder.hpp:211
Definition WsfPathFinder.hpp:109
WsfDraw * mDebugDrawObjPtr
Definition WsfPathFinder.hpp:205
std::vector< node_iterator > mGrid
Definition WsfPathFinder.hpp:199
long mXSize
Definition WsfPathFinder.hpp:196
UtGraphT< WsfPFNode, WsfPFEdge, false, double > PFGraph
Definition WsfPathFinder.hpp:111
double mGridSizeDegrees
Definition WsfPathFinder.hpp:194
virtual void DebugDrawZones()
Definition WsfPathFinder.cpp:1052
WsfPathFinder(WsfScenario *aScenarioPtr, WsfGeoPoint &aUpperLeftPtr, WsfGeoPoint &aLowerRightPtr, double aGridSizeDegrees)
Definition WsfPathFinder.cpp:64
void RecalculateWeights()
Definition WsfPathFinder.cpp:259
const char * GetScriptClassName() const override
Definition WsfPathFinder.cpp:1295
void RemoveZone(WsfZone *aZonePtr)
Definition WsfPathFinder.cpp:216
node_iterator GetGridAbsolute(unsigned long idx)
Definition WsfPathFinder.cpp:423
WsfGeoPoint GetLowerRight()
Definition WsfPathFinder.hpp:154
WsfGeoPoint mUpperLeft
Definition WsfPathFinder.hpp:191
WsfSimulation * mSimulationPtr
Definition WsfPathFinder.hpp:187
WsfGeoPoint mLowerRight
Definition WsfPathFinder.hpp:192
long GetXSize()
Definition WsfPathFinder.hpp:147
bool ProcessInput(UtInput &aInput) override
Definition WsfPathFinder.cpp:465
std::list< WsfZone * > mZones
Definition WsfPathFinder.hpp:188
void SetGrid(unsigned long aX, unsigned long aY, node_iterator aIterPtr)
Definition WsfPathFinder.cpp:454
WsfSimulation * GetSimulation() const
Definition WsfPathFinder.hpp:169
double GetGridSizeDegrees()
Definition WsfPathFinder.hpp:152
void print() const
Definition WsfPathFinder.cpp:368
bool mDebugEnabled
Definition WsfPathFinder.hpp:206
WsfGeoPoint GetUpperLeft()
Definition WsfPathFinder.hpp:153
WsfGeoPoint GetNodeLocation(int nodeIndex)
Definition WsfPathFinder.cpp:428
void AddZone(WsfZone *aZonePtr, double aWeight)
Definition WsfPathFinder.cpp:206
bool DebugEnabled() const
Definition WsfPathFinder.hpp:168
virtual bool FindPath(const WsfGeoPoint &aStartPtr, WsfGeoPoint &aEndPtr, WsfRoute &aRoute, double &aCost)
Definition WsfPathFinder.cpp:314
virtual void SetDebugEnabled(bool aDebugEnabled)
Definition WsfPathFinder.hpp:167
virtual bool Initialize(WsfSimulation *aSimulationPtr)
Definition WsfPathFinder.cpp:102
node_iterator GetGrid(long aX, long aY)
Definition WsfPathFinder.cpp:418
long mYSize
Definition WsfPathFinder.hpp:197
void SetGridAbsolute(unsigned long aX, node_iterator aIterPtr)
Definition WsfPathFinder.cpp:459
std::map< WsfZone *, double > mZoneWeights
Definition WsfPathFinder.hpp:189
long GetYSize()
Definition WsfPathFinder.hpp:148
void DebugDrawGrid()
Definition WsfPathFinder.cpp:1029
WsfScenario * mScenarioPtr
Definition WsfPathFinder.hpp:186
virtual const WsfPFNode * GetClosestNode(const WsfGeoPoint &aPointPtr)
Definition WsfPathFinder.cpp:292
bool mInitialized
Definition WsfPathFinder.hpp:200
Platforms represent a entity within the simulation.
Definition WsfPlatform.hpp:115
Definition WsfRouteComputer.hpp:26
A collection of WsfWaypoint objects that represent a path to be followed.
Definition WsfRoute.hpp:40
Contains the data required to create a simulation, and acts as the entry point for input file process...
Definition WsfScenario.hpp:111
WsfScriptObjectClass(const std::string &aClassName, UtScriptTypes *aTypesPtr)
Definition WsfScriptObjectClass.cpp:24
UT_DECLARE_SCRIPT_METHOD(RandomLocation)
UT_DECLARE_SCRIPT_METHOD(NearEndOfPath)
UT_DECLARE_SCRIPT_METHOD(FindClosestValidPoint)
UT_DECLARE_SCRIPT_METHOD(FindClosestPointOnEdge)
UT_DECLARE_SCRIPT_METHOD(DebugDrawGrid)
UT_DECLARE_SCRIPT_METHOD(SetDebugEnabled)
WsfScriptPathFinderClass(const std::string &aClassName, UtScriptTypes *aScriptTypesPtr)
Definition WsfPathFinder.cpp:1083
UT_DECLARE_SCRIPT_METHOD(DebugEnabled)
UT_DECLARE_SCRIPT_METHOD(DebugDrawZones)
The main controller for a simulation.
Definition WsfSimulation.hpp:109
WsfUncloneableObject()
Definition WsfObject.hpp:148
Definition WsfZone.hpp:63
Definition WsfPathFinder.hpp:87
WsfPFNode * mDstNodePtr
Definition WsfPathFinder.hpp:101
bool operator==(const WsfPFEdge &aRhs) const
Definition WsfPathFinder.hpp:95
double mLength
Definition WsfPathFinder.hpp:102
WsfPFEdge(WsfPFNode *aSrcNodePtr, WsfPFNode *aDstNodePtr)
Definition WsfPathFinder.hpp:88
WsfPFNode * mSrcNodePtr
Definition WsfPathFinder.hpp:100
Copyrights Multiple, All Rights Reserved