Skip to content

Commit

Permalink
Avoid passing raw ptrs around in SpeakerLabelPlacement
Browse files Browse the repository at this point in the history
Also avoids calling getPrevSpk/getNextSpk twice each on each call to processSpeaker
  • Loading branch information
firthm01 committed Feb 7, 2024
1 parent 0a97de3 commit 5a6f5ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
52 changes: 23 additions & 29 deletions shared/components/speaker_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,67 +193,61 @@ void SpeakerLabelPlacement::sortSpeakers()

void SpeakerLabelPlacement::processSpeaker(int spkIndex)
{
if (tooCloseToPrev(spkIndex)) {
auto prvIndex = getPrevSpkIndex(spkIndex);
if (prvIndex >= 0 && prvIndex != spkIndex &&
tooClose(drawableSpeakers[spkIndex], drawableSpeakers[prvIndex])) {
drawableSpeakers[spkIndex].inner = true;
drawableSpeakers[spkIndex].labAz += bigAzNudge;
getPrevSpk(spkIndex)->labAz -= littleAzNudge;
drawableSpeakers[prvIndex].labAz -= littleAzNudge;
}

if (tooCloseToNext(spkIndex)) {
auto nxtIndex = getNextSpkIndex(spkIndex);
if (nxtIndex >= 0 && nxtIndex != spkIndex &&
tooClose(drawableSpeakers[spkIndex], drawableSpeakers[nxtIndex])) {
drawableSpeakers[spkIndex].inner = true;
drawableSpeakers[spkIndex].labAz -= bigAzNudge;
getNextSpk(spkIndex)->labAz += littleAzNudge;
drawableSpeakers[nxtIndex].labAz += littleAzNudge;
}
}

bool SpeakerLabelPlacement::tooCloseToNext(int spkIndex)
bool SpeakerLabelPlacement::tooClose(const SpUi& spkA, const SpUi& spkB)
{
auto spk = getNextSpk(spkIndex);
return tooClose(&drawableSpeakers[spkIndex], spk);
}

bool SpeakerLabelPlacement::tooCloseToPrev(int spkIndex)
{
auto spk = getPrevSpk(spkIndex);
return tooClose(&drawableSpeakers[spkIndex], spk);
}

bool SpeakerLabelPlacement::tooClose(const SpUi* spkA, const SpUi* spkB)
{
// Check there actually is a neighbour
if (!spkA || !spkB) return false;
// Check that the neighbour is on a separate ring
if (spkA->inner != spkB->inner) return false;
// If their labels are on seperate rings, they're not too close
if (spkA.inner != spkB.inner) return false;
// Check angular distance between speakers
return angularDistance(*spkA, *spkB) < minAzDist;
return angularDistance(spkA, spkB) < minAzDist;
}

float SpeakerLabelPlacement::angularDistance(const SpUi& spkA, const SpUi& spkB)
{
float mid = std::max(spkA.spAz, spkB.spAz);
float lower = std::min(spkA.spAz, spkB.spAz);
float upper = lower + 360.0;
float upper = lower + 360.f;
return std::min(mid - lower, upper - mid);
}

SpeakerLabelPlacement::SpUi* SpeakerLabelPlacement::getNextSpk(int fromIndex)
int SpeakerLabelPlacement::getNextSpkIndex(int fromIndex)
{
if (drawableSpeakers.size() < 2) return nullptr;
bool oob = fromIndex < 0 || fromIndex > drawableSpeakers.size();
assert(!oob);
if (oob || drawableSpeakers.size() < 2) return -1;
int index = fromIndex + 1;
const int lastIndex = static_cast<int>(drawableSpeakers.size()) - 1;
if (index > lastIndex)
index = 0;
return &drawableSpeakers[index];
return index;
}

SpeakerLabelPlacement::SpUi* SpeakerLabelPlacement::getPrevSpk(int fromIndex)
int SpeakerLabelPlacement::getPrevSpkIndex(int fromIndex)
{
if (drawableSpeakers.size() < 2) return nullptr;
bool oob = fromIndex < 0 || fromIndex > drawableSpeakers.size();
assert(!oob);
if (oob || drawableSpeakers.size() < 2) return -1;
int index = fromIndex - 1;
const int lastIndex = static_cast<int>(drawableSpeakers.size()) - 1;
if (index < 0)
index = lastIndex;
return &drawableSpeakers[index];
return index;
}

} // namespace ui
Expand Down
8 changes: 3 additions & 5 deletions shared/components/speaker_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ class SpeakerLabelPlacement {
std::vector<SpUi> drawableSpeakers;
void sortSpeakers();
void processSpeaker(int spkIndex);
bool tooCloseToNext(int spkIndex);
bool tooCloseToPrev(int spkIndex);
bool tooClose(const SpUi* spkA, const SpUi* spkB);
bool tooClose(const SpUi& spkA, const SpUi& spkB);
float angularDistance(const SpUi& spkA, const SpUi& spkB);
SpUi* getNextSpk(int fromIndex);
SpUi* getPrevSpk(int fromIndex);
int getNextSpkIndex(int fromIndex);
int getPrevSpkIndex(int fromIndex);
const float minAzDist = 15.f;
const float bigAzNudge = 10.f;
const float littleAzNudge = 5.f;
Expand Down

0 comments on commit 5a6f5ca

Please sign in to comment.