[Positioner] Positioning::filter() (refactoring)

Refactor the filtering code into a separate function,
Positioning::filter().
master
Matteo Cypriani 10 years ago
parent f35215388b
commit 7e3b41a46f

@ -18,7 +18,10 @@
#include "frbhmbasic.hh"
#include "interlinknetworks.hh"
#include "nss.hh"
#include "request.hh"
#include "result.hh"
#include "resultlist.hh"
#include "timestamp.hh"
#include "configuration.hh"
#include "posexcept.hh"
#include "stock.hh"
@ -128,17 +131,12 @@ void Positioning::loop()
{
vector<PositioningAlgorithm*>::const_iterator algo ;
float filter_max_speed =
Configuration::float_value("positioning.filter.max-speed") ;
while (! input.eof() && owl_run)
{
const Request &request = input.get_next_request() ;
if (! request)
continue ;
const Mobile *const mobile = request.get_mobile() ;
Point3D real_position ;
bool compute_error = false ;
ResultList results(&request) ;
@ -147,7 +145,6 @@ void Positioning::loop()
const string &algo_name = (*algo)->get_name() ;
Result res ;
try
{
res = (*algo)->compute(request) ;
@ -161,43 +158,8 @@ void Positioning::loop()
continue ;
}
// Apply filter if the filter is enabled and that it is not the
// first result we compute for this mobile
if (filter_max_speed > 0
&& ! mobile->get_last_results().empty()
&& algo_name != "Real") // don't filter the real position
{
const ResultList &last_results = mobile->get_last_results() ;
const Result &prev_res =
last_results.get_result_for_algo(algo_name) ;
// Calculate the travel time (duration)
const Timestamp &timestamp =
res.get_request()->get_time_sent() ;
const Timestamp &prev_timestamp =
last_results.get_request()->get_time_sent() ;
Timestamp duration = prev_timestamp.elapsed(timestamp) ;
// Get the current and previous coordinates
const Point3D &pos = res.get_position() ;
const Point3D &prev_pos = prev_res.get_position() ;
// Recompute the result by interpolation
Point3D filtered_pos(
prev_pos.interpolate(pos, filter_max_speed, duration)) ;
if (filtered_pos != pos)
{
if (Configuration::is_configured("verbose"))
cerr
<< "Filtering (algorithm " << algo_name << "): "
<< "for request sent at " << timestamp
<< " by mobile " << mobile->get_mac_addr()
<< ", " << pos
<< " was replaced by " << filtered_pos
<< ".\n" ;
res.set_position(filtered_pos) ;
}
}
// Apply filtering if needed
filter(request, res) ;
if (compute_error)
res.compute_error(real_position) ;
@ -212,9 +174,68 @@ void Positioning::loop()
// If filtering is enabled, update the last known position(s) of
// the current mobile
if (filter_max_speed > 0)
const_cast<Mobile*>(mobile)->set_last_results(results) ;
bool do_filter =
Configuration::float_value("positioning.filter.max-speed") > 0 ;
if (do_filter)
{
const Mobile *const mobile = request.get_mobile() ;
const_cast<Mobile*>(mobile)->set_last_results(results) ;
}
output.write(results) ;
}
}
/**
* @param[in] request The request from which `result` has been computed.
* @param[in,out] result The unfiltered Result, that will be updated by
* this function if needed.
*/
void Positioning::filter(const Request &request, Result &result)
{
// Don't apply filter if filtering is disabled
float max_speed =
Configuration::float_value("positioning.filter.max-speed") ;
if (max_speed <= 0)
return ;
// Don't apply filter if it is the first result we compute for this
// mobile (hence we don't have previous results to compare to)
const Mobile *const mobile = request.get_mobile() ;
const ResultList &last_results = mobile->get_last_results() ;
if(last_results.empty())
return ;
// Don't apply filter if the "algorithm" is the real position
string algo_name = result.get_algorithm() ;
if (algo_name == "Real")
return ;
const Result &prev_result = last_results.get_result_for_algo(algo_name) ;
// Calculate the travel time (duration)
const Timestamp &timestamp = result.get_request()->get_time_sent() ;
const Timestamp &prev_timestamp =
last_results.get_request()->get_time_sent() ;
Timestamp duration = prev_timestamp.elapsed(timestamp) ;
// Get the current and previous coordinates
const Point3D &pos = result.get_position() ;
const Point3D &prev_pos = prev_result.get_position() ;
// Recompute the result by interpolation
Point3D filtered_pos(prev_pos.interpolate(pos, max_speed, duration)) ;
if (filtered_pos != pos)
{
if (Configuration::is_configured("verbose"))
cerr
<< "Filtering (algorithm " << algo_name << "): "
<< "for request sent at " << timestamp
<< " by mobile " << mobile->get_mac_addr()
<< ", " << pos
<< " was replaced by " << filtered_pos
<< ".\n" ;
result.set_position(filtered_pos) ;
}
}

@ -16,6 +16,8 @@
#define _OWLPS_POSITIONING_POSITIONING_HH_
class PositioningAlgorithm ;
class Request ;
class Result ;
#include "input.hh"
#include "output.hh"
@ -33,6 +35,8 @@ protected:
void initialise_algorithms(void) ;
void loop(void) ;
/// Sets a new filtered position in `result`
void filter(const Request &request, Result &result) ;
public:
Positioning(void) ;

Loading…
Cancel
Save