Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The #GreatAccidentalsReform #21042

Merged
merged 4 commits into from
May 24, 2024
Merged

Conversation

mike-spa
Copy link
Contributor

@mike-spa mike-spa commented Jan 16, 2024

Resolves: #9302

A complete rewrite of the accidental placement algorithms, making good use of the recently introduced accidental cutouts. Basically see the vtests.

@MarcSabatella
Copy link
Contributor

MarcSabatella commented Jan 16, 2024

Wow, quite the project! I look forward to checking out the results. I wanted to find my old test file based on Daniel Spreadbury's blog article https://blog.dorico.com/2014/03/development-diary-part-six, but unfortunately I can't seem to find the MSCZ file itself. I did find the results of one of my other tests from back then, and I'll be curious to compare:

image

@mike-spa mike-spa marked this pull request as draft January 17, 2024 08:26
@mike-spa
Copy link
Contributor Author

Disclaimer: don't get overexcited over any particular details. This is just me not resisting the urge to use the new cutouts and trying out stuff.

@Tantacrul
Copy link
Contributor

LOVING THIS

@bkunda
Copy link

bkunda commented Jan 18, 2024

Fabulous progress @mike-spa!

@mike-spa mike-spa marked this pull request as ready for review April 2, 2024 10:30
@musescore musescore deleted a comment from sammik Apr 2, 2024
@mike-spa mike-spa requested a review from oktophonie April 2, 2024 10:44
@oktophonie oktophonie added the vtests This PR produces approved changes to vtest results label Apr 3, 2024
@mike-spa mike-spa force-pushed the accidentalsReform branch 2 times, most recently from 642a240 to 5777d2e Compare April 16, 2024 07:30
@musescore musescore deleted a comment from Jojo-Schmitz Apr 16, 2024
@mike-spa mike-spa force-pushed the accidentalsReform branch 2 times, most recently from d5c2f0e to ec94049 Compare May 15, 2024 07:55

m_spatium = accidental->spatium();

m_verticalAccidentalToAccidentalClearance = 0.15 * m_spatium;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still actual

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how else should I write this? I'm just initializing constants that I will need later. Some of these constants have a style, others are hardcoded. Problem is I can't hardcode these as const or constexpr because they need to be constructed from other information when constructing AccidentalsLayoutContext data

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should specify (in a comment) how you found / why exactly you are using these constants

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This 0.15 (and other magic numbers) is used in a lot of different places in this PR. Worth adding a named constexpr

Copy link
Contributor Author

@mike-spa mike-spa May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed all the magic numbers I had used around the file and collected them all here with proper names and a comment that explains that they are just engraving parameters that don't have a style. The fact that some of them have the same value (like 0.15) is just a coincidence, they actually all mean different things.

@mike-spa mike-spa force-pushed the accidentalsReform branch 2 times, most recently from ecee97a to e1b8bb6 Compare May 17, 2024 11:20
@mike-spa
Copy link
Contributor Author

@RomanPudashkin done! 👍

{
std::vector<Accidental*> accidentals;

for (Chord* chord : chords) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use const with pointers unless you are going to change their value

if (!acc) {
continue;
}
acc->setPos(0.0, 0.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not ideal... This function not only collects accidentals but also changes some properties. Ideally, it should have only one responsibility (find and return accidentals)

verticallyAlignAccidentals(ctx);
}

void AccidentalsLayout::findOctaves(AccidentalsLayoutContext& ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this code will be called quite often, it is probably better to combine this function with findSeconds. This way, we will reduce the complexity of the algorithm

firstGroup.push_back(ctx.allAccidentals.front());
ctx.accidentalSubChords.push_back(firstGroup);

for (int i = 1; i < ctx.allAccidentals.size(); ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will generate a warning as size() returns size_t

TLayout::layoutAccidental(acc, acc->mutldata(), ctx.conf());
}

AccidentalsLayoutContext accidentalsLayoutContext(allAccidentals, chords);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use std::move with allAccidentals to avoid creating a copy (the AccidentalsLayoutContext constructor also needs to be adjusted)

void AccidentalsLayout::mergeSubGroupsWithOctavesAcross(AccidentalsLayoutContext& ctx)
{
bool foundOctave = false;
do {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what's going on in this function, but it's very complex (5 nested loops + lots of operations on std::vector). Is there any way to simplify/optimize it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved out some code to make it more readable, but unfortunately I can't think of any way to simplify this. These are very gnarly engraving requirements resulting in very gnarly code logic. Luckily this function (and the next one that you pointed out) is used rarely so it shouldn't impact performance

applyOrderingOffsets(accidentals);
}

std::vector<std::vector<Accidental*> > AccidentalsLayout::splitIntoPriorityGroups
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to add an alias for this type

void AccidentalsLayout::moveOctavesToSecondGroup(std::vector<std::vector<Accidental*> >& subGroups)
{
std::vector<Accidental*>& firstGroup = subGroups[0];
std::vector<Accidental*>& secondGroup = subGroups[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like std::array might be a better type for subGroups

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one particular case (with conditions defined by the caller), but in general subGroups may have different sizes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding this

IF_ASSERT_FAILED(subGroups.size() == 2) {
return;
}

return 0.0;
}

double AccidentalsLayout::computePadding(Accidental* acc, const EngravingItem* chordElement, AccidentalsLayoutContext& ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const AccidentalsLayoutContext&

AccidentalType accType = acc->accidentalType();
if (chordItem->isLedgerLine() && (accType == AccidentalType::NATURAL || accType == AccidentalType::SHARP)) {
if (chordItem->y() > accShape.top() && chordItem->y() < accShape.bottom()) {
return accShape.right() - chordElement.left() + 0.1 * ctx.spatium();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.1?

for (int j = 0; j < thisGroup.size(); ++j) {
Accidental* acc1 = thisGroup[j];
for (Accidental* secondAcc : acc1->ldata()->seconds.value()) {
if (muse::remove(nextGroup, secondAcc)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question. remove() isn't very efficient with std::vector

@mike-spa mike-spa force-pushed the accidentalsReform branch 2 times, most recently from adc9a8a to 6e0f341 Compare May 22, 2024 09:33
@mike-spa
Copy link
Contributor Author

@RomanPudashkin done! 👍

TLayout::layoutAccidental(acc, acc->mutldata(), ctx.conf());
}

AccidentalsLayoutContext accidentalsLayoutContext(std::move(allAccidentals), std::move(chords));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move(chords) - this has no effect as chords is const std::vector<Chord*>& :)

return subGroups;
}

AccidentalGroups AccidentalsLayout::groupAccidentalsByNoteXPos(std::vector<Accidental*>& accidentals)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This vector should be const

@RomanPudashkin RomanPudashkin merged commit 6644d91 into musescore:master May 24, 2024
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vtests This PR produces approved changes to vtest results
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MU4 Issue] Sharps are not laid out correctly when notes are a 6th apart
6 participants