12 impl_->ensureSorted();
13 impl_->ensureStateSize();
18void Skeleton2D::requireBone(
int boneId)
const {
19 if (!impl_->boneAt(boneId)) {
20 throw Exception(
"Skeleton2D: invalid bone id %d", boneId);
25 impl_->ensureSorted();
28 throw Exception(
"Skeleton2D.createBone: length must be > 0");
32 impl_->sk.topologicalSort();
33 impl_->ensureStateSize();
34 return static_cast<int>(child->id);
38 const_cast<Skeleton2D *
>(
this)->impl_->ensureSorted();
39 return static_cast<int>(impl_->sk.bones().size());
44 ::ik::bone2d *
b = impl_->boneAt(boneId);
45 if (!
b->parent)
return -1;
46 return static_cast<int>(
b->parent->id);
51 return static_cast<int>(impl_->boneAt(boneId)->children.size());
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");
60 return static_cast<int>(
b->children[
static_cast<size_t>(childIndex)]->
id);
65 return impl_->boneAt(boneId)->length;
71 throw Exception(
"Skeleton2D.setLength: root length is always 0");
74 throw Exception(
"Skeleton2D.setLength: length must be > 0");
76 impl_->boneAt(boneId)->setLength(
length);
81 impl_->ensureStateSize();
82 impl_->boneAt(boneId)->position(impl_->state) = {
x,
y};
87 const_cast<Skeleton2D *
>(
this)->impl_->ensureStateSize();
88 return impl_->boneAt(boneId)->position(impl_->state)[0];
93 const_cast<Skeleton2D *
>(
this)->impl_->ensureStateSize();
94 return impl_->boneAt(boneId)->position(impl_->state)[1];
99 impl_->ensureStateSize();
100 impl_->boneAt(boneId)->orientation(impl_->state) = {
x,
y};
105 const_cast<Skeleton2D *
>(
this)->impl_->ensureStateSize();
106 return impl_->boneAt(boneId)->orientation(impl_->state)[0];
111 const_cast<Skeleton2D *
>(
this)->impl_->ensureStateSize();
112 return impl_->boneAt(boneId)->orientation(impl_->state)[1];
117 impl_->ensureStateSize();
118 impl_->boneAt(boneId)->rotation(impl_->state) = {
angle};
123 const_cast<Skeleton2D *
>(
this)->impl_->ensureStateSize();
124 return impl_->boneAt(boneId)->rotation(impl_->state)[0];
129 if (minAngle > maxAngle) {
130 throw Exception(
"Skeleton2D.setConstraints: minAngle must be <= maxAngle");
132 impl_->boneAt(boneId)->setConstraints(::ik::vec<float, 1>{minAngle}, ::ik::vec<float, 1>{maxAngle});
137 impl_->boneAt(boneId)->clearConstraints();
142 return impl_->boneAt(boneId)->constrained;
146 impl_->ensureSorted();
147 impl_->ensureStateSize();
148 impl_->sk.init_straight_pose(impl_->state, ::ik::vec2{rootX, rootY});
152 impl_->ensureSorted();
153 impl_->ensureStateSize();
154 impl_->sk.bind(impl_->state);
158 impl_->ensureSorted();
159 impl_->ensureStateSize();
160 impl_->sk.forward_kinematics(impl_->state);
164 impl_->ensureSorted();
165 impl_->ensureStateSize();
166 impl_->sk.update_rotations(impl_->state);
171 return impl_->sk.total_length_to(impl_->boneAt(boneId));
179 impl_->ensureSorted();
180 impl_->ensureStateSize();
185void Skeleton3D::requireBone(
int boneId)
const {
186 if (!impl_->boneAt(boneId)) {
187 throw Exception(
"Skeleton3D: invalid bone id %d", boneId);
192 impl_->ensureSorted();
195 throw Exception(
"Skeleton3D.createBone: length must be > 0");
199 impl_->sk.topologicalSort();
200 impl_->ensureStateSize();
201 return static_cast<int>(child->id);
205 const_cast<Skeleton3D *
>(
this)->impl_->ensureSorted();
206 return static_cast<int>(impl_->sk.bones().size());
211 ::ik::bone3d *
b = impl_->boneAt(boneId);
212 if (!
b->parent)
return -1;
213 return static_cast<int>(
b->parent->id);
218 return static_cast<int>(impl_->boneAt(boneId)->children.size());
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");
227 return static_cast<int>(
b->children[
static_cast<size_t>(childIndex)]->
id);
232 return impl_->boneAt(boneId)->length;
238 throw Exception(
"Skeleton3D.setLength: root length is always 0");
241 throw Exception(
"Skeleton3D.setLength: length must be > 0");
243 impl_->boneAt(boneId)->setLength(
length);
248 impl_->ensureStateSize();
249 impl_->boneAt(boneId)->position(impl_->state) = {
x,
y,
z};
254 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
255 return impl_->boneAt(boneId)->position(impl_->state)[0];
260 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
261 return impl_->boneAt(boneId)->position(impl_->state)[1];
266 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
267 return impl_->boneAt(boneId)->position(impl_->state)[2];
272 impl_->ensureStateSize();
273 impl_->boneAt(boneId)->orientation(impl_->state) = {
x,
y,
z};
278 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
279 return impl_->boneAt(boneId)->orientation(impl_->state)[0];
284 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
285 return impl_->boneAt(boneId)->orientation(impl_->state)[1];
290 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
291 return impl_->boneAt(boneId)->orientation(impl_->state)[2];
296 impl_->ensureStateSize();
297 impl_->boneAt(boneId)->rotation(impl_->state) = {
yaw,
pitch};
302 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
303 return impl_->boneAt(boneId)->rotation(impl_->state)[0];
308 const_cast<Skeleton3D *
>(
this)->impl_->ensureStateSize();
309 return impl_->boneAt(boneId)->rotation(impl_->state)[1];
314 if (minYaw > maxYaw || minPitch > maxPitch) {
315 throw Exception(
"Skeleton3D.setConstraints: min must be <= max per axis");
317 impl_->boneAt(boneId)->setConstraints(::ik::vec2{minYaw, minPitch}, ::ik::vec2{maxYaw, maxPitch});
322 impl_->boneAt(boneId)->clearConstraints();
327 return impl_->boneAt(boneId)->constrained;
331 impl_->ensureSorted();
332 impl_->ensureStateSize();
333 impl_->sk.init_straight_pose(impl_->state, ::ik::vec3{rootX, rootY, rootZ});
337 impl_->ensureSorted();
338 impl_->ensureStateSize();
339 impl_->sk.bind(impl_->state);
343 impl_->ensureSorted();
344 impl_->ensureStateSize();
345 impl_->sk.forward_kinematics(impl_->state);
349 impl_->ensureSorted();
350 impl_->ensureStateSize();
351 impl_->sk.update_rotations(impl_->state);
356 return impl_->sk.total_length_to(impl_->boneAt(boneId));
const ColourScope * parent
EVENGINE_API_FOUNDATION public API.
Script-facing 2D skeleton + pose state. Bone indices are stable after each createBone / topological r...
~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.
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.