# class cogs::Bone

## Overview

    #include <Bone.h>

    class Bone {
    public:
        // enums

        enum PropertyType;

        // structs

        struct Cache;

        // methods

        uint32_t GetId() const;
        std::string GetName() const;
        glm::vec3 GetHead() const;
        glm::vec3 GetTail() const;
        void SetTail(glm::vec3 tail);
        float GetProperty(const PropertyType prop) const;
        float GetProperty(const PropertyType prop, const float default_value) const;
        void SetProperty(const PropertyType prop, const float value);
        bool HasProperty(const PropertyType prop) const;
        float GetLength() const;
        void SetLength(const float new_length);
        glm::vec3 GetDirection() const;
        glm::mat4 GetBasisMat4() const;
        glm::mat3 GetBasisMat3() const;
        glm::vec3 GetBasisVector(const geom::Axis axis) const;
        glm::mat4 GetGlobalTransformHead() const;
        glm::mat4 GetGlobalTransformTail() const;
        const BoneTransform& GetTransform() const;
        void Roll(const float& angle);
        void LocalRoll(const float& angle);
        void Pitch(const float& angle);
        void Yaw(const float& angle);
        bool IsRoot() const;
        bool IsLeaf() const;
        Bone* GetParent() const;
        uint32_t GetChildrenCount() const;
        std::vector<Bone*> GetChildren() const;
        std::vector<Bone*> GetHeadNeigbours() const;
        glm::ivec4 GetHeadNeigbourIds() const;
        std::vector<Bone*> GetTailNeigbours() const;
        glm::ivec4 GetTailNeigbourIds() const;
        void Rotate(const glm::quat& rotation);
        void RotateToDirection(glm::vec3 new_bone_direction);
        glm::vec3 ToVector() const;
        geom::LineSegment3 ToLineSegment() const;
        geom::Line3 ToLine() const;
        geom::ConicalCapsule ToConicalCapsule() const;
        void SortChildrenByBone(const Bone& bone);

    protected:
        // fields

        friend Skeleton;
        uint32_t id_;
        std::string caption_;
        Skeleton* skeleton_owner_ = nullptr;
        Bone* parent_ = nullptr;
        std::vector<Bone*> children_;
        std::unordered_map<PropertyType, float> properties_;
        BoneTransform transform_;
        BoneTransform bindpose_transform_;
        struct cogs::Bone::Cache cache_;

        // construction

        Bone(const uint32_t id, const std::string caption);

        // methods

        glm::mat4 GetParentTransform();
        void ApplyBindpose();
        void ApplyCurrentPoseAsBindpose();
        void UpdateTransformCache();
        void UpdateTransformCacheRecursive();
        void ResetBindposeTransform(const BoneTransform& new_transform);
        void ResetBindposeTransformRecursive(const BoneTransform& new_transform);
    };

## Detailed Documentation

### Methods

    uint32_t GetId() const

Returns bone index in skeleton structure.

    std::string GetName() const

Returns bone name.

    glm::vec3 GetHead() const

Position of bone start point.

    glm::vec3 GetTail() const

Position of bone end point.

    void SetTail(glm::vec3 tail)

Rotates bone by shortest possible path and changes its length, so that its tail lays in specified position.

    float GetProperty(const PropertyType prop) const

Returns the specific bone property. If the property do not exist, returns zero.

    float GetProperty(const PropertyType prop, const float default_value) const

Returns the specific bone property. If the property do not exist, returns default value.

    void SetProperty(const PropertyType prop, const float value)

Sets the specific bone property. If the property do not exist yet, creates new property record.

    bool HasProperty(const PropertyType prop) const

Checks if the property exist.

    float GetLength() const

Distance from head to tail.

    void SetLength(const float new_length)

Set distance from head to tail.

    glm::vec3 GetDirection() const

Normalized direction of bone. Equivalent to GetBasisVector(geom::Axis::X).

    glm::mat4 GetBasisMat4() const

Returns bone basis vectors in a matrix representation.

    glm::mat3 GetBasisMat3() const

Returns bone basis vectors in a matrix representation.

    glm::vec3 GetBasisVector(const geom::Axis axis) const

Specified vector component of bone basis.

    glm::mat4 GetGlobalTransformHead() const

Returns actual transformation from global space to bone space at head position.

    glm::mat4 GetGlobalTransformTail() const

Returns actual transformation from global space to bone space at tail position.

    const BoneTransform& GetTransform() const

Returns local transformation of bone according to parent.

    void Roll(const float& angle)

Rotates by angle in radians right-handily around X basis axis.

    void LocalRoll(const float& angle)

Rotates by angle in radians right-handily around X basis axis.

    void Pitch(const float& angle)

Rotates by angle in radians right-handily around Y basis axis.

    void Yaw(const float& angle)

Rotates by angle in radians right-handily around Z basis axis.

    bool IsRoot() const

Check whether the bone is first in skeleton hierarchy = has no parent bone.

    bool IsLeaf() const

Check whether the bone is last in skeleton hierarchy.

    Bone* GetParent() const

Hierarchical parent bone of this node.

    uint32_t GetChildrenCount() const

Number of children which this bone has.

    std::vector<Bone*> GetChildren() const

Array of this bone’s children.

    std::vector<Bone*> GetHeadNeigbours() const

Array of bones which are connected by head. If bone has parent, it is always first.

    glm::ivec4 GetHeadNeigbourIds() const

Array of bones ids which are connected by head. If bone has parent, it is always first.

    std::vector<Bone*> GetTailNeigbours() const

Array of bones which are connected by tail.

    glm::ivec4 GetTailNeigbourIds() const

Array of bone ids which are connected by tail.

    void Rotate(const glm::quat& rotation)

Recursively applies rotation on bone.

    void RotateToDirection(glm::vec3 new_bone_direction)

Recursively rotates bone to the specified direction, by a shortest rotation possible.

    glm::vec3 ToVector() const

Converts bone to oriented vector with length equal to bone’s length.

    geom::LineSegment3 ToLineSegment() const

Converts bone to line segment.

    geom::Line3 ToLine() const

Converts bone to non ending line with direction of a bone.

    geom::ConicalCapsule ToConicalCapsule() const

Converts bone with its radiuses to conical capsule.

    void SortChildrenByBone(const Bone& bone)

Recursively, changes order of child bones, so that they match order of specified bone.