Skip to content

Commit

Permalink
Enum metadata support for Unreal
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Feb 5, 2025
1 parent f03dc33 commit f8924b6
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Source/CesiumRuntime/Private/CesiumPropertyTableProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "CesiumPropertyTableProperty.h"
#include "CesiumGltf/MetadataConversions.h"
#include "CesiumGltf/PropertyEnumValue.h"
#include "CesiumGltf/PropertyTypeTraits.h"
#include "UnrealMetadataConversions.h"
#include <utility>
Expand Down Expand Up @@ -756,6 +757,12 @@ TResult arrayPropertyTablePropertyCallback(
false,
TResult,
Callback>(property, std::forward<Callback>(callback));
case ECesiumMetadataType::Enum:
return propertyTablePropertyCallback<
CesiumGltf::PropertyArrayView<CesiumGltf::PropertyEnumValue>,
false,
TResult,
Callback>(property, std::forward<Callback>(callback));
default:
return callback(CesiumGltf::PropertyTablePropertyView<uint8_t>());
}
Expand Down Expand Up @@ -824,6 +831,12 @@ TResult propertyTablePropertyCallback(
false,
TResult,
Callback>(property, std::forward<Callback>(callback));
case ECesiumMetadataType::Enum:
return propertyTablePropertyCallback<
CesiumGltf::PropertyEnumValue,
false,
TResult,
Callback>(property, std::forward<Callback>(callback));
default:
return callback(CesiumGltf::PropertyTablePropertyView<uint8_t>());
}
Expand Down Expand Up @@ -1257,6 +1270,12 @@ FString UCesiumPropertyTablePropertyBlueprintLibrary::GetString(
CesiumGltf::IsMetadataMatN<ValueType>::value ||
CesiumGltf::IsMetadataString<ValueType>::value) {
return UnrealMetadataConversions::toString(value);
} else if constexpr (CesiumGltf::IsMetadataEnum<ValueType>::value) {
if (v.enumDefinition()) {
return UnrealMetadataConversions::toString(
value.name(*v.enumDefinition()));
}
return FString();
} else {
auto maybeString = CesiumGltf::
MetadataConversions<std::string, decltype(value)>::convert(value);
Expand Down
32 changes: 32 additions & 0 deletions Source/CesiumRuntime/Private/Tests/CesiumMetadataValue.spec.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2024 CesiumGS, Inc. and Contributors

#include "CesiumMetadataValue.h"
#include "CesiumGltf/PropertyEnumValue.h"
#include "CesiumPropertyArrayBlueprintLibrary.h"
#include "Misc/AutomationTest.h"

Expand Down Expand Up @@ -101,6 +102,18 @@ void FCesiumMetadataValueSpec::Define() {
ECesiumMetadataComponentType::Uint8);
TestTrue("IsArray", valueType.bIsArray);
});

It("constructs enum value with correct type", [this]() {
FCesiumMetadataValue value(CesiumGltf::PropertyEnumValue(0));
FCesiumMetadataValueType valueType =
UCesiumMetadataValueBlueprintLibrary::GetValueType(value);
TestEqual("Type", valueType.Type, ECesiumMetadataType::Enum);
TestEqual(
"ComponentType",
valueType.ComponentType,
ECesiumMetadataComponentType::None);
TestFalse("IsArray", valueType.bIsArray);
});
});

Describe("GetBoolean", [this]() {
Expand Down Expand Up @@ -257,6 +270,14 @@ void FCesiumMetadataValueSpec::Define() {
-1234);
});

It("gets from enum", [this]() {
FCesiumMetadataValue value(CesiumGltf::PropertyEnumValue{0xff});
TestEqual(
"value",
UCesiumMetadataValueBlueprintLibrary::GetInteger(value, 0),
0xff);
});

