math 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +1605 -0
- package/LICENSE +21 -0
- package/README.md +197 -0
- package/dist/color/color.js +136 -0
- package/dist/color/color.js.map +1 -0
- package/dist/color/colorspace.js +43 -0
- package/dist/color/colorspace.js.map +1 -0
- package/dist/color/hsl.js +123 -0
- package/dist/color/hsl.js.map +1 -0
- package/dist/color/index.js +7 -0
- package/dist/color/index.js.map +1 -0
- package/dist/color/parse.js +276 -0
- package/dist/color/parse.js.map +1 -0
- package/dist/core/angle.js +43 -0
- package/dist/core/angle.js.map +1 -0
- package/dist/core/euler.js +212 -0
- package/dist/core/euler.js.map +1 -0
- package/dist/core/mat2.js +385 -0
- package/dist/core/mat2.js.map +1 -0
- package/dist/core/mat2d.js +422 -0
- package/dist/core/mat2d.js.map +1 -0
- package/dist/core/mat3.js +749 -0
- package/dist/core/mat3.js.map +1 -0
- package/dist/core/mat4.js +2197 -0
- package/dist/core/mat4.js.map +1 -0
- package/dist/core/polar.js +200 -0
- package/dist/core/polar.js.map +1 -0
- package/dist/core/quat.js +730 -0
- package/dist/core/quat.js.map +1 -0
- package/dist/core/quat2.js +883 -0
- package/dist/core/quat2.js.map +1 -0
- package/dist/core/scalar.js +112 -0
- package/dist/core/scalar.js.map +1 -0
- package/dist/core/spherical.js +248 -0
- package/dist/core/spherical.js.map +1 -0
- package/dist/core/vec2.js +597 -0
- package/dist/core/vec2.js.map +1 -0
- package/dist/core/vec3.js +916 -0
- package/dist/core/vec3.js.map +1 -0
- package/dist/core/vec4.js +573 -0
- package/dist/core/vec4.js.map +1 -0
- package/dist/geometry/circumcircle.js +39 -0
- package/dist/geometry/circumcircle.js.map +1 -0
- package/dist/geometry/index.js +6 -0
- package/dist/geometry/index.js.map +1 -0
- package/dist/geometry/polygon2-decompose.js +411 -0
- package/dist/geometry/polygon2-decompose.js.map +1 -0
- package/dist/geometry/polygon2-triangulate.js +222 -0
- package/dist/geometry/polygon2-triangulate.js.map +1 -0
- package/dist/geometry/quickhull2.js +123 -0
- package/dist/geometry/quickhull2.js.map +1 -0
- package/dist/geometry/quickhull3.js +605 -0
- package/dist/geometry/quickhull3.js.map +1 -0
- package/dist/ik/fabrik2.js +675 -0
- package/dist/ik/fabrik2.js.map +1 -0
- package/dist/ik/fabrik3.js +912 -0
- package/dist/ik/fabrik3.js.map +1 -0
- package/dist/ik/index.js +5 -0
- package/dist/ik/index.js.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/noise/fractal.js +185 -0
- package/dist/noise/fractal.js.map +1 -0
- package/dist/noise/index.js +16 -0
- package/dist/noise/index.js.map +1 -0
- package/dist/noise/perlin2d.js +50 -0
- package/dist/noise/perlin2d.js.map +1 -0
- package/dist/noise/perlin3d.js +72 -0
- package/dist/noise/perlin3d.js.map +1 -0
- package/dist/noise/permutation.js +120 -0
- package/dist/noise/permutation.js.map +1 -0
- package/dist/noise/simplex2d.js +84 -0
- package/dist/noise/simplex2d.js.map +1 -0
- package/dist/noise/simplex3d.js +147 -0
- package/dist/noise/simplex3d.js.map +1 -0
- package/dist/noise/simplex4d.js +149 -0
- package/dist/noise/simplex4d.js.map +1 -0
- package/dist/noise/worley2d.js +50 -0
- package/dist/noise/worley2d.js.map +1 -0
- package/dist/noise/worley3d.js +61 -0
- package/dist/noise/worley3d.js.map +1 -0
- package/dist/random/index.js +9 -0
- package/dist/random/index.js.map +1 -0
- package/dist/random/isaac32.js +164 -0
- package/dist/random/isaac32.js.map +1 -0
- package/dist/random/isaac64.js +215 -0
- package/dist/random/isaac64.js.map +1 -0
- package/dist/random/mulberry32.js +48 -0
- package/dist/random/mulberry32.js.map +1 -0
- package/dist/random/random.js +120 -0
- package/dist/random/random.js.map +1 -0
- package/dist/shapes/box2.js +300 -0
- package/dist/shapes/box2.js.map +1 -0
- package/dist/shapes/box3.js +550 -0
- package/dist/shapes/box3.js.map +1 -0
- package/dist/shapes/circle.js +6 -0
- package/dist/shapes/circle.js.map +1 -0
- package/dist/shapes/frustum.js +662 -0
- package/dist/shapes/frustum.js.map +1 -0
- package/dist/shapes/index.js +25 -0
- package/dist/shapes/index.js.map +1 -0
- package/dist/shapes/obb3.js +557 -0
- package/dist/shapes/obb3.js.map +1 -0
- package/dist/shapes/plane3.js +267 -0
- package/dist/shapes/plane3.js.map +1 -0
- package/dist/shapes/polygon2.js +451 -0
- package/dist/shapes/polygon2.js.map +1 -0
- package/dist/shapes/raycast3.js +164 -0
- package/dist/shapes/raycast3.js.map +1 -0
- package/dist/shapes/segment2.js +82 -0
- package/dist/shapes/segment2.js.map +1 -0
- package/dist/shapes/sphere.js +23 -0
- package/dist/shapes/sphere.js.map +1 -0
- package/dist/shapes/triangle2.js +79 -0
- package/dist/shapes/triangle2.js.map +1 -0
- package/dist/shapes/triangle3.js +60 -0
- package/dist/shapes/triangle3.js.map +1 -0
- package/dist/src/color/color.d.ts +50 -0
- package/dist/src/color/colorspace.d.ts +15 -0
- package/dist/src/color/hsl.d.ts +28 -0
- package/dist/src/color/index.d.ts +5 -0
- package/dist/src/color/parse.d.ts +15 -0
- package/dist/src/core/angle.d.ts +29 -0
- package/dist/src/core/arrays.d.ts +4 -0
- package/dist/src/core/euler.d.ts +78 -0
- package/dist/src/core/index.d.ts +27 -0
- package/dist/src/core/mat2.d.ts +217 -0
- package/dist/src/core/mat2d.d.ts +218 -0
- package/dist/src/core/mat3.d.ts +295 -0
- package/dist/src/core/mat4.d.ts +667 -0
- package/dist/src/core/polar.d.ts +151 -0
- package/dist/src/core/quat.d.ts +370 -0
- package/dist/src/core/quat2.d.ts +344 -0
- package/dist/src/core/scalar.d.ts +75 -0
- package/dist/src/core/spherical.d.ts +165 -0
- package/dist/src/core/vec2.d.ts +403 -0
- package/dist/src/core/vec3.d.ts +493 -0
- package/dist/src/core/vec4.d.ts +336 -0
- package/dist/src/geometry/circumcircle.d.ts +9 -0
- package/dist/src/geometry/index.d.ts +5 -0
- package/dist/src/geometry/polygon2-decompose.d.ts +25 -0
- package/dist/src/geometry/polygon2-triangulate.d.ts +15 -0
- package/dist/src/geometry/quickhull2.d.ts +10 -0
- package/dist/src/geometry/quickhull3.d.ts +27 -0
- package/dist/src/ik/fabrik2.d.ts +314 -0
- package/dist/src/ik/fabrik3.d.ts +371 -0
- package/dist/src/ik/index.d.ts +2 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/noise/fractal.d.ts +95 -0
- package/dist/src/noise/index.d.ts +9 -0
- package/dist/src/noise/perlin2d.d.ts +19 -0
- package/dist/src/noise/perlin3d.d.ts +20 -0
- package/dist/src/noise/permutation.d.ts +22 -0
- package/dist/src/noise/simplex2d.d.ts +19 -0
- package/dist/src/noise/simplex3d.d.ts +20 -0
- package/dist/src/noise/simplex4d.d.ts +24 -0
- package/dist/src/noise/worley2d.d.ts +22 -0
- package/dist/src/noise/worley3d.d.ts +23 -0
- package/dist/src/random/index.d.ts +8 -0
- package/dist/src/random/isaac32.d.ts +62 -0
- package/dist/src/random/isaac64.d.ts +75 -0
- package/dist/src/random/mulberry32.d.ts +43 -0
- package/dist/src/random/random.d.ts +67 -0
- package/dist/src/shapes/box2.d.ts +174 -0
- package/dist/src/shapes/box3.d.ts +213 -0
- package/dist/src/shapes/circle.d.ts +7 -0
- package/dist/src/shapes/frustum.d.ts +147 -0
- package/dist/src/shapes/index.d.ts +20 -0
- package/dist/src/shapes/obb3.d.ts +105 -0
- package/dist/src/shapes/plane3.d.ts +130 -0
- package/dist/src/shapes/polygon2.d.ts +155 -0
- package/dist/src/shapes/raycast3.d.ts +42 -0
- package/dist/src/shapes/segment2.d.ts +34 -0
- package/dist/src/shapes/sphere.d.ts +19 -0
- package/dist/src/shapes/triangle2.d.ts +53 -0
- package/dist/src/shapes/triangle3.d.ts +29 -0
- package/dist/src/time/easing.d.ts +21 -0
- package/dist/src/time/index.d.ts +6 -0
- package/dist/src/time/spring-core.d.ts +15 -0
- package/dist/src/time/spring.d.ts +36 -0
- package/dist/src/time/spring2.d.ts +11 -0
- package/dist/src/time/spring3.d.ts +11 -0
- package/dist/src/time/spring4.d.ts +11 -0
- package/dist/time/easing.js +24 -0
- package/dist/time/easing.js.map +1 -0
- package/dist/time/index.js +11 -0
- package/dist/time/index.js.map +1 -0
- package/dist/time/spring-core.js +48 -0
- package/dist/time/spring-core.js.map +1 -0
- package/dist/time/spring.js +62 -0
- package/dist/time/spring.js.map +1 -0
- package/dist/time/spring2.js +31 -0
- package/dist/time/spring2.js.map +1 -0
- package/dist/time/spring3.js +32 -0
- package/dist/time/spring3.js.map +1 -0
- package/dist/time/spring4.js +32 -0
- package/dist/time/spring4.js.map +1 -0
- package/package.json +90 -16
- package/skills/math/SKILL.md +140 -0
- package/AUTHORS +0 -1
- package/Cakefile +0 -28
- package/math.coffee +0 -112
- package/math.js +0 -141
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import { distance, subtract, squaredLength, copy, normalize, squaredDistance, rotate, signedAngle } from '../core/vec2.js';
|
|
2
|
+
|
|
3
|
+
// FABRIK (Forward And Backward Reaching Inverse Kinematics) for 2D chains.
|
|
4
|
+
//
|
|
5
|
+
// The 2D counterpart of `fabrik3`, same shape and same two passes. What differs is the constraint
|
|
6
|
+
// model: with no axis to hinge about, a joint is a wedge - how far the bone may swing clockwise and
|
|
7
|
+
// anticlockwise of a baseline, either the previous bone (`LOCAL`) or a fixed world direction
|
|
8
|
+
// (`GLOBAL`, which pins the bone's absolute heading).
|
|
9
|
+
//
|
|
10
|
+
// The constraint model follows Caliko, the reference implementation accompanying Aristidou &
|
|
11
|
+
// Lasenby (2011), "FABRIK: A fast, iterative solver for the Inverse Kinematics problem".
|
|
12
|
+
/** What a joint's clockwise and anticlockwise limits are measured from. */
|
|
13
|
+
var ConstraintCoordinateSystem;
|
|
14
|
+
(function (ConstraintCoordinateSystem) {
|
|
15
|
+
/** The direction of the previous bone, so the limits bound how far this bone may bend. */
|
|
16
|
+
ConstraintCoordinateSystem[ConstraintCoordinateSystem["LOCAL"] = 0] = "LOCAL";
|
|
17
|
+
/** A fixed world direction, so the limits bound this bone's absolute heading. */
|
|
18
|
+
ConstraintCoordinateSystem[ConstraintCoordinateSystem["GLOBAL"] = 1] = "GLOBAL";
|
|
19
|
+
})(ConstraintCoordinateSystem || (ConstraintCoordinateSystem = {}));
|
|
20
|
+
/**
|
|
21
|
+
* How the first bone in a chain is constrained.
|
|
22
|
+
*
|
|
23
|
+
* The first bone has no bone before it, so it is constrained against a direction held on the chain.
|
|
24
|
+
* The `LOCAL_` types only mean anything for a chain in a {@link Structure2}, where
|
|
25
|
+
* {@link solveStructure} resolves them against the bone the chain hangs off.
|
|
26
|
+
*/
|
|
27
|
+
var BaseboneConstraintType;
|
|
28
|
+
(function (BaseboneConstraintType) {
|
|
29
|
+
/** The first bone may point anywhere. */
|
|
30
|
+
BaseboneConstraintType[BaseboneConstraintType["NONE"] = 0] = "NONE";
|
|
31
|
+
/** Constrained against a fixed world direction. */
|
|
32
|
+
BaseboneConstraintType[BaseboneConstraintType["GLOBAL_ABSOLUTE"] = 1] = "GLOBAL_ABSOLUTE";
|
|
33
|
+
/** Constrained against the host bone's direction, so the chain bends relative to what it hangs off. */
|
|
34
|
+
BaseboneConstraintType[BaseboneConstraintType["LOCAL_RELATIVE"] = 2] = "LOCAL_RELATIVE";
|
|
35
|
+
/**
|
|
36
|
+
* Constrained against a direction expressed in the host bone's frame, where **+Y** is the host
|
|
37
|
+
* bone's own direction. (`fabrik3`'s equivalent uses +Z, so a rig ported between the two needs
|
|
38
|
+
* its axes rotated.)
|
|
39
|
+
*/
|
|
40
|
+
BaseboneConstraintType[BaseboneConstraintType["LOCAL_ABSOLUTE"] = 3] = "LOCAL_ABSOLUTE";
|
|
41
|
+
})(BaseboneConstraintType || (BaseboneConstraintType = {}));
|
|
42
|
+
/** Which end of a host bone a connected chain hangs off. */
|
|
43
|
+
var BoneConnectionPoint;
|
|
44
|
+
(function (BoneConnectionPoint) {
|
|
45
|
+
/** The host bone's start - the joint it shares with the bone before it. */
|
|
46
|
+
BoneConnectionPoint[BoneConnectionPoint["START"] = 0] = "START";
|
|
47
|
+
/** The host bone's end. */
|
|
48
|
+
BoneConnectionPoint[BoneConnectionPoint["END"] = 1] = "END";
|
|
49
|
+
})(BoneConnectionPoint || (BoneConnectionPoint = {}));
|
|
50
|
+
// the most iterations a solve runs before giving up on reaching the target
|
|
51
|
+
const DEFAULT_MAX_ITERATIONS = 20;
|
|
52
|
+
// how close the effector must get before a solve stops early. in world units, so scale it with the
|
|
53
|
+
// scene - the default suits a chain a few units long
|
|
54
|
+
const DEFAULT_SOLVE_DISTANCE_THRESHOLD = 0.01;
|
|
55
|
+
// how little an iteration may improve the solve distance before it counts as stalled. a constrained
|
|
56
|
+
// chain can reach a pose no further iteration improves on while still short of the threshold, and
|
|
57
|
+
// without this it would burn every remaining iteration going nowhere. two orders of magnitude below
|
|
58
|
+
// the distance threshold, so a solve still closing in on the target is never mistaken for a stalled
|
|
59
|
+
// one
|
|
60
|
+
const DEFAULT_MIN_ITERATION_CHANGE = 1e-4;
|
|
61
|
+
// below this squared length a vector carries no usable direction - 1e-12 in length terms, well above
|
|
62
|
+
// a normalize's noise floor and well below anything meaningful. every test against it also checks
|
|
63
|
+
// for NaN, so a value poisoned upstream takes the same fallback instead of spreading down the chain
|
|
64
|
+
const DEGENERATE_SQUARED_LENGTH = 1e-24;
|
|
65
|
+
/**
|
|
66
|
+
* Normalizes `axis` into `out`, leaving `out` untouched if `axis` has no direction to give.
|
|
67
|
+
*
|
|
68
|
+
* A zero axis cannot be normalized, and storing one turns every constraint that reads it into NaN.
|
|
69
|
+
* `out` always starts as a valid unit vector, so keeping it is the safe fallback.
|
|
70
|
+
*/
|
|
71
|
+
function setUnitAxis(out, axis) {
|
|
72
|
+
if (!hasDirection(squaredLength(axis)))
|
|
73
|
+
return out;
|
|
74
|
+
return normalize(out, axis);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Whether a squared length is a usable magnitude: a real number, and not effectively zero.
|
|
78
|
+
*
|
|
79
|
+
* NaN counts as unusable. A value poisoned upstream then takes the same fallback as a zero-length
|
|
80
|
+
* one, instead of spreading down the chain and leaving it stuck.
|
|
81
|
+
*/
|
|
82
|
+
function hasDirection(squaredLength) {
|
|
83
|
+
if (Number.isNaN(squaredLength))
|
|
84
|
+
return false;
|
|
85
|
+
return squaredLength >= DEGENERATE_SQUARED_LENGTH;
|
|
86
|
+
}
|
|
87
|
+
/** Creates an unconstrained joint. */
|
|
88
|
+
function createJoint2() {
|
|
89
|
+
return {
|
|
90
|
+
clockwise: Math.PI,
|
|
91
|
+
anticlockwise: Math.PI,
|
|
92
|
+
coordinateSystem: ConstraintCoordinateSystem.LOCAL,
|
|
93
|
+
globalAxis: [0, 1],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Creates an empty chain with a fixed base at the origin and no basebone constraint. */
|
|
97
|
+
function createChain2() {
|
|
98
|
+
return {
|
|
99
|
+
bones: [],
|
|
100
|
+
length: 0,
|
|
101
|
+
base: [0, 0],
|
|
102
|
+
fixedBase: true,
|
|
103
|
+
baseboneConstraintType: BaseboneConstraintType.NONE,
|
|
104
|
+
baseboneAxis: [0, 1],
|
|
105
|
+
baseboneWorldAxis: [0, 1],
|
|
106
|
+
baseboneClockwise: Math.PI,
|
|
107
|
+
baseboneAnticlockwise: Math.PI,
|
|
108
|
+
embeddedTarget: [0, 0],
|
|
109
|
+
useEmbeddedTarget: false,
|
|
110
|
+
maxIterations: DEFAULT_MAX_ITERATIONS,
|
|
111
|
+
solveDistanceThreshold: DEFAULT_SOLVE_DISTANCE_THRESHOLD,
|
|
112
|
+
minIterationChange: DEFAULT_MIN_ITERATION_CHANGE,
|
|
113
|
+
solveDistance: Number.POSITIVE_INFINITY,
|
|
114
|
+
bestSolution: [],
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Appends a bone spanning `start` to `end`, copying both.
|
|
119
|
+
*
|
|
120
|
+
* The bone's length is taken from the distance between them. When this is the first bone the
|
|
121
|
+
* chain's base is moved to `start`.
|
|
122
|
+
*
|
|
123
|
+
* @param chain the chain to append to
|
|
124
|
+
* @param start the bone's start point
|
|
125
|
+
* @param end the bone's end point
|
|
126
|
+
* @param joint the bone's joint, or a fresh unconstrained one if omitted
|
|
127
|
+
* @returns the appended bone
|
|
128
|
+
*/
|
|
129
|
+
function addBone(chain, start, end, joint = createJoint2()) {
|
|
130
|
+
const bone = {
|
|
131
|
+
start: [start[0], start[1]],
|
|
132
|
+
end: [end[0], end[1]],
|
|
133
|
+
length: distance(start, end),
|
|
134
|
+
joint,
|
|
135
|
+
};
|
|
136
|
+
if (chain.bones.length === 0) {
|
|
137
|
+
chain.base[0] = start[0];
|
|
138
|
+
chain.base[1] = start[1];
|
|
139
|
+
}
|
|
140
|
+
chain.bones.push(bone);
|
|
141
|
+
chain.length += bone.length;
|
|
142
|
+
// four numbers of best-pose scratch per bone, so `solve` never allocates
|
|
143
|
+
chain.bestSolution.push(0, 0, 0, 0);
|
|
144
|
+
return bone;
|
|
145
|
+
}
|
|
146
|
+
const _addConsecutive_end = [0, 0];
|
|
147
|
+
/**
|
|
148
|
+
* Appends a bone starting where the chain currently ends, running `length` along `direction`.
|
|
149
|
+
*
|
|
150
|
+
* @param chain the chain to append to, which must already have at least one bone
|
|
151
|
+
* @param direction the direction to extend in, assumed to be unit length
|
|
152
|
+
* @param length the length of the new bone
|
|
153
|
+
* @param joint the bone's joint, or a fresh unconstrained one if omitted
|
|
154
|
+
* @returns the appended bone
|
|
155
|
+
*/
|
|
156
|
+
function addConsecutiveBone(chain, direction, length, joint = createJoint2()) {
|
|
157
|
+
const previous = chain.bones[chain.bones.length - 1];
|
|
158
|
+
_addConsecutive_end[0] = previous.end[0] + direction[0] * length;
|
|
159
|
+
_addConsecutive_end[1] = previous.end[1] + direction[1] * length;
|
|
160
|
+
return addBone(chain, previous.end, _addConsecutive_end, joint);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Prepends a bone at the base end, extending the chain backward.
|
|
164
|
+
*
|
|
165
|
+
* This is what a follower grows with: driven by {@link forward} alone the base end is the tail.
|
|
166
|
+
*
|
|
167
|
+
* `joint` becomes the joint of the bone that was previously first, because that is the junction the
|
|
168
|
+
* new bone creates. The new first bone's own joint is unused, as always.
|
|
169
|
+
*
|
|
170
|
+
* @param chain the chain to prepend to, which must already have at least one bone
|
|
171
|
+
* @param direction the direction the new bone points, from its own start toward the existing chain. Assumed to be unit length
|
|
172
|
+
* @param length the length of the new bone
|
|
173
|
+
* @param joint the joint for the junction this creates, or a fresh unconstrained one if omitted
|
|
174
|
+
* @returns the prepended bone
|
|
175
|
+
*/
|
|
176
|
+
function addBoneAtBase(chain, direction, length, joint = createJoint2()) {
|
|
177
|
+
const first = chain.bones[0];
|
|
178
|
+
// the bone that was first now sits at index 1, so it is the one whose joint governs the new
|
|
179
|
+
// junction. its old joint was the unused basebone slot
|
|
180
|
+
first.joint = joint;
|
|
181
|
+
const bone = {
|
|
182
|
+
start: [first.start[0] - direction[0] * length, first.start[1] - direction[1] * length],
|
|
183
|
+
end: [first.start[0], first.start[1]],
|
|
184
|
+
length,
|
|
185
|
+
joint: createJoint2(),
|
|
186
|
+
};
|
|
187
|
+
chain.bones.unshift(bone);
|
|
188
|
+
chain.length += length;
|
|
189
|
+
chain.base[0] = bone.start[0];
|
|
190
|
+
chain.base[1] = bone.start[1];
|
|
191
|
+
chain.bestSolution.push(0, 0, 0, 0);
|
|
192
|
+
return bone;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Sets a joint's limits relative to the previous bone's direction, which is the usual case.
|
|
196
|
+
*
|
|
197
|
+
* @param joint the joint to configure
|
|
198
|
+
* @param clockwise how far the bone may swing clockwise of the previous bone, in radians, clamped to [0, PI]
|
|
199
|
+
* @param anticlockwise how far it may swing anticlockwise, in radians, clamped to [0, PI]
|
|
200
|
+
* @returns the joint
|
|
201
|
+
*/
|
|
202
|
+
function setLocalJoint(joint, clockwise, anticlockwise) {
|
|
203
|
+
joint.coordinateSystem = ConstraintCoordinateSystem.LOCAL;
|
|
204
|
+
joint.clockwise = clampAngle(clockwise);
|
|
205
|
+
joint.anticlockwise = clampAngle(anticlockwise);
|
|
206
|
+
return joint;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Sets a joint's limits relative to a fixed world direction, pinning the bone's absolute heading
|
|
210
|
+
* however the bones before it move.
|
|
211
|
+
*
|
|
212
|
+
* @param joint the joint to configure
|
|
213
|
+
* @param axis the world direction the limits are measured from, assumed to be unit length
|
|
214
|
+
* @param clockwise how far the bone may swing clockwise of `axis`, in radians, clamped to [0, PI]
|
|
215
|
+
* @param anticlockwise how far it may swing anticlockwise, in radians, clamped to [0, PI]
|
|
216
|
+
* @returns the joint
|
|
217
|
+
*/
|
|
218
|
+
function setGlobalJoint(joint, axis, clockwise, anticlockwise) {
|
|
219
|
+
joint.coordinateSystem = ConstraintCoordinateSystem.GLOBAL;
|
|
220
|
+
joint.clockwise = clampAngle(clockwise);
|
|
221
|
+
joint.anticlockwise = clampAngle(anticlockwise);
|
|
222
|
+
setUnitAxis(joint.globalAxis, axis);
|
|
223
|
+
return joint;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Constrains the first bone to a wedge about `axis`.
|
|
227
|
+
*
|
|
228
|
+
* @param chain the chain to configure
|
|
229
|
+
* @param type which space `axis` is expressed in
|
|
230
|
+
* @param axis the baseline direction, assumed to be unit length. Ignored for {@link BaseboneConstraintType.LOCAL_RELATIVE}, which uses the host bone's direction
|
|
231
|
+
* @param clockwise how far the first bone may swing clockwise of the baseline, in radians, clamped to [0, PI]
|
|
232
|
+
* @param anticlockwise how far it may swing anticlockwise, in radians, clamped to [0, PI]
|
|
233
|
+
* @returns the chain
|
|
234
|
+
*/
|
|
235
|
+
function setBaseboneConstraint(chain, type, axis, clockwise, anticlockwise) {
|
|
236
|
+
chain.baseboneConstraintType = type;
|
|
237
|
+
chain.baseboneClockwise = clampAngle(clockwise);
|
|
238
|
+
chain.baseboneAnticlockwise = clampAngle(anticlockwise);
|
|
239
|
+
setUnitAxis(chain.baseboneAxis, axis);
|
|
240
|
+
copy(chain.baseboneWorldAxis, chain.baseboneAxis);
|
|
241
|
+
return chain;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Moves the chain's pinned base, without moving the bones.
|
|
245
|
+
*
|
|
246
|
+
* The next {@link backward} or {@link solve} pulls the chain to it.
|
|
247
|
+
*/
|
|
248
|
+
function setBaseLocation(chain, base) {
|
|
249
|
+
chain.base[0] = base[0];
|
|
250
|
+
chain.base[1] = base[1];
|
|
251
|
+
return chain;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Lays the chain out straight from its base along `direction`, discarding the current pose.
|
|
255
|
+
*
|
|
256
|
+
* A dead-straight chain is the worst starting pose for {@link solve} - see the note there. Bend
|
|
257
|
+
* `direction` slightly between bones instead if the chain will be solved cold.
|
|
258
|
+
*/
|
|
259
|
+
function straighten(chain, direction) {
|
|
260
|
+
const bones = chain.bones;
|
|
261
|
+
let x = chain.base[0];
|
|
262
|
+
let y = chain.base[1];
|
|
263
|
+
for (let i = 0; i < bones.length; i++) {
|
|
264
|
+
const bone = bones[i];
|
|
265
|
+
bone.start[0] = x;
|
|
266
|
+
bone.start[1] = y;
|
|
267
|
+
x += direction[0] * bone.length;
|
|
268
|
+
y += direction[1] * bone.length;
|
|
269
|
+
bone.end[0] = x;
|
|
270
|
+
bone.end[1] = y;
|
|
271
|
+
}
|
|
272
|
+
return chain;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Writes the end effector's position - the last bone's end - into `out`.
|
|
276
|
+
*
|
|
277
|
+
* A chain with no bones has its base as its effector, matching {@link solve}, which treats an empty
|
|
278
|
+
* chain as a supported state rather than an error.
|
|
279
|
+
*/
|
|
280
|
+
function getEffector(out, chain) {
|
|
281
|
+
const count = chain.bones.length;
|
|
282
|
+
return copy(out, count === 0 ? chain.base : chain.bones[count - 1].end);
|
|
283
|
+
}
|
|
284
|
+
/** Writes the unit direction of bone `index`, from its start toward its end, into `out`. */
|
|
285
|
+
function getBoneDirection(out, chain, index) {
|
|
286
|
+
const bone = chain.bones[index];
|
|
287
|
+
subtract(out, bone.end, bone.start);
|
|
288
|
+
if (!hasDirection(squaredLength(out))) {
|
|
289
|
+
return copy(out, UP);
|
|
290
|
+
}
|
|
291
|
+
return normalize(out, out);
|
|
292
|
+
}
|
|
293
|
+
/** The angle of bone `index`, in radians, measured counter-clockwise from the +X axis. */
|
|
294
|
+
function getBoneAngle(chain, index) {
|
|
295
|
+
const bone = chain.bones[index];
|
|
296
|
+
return Math.atan2(bone.end[1] - bone.start[1], bone.end[0] - bone.start[0]);
|
|
297
|
+
}
|
|
298
|
+
/** Whether `target` is within reach of the chain's base, so a solve can place the effector exactly on it. */
|
|
299
|
+
function isReachable(chain, target) {
|
|
300
|
+
return squaredDistance(chain.base, target) <= chain.length * chain.length;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* The forward pass: snaps the end effector onto `target` and drags the rest of the chain after it.
|
|
304
|
+
*
|
|
305
|
+
* A non-finite target leaves the chain untouched rather than poisoning it.
|
|
306
|
+
*
|
|
307
|
+
* The base is not pinned, so the whole chain moves. Run alone once per frame this is a follower
|
|
308
|
+
* rather than a solver - a rope or tentacle whose head leads. Pair it with {@link backward}, or use
|
|
309
|
+
* {@link solve}, to keep the base put.
|
|
310
|
+
*
|
|
311
|
+
* @param chain the chain to move, mutated in place
|
|
312
|
+
* @param target where the end effector should go
|
|
313
|
+
* @returns the chain
|
|
314
|
+
*/
|
|
315
|
+
function forward(chain, target) {
|
|
316
|
+
const bones = chain.bones;
|
|
317
|
+
const count = bones.length;
|
|
318
|
+
if (count === 0)
|
|
319
|
+
return chain;
|
|
320
|
+
// a non-finite target would be written straight into the effector and spread down the chain
|
|
321
|
+
if (!Number.isFinite(target[0]) || !Number.isFinite(target[1]))
|
|
322
|
+
return chain;
|
|
323
|
+
// snap the effector onto the target. the rest of the pass follows from it
|
|
324
|
+
const effector = bones[count - 1];
|
|
325
|
+
effector.end[0] = target[0];
|
|
326
|
+
effector.end[1] = target[1];
|
|
327
|
+
// the outer-to-inner direction of the bone one step further out, which is what a joint on this
|
|
328
|
+
// bone is constrained against on this pass. undefined for the effector itself.
|
|
329
|
+
let hasReference = false;
|
|
330
|
+
for (let i = count - 1; i >= 0; i--) {
|
|
331
|
+
const bone = bones[i];
|
|
332
|
+
const start = bone.start;
|
|
333
|
+
const end = bone.end;
|
|
334
|
+
// this bone's outer-to-inner direction
|
|
335
|
+
let dx = start[0] - end[0];
|
|
336
|
+
let dy = start[1] - end[1];
|
|
337
|
+
const squaredLength = dx * dx + dy * dy;
|
|
338
|
+
if (!hasDirection(squaredLength)) {
|
|
339
|
+
// the two ends coincide, so there is no direction to preserve. carry on along the bone
|
|
340
|
+
// further out, or straight up if this is the effector
|
|
341
|
+
dx = hasReference ? _pass_reference[0] : UP[0];
|
|
342
|
+
dy = hasReference ? _pass_reference[1] : UP[1];
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
const inverseLength = 1 / Math.sqrt(squaredLength);
|
|
346
|
+
dx *= inverseLength;
|
|
347
|
+
dy *= inverseLength;
|
|
348
|
+
}
|
|
349
|
+
constrainForward(chain, i, dx, dy, hasReference);
|
|
350
|
+
dx = _pass_direction[0];
|
|
351
|
+
dy = _pass_direction[1];
|
|
352
|
+
const x = end[0] + dx * bone.length;
|
|
353
|
+
const y = end[1] + dy * bone.length;
|
|
354
|
+
start[0] = x;
|
|
355
|
+
start[1] = y;
|
|
356
|
+
// the previous bone shares this point
|
|
357
|
+
if (i > 0) {
|
|
358
|
+
const previousEnd = bones[i - 1].end;
|
|
359
|
+
previousEnd[0] = x;
|
|
360
|
+
previousEnd[1] = y;
|
|
361
|
+
}
|
|
362
|
+
_pass_reference[0] = dx;
|
|
363
|
+
_pass_reference[1] = dy;
|
|
364
|
+
hasReference = true;
|
|
365
|
+
}
|
|
366
|
+
return chain;
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* The backward pass: pins the base and pushes each bone outward from it. The basebone constraint
|
|
370
|
+
* is applied here.
|
|
371
|
+
*
|
|
372
|
+
* @param chain the chain to move, mutated in place
|
|
373
|
+
* @param base where the base should go, used only when `chain.fixedBase` is set
|
|
374
|
+
* @returns the chain
|
|
375
|
+
*/
|
|
376
|
+
function backward(chain, base) {
|
|
377
|
+
const bones = chain.bones;
|
|
378
|
+
const count = bones.length;
|
|
379
|
+
if (count === 0)
|
|
380
|
+
return chain;
|
|
381
|
+
if (!Number.isFinite(base[0]) || !Number.isFinite(base[1]))
|
|
382
|
+
return chain;
|
|
383
|
+
if (chain.fixedBase) {
|
|
384
|
+
const start = bones[0].start;
|
|
385
|
+
start[0] = base[0];
|
|
386
|
+
start[1] = base[1];
|
|
387
|
+
}
|
|
388
|
+
let hasReference = false;
|
|
389
|
+
for (let i = 0; i < count; i++) {
|
|
390
|
+
const bone = bones[i];
|
|
391
|
+
const start = bone.start;
|
|
392
|
+
const end = bone.end;
|
|
393
|
+
// this bone's inner-to-outer direction
|
|
394
|
+
let dx = end[0] - start[0];
|
|
395
|
+
let dy = end[1] - start[1];
|
|
396
|
+
const squaredLength = dx * dx + dy * dy;
|
|
397
|
+
if (!hasDirection(squaredLength)) {
|
|
398
|
+
dx = hasReference ? _pass_reference[0] : UP[0];
|
|
399
|
+
dy = hasReference ? _pass_reference[1] : UP[1];
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
const inverseLength = 1 / Math.sqrt(squaredLength);
|
|
403
|
+
dx *= inverseLength;
|
|
404
|
+
dy *= inverseLength;
|
|
405
|
+
}
|
|
406
|
+
constrainBackward(chain, i, dx, dy);
|
|
407
|
+
dx = _pass_direction[0];
|
|
408
|
+
dy = _pass_direction[1];
|
|
409
|
+
// when the base is free, the first bone swings about its end rather than its start
|
|
410
|
+
if (i === 0 && !chain.fixedBase) {
|
|
411
|
+
start[0] = end[0] - dx * bone.length;
|
|
412
|
+
start[1] = end[1] - dy * bone.length;
|
|
413
|
+
}
|
|
414
|
+
const x = start[0] + dx * bone.length;
|
|
415
|
+
const y = start[1] + dy * bone.length;
|
|
416
|
+
end[0] = x;
|
|
417
|
+
end[1] = y;
|
|
418
|
+
if (i < count - 1) {
|
|
419
|
+
const nextStart = bones[i + 1].start;
|
|
420
|
+
nextStart[0] = x;
|
|
421
|
+
nextStart[1] = y;
|
|
422
|
+
}
|
|
423
|
+
_pass_reference[0] = dx;
|
|
424
|
+
_pass_reference[1] = dy;
|
|
425
|
+
hasReference = true;
|
|
426
|
+
}
|
|
427
|
+
return chain;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* One full FABRIK iteration - {@link forward} then {@link backward}.
|
|
431
|
+
*
|
|
432
|
+
* @param chain the chain to move, mutated in place
|
|
433
|
+
* @param target where the end effector should go
|
|
434
|
+
* @returns the distance from the effector to `target` afterwards
|
|
435
|
+
*/
|
|
436
|
+
function iterate(chain, target) {
|
|
437
|
+
if (chain.bones.length === 0)
|
|
438
|
+
return Number.POSITIVE_INFINITY;
|
|
439
|
+
forward(chain, target);
|
|
440
|
+
backward(chain, chain.base);
|
|
441
|
+
return distance(chain.bones[chain.bones.length - 1].end, target);
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Solves the chain for `target`, iterating until it is close enough, stops improving, or runs out
|
|
445
|
+
* of attempts.
|
|
446
|
+
*
|
|
447
|
+
* A constrained chain does not converge monotonically, so the best pose seen is kept and restored.
|
|
448
|
+
*
|
|
449
|
+
* A dead-straight chain is the worst case to solve cold: every direction lies on one line, leaving
|
|
450
|
+
* nothing sideways to bend it with. Give it a slight bend if the first solve matters.
|
|
451
|
+
*
|
|
452
|
+
* @param chain the chain to solve, mutated in place
|
|
453
|
+
* @param target where the end effector should go
|
|
454
|
+
* @returns the distance from the effector to `target`, also stored as `chain.solveDistance`
|
|
455
|
+
*/
|
|
456
|
+
function solve(chain, target) {
|
|
457
|
+
const count = chain.bones.length;
|
|
458
|
+
if (count === 0) {
|
|
459
|
+
chain.solveDistance = Number.POSITIVE_INFINITY;
|
|
460
|
+
return chain.solveDistance;
|
|
461
|
+
}
|
|
462
|
+
// in case bones were pushed onto the chain directly rather than through addBone
|
|
463
|
+
if (chain.bestSolution.length < count * 4) {
|
|
464
|
+
chain.bestSolution.length = count * 4;
|
|
465
|
+
chain.bestSolution.fill(0);
|
|
466
|
+
}
|
|
467
|
+
let best = Number.POSITIVE_INFINITY;
|
|
468
|
+
let previous = Number.POSITIVE_INFINITY;
|
|
469
|
+
for (let i = 0; i < chain.maxIterations; i++) {
|
|
470
|
+
const distance = iterate(chain, target);
|
|
471
|
+
if (distance < best) {
|
|
472
|
+
best = distance;
|
|
473
|
+
saveSolution(chain);
|
|
474
|
+
if (distance <= chain.solveDistanceThreshold)
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
else if (Math.abs(distance - previous) < chain.minIterationChange) {
|
|
478
|
+
// ground to a halt - more iterations will not improve on what we already have
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
previous = distance;
|
|
482
|
+
}
|
|
483
|
+
if (best !== Number.POSITIVE_INFINITY) {
|
|
484
|
+
restoreSolution(chain);
|
|
485
|
+
}
|
|
486
|
+
chain.solveDistance = best;
|
|
487
|
+
return best;
|
|
488
|
+
}
|
|
489
|
+
/** Creates an empty structure. */
|
|
490
|
+
function createStructure2() {
|
|
491
|
+
return { chains: [], connections: [] };
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Adds a chain that hangs off nothing, solving directly for the structure's target.
|
|
495
|
+
*
|
|
496
|
+
* @returns the index of the added chain
|
|
497
|
+
*/
|
|
498
|
+
function addChain(structure, chain) {
|
|
499
|
+
structure.chains.push(chain);
|
|
500
|
+
structure.connections.push({ hostChain: -1, hostBone: 0, point: BoneConnectionPoint.END });
|
|
501
|
+
return structure.chains.length - 1;
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Adds a chain whose base is pinned to one end of a bone in a chain already in the structure.
|
|
505
|
+
*
|
|
506
|
+
* Add the host first: {@link solveStructure} walks chains in insertion order, so a chain always
|
|
507
|
+
* sees its host already posed.
|
|
508
|
+
*
|
|
509
|
+
* @param structure the structure to add to
|
|
510
|
+
* @param chain the chain to add
|
|
511
|
+
* @param hostChain the index of the chain to hang off
|
|
512
|
+
* @param hostBone the index of the bone within that chain
|
|
513
|
+
* @param point which end of the host bone to attach to
|
|
514
|
+
* @returns the index of the added chain
|
|
515
|
+
*/
|
|
516
|
+
function connectChain(structure, chain, hostChain, hostBone, point) {
|
|
517
|
+
structure.chains.push(chain);
|
|
518
|
+
structure.connections.push({ hostChain, hostBone, point });
|
|
519
|
+
return structure.chains.length - 1;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Solves every chain in the structure.
|
|
523
|
+
*
|
|
524
|
+
* A connected chain has its base moved onto its host bone's connection point and any `LOCAL_`
|
|
525
|
+
* basebone constraint resolved against the host bone's direction first. A chain with
|
|
526
|
+
* `useEmbeddedTarget` set solves for its own `embeddedTarget` instead.
|
|
527
|
+
*
|
|
528
|
+
* @param structure the structure to solve, mutated in place
|
|
529
|
+
* @param target the target for every chain that does not use an embedded target
|
|
530
|
+
*/
|
|
531
|
+
function solveStructure(structure, target) {
|
|
532
|
+
const chains = structure.chains;
|
|
533
|
+
for (let i = 0; i < chains.length; i++) {
|
|
534
|
+
const chain = chains[i];
|
|
535
|
+
const connection = structure.connections[i];
|
|
536
|
+
if (connection.hostChain >= 0) {
|
|
537
|
+
const host = chains[connection.hostChain];
|
|
538
|
+
const hostBone = host.bones[connection.hostBone];
|
|
539
|
+
setBaseLocation(chain, connection.point === BoneConnectionPoint.START ? hostBone.start : hostBone.end);
|
|
540
|
+
// being connected IS what fixes this chain's base - a connected chain with a free base
|
|
541
|
+
// would drift away from the host it is supposed to hang off, so the flag is not the
|
|
542
|
+
// caller's to clear here
|
|
543
|
+
chain.fixedBase = true;
|
|
544
|
+
const type = chain.baseboneConstraintType;
|
|
545
|
+
if (type === BaseboneConstraintType.LOCAL_RELATIVE) {
|
|
546
|
+
// the baseline is the host bone itself, so the chain bends relative to what it hangs off
|
|
547
|
+
getBoneDirection(chain.baseboneWorldAxis, host, connection.hostBone);
|
|
548
|
+
}
|
|
549
|
+
else if (type === BaseboneConstraintType.LOCAL_ABSOLUTE) {
|
|
550
|
+
// the baseline is expressed in the host bone's frame, so turn it by however far the
|
|
551
|
+
// host bone is turned from straight up
|
|
552
|
+
getBoneDirection(_structure_direction, host, connection.hostBone);
|
|
553
|
+
rotate(chain.baseboneWorldAxis, chain.baseboneAxis, ORIGIN, signedAngle(UP, _structure_direction));
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
solve(chain, chain.useEmbeddedTarget ? chain.embeddedTarget : target);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/* internals */
|
|
560
|
+
const UP = [0, 1];
|
|
561
|
+
const ORIGIN = [0, 0];
|
|
562
|
+
const _structure_direction = [0, 0];
|
|
563
|
+
// the direction the current pass is constraining against - the previous bone's, whichever way the
|
|
564
|
+
// pass is walking - and the constrained result handed back from `constrain*`
|
|
565
|
+
const _pass_reference = [0, 0];
|
|
566
|
+
const _pass_direction = [0, 0];
|
|
567
|
+
const _constrain_baseline = [0, 0];
|
|
568
|
+
function clampAngle(radians) {
|
|
569
|
+
return radians < 0 ? 0 : radians > Math.PI ? Math.PI : radians;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Clamps the direction `(x, y)` into the wedge reaching `clockwise` one way and `anticlockwise` the
|
|
573
|
+
* other from `baseline`. Writes `_pass_direction`.
|
|
574
|
+
*/
|
|
575
|
+
function constrainToWedge(x, y, baseline, clockwise, anticlockwise) {
|
|
576
|
+
_pass_direction[0] = x;
|
|
577
|
+
_pass_direction[1] = y;
|
|
578
|
+
if (clockwise >= Math.PI && anticlockwise >= Math.PI)
|
|
579
|
+
return;
|
|
580
|
+
// anticlockwise is positive
|
|
581
|
+
const signed = signedAngle(baseline, _pass_direction);
|
|
582
|
+
if (signed > anticlockwise) {
|
|
583
|
+
rotate(_pass_direction, baseline, ORIGIN, anticlockwise);
|
|
584
|
+
}
|
|
585
|
+
else if (signed < -clockwise) {
|
|
586
|
+
rotate(_pass_direction, baseline, ORIGIN, -clockwise);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Constrains a bone's outer-to-inner direction during the forward pass.
|
|
591
|
+
*
|
|
592
|
+
* A `GLOBAL` joint pins this bone's own heading. A `LOCAL` joint limits the bend between two bones,
|
|
593
|
+
* and that angle belongs to the joint between the two, which walking inward is the next bone's.
|
|
594
|
+
*
|
|
595
|
+
* The effector needs none of its own: its pair is clamped when the bone behind it is processed.
|
|
596
|
+
*
|
|
597
|
+
* Writes the result into `_pass_direction`, which starts out holding the unconstrained direction,
|
|
598
|
+
* so a branch that does not apply can simply return.
|
|
599
|
+
*/
|
|
600
|
+
function constrainForward(chain, index, x, y, hasReference) {
|
|
601
|
+
// start from the unconstrained direction. each branch below narrows it if it applies
|
|
602
|
+
_pass_direction[0] = x;
|
|
603
|
+
_pass_direction[1] = y;
|
|
604
|
+
// a GLOBAL joint pins this bone's heading whatever its neighbours do. this pass works in
|
|
605
|
+
// outer-to-inner directions, so the baseline is flipped to match
|
|
606
|
+
if (index > 0) {
|
|
607
|
+
const joint = chain.bones[index].joint;
|
|
608
|
+
if (joint.coordinateSystem === ConstraintCoordinateSystem.GLOBAL) {
|
|
609
|
+
_constrain_baseline[0] = -joint.globalAxis[0];
|
|
610
|
+
_constrain_baseline[1] = -joint.globalAxis[1];
|
|
611
|
+
constrainToWedge(x, y, _constrain_baseline, joint.clockwise, joint.anticlockwise);
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
// the wedge shared with the bone one step further out. the end effector has no such bone
|
|
616
|
+
if (!hasReference)
|
|
617
|
+
return;
|
|
618
|
+
const outer = chain.bones[index + 1].joint;
|
|
619
|
+
if (outer.coordinateSystem !== ConstraintCoordinateSystem.LOCAL)
|
|
620
|
+
return;
|
|
621
|
+
if (outer.clockwise >= Math.PI && outer.anticlockwise >= Math.PI)
|
|
622
|
+
return;
|
|
623
|
+
// this pass measures the pair the other way round - from the outer bone back to this one - and a
|
|
624
|
+
// wedge is not symmetric, so the two limits trade places
|
|
625
|
+
constrainToWedge(x, y, _pass_reference, outer.anticlockwise, outer.clockwise);
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Constrains a bone's inner-to-outer direction during the backward pass.
|
|
629
|
+
*
|
|
630
|
+
* Writes the result into `_pass_direction`, which starts out holding the unconstrained direction,
|
|
631
|
+
* so a branch that does not apply can simply return.
|
|
632
|
+
*/
|
|
633
|
+
function constrainBackward(chain, index, x, y) {
|
|
634
|
+
// start from the unconstrained direction. each branch below narrows it if it applies
|
|
635
|
+
_pass_direction[0] = x;
|
|
636
|
+
_pass_direction[1] = y;
|
|
637
|
+
if (index === 0) {
|
|
638
|
+
if (chain.baseboneConstraintType === BaseboneConstraintType.NONE)
|
|
639
|
+
return;
|
|
640
|
+
constrainToWedge(x, y, chain.baseboneWorldAxis, chain.baseboneClockwise, chain.baseboneAnticlockwise);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
const joint = chain.bones[index].joint;
|
|
644
|
+
if (joint.clockwise >= Math.PI && joint.anticlockwise >= Math.PI)
|
|
645
|
+
return;
|
|
646
|
+
const baseline = joint.coordinateSystem === ConstraintCoordinateSystem.GLOBAL ? joint.globalAxis : _pass_reference;
|
|
647
|
+
constrainToWedge(x, y, baseline, joint.clockwise, joint.anticlockwise);
|
|
648
|
+
}
|
|
649
|
+
function saveSolution(chain) {
|
|
650
|
+
const bones = chain.bones;
|
|
651
|
+
const solution = chain.bestSolution;
|
|
652
|
+
for (let i = 0; i < bones.length; i++) {
|
|
653
|
+
const bone = bones[i];
|
|
654
|
+
const offset = i * 4;
|
|
655
|
+
solution[offset] = bone.start[0];
|
|
656
|
+
solution[offset + 1] = bone.start[1];
|
|
657
|
+
solution[offset + 2] = bone.end[0];
|
|
658
|
+
solution[offset + 3] = bone.end[1];
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function restoreSolution(chain) {
|
|
662
|
+
const bones = chain.bones;
|
|
663
|
+
const solution = chain.bestSolution;
|
|
664
|
+
for (let i = 0; i < bones.length; i++) {
|
|
665
|
+
const bone = bones[i];
|
|
666
|
+
const offset = i * 4;
|
|
667
|
+
bone.start[0] = solution[offset];
|
|
668
|
+
bone.start[1] = solution[offset + 1];
|
|
669
|
+
bone.end[0] = solution[offset + 2];
|
|
670
|
+
bone.end[1] = solution[offset + 3];
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
export { BaseboneConstraintType, BoneConnectionPoint, ConstraintCoordinateSystem, addBone, addBoneAtBase, addChain, addConsecutiveBone, backward, connectChain, createChain2, createJoint2, createStructure2, forward, getBoneAngle, getBoneDirection, getEffector, isReachable, iterate, setBaseLocation, setBaseboneConstraint, setGlobalJoint, setLocalJoint, solve, solveStructure, straighten };
|
|
675
|
+
//# sourceMappingURL=fabrik2.js.map
|