载入中...
搜索中...
未找到
Solver2D.cpp
浏览该文件的文档.
1#include "ik/Solver2D.h"
2#include "ik/Skeleton2D.h"
3#include "ik/Skeleton3D.h"
4#include "ik/Solver3D.h"
6
7#include "common/Exception.h"
8
9#include "ik.hpp"
10
11#include <utility>
12
13namespace eve::ik {
14
16 ::ik::solver2d solver;
17 bool keepTrace = false;
18 int maxIterations = 16;
19 float tolerance = 1e-3f;
20 float force = 0.f;
21};
22
23Solver2D::Solver2D() : impl_(std::make_unique<Impl>()) {}
24Solver2D::~Solver2D() = default;
25
26void Solver2D::setKeepTrace(bool keep) {
27 impl_->keepTrace = keep;
28 impl_->solver.setKeepTrace(keep);
29}
30
31bool Solver2D::getKeepTrace() const { return impl_->keepTrace; }
32
34 if (iterations < 0) {
35 throw Exception("Solver2D.setMaxIterations: iterations must be >= 0");
36 }
37 impl_->maxIterations = iterations;
38 impl_->solver.setMaxIterations(static_cast<unsigned>(iterations));
39}
40
41int Solver2D::getMaxIterations() const { return impl_->maxIterations; }
42
43void Solver2D::setTolerance(float tol) {
44 if (tol < 0.f) {
45 throw Exception("Solver2D.setTolerance: tolerance must be >= 0");
46 }
47 impl_->tolerance = tol;
48 impl_->solver.setTolerance(tol);
49}
50
51float Solver2D::getTolerance() const { return impl_->tolerance; }
52
54 impl_->force = force;
55 impl_->solver.setForce(force);
56}
57
58float Solver2D::getForce() const { return impl_->force; }
59
60void Solver2D::setInfluence(float influence) {
61 if (influence < 0.f || influence > 1.f) {
62 throw Exception("Solver2D.setInfluence: influence must be in [0, 1]");
63 }
64 impl_->solver.setInfluence(influence);
65}
66
67float Solver2D::getInfluence() const { return impl_->solver.getInfluence(); }
68
69void Solver2D::clearTargets() { impl_->solver.clearTargets(); }
70
71void Solver2D::addTarget(int boneId, float x, float y, float weight) {
72 if (boneId < 0) {
73 throw Exception("Solver2D.addTarget: boneId must be >= 0");
74 }
75 impl_->solver.addTarget(static_cast<unsigned>(boneId), ::ik::vec2{x, y}, weight);
76}
77
78int Solver2D::getTargetCount() const { return static_cast<int>(impl_->solver.targets().size()); }
79
80bool Solver2D::solve(Skeleton2D *skeleton) {
81 if (!skeleton) {
82 throw Exception("Solver2D.solve: skeleton is null");
83 }
84 skeleton->impl_->ensureSorted();
85 skeleton->impl_->ensureStateSize();
86 return impl_->solver.solve(skeleton->impl_->sk, skeleton->impl_->state);
87}
88
89void Solver2D::step(Skeleton2D *skeleton, float dt) {
90 if (!skeleton) {
91 throw Exception("Solver2D.step: skeleton is null");
92 }
93 skeleton->impl_->ensureSorted();
94 skeleton->impl_->ensureStateSize();
95 impl_->solver.step(skeleton->impl_->sk, skeleton->impl_->state, dt);
96}
97
98void Solver2D::requireChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId) const {
99 if (!skeleton) {
100 throw Exception("Solver2D.solveChain: skeleton is null");
101 }
102 if (rootBoneId < 0 || tipBoneId < 0) {
103 throw Exception("Solver2D.solveChain: bone ids must be >= 0");
104 }
105 const int count = skeleton->getBoneCount();
106 if (rootBoneId >= count || tipBoneId >= count) {
107 throw Exception("Solver2D.solveChain: bone id out of range");
108 }
109 if (rootBoneId == tipBoneId) {
110 throw Exception("Solver2D.solveChain: root and tip must differ");
111 }
112 int b = tipBoneId;
113 bool found = false;
114 while (b >= 0) {
115 if (b == rootBoneId) {
116 found = true;
117 break;
118 }
119 b = skeleton->getParent(b);
120 }
121 if (!found) {
122 throw Exception("Solver2D.solveChain: tipBoneId is not a descendant of rootBoneId");
123 }
124}
125
126bool Solver2D::solveChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId) {
127 requireChain(skeleton, rootBoneId, tipBoneId);
128 skeleton->impl_->ensureSorted();
129 skeleton->impl_->ensureStateSize();
130 return impl_->solver.solveChain(skeleton->impl_->sk, skeleton->impl_->state, static_cast<unsigned>(rootBoneId),
131 static_cast<unsigned>(tipBoneId));
132}
133
134void Solver2D::stepChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId, float dt) {
135 requireChain(skeleton, rootBoneId, tipBoneId);
136 skeleton->impl_->ensureSorted();
137 skeleton->impl_->ensureStateSize();
138 impl_->solver.stepChain(skeleton->impl_->sk, skeleton->impl_->state, static_cast<unsigned>(rootBoneId),
139 static_cast<unsigned>(tipBoneId), dt);
140}
141
142int Solver2D::getTraceSize() const { return static_cast<int>(impl_->solver.traceSize()); }
143
144void Solver2D::clearTrace() { impl_->solver.clearTrace(); }
145
146// ---------------------------------------------------------------------------
147// Solver3D
148// ---------------------------------------------------------------------------
149
151 ::ik::solver3d solver;
152 bool keepTrace = false;
154 float tolerance = 1e-3f;
155 float force = 0.f;
156};
157
158Solver3D::Solver3D() : impl_(std::make_unique<Impl>()) {}
159Solver3D::~Solver3D() = default;
160
161void Solver3D::setKeepTrace(bool keep) {
162 impl_->keepTrace = keep;
163 impl_->solver.setKeepTrace(keep);
164}
165
166bool Solver3D::getKeepTrace() const { return impl_->keepTrace; }
167
169 if (iterations < 0) {
170 throw Exception("Solver3D.setMaxIterations: iterations must be >= 0");
171 }
172 impl_->maxIterations = iterations;
173 impl_->solver.setMaxIterations(static_cast<unsigned>(iterations));
174}
175
176int Solver3D::getMaxIterations() const { return impl_->maxIterations; }
177
178void Solver3D::setTolerance(float tol) {
179 if (tol < 0.f) {
180 throw Exception("Solver3D.setTolerance: tolerance must be >= 0");
181 }
182 impl_->tolerance = tol;
183 impl_->solver.setTolerance(tol);
184}
185
186float Solver3D::getTolerance() const { return impl_->tolerance; }
187
189 impl_->force = force;
190 impl_->solver.setForce(force);
191}
192
193float Solver3D::getForce() const { return impl_->force; }
194
195void Solver3D::setInfluence(float influence) {
196 if (influence < 0.f || influence > 1.f) {
197 throw Exception("Solver3D.setInfluence: influence must be in [0, 1]");
198 }
199 impl_->solver.setInfluence(influence);
200}
201
202float Solver3D::getInfluence() const { return impl_->solver.getInfluence(); }
203
204void Solver3D::clearTargets() { impl_->solver.clearTargets(); }
205
206void Solver3D::addTarget(int boneId, float x, float y, float z, float weight) {
207 if (boneId < 0) {
208 throw Exception("Solver3D.addTarget: boneId must be >= 0");
209 }
210 impl_->solver.addTarget(static_cast<unsigned>(boneId), ::ik::vec3{x, y, z}, weight);
211}
212
213int Solver3D::getTargetCount() const { return static_cast<int>(impl_->solver.targets().size()); }
214
216 if (!skeleton) {
217 throw Exception("Solver3D.solve: skeleton is null");
218 }
219 skeleton->impl_->ensureSorted();
220 skeleton->impl_->ensureStateSize();
221 return impl_->solver.solve(skeleton->impl_->sk, skeleton->impl_->state);
222}
223
224void Solver3D::step(Skeleton3D *skeleton, float dt) {
225 if (!skeleton) {
226 throw Exception("Solver3D.step: skeleton is null");
227 }
228 skeleton->impl_->ensureSorted();
229 skeleton->impl_->ensureStateSize();
230 impl_->solver.step(skeleton->impl_->sk, skeleton->impl_->state, dt);
231}
232
233void Solver3D::requireChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId) const {
234 if (!skeleton) {
235 throw Exception("Solver3D.solveChain: skeleton is null");
236 }
237 if (rootBoneId < 0 || tipBoneId < 0) {
238 throw Exception("Solver3D.solveChain: bone ids must be >= 0");
239 }
240 const int count = skeleton->getBoneCount();
241 if (rootBoneId >= count || tipBoneId >= count) {
242 throw Exception("Solver3D.solveChain: bone id out of range");
243 }
244 if (rootBoneId == tipBoneId) {
245 throw Exception("Solver3D.solveChain: root and tip must differ");
246 }
247 int b = tipBoneId;
248 bool found = false;
249 while (b >= 0) {
250 if (b == rootBoneId) {
251 found = true;
252 break;
253 }
254 b = skeleton->getParent(b);
255 }
256 if (!found) {
257 throw Exception("Solver3D.solveChain: tipBoneId is not a descendant of rootBoneId");
258 }
259}
260
261bool Solver3D::solveChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId) {
262 requireChain(skeleton, rootBoneId, tipBoneId);
263 skeleton->impl_->ensureSorted();
264 skeleton->impl_->ensureStateSize();
265 return impl_->solver.solveChain(skeleton->impl_->sk, skeleton->impl_->state, static_cast<unsigned>(rootBoneId),
266 static_cast<unsigned>(tipBoneId));
267}
268
269void Solver3D::stepChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId, float dt) {
270 requireChain(skeleton, rootBoneId, tipBoneId);
271 skeleton->impl_->ensureSorted();
272 skeleton->impl_->ensureStateSize();
273 impl_->solver.stepChain(skeleton->impl_->sk, skeleton->impl_->state, static_cast<unsigned>(rootBoneId),
274 static_cast<unsigned>(tipBoneId), dt);
275}
276
277void Solver3D::setPole(float x, float y, float z, float weight) { impl_->solver.setPole(::ik::vec3{x, y, z}, weight); }
278
279void Solver3D::clearPole() { impl_->solver.clearPole(); }
280
281bool Solver3D::hasPole() const { return impl_->solver.hasPole(); }
282
283float Solver3D::getPoleWeight() const { return impl_->solver.getPoleWeight(); }
284
285void Solver3D::setTipRotation(int boneId, float yaw, float pitch, float weight) {
286 if (boneId < 0) {
287 throw Exception("Solver3D.setTipRotation: boneId must be >= 0");
288 }
289 impl_->solver.setTipRotation(static_cast<unsigned>(boneId), ::ik::vec2{yaw, pitch}, weight);
290}
291
292void Solver3D::clearTipRotation() { impl_->solver.clearTipRotation(); }
293
294float Solver3D::getTipRotationWeight() const { return impl_->solver.getTipRotationWeight(); }
295
296int Solver3D::getTraceSize() const { return static_cast<int>(impl_->solver.traceSize()); }
297
298void Solver3D::clearTrace() { impl_->solver.clearTrace(); }
299
300} // namespace eve::ik
double z
double y
double x
float yaw
Definition BushMesh.cpp:37
ForceVector force
MeleePoint3 b
Definition MeleeHit.cpp:41
double dt
bool found
int count
int iterations
Definition TreeMesh.cpp:448
double weight
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
int getBoneCount() const
Number of bones currently in the skeleton.
int getParent(int boneId) const
Parent bone id, or -1 for the root.
Script-facing 3D skeleton + pose state. Local angles are yaw/pitch in the parent bone frame.
Definition Skeleton3D.h:15
int getParent(int boneId) const
Parent bone id, or -1 for the root.
int getBoneCount() const
Number of bones currently in the skeleton.
int getMaxIterations() const
Configured maximum FABRIK iterations.
Definition Solver2D.cpp:41
int getTargetCount() const
Number of configured targets.
Definition Solver2D.cpp:78
void setForce(float force)
Soft-IK blend toward the target (0 = hard IK).
Definition Solver2D.cpp:53
bool getKeepTrace() const
Whether solve tracing is enabled.
Definition Solver2D.cpp:31
void setMaxIterations(int iterations)
Maximum FABRIK iterations per solve.
Definition Solver2D.cpp:33
void step(Skeleton2D *skeleton, float dt=1.f)
Runs a time-scaled FABRIK step (dt scales iteration effort).
Definition Solver2D.cpp:89
void setTolerance(float tol)
End-effector distance tolerance for success.
Definition Solver2D.cpp:43
Solver2D()
Creates a 2D FABRIK solver with default tolerances.
Definition Solver2D.cpp:23
int getTraceSize() const
Number of recorded trace samples.
Definition Solver2D.cpp:142
void clearTargets()
Removes all IK targets.
Definition Solver2D.cpp:69
~Solver2D()
Releases solver state.
void setKeepTrace(bool keep)
When true, records intermediate solve samples.
Definition Solver2D.cpp:26
void setInfluence(float influence)
Overall pose blend: 0 = keep the input pose, 1 = full solve (default).
Definition Solver2D.cpp:60
float getForce() const
Configured soft-IK force.
Definition Solver2D.cpp:58
void addTarget(int boneId, float x, float y, float weight=1.f)
Adds an end-effector target for a bone.
Definition Solver2D.cpp:71
bool solve(Skeleton2D *skeleton)
Returns true if every target is within tolerance.
Definition Solver2D.cpp:80
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:126
void stepChain(Skeleton2D *skeleton, int rootBoneId, int tipBoneId, float dt=1.f)
Time-scaled chain-scoped FABRIK step.
Definition Solver2D.cpp:134
float getTolerance() const
Configured end-effector tolerance.
Definition Solver2D.cpp:51
void clearTrace()
Clears recorded solve trace samples.
Definition Solver2D.cpp:144
float getInfluence() const
Pose blend weight (0 = keep input, 1 = full solve).
Definition Solver2D.cpp:67
void addTarget(int boneId, float x, float y, float z, float weight=1.f)
Adds an end-effector target for a bone.
Definition Solver2D.cpp:206
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 Solver2D.cpp:285
float getTipRotationWeight() const
Configured tip rotation blend weight.
Definition Solver2D.cpp:294
float getPoleWeight() const
Configured pole-vector weight in [0,1].
Definition Solver2D.cpp:283
float getForce() const
Configured soft-IK force.
Definition Solver2D.cpp:193
void setTolerance(float tol)
End-effector distance tolerance for success.
Definition Solver2D.cpp:178
void clearTrace()
Clears recorded solve trace samples.
Definition Solver2D.cpp:298
void clearTipRotation()
Clears tip orientation override.
Definition Solver2D.cpp:292
bool solve(Skeleton3D *skeleton)
Runs FABRIK until tolerance or max iterations.
Definition Solver2D.cpp:215
void setInfluence(float influence)
Overall pose blend: 0 = keep the input pose, 1 = full solve (default).
Definition Solver2D.cpp:195
void clearPole()
Disables the pole-vector hint.
Definition Solver2D.cpp:279
int getMaxIterations() const
Configured maximum FABRIK iterations.
Definition Solver2D.cpp:176
void stepChain(Skeleton3D *skeleton, int rootBoneId, int tipBoneId, float dt=1.f)
Time-scaled chain-scoped FABRIK step.
Definition Solver2D.cpp:269
void setForce(float force)
Soft-IK blend toward the target (0 = hard IK).
Definition Solver2D.cpp:188
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 Solver2D.cpp:261
void clearTargets()
Removes all IK targets.
Definition Solver2D.cpp:204
float getInfluence() const
Pose blend weight (0 = keep input, 1 = full solve).
Definition Solver2D.cpp:202
bool hasPole() const
True when a pole vector is configured.
Definition Solver2D.cpp:281
void setMaxIterations(int iterations)
Maximum FABRIK iterations per solve.
Definition Solver2D.cpp:168
void step(Skeleton3D *skeleton, float dt=1.f)
Runs a time-scaled FABRIK step (dt scales iteration effort).
Definition Solver2D.cpp:224
int getTargetCount() const
Number of configured targets.
Definition Solver2D.cpp:213
int getTraceSize() const
Number of recorded trace samples.
Definition Solver2D.cpp:296
bool getKeepTrace() const
Whether solve tracing is enabled.
Definition Solver2D.cpp:166
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 Solver2D.cpp:277
float getTolerance() const
Configured end-effector tolerance.
Definition Solver2D.cpp:186
Solver3D()
Creates a 3D FABRIK solver with default tolerances.
Definition Solver2D.cpp:158
void setKeepTrace(bool keep)
When true, records intermediate solve samples.
Definition Solver2D.cpp:161
~Solver3D()
Releases solver state.
::ik::solver2d solver
Definition Solver2D.cpp:16
::ik::solver3d solver
Definition Solver2D.cpp:151