It("returns default value for out-of-range numbers", [this]() {
FCesiumMetadataValue value(std::numeric_limits<int64_t>::min());
TestEqual(
Expand Down Expand Up @@ -351,6 +372,17 @@ void FCesiumMetadataValueSpec::Define() {
static_cast<int64_t>(-1234));
});

It("gets from enum", [this, defaultValue]() {
FCesiumMetadataValue value(
CesiumGltf::PropertyEnumValue(0x6fffffffffffffff));
TestEqual<int64>(
"value",
UCesiumMetadataValueBlueprintLibrary::GetInteger64(
value,
defaultValue),
static_cast<int64_t>(0x6fffffffffffffff));
});

It("returns default value for out-of-range numbers",
[this, defaultValue]() {
FCesiumMetadataValue value(std::numeric_limits<float>::lowest());
Expand Down
53 changes: 52 additions & 1 deletion Source/CesiumRuntime/Private/Tests/CesiumPropertyTable.spec.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
// Copyright 2020-2024 CesiumGS, Inc. and Contributors

#include "CesiumPropertyTable.h"
#include "CesiumGltf/Enum.h"
#include "CesiumGltf/EnumValue.h"
#include "CesiumGltf/ExtensionModelExtStructuralMetadata.h"
#include "CesiumGltf/Model.h"
#include "CesiumGltfSpecUtility.h"
#include "Misc/AutomationTest.h"
#include <limits>

namespace {

CesiumGltf::EnumValue makeEnumValue(const std::string& name, int64_t value) {
CesiumGltf::EnumValue enumValue;
enumValue.name = name;
enumValue.value = value;
return enumValue;
}

} // namespace

BEGIN_DEFINE_SPEC(
FCesiumPropertyTableSpec,
"Cesium.Unit.PropertyTable",
Expand Down Expand Up @@ -769,6 +782,36 @@ void FCesiumPropertyTableSpec::Define() {
CesiumGltf::ClassProperty::ComponentType::FLOAT32,
vec2Values);

std::string enumPropertyName("enumProperty");
std::vector<int16_t> enumValues{0, 1, 2, 3};
std::vector<std::string> enumNames{"Foo", "Bar", "Baz", "Qux"};
CesiumGltf::PropertyTableProperty& enumTableProperty =
AddPropertyTablePropertyToModel(
model,
*pPropertyTable,
enumPropertyName,
CesiumGltf::ClassProperty::Type::ENUM,
std::nullopt,
enumValues);

CesiumGltf::Schema& schema =
*model
.getExtension<CesiumGltf::ExtensionModelExtStructuralMetadata>()
->schema;
schema.classes[pPropertyTable->classProperty]
.properties[enumPropertyName]
.enumType = "TestEnum";

CesiumGltf::Enum& enumDef = schema.enums["TestEnum"];
enumDef.name = "Test";
enumDef.description = "An example enum";
enumDef.values = std::vector<CesiumGltf::EnumValue>{
makeEnumValue("Foo", 0),
makeEnumValue("Bar", 1),
makeEnumValue("Baz", 2),
makeEnumValue("Qux", 3)};
enumDef.valueType = CesiumGltf::Enum::ValueType::INT16;

FCesiumPropertyTable propertyTable(model, *pPropertyTable);

TestEqual(
Expand All @@ -787,14 +830,17 @@ void FCesiumPropertyTableSpec::Define() {
GetMetadataValuesForFeatureAsStrings(
propertyTable,
static_cast<int64>(i));
TestEqual("number of values", values.Num(), 2);
TestEqual("number of values", values.Num(), 3);

TestTrue(
"contains scalar value",
values.Contains(FString(scalarPropertyName.c_str())));
TestTrue(
"contains vec2 value",
values.Contains(FString(vec2PropertyName.c_str())));
TestTrue(
"contains enum value",
values.Contains(FString(enumPropertyName.c_str())));

const FString& scalarValue =
*values.Find(FString(scalarPropertyName.c_str()));
Expand All @@ -808,6 +854,11 @@ void FCesiumPropertyTableSpec::Define() {
" Y=" + std::to_string(vec2Values[i][1]));
expected = FString(expectedString.c_str());
TestEqual("vec2 value as string", vec2Value, expected);

const FString& enumValue =
*values.Find(FString(enumPropertyName.c_str()));
expected = FString(enumNames[i].c_str());
TestEqual("enum value as string", enumValue, expected);
}
});

Expand Down
5 changes: 4 additions & 1 deletion Source/CesiumRuntime/Public/CesiumMetadataValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include "CesiumGltf/PropertyEnumValue.h"
#include "CesiumGltf/PropertyTypeTraits.h"
#include "CesiumMetadataValueType.h"
#include "CesiumPropertyArray.h"
Expand Down Expand Up @@ -37,6 +38,7 @@ struct CESIUMRUNTIME_API FCesiumMetadataValue {
double,
bool,
std::string_view,
CesiumGltf::PropertyEnumValue,
glm::vec<2, int8_t>,
glm::vec<2, uint8_t>,
glm::vec<2, int16_t>,
Expand Down Expand Up @@ -168,7 +170,8 @@ struct CESIUMRUNTIME_API FCesiumMetadataValue {
ArrayView<glm::mat<4, 4, int64_t>>,
ArrayView<glm::mat<4, 4, uint64_t>>,
ArrayView<glm::mat<4, 4, float>>,
ArrayView<glm::mat<4, 4, double>>>;
ArrayView<glm::mat<4, 4, double>>,
ArrayView<CesiumGltf::PropertyEnumValue>>;
#pragma endregion

public:
Expand Down
4 changes: 3 additions & 1 deletion Source/CesiumRuntime/Public/CesiumPropertyArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include "CesiumGltf/PropertyArrayView.h"
#include "CesiumGltf/PropertyEnumValue.h"
#include "CesiumGltf/PropertyTypeTraits.h"
#include "CesiumMetadataValueType.h"
#include "UObject/ObjectMacros.h"
Expand Down Expand Up @@ -93,7 +94,8 @@ struct CESIUMRUNTIME_API FCesiumPropertyArray {
ArrayPropertyView<glm::mat<4, 4, int64_t>>,
ArrayPropertyView<glm::mat<4, 4, uint64_t>>,
ArrayPropertyView<glm::mat<4, 4, float>>,
ArrayPropertyView<glm::mat<4, 4, double>>>;
ArrayPropertyView<glm::mat<4, 4, double>>,
ArrayPropertyView<CesiumGltf::PropertyEnumValue>>;
#pragma endregion

public:
Expand Down

0 comments on commit f8924b6

Please sign in to comment.