Commit 80371cda authored by Lucas Moesch's avatar Lucas Moesch

Initial clustering setup for real time renddering added.

parent 92715358
#include "VABinauralCluster.h"
VABinauralCluster::VABinauralCluster()
{
}
VABinauralCluster::VABinauralCluster(const VABinauralCluster& cluster) :
maxError(cluster.maxError),
_numSources(cluster._numSources),
_clusterSourcePos(cluster._clusterSourcePos)
{
}
VABinauralCluster::~VABinauralCluster()
{
}
void
VABinauralCluster::init(VABinauralSoundSource* source)
{
_clusterSourcePos = _clusterSourcePos + source->predPos;
maxError = getDistError(source);
++_numSources;
}
double
VABinauralCluster::getDistError(VABinauralSoundSource* source)
{
double dotp = _clusterSourcePos.Dot(source->predPos);
return ((dotp * dotp) / (_clusterSourcePos.Dot(_clusterSourcePos)) * (source->predPos.Dot(source->predPos)));
}
void
VABinauralCluster::addSource(VABinauralSoundSource* source)
{
double err = getDistError(source);
_clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1);
maxError = std::max(err, maxError);
++_numSources;
}
void
VABinauralCluster::addSource(VABinauralSoundSource* source, double error)
{
_clusterSourcePos = (source->predPos + _clusterSourcePos * _numSources) / (_numSources + 1);
maxError = std::max(error, maxError);
++_numSources;
}
#ifndef IW_VACORE_BINAURALCLUSTER
#define IW_VACORE_BINAURALCLUSTER
// VA Includes
#include <VA.h>
// Utils
#include "../BinauralSoundSource/VABinauralSoundSource.h"
class VABinauralCluster : public CVAPoolObject
{
public:
double maxError;
VABinauralCluster();
VABinauralCluster(const VABinauralCluster& cluster);
~VABinauralCluster();
void
init(VABinauralSoundSource* source);
double
getDistError(VABinauralSoundSource* source);
void
addSource(VABinauralSoundSource* source);
void
addSource(VABinauralSoundSource* source, double error);
void
removeSource();
void
reset();
private:
int _numSources;
VAVec3 _clusterSourcePos;
double
cummulativeMovingAverage(int n, double average, double value);
};
#endif // IW_VACORE_BINAURALCLUSTER
\ No newline at end of file
#include "VABinauralClusterEngine.h"
// VA includes
#include <VAObjectPool.h>
VABinauralClusterEngine::VABinauralClusterEngine()
{
}
VABinauralClusterEngine::~VABinauralClusterEngine()
{
}
void
VABinauralClusterEngine::update()
{
std::map< int, VABinauralClustering* >::iterator it;
for (it = _clusterings.begin(); it != _clusterings.end(); ++it)
{
it->second->update();
}
}
void
VABinauralClusterEngine::addSource( int sourceID, VABinauralSoundSource* source )
{
std::map< int, VABinauralClustering* >::iterator it;
for (it = _clusterings.begin(); it != _clusterings.end(); ++it)
{
it->second->addSource(sourceID, source);
}
}
void
VABinauralClusterEngine::addListener(int listenerID, VABinauralListener* listener, clusterConfig_t& conf)
{
VABinauralClustering* clustering = dynamic_cast< VABinauralClustering* >(_clusteringPool->RequestObject()); // Reference = 1
clustering->init(listenerID, listener, conf.numCluster);
// add local reference
_clusterings.insert(std::pair< int, VABinauralClustering* >(listenerID, clustering));
}
void
VABinauralClusterEngine::removeListener(int listenerID)
{
std::map< int, VABinauralClustering* >::iterator it = _clusterings.find(listenerID);
VABinauralClustering* listener = it->second;
_clusterings.erase(it);
/*
TODO: foreach cluster in CL delete cluster
*/
}
#ifndef IW_VACORE_BINAURALCLUSTERENGINE
#define IW_VACORE_BINAURALCLUSTERENGINE
// VA includes
#include <VA.h>
// ITA includes
// Utils
#include "../BinauralListener/VABinauralListener.h"
#include "../BinauralSoundSource/VABinauralSoundSource.h"
#include "./VABinauralClustering.h"
#include "./VABinauralCluster.h"
class VABinauralClusterEngine
{
public:
struct clusterConfig_t {
int numCluster;
};
~VABinauralClusterEngine();
VABinauralClusterEngine();
void
update();
void
addSource( int sourceID, VABinauralSoundSource* source );
void
addListener( int listenerID, VABinauralListener* listener,clusterConfig_t& conf );
void
removeListener( int listenerID );
void
removeSource( int sourceID );
private:
std::map< int, VABinauralSoundSource* > _newSources;
std::map< int, VABinauralClustering* > _clusterings;
IVAObjectPool* _sourcePool;
IVAObjectPool* _clusteringPool;
//std::map< int, VABinauralSoundSource* > _sources;
//std::map< int, VABinauralListener* > _listener;
};
#endif // IW_VACORE_BINAURALCLUSTERENGINE
#include "VABinauralClusterPoolFactory.h"
// Utils
#include "./VABinauralCluster.h"
VABinauralClusterPoolFactory::VABinauralClusterPoolFactory()
{
}
VABinauralClusterPoolFactory::~VABinauralClusterPoolFactory()
{
}
CVAPoolObject*
VABinauralClusterPoolFactory::CreatePoolObject()
{
return new VABinauralCluster();
};
\ No newline at end of file
#ifndef IW_VACORE_BINAURALCLUSTERPOOLFACTORY
#define IW_VACORE_BINAURALCLUSTERPOOLFACTORY
// VA Includes
#include <VA.h>
#include <VAPoolObject.h>
class VABinauralClusterPoolFactory : public IVAPoolObjectFactory
{
public:
VABinauralClusterPoolFactory();
~VABinauralClusterPoolFactory();
CVAPoolObject* CreatePoolObject();
};
#endif // IW_VACORE_BINAURALCLUSTERPOOLFACTORY
#define NOMINMAX
#include <limits>
#include <algorithm>
#include <math.h>
// VA
#include <VAObjectPool.h>
// Utils
#include "./VABinauralClustering.h"
#include "./VABinauralClusterPoolFactory.h"
VABinauralClustering::~VABinauralClustering()
{
IVAPoolObjectFactory* clusterFactory = new VABinauralClusterPoolFactory();
_clusterPool = IVAObjectPool::Create(16, 2, clusterFactory, true);
};
VABinauralClustering::VABinauralClustering(){};
void
VABinauralClustering::addSource(int sourceID, VABinauralSoundSource* source)
{
_unassignedSources.insert(std::pair< int, VABinauralSoundSource* >(sourceID, source));
};
void
VABinauralClustering::init(int listenerID, VABinauralListener* listener, int numClusters)
{
_listenerID = listenerID;
_listener = listener;
_numClusters = numClusters;
_threshold = cos(180. / numClusters) * cos(180. / numClusters);
}
void
VABinauralClustering::update()
{
VABinauralClusteringState* state = new VABinauralClusteringState(*_clustering);
// update unassigned sources
std::map< int , VABinauralSoundSource* >::iterator it;
for (it = _unassignedSources.begin(); it != _unassignedSources.end(); ++it)
{
state->addSource(it->first, it->second, _threshold, 0);
}
// TODO: refinement
for (it = _unassignedSources.begin(); it != _unassignedSources.end(); ++it)
{
_assignedSources.insert(std::pair< int, VABinauralSoundSource* >(it->first, it->second));
}
_unassignedSources.clear();
// TODO: update fixed clustertrajectories
}
void
VABinauralClustering::PreRequest(){};
void
VABinauralClustering::PreRelease(){};
\ No newline at end of file
#ifndef IW_VACORE_BINAURALCLUSTERING
#define IW_VACORE_BINAURALCLUSTERING
#include <queue>
// VA Includes
#include <VA.h>
#include <VAPoolObject.h>
// Utils
#include "VABinauralCluster.h"
#include "VABinauralClusteringState.h"
#include "../BinauralListener/VABinauralListener.h"
#include "../BinauralSoundSource/VABinauralSoundSource.h"
class VABinauralClustering : public CVAPoolObject
{
public:
~VABinauralClustering();
VABinauralClustering();
void
init(int listenerID, VABinauralListener* listener, int numClusters);
void
update();
void
addSource(int sourceID, VABinauralSoundSource* source);
private:
int _listenerID;
int _numClusters;
double _threshold;
VABinauralClusteringState* _clustering;
VABinauralListener* _listener;
IVAObjectPool* _clusterPool;
std::map< int, VABinauralSoundSource* > _unassignedSources;
std::map< int, VABinauralSoundSource* > _assignedSources;
std::pair< int, VABinauralCluster* >
assignCluster(VABinauralClusteringState* clustering, VABinauralSoundSource* source, double threshold, int numFreeClusters);
void
PreRequest();
void
PreRelease();
};
#endif // IW_VACORE_BINAURALCLUSTERING
#include "VABinauralClusteringPoolFactory.h"
#include "VABinauralClustering.h"
VABinauralClusteringPoolFactory::VABinauralClusteringPoolFactory()
{
}
VABinauralClusteringPoolFactory::~VABinauralClusteringPoolFactory()
{
}
CVAPoolObject*
VABinauralClusteringPoolFactory::CreatePoolObject()
{
return new VABinauralClustering();
};
\ No newline at end of file
#ifndef IW_VACORE_BINAURALCLUSTERINGPOOLFACTORY
#define IW_VACORE_BINAURALCLUSTERINGPOOLFACTORY
// VA Includes
#include <VA.h>
#include <VAPoolObject.h>
class VABinauralClusteringPoolFactory : public IVAPoolObjectFactory
{
public:
VABinauralClusteringPoolFactory();
~VABinauralClusteringPoolFactory();
CVAPoolObject*
CreatePoolObject();
};
#endif // IW_VACORE_BINAURALCLUSTERINGPOOLFACTORY
\ No newline at end of file
#define NOMINMAX
#include <algorithm>
// VA
#include <VAObjectPool.h>
// Utils
#include "VABinauralClusteringState.h"
#include "./VABinauralClusterPoolFactory.h"
VABinauralClusteringState::VABinauralClusteringState(int numClusters)
{
for (int i = numClusters - 1; i >= 0; --i)
{
freeClusterIDs.push(i);
}
}
VABinauralClusteringState::~VABinauralClusteringState()
{
}
VABinauralClusteringState::VABinauralClusteringState(const VABinauralClusteringState& state) :
numClusters(state.numClusters),
sourceClusterReference(state.sourceClusterReference)
{
std::map< int, VABinauralCluster* >::const_iterator it;
for (it = state.clusters.begin(); it != state.clusters.end(); ++it)
{
int clusterID = it->first;
VABinauralCluster* cluster = new VABinauralCluster(*(it->second));
clusters.insert(std::pair< int, VABinauralCluster* >(clusterID, cluster));
}
}
void
VABinauralClusteringState::addSource(int sourceID, VABinauralSoundSource* source, double threshold, int numBlockedClusters)
{
int numFreeClusters = numClusters - numBlockedClusters - clusters.size();
double err = 0, minerr = std::numeric_limits<double>::infinity();
int nearestClusterID = -1;
VABinauralCluster* nearestCluster = nullptr;
std::map< int, VABinauralCluster* >::iterator it;
for (it = clusters.begin(); it != clusters.end(); ++it)
{
err = it->second->getDistError(source);
if (err < minerr)
{
minerr = err;
nearestClusterID = it->first;
nearestCluster = it->second;
}
}
if ((minerr > threshold) && (numFreeClusters > 0))
{
std::pair< int, VABinauralCluster*> p;
p = createCluster(source);
nearestClusterID = p.first;
nearestCluster = p.second;
}
nearestCluster->addSource(source, minerr);
sourceClusterReference.insert(std::pair< int, int >(sourceID, nearestClusterID));
}
std::pair< int, VABinauralCluster*>
VABinauralClusteringState::createCluster(VABinauralSoundSource* source)
{
int clusterID = freeClusterIDs.back();
VABinauralCluster* cluster = dynamic_cast< VABinauralCluster* >(_clusterPool->RequestObject()); // Reference = 1
cluster->init(source);
clusters.insert(std::pair< int, VABinauralCluster* >(clusterID, cluster));
freeClusterIDs.pop();
return std::pair< int, VABinauralCluster* >(clusterID, cluster);
}
double
VABinauralClusteringState::getMaxError()
{
double max = 0;
std::map< int, VABinauralCluster* >::iterator it;
for (it = clusters.begin(); it != clusters.end(); ++it)
{
max = std::max(max, it->second->maxError);
}
return max;
}
#ifndef IW_VACORE_BINAURALCLUSTERINGSTATE
#define IW_VACORE_BINAURALCLUSTERINGSTATE
#include <queue>
// VA Includes
#include <VA.h>
#include <VAPoolObject.h>
// Utils
#include "VABinauralCluster.h"
#include "../BinauralSoundSource/VABinauralSoundSource.h"
class VABinauralClusteringState
{
public:
int numClusters;
std::queue< int > freeClusterIDs;
std::map< int, int> sourceClusterReference;
std::map< int, VABinauralCluster* > clusters;
VABinauralClusteringState(int numClusters);
VABinauralClusteringState(const VABinauralClusteringState& state);
~VABinauralClusteringState();
void
addSource(int sourceID, VABinauralSoundSource* source, double threshold, int numBlockedClusters);
std::pair< int, VABinauralCluster*>
createCluster(VABinauralSoundSource* source);
double getMaxError();
private:
IVAObjectPool* _clusterPool;
};
#endif // IW_VACORE_BINAURALCLUSTERINGSTATE
\ No newline at end of file
# $Id:$
set( RelativeDir "src/Rendering/Binaural/RealTime/Utils/BinauralClusterEngine" )
set( RelativeSourceGroup "Source Files\\Rendering\\Binaural\\Realtime\\Utils\\BinauralClusterEngine" )
set( DirFiles
VABinauralClusterEngine.cpp
VABinauralClustering.cpp
VABinauralCluster.cpp
VABinauralClusterEngine.h
VABinauralClustering.h
VABinauralCluster.h
VABinauralClusteringState.h
VABinauralClusteringState.cpp
VABinauralClusterPoolFactory.cpp
VABinauralClusterPoolFactory.h
VABinauralClusteringPoolFactory.cpp
VABinauralClusteringPoolFactory.h
_SourceFiles.cmake
)
set( DirFiles_SourceGroup "${RelativeSourceGroup}" )
set( LocalSourceGroupFiles )
foreach( File ${DirFiles} )
list( APPEND LocalSourceGroupFiles "${RelativeDir}/${File}" )