Antkeeper  0.0.1
skeleton.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_ANIMATION_SKELETON_HPP
21 #define ANTKEEPER_ANIMATION_SKELETON_HPP
22 
27 #include <unordered_map>
28 #include <vector>
29 #include <optional>
30 
34 class skeleton
35 {
36 public:
42  explicit skeleton(std::size_t bone_count);
43 
45  skeleton();
46 
50  void update_rest_pose();
51 
59  bone_index_type add_bones(std::size_t bone_count);
60 
67  {
68  return add_bones(1);
69  }
70 
81 
85  void remove_bones();
86 
97 
105  void remove_pose(hash::fnv1a32_t name);
106 
110  void remove_poses();
111 
122  void set_bone_parent(bone_index_type child_index, bone_index_type parent_index);
123 
130  inline void set_bone_transform(bone_index_type index, const bone_transform_type& transform)
131  {
132  m_rest_pose.set_relative_transform(index, transform);
133  }
134 
144 
148  [[nodiscard]] inline std::size_t get_bone_count() const noexcept
149  {
150  return m_bone_parents.size();
151  }
152 
160  [[nodiscard]] inline bone_index_type get_bone_parent(bone_index_type child_index) const
161  {
162  return m_bone_parents[child_index];
163  }
164 
172  [[nodiscard]] std::optional<bone_index_type> get_bone_index(hash::fnv1a32_t name) const;
173 
182  [[nodiscard]] const animation_pose* get_pose(hash::fnv1a32_t name) const;
183  [[nodiscard]] animation_pose* get_pose(hash::fnv1a32_t name);
185 
187  [[nodiscard]] inline const rest_pose& get_rest_pose() const noexcept
188  {
189  return m_rest_pose;
190  }
191 
192 private:
194  std::vector<bone_index_type> m_bone_parents;
195 
197  rest_pose m_rest_pose;
198 
200  std::unordered_map<hash::fnv1a32_t, bone_index_type> m_bone_map;
201 
203  std::unordered_map<hash::fnv1a32_t, animation_pose> m_pose_map;
204 };
205 
206 #endif // ANTKEEPER_ANIMATION_SKELETON_HPP
std::uint16_t bone_index_type
Bone index type.
Definition: bone.hpp:31
Animatable skeleton pose.
void set_relative_transform(bone_index_type index, const bone_transform_type &transform)
Sets the relative transform describing a bone pose.
Definition: pose.hpp:65
Skeleton rest pose.
Definition: rest-pose.hpp:29
Skeletal animation skeleton.
Definition: skeleton.hpp:35
void set_bone_transform(bone_index_type index, const bone_transform_type &transform)
Sets the transform of a bone, relative to its parent bone.
Definition: skeleton.hpp:130
bone_index_type get_bone_parent(bone_index_type child_index) const
Returns the index of the parent of a bone.
Definition: skeleton.hpp:160
void remove_poses()
Removes all poses from the skeleton, excluding the rest pose.
Definition: skeleton.cpp:83
std::optional< bone_index_type > get_bone_index(hash::fnv1a32_t name) const
Finds the index of a bone from the bone's name.
Definition: skeleton.cpp:113
bone_index_type add_bones(std::size_t bone_count)
Add one or more bones to the skeleton.
Definition: skeleton.cpp:37
void update_rest_pose()
Updates the rest pose of the skeleton.
Definition: skeleton.cpp:32
void remove_bones()
Removes all bones from the skeleton.
Definition: skeleton.cpp:56
const animation_pose * get_pose(hash::fnv1a32_t name) const
Finds a pose from the poses's name.
Definition: skeleton.cpp:123
void set_bone_parent(bone_index_type child_index, bone_index_type parent_index)
Sets the parent of a bone.
Definition: skeleton.cpp:88
std::size_t get_bone_count() const noexcept
Returns the number of bones in the skeleton.
Definition: skeleton.hpp:148
void set_bone_name(bone_index_type index, hash::fnv1a32_t name)
Sets the name of a bone.
Definition: skeleton.cpp:98
bone_index_type add_bone()
Adds a single bone to the skeleton.
Definition: skeleton.hpp:66
skeleton()
Constructs an empty skeleton.
Definition: skeleton.cpp:28
animation_pose & add_pose(hash::fnv1a32_t name)
Adds a pose to the skeleton.
Definition: skeleton.cpp:63
void remove_pose(hash::fnv1a32_t name)
Removes a pose from the skeleton.
Definition: skeleton.cpp:75
const rest_pose & get_rest_pose() const noexcept
Returns the skeleton's rest pose.
Definition: skeleton.hpp:187
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117