Skip to content

Commit

Permalink
Add convertPropertyComponentTypeToAccessorComponentType
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Jan 31, 2025
1 parent 7c9e48e commit 83d76b7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
12 changes: 12 additions & 0 deletions CesiumGltf/include/CesiumGltf/PropertyType.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ convertStringOffsetTypeStringToPropertyComponentType(const std::string& str);
PropertyComponentType
convertAccessorComponentTypeToPropertyComponentType(int componentType);

/**
* @brief Converts a \ref PropertyComponentType to an integer type ID listed in
* \ref AccessorSpec::ComponentType.
*
* @param componentType The \ref PropertyComponentType to convert to an integer
* type ID.
* @returns The integer type ID listed in \ref AccessorSpec::ComponentType, or
* -1 if no conversion is possible.
*/
int32_t convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType componentType);

/**
* @brief Checks if the given \ref PropertyType represents a vector with any
* number of components.
Expand Down
28 changes: 28 additions & 0 deletions CesiumGltf/src/PropertyType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,34 @@ convertAccessorComponentTypeToPropertyComponentType(int componentType) {
}
}

int32_t convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType componentType) {
switch (componentType) {
case PropertyComponentType::Int8:
return AccessorSpec::ComponentType::BYTE;
case PropertyComponentType::Uint8:
return AccessorSpec::ComponentType::UNSIGNED_BYTE;
case PropertyComponentType::Int16:
return AccessorSpec::ComponentType::SHORT;
case PropertyComponentType::Uint16:
return AccessorSpec::ComponentType::UNSIGNED_SHORT;
case PropertyComponentType::Int32:
return AccessorSpec::ComponentType::INT;
case PropertyComponentType::Uint32:
return AccessorSpec::ComponentType::UNSIGNED_INT;
case PropertyComponentType::Int64:
return AccessorSpec::ComponentType::INT64;
case PropertyComponentType::Uint64:
return AccessorSpec::ComponentType::UNSIGNED_INT64;
case PropertyComponentType::Float32:
return AccessorSpec::ComponentType::FLOAT;
case PropertyComponentType::Float64:
return AccessorSpec::ComponentType::DOUBLE;
default:
return -1;
}
}

bool isPropertyTypeVecN(PropertyType type) {
return type == PropertyType::Vec2 || type == PropertyType::Vec3 ||
type == PropertyType::Vec4;
Expand Down
42 changes: 42 additions & 0 deletions CesiumGltf/test/TestPropertyType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,48 @@ TEST_CASE("Test convertAccessorComponentTypeToPropertyComponentType") {
PropertyComponentType::None);
}

TEST_CASE("Test convertPropertyComponentTypeToAccessorComponentType") {
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Int8) == AccessorSpec::ComponentType::BYTE);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Uint8) ==
AccessorSpec::ComponentType::UNSIGNED_BYTE);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Int16) == AccessorSpec::ComponentType::SHORT);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Uint16) ==
AccessorSpec::ComponentType::UNSIGNED_SHORT);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Int32) == AccessorSpec::ComponentType::INT);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Uint32) ==
AccessorSpec::ComponentType::UNSIGNED_INT);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Int64) == AccessorSpec::ComponentType::INT64);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Uint64) ==
AccessorSpec::ComponentType::UNSIGNED_INT64);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Float32) ==
AccessorSpec::ComponentType::FLOAT);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::Float64) ==
AccessorSpec::ComponentType::DOUBLE);
REQUIRE(
convertPropertyComponentTypeToAccessorComponentType(
PropertyComponentType::None) == -1);
}

TEST_CASE("Test isPropertyTypeVecN") {
REQUIRE(isPropertyTypeVecN(PropertyType::Vec2));
REQUIRE(isPropertyTypeVecN(PropertyType::Vec3));
Expand Down

0 comments on commit 83d76b7

Please sign in to comment.