Skip to content

Commit

Permalink
Add: Some tests to validate convertComponent functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Nov 28, 2023
1 parent d088f80 commit cf8c9fa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ option(FASTGLTF_ENABLE_TESTS "Enables test targets for fastlgtf" OFF)
option(FASTGLTF_ENABLE_EXAMPLES "Enables example targets for fastgltf" OFF)
option(FASTGLTF_ENABLE_DOCS "Enables the configuration of targets that build/generate documentation" OFF)
option(FASTGLTF_ENABLE_GLTF_RS "Enables the benchmark usage of gltf-rs" OFF)
option(FASTGLTF_ENABLE_ASSIMP "Enables the benchmark usage of assimp" OFF)
option(FASTGLTF_ENABLE_DEPRECATED_EXT "Enables support for deprecated extensions" OFF)
option(FASTGLTF_DISABLE_CUSTOM_MEMORY_POOL "Disables the memory allocation algorithm based on polymorphic resources" OFF)
option(FASTGLTF_USE_64BIT_FLOAT "Default to 64-bit double precision floats for everything" OFF)
Expand Down
11 changes: 7 additions & 4 deletions include/fastgltf/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ concept Element = std::is_arithmetic_v<typename ElementTraits<ElementType>::comp

namespace internal {

template <typename SourceType, typename DestType, std::size_t Index>
constexpr DestType convertComponent(const std::byte* bytes, bool normalized) {
auto source = reinterpret_cast<const SourceType*>(bytes)[Index];

template <typename DestType, typename SourceType>
constexpr DestType convertComponent(const SourceType& source, bool normalized) {
if (normalized) {
if constexpr (std::is_floating_point_v<SourceType> && std::is_integral_v<DestType>) {
// float -> int conversion
Expand All @@ -150,6 +148,11 @@ constexpr DestType convertComponent(const std::byte* bytes, bool normalized) {
return static_cast<DestType>(source);
}

template <typename SourceType, typename DestType, std::size_t Index>
constexpr DestType convertComponent(const std::byte* bytes, bool normalized) {
return convertComponent<DestType>(reinterpret_cast<const SourceType*>(bytes)[Index], normalized);
}

template <typename ElementType, typename SourceType, std::size_t... I>
#if FASTGLTF_HAS_CONCEPTS
requires Element<ElementType>
Expand Down
2 changes: 1 addition & 1 deletion src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ fg::Error fg::Parser::generateMeshIndices(fastgltf::Asset& asset) const {
fastgltf::span<std::uint32_t> indices { reinterpret_cast<std::uint32_t*>(generatedIndices.bytes.data()),
generatedIndices.bytes.size() / sizeof(std::uint32_t) };
for (std::size_t i = 0; i < positionAccessor.count; ++i) {
indices[i] = i;
indices[i] = static_cast<std::uint32_t>(i);
}

auto bufferIdx = asset.buffers.size();
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (FASTGLTF_ENABLE_GLTF_RS AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf-rs/src/
target_compile_definitions(fastgltf_tests PRIVATE HAS_GLTFRS=1)
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf_loaders/assimp")
if(FASTGLTF_ENABLE_ASSIMP AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/gltf_loaders/assimp")
message(STATUS "fastgltf: Found assimp")
# Only enable glTF importer
set(ASSIMP_NO_EXPORT ON CACHE BOOL "")
Expand Down
26 changes: 26 additions & 0 deletions tests/accessor_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <catch2/catch_test_macros.hpp>

#include <glm/vec3.hpp>
#include <glm/gtc/epsilon.hpp>
#include <glm/ext/scalar_constants.hpp>

#include <fastgltf/parser.hpp>
#include <fastgltf/tools.hpp>
Expand All @@ -25,6 +27,30 @@ static const std::byte* getBufferData(const fastgltf::Buffer& buffer) {
return result;
}

TEST_CASE("Test data type conversion", "[gltf-tools]") {
// normalized int-to-float and normalized float-to-int
for (auto i = std::numeric_limits<std::int8_t>::min(); i < std::numeric_limits<std::int8_t>::max(); ++i) {
auto converted = fastgltf::internal::convertComponent<float>(i, true);
REQUIRE(glm::epsilonEqual<float>(converted, fastgltf::max<float>(i / 127.0f, -1), glm::epsilon<float>()));
REQUIRE(fastgltf::internal::convertComponent<std::int8_t>(converted, true) == std::round(converted * 127.0f));
}
for (auto i = std::numeric_limits<std::uint8_t>::min(); i < std::numeric_limits<std::uint8_t>::max(); ++i) {
auto converted = fastgltf::internal::convertComponent<float>(i, true);
REQUIRE(glm::epsilonEqual<float>(converted, i / 255.0f, glm::epsilon<float>()));
REQUIRE(fastgltf::internal::convertComponent<std::uint8_t>(converted, true) == std::round(converted * 255.0f));
}
for (auto i = std::numeric_limits<std::int16_t>::min(); i < std::numeric_limits<std::int16_t>::max(); ++i) {
auto converted = fastgltf::internal::convertComponent<float>(i, true);
REQUIRE(glm::epsilonEqual<float>(converted, fastgltf::max<float>(i / 32767.0f, -1), glm::epsilon<float>()));
REQUIRE(fastgltf::internal::convertComponent<std::int16_t>(converted, true) == std::round(converted * 32767.0f));
}
for (auto i = std::numeric_limits<std::uint16_t>::min(); i < std::numeric_limits<std::uint16_t>::max(); ++i) {
auto converted = fastgltf::internal::convertComponent<float>(i, true);
REQUIRE(glm::epsilonEqual<float>(converted, i / 65535.0f, glm::epsilon<float>()));
REQUIRE(fastgltf::internal::convertComponent<std::uint16_t>(converted, true) == std::round(converted * 65535.0f));
}
}

TEST_CASE("Test accessor", "[gltf-tools]") {
auto lightsLamp = sampleModels / "2.0" / "LightsPunctualLamp" / "glTF";
fastgltf::GltfDataBuffer jsonData;
Expand Down

0 comments on commit cf8c9fa

Please sign in to comment.