From 4b980458efb148dd87338558681dd5c8d1bcfcba Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 18 Jun 2020 15:54:11 -0400 Subject: [PATCH] CAnimationLoader: Make use of ranged for where applicable --- .../Resource/Factory/CAnimationLoader.cpp | 199 +++++++++--------- src/Core/Resource/Factory/CAnimationLoader.h | 2 +- 2 files changed, 106 insertions(+), 95 deletions(-) diff --git a/src/Core/Resource/Factory/CAnimationLoader.cpp b/src/Core/Resource/Factory/CAnimationLoader.cpp index 8d267df8..18fb3b74 100644 --- a/src/Core/Resource/Factory/CAnimationLoader.cpp +++ b/src/Core/Resource/Factory/CAnimationLoader.cpp @@ -10,29 +10,29 @@ bool CAnimationLoader::UncompressedCheckEchoes() // a little weird because we have to make sure we don't try to seek or read anything // past the end of the file. The +4 being added to each size we test is to account // for the next size value of the next array. - uint32 End = mpInput->Size(); + const uint32 End = mpInput->Size(); - uint32 NumRotIndices = mpInput->ReadLong(); + const uint32 NumRotIndices = mpInput->ReadLong(); if (mpInput->Tell() + NumRotIndices + 4 >= End) return false; mpInput->Seek(NumRotIndices, SEEK_CUR); - uint32 NumTransIndices = mpInput->ReadLong(); + const uint32 NumTransIndices = mpInput->ReadLong(); if (mpInput->Tell() + NumTransIndices + 4 >= End) return false; mpInput->Seek(NumTransIndices, SEEK_CUR); - uint32 NumScaleIndices = mpInput->ReadLong(); + const uint32 NumScaleIndices = mpInput->ReadLong(); if (mpInput->Tell() + NumScaleIndices + 4 >= End) return false; mpInput->Seek(NumScaleIndices, SEEK_CUR); - uint32 ScaleKeysSize = mpInput->ReadLong() * 0xC; + const uint32 ScaleKeysSize = mpInput->ReadLong() * 0xC; if (mpInput->Tell() + ScaleKeysSize + 4 >= End) return false; mpInput->Seek(ScaleKeysSize, SEEK_CUR); - uint32 RotKeysSize = mpInput->ReadLong() * 0x10; + const uint32 RotKeysSize = mpInput->ReadLong() * 0x10; if (mpInput->Tell() + RotKeysSize + 4 >= End) return false; mpInput->Seek(RotKeysSize, SEEK_CUR); - uint32 TransKeysSize = mpInput->ReadLong() * 0xC; + const uint32 TransKeysSize = mpInput->ReadLong() * 0xC; return (mpInput->Tell() + TransKeysSize == End); } @@ -40,8 +40,8 @@ EGame CAnimationLoader::UncompressedCheckVersion() { // Call this function after the bone channel index array // No version number, so this is how we have to determine the version... - uint32 Start = mpInput->Tell(); - bool Echoes = UncompressedCheckEchoes(); + const uint32 Start = mpInput->Tell(); + const bool Echoes = UncompressedCheckEchoes(); mpInput->Seek(Start, SEEK_SET); return (Echoes ? EGame::Echoes : EGame::Prime); } @@ -63,15 +63,15 @@ void CAnimationLoader::ReadUncompressedANIM() uint32 NumTranslationChannels = 0; // Bone channel list - uint32 NumBoneIndices = mpInput->ReadLong(); + const uint32 NumBoneIndices = mpInput->ReadLong(); ASSERT(NumBoneIndices == 100); std::vector BoneIndices(NumBoneIndices); - for (uint32 iChan = 0; iChan < NumBoneIndices; iChan++) + for (auto& index : BoneIndices) { - BoneIndices[iChan] = mpInput->ReadByte(); + index = static_cast(mpInput->ReadByte()); - if (BoneIndices[iChan] != 0xFF) + if (index != 0xFF) NumBoneChannels++; } @@ -83,42 +83,41 @@ void CAnimationLoader::ReadUncompressedANIM() if (mGame >= EGame::EchoesDemo) { - uint32 NumRotationIndices = mpInput->ReadLong(); + const uint32 NumRotationIndices = mpInput->ReadLong(); RotationIndices.resize(NumRotationIndices); - for (uint32 iRot = 0; iRot < NumRotationIndices; iRot++) + for (auto& index : RotationIndices) { - RotationIndices[iRot] = mpInput->ReadByte(); + index = static_cast(mpInput->ReadByte()); - if (RotationIndices[iRot] != 0xFF) + if (index != 0xFF) NumRotationChannels++; } } - else { // In MP1 every bone channel has a rotation, so just copy the valid channels from the bone channel list. RotationIndices.resize(NumBoneChannels); - for (uint32 iBone = 0; iBone < BoneIndices.size(); iBone++) + for (const auto index : BoneIndices) { - if (BoneIndices[iBone] != 0xFF) + if (index != 0xFF) { - RotationIndices[NumRotationChannels] = BoneIndices[iBone]; + RotationIndices[NumRotationChannels] = index; NumRotationChannels++; } } } // Translation channel indices - uint32 NumTransIndices = mpInput->ReadLong(); + const uint32 NumTransIndices = mpInput->ReadLong(); std::vector TransIndices(NumTransIndices); - for (uint32 iTrans = 0; iTrans < NumTransIndices; iTrans++) + for (auto& index : TransIndices) { - TransIndices[iTrans] = mpInput->ReadByte(); + index = static_cast(mpInput->ReadByte()); - if (TransIndices[iTrans] != 0xFF) + if (index != 0xFF) NumTranslationChannels++; } @@ -127,22 +126,22 @@ void CAnimationLoader::ReadUncompressedANIM() if (mGame >= EGame::EchoesDemo) { - uint32 NumScaleIndices = mpInput->ReadLong(); + const uint32 NumScaleIndices = mpInput->ReadLong(); ScaleIndices.resize(NumScaleIndices); - for (uint32 iScale = 0; iScale < NumScaleIndices; iScale++) + for (auto& index : ScaleIndices) { - ScaleIndices[iScale] = mpInput->ReadByte(); + index = static_cast(mpInput->ReadByte()); - if (ScaleIndices[iScale] != 0xFF) + if (index != 0xFF) NumScaleChannels++; } } // Set up bone channel info - for (uint32 iBone = 0, iChan = 0; iBone < NumBoneIndices; iBone++) + for (size_t iBone = 0, iChan = 0; iBone < NumBoneIndices; iBone++) { - uint8 BoneIdx = BoneIndices[iBone]; + const uint8 BoneIdx = BoneIndices[iBone]; if (BoneIdx != 0xFF) { @@ -166,11 +165,11 @@ void CAnimationLoader::ReadUncompressedANIM() mpInput->Seek(0x4, SEEK_CUR); // Skipping scale key count mpAnim->mScaleChannels.resize(NumScaleChannels); - for (uint32 iScale = 0; iScale < NumScaleChannels; iScale++) + for (size_t iScale = 0; iScale < NumScaleChannels; iScale++) { mpAnim->mScaleChannels[iScale].resize(mpAnim->mNumKeys); - for (uint32 iKey = 0; iKey < mpAnim->mNumKeys; iKey++) + for (size_t iKey = 0; iKey < mpAnim->mNumKeys; iKey++) mpAnim->mScaleChannels[iScale][iKey] = CVector3f(*mpInput); } } @@ -178,22 +177,22 @@ void CAnimationLoader::ReadUncompressedANIM() mpInput->Seek(0x4, SEEK_CUR); // Skipping rotation key count mpAnim->mRotationChannels.resize(NumRotationChannels); - for (uint32 iRot = 0; iRot < NumRotationChannels; iRot++) + for (size_t iRot = 0; iRot < NumRotationChannels; iRot++) { mpAnim->mRotationChannels[iRot].resize(mpAnim->mNumKeys); - for (uint32 iKey = 0; iKey < mpAnim->mNumKeys; iKey++) + for (size_t iKey = 0; iKey < mpAnim->mNumKeys; iKey++) mpAnim->mRotationChannels[iRot][iKey] = CQuaternion(*mpInput); } mpInput->Seek(0x4, SEEK_CUR); // Skipping translation key count mpAnim->mTranslationChannels.resize(NumTranslationChannels); - for (uint32 iTrans = 0; iTrans < NumTranslationChannels; iTrans++) + for (size_t iTrans = 0; iTrans < NumTranslationChannels; iTrans++) { mpAnim->mTranslationChannels[iTrans].resize(mpAnim->mNumKeys); - for (uint32 iKey = 0; iKey < mpAnim->mNumKeys; iKey++) + for (size_t iKey = 0; iKey < mpAnim->mNumKeys; iKey++) mpAnim->mTranslationChannels[iTrans][iKey] = CVector3f(*mpInput); } @@ -225,19 +224,20 @@ void CAnimationLoader::ReadCompressedANIM() mRotationDivisor = mpInput->ReadLong(); mTranslationMultiplier = mpInput->ReadFloat(); - if (mGame >= EGame::EchoesDemo) mScaleMultiplier = mpInput->ReadFloat(); - uint32 NumBoneChannels = mpInput->ReadLong(); + if (mGame >= EGame::EchoesDemo) + mScaleMultiplier = mpInput->ReadFloat(); + const uint32 NumBoneChannels = mpInput->ReadLong(); mpInput->Seek(0x4, SEEK_CUR); // Skip unknown value // Read key flags - uint32 NumKeys = mpInput->ReadLong(); + const uint32 NumKeys = mpInput->ReadLong(); mpAnim->mNumKeys = NumKeys; mKeyFlags.resize(NumKeys); { CBitStreamInWrapper BitStream(mpInput); - for (uint32 iBit = 0; iBit < NumKeys; iBit++) - mKeyFlags[iBit] = BitStream.ReadBit(); + for (auto& flag : mKeyFlags) + flag = BitStream.ReadBit(); } mpInput->Seek(mGame == EGame::Prime ? 0x8 : 0x4, SEEK_CUR); @@ -247,7 +247,7 @@ void CAnimationLoader::ReadCompressedANIM() mpAnim->mRotationChannels.resize(NumBoneChannels); mpAnim->mTranslationChannels.resize(NumBoneChannels); - for (uint32 iChan = 0; iChan < NumBoneChannels; iChan++) + for (size_t iChan = 0; iChan < NumBoneChannels; iChan++) { SCompressedChannel& rChan = mCompressedChannels[iChan]; rChan.BoneID = (mGame == EGame::Prime ? mpInput->ReadLong() : mpInput->ReadByte()); @@ -257,30 +257,36 @@ void CAnimationLoader::ReadCompressedANIM() if (rChan.NumRotationKeys > 0) { - for (uint32 iComp = 0; iComp < 3; iComp++) + for (size_t iComp = 0; iComp < 3; iComp++) { rChan.Rotation[iComp] = mpInput->ReadShort(); rChan.RotationBits[iComp] = mpInput->ReadByte(); } - mpAnim->mBoneInfo[rChan.BoneID].RotationChannelIdx = (uint8) iChan; + mpAnim->mBoneInfo[rChan.BoneID].RotationChannelIdx = static_cast(iChan); + } + else + { + mpAnim->mBoneInfo[rChan.BoneID].RotationChannelIdx = 0xFF; } - else mpAnim->mBoneInfo[rChan.BoneID].RotationChannelIdx = 0xFF; // Read translation parameters rChan.NumTranslationKeys = mpInput->ReadShort(); if (rChan.NumTranslationKeys > 0) { - for (uint32 iComp = 0; iComp < 3; iComp++) + for (size_t iComp = 0; iComp < 3; iComp++) { rChan.Translation[iComp] = mpInput->ReadShort(); rChan.TranslationBits[iComp] = mpInput->ReadByte(); } - mpAnim->mBoneInfo[rChan.BoneID].TranslationChannelIdx = (uint8) iChan; + mpAnim->mBoneInfo[rChan.BoneID].TranslationChannelIdx = static_cast(iChan); + } + else + { + mpAnim->mBoneInfo[rChan.BoneID].TranslationChannelIdx = 0xFF; } - else mpAnim->mBoneInfo[rChan.BoneID].TranslationChannelIdx = 0xFF; // Read scale parameters uint8 ScaleIdx = 0xFF; @@ -291,13 +297,13 @@ void CAnimationLoader::ReadCompressedANIM() if (rChan.NumScaleKeys > 0) { - for (uint32 iComp = 0; iComp < 3; iComp++) + for (size_t iComp = 0; iComp < 3; iComp++) { rChan.Scale[iComp] = mpInput->ReadShort(); rChan.ScaleBits[iComp] = mpInput->ReadByte(); } - ScaleIdx = (uint8) iChan; + ScaleIdx = static_cast(iChan); } } mpAnim->mBoneInfo[rChan.BoneID].ScaleChannelIdx = ScaleIdx; @@ -312,7 +318,7 @@ void CAnimationLoader::ReadCompressedAnimationData() CBitStreamInWrapper BitStream(mpInput); // Initialize - for (uint32 iChan = 0; iChan < mCompressedChannels.size(); iChan++) + for (size_t iChan = 0; iChan < mCompressedChannels.size(); iChan++) { SCompressedChannel& rChan = mCompressedChannels[iChan]; @@ -340,11 +346,11 @@ void CAnimationLoader::ReadCompressedAnimationData() } // Read keys - for (uint32 iKey = 0; iKey < mpAnim->mNumKeys - 1; iKey++) + for (size_t iKey = 0; iKey < mpAnim->mNumKeys - 1; iKey++) { - bool KeyPresent = mKeyFlags[iKey+1]; + const bool KeyPresent = mKeyFlags[iKey + 1]; - for (uint32 iChan = 0; iChan < mCompressedChannels.size(); iChan++) + for (size_t iChan = 0; iChan < mCompressedChannels.size(); iChan++) { SCompressedChannel& rChan = mCompressedChannels[iChan]; @@ -353,16 +359,16 @@ void CAnimationLoader::ReadCompressedAnimationData() { // Note if KeyPresent is false, this isn't the correct value of WSign. // However, we're going to recreate this key later via interpolation, so it doesn't matter what value we use here. - bool WSign = (KeyPresent ? BitStream.ReadBit() : false); + const bool WSign = (KeyPresent ? BitStream.ReadBit() : false); if (KeyPresent) { - rChan.Rotation[0] += (int16) BitStream.ReadBits(rChan.RotationBits[0]); - rChan.Rotation[1] += (int16) BitStream.ReadBits(rChan.RotationBits[1]); - rChan.Rotation[2] += (int16) BitStream.ReadBits(rChan.RotationBits[2]); + rChan.Rotation[0] += static_cast(BitStream.ReadBits(rChan.RotationBits[0])); + rChan.Rotation[1] += static_cast(BitStream.ReadBits(rChan.RotationBits[1])); + rChan.Rotation[2] += static_cast(BitStream.ReadBits(rChan.RotationBits[2])); } - CQuaternion Rotation = DequantizeRotation(WSign, rChan.Rotation[0], rChan.Rotation[1], rChan.Rotation[2]); + const CQuaternion Rotation = DequantizeRotation(WSign, rChan.Rotation[0], rChan.Rotation[1], rChan.Rotation[2]); mpAnim->mRotationChannels[iChan].push_back(Rotation); } @@ -371,12 +377,12 @@ void CAnimationLoader::ReadCompressedAnimationData() { if (KeyPresent) { - rChan.Translation[0] += (int16) BitStream.ReadBits(rChan.TranslationBits[0]); - rChan.Translation[1] += (int16) BitStream.ReadBits(rChan.TranslationBits[1]); - rChan.Translation[2] += (int16) BitStream.ReadBits(rChan.TranslationBits[2]); + rChan.Translation[0] += static_cast(BitStream.ReadBits(rChan.TranslationBits[0])); + rChan.Translation[1] += static_cast(BitStream.ReadBits(rChan.TranslationBits[1])); + rChan.Translation[2] += static_cast(BitStream.ReadBits(rChan.TranslationBits[2])); } - CVector3f Translate = CVector3f(rChan.Translation[0], rChan.Translation[1], rChan.Translation[2]) * mTranslationMultiplier; + const CVector3f Translate = CVector3f(rChan.Translation[0], rChan.Translation[1], rChan.Translation[2]) * mTranslationMultiplier; mpAnim->mTranslationChannels[iChan].push_back(Translate); } @@ -385,12 +391,12 @@ void CAnimationLoader::ReadCompressedAnimationData() { if (KeyPresent) { - rChan.Scale[0] += (int16) BitStream.ReadBits(rChan.ScaleBits[0]); - rChan.Scale[1] += (int16) BitStream.ReadBits(rChan.ScaleBits[1]); - rChan.Scale[2] += (int16) BitStream.ReadBits(rChan.ScaleBits[2]); + rChan.Scale[0] += static_cast(BitStream.ReadBits(rChan.ScaleBits[0])); + rChan.Scale[1] += static_cast(BitStream.ReadBits(rChan.ScaleBits[1])); + rChan.Scale[2] += static_cast(BitStream.ReadBits(rChan.ScaleBits[2])); } - CVector3f Scale = CVector3f(rChan.Scale[0], rChan.Scale[1], rChan.Scale[2]) * mScaleMultiplier; + const CVector3f Scale = CVector3f(rChan.Scale[0], rChan.Scale[1], rChan.Scale[2]) * mScaleMultiplier; mpAnim->mScaleChannels[iChan].push_back(Scale); } } @@ -399,47 +405,48 @@ void CAnimationLoader::ReadCompressedAnimationData() // Fill in missing keys uint32 NumMissedKeys = 0; - for (uint32 iKey = 0; iKey < mpAnim->mNumKeys; iKey++) + for (size_t iKey = 0; iKey < mpAnim->mNumKeys; iKey++) { if (!mKeyFlags[iKey]) + { NumMissedKeys++; - + } else if (NumMissedKeys > 0) { - uint32 FirstIndex = iKey - NumMissedKeys - 1; - uint32 LastIndex = iKey; - uint32 RelLastIndex = LastIndex - FirstIndex; + const uint32 FirstIndex = iKey - NumMissedKeys - 1; + const size_t LastIndex = iKey; + const uint32 RelLastIndex = LastIndex - FirstIndex; - for (uint32 iMissed = 0; iMissed < NumMissedKeys; iMissed++) + for (size_t iMissed = 0; iMissed < NumMissedKeys; iMissed++) { - uint32 KeyIndex = FirstIndex + iMissed + 1; - uint32 RelKeyIndex = (KeyIndex - FirstIndex); - float Interp = (float) RelKeyIndex / (float) RelLastIndex; + const size_t KeyIndex = FirstIndex + iMissed + 1; + const size_t RelKeyIndex = (KeyIndex - FirstIndex); + const float Interp = static_cast(RelKeyIndex) / static_cast(RelLastIndex); for (uint32 iChan = 0; iChan < mCompressedChannels.size(); iChan++) { - bool HasTranslationKeys = mCompressedChannels[iChan].NumTranslationKeys > 0; - bool HasRotationKeys = mCompressedChannels[iChan].NumRotationKeys > 0; - bool HasScaleKeys = mCompressedChannels[iChan].NumScaleKeys > 0; + const bool HasTranslationKeys = mCompressedChannels[iChan].NumTranslationKeys > 0; + const bool HasRotationKeys = mCompressedChannels[iChan].NumRotationKeys > 0; + const bool HasScaleKeys = mCompressedChannels[iChan].NumScaleKeys > 0; if (HasRotationKeys) { - CQuaternion Left = mpAnim->mRotationChannels[iChan][FirstIndex]; - CQuaternion Right = mpAnim->mRotationChannels[iChan][LastIndex]; + const CQuaternion Left = mpAnim->mRotationChannels[iChan][FirstIndex]; + const CQuaternion Right = mpAnim->mRotationChannels[iChan][LastIndex]; mpAnim->mRotationChannels[iChan][KeyIndex] = Left.Slerp(Right, Interp); } if (HasTranslationKeys) { - CVector3f Left = mpAnim->mTranslationChannels[iChan][FirstIndex]; - CVector3f Right = mpAnim->mTranslationChannels[iChan][LastIndex]; + const CVector3f Left = mpAnim->mTranslationChannels[iChan][FirstIndex]; + const CVector3f Right = mpAnim->mTranslationChannels[iChan][LastIndex]; mpAnim->mTranslationChannels[iChan][KeyIndex] = Math::Lerp(Left, Right, Interp); } if (HasScaleKeys) { - CVector3f Left = mpAnim->mScaleChannels[iChan][FirstIndex]; - CVector3f Right = mpAnim->mScaleChannels[iChan][LastIndex]; + const CVector3f Left = mpAnim->mScaleChannels[iChan][FirstIndex]; + const CVector3f Right = mpAnim->mScaleChannels[iChan][LastIndex]; mpAnim->mScaleChannels[iChan][KeyIndex] = Math::Lerp(Left, Right, Interp); } } @@ -450,15 +457,19 @@ void CAnimationLoader::ReadCompressedAnimationData() } } -CQuaternion CAnimationLoader::DequantizeRotation(bool Sign, int16 X, int16 Y, int16 Z) +CQuaternion CAnimationLoader::DequantizeRotation(bool Sign, int16 X, int16 Y, int16 Z) const { + const float Multiplier = Math::skHalfPi / static_cast(mRotationDivisor); + CQuaternion Out; - float Multiplier = Math::skHalfPi / (float) mRotationDivisor; - Out.X = sinf(X * Multiplier); - Out.Y = sinf(Y * Multiplier); - Out.Z = sinf(Z * Multiplier); - Out.W = Math::Sqrt( fmax(1.f - ((Out.X * Out.X) + (Out.Y * Out.Y) + (Out.Z * Out.Z)), 0.f) ); - if (Sign) Out.W = -Out.W; + Out.X = std::sinf(static_cast(X) * Multiplier); + Out.Y = std::sinf(static_cast(Y) * Multiplier); + Out.Z = std::sinf(static_cast(Z) * Multiplier); + Out.W = Math::Sqrt(std::fmax(1.f - ((Out.X * Out.X) + (Out.Y * Out.Y) + (Out.Z * Out.Z)), 0.f)); + + if (Sign) + Out.W = -Out.W; + return Out; } @@ -469,7 +480,7 @@ std::unique_ptr CAnimationLoader::LoadANIM(IInputStream& rANIM, CRes if (pEntry->Game() > EGame::Echoes) return std::make_unique(pEntry); - uint32 CompressionType = rANIM.ReadLong(); + const uint32 CompressionType = rANIM.ReadLong(); if (CompressionType != 0 && CompressionType != 2) { diff --git a/src/Core/Resource/Factory/CAnimationLoader.h b/src/Core/Resource/Factory/CAnimationLoader.h index 736d4162..3a6b2aea 100644 --- a/src/Core/Resource/Factory/CAnimationLoader.h +++ b/src/Core/Resource/Factory/CAnimationLoader.h @@ -40,7 +40,7 @@ class CAnimationLoader void ReadUncompressedANIM(); void ReadCompressedANIM(); void ReadCompressedAnimationData(); - CQuaternion DequantizeRotation(bool Sign, int16 X, int16 Y, int16 Z); + CQuaternion DequantizeRotation(bool Sign, int16 X, int16 Y, int16 Z) const; public: static std::unique_ptr LoadANIM(IInputStream& rANIM, CResourceEntry *pEntry);