float and integer material property bool conversion

All reading float and integer material property values as bool
Zero => False
Non-Zero => True
pull/4203/head
RichardTea 2021-11-23 13:53:58 +00:00
parent 42766af702
commit 2fdf52488f
2 changed files with 101 additions and 7 deletions

View File

@ -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<bool>(value);
return AI_SUCCESS;
}
return AI_FAILURE;
}
}
}
return ret;
}
// ---------------------------------------------------------------------------
AI_FORCE_INLINE
aiReturn aiMaterial::Get(const char* pKey,unsigned int type,

View File

@ -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);
}