<commit_before>#include "AnimationController.hpp"

#include "../Animation/Skeleton.hpp"
#include "../Animation/AnimationClip.hpp"
#include <Utility/Log.hpp>
#include "glm/gtc/quaternion.hpp"
#include <cstring>

using namespace Component;

AnimationController::AnimationController() {
    // Push back identity matrices.
    for (unsigned int i = 0; i < 50; ++i) {
        bones.push_back(glm::mat4(1.0f));
    }
}

Json::Value AnimationController::Save() const {
    Json::Value component;

    if (controller != nullptr)
        component["animationController"] = controller->path + controller->name;

    if (skeleton != nullptr)
        component["skeleton"] = skeleton->path + skeleton->name;

    return component;
}

void Component::AnimationController::UpdateAnimation(float deltaTime) {
    if (skeleton == nullptr || controller == nullptr)
        return;

    if (skeleton != nullptr && bones.size() != skeleton->skeletonBones.size()) {
        bones.clear();
        bones.resize(skeleton->skeletonBones.size());
    }

    // Select the first node in the animation controller.
    // This will work as the entry point for the animation.
    if (activeAction1 == nullptr) {
        if (controller->animationNodes.size() > 0) {
            Animation::AnimationController::AnimationAction* tmpAction = dynamic_cast<Animation::AnimationController::AnimationAction*>(controller->animationNodes[0]);
            if (tmpAction != nullptr)
                activeAction1 = tmpAction;
            else
                return;
        } else
            return;
    }

    if (activeTransition == nullptr) {
        for (uint32_t i = 0; i < activeAction1->numOutputSlots; ++i) {
            Animation::AnimationController::AnimationTransition* tmpTransition = dynamic_cast<Animation::AnimationController::AnimationTransition*>(controller->animationNodes[activeAction1->outputIndex[i]]);
            if (tmpTransition != nullptr) {
                // If the bool in the boolMap is true then set this to the activeTransition.
                if (controller->boolMap[tmpTransition->transitionBoolIndex]) {
                    activeTransition = tmpTransition;
                    activeTransition->transitionProcess = 0.0f;
                    break;
                }
            }
        }
    }

    if (activeTransition == nullptr)
        Animate(deltaTime, activeAction1);
    else {
        activeTransition->transitionProcess += deltaTime;
        if (activeTransition->transitionProcess > activeTransition->transitionTime) {
            activeTransition->transitionProcess = 0.0f;
            activeTransition = nullptr;

            // Set action 1 to action 2.
            activeAction1 = activeAction2;
            activeAction2 = nullptr;

            Animate(deltaTime, activeAction1);
        } else {
            Animate(deltaTime, activeAction1);
            Animate(deltaTime, activeAction2);
            Interpolate(deltaTime);
        }
    }
}

