Skip to content

Commit

Permalink
Initial implementation of filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
enneract committed May 10, 2015
1 parent 5f38999 commit 761a16a
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -892,6 +892,7 @@ set( CGAMELIST
${GAMELOGIC_DIR}/cgame/cg_rocket_dataformatter.cpp
${GAMELOGIC_DIR}/cgame/cg_gameinfo.cpp
${GAMELOGIC_DIR}/cgame/cg_parseutils.cpp
${GAMELOGIC_DIR}/cgame/Filter.cpp
${ENGINE_DIR}/client/cg_api.h
${ENGINE_DIR}/client/cg_msgdef.h
${ENGINE_DIR}/qcommon/print_translated.h
Expand Down
94 changes: 94 additions & 0 deletions src/gamelogic/cgame/Filter.cpp
@@ -0,0 +1,94 @@
/*
===========================================================================
Copyright 2015 Unvanquished Developers
This file is part of Daemon.
Daemon 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 3 of the License, or
(at your option) any later version.
Daemon 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 Daemon. If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

#include "cg_local.h"

template <class T>
Filter<T>::Filter( int a_width )
{
this->width = a_width;
}

template <class T>
void Filter<T>::Insert( T sample )
{
this->samples.remove_if(
[&]( std::pair<int,T>& sample )
{
return cg.time - sample.first > width;
}
);
this->samples.emplace( this->samples.begin( ), cg.time, sample );
}

template <class T>
void Filter<T>::Reset( )
{
this->samples.clear( );
}


template <class T>
T MAFilter<T>::Get( )
{
T total = 0;

for( auto s : this->samples )
total += s.second;

return total / this->samples.size( );
}

template <class T>
T CubicMAFilter<T>::Get( )
{
T total = 0;
float weight, total_weight = 0;

for( auto s: this->samples )
{
weight = 1.0f - (float)( cg.time - s.first ) / this->width;
weight = weight * weight * weight;
total_weight += weight;
total += s.second * weight;
}

return total / total_weight;
}

template <class T>
T GaussianMAFilter<T>::Get( )
{
T total = 0;
float weight, total_weight = 0;

for( auto s: this->samples )
{
weight = (float)( cg.time - s.first ) / this->width;
weight = exp( -8.0f * weight * weight );
total_weight += weight;
total += s.second * weight;
}

return total / total_weight;
}
43 changes: 43 additions & 0 deletions src/gamelogic/cgame/cg_local.h
Expand Up @@ -2375,5 +2375,48 @@ float CG_Rocket_ProgressBarValueByName( const char *name );
// cg_gameinfo.c
//
void CG_LoadArenas();

//
// Filter.cpp
//

template <class T>
class Filter
{
protected:
std::list<std::pair<int,T> > samples;
int width;

public:
Filter( int a_width );
void Insert( T sample );
void Reset( );
virtual T Get( ) = 0;
};

template <class T>
class MAFilter: public Filter<T>
{
public:
using Filter<T>::Filter;
T Get( );
};

template <class T>
class CubicMAFilter: public Filter<T>
{
public:
using Filter<T>::Filter;
T Get( );
};

template <class T>
class GaussianMAFilter: public Filter<T>
{
public:
using Filter<T>::Filter;
T Get( );
};

#endif

0 comments on commit 761a16a

Please sign in to comment.