Small float values should be true

pull/4203/head
RichardTea 2021-11-23 14:15:32 +00:00
parent 5143044cc8
commit d18709b899
2 changed files with 38 additions and 5 deletions

View File

@ -163,16 +163,21 @@ AI_FORCE_INLINE
} 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)) {
case aiPTI_Double: {
// Read as float and cast to bool
float value = 0.0f;
if (AI_SUCCESS == ::aiGetMaterialFloat(this, pKey, type, idx, &value)) {
pOut = static_cast<bool>(value);
return AI_SUCCESS;
}
return AI_FAILURE;
}
case aiPTI_Integer: {
// Cast to bool
const int value = static_cast<int>(*prop->mData);
pOut = static_cast<bool>(value);
return AI_SUCCESS;
}
}
}
return ret;

View File

@ -151,6 +151,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) {
EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "integer"));
value = 0;
EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero"));
value = -1;
EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "negative"));
// To float
float valFloat = 0.0f;
@ -158,6 +160,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) {
EXPECT_EQ(10.0f, valFloat);
EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valFloat));
EXPECT_EQ(0.0f, valFloat);
EXPECT_EQ(AI_SUCCESS, pcMat->Get("negative", 0, 0, valFloat));
EXPECT_EQ(-1.0f, valFloat);
// To bool
bool valBool = false;
@ -165,6 +169,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) {
EXPECT_EQ(true, valBool);
EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool));
EXPECT_EQ(false, valBool);
EXPECT_EQ(AI_SUCCESS, pcMat->Get("negative", 0, 0, valBool));
EXPECT_EQ(true, valBool);
}
// ------------------------------------------------------------------------------------------------
@ -188,3 +194,25 @@ TEST_F(MaterialSystemTest, testCastFloatProperty) {
EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool));
EXPECT_EQ(false, valBool);
}
// ------------------------------------------------------------------------------------------------
TEST_F(MaterialSystemTest, testCastSmallFloatProperty) {
float value = 0.0078125f;
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(0, 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);
}