void AnimationController::Animate(float deltaTime, Animation::AnimationController::AnimationAction* action) {
    Animation::AnimationClip::Animation* anim = action->animationClip->animation;
    std::size_t size = skeleton->skeletonBones.size() > anim->numBones ? anim->numBones : skeleton->skeletonBones.size();

    anim->currentFrame += deltaTime * 30.0f;
    if (anim->currentFrame > anim->length) {
        anim->currentFrame = 0;

        for (unsigned int i = 0; i < size; ++i)
            anim->bones[i].currentKeyIndex = 0;
    }

    skeleton->skeletonBones[0]->globalTx = skeleton->skeletonBones[0]->localTx;
    bones[0] = skeleton->skeletonBones[0]->globalTx * skeleton->skeletonBones[0]->inversed;

    for (std::size_t i = 1; i < size; ++i) {
        Animation::AnimationClip::Bone* bone = &anim->bones[i];

        // Loop if the animation is very fast.
        while ((float)bone->rotationKeys[bone->currentKeyIndex + 1] < anim->currentFrame) {
            ++bone->currentKeyIndex;
        }

        float interpolation = (anim->currentFrame - (float)bone->rotationKeys[bone->currentKeyIndex]) / ((float)bone->rotationKeys[bone->currentKeyIndex + 1] - (float)bone->rotationKeys[bone->currentKeyIndex]);

        // Clamp interpolation.
        if (interpolation > 0.999f)
            interpolation = 0.999f;
        else if (interpolation < 0.001f)
            interpolation = 0.001f;

        // Convert to quaternion to interpolate animation then back to matrix.
        glm::quat rotation1 = glm::quat_cast(bone->rotations[bone->currentKeyIndex]);
        glm::quat rotation2 = glm::quat_cast(bone->rotations[bone->currentKeyIndex + 1]);
        glm::quat finalRot = glm::slerp(rotation1, rotation2, interpolation);

        glm::mat4 matrixRot = glm::mat4(finalRot);

        if (skeleton->skeletonBones[i]->parentId == -1)
            continue;

        skeleton->skeletonBones[i]->globalTx = skeleton->skeletonBones[skeleton->skeletonBones[i]->parentId]->globalTx * skeleton->skeletonBones[i]->localTx * matrixRot;     //skeleton->skeletonBones[i]->localTx:
        bones[i] = skeleton->skeletonBones[i]->globalTx * skeleton->skeletonBones[i]->inversed;
    }<commit_msg>Removed brackets.<commit_after>#include "AnimationController.hpp"

#include "../Animation/Skeleton.hpp"
#include "../Animation/AnimationClip.hpp"
#include <Utility/Log.hpp>
#include "glm/gtc/quaternion.hpp"
#include <cstring>

using namespace Component;

AnimationController::AnimationController() {
    // Push back identity matrices.
    for (unsigned int i = 0; i < 50; ++i) {
        bones.push_back(glm::mat4(1.0f));
    }
}

Json::Value AnimationController::Save() const {
    Json::Value component;

    if (controller != nullptr)
        component["animationController"] = controller->path + controller->name;

    if (skeleton != nullptr)
        component["skeleton"] = skeleton->path + skeleton->name;

    return component;
}

void Component::AnimationController::UpdateAnimation(float deltaTime) {
    if (skeleton == nullptr || controller == nullptr)
        return;

    if (skeleton != nullptr && bones.size() != skeleton->skeletonBones.size()) {
        bones.clear();
        bones.resize(skeleton->skeletonBones.size());
    }

    // Select the first node in the animation controller.
    // This will work as the entry point for the animation.
    if (activeAction1 == nullptr) {
        if (controller->animationNodes.size() > 0) {
            Animation::AnimationController::AnimationAction* tmpAction = dynamic_cast<Animation::AnimationController::AnimationAction*>(controller->animationNodes[0]);
            if (tmpAction != nullptr)
                activeAction1 = tmpAction;
            else
                return;
        } else
            return;
    }

    if (activeTransition == nullptr) {
        for (uint32_t i = 0; i < activeAction1->numOutputSlots; ++i) {
            Animation::AnimationController::AnimationTransition* tmpTransition = dynamic_cast<Animation::AnimationController::AnimationTransition*>(controller->animationNodes[activeAction1->outputIndex[i]]);
            if (tmpTransition != nullptr) {
                // If the bool in the boolMap is true then set this to the activeTransition.
                if (controller->boolMap[tmpTransition->transitionBoolIndex]) {
                    activeTransition = tmpTransition;
                    activeTransition->transitionProcess = 0.0f;
                    break;
                }
            }
        }
    }

    if (activeTransition == nullptr)
        Animate(deltaTime, activeAction1);
    else {
        activeTransition->transitionProcess += deltaTime;
        if (activeTransition->transitionProcess > activeTransition->transitionTime) {
            activeTransition->transitionProcess = 0.0f;
            activeTransition = nullptr;

            // Set action 1 to action 2.
            activeAction1 = activeAction2;
            activeAction2 = nullptr;

            Animate(deltaTime, activeAction1);
        } else {
            Animate(deltaTime, activeAction1);
            Animate(deltaTime, activeAction2);
            Interpolate(deltaTime);
        }
    }
}

void AnimationController::Animate(float deltaTime, Animation::AnimationController::AnimationAction* action) {
    Animation::AnimationClip::Animation* anim = action->animationClip->animation;
    std::size_t size = skeleton->skeletonBones.size() > anim->numBones ? anim->numBones : skeleton->skeletonBones.size();

    anim->currentFrame += deltaTime * 30.0f;
    if (anim->currentFrame > anim->length) {
        anim->currentFrame = 0;

        for (unsigned int i = 0; i < size; ++i)
            anim->bones[i].currentKeyIndex = 0;
    }

    skeleton->skeletonBones[0]->globalTx = skeleton->skeletonBones[0]->localTx;
    bones[0] = skeleton->skeletonBones[0]->globalTx * skeleton->skeletonBones[0]->inversed;

    for (std::size_t i = 1; i < size; ++i) {
        Animation::AnimationClip::Bone* bone = &anim->bones[i];

        // Loop if the animation is very fast.
        while ((float)bone->rotationKeys[bone->currentKeyIndex + 1] < anim->currentFrame)
            ++bone->currentKeyIndex;

        float interpolation = (anim->currentFrame - (float)bone->rotationKeys[bone->currentKeyIndex]) / ((float)bone->rotationKeys[bone->currentKeyIndex + 1] - (float)bone->rotationKeys[bone->currentKeyIndex]);

        // Clamp interpolation.
        if (interpolation > 0.999f)
            interpolation = 0.999f;
        else if (interpolation < 0.001f)
            interpolation = 0.001f;

        // Convert to quaternion to interpolate animation then back to matrix.
        glm::quat rotation1 = glm::quat_cast(bone->rotations[bone->currentKeyIndex]);
        glm::quat rotation2 = glm::quat_cast(bone->rotations[bone->currentKeyIndex + 1]);
        glm::quat finalRot = glm::slerp(rotation1, rotation2, interpolation);

        glm::mat4 matrixRot = glm::mat4(finalRot);

        if (skeleton->skeletonBones[i]->parentId == -1)
            continue;

        skeleton->skeletonBones[i]->globalTx = skeleton->skeletonBones[skeleton->skeletonBones[i]->parentId]->globalTx * skeleton->skeletonBones[i]->localTx * matrixRot;     //skeleton->skeletonBones[i]->localTx:
        bones[i] = skeleton->skeletonBones[i]->globalTx * skeleton->skeletonBones[i]->inversed;
    }
{'fraction_non_alphanumeric': 0.127108784176847, 'fraction_numerical': 0.012798138452588714, 'mean_word_length': 2.8821979676326683, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 159, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7310909', 'n_tokens_mistral': 2961, 'n_tokens_neox': 2787, 'n_words': 573}