VABinauralClusteringState.cpp 2.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#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;
}