From 2fdf52488fba296beae885b4b6e983de375a8236 Mon Sep 17 00:00:00 2001 From: RichardTea <31507749+RichardTea@users.noreply.github.com> Date: Tue, 23 Nov 2021 13:53:58 +0000 Subject: [PATCH] float and integer material property bool conversion All reading float and integer material property values as bool Zero => False Non-Zero => True --- include/assimp/material.inl | 41 +++++++++++++++++++++ test/unit/utMaterialSystem.cpp | 67 ++++++++++++++++++++++++++++++---- 2 files changed, 101 insertions(+), 7 deletions(-) diff --git a/include/assimp/material.inl b/include/assimp/material.inl index 693785c3c..1aec6d865 100644 --- a/include/assimp/material.inl +++ b/include/assimp/material.inl @@ -137,6 +137,47 @@ aiReturn aiMaterial::Get(const char* pKey,unsigned int type, return ret; } +// --------------------------------------------------------------------------- +// Specialisation for a single bool. +// Casts floating point and integer to bool +template <> +AI_FORCE_INLINE + aiReturn + aiMaterial::Get(const char *pKey, unsigned int type, + unsigned int idx, bool &pOut) const { + const aiMaterialProperty *prop; + const aiReturn ret = ::aiGetMaterialProperty(this, pKey, type, idx, + (const aiMaterialProperty **)&prop); + if (AI_SUCCESS == ret) { + + switch (prop->mType) { + // Type cannot be converted + default: return AI_FAILURE; + + case aiPTI_Buffer: { + // Native bool value storage + if (prop->mDataLength < sizeof(bool)) { + return AI_FAILURE; + } + ::memcpy(&pOut, prop->mData, sizeof(bool)); + } break; + + case aiPTI_Float: + case aiPTI_Double: + case aiPTI_Integer: { + // Read as integer and cast to bool + int value = 0; + if (AI_SUCCESS == ::aiGetMaterialInteger(this, pKey, type, idx, &value)) { + pOut = static_cast(value); + return AI_SUCCESS; + } + return AI_FAILURE; + } + } + } + return ret; +} + // --------------------------------------------------------------------------- AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type, diff --git a/test/unit/utMaterialSystem.cpp b/test/unit/utMaterialSystem.cpp index 8db014d9c..9df64ae3b 100644 --- a/test/unit/utMaterialSystem.cpp +++ b/test/unit/utMaterialSystem.cpp @@ -125,13 +125,66 @@ TEST_F(MaterialSystemTest, testStringProperty) { } // ------------------------------------------------------------------------------------------------ -TEST_F(MaterialSystemTest, testMaterialNameAccess) { - aiMaterial *mat = new aiMaterial(); - EXPECT_NE(nullptr, mat); - - aiString name = mat->GetName(); +TEST_F(MaterialSystemTest, testDefaultMaterialName) { + aiString name = pcMat->GetName(); const int retValue(strncmp(name.C_Str(), AI_DEFAULT_MATERIAL_NAME, name.length)); EXPECT_EQ(0, retValue); - - delete mat; +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testBoolProperty) { + const bool valTrue = true; + const bool valFalse = false; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&valTrue, 1, "bool_true")); + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&valFalse, 1, "bool_false")); + + bool read = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("bool_true", 0, 0, read)); + EXPECT_TRUE(read) << "read true bool"; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("bool_false", 0, 0, read)); + EXPECT_FALSE(read) << "read false bool"; +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testCastIntProperty) { + int value = 10; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "integer")); + value = 0; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero")); + + // To float + float valFloat = 0.0f; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("integer", 0, 0, valFloat)); + EXPECT_EQ(10.0f, valFloat); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valFloat)); + EXPECT_EQ(0.0f, valFloat); + + // To bool + bool valBool = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("integer", 0, 0, valBool)); + EXPECT_EQ(true, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool)); + EXPECT_EQ(false, valBool); +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testCastFloatProperty) { + float value = 150392.63f; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "float")); + value = 0; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero")); + + // To int + int valInt = 0.0f; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valInt)); + EXPECT_EQ(150392, valInt); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valInt)); + EXPECT_EQ(0, valInt); + + // To bool + bool valBool = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valBool)); + EXPECT_EQ(true, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool)); + EXPECT_EQ(false, valBool); }