3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "UnorderedTask.hpp"
27 #include "UnorderedTaskPoint.hpp"
28 #include "GlideSolvers/GlidePolar.hpp"
29 #include "AlternatePoint.hpp"
30 #include "AlternateList.hpp"
36 class AbortIntersectionTest
{
38 virtual bool Intersects(const AGeoPoint
& destination
) = 0;
42 * Abort task provides automatic management of a sorted list of task points
43 * that are reachable or close to reachable, and landable (with airfields preferred).
46 * - should prefer landable points that are non-intersecting with terrain
48 * Sorting order is as follows:
49 * - airfields reachable from final glide (sorted by arrival time) at safety mc
50 * - landpoints reachable from final glide (sorted by arrival time) at safety mc
51 * - airfields reachable with climb (sorted by arrival time including climb time)
53 * - landpoints reachable with climb (sorted by arrival time including climb time)
57 class AbortTask
: public UnorderedTask
59 /** max number of items in list */
60 static constexpr unsigned max_abort
= 10;
63 struct AlternateTaskPoint
{
64 UnorderedTaskPoint point
;
67 AlternateTaskPoint(const Waypoint
&waypoint
, const TaskBehaviour
&tb
,
68 const GlideResult
&_solution
)
69 :point(waypoint
, tb
), solution(_solution
) {}
72 typedef std::vector
<AlternateTaskPoint
> AlternateTaskVector
;
73 AlternateTaskVector task_points
;
75 /** whether the AbortTask is the master or running in background */
78 const Waypoints
&waypoints
;
80 /** Hook for external intersection tests */
81 AbortIntersectionTest
* intersection_test
;
84 unsigned active_waypoint
;
85 bool reachable_landable
;
91 * @param tb Global task behaviour settings
92 * @param gp Global glide polar used for navigation calculations
93 * @param wps Waypoints container to be scanned during updates
95 * @return Initialised object (with nothing in task)
97 AbortTask(const TaskBehaviour
&tb
,
98 const Waypoints
&wps
);
100 void SetTaskBehaviour(const TaskBehaviour
&tb
);
102 const UnorderedTaskPoint
&GetAlternate(unsigned i
) const {
103 assert(i
< task_points
.size());
105 return task_points
[i
].point
;
109 * Retrieves the active task point index.
111 * @return Index of active task point sequence
113 unsigned GetActiveIndex() const {
114 return active_task_point
;
118 * Determine if any landable reachable waypoints were found in the
121 * @return True if a landable waypoint was found
123 bool HasReachableLandable() const {
124 return reachable_landable
;
128 * Calculate vector to home waypoint
130 * @param state State of aircraft
131 * @return Vector to home waypoint
133 GeoVector
GetHomeVector(const AircraftState
&state
) const;
134 const Waypoint
*GetHome() const;
138 * Clears task points in list
140 virtual void Clear();
143 * Check whether abort task list is full
145 * @return True if no more task points can be added
148 bool IsTaskFull() const;
151 * Calculate distance to search for landable waypoints for aircraft.
153 * @param state_now Aircraft state
155 * @return Distance (m) of approximate glide range of aircraft
158 fixed
GetAbortRange(const AircraftState
&state_now
,
159 const GlidePolar
&glide_polar
) const;
162 * Fill abort task list with candidate waypoints given a list of
163 * waypoints satisfying approximate range queries. Can be used
164 * to add airfields only, or landpoints.
166 * @param state Aircraft state
167 * @param approx_waypoints List of candidate waypoints
168 * @param polar Polar used for tests
169 * @param only_airfield If true, only add waypoints that are airfields.
170 * @param final_glide Whether solution must be glide only or climb allowed
171 * @param safety Whether solution uses safety polar
173 * @return True if a landpoint within final glide was found
175 bool FillReachable(const AircraftState
&state
,
176 AlternateList
&approx_waypoints
,
177 const GlidePolar
&polar
, bool only_airfield
,
178 bool final_glide
, bool safety
);
182 * This is called by update_sample after the turnpoint list has
183 * been filled with landpoints.
184 * It's first called after the reachable scan, then may be called again after scanning
187 virtual void ClientUpdate(const AircraftState
&state_now
, bool reachable
);
191 * Specify whether the task is active or not. If it's active, it will
192 * notify the user about the abort task point being changed via the events.
194 void SetActive(bool _active
) {
199 * Set external test function to be used for additional intersection tests
201 void SetIntersectionTest(AbortIntersectionTest
*test
) {
202 intersection_test
= test
;
206 * Accept a const task point visitor; makes the visitor visit
207 * all TaskPoint in the task
209 * @param visitor Visitor to accept
210 * @param reverse Visit task points in reverse order
212 void AcceptTaskPointVisitor(TaskPointConstVisitor
&visitor
) const override
;
215 /* virtual methods from class TaskInterface */
216 virtual unsigned TaskSize() const override
;
217 virtual void SetActiveTaskPoint(unsigned index
) override
;
218 virtual TaskWaypoint
*GetActiveTaskPoint() const override
;
219 virtual bool IsValidTaskPoint(int index_offset
) const override
;
221 /* virtual methods from class AbstractTask */
222 virtual void Reset() override
;
225 virtual bool UpdateSample(const AircraftState
&state_now
,
226 const GlidePolar
&glide_polar
,
227 bool full_update
) override
;
228 virtual bool CheckTransitions(const AircraftState
&state_now
,
229 const AircraftState
&state_last
) override
;