Skip to content

Commit

Permalink
Integrate MuscleColorSourceScaling into OpenSimDecorationOptions (#933)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Oct 28, 2024
1 parent 4eb2233 commit a872b84
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/OpenSimCreator/Graphics/CustomRenderingOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void osc::CustomRenderingOptions::tryUpdFromValues(std::string_view keyPrefix, c
for (const auto& metadata : GetAllCustomRenderingOptionFlagsMetadata()) {

std::string key = std::string{keyPrefix} + metadata.id;
if (const auto* v = lookup_or_nullptr(lut, key); v->type() == VariantType::Bool) {
if (const auto* v = lookup_or_nullptr(lut, key); v and v->type() == VariantType::Bool) {
SetOption(m_Flags, metadata.value, to<bool>(*v));
}
}
Expand Down
30 changes: 25 additions & 5 deletions src/OpenSimCreator/Graphics/OpenSimDecorationOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ osc::OpenSimDecorationOptions::OpenSimDecorationOptions() :
m_MuscleDecorationStyle{MuscleDecorationStyle::Default},
m_MuscleColorSource{MuscleColorSource::Default},
m_MuscleSizingStyle{MuscleSizingStyle::Default},
m_MuscleColourSourceScaling{MuscleColorSourceScaling::Default},
m_Flags{OpenSimDecorationOptionFlags::Default}
{}

Expand Down Expand Up @@ -50,6 +51,16 @@ void osc::OpenSimDecorationOptions::setMuscleSizingStyle(MuscleSizingStyle s)
m_MuscleSizingStyle = s;
}

MuscleColorSourceScaling osc::OpenSimDecorationOptions::getMuscleColorSourceScaling() const
{
return m_MuscleColourSourceScaling;
}

void osc::OpenSimDecorationOptions::setMuscleColorSourceScaling(MuscleColorSourceScaling s)
{
m_MuscleColourSourceScaling = s;
}

size_t osc::OpenSimDecorationOptions::getNumOptions() const
{
return num_flags<OpenSimDecorationOptionFlags>();
Expand Down Expand Up @@ -190,6 +201,7 @@ void osc::OpenSimDecorationOptions::forEachOptionAsAppSettingValue(const std::fu
callback("muscle_decoration_style", GetMuscleDecorationStyleMetadata(m_MuscleDecorationStyle).id);
callback("muscle_coloring_style", GetMuscleColoringStyleMetadata(m_MuscleColorSource).id);
callback("muscle_sizing_style", GetMuscleSizingStyleMetadata(m_MuscleSizingStyle).id);
callback("muscle_color_scaling", GetMuscleColorSourceScalingMetadata(m_MuscleColourSourceScaling).id);
for (size_t i = 0; i < num_flags<OpenSimDecorationOptionFlags>(); ++i) {
const auto& meta = GetIthOptionMetadata(i);
callback(meta.id, static_cast<bool>(m_Flags & GetIthOption(i)));
Expand All @@ -212,7 +224,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues(
return lookup_or_nullptr(lut, buf);
};

if (auto* appVal = lookup("muscle_decoration_style"); appVal->type() == VariantType::String)
if (auto* appVal = lookup("muscle_decoration_style"); appVal and appVal->type() == VariantType::String)
{
const auto metadata = GetAllMuscleDecorationStyleMetadata();
const auto it = rgs::find(metadata, to<std::string>(*appVal), [](const auto& m) { return m.id; });
Expand All @@ -221,7 +233,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues(
}
}

if (auto* appVal = lookup("muscle_coloring_style"); appVal->type() == VariantType::String)
if (auto* appVal = lookup("muscle_coloring_style"); appVal and appVal->type() == VariantType::String)
{
const auto metadata = GetAllPossibleMuscleColoringSourcesMetadata();
const auto it = rgs::find(metadata, to<std::string>(*appVal), [](const auto& m) { return m.id; });
Expand All @@ -230,7 +242,7 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues(
}
}

if (auto* appVal = lookup("muscle_sizing_style"); appVal->type() == VariantType::String)
if (auto* appVal = lookup("muscle_sizing_style"); appVal and appVal->type() == VariantType::String)
{
const auto metadata = GetAllMuscleSizingStyleMetadata();
const auto it = rgs::find(metadata, to<std::string>(*appVal), [](const auto& m) { return m.id; });
Expand All @@ -239,10 +251,18 @@ void osc::OpenSimDecorationOptions::tryUpdFromValues(
}
}

if (auto* appVal = lookup("muscle_color_scaling"); appVal and appVal->type() == VariantType::String)
{
const auto metadata = GetAllPossibleMuscleColorSourceScalingMetadata();
const auto it = rgs::find(metadata, to<std::string>(*appVal), [](const auto& m) { return m.id; });
if (it != metadata.end()) {
m_MuscleColourSourceScaling = it->value;
}
}

for (size_t i = 0; i < num_flags<OpenSimDecorationOptionFlags>(); ++i) {
const auto& metadata = GetIthOptionMetadata(i);
if (auto* appVal = lookup(metadata.id); appVal->type() == VariantType::Bool)
{
if (auto* appVal = lookup(metadata.id); appVal and appVal->type() == VariantType::Bool) {
SetOption(m_Flags, GetIthOption(i), to<bool>(*appVal));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/OpenSimCreator/Graphics/OpenSimDecorationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <OpenSimCreator/Graphics/MuscleColorSource.h>
#include <OpenSimCreator/Graphics/MuscleDecorationStyle.h>
#include <OpenSimCreator/Graphics/MuscleSizingStyle.h>
#include <OpenSimCreator/Graphics/MuscleColorSourceScaling.h>
#include <OpenSimCreator/Graphics/OpenSimDecorationOptionFlags.h>

#include <oscar/Utils/CStringView.h>
Expand Down Expand Up @@ -32,6 +33,9 @@ namespace osc
MuscleSizingStyle getMuscleSizingStyle() const;
void setMuscleSizingStyle(MuscleSizingStyle);

MuscleColorSourceScaling getMuscleColorSourceScaling() const;
void setMuscleColorSourceScaling(MuscleColorSourceScaling);

// the ones below here are toggle-able options with user-facing strings etc
size_t getNumOptions() const;
bool getOptionValue(ptrdiff_t) const;
Expand Down Expand Up @@ -81,6 +85,7 @@ namespace osc
MuscleDecorationStyle m_MuscleDecorationStyle;
MuscleColorSource m_MuscleColorSource;
MuscleSizingStyle m_MuscleSizingStyle;
MuscleColorSourceScaling m_MuscleColourSourceScaling;
OpenSimDecorationOptionFlags m_Flags;
};
}
1 change: 1 addition & 0 deletions tests/TestOpenSimCreator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_executable(TestOpenSimCreator
Graphics/TestMuscleColorSourceScaling.cpp
Graphics/TestOpenSimDecorationGenerator.cpp
Graphics/TestSimTKDecorationGenerator.cpp
Graphics/TestOpenSimDecorationOptions.cpp
MetaTests/TestOpenSimLibraryAPI.cpp
Platform/TestRecentFiles.cpp
UI/Shared/TestFunctionCurveViewerPopup.cpp
Expand Down
39 changes: 39 additions & 0 deletions tests/TestOpenSimCreator/Graphics/TestOpenSimDecorationOptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <OpenSimCreator/Graphics/OpenSimDecorationOptions.h>

#include <oscar/Utils/Conversion.h>
#include <oscar/Utils/StringHelpers.h>
#include <oscar/Variant.h>
#include <gtest/gtest.h>

#include <string_view>
#include <unordered_map>

using namespace osc;

TEST(OpenSimDecorationOptions, RemembersColorScaling)
{
OpenSimDecorationOptions opts;
opts.setMuscleColorSourceScaling(MuscleColorSourceScaling::ModelWide);
bool emitted = false;
opts.forEachOptionAsAppSettingValue([&emitted](std::string_view k, const Variant& v)
{
// Yep, this is hard-coded: it's just here as a sanity check: change/remove
// it if it's causing trouble.
if (k == "muscle_color_scaling" and to<std::string>(v) == "model_wide") {
emitted = true;
}
});
ASSERT_TRUE(emitted);
}

TEST(OpenSimDecorationOptions, ReadsColorScalingFromDict)
{
const std::unordered_map<std::string, Variant> lookup = {
{"muscle_color_scaling", "model_wide"},
};

OpenSimDecorationOptions opts;
ASSERT_NE(opts.getMuscleColorSourceScaling(), MuscleColorSourceScaling::ModelWide);
opts.tryUpdFromValues("", lookup);
ASSERT_EQ(opts.getMuscleColorSourceScaling(), MuscleColorSourceScaling::ModelWide);
}

0 comments on commit a872b84

Please sign in to comment.