载入中...
搜索中...
未找到
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
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
Inverse Kinematics module — wraps header-only sunxfancy/ik.hpp (FABRIK). Script: ik <- eve....
Definition IK.h:19
Solver2D * newSolver2D()
Definition IK.cpp:15
Skeleton3D * newSkeleton3D()
Definition IK.cpp:14
Solver3D * newSolver3D()
Definition IK.cpp:16
Skeleton2D * newSkeleton2D()
Definition IK.cpp:13
Script-facing 2D skeleton + pose state (ik::skeleton2d + ik::ecs2d). Bone indices are stable after ea...
Definition Skeleton2D.h:12
float getLength(int boneId) const
void setOrientation(int boneId, float x, float y)
int getRootId() const
Definition Skeleton2D.h:24
void setRotation(int boneId, float angle)
Local hinge angle (radians) relative to parent forward.
float totalLengthTo(int boneId) const
float getY(int boneId) const
int getChildCount(int boneId) const
bool hasConstraints(int boneId) const
int getBoneCount() const
float getX(int boneId) const
void initStraightPose(float rootX=0.f, float rootY=0.f)
float getRotation(int boneId) const
int getParent(int boneId) const
void clearConstraints(int boneId)
float getOrientationX(int boneId) const
float getOrientationY(int boneId) const
void setConstraints(int boneId, float minAngle, float maxAngle)
Joint limits for the single 2D hinge DOF.
void setLength(int boneId, float length)
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)
int getChild(int boneId, int childIndex) const
Script-facing 3D skeleton + pose state (ik::skeleton3d + ik::ecs3d). Local angles are yaw/pitch in th...
Definition Skeleton3D.h:11
void setPosition(int boneId, float x, float y, float z)
void setConstraints(int boneId, float minYaw, float minPitch, float maxYaw, float maxPitch)
float getLength(int boneId) const
void setLength(int boneId, float length)
int getChildCount(int boneId) const
int createBone(int parentId, float length=1.f)
float getRotationPitch(int boneId) const
void setRotation(int boneId, float yaw, float pitch)
void setOrientation(int boneId, float x, float y, float z)
void initStraightPose(float rootX=0.f, float rootY=0.f, float rootZ=0.f)
float totalLengthTo(int boneId) const
float getOrientationZ(int boneId) const
void clearConstraints(int boneId)
float getZ(int boneId) const
float getY(int boneId) const
float getOrientationY(int boneId) const
bool hasConstraints(int boneId) const
float getX(int boneId) const
int getChild(int boneId, int childIndex) const
float getOrientationX(int boneId) const
float getRotationYaw(int boneId) const
int getParent(int boneId) const
int getRootId() const
Definition Skeleton3D.h:22
int getBoneCount() const
FABRIK solver for Skeleton2D. Script type: Solver2D.
Definition Solver2D.h:11
int getMaxIterations() const
Definition Solver2D.cpp:24
int getTargetCount() const
Definition Solver2D.cpp:61
void setForce(float force)
Definition Solver2D.cpp:36
bool getKeepTrace() const
Definition Solver2D.cpp:14
void setMaxIterations(int iterations)
Definition Solver2D.cpp:16
void step(Skeleton2D *skeleton, float dt=1.f)
Definition Solver2D.cpp:85
void setTolerance(float tol)
Definition Solver2D.cpp:26
int getTraceSize() const
Definition Solver2D.cpp:160
void setKeepTrace(bool keep)
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
Definition Solver2D.cpp:41
void addTarget(int boneId, float x, float y, float weight=1.f)
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)
Definition Solver2D.cpp:113
float getTolerance() const
Definition Solver2D.cpp:34
float getInfluence() const
Definition Solver2D.cpp:50
FABRIK solver for Skeleton3D. Script type: Solver3D.
Definition Solver3D.h:11
void addTarget(int boneId, float x, float y, float z, float weight=1.f)
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
Definition Solver3D.cpp:203
float getPoleWeight() const
Definition Solver3D.cpp:184
float getForce() const
Definition Solver3D.cpp:41
void setTolerance(float tol)
Definition Solver3D.cpp:26
void clearTipRotation()
Definition Solver3D.cpp:196
bool solve(Skeleton3D *skeleton)
Definition Solver3D.cpp:65
void setInfluence(float influence)
Overall pose blend: 0 = keep the input pose, 1 = full solve (default).
Definition Solver3D.cpp:43
int getMaxIterations() const
Definition Solver3D.cpp:24
void stepChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId, float dt=1.f)
Definition Solver3D.cpp:118
void setForce(float force)
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
float getInfluence() const
Definition Solver3D.cpp:50
bool hasPole() const
Definition Solver3D.cpp:182
void setMaxIterations(int iterations)
Definition Solver3D.cpp:16
void step(Skeleton3D *skeleton, float dt=1.f)
Definition Solver3D.cpp:86
int getTargetCount() const
Definition Solver3D.cpp:61
int getTraceSize() const
Definition Solver3D.cpp:238
bool getKeepTrace() const
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
Definition Solver3D.cpp:34
void setKeepTrace(bool keep)
Definition Solver3D.cpp:9