Skip to content

Commit

Permalink
Add #42: Set default values in struct initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Jan 4, 2024
1 parent b310469 commit eee2b0e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 82 deletions.
43 changes: 21 additions & 22 deletions include/fastgltf/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ namespace fastgltf {
struct AnimationSampler {
std::size_t inputAccessor;
std::size_t outputAccessor;
AnimationInterpolation interpolation;
AnimationInterpolation interpolation = AnimationInterpolation::Linear;
};

struct Animation {
Expand Down Expand Up @@ -1324,8 +1324,8 @@ namespace fastgltf {
struct Sampler {
Optional<Filter> magFilter;
Optional<Filter> minFilter;
Wrap wrapS;
Wrap wrapT;
Wrap wrapS = Wrap::Repeat;
Wrap wrapT = Wrap::Repeat;

FASTGLTF_STD_PMR_NS::string name;
};
Expand All @@ -1350,9 +1350,9 @@ namespace fastgltf {
FASTGLTF_FG_PMR_NS::MaybeSmallVector<num> weights;

struct TRS {
std::array<num, 3> translation;
std::array<num, 4> rotation;
std::array<num, 3> scale;
std::array<num, 3> translation = {{ 0.f, 0.f, 0.f }};
std::array<num, 4> rotation = {{ 0.f, 0.f, 0.f, 1.f }};
std::array<num, 3> scale = {{ 1.f, 1.f, 1.f }};
};
using TransformMatrix = std::array<num, 16>;

Expand Down Expand Up @@ -1393,7 +1393,7 @@ namespace fastgltf {
// Instead of a map, we have a list of attributes here. Each pair contains
// the name of the attribute and the corresponding accessor index.
FASTGLTF_FG_PMR_NS::SmallVector<attribute_type, 4> attributes;
PrimitiveType type;
PrimitiveType type = PrimitiveType::Triangles;

FASTGLTF_STD_PMR_NS::vector<FASTGLTF_FG_PMR_NS::SmallVector<attribute_type, 4>> targets;

Expand Down Expand Up @@ -1469,7 +1469,7 @@ namespace fastgltf {

struct TextureInfo {
std::size_t textureIndex;
std::size_t texCoordIndex;
std::size_t texCoordIndex = 0;

/**
* Data from KHR_texture_transform, and nullptr if the extension wasn't enabled or used.
Expand All @@ -1478,11 +1478,11 @@ namespace fastgltf {
};

struct NormalTextureInfo : TextureInfo {
num scale;
num scale = 1.f;
};

struct OcclusionTextureInfo : TextureInfo {
num strength;
num strength = 1.f;
};

struct PBRData {
Expand Down Expand Up @@ -1597,26 +1597,25 @@ namespace fastgltf {
Optional<TextureInfo> emissiveTexture;

/**
* The factors for the emissive color of the material. Defaults to 0,0,0
* The factors for the emissive color of the material.
*/
std::array<num, 3> emissiveFactor;
std::array<num, 3> emissiveFactor = {{ 0.f, 0.f, 0.f }};

/**
* The values used to determine the transparency of the material.
* Defaults to #AlphaMode::Opaque.
*/
AlphaMode alphaMode;
AlphaMode alphaMode = AlphaMode::Opaque;

/**
* The alpha value that determines the upper limit for fragments that
* should be discarded for transparency. Defaults to 0.5.
* should be discarded for transparency.
*/
num alphaCutoff;
num alphaCutoff = 0.5f;

/**
* Determines whether back-face culling should be disabled when using this material.
*/
bool doubleSided;
bool doubleSided = false;

std::unique_ptr<MaterialAnisotropy> anisotropy;

Expand Down Expand Up @@ -1717,18 +1716,18 @@ namespace fastgltf {
struct SparseAccessor {
std::size_t count;
std::size_t indicesBufferView;
std::size_t indicesByteOffset;
std::size_t indicesByteOffset = 0;
std::size_t valuesBufferView;
std::size_t valuesByteOffset;
std::size_t valuesByteOffset = 0;
ComponentType indexComponentType;
};

struct Accessor {
std::size_t byteOffset;
std::size_t byteOffset = 0;
std::size_t count;
AccessorType type;
ComponentType componentType;
bool normalized;
bool normalized = false;

std::variant<std::monostate, FASTGLTF_STD_PMR_NS::vector<double>, FASTGLTF_STD_PMR_NS::vector<std::int64_t>> max;
std::variant<std::monostate, FASTGLTF_STD_PMR_NS::vector<double>, FASTGLTF_STD_PMR_NS::vector<std::int64_t>> min;
Expand All @@ -1754,7 +1753,7 @@ namespace fastgltf {

struct BufferView {
std::size_t bufferIndex;
std::size_t byteOffset;
std::size_t byteOffset = 0;
std::size_t byteLength;

Optional<std::size_t> byteStride;
Expand Down
115 changes: 56 additions & 59 deletions src/fastgltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,26 +238,26 @@ namespace fastgltf {
return Error::InvalidGltf;
}

if (child["texCoord"].get_uint64().get(index) == SUCCESS) {
if (auto error = child["texCoord"].get_uint64().get(index); error == SUCCESS) {
info->texCoordIndex = static_cast<std::size_t>(index);
} else {
info->texCoordIndex = 0;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidJson;
}

if (type == TextureInfoType::NormalTexture) {
double scale = 1.0F;
if (child["scale"].get_double().get(scale) == SUCCESS) {
double scale;
if (auto error = child["scale"].get_double().get(scale); error == SUCCESS) {
reinterpret_cast<NormalTextureInfo*>(info)->scale = static_cast<float>(scale);
} else {
reinterpret_cast<NormalTextureInfo*>(info)->scale = 1.0F;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}
} else if (type == TextureInfoType::OcclusionTexture) {
double strength = 1.0F;
if (child["strength"].get_double().get(strength) == SUCCESS) {
double strength;
if (auto error = child["strength"].get_double().get(strength); error == SUCCESS) {
reinterpret_cast<OcclusionTextureInfo*>(info)->strength = static_cast<float>(strength);
} else {
reinterpret_cast<OcclusionTextureInfo*>(info)->strength = 1.0F;
}
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}
}

dom::object extensionsObject;
Expand Down Expand Up @@ -1262,10 +1262,10 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass

// byteOffset is optional, but defaults to 0
std::uint64_t byteOffset;
if (accessorObject["byteOffset"].get_uint64().get(byteOffset) != SUCCESS) {
accessor.byteOffset = 0U;
} else {
if (auto error = accessorObject["byteOffset"].get_uint64().get(byteOffset); error == SUCCESS) {
accessor.byteOffset = static_cast<std::size_t>(byteOffset);
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

// Type of min and max should always be the same.
Expand Down Expand Up @@ -1353,8 +1353,8 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass
return error;
}

if (accessorObject["normalized"].get_bool().get(accessor.normalized) != SUCCESS) {
accessor.normalized = false;
if (auto error = accessorObject["normalized"].get_bool().get(accessor.normalized); error != SUCCESS && error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

// This property MUST NOT be set to true for accessors with FLOAT or UNSIGNED_INT component type.
Expand Down Expand Up @@ -1382,10 +1382,10 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass
}
sparse.indicesBufferView = static_cast<std::size_t>(value);

if (child["byteOffset"].get_uint64().get(value) != SUCCESS) {
sparse.indicesByteOffset = 0;
} else {
if (auto error = child["byteOffset"].get_uint64().get(value); error == SUCCESS) {
sparse.indicesByteOffset = static_cast<std::size_t>(value);
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

if (child["componentType"].get_uint64().get(value) != SUCCESS) {
Expand All @@ -1403,10 +1403,10 @@ fg::Error fg::Parser::parseAccessors(simdjson::dom::array& accessors, Asset& ass
}
sparse.valuesBufferView = static_cast<std::size_t>(value);

if (child["byteOffset"].get_uint64().get(value) != SUCCESS) {
sparse.valuesByteOffset = 0;
} else {
if (auto error = child["byteOffset"].get_uint64().get(value); error == SUCCESS) {
sparse.valuesByteOffset = static_cast<std::size_t>(value);
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

accessor.sparse = sparse;
Expand Down Expand Up @@ -1648,9 +1648,7 @@ fg::Error fg::Parser::parseBufferViews(simdjson::dom::array& bufferViews, Asset&

if (auto error = bufferViewObject["byteOffset"].get_uint64().get(number); error == SUCCESS) {
view.byteOffset = static_cast<std::size_t>(number);
} else if (error == NO_SUCH_FIELD) {
view.byteOffset = 0;
} else {
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidJson;
}

Expand Down Expand Up @@ -2097,7 +2095,7 @@ fg::Error fg::Parser::parseMaterials(simdjson::dom::array& materials, Asset& ass
Material material = {};

dom::array emissiveFactor;
if (materialObject["emissiveFactor"].get_array().get(emissiveFactor) == SUCCESS) {
if (auto error = materialObject["emissiveFactor"].get_array().get(emissiveFactor); error == SUCCESS) {
if (emissiveFactor.size() != 3) {
return Error::InvalidGltf;
}
Expand All @@ -2108,8 +2106,8 @@ fg::Error fg::Parser::parseMaterials(simdjson::dom::array& materials, Asset& ass
}
material.emissiveFactor[i] = static_cast<num>(val);
}
} else {
material.emissiveFactor = {{ 0, 0, 0 }};
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

{
Expand Down Expand Up @@ -2179,7 +2177,7 @@ fg::Error fg::Parser::parseMaterials(simdjson::dom::array& materials, Asset& ass
}

std::string_view alphaMode;
if (materialObject["alphaMode"].get_string().get(alphaMode) == SUCCESS) {
if (auto error = materialObject["alphaMode"].get_string().get(alphaMode); error == SUCCESS) {
if (alphaMode == "OPAQUE") {
material.alphaMode = AlphaMode::Opaque;
} else if (alphaMode == "MASK") {
Expand All @@ -2189,22 +2187,22 @@ fg::Error fg::Parser::parseMaterials(simdjson::dom::array& materials, Asset& ass
} else {
return Error::InvalidGltf;
}
} else {
material.alphaMode = AlphaMode::Opaque;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

double alphaCutoff = 0.5;
if (materialObject["alphaCutoff"].get_double().get(alphaCutoff) == SUCCESS) {
double alphaCutoff;
if (auto error = materialObject["alphaCutoff"].get_double().get(alphaCutoff); error == SUCCESS) {
material.alphaCutoff = static_cast<num>(alphaCutoff);
} else {
material.alphaCutoff = 0.5F;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

bool doubleSided = false;
if (materialObject["doubleSided"].get_bool().get(doubleSided) == SUCCESS) {
bool doubleSided;
if (auto error = materialObject["doubleSided"].get_bool().get(doubleSided); error == SUCCESS) {
material.doubleSided = doubleSided;
} else {
material.doubleSided = false;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

std::string_view name;
Expand Down Expand Up @@ -2786,12 +2784,11 @@ fg::Error fg::Parser::parseMeshes(simdjson::dom::array& meshes, Asset& asset) {
}
}

// Mode shall default to 4.
std::uint64_t value;
if (primitiveObject["mode"].get_uint64().get(value) != SUCCESS) {
primitive.type = PrimitiveType::Triangles;
} else {
if (auto error = primitiveObject["mode"].get_uint64().get(value); error == SUCCESS) {
primitive.type = static_cast<PrimitiveType>(value);
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

if (primitiveObject["indices"].get_uint64().get(value) == SUCCESS) {
Expand Down Expand Up @@ -2910,7 +2907,7 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) {
Node::TRS trs = {};

// There's no matrix, let's see if there's scale, rotation, or rotation fields.
if (nodeObject["scale"].get_array().get(array) == SUCCESS) {
if (auto error = nodeObject["scale"].get_array().get(array); error == SUCCESS) {
auto i = 0U;
for (auto num : array) {
double val;
Expand All @@ -2920,11 +2917,11 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) {
trs.scale[i] = static_cast<fastgltf::num>(val);
++i;
}
} else {
trs.scale = {{ 1.0F, 1.0F, 1.0F }};
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidJson;
}

if (nodeObject["translation"].get_array().get(array) == SUCCESS) {
if (auto error = nodeObject["translation"].get_array().get(array); error == SUCCESS) {
auto i = 0U;
for (auto num : array) {
double val;
Expand All @@ -2934,11 +2931,11 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) {
trs.translation[i] = static_cast<fastgltf::num>(val);
++i;
}
} else {
trs.translation = {{ 0.0F, 0.0F, 0.0F }};
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

if (nodeObject["rotation"].get_array().get(array) == SUCCESS) {
if (auto error = nodeObject["rotation"].get_array().get(array); error == SUCCESS) {
auto i = 0U;
for (auto num : array) {
double val;
Expand All @@ -2948,8 +2945,8 @@ fg::Error fg::Parser::parseNodes(simdjson::dom::array& nodes, Asset& asset) {
trs.rotation[i] = static_cast<fastgltf::num>(val);
++i;
}
} else {
trs.rotation = {{ 0.0F, 0.0F, 0.0F, 1.0F }};
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

node.transform = trs;
Expand Down Expand Up @@ -3026,15 +3023,15 @@ fg::Error fg::Parser::parseSamplers(simdjson::dom::array& samplers, Asset& asset
sampler.minFilter = static_cast<Filter>(number);
}

if (samplerObject["wrapS"].get_uint64().get(number) == SUCCESS) {
if (auto error = samplerObject["wrapS"].get_uint64().get(number); error == SUCCESS) {
sampler.wrapS = static_cast<Wrap>(number);
} else {
sampler.wrapS = Wrap::Repeat;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}
if (samplerObject["wrapT"].get_uint64().get(number) == SUCCESS) {
if (auto error = samplerObject["wrapT"].get_uint64().get(number); error == SUCCESS) {
sampler.wrapT = static_cast<Wrap>(number);
} else {
sampler.wrapT = Wrap::Repeat;
} else if (error != NO_SUCH_FIELD) {
return Error::InvalidGltf;
}

asset.samplers.emplace_back(std::move(sampler));
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ TEST_CASE("Compare base64 decoding performance", "[gltf-benchmark]") {
}
#elif defined(FASTGLTF_IS_A64)
const auto& impls = simdjson::get_available_implementations();
if (const auto* neon = impls["arm64"]; avx2 != nullptr && neon->supported_by_runtime_system()) {
if (const auto* neon = impls["arm64"]; neon != nullptr && neon->supported_by_runtime_system()) {
BENCHMARK("Run fastgltf's Neon base64 decoder") {
return fastgltf::base64::neon_decode(generatedData);
};
Expand Down

0 comments on commit eee2b0e

Please sign in to comment.