]> git.mxchange.org Git - flightgear.git/blob - src/Aircraft/FlightHistory.cxx
Flight-path-history.
[flightgear.git] / src / Aircraft / FlightHistory.cxx
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 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include "FlightHistory.hxx"
28
29 #include <algorithm>
30 #include <boost/foreach.hpp>
31
32 #include <simgear/sg_inlines.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/misc/strutils.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/math/SGMath.hxx>
38
39 #include <Main/fg_props.hxx>
40 #include <Main/globals.hxx>
41
42 FGFlightHistory::FGFlightHistory() :
43     m_sampleInterval(5.0),
44     m_validSampleCount(SAMPLE_BUCKET_WIDTH)
45 {
46 }
47
48 FGFlightHistory::~FGFlightHistory()
49 {
50 }
51
52 void FGFlightHistory::init()
53 {
54     m_sampleInterval = fgGetDouble("/sim/history/sample-interval-sec", 1.0);
55     if (m_sampleInterval <= 0.0) { // would be bad
56         SG_LOG(SG_FLIGHT, SG_INFO, "invalid flight-history sample interval:" << m_sampleInterval
57                << ", defaulting to " << m_sampleInterval);
58         m_sampleInterval = 1.0;
59     }
60     
61     m_weightOnWheels = NULL;
62 // reset the history when we detect a take-off
63     if (fgGetBool("/sim/history/clear-on-takeoff", true)) {
64         m_weightOnWheels = fgGetNode("/gear/gear[1]/wow", 0, true);
65         m_lastWoW = m_weightOnWheels->getBoolValue();
66     }
67     
68     // force bucket re-allocation
69     m_validSampleCount = SAMPLE_BUCKET_WIDTH;
70 }
71
72 void FGFlightHistory::shutdown()
73 {
74     clear();
75 }
76
77 void FGFlightHistory::reinit()
78 {
79     shutdown();
80     init();
81 }
82
83 void FGFlightHistory::update(double dt)
84 {
85     SG_UNUSED(dt); // we care about sim-time, not frame-time
86     
87     if (m_weightOnWheels) {
88         
89         if (m_lastWoW && !m_weightOnWheels->getBoolValue()) {
90             SG_LOG(SG_FLIGHT, SG_INFO, "history: detected main-gear takeoff, clearing history");
91             clear();
92         }
93     } // of rest-on-takeoff enabled
94     
95     double elapsed = globals->get_sim_time_sec() - m_lastCaptureTime;
96     if (elapsed > m_sampleInterval) {
97         capture();
98     }
99 }
100
101 void FGFlightHistory::allocateNewBucket()
102 {
103     SampleBucket* bucket = new SampleBucket;
104     m_buckets.push_back(bucket);
105     m_validSampleCount = 0;
106 }
107
108 void FGFlightHistory::capture()
109 {
110     if (m_validSampleCount == SAMPLE_BUCKET_WIDTH) {
111         // bucket is full, allocate a new one
112         allocateNewBucket();
113     }
114     
115     m_lastCaptureTime = globals->get_sim_time_sec();
116     Sample* sample = m_buckets.back()->samples + m_validSampleCount;
117     
118     sample->simTimeMSec = static_cast<int>(m_lastCaptureTime * 1000.0);
119     sample->position = globals->get_aircraft_position();
120     
121     double heading, pitch, roll;
122     globals->get_aircraft_orientation(heading, pitch, roll);
123     sample->heading = static_cast<float>(heading);
124     sample->pitch = static_cast<float>(pitch);
125     sample->roll = static_cast<float>(roll);
126     
127     ++m_validSampleCount;
128 }
129
130 SGGeodVec FGFlightHistory::pathForHistory(double minEdgeLengthM) const
131 {
132     SGGeodVec result;
133     if (m_buckets.empty()) {
134         return result;
135     }
136     
137     result.push_back(m_buckets.front()->samples[0].position);
138     SGVec3d lastOutputCart = SGVec3d::fromGeod(result.back());
139     double minLengthSqr = minEdgeLengthM * minEdgeLengthM;
140     
141     BOOST_FOREACH(SampleBucket* bucket, m_buckets) {
142         unsigned int count = (bucket == m_buckets.back() ? m_validSampleCount : SAMPLE_BUCKET_WIDTH);
143     
144         // iterate over all the valid samples in the bucket
145         for (unsigned int index = 0; index < count; ++index) {
146             SGGeod g = bucket->samples[index].position;
147             SGVec3d cart(SGVec3d::fromGeod(g));
148             if (distSqr(cart, lastOutputCart) > minLengthSqr) {
149                 lastOutputCart =  cart;
150                 result.push_back(g);
151             }
152         } // of samples iteration
153     } // of buckets iteration
154     
155     return result;
156 }
157
158 void FGFlightHistory::clear()
159 {
160     BOOST_FOREACH(SampleBucket* ptr, m_buckets) {
161         delete ptr;
162     }
163     m_buckets.clear();
164     m_validSampleCount = SAMPLE_BUCKET_WIDTH;
165 }
166