载入中...
搜索中...
未找到
Skeleton2D.cpp
浏览该文件的文档.
1#include "ik/Skeleton2D.h"
2#include "ik/Skeleton3D.h"
4
5#include "common/Exception.h"
6
7#include <utility>
8
9namespace eve::ik {
10
11Skeleton2D::Skeleton2D() : impl_(std::make_unique<Impl>()) {
12 impl_->ensureSorted();
13 impl_->ensureStateSize();
14}
15
16Skeleton2D::~Skeleton2D() = default;
17
18void Skeleton2D::requireBone(int boneId) const {
19 if (!impl_->boneAt(boneId)) {
20 throw Exception("Skeleton2D: invalid bone id %d", boneId);
21 }
22}
23
25 impl_->ensureSorted();
26 requireBone(parentId);
27 if (length <= 0.f) {
28 throw Exception("Skeleton2D.createBone: length must be > 0");
29 }
30 ::ik::bone2d *parent = impl_->boneAt(parentId);
31 ::ik::bone2d *child = parent->createBone(length);
32 impl_->sk.topologicalSort();
33 impl_->ensureStateSize();
34 return static_cast<int>(child->id);
35}
36
38 const_cast<Skeleton2D *>(this)->impl_->ensureSorted();
39 return static_cast<int>(impl_->sk.bones().size());
40}
41
42int Skeleton2D::getParent(int boneId) const {
43 requireBone(boneId);
44 ::ik::bone2d *b = impl_->boneAt(boneId);
45 if (!b->parent) return -1;
46 return static_cast<int>(b->parent->id);
47}
48
49int Skeleton2D::getChildCount(int boneId) const {
50 requireBone(boneId);
51 return static_cast<int>(impl_->boneAt(boneId)->children.size());
52}
53
54int Skeleton2D::getChild(int boneId, int childIndex) const {
55 requireBone(boneId);
56 ::ik::bone2d *b = impl_->boneAt(boneId);
57 if (childIndex < 0 || static_cast<size_t>(childIndex) >= b->children.size()) {
58 throw Exception("Skeleton2D.getChild: childIndex out of range");
59 }
60 return static_cast<int>(b->children[static_cast<size_t>(childIndex)]->id);
61}
62
63float Skeleton2D::getLength(int boneId) const {
64 requireBone(boneId);
65 return impl_->boneAt(boneId)->length;
66}
67
68void Skeleton2D::setLength(int boneId, float length) {
69 requireBone(boneId);
70 if (boneId == 0) {
71 throw Exception("Skeleton2D.setLength: root length is always 0");
72 }
73 if (length <= 0.f) {
74 throw Exception("Skeleton2D.setLength: length must be > 0");
75 }
76 impl_->boneAt(boneId)->setLength(length);
77}
78
79void Skeleton2D::setPosition(int boneId, float x, float y) {
80 requireBone(boneId);
81 impl_->ensureStateSize();
82 impl_->boneAt(boneId)->position(impl_->state) = {x, y};
83}
84
85float Skeleton2D::getX(int boneId) const {
86 requireBone(boneId);
87 const_cast<Skeleton2D *>(this)->impl_->ensureStateSize();
88 return impl_->boneAt(boneId)->position(impl_->state)[0];
89}
90
91float Skeleton2D::getY(int boneId) const {
92 requireBone(boneId);
93 const_cast<Skeleton2D *>(this)->impl_->ensureStateSize();
94 return impl_->boneAt(boneId)->position(impl_->state)[1];
95}
96
97void Skeleton2D::setOrientation(int boneId, float x, float y) {
98 requireBone(boneId);
99 impl_->ensureStateSize();
100 impl_->boneAt(boneId)->orientation(impl_->state) = {x, y};
101}
102
103float Skeleton2D::getOrientationX(int boneId) const {
104 requireBone(boneId);
105 const_cast<Skeleton2D *>(this)->impl_->ensureStateSize();
106 return impl_->boneAt(boneId)->orientation(impl_->state)[0];
107}
108
109float Skeleton2D::getOrientationY(int boneId) const {
110 requireBone(boneId);
111 const_cast<Skeleton2D *>(this)->impl_->ensureStateSize();
112 return impl_->boneAt(boneId)->orientation(impl_->state)[1];
113}
114
115void Skeleton2D::setRotation(int boneId, float angle) {
116 requireBone(boneId);
117 impl_->ensureStateSize();
118 impl_->boneAt(boneId)->rotation(impl_->state) = {angle};
119}
120
121float Skeleton2D::getRotation(int boneId) const {
122 requireBone(boneId);
123 const_cast<Skeleton2D *>(this)->impl_->ensureStateSize();
124 return impl_->boneAt(boneId)->rotation(impl_->state)[0];
125}
126
127void Skeleton2D::setConstraints(int boneId, float minAngle, float maxAngle) {
128 requireBone(boneId);
129 if (minAngle > maxAngle) {
130 throw Exception("Skeleton2D.setConstraints: minAngle must be <= maxAngle");
131 }
132 impl_->boneAt(boneId)->setConstraints(::ik::vec<float, 1>{minAngle}, ::ik::vec<float, 1>{maxAngle});
133}
134
136 requireBone(boneId);
137 impl_->boneAt(boneId)->clearConstraints();
138}
139
140bool Skeleton2D::hasConstraints(int boneId) const {
141 requireBone(boneId);
142 return impl_->boneAt(boneId)->constrained;
143}
144
145void Skeleton2D::initStraightPose(float rootX, float rootY) {
146 impl_->ensureSorted();
147 impl_->ensureStateSize();
148 impl_->sk.init_straight_pose(impl_->state, ::ik::vec2{rootX, rootY});
149}
150
152 impl_->ensureSorted();
153 impl_->ensureStateSize();
154 impl_->sk.bind(impl_->state);
155}
156
158 impl_->ensureSorted();
159 impl_->ensureStateSize();
160 impl_->sk.forward_kinematics(impl_->state);
161}
162
164 impl_->ensureSorted();
165 impl_->ensureStateSize();
166 impl_->sk.update_rotations(impl_->state);
167}
168
169float Skeleton2D::totalLengthTo(int boneId) const {
170 requireBone(boneId);
171 return impl_->sk.total_length_to(impl_->boneAt(boneId));
172}
173
174// ---------------------------------------------------------------------------
175// Skeleton3D
176// ---------------------------------------------------------------------------
177
178Skeleton3D::Skeleton3D() : impl_(std::make_unique<Impl>()) {
179 impl_->ensureSorted();
180 impl_->ensureStateSize();
181}
182
183Skeleton3D::~Skeleton3D() = default;
184
185void Skeleton3D::requireBone(int boneId) const {
186 if (!impl_->boneAt(boneId)) {
187 throw Exception("Skeleton3D: invalid bone id %d", boneId);
188 }
189}
190
192 impl_->ensureSorted();
193 requireBone(parentId);
194 if (length <= 0.f) {
195 throw Exception("Skeleton3D.createBone: length must be > 0");
196 }
197 ::ik::bone3d *parent = impl_->boneAt(parentId);
198 ::ik::bone3d *child = parent->createBone(length);
199 impl_->sk.topologicalSort();
200 impl_->ensureStateSize();
201 return static_cast<int>(child->id);
202}
203
205 const_cast<Skeleton3D *>(this)->impl_->ensureSorted();
206 return static_cast<int>(impl_->sk.bones().size());
207}
208
209int Skeleton3D::getParent(int boneId) const {
210 requireBone(boneId);
211 ::ik::bone3d *b = impl_->boneAt(boneId);
212 if (!b->parent) return -1;
213 return static_cast<int>(b->parent->id);
214}
215
216int Skeleton3D::getChildCount(int boneId) const {
217 requireBone(boneId);
218 return static_cast<int>(impl_->boneAt(boneId)->children.size());
219}
220
221int Skeleton3D::getChild(int boneId, int childIndex) const {
222 requireBone(boneId);
223 ::ik::bone3d *b = impl_->boneAt(boneId);
224 if (childIndex < 0 || static_cast<size_t>(childIndex) >= b->children.size()) {
225 throw Exception("Skeleton3D.getChild: childIndex out of range");
226 }
227 return static_cast<int>(b->children[static_cast<size_t>(childIndex)]->id);
228}
229
230float Skeleton3D::getLength(int boneId) const {
231 requireBone(boneId);
232 return impl_->boneAt(boneId)->length;
233}
234
235void Skeleton3D::setLength(int boneId, float length) {
236 requireBone(boneId);
237 if (boneId == 0) {
238 throw Exception("Skeleton3D.setLength: root length is always 0");
239 }
240 if (length <= 0.f) {
241 throw Exception("Skeleton3D.setLength: length must be > 0");
242 }
243 impl_->boneAt(boneId)->setLength(length);
244}
245
246void Skeleton3D::setPosition(int boneId, float x, float y, float z) {
247 requireBone(boneId);
248 impl_->ensureStateSize();
249 impl_->boneAt(boneId)->position(impl_->state) = {x, y, z};
250}
251
252float Skeleton3D::getX(int boneId) const {
253 requireBone(boneId);
254 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
255 return impl_->boneAt(boneId)->position(impl_->state)[0];
256}
257
258float Skeleton3D::getY(int boneId) const {
259 requireBone(boneId);
260 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
261 return impl_->boneAt(boneId)->position(impl_->state)[1];
262}
263
264float Skeleton3D::getZ(int boneId) const {
265 requireBone(boneId);
266 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
267 return impl_->boneAt(boneId)->position(impl_->state)[2];
268}
269
270void Skeleton3D::setOrientation(int boneId, float x, float y, float z) {
271 requireBone(boneId);
272 impl_->ensureStateSize();
273 impl_->boneAt(boneId)->orientation(impl_->state) = {x, y, z};
274}
275
276float Skeleton3D::getOrientationX(int boneId) const {
277 requireBone(boneId);
278 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
279 return impl_->boneAt(boneId)->orientation(impl_->state)[0];
280}
281
282float Skeleton3D::getOrientationY(int boneId) const {
283 requireBone(boneId);
284 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
285 return impl_->boneAt(boneId)->orientation(impl_->state)[1];
286}
287
288float Skeleton3D::getOrientationZ(int boneId) const {
289 requireBone(boneId);
290 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
291 return impl_->boneAt(boneId)->orientation(impl_->state)[2];
292}
293
294void Skeleton3D::setRotation(int boneId, float yaw, float pitch) {
295 requireBone(boneId);
296 impl_->ensureStateSize();
297 impl_->boneAt(boneId)->rotation(impl_->state) = {yaw, pitch};
298}
299
300float Skeleton3D::getRotationYaw(int boneId) const {
301 requireBone(boneId);
302 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
303 return impl_->boneAt(boneId)->rotation(impl_->state)[0];
304}
305
306float Skeleton3D::getRotationPitch(int boneId) const {
307 requireBone(boneId);
308 const_cast<Skeleton3D *>(this)->impl_->ensureStateSize();
309 return impl_->boneAt(boneId)->rotation(impl_->state)[1];
310}
311
312void Skeleton3D::setConstraints(int boneId, float minYaw, float minPitch, float maxYaw, float maxPitch) {
313 requireBone(boneId);
314 if (minYaw > maxYaw || minPitch > maxPitch) {
315 throw Exception("Skeleton3D.setConstraints: min must be <= max per axis");
316 }
317 impl_->boneAt(boneId)->setConstraints(::ik::vec2{minYaw, minPitch}, ::ik::vec2{maxYaw, maxPitch});
318}
319
321 requireBone(boneId);
322 impl_->boneAt(boneId)->clearConstraints();
323}
324
325bool Skeleton3D::hasConstraints(int boneId) const {
326 requireBone(boneId);
327 return impl_->boneAt(boneId)->constrained;
328}
329
330void Skeleton3D::initStraightPose(float rootX, float rootY, float rootZ) {
331 impl_->ensureSorted();
332 impl_->ensureStateSize();
333 impl_->sk.init_straight_pose(impl_->state, ::ik::vec3{rootX, rootY, rootZ});
334}
335
337 impl_->ensureSorted();
338 impl_->ensureStateSize();
339 impl_->sk.bind(impl_->state);
340}
341
343 impl_->ensureSorted();
344 impl_->ensureStateSize();
345 impl_->sk.forward_kinematics(impl_->state);
346}
347
349 impl_->ensureSorted();
350 impl_->ensureStateSize();
351 impl_->sk.update_rotations(impl_->state);
352}
353
354float Skeleton3D::totalLengthTo(int boneId) const {
355 requireBone(boneId);
356 return impl_->sk.total_length_to(impl_->boneAt(boneId));
357}
358
359} // namespace eve::ik
double z
double y
double x
float yaw
Definition BushMesh.cpp:37
std::uint64_t parentId
float length
const ColourScope * parent
MeleePoint3 b
Definition MeleeHit.cpp:41
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Script-facing 2D skeleton + pose state. Bone indices are stable after each createBone / topological r...
Definition Skeleton2D.h:16
~Skeleton2D()
Releases skeleton and pose state.
float getLength(int boneId) const
Rest length of the bone.
Skeleton2D()
Creates an empty 2D skeleton with a root bone.
void setOrientation(int boneId, float x, float y)
Sets the bone forward orientation vector.
void setRotation(int boneId, float angle)
Local hinge angle (radians) relative to parent forward.
float totalLengthTo(int boneId) const
Sum of bone lengths from root to boneId inclusive.
void bind()
Binds the current pose as the rest pose.
void updateRotations()
Derives local rotations from world positions.
float getY(int boneId) const
World-space Y of the bone (or component).
int getChildCount(int boneId) const
Number of direct children of boneId.
bool hasConstraints(int boneId) const
True if the bone has joint limits.
int getBoneCount() const
Number of bones currently in the skeleton.
float getX(int boneId) const
World-space X of the bone (or component).
void forwardKinematics()
Updates world positions from local rotations.
void initStraightPose(float rootX=0.f, float rootY=0.f)
Initializes a straight chain pose from the root.
float getRotation(int boneId) const
Local hinge angle in radians.
int getParent(int boneId) const
Parent bone id, or -1 for the root.
void clearConstraints(int boneId)
Removes joint limits from the bone.
float getOrientationX(int boneId) const
Orientation vector X.
float getOrientationY(int boneId) const
Orientation vector Y.
void setConstraints(int boneId, float minAngle, float maxAngle)
Joint limits for the single 2D hinge DOF.
void setLength(int boneId, float length)
Sets the rest length of the bone.
int createBone(int parentId, float length=1.f)
Create a child bone under parentId; returns new bone id.
void setPosition(int boneId, float x, float y)
Sets the world-space bone position.
int getChild(int boneId, int childIndex) const
Child bone id at childIndex under boneId.
Script-facing 3D skeleton + pose state. Local angles are yaw/pitch in the parent bone frame.
Definition Skeleton3D.h:15
void setPosition(int boneId, float x, float y, float z)
Sets the world-space bone position.
void setConstraints(int boneId, float minYaw, float minPitch, float maxYaw, float maxPitch)
Sets joint angle limits for the bone.
float getLength(int boneId) const
Rest length of the bone.
void setLength(int boneId, float length)
Sets the rest length of the bone.
void forwardKinematics()
Updates world positions from local rotations.
int getChildCount(int boneId) const
Number of direct children of boneId.
~Skeleton3D()
Releases skeleton and pose state.
Skeleton3D()
Creates an empty 3D skeleton with a root bone.
void bind()
Binds the current pose as the rest pose.
int createBone(int parentId, float length=1.f)
Create a child bone under parentId; returns new bone id.
float getRotationPitch(int boneId) const
Local pitch in radians.
void setRotation(int boneId, float yaw, float pitch)
Sets local hinge/yaw-pitch rotation (radians).
void setOrientation(int boneId, float x, float y, float z)
Sets the bone forward orientation vector.
void initStraightPose(float rootX=0.f, float rootY=0.f, float rootZ=0.f)
Initializes a straight chain pose from the root.
float totalLengthTo(int boneId) const
Sum of bone lengths from root to boneId inclusive.
float getOrientationZ(int boneId) const
Orientation vector Z.
void clearConstraints(int boneId)
Removes joint limits from the bone.
float getZ(int boneId) const
World-space Z of the bone (or component).
float getY(int boneId) const
World-space Y of the bone (or component).
float getOrientationY(int boneId) const
Orientation vector Y.
bool hasConstraints(int boneId) const
True if the bone has joint limits.
void updateRotations()
Derives local rotations from world positions.
float getX(int boneId) const
World-space X of the bone (or component).
int getChild(int boneId, int childIndex) const
Child bone id at childIndex under boneId.
float getOrientationX(int boneId) const
Orientation vector X.
float getRotationYaw(int boneId) const
Local yaw in radians.
int getParent(int boneId) const
Parent bone id, or -1 for the root.
int getBoneCount() const
Number of bones currently in the skeleton.