VABinauralClusteringState.cpp 2.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#define NOMINMAX

#include <algorithm> 

// VA
#include <VAObjectPool.h>

// Utils
#include "VABinauralClusteringState.h"
#include "./VABinauralClusterPoolFactory.h"


13 14 15
VABinauralClusteringState::VABinauralClusteringState(int numClusters, VABinauralListener* listener) :
	numClusters(numClusters),
	listener(listener)
16 17 18 19 20 21 22 23 24 25 26 27 28 29
{
	for (int i = numClusters - 1; i >= 0; --i)
	{
		freeClusterIDs.push(i);
	}
}


VABinauralClusteringState::~VABinauralClusteringState()
{
}

VABinauralClusteringState::VABinauralClusteringState(const VABinauralClusteringState& state) : 
	numClusters(state.numClusters),
30
	listener(state.listener),
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	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;
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
68
		p = createCluster(sourceID, source);
69 70 71 72 73

		nearestClusterID = p.first;
		nearestCluster = p.second;
	}

74
	nearestCluster->addSource(sourceID, source, minerr);
75 76 77 78
	sourceClusterReference.insert(std::pair< int, int >(sourceID, nearestClusterID));
}

std::pair< int, VABinauralCluster*>
Lucas Moesch's avatar
WIP  
Lucas Moesch committed
79
VABinauralClusteringState::createCluster(int sourceID,  VABinauralSoundSource* source)
80 81 82 83
{
	int clusterID = freeClusterIDs.back();
	VABinauralCluster* cluster = dynamic_cast< VABinauralCluster* >(_clusterPool->RequestObject()); // Reference = 1

84
	cluster->init(sourceID, source, listener);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	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;
}