载入中...
搜索中...
未找到
IK.cpp
浏览该文件的文档.
1#include "ik/IK.h"
2#include "ik/Skeleton2D.h"
3#include "ik/Skeleton3D.h"
4#include "ik/Solver2D.h"
5#include "ik/Solver3D.h"
6
7#include <simplesquirrel/simplesquirrel.hpp>
8
9namespace eve::ik {
10
12
15Solver2D *IK::newSolver2D() { return new Solver2D(); }
16Solver3D *IK::newSolver3D() { return new Solver3D(); }
17
18void IK::expose(ssq::Table &table) {
19 auto cls = table.addClass(name, IK::create, false);
20 expose(cls);
21
22 auto sk2 = table.addClass<Skeleton2D>(
23 "Skeleton2D", std::function<Skeleton2D *()>([]() -> Skeleton2D * { return nullptr; }),
24 true);
25 sk2.addFunc("createBone", &Skeleton2D::createBone);
26 sk2.addFunc("getBoneCount", &Skeleton2D::getBoneCount);
27 sk2.addFunc("getRootId", &Skeleton2D::getRootId);
28 sk2.addFunc("getParent", &Skeleton2D::getParent);
29 sk2.addFunc("getChildCount", &Skeleton2D::getChildCount);
30 sk2.addFunc("getChild", &Skeleton2D::getChild);
31 sk2.addFunc("getLength", &Skeleton2D::getLength);
32 sk2.addFunc("setLength", &Skeleton2D::setLength);
33 sk2.addFunc("setPosition", &Skeleton2D::setPosition);
34 sk2.addFunc("getX", &Skeleton2D::getX);
35 sk2.addFunc("getY", &Skeleton2D::getY);
36 sk2.addFunc("setOrientation", &Skeleton2D::setOrientation);
37 sk2.addFunc("getOrientationX", &Skeleton2D::getOrientationX);
38 sk2.addFunc("getOrientationY", &Skeleton2D::getOrientationY);
39 sk2.addFunc("setRotation", &Skeleton2D::setRotation);
40 sk2.addFunc("getRotation", &Skeleton2D::getRotation);
41 sk2.addFunc("setConstraints", &Skeleton2D::setConstraints);
42 sk2.addFunc("clearConstraints", &Skeleton2D::clearConstraints);
43 sk2.addFunc("hasConstraints", &Skeleton2D::hasConstraints);
44 sk2.addFunc("initStraightPose", &Skeleton2D::initStraightPose);
45 sk2.addFunc("bind", &Skeleton2D::bind);
46 sk2.addFunc("forwardKinematics", &Skeleton2D::forwardKinematics);
47 sk2.addFunc("updateRotations", &Skeleton2D::updateRotations);
48 sk2.addFunc("totalLengthTo", &Skeleton2D::totalLengthTo);
49
50 auto sk3 = table.addClass<Skeleton3D>(
51 "Skeleton3D", std::function<Skeleton3D *()>([]() -> Skeleton3D * { return nullptr; }),
52 true);
53 sk3.addFunc("createBone", &Skeleton3D::createBone);
54 sk3.addFunc("getBoneCount", &Skeleton3D::getBoneCount);
55 sk3.addFunc("getRootId", &Skeleton3D::getRootId);
56 sk3.addFunc("getParent", &Skeleton3D::getParent);
57 sk3.addFunc("getChildCount", &Skeleton3D::getChildCount);
58 sk3.addFunc("getChild", &Skeleton3D::getChild);
59 sk3.addFunc("getLength", &Skeleton3D::getLength);
60 sk3.addFunc("setLength", &Skeleton3D::setLength);
61 sk3.addFunc("setPosition", &Skeleton3D::setPosition);
62 sk3.addFunc("getX", &Skeleton3D::getX);
63 sk3.addFunc("getY", &Skeleton3D::getY);
64 sk3.addFunc("getZ", &Skeleton3D::getZ);
65 sk3.addFunc("setOrientation", &Skeleton3D::setOrientation);
66 sk3.addFunc("getOrientationX", &Skeleton3D::getOrientationX);
67 sk3.addFunc("getOrientationY", &Skeleton3D::getOrientationY);
68 sk3.addFunc("getOrientationZ", &Skeleton3D::getOrientationZ);
69 sk3.addFunc("setRotation", &Skeleton3D::setRotation);
70 sk3.addFunc("getRotationYaw", &Skeleton3D::getRotationYaw);
71 sk3.addFunc("getRotationPitch", &Skeleton3D::getRotationPitch);
72 sk3.addFunc("setConstraints", &Skeleton3D::setConstraints);
73 sk3.addFunc("clearConstraints", &Skeleton3D::clearConstraints);
74 sk3.addFunc("hasConstraints", &Skeleton3D::hasConstraints);
75 sk3.addFunc("initStraightPose", &Skeleton3D::initStraightPose);
76 sk3.addFunc("bind", &Skeleton3D::bind);
77 sk3.addFunc("forwardKinematics", &Skeleton3D::forwardKinematics);
78 sk3.addFunc("updateRotations", &Skeleton3D::updateRotations);
79 sk3.addFunc("totalLengthTo", &Skeleton3D::totalLengthTo);
80
81 auto s2 = table.addClass<Solver2D>(
82 "Solver2D", std::function<Solver2D *()>([]() -> Solver2D * { return nullptr; }), true);
83 s2.addFunc("setKeepTrace", &Solver2D::setKeepTrace);
84 s2.addFunc("getKeepTrace", &Solver2D::getKeepTrace);
85 s2.addFunc("setMaxIterations", &Solver2D::setMaxIterations);
86 s2.addFunc("getMaxIterations", &Solver2D::getMaxIterations);
87 s2.addFunc("setTolerance", &Solver2D::setTolerance);
88 s2.addFunc("getTolerance", &Solver2D::getTolerance);
89 s2.addFunc("setForce", &Solver2D::setForce);
90 s2.addFunc("getForce", &Solver2D::getForce);
91 s2.addFunc("setInfluence", &Solver2D::setInfluence);
92 s2.addFunc("getInfluence", &Solver2D::getInfluence);
93 s2.addFunc("clearTargets", &Solver2D::clearTargets);
94 s2.addFunc("addTarget", &Solver2D::addTarget);
95 s2.addFunc("getTargetCount", &Solver2D::getTargetCount);
96 s2.addFunc("solve", &Solver2D::solve);
97 s2.addFunc("step", &Solver2D::step);
98 s2.addFunc("solveChain", &Solver2D::solveChain);
99 s2.addFunc("stepChain", &Solver2D::stepChain);
100 s2.addFunc("getTraceSize", &Solver2D::getTraceSize);
101 s2.addFunc("clearTrace", &Solver2D::clearTrace);
102
103 auto s3 = table.addClass<Solver3D>(
104 "Solver3D", std::function<Solver3D *()>([]() -> Solver3D * { return nullptr; }), true);
105 s3.addFunc("setKeepTrace", &Solver3D::setKeepTrace);
106 s3.addFunc("getKeepTrace", &Solver3D::getKeepTrace);
107 s3.addFunc("setMaxIterations", &Solver3D::setMaxIterations);
108 s3.addFunc("getMaxIterations", &Solver3D::getMaxIterations);
109 s3.addFunc("setTolerance", &Solver3D::setTolerance);
110 s3.addFunc("getTolerance", &Solver3D::getTolerance);
111 s3.addFunc("setForce", &Solver3D::setForce);
112 s3.addFunc("getForce", &Solver3D::getForce);
113 s3.addFunc("setInfluence", &Solver3D::setInfluence);
114 s3.addFunc("getInfluence", &Solver3D::getInfluence);
115 s3.addFunc("clearTargets", &Solver3D::clearTargets);
116 s3.addFunc("addTarget", &Solver3D::addTarget);
117 s3.addFunc("getTargetCount", &Solver3D::getTargetCount);
118 s3.addFunc("solve", &Solver3D::solve);
119 s3.addFunc("step", &Solver3D::step);
120 s3.addFunc("solveChain", &Solver3D::solveChain);
121 s3.addFunc("stepChain", &Solver3D::stepChain);
122 s3.addFunc("setPole", &Solver3D::setPole);
123 s3.addFunc("clearPole", &Solver3D::clearPole);
124 s3.addFunc("hasPole", &Solver3D::hasPole);
125 s3.addFunc("getPoleWeight", &Solver3D::getPoleWeight);
126 s3.addFunc("setTipRotation", &Solver3D::setTipRotation);
127 s3.addFunc("clearTipRotation", &Solver3D::clearTipRotation);
128 s3.addFunc("getTipRotationWeight", &Solver3D::getTipRotationWeight);
129 s3.addFunc("getTraceSize", &Solver3D::getTraceSize);
130 s3.addFunc("clearTrace", &Solver3D::clearTrace);
131}
132
133void IK::expose(ssq::Class &cls) {
134 cls.addFunc("getName", &IK::getName);
135 cls.addFunc("newSkeleton2D", &IK::newSkeleton2D);
136 cls.addFunc("newSkeleton3D", &IK::newSkeleton3D);
137 cls.addFunc("newSolver2D", &IK::newSolver2D);
138 cls.addFunc("newSolver3D", &IK::newSolver3D);
139}
140
141} // namespace eve::ik
HSQOBJECT cls
Definition ECS.cpp:21
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
virtual std::string getName() const =0
Returns the name.
Inverse Kinematics module — wraps header-only sunxfancy/ik.hpp (FABRIK). Script: ik <- eve....
Definition IK.h:19
Solver2D * newSolver2D()
Creates a 2D FABRIK solver. @ownership Caller deletes.
Definition IK.cpp:15
Skeleton3D * newSkeleton3D()
Creates an empty 3D skeleton. @ownership Caller deletes.
Definition IK.cpp:14
Solver3D * newSolver3D()
Creates a 3D FABRIK solver. @ownership Caller deletes.
Definition IK.cpp:16
Skeleton2D * newSkeleton2D()
Creates an empty 2D skeleton. @ownership Caller deletes.
Definition IK.cpp:13
Script-facing 2D skeleton + pose state (ik::skeleton2d + ik::ecs2d). Bone indices are stable after ea...
Definition Skeleton2D.h:14
float getLength(int boneId) const
Rest length of the bone.
void setOrientation(int boneId, float x, float y)
Sets the bone forward orientation vector.
int getRootId() const
Root bone id (always 0).
Definition Skeleton2D.h:30
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 (ik::skeleton3d + ik::ecs3d). Local angles are yaw/pitch in th...
Definition Skeleton3D.h:13
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.
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 getRootId() const
Root bone id (always 0).
Definition Skeleton3D.h:29
int getBoneCount() const
Number of bones currently in the skeleton.
FABRIK solver for Skeleton2D. Script type: Solver2D.
Definition Solver2D.h:12
int getMaxIterations() const
Configured maximum FABRIK iterations.
Definition Solver2D.cpp:24
int getTargetCount() const
Number of configured targets.
Definition Solver2D.cpp:61
void setForce(float force)
Soft-IK blend toward the target (0 = hard IK).
Definition Solver2D.cpp:36
bool getKeepTrace() const
Whether solve tracing is enabled.
Definition Solver2D.cpp:14
void setMaxIterations(int iterations)
Maximum FABRIK iterations per solve.
Definition Solver2D.cpp:16
void step(Skeleton2D *skeleton, float dt=1.f)
Runs a time-scaled FABRIK step (dt scales iteration effort).
Definition Solver2D.cpp:85
void setTolerance(float tol)
End-effector distance tolerance for success.
Definition Solver2D.cpp:26
int getTraceSize() const
Number of recorded trace samples.
Definition Solver2D.cpp:160
void clearTargets()
Removes all IK targets.
Definition Solver2D.cpp:52
void setKeepTrace(bool keep)
When true, records intermediate solve samples.
Definition Solver2D.cpp:9
void setInfluence(float influence)
Overall pose blend: 0 = keep the input pose, 1 = full solve (default).
Definition Solver2D.cpp:43
float getForce() const
Configured soft-IK force.
Definition Solver2D.cpp:41
void addTarget(int boneId, float x, float y, float weight=1.f)
Adds an end-effector target for a bone.
Definition Solver2D.cpp:54
bool solve(Skeleton2D *skeleton)
Returns true if every target is within tolerance.
Definition Solver2D.cpp:65
bool solveChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId)
FABRIK solve restricted to the bone chain rootBoneId..tipBoneId. The chain root stays pinned and bone...
Definition Solver2D.cpp:104
void stepChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId, float dt=1.f)
Time-scaled chain-scoped FABRIK step.
Definition Solver2D.cpp:113
float getTolerance() const
Configured end-effector tolerance.
Definition Solver2D.cpp:34
void clearTrace()
Clears recorded solve trace samples.
Definition Solver2D.cpp:162
float getInfluence() const
Pose blend weight (0 = keep input, 1 = full solve).
Definition Solver2D.cpp:50
FABRIK solver for Skeleton3D. Script type: Solver3D.
Definition Solver3D.h:12
void addTarget(int boneId, float x, float y, float z, float weight=1.f)
Adds an end-effector target for a bone.
Definition Solver3D.cpp:54
void setTipRotation(int boneId, float yaw, float pitch, float weight)
Tip orientation override: after solving, blends the tip bone's local yaw/pitch toward the given angle...
Definition Solver3D.cpp:186
float getTipRotationWeight() const
Configured tip rotation blend weight.
Definition Solver3D.cpp:203
float getPoleWeight() const
Configured pole-vector weight in [0,1].
Definition Solver3D.cpp:184
float getForce() const
Configured soft-IK force.
Definition Solver3D.cpp:41
void setTolerance(float tol)
End-effector distance tolerance for success.
Definition Solver3D.cpp:26
void clearTrace()
Clears recorded solve trace samples.
Definition Solver3D.cpp:240
void clearTipRotation()
Clears tip orientation override.
Definition Solver3D.cpp:196
bool solve(Skeleton3D *skeleton)
Runs FABRIK until tolerance or max iterations.
Definition Solver3D.cpp:65
void setInfluence(float influence)
Overall pose blend: 0 = keep the input pose, 1 = full solve (default).
Definition Solver3D.cpp:43
void clearPole()
Disables the pole-vector hint.
Definition Solver3D.cpp:177
int getMaxIterations() const
Configured maximum FABRIK iterations.
Definition Solver3D.cpp:24
void stepChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId, float dt=1.f)
Time-scaled chain-scoped FABRIK step.
Definition Solver3D.cpp:118
void setForce(float force)
Soft-IK blend toward the target (0 = hard IK).
Definition Solver3D.cpp:36
bool solveChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId)
FABRIK solve restricted to the bone chain rootBoneId..tipBoneId. The chain root stays pinned and bone...
Definition Solver3D.cpp:106
void clearTargets()
Removes all IK targets.
Definition Solver3D.cpp:52
float getInfluence() const
Pose blend weight (0 = keep input, 1 = full solve).
Definition Solver3D.cpp:50
bool hasPole() const
True when a pole vector is configured.
Definition Solver3D.cpp:182
void setMaxIterations(int iterations)
Maximum FABRIK iterations per solve.
Definition Solver3D.cpp:16
void step(Skeleton3D *skeleton, float dt=1.f)
Runs a time-scaled FABRIK step (dt scales iteration effort).
Definition Solver3D.cpp:86
int getTargetCount() const
Number of configured targets.
Definition Solver3D.cpp:61
int getTraceSize() const
Number of recorded trace samples.
Definition Solver3D.cpp:238
bool getKeepTrace() const
Whether solve tracing is enabled.
Definition Solver3D.cpp:14
void setPole(float x, float y, float z, float weight)
Pole vector (magnet / hint): pulls the middle joints of a chain toward a point so the limb bends in t...
Definition Solver3D.cpp:171
float getTolerance() const
Configured end-effector tolerance.
Definition Solver3D.cpp:34
void setKeepTrace(bool keep)
When true, records intermediate solve samples.
Definition Solver3D.cpp:9
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:65