Skip to content

Commit bcb1f98

Browse files
authored
Merge pull request #3 from carbonengine/force-reclustering
Force re-clustering when the user changes the distance threshold value
2 parents 93b5967 + 77b3de1 commit bcb1f98

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

ObjectCluster/SoundEnginePlugin/KMeans.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ class KMeans {
223223
float minDistanceThreshold = 10.f,
224224
float maxDistanceThreshold = 1000.f);
225225

226+
/**
227+
* @brief Resets the KMeans instance, clearing all clusters and centroids.
228+
*/
229+
void reset();
230+
226231
/**
227232
* @brief Sets the convergence tolerance.
228233
* @param newValue The new tolerance value.

ObjectCluster/SoundEnginePlugin/Kmeans.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,15 @@ KMeans::KMeans(float tolerance, float distanceThreshold, float minDistanceThresh
342342
seed = rd();
343343
}
344344

345+
void KMeans::reset()
346+
{
347+
centroids.clear();
348+
labels.clear();
349+
clusters.clear();
350+
sse_values.clear();
351+
unassignedPoints.clear();
352+
}
353+
345354
void KMeans::setTolerance(float newValue) {
346355
m_tolerance = clamp(newValue, 0.001f, 1.0f);
347356
}

ObjectCluster/SoundEnginePlugin/ObjectClusterFX.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,21 @@ void ObjectClusterFX::Execute(
117117
UpdateClusterPositions(inObjects);
118118
}
119119

120+
void ObjectClusterFX::ForceReclustering()
121+
{
122+
m_clusters.clear();
123+
124+
auto it = m_mapInObjsToOutObjs.Begin();
125+
while (it != m_mapInObjsToOutObjs.End()) {
126+
if ((*it).pUserData && (*it).pUserData->isClustered) {
127+
(*it).pUserData->isClustered = false;
128+
}
129+
++it;
130+
}
131+
132+
m_kmeans->reset();
133+
}
134+
120135
void ObjectClusterFX::PrepareAudioObjects(const AkAudioObjects& inObjects)
121136
{
122137
FeedPositionsToKMeans(inObjects);
@@ -369,28 +384,29 @@ void ObjectClusterFX::MixToCluster(const AkAudioObject* inObject, AkAudioBuffer*
369384
AKPLATFORM::AkMemCpy(pGeneratedObject->volumeMatrix, currentVolumes, uTransmixSize);
370385
}
371386

372-
void ObjectClusterFX::FeedPositionsToKMeans(const AkAudioObjects& inObjects)
373-
{
374-
387+
void ObjectClusterFX::FeedPositionsToKMeans(const AkAudioObjects& inObjects) {
388+
// Check for threshold change before normal processing
375389
if (m_lastDistanceThreshold != m_pParams->RTPC.distanceThreshold) {
376390
m_kmeans->setDistanceThreshold(m_pParams->RTPC.distanceThreshold);
377391
m_lastDistanceThreshold = m_pParams->RTPC.distanceThreshold;
392+
ForceReclustering();
378393
}
379394

380395
std::vector<ObjectPosition> objectPositions;
381396
objectPositions.reserve(inObjects.uNumObjects);
382397

398+
// Check if objects should be clustered based on spatialization mode
383399
for (AkUInt32 i = 0; i < inObjects.uNumObjects; ++i) {
384400
AkAudioObject* inobj = inObjects.ppObjects[i];
385401

386-
// Check if this is either position-only or position+orientation
387402
bool shouldCluster = (inobj->positioning.behavioral.spatMode == AK_SpatializationMode_PositionOnly ||
388403
inobj->positioning.behavioral.spatMode == AK_SpatializationMode_PositionAndOrientation);
389404

390405
if (shouldCluster) {
391406
objectPositions.push_back({ inobj->positioning.threeD.xform.Position(), inobj->key });
392407
}
393408
}
409+
394410
// Perform clustering only if there are objects
395411
m_clusters.clear();
396412
if (!objectPositions.empty()) {

ObjectCluster/SoundEnginePlugin/ObjectClusterFX.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ class ObjectClusterFX : public AK::IAkOutOfPlaceObjectPlugin
114114
AK::IAkPluginMemAlloc* m_pAllocator;
115115
AK::IAkEffectPluginContext* m_pContext;
116116

117+
/**
118+
* @brief Forces reclustering by clearing current clusters and resetting the KMeans algorithm.
119+
*/
120+
void ForceReclustering();
121+
117122
/**
118123
* @brief Updates KMeans algorithm with input object positions
119124
* @param inObjects Input audio objects

0 commit comments

Comments
 (0)