]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/FlightHistory.hxx
Interim windows build fix
[flightgear.git] / src / Aircraft / FlightHistory.hxx
1 // FlightHistory
2 //
3 // Written by James Turner, started December 2012.
4 //
5 // Copyright (C) 2012 James Turner - zakalawe (at) mac com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #ifndef FG_AIRCRAFT_FLIGHT_HISTORY_HXX
24 #define FG_AIRCRAFT_FLIGHT_HISTORY_HXX
25
26 #include <simgear/structure/subsystem_mgr.hxx>
27 #include <simgear/props/props.hxx>
28 #include <simgear/math/SGMath.hxx>
29
30 #include <vector>
31 #include <memory> // for std::auto_ptr
32
33 typedef std::vector<SGGeod> SGGeodVec;
34
35 class PagedPathForHistory : public SGReferenced {
36 public:
37         PagedPathForHistory() : last_seen(0) {}
38         virtual ~PagedPathForHistory() {}
39         SGGeodVec path;
40         time_t last_seen;
41 };
42 typedef SGSharedPtr<PagedPathForHistory> PagedPathForHistory_ptr;
43
44 const unsigned int SAMPLE_BUCKET_WIDTH = 1024;
45
46 /**
47  * record the history of the aircraft's movements, making it available
48  * as a contiguous block. This can be used to show the historical flight-path
49  * over a long period of time (unlike the replay system), but only a small,
50  * fixed set of properties are recorded. (Positioned and orientation, but
51  * not velocity, acceleration, control inputs, or so on)
52  */
53 class FGFlightHistory : public SGSubsystem
54 {
55 public:
56     FGFlightHistory();
57     virtual ~FGFlightHistory();
58     
59     virtual void init();
60     virtual void shutdown();
61     virtual void reinit();
62     virtual void update(double dt);
63     
64     PagedPathForHistory_ptr pagedPathForHistory(size_t max_entries, size_t newerThan = 0) const;
65     /**
66      * retrieve the path, collapsing segments shorter than
67      * the specified minimum length
68      */
69     SGGeodVec pathForHistory(double minEdgeLengthM = 50.0) const;
70
71     /**
72      * clear the history
73      */
74
75     void clear();
76
77 private:
78     /**
79      * @class A single data sample in the history system.
80      */
81     class Sample
82     {
83     public:
84         SGGeod position;
85         /// heading, pitch and roll can be recorded at lower precision
86         /// than a double - actually 16 bits might be sufficient
87         float heading, pitch, roll;
88         size_t simTimeMSec;
89     };
90     
91     
92     
93     /**
94      * Bucket is a fixed-size container of samples. This is a crude slab
95      * allocation of samples, in chunks defined by the width constant above.
96      * Keep in mind that even with a 1Hz sample frequency, we use less than
97      * 200kbytes per hour - avoiding continous malloc traffic, or expensive
98      * std::vector reallocations, is the key factor here.
99      */
100     class SampleBucket
101     {
102     public:
103         Sample samples[SAMPLE_BUCKET_WIDTH];
104     };
105     
106     double m_lastCaptureTime;
107     double m_sampleInterval; ///< sample interval in seconds
108 /// our store of samples (in buckets). The last bucket is partially full,
109 /// with the number of valid samples indicated by m_validSampleCount
110     std::vector<SampleBucket*> m_buckets;
111     
112 /// number of valid samples in the final bucket
113     unsigned int m_validSampleCount;
114     
115     SGPropertyNode_ptr m_weightOnWheels;
116     SGPropertyNode_ptr m_enabled;
117   
118     bool m_lastWoW;
119     size_t m_maxMemoryUseBytes;
120   
121     void allocateNewBucket();
122     
123     void capture();
124   
125     size_t currentMemoryUseBytes() const;
126 };
127
128 #endif