Antkeeper  0.0.1
skeleton.cpp
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 
21 #include <stdexcept>
22 
23 skeleton::skeleton(std::size_t bone_count):
24  m_bone_parents(bone_count, 0),
25  m_rest_pose(*this)
26 {}
27 
29  skeleton(0)
30 {}
31 
33 {
34  m_rest_pose.update();
35 }
36 
37 bone_index_type skeleton::add_bones(std::size_t bone_count)
38 {
39  const bone_index_type first_bone_index = static_cast<bone_index_type>(m_bone_parents.size());
40 
41  m_bone_parents.resize(m_bone_parents.size() + bone_count, 0);
42  m_rest_pose.resize();
43 
44  return first_bone_index;
45 }
46 
48 {
50 
52 
53  return bone_index;
54 }
55 
57 {
58  m_bone_parents.clear();
59  m_bone_map.clear();
60  m_rest_pose.resize();
61 }
62 
64 {
65  const auto [iterator, inserted] = m_pose_map.emplace(name, *this);
66 
67  if (!inserted)
68  {
69  throw std::invalid_argument("Duplicate pose name");
70  }
71 
72  return iterator->second;
73 }
74 
76 {
77  if (!m_pose_map.erase(name))
78  {
79  throw std::invalid_argument("Pose not found");
80  }
81 }
82 
84 {
85  m_pose_map.clear();
86 }
87 
89 {
90  if (child_index < parent_index)
91  {
92  throw std::invalid_argument("Child bone index precedes parent bone index");
93  }
94 
95  m_bone_parents[child_index] = parent_index;
96 }
97 
99 {
100  if (auto i = m_bone_map.find(name); i != m_bone_map.end())
101  {
102  if (i->second != index)
103  {
104  throw std::invalid_argument("Duplicate bone name");
105  }
106  }
107  else
108  {
109  m_bone_map[name] = index;
110  }
111 }
112 
113 std::optional<bone_index_type> skeleton::get_bone_index(hash::fnv1a32_t name) const
114 {
115  if (auto i = m_bone_map.find(name); i != m_bone_map.end())
116  {
117  return i->second;
118  }
119 
120  return std::nullopt;
121 }
122 
124 {
125  if (auto i = m_pose_map.find(name); i != m_pose_map.end())
126  {
127  return &i->second;
128  }
129 
130  return nullptr;
131 }
132 
134 {
135  if (auto i = m_pose_map.find(name); i != m_pose_map.end())
136  {
137  return &i->second;
138  }
139 
140  return nullptr;
141 }
std::uint16_t bone_index_type
Bone index type.
Definition: bone.hpp:31
Animatable skeleton pose.
void resize()
Updates the number of bones in the rest pose if the skeleton has been modified.
Definition: rest-pose.cpp:50
void update(bone_index_type first_index, std::size_t bone_count) override
Updates a subset of the pose after one or more relative transforms have been changed.
Definition: rest-pose.cpp:30
Skeletal animation skeleton.
Definition: skeleton.hpp:35
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
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
32-bit FNV-1a hash value.
Definition: fnv1a.hpp:117