PathConstraint.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/PathConstraint.h>
  31. #include <spine/Skeleton.h>
  32. #include <spine/extension.h>
  33. #define PATHCONSTRAINT_NONE -1
  34. #define PATHCONSTRAINT_BEFORE -2
  35. #define PATHCONSTRAINT_AFTER -3
  36. spPathConstraint* spPathConstraint_create (spPathConstraintData* data, const spSkeleton* skeleton) {
  37. int i;
  38. spPathConstraint *self = NEW(spPathConstraint);
  39. CONST_CAST(spPathConstraintData*, self->data) = data;
  40. self->bonesCount = data->bonesCount;
  41. CONST_CAST(spBone**, self->bones) = MALLOC(spBone*, self->bonesCount);
  42. for (i = 0; i < self->bonesCount; ++i)
  43. self->bones[i] = spSkeleton_findBone(skeleton, self->data->bones[i]->name);
  44. self->target = spSkeleton_findSlot(skeleton, self->data->target->name);
  45. self->position = data->position;
  46. self->spacing = data->spacing;
  47. self->rotateMix = data->rotateMix;
  48. self->translateMix = data->translateMix;
  49. self->spacesCount = 0;
  50. self->spaces = 0;
  51. self->positionsCount = 0;
  52. self->positions = 0;
  53. self->worldCount = 0;
  54. self->world = 0;
  55. self->curvesCount = 0;
  56. self->curves = 0;
  57. self->lengthsCount = 0;
  58. self->lengths = 0;
  59. return self;
  60. }
  61. void spPathConstraint_dispose (spPathConstraint* self) {
  62. FREE(self->bones);
  63. FREE(self->spaces);
  64. if (self->positions) FREE(self->positions);
  65. if (self->world) FREE(self->world);
  66. if (self->curves) FREE(self->curves);
  67. if (self->lengths) FREE(self->lengths);
  68. FREE(self);
  69. }
  70. void spPathConstraint_apply (spPathConstraint* self) {
  71. int i, p, n;
  72. float length, x, y, dx, dy, s;
  73. float* spaces, *lengths, *positions;
  74. float spacing;
  75. float boneX, boneY, offsetRotation;
  76. int/*bool*/tip;
  77. float rotateMix = self->rotateMix, translateMix = self->translateMix;
  78. int/*bool*/ translate = translateMix > 0, rotate = rotateMix > 0;
  79. spPathAttachment* attachment = (spPathAttachment*)self->target->attachment;
  80. spPathConstraintData* data = self->data;
  81. spSpacingMode spacingMode = data->spacingMode;
  82. int lengthSpacing = spacingMode == SP_SPACING_MODE_LENGTH;
  83. spRotateMode rotateMode = data->rotateMode;
  84. int tangents = rotateMode == SP_ROTATE_MODE_TANGENT, scale = rotateMode == SP_ROTATE_MODE_CHAIN_SCALE;
  85. int boneCount = self->bonesCount, spacesCount = tangents ? boneCount : boneCount + 1;
  86. spBone** bones = self->bones;
  87. spBone* pa;
  88. if (!translate && !rotate) return;
  89. if ((attachment == 0) || (attachment->super.super.type != SP_ATTACHMENT_PATH)) return;
  90. if (self->spacesCount != spacesCount) {
  91. if (self->spaces) FREE(self->spaces);
  92. self->spaces = MALLOC(float, spacesCount);
  93. self->spacesCount = spacesCount;
  94. }
  95. spaces = self->spaces;
  96. spaces[0] = 0;
  97. lengths = 0;
  98. spacing = self->spacing;
  99. if (scale || lengthSpacing) {
  100. if (scale) {
  101. if (self->lengthsCount != boneCount) {
  102. if (self->lengths) FREE(self->lengths);
  103. self->lengths = MALLOC(float, boneCount);
  104. self->lengthsCount = boneCount;
  105. }
  106. lengths = self->lengths;
  107. }
  108. for (i = 0, n = spacesCount - 1; i < n;) {
  109. spBone* bone = bones[i];
  110. length = bone->data->length, x = length * bone->a, y = length * bone->c;
  111. length = SQRT(x * x + y * y);
  112. if (scale) lengths[i] = length;
  113. spaces[++i] = lengthSpacing ? MAX(0, length + spacing) : spacing;
  114. }
  115. } else {
  116. for (i = 1; i < spacesCount; i++) {
  117. spaces[i] = spacing;
  118. }
  119. }
  120. positions = spPathConstraint_computeWorldPositions(self, attachment, spacesCount, tangents,
  121. data->positionMode == SP_POSITION_MODE_PERCENT, spacingMode == SP_SPACING_MODE_PERCENT);
  122. boneX = positions[0], boneY = positions[1], offsetRotation = self->data->offsetRotation;
  123. tip = 0;
  124. if (offsetRotation == 0)
  125. tip = rotateMode == SP_ROTATE_MODE_CHAIN;
  126. else {
  127. tip = 0;
  128. pa = self->target->bone;
  129. offsetRotation *= pa->a * pa->d - pa->b * pa->c > 0 ? DEG_RAD : -DEG_RAD;
  130. }
  131. for (i = 0, p = 3; i < boneCount; i++, p += 3) {
  132. spBone* bone = bones[i];
  133. CONST_CAST(float, bone->worldX) += (boneX - bone->worldX) * translateMix;
  134. CONST_CAST(float, bone->worldY) += (boneY - bone->worldY) * translateMix;
  135. x = positions[p], y = positions[p + 1], dx = x - boneX, dy = y - boneY;
  136. if (scale) {
  137. length = lengths[i];
  138. if (length != 0) {
  139. s = (SQRT(dx * dx + dy * dy) / length - 1) * rotateMix + 1;
  140. CONST_CAST(float, bone->a) *= s;
  141. CONST_CAST(float, bone->c) *= s;
  142. }
  143. }
  144. boneX = x;
  145. boneY = y;
  146. if (rotate) {
  147. float a = bone->a, b = bone->b, c = bone->c, d = bone->d, r, cosine, sine;
  148. if (tangents)
  149. r = positions[p - 1];
  150. else if (spaces[i + 1] == 0)
  151. r = positions[p + 2];
  152. else
  153. r = ATAN2(dy, dx);
  154. r -= ATAN2(c, a) - offsetRotation * DEG_RAD;
  155. if (tip) {
  156. cosine = COS(r);
  157. sine = SIN(r);
  158. length = bone->data->length;
  159. boneX += (length * (cosine * a - sine * c) - dx) * rotateMix;
  160. boneY += (length * (sine * a + cosine * c) - dy) * rotateMix;
  161. } else
  162. r += offsetRotation;
  163. if (r > PI)
  164. r -= PI2;
  165. else if (r < -PI)
  166. r += PI2;
  167. r *= rotateMix;
  168. cosine = COS(r);
  169. sine = SIN(r);
  170. CONST_CAST(float, bone->a) = cosine * a - sine * c;
  171. CONST_CAST(float, bone->b) = cosine * b - sine * d;
  172. CONST_CAST(float, bone->c) = sine * a + cosine * c;
  173. CONST_CAST(float, bone->d) = sine * b + cosine * d;
  174. }
  175. CONST_CAST(int, bone->appliedValid) = -1;
  176. }
  177. }
  178. static void _addBeforePosition(float p, float* temp, int i, float* out, int o) {
  179. float x1 = temp[i], y1 = temp[i + 1], dx = temp[i + 2] - x1, dy = temp[i + 3] - y1, r = ATAN2(dy, dx);
  180. out[o] = x1 + p * COS(r);
  181. out[o + 1] = y1 + p * SIN(r);
  182. out[o + 2] = r;
  183. }
  184. static void _addAfterPosition (float p, float* temp, int i, float* out, int o) {
  185. float x1 = temp[i + 2], y1 = temp[i + 3], dx = x1 - temp[i], dy = y1 - temp[i + 1], r = ATAN2(dy, dx);
  186. out[o] = x1 + p * COS(r);
  187. out[o + 1] = y1 + p * SIN(r);
  188. out[o + 2] = r;
  189. }
  190. static void _addCurvePosition (float p, float x1, float y1, float cx1, float cy1, float cx2, float cy2, float x2, float y2,
  191. float* out, int o, int/*bool*/tangents) {
  192. float tt, ttt, u, uu, uuu;
  193. float ut, ut3, uut3, utt3;
  194. float x, y;
  195. if (p == 0) p = 0.0001f;
  196. tt = p * p, ttt = tt * p, u = 1 - p, uu = u * u, uuu = uu * u;
  197. ut = u * p, ut3 = ut * 3, uut3 = u * ut3, utt3 = ut3 * p;
  198. x = x1 * uuu + cx1 * uut3 + cx2 * utt3 + x2 * ttt, y = y1 * uuu + cy1 * uut3 + cy2 * utt3 + y2 * ttt;
  199. out[o] = x;
  200. out[o + 1] = y;
  201. if (tangents) out[o + 2] = ATAN2(y - (y1 * uu + cy1 * ut * 2 + cy2 * tt), x - (x1 * uu + cx1 * ut * 2 + cx2 * tt));
  202. }
  203. float* spPathConstraint_computeWorldPositions(spPathConstraint* self, spPathAttachment* path, int spacesCount, int/*bool*/ tangents, int/*bool*/percentPosition, int/**/percentSpacing) {
  204. int i, o, w, curve, segment, /*bool*/closed, verticesLength, curveCount, prevCurve;
  205. float* out, *curves, *segments;
  206. float tmpx, tmpy, dddfx, dddfy, ddfx, ddfy, dfx, dfy, pathLength, curveLength, p;
  207. float x1, y1, cx1, cy1, cx2, cy2, x2, y2;
  208. spSlot* target = self->target;
  209. float position = self->position;
  210. float* spaces = self->spaces, *world = 0;
  211. if (self->positionsCount != spacesCount * 3 + 2) {
  212. if (self->positions) FREE(self->positions);
  213. self->positions = MALLOC(float, spacesCount * 3 + 2);
  214. self->positionsCount = spacesCount * 3 + 2;
  215. }
  216. out = self->positions;
  217. closed = path->closed;
  218. verticesLength = path->super.worldVerticesLength, curveCount = verticesLength / 6, prevCurve = PATHCONSTRAINT_NONE;
  219. if (!path->constantSpeed) {
  220. float* lengths = path->lengths;
  221. curveCount -= closed ? 1 : 2;
  222. pathLength = lengths[curveCount];
  223. if (percentPosition) position *= pathLength;
  224. if (percentSpacing) {
  225. for (i = 0; i < spacesCount; i++)
  226. spaces[i] *= pathLength;
  227. }
  228. if (self->worldCount != 8) {
  229. if (self->world) FREE(self->world);
  230. self->world = MALLOC(float, 8);
  231. self->worldCount = 8;
  232. }
  233. world = self->world;
  234. for (i = 0, o = 0, curve = 0; i < spacesCount; i++, o += 3) {
  235. float space = spaces[i];
  236. position += space;
  237. p = position;
  238. if (closed) {
  239. p = FMOD(p, pathLength);
  240. if (p < 0) p += pathLength;
  241. curve = 0;
  242. } else if (p < 0) {
  243. if (prevCurve != PATHCONSTRAINT_BEFORE) {
  244. prevCurve = PATHCONSTRAINT_BEFORE;
  245. spPathAttachment_computeWorldVertices1(path, target, 2, 4, world, 0);
  246. }
  247. _addBeforePosition(p, world, 0, out, o);
  248. continue;
  249. } else if (p > pathLength) {
  250. if (prevCurve != PATHCONSTRAINT_AFTER) {
  251. prevCurve = PATHCONSTRAINT_AFTER;
  252. spPathAttachment_computeWorldVertices1(path, target, verticesLength - 6, 4, world, 0);
  253. }
  254. _addAfterPosition(p - pathLength, world, 0, out, o);
  255. continue;
  256. }
  257. /* Determine curve containing position. */
  258. for (;; curve++) {
  259. float length = lengths[curve];
  260. if (p > length) continue;
  261. if (curve == 0)
  262. p /= length;
  263. else {
  264. float prev = lengths[curve - 1];
  265. p = (p - prev) / (length - prev);
  266. }
  267. break;
  268. }
  269. if (curve != prevCurve) {
  270. prevCurve = curve;
  271. if (closed && curve == curveCount) {
  272. spPathAttachment_computeWorldVertices1(path, target, verticesLength - 4, 4, world, 0);
  273. spPathAttachment_computeWorldVertices1(path, target, 0, 4, world, 4);
  274. } else
  275. spPathAttachment_computeWorldVertices1(path, target, curve * 6 + 2, 8, world, 0);
  276. }
  277. _addCurvePosition(p, world[0], world[1], world[2], world[3], world[4], world[5], world[6], world[7], out, o,
  278. tangents || (i > 0 && space == 0));
  279. }
  280. return out;
  281. }
  282. /* World vertices. */
  283. if (closed) {
  284. verticesLength += 2;
  285. if (self->worldCount != verticesLength) {
  286. if (self->world) FREE(self->world);
  287. self->world = MALLOC(float, verticesLength);
  288. self->worldCount = verticesLength;
  289. }
  290. world = self->world;
  291. spPathAttachment_computeWorldVertices1(path, target, 2, verticesLength - 4, world, 0);
  292. spPathAttachment_computeWorldVertices1(path, target, 0, 2, world, verticesLength - 4);
  293. world[verticesLength - 2] = world[0];
  294. world[verticesLength - 1] = world[1];
  295. } else {
  296. curveCount--;
  297. verticesLength -= 4;
  298. if (self->worldCount != verticesLength) {
  299. if (self->world) FREE(self->world);
  300. self->world = MALLOC(float, verticesLength);
  301. self->worldCount = verticesLength;
  302. }
  303. world = self->world;
  304. spPathAttachment_computeWorldVertices1(path, target, 2, verticesLength, world, 0);
  305. }
  306. /* Curve lengths. */
  307. if (self->curvesCount != curveCount) {
  308. if (self->curves) FREE(self->curves);
  309. self->curves = MALLOC(float, curveCount);
  310. self->curvesCount = curveCount;
  311. }
  312. curves = self->curves;
  313. pathLength = 0;
  314. x1 = world[0], y1 = world[1], cx1 = 0, cy1 = 0, cx2 = 0, cy2 = 0, x2 = 0, y2 = 0;
  315. for (i = 0, w = 2; i < curveCount; i++, w += 6) {
  316. cx1 = world[w];
  317. cy1 = world[w + 1];
  318. cx2 = world[w + 2];
  319. cy2 = world[w + 3];
  320. x2 = world[w + 4];
  321. y2 = world[w + 5];
  322. tmpx = (x1 - cx1 * 2 + cx2) * 0.1875f;
  323. tmpy = (y1 - cy1 * 2 + cy2) * 0.1875f;
  324. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.09375f;
  325. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.09375f;
  326. ddfx = tmpx * 2 + dddfx;
  327. ddfy = tmpy * 2 + dddfy;
  328. dfx = (cx1 - x1) * 0.75f + tmpx + dddfx * 0.16666667f;
  329. dfy = (cy1 - y1) * 0.75f + tmpy + dddfy * 0.16666667f;
  330. pathLength += SQRT(dfx * dfx + dfy * dfy);
  331. dfx += ddfx;
  332. dfy += ddfy;
  333. ddfx += dddfx;
  334. ddfy += dddfy;
  335. pathLength += SQRT(dfx * dfx + dfy * dfy);
  336. dfx += ddfx;
  337. dfy += ddfy;
  338. pathLength += SQRT(dfx * dfx + dfy * dfy);
  339. dfx += ddfx + dddfx;
  340. dfy += ddfy + dddfy;
  341. pathLength += SQRT(dfx * dfx + dfy * dfy);
  342. curves[i] = pathLength;
  343. x1 = x2;
  344. y1 = y2;
  345. }
  346. if (percentPosition) position *= pathLength;
  347. if (percentSpacing) {
  348. for (i = 0; i < spacesCount; i++)
  349. spaces[i] *= pathLength;
  350. }
  351. segments = self->segments;
  352. curveLength = 0;
  353. for (i = 0, o = 0, curve = 0, segment = 0; i < spacesCount; i++, o += 3) {
  354. float space = spaces[i];
  355. position += space;
  356. p = position;
  357. if (closed) {
  358. p = FMOD(p, pathLength);
  359. if (p < 0) p += pathLength;
  360. curve = 0;
  361. } else if (p < 0) {
  362. _addBeforePosition(p, world, 0, out, o);
  363. continue;
  364. } else if (p > pathLength) {
  365. _addAfterPosition(p - pathLength, world, verticesLength - 4, out, o);
  366. continue;
  367. }
  368. /* Determine curve containing position. */
  369. for (;; curve++) {
  370. float length = curves[curve];
  371. if (p > length) continue;
  372. if (curve == 0)
  373. p /= length;
  374. else {
  375. float prev = curves[curve - 1];
  376. p = (p - prev) / (length - prev);
  377. }
  378. break;
  379. }
  380. /* Curve segment lengths. */
  381. if (curve != prevCurve) {
  382. int ii;
  383. prevCurve = curve;
  384. ii = curve * 6;
  385. x1 = world[ii];
  386. y1 = world[ii + 1];
  387. cx1 = world[ii + 2];
  388. cy1 = world[ii + 3];
  389. cx2 = world[ii + 4];
  390. cy2 = world[ii + 5];
  391. x2 = world[ii + 6];
  392. y2 = world[ii + 7];
  393. tmpx = (x1 - cx1 * 2 + cx2) * 0.03f;
  394. tmpy = (y1 - cy1 * 2 + cy2) * 0.03f;
  395. dddfx = ((cx1 - cx2) * 3 - x1 + x2) * 0.006f;
  396. dddfy = ((cy1 - cy2) * 3 - y1 + y2) * 0.006f;
  397. ddfx = tmpx * 2 + dddfx;
  398. ddfy = tmpy * 2 + dddfy;
  399. dfx = (cx1 - x1) * 0.3f + tmpx + dddfx * 0.16666667f;
  400. dfy = (cy1 - y1) * 0.3f + tmpy + dddfy * 0.16666667f;
  401. curveLength = SQRT(dfx * dfx + dfy * dfy);
  402. segments[0] = curveLength;
  403. for (ii = 1; ii < 8; ii++) {
  404. dfx += ddfx;
  405. dfy += ddfy;
  406. ddfx += dddfx;
  407. ddfy += dddfy;
  408. curveLength += SQRT(dfx * dfx + dfy * dfy);
  409. segments[ii] = curveLength;
  410. }
  411. dfx += ddfx;
  412. dfy += ddfy;
  413. curveLength += SQRT(dfx * dfx + dfy * dfy);
  414. segments[8] = curveLength;
  415. dfx += ddfx + dddfx;
  416. dfy += ddfy + dddfy;
  417. curveLength += SQRT(dfx * dfx + dfy * dfy);
  418. segments[9] = curveLength;
  419. segment = 0;
  420. }
  421. /* Weight by segment length. */
  422. p *= curveLength;
  423. for (;; segment++) {
  424. float length = segments[segment];
  425. if (p > length) continue;
  426. if (segment == 0)
  427. p /= length;
  428. else {
  429. float prev = segments[segment - 1];
  430. p = segment + (p - prev) / (length - prev);
  431. }
  432. break;
  433. }
  434. _addCurvePosition(p * 0.1f, x1, y1, cx1, cy1, cx2, cy2, x2, y2, out, o, tangents || (i > 0 && space == 0));
  435. }
  436. return out;
  437. }