2003-07-17 18:24:17 +00:00
|
|
|
// replay.cxx - a system to record and replay FlightGear flights
|
|
|
|
//
|
2011-10-01 20:58:40 +00:00
|
|
|
// Written by Curtis Olson, started July 2003.
|
|
|
|
// Updated by Thorsten Brehm, September 2011.
|
2003-07-17 18:24:17 +00:00
|
|
|
//
|
2004-11-19 22:10:41 +00:00
|
|
|
// Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
|
2003-07-17 18:24:17 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
2006-02-21 01:16:04 +00:00
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-07-17 18:24:17 +00:00
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
2006-02-18 13:58:09 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2011-03-24 22:30:09 +00:00
|
|
|
#include <float.h>
|
2011-10-01 20:58:40 +00:00
|
|
|
|
2003-07-17 21:21:00 +00:00
|
|
|
#include <simgear/constants.h>
|
2011-03-21 22:07:05 +00:00
|
|
|
#include <simgear/structure/exception.hxx>
|
2003-07-17 21:21:00 +00:00
|
|
|
|
2003-07-18 14:14:24 +00:00
|
|
|
#include <Main/fg_props.hxx>
|
2003-07-17 18:24:17 +00:00
|
|
|
|
|
|
|
#include "replay.hxx"
|
2011-10-01 20:58:40 +00:00
|
|
|
#include "flightrecorder.hxx"
|
2003-07-17 18:24:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
|
2011-03-21 22:07:05 +00:00
|
|
|
FGReplay::FGReplay() :
|
2011-10-01 20:58:40 +00:00
|
|
|
last_replay_state(0),
|
|
|
|
m_high_res_time(60.0),
|
|
|
|
m_medium_res_time(600.0),
|
|
|
|
m_low_res_time(3600.0),
|
|
|
|
m_medium_sample_rate(0.5), // medium term sample rate (sec)
|
|
|
|
m_long_sample_rate(5.0), // long term sample rate (sec)
|
|
|
|
m_pRecorder(new FGFlightRecorder("replay-config"))
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
|
2011-03-21 22:07:05 +00:00
|
|
|
FGReplay::~FGReplay()
|
|
|
|
{
|
|
|
|
clear();
|
2011-10-01 20:58:40 +00:00
|
|
|
|
|
|
|
delete m_pRecorder;
|
|
|
|
m_pRecorder = NULL;
|
2011-03-21 22:07:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all internal buffers.
|
|
|
|
*/
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::clear()
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
|
|
|
while ( !short_term.empty() )
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
m_pRecorder->deleteRecord(short_term.front());
|
2008-04-02 18:55:39 +00:00
|
|
|
short_term.pop_front();
|
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
while ( !medium_term.empty() )
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
m_pRecorder->deleteRecord(medium_term.front());
|
2008-04-02 18:55:39 +00:00
|
|
|
medium_term.pop_front();
|
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
while ( !long_term.empty() )
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
m_pRecorder->deleteRecord(long_term.front());
|
2008-04-02 18:55:39 +00:00
|
|
|
long_term.pop_front();
|
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
while ( !recycler.empty() )
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
m_pRecorder->deleteRecord(recycler.front());
|
2008-04-02 18:55:39 +00:00
|
|
|
recycler.pop_front();
|
|
|
|
}
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the data structures
|
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::init()
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
disable_replay = fgGetNode("/sim/replay/disable", true);
|
|
|
|
replay_master = fgGetNode("/sim/freeze/replay-state", true);
|
|
|
|
replay_time = fgGetNode("/sim/replay/time", true);
|
|
|
|
replay_time_str = fgGetNode("/sim/replay/time-str", true);
|
|
|
|
replay_looped = fgGetNode("/sim/replay/looped", true);
|
|
|
|
speed_up = fgGetNode("/sim/speed-up", true);
|
2011-03-20 13:59:19 +00:00
|
|
|
reinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset replay queues.
|
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::reinit()
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
sim_time = 0.0;
|
|
|
|
last_mt_time = 0.0;
|
|
|
|
last_lt_time = 0.0;
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
// Flush queues
|
2011-03-21 22:07:05 +00:00
|
|
|
clear();
|
2011-10-01 20:58:40 +00:00
|
|
|
m_pRecorder->reinit();
|
|
|
|
|
|
|
|
m_high_res_time = fgGetDouble("/sim/replay/buffer/high-res-time", 60.0);
|
|
|
|
m_medium_res_time = fgGetDouble("/sim/replay/buffer/medium-res-time", 600.0); // 10 mins
|
|
|
|
m_low_res_time = fgGetDouble("/sim/replay/buffer/low-res-time", 3600.0); // 1 h
|
|
|
|
// short term sample rate is as every frame
|
|
|
|
m_medium_sample_rate = fgGetDouble("/sim/replay/buffer/medium-res-sample-dt", 0.5); // medium term sample rate (sec)
|
|
|
|
m_long_sample_rate = fgGetDouble("/sim/replay/buffer/low-res-sample-dt", 5.0); // long term sample rate (sec)
|
2011-03-21 22:07:05 +00:00
|
|
|
|
2008-04-02 18:55:39 +00:00
|
|
|
// Create an estimated nr of required ReplayData objects
|
2011-10-01 20:58:40 +00:00
|
|
|
// 120 is an estimated maximum frame rate.
|
|
|
|
int estNrObjects = (int) ((m_high_res_time*120) + (m_medium_res_time*m_medium_sample_rate) +
|
|
|
|
(m_low_res_time*m_long_sample_rate));
|
2011-03-21 22:07:05 +00:00
|
|
|
for (int i = 0; i < estNrObjects; i++)
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
FGReplayData* r = m_pRecorder->createEmptyRecord();
|
|
|
|
if (r)
|
|
|
|
recycler.push_back(r);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
|
|
|
|
}
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
2011-03-22 20:02:57 +00:00
|
|
|
replay_master->setIntValue(0);
|
|
|
|
disable_replay->setBoolValue(0);
|
|
|
|
replay_time->setDoubleValue(0);
|
2011-10-01 20:58:40 +00:00
|
|
|
replay_time_str->setStringValue("");
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind to the property tree
|
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::bind()
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unbind from the property tree
|
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::unbind()
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
// nothing to unbind
|
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
static void
|
|
|
|
printTimeStr(char* pStrBuffer,double _Time, bool ShowDecimal=true)
|
|
|
|
{
|
|
|
|
if (_Time<0)
|
|
|
|
_Time = 0;
|
2012-01-31 22:19:02 +00:00
|
|
|
unsigned int Time = _Time*10;
|
|
|
|
unsigned int h = Time/36000;
|
|
|
|
unsigned int m = (Time % 36000)/600;
|
|
|
|
unsigned int s = (Time % 600)/10;
|
|
|
|
unsigned int d = Time % 10;
|
|
|
|
|
|
|
|
int len;
|
2011-10-01 20:58:40 +00:00
|
|
|
if (h>0)
|
2012-01-31 22:19:02 +00:00
|
|
|
len = sprintf(pStrBuffer,"%u:%02u:%02u",h,m,s);
|
2011-10-01 20:58:40 +00:00
|
|
|
else
|
2012-01-31 22:19:02 +00:00
|
|
|
len = sprintf(pStrBuffer,"%u:%02u",m,s);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
{
|
|
|
|
*pStrBuffer = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
if (ShowDecimal)
|
2012-01-31 22:19:02 +00:00
|
|
|
sprintf(&pStrBuffer[len],".%u",d);
|
2011-10-01 20:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Start replay session
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
FGReplay::start()
|
|
|
|
{
|
|
|
|
// freeze the fdm, resume from sim pause
|
|
|
|
double StartTime = get_start_time();
|
|
|
|
double EndTime = get_end_time();
|
|
|
|
fgSetDouble("/sim/replay/start-time", StartTime);
|
|
|
|
fgSetDouble("/sim/replay/end-time", EndTime);
|
|
|
|
char StrBuffer[30];
|
|
|
|
printTimeStr(StrBuffer,StartTime,false);
|
|
|
|
fgSetString("/sim/replay/start-time-str", StrBuffer);
|
|
|
|
printTimeStr(StrBuffer,EndTime,false);
|
|
|
|
fgSetString("/sim/replay/end-time-str", StrBuffer);
|
|
|
|
|
|
|
|
unsigned long buffer_elements = short_term.size()+medium_term.size()+long_term.size();
|
|
|
|
fgSetDouble("/sim/replay/buffer-size-mbyte",
|
|
|
|
buffer_elements*m_pRecorder->getRecordSize() / (1024*1024.0));
|
|
|
|
if ((fgGetBool("/sim/freeze/master"))||
|
|
|
|
(0 == replay_master->getIntValue()))
|
|
|
|
fgSetString("/sim/messages/copilot", "Replay active. 'Esc' to stop.");
|
|
|
|
fgSetBool ("/sim/freeze/master", 0);
|
|
|
|
fgSetBool ("/sim/freeze/clock", 0);
|
|
|
|
if (0 == replay_master->getIntValue())
|
|
|
|
{
|
|
|
|
replay_master->setIntValue(1);
|
|
|
|
replay_time->setDoubleValue(-1);
|
|
|
|
replay_time_str->setStringValue("");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2003-07-17 18:24:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the saved data
|
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::update( double dt )
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
int current_replay_state = last_replay_state;
|
2008-04-02 18:55:39 +00:00
|
|
|
timingInfo.clear();
|
|
|
|
stamp("begin");
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2011-03-24 22:30:09 +00:00
|
|
|
if ( disable_replay->getBoolValue() )
|
2011-03-21 22:07:05 +00:00
|
|
|
{
|
2011-10-09 17:46:49 +00:00
|
|
|
if (fgGetBool("/sim/freeze/master",false)||
|
|
|
|
fgGetBool("/sim/freeze/clock",false))
|
|
|
|
{
|
|
|
|
fgSetBool("/sim/freeze/master",false);
|
|
|
|
fgSetBool("/sim/freeze/clock",false);
|
|
|
|
last_replay_state = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if ((replay_master->getIntValue() != 3)||
|
|
|
|
(last_replay_state == 3))
|
|
|
|
{
|
|
|
|
current_replay_state = replay_master->getIntValue();
|
|
|
|
replay_master->setIntValue(0);
|
|
|
|
replay_time->setDoubleValue(0);
|
|
|
|
replay_time_str->setStringValue("");
|
|
|
|
disable_replay->setBoolValue(0);
|
|
|
|
speed_up->setDoubleValue(1.0);
|
|
|
|
speed_up->setDoubleValue(1.0);
|
|
|
|
if (fgGetBool("/sim/replay/mute",false))
|
|
|
|
{
|
|
|
|
fgSetBool("/sim/sound/enabled",true);
|
|
|
|
fgSetBool("/sim/replay/mute",false);
|
|
|
|
}
|
|
|
|
fgSetString("/sim/messages/copilot", "Replay stopped. Your controls!");
|
|
|
|
}
|
2005-08-22 17:49:50 +00:00
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
|
|
|
|
int replay_state = replay_master->getIntValue();
|
|
|
|
if ((replay_state == 0)&&
|
|
|
|
(last_replay_state > 0))
|
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
if (current_replay_state == 3)
|
|
|
|
{
|
2011-10-09 17:46:49 +00:00
|
|
|
// take control at current replay position ("My controls!").
|
2011-10-01 20:58:40 +00:00
|
|
|
// May need to uncrash the aircraft here :)
|
|
|
|
fgSetBool("/sim/crashed", false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-09 17:46:49 +00:00
|
|
|
// normal replay exit, restore most recent frame
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(DBL_MAX);
|
|
|
|
}
|
2011-10-09 17:46:49 +00:00
|
|
|
|
|
|
|
// replay is finished
|
|
|
|
last_replay_state = replay_state;
|
|
|
|
return;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
|
|
|
|
// remember recent state
|
|
|
|
last_replay_state = replay_state;
|
|
|
|
|
|
|
|
switch(replay_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// replay inactive, keep recording
|
|
|
|
break;
|
2011-10-09 17:46:49 +00:00
|
|
|
case 1: // normal replay
|
|
|
|
case 3: // prepare to resume normal flight at current replay position
|
2011-10-01 20:58:40 +00:00
|
|
|
{
|
|
|
|
// replay active
|
|
|
|
double current_time = replay_time->getDoubleValue();
|
2012-02-19 13:34:43 +00:00
|
|
|
bool ResetTime = (current_time<=0.0);
|
|
|
|
if (ResetTime)
|
2011-05-14 07:17:51 +00:00
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
// initialize start time
|
|
|
|
double startTime = get_start_time();
|
|
|
|
double endTime = get_end_time();
|
|
|
|
fgSetDouble( "/sim/replay/start-time", startTime );
|
|
|
|
fgSetDouble( "/sim/replay/end-time", endTime );
|
|
|
|
double duration = fgGetDouble( "/sim/replay/duration" );
|
|
|
|
if( duration && (duration < (endTime - startTime)) ) {
|
|
|
|
current_time = endTime - duration;
|
|
|
|
} else {
|
|
|
|
current_time = startTime;
|
2011-05-14 08:19:51 +00:00
|
|
|
}
|
2011-05-14 07:17:51 +00:00
|
|
|
}
|
2011-10-01 20:58:40 +00:00
|
|
|
bool IsFinished = replay( replay_time->getDoubleValue() );
|
|
|
|
if (IsFinished)
|
|
|
|
current_time = (replay_looped->getBoolValue()) ? -1 : get_end_time()+0.01;
|
|
|
|
else
|
|
|
|
current_time += dt * speed_up->getDoubleValue();
|
|
|
|
replay_time->setDoubleValue(current_time);
|
|
|
|
char StrBuffer[30];
|
|
|
|
printTimeStr(StrBuffer,current_time);
|
|
|
|
replay_time_str->setStringValue((const char*)StrBuffer);
|
2012-02-19 13:34:43 +00:00
|
|
|
|
|
|
|
// when time skipped (looped replay), trigger listeners to reset views etc
|
|
|
|
if (ResetTime)
|
|
|
|
replay_master->setIntValue(replay_state);
|
|
|
|
|
2011-03-21 22:07:05 +00:00
|
|
|
return; // don't record the replay session
|
2011-10-01 20:58:40 +00:00
|
|
|
}
|
|
|
|
case 2: // normal replay operation
|
2011-03-21 22:07:05 +00:00
|
|
|
return; // don't record the replay session
|
|
|
|
default:
|
|
|
|
throw sg_range_exception("unknown FGReplay state");
|
|
|
|
}
|
|
|
|
|
|
|
|
// flight recording
|
|
|
|
|
2008-04-02 18:55:39 +00:00
|
|
|
//cerr << "Recording replay" << endl;
|
2011-10-01 20:58:40 +00:00
|
|
|
sim_time += dt * speed_up->getDoubleValue();
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2003-07-18 01:51:45 +00:00
|
|
|
// sanity check, don't collect data if FDM data isn't good
|
2010-07-13 12:47:24 +00:00
|
|
|
if (!fgGetBool("/sim/fdm-initialized", false)) {
|
2003-07-18 01:51:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-03-21 22:07:05 +00:00
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
FGReplayData* r = record(sim_time);
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
|
|
|
|
return;
|
|
|
|
}
|
2003-07-17 18:24:17 +00:00
|
|
|
|
|
|
|
// update the short term list
|
2008-04-02 18:55:39 +00:00
|
|
|
//stamp("point_06");
|
2003-07-17 18:24:17 +00:00
|
|
|
short_term.push_back( r );
|
2008-04-02 18:55:39 +00:00
|
|
|
//stamp("point_07");
|
|
|
|
FGReplayData *st_front = short_term.front();
|
2011-10-01 20:58:40 +00:00
|
|
|
|
|
|
|
if (!st_front)
|
|
|
|
{
|
|
|
|
SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Inconsistent data!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sim_time - st_front->sim_time > m_high_res_time ) {
|
|
|
|
while ( sim_time - st_front->sim_time > m_high_res_time ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
st_front = short_term.front();
|
2008-04-02 18:55:39 +00:00
|
|
|
recycler.push_back(st_front);
|
2003-07-17 18:24:17 +00:00
|
|
|
short_term.pop_front();
|
|
|
|
}
|
2008-04-02 18:55:39 +00:00
|
|
|
//stamp("point_08");
|
2003-07-17 18:24:17 +00:00
|
|
|
// update the medium term list
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( sim_time - last_mt_time > m_medium_sample_rate ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
last_mt_time = sim_time;
|
2008-04-02 18:55:39 +00:00
|
|
|
st_front = short_term.front();
|
2003-07-17 18:24:17 +00:00
|
|
|
medium_term.push_back( st_front );
|
2008-04-02 18:55:39 +00:00
|
|
|
short_term.pop_front();
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2008-04-02 18:55:39 +00:00
|
|
|
FGReplayData *mt_front = medium_term.front();
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( sim_time - mt_front->sim_time > m_medium_res_time ) {
|
2008-04-02 18:55:39 +00:00
|
|
|
//stamp("point_09");
|
2011-10-01 20:58:40 +00:00
|
|
|
while ( sim_time - mt_front->sim_time > m_medium_res_time ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
mt_front = medium_term.front();
|
2008-04-02 18:55:39 +00:00
|
|
|
recycler.push_back(mt_front);
|
2003-07-17 18:24:17 +00:00
|
|
|
medium_term.pop_front();
|
|
|
|
}
|
|
|
|
// update the long term list
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( sim_time - last_lt_time > m_long_sample_rate ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
last_lt_time = sim_time;
|
2008-04-02 18:55:39 +00:00
|
|
|
mt_front = medium_term.front();
|
2003-07-17 18:24:17 +00:00
|
|
|
long_term.push_back( mt_front );
|
2008-04-02 18:55:39 +00:00
|
|
|
medium_term.pop_front();
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2008-04-02 18:55:39 +00:00
|
|
|
FGReplayData *lt_front = long_term.front();
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( sim_time - lt_front->sim_time > m_low_res_time ) {
|
2011-05-14 07:17:51 +00:00
|
|
|
//stamp("point_10");
|
2011-10-01 20:58:40 +00:00
|
|
|
while ( sim_time - lt_front->sim_time > m_low_res_time ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
lt_front = long_term.front();
|
2008-04-02 18:55:39 +00:00
|
|
|
recycler.push_back(lt_front);
|
2003-07-17 18:24:17 +00:00
|
|
|
long_term.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
cout << "short term size = " << short_term.size()
|
|
|
|
<< " time = " << sim_time - short_term.front().sim_time
|
|
|
|
<< endl;
|
|
|
|
cout << "medium term size = " << medium_term.size()
|
|
|
|
<< " time = " << sim_time - medium_term.front().sim_time
|
|
|
|
<< endl;
|
|
|
|
cout << "long term size = " << long_term.size()
|
|
|
|
<< " time = " << sim_time - long_term.front().sim_time
|
|
|
|
<< endl;
|
|
|
|
#endif
|
2008-04-02 18:55:39 +00:00
|
|
|
//stamp("point_finished");
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
FGReplayData*
|
|
|
|
FGReplay::record(double time)
|
2003-07-17 18:24:17 +00:00
|
|
|
{
|
2011-10-01 20:58:40 +00:00
|
|
|
FGReplayData* r = NULL;
|
2003-07-22 20:06:03 +00:00
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
if (recycler.size())
|
|
|
|
{
|
|
|
|
r = recycler.front();
|
|
|
|
recycler.pop_front();
|
2003-07-22 20:06:03 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
r = m_pRecorder->capture(time, r);
|
2003-07-22 20:06:03 +00:00
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
return r;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* interpolate a specific time from a specific list
|
|
|
|
*/
|
2011-10-01 20:58:40 +00:00
|
|
|
void
|
|
|
|
FGReplay::interpolate( double time, const replay_list_type &list)
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
// sanity checking
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( list.size() == 0 )
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
// handle empty list
|
|
|
|
return;
|
2011-10-01 20:58:40 +00:00
|
|
|
} else if ( list.size() == 1 )
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
// handle list size == 1
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(time, list[0]);
|
2003-07-17 18:24:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int last = list.size() - 1;
|
|
|
|
unsigned int first = 0;
|
|
|
|
unsigned int mid = ( last + first ) / 2;
|
|
|
|
|
|
|
|
bool done = false;
|
2011-10-01 20:58:40 +00:00
|
|
|
while ( !done )
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
// cout << " " << first << " <=> " << last << endl;
|
|
|
|
if ( last == first ) {
|
|
|
|
done = true;
|
2008-04-02 18:55:39 +00:00
|
|
|
} else if ( list[mid]->sim_time < time && list[mid+1]->sim_time < time ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
// too low
|
|
|
|
first = mid;
|
|
|
|
mid = ( last + first ) / 2;
|
2008-04-02 18:55:39 +00:00
|
|
|
} else if ( list[mid]->sim_time > time && list[mid+1]->sim_time > time ) {
|
2003-07-17 18:24:17 +00:00
|
|
|
// too high
|
|
|
|
last = mid;
|
|
|
|
mid = ( last + first ) / 2;
|
|
|
|
} else {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(time, list[mid+1], list[mid]);
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replay a saved frame based on time, interpolate from the two
|
|
|
|
* nearest saved frames.
|
2011-05-14 07:17:51 +00:00
|
|
|
* Returns true when replay sequence has finished, false otherwise.
|
2003-07-17 18:24:17 +00:00
|
|
|
*/
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
bool
|
|
|
|
FGReplay::replay( double time ) {
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "replay: " << time << " ";
|
2003-07-17 18:24:17 +00:00
|
|
|
// find the two frames to interpolate between
|
|
|
|
double t1, t2;
|
|
|
|
|
|
|
|
if ( short_term.size() > 0 ) {
|
2008-04-02 18:55:39 +00:00
|
|
|
t1 = short_term.back()->sim_time;
|
|
|
|
t2 = short_term.front()->sim_time;
|
2003-07-17 18:24:17 +00:00
|
|
|
if ( time > t1 ) {
|
|
|
|
// replay the most recent frame
|
2011-10-01 20:58:40 +00:00
|
|
|
replay( time, short_term.back() );
|
2011-05-14 07:17:51 +00:00
|
|
|
// replay is finished now
|
|
|
|
return true;
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "first frame" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else if ( time <= t1 && time >= t2 ) {
|
|
|
|
interpolate( time, short_term );
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "from short term" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else if ( medium_term.size() > 0 ) {
|
2008-04-02 18:55:39 +00:00
|
|
|
t1 = short_term.front()->sim_time;
|
|
|
|
t2 = medium_term.back()->sim_time;
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( time <= t1 && time >= t2 )
|
|
|
|
{
|
|
|
|
replay(time, medium_term.back(), short_term.front());
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "from short/medium term" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else {
|
2008-04-02 18:55:39 +00:00
|
|
|
t1 = medium_term.back()->sim_time;
|
|
|
|
t2 = medium_term.front()->sim_time;
|
2003-07-17 18:24:17 +00:00
|
|
|
if ( time <= t1 && time >= t2 ) {
|
|
|
|
interpolate( time, medium_term );
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "from medium term" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else if ( long_term.size() > 0 ) {
|
2008-04-02 18:55:39 +00:00
|
|
|
t1 = medium_term.front()->sim_time;
|
|
|
|
t2 = long_term.back()->sim_time;
|
2011-10-01 20:58:40 +00:00
|
|
|
if ( time <= t1 && time >= t2 )
|
|
|
|
{
|
|
|
|
replay(time, long_term.back(), medium_term.front());
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "from medium/long term" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else {
|
2008-04-02 18:55:39 +00:00
|
|
|
t1 = long_term.back()->sim_time;
|
|
|
|
t2 = long_term.front()->sim_time;
|
2003-07-17 18:24:17 +00:00
|
|
|
if ( time <= t1 && time >= t2 ) {
|
|
|
|
interpolate( time, long_term );
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "from long term" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
} else {
|
|
|
|
// replay the oldest long term frame
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(time, long_term.front());
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "oldest long term frame" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// replay the oldest medium term frame
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(time, medium_term.front());
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "oldest medium term frame" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// replay the oldest short term frame
|
2011-10-01 20:58:40 +00:00
|
|
|
replay(time, short_term.front());
|
2003-07-23 13:48:19 +00:00
|
|
|
// cout << "oldest short term frame" << endl;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// nothing to replay
|
2011-05-14 07:17:51 +00:00
|
|
|
return true;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
2011-05-14 07:17:51 +00:00
|
|
|
return false;
|
2003-07-17 18:24:17 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
/**
|
|
|
|
* given two FGReplayData elements and a time, interpolate between them
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
FGReplay::replay(double time, FGReplayData* pCurrentFrame, FGReplayData* pOldFrame)
|
|
|
|
{
|
|
|
|
m_pRecorder->replay(time,pCurrentFrame,pOldFrame);
|
|
|
|
}
|
2003-07-17 18:24:17 +00:00
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
double
|
|
|
|
FGReplay::get_start_time()
|
|
|
|
{
|
|
|
|
if ( long_term.size() > 0 )
|
|
|
|
{
|
|
|
|
return long_term.front()->sim_time;
|
|
|
|
} else if ( medium_term.size() > 0 )
|
|
|
|
{
|
|
|
|
return medium_term.front()->sim_time;
|
|
|
|
} else if ( short_term.size() )
|
|
|
|
{
|
|
|
|
return short_term.front()->sim_time;
|
|
|
|
} else
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-01 20:58:40 +00:00
|
|
|
double
|
|
|
|
FGReplay::get_end_time()
|
|
|
|
{
|
|
|
|
if ( short_term.size() )
|
|
|
|
{
|
|
|
|
return short_term.back()->sim_time;
|
|
|
|
} else
|
|
|
|
{
|
2003-07-17 18:24:17 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
}
|