SkeletonBounds.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/SkeletonBounds.h>
  31. #include <limits.h>
  32. #include <spine/extension.h>
  33. spPolygon* spPolygon_create (int capacity) {
  34. spPolygon* self = NEW(spPolygon);
  35. self->capacity = capacity;
  36. CONST_CAST(float*, self->vertices) = MALLOC(float, capacity);
  37. return self;
  38. }
  39. void spPolygon_dispose (spPolygon* self) {
  40. FREE(self->vertices);
  41. FREE(self);
  42. }
  43. int/*bool*/spPolygon_containsPoint (spPolygon* self, float x, float y) {
  44. int prevIndex = self->count - 2;
  45. int inside = 0;
  46. int i;
  47. for (i = 0; i < self->count; i += 2) {
  48. float vertexY = self->vertices[i + 1];
  49. float prevY = self->vertices[prevIndex + 1];
  50. if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
  51. float vertexX = self->vertices[i];
  52. if (vertexX + (y - vertexY) / (prevY - vertexY) * (self->vertices[prevIndex] - vertexX) < x) inside = !inside;
  53. }
  54. prevIndex = i;
  55. }
  56. return inside;
  57. }
  58. int/*bool*/spPolygon_intersectsSegment (spPolygon* self, float x1, float y1, float x2, float y2) {
  59. float width12 = x1 - x2, height12 = y1 - y2;
  60. float det1 = x1 * y2 - y1 * x2;
  61. float x3 = self->vertices[self->count - 2], y3 = self->vertices[self->count - 1];
  62. int i;
  63. for (i = 0; i < self->count; i += 2) {
  64. float x4 = self->vertices[i], y4 = self->vertices[i + 1];
  65. float det2 = x3 * y4 - y3 * x4;
  66. float width34 = x3 - x4, height34 = y3 - y4;
  67. float det3 = width12 * height34 - height12 * width34;
  68. float x = (det1 * width34 - width12 * det2) / det3;
  69. if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
  70. float y = (det1 * height34 - height12 * det2) / det3;
  71. if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return 1;
  72. }
  73. x3 = x4;
  74. y3 = y4;
  75. }
  76. return 0;
  77. }
  78. /**/
  79. typedef struct {
  80. spSkeletonBounds super;
  81. int capacity;
  82. } _spSkeletonBounds;
  83. spSkeletonBounds* spSkeletonBounds_create () {
  84. return SUPER(NEW(_spSkeletonBounds));
  85. }
  86. void spSkeletonBounds_dispose (spSkeletonBounds* self) {
  87. int i;
  88. for (i = 0; i < SUB_CAST(_spSkeletonBounds, self)->capacity; ++i)
  89. if (self->polygons[i]) spPolygon_dispose(self->polygons[i]);
  90. FREE(self->polygons);
  91. FREE(self->boundingBoxes);
  92. FREE(self);
  93. }
  94. void spSkeletonBounds_update (spSkeletonBounds* self, spSkeleton* skeleton, int/*bool*/updateAabb) {
  95. int i;
  96. _spSkeletonBounds* internal = SUB_CAST(_spSkeletonBounds, self);
  97. if (internal->capacity < skeleton->slotsCount) {
  98. spPolygon** newPolygons;
  99. FREE(self->boundingBoxes);
  100. self->boundingBoxes = MALLOC(spBoundingBoxAttachment*, skeleton->slotsCount);
  101. newPolygons = CALLOC(spPolygon*, skeleton->slotsCount);
  102. memcpy(newPolygons, self->polygons, sizeof(spPolygon*) * internal->capacity);
  103. FREE(self->polygons);
  104. self->polygons = newPolygons;
  105. internal->capacity = skeleton->slotsCount;
  106. }
  107. self->minX = (float)INT_MAX;
  108. self->minY = (float)INT_MAX;
  109. self->maxX = (float)INT_MIN;
  110. self->maxY = (float)INT_MIN;
  111. self->count = 0;
  112. for (i = 0; i < skeleton->slotsCount; ++i) {
  113. spPolygon* polygon;
  114. spBoundingBoxAttachment* boundingBox;
  115. spSlot* slot = skeleton->slots[i];
  116. spAttachment* attachment = slot->attachment;
  117. if (!attachment || attachment->type != SP_ATTACHMENT_BOUNDING_BOX) continue;
  118. boundingBox = (spBoundingBoxAttachment*)attachment;
  119. self->boundingBoxes[self->count] = boundingBox;
  120. polygon = self->polygons[self->count];
  121. if (!polygon || polygon->capacity < boundingBox->super.worldVerticesLength) {
  122. if (polygon) spPolygon_dispose(polygon);
  123. self->polygons[self->count] = polygon = spPolygon_create(boundingBox->super.worldVerticesLength);
  124. }
  125. polygon->count = boundingBox->super.worldVerticesLength;
  126. spBoundingBoxAttachment_computeWorldVertices(boundingBox, slot, polygon->vertices);
  127. if (updateAabb) {
  128. int ii = 0;
  129. for (; ii < polygon->count; ii += 2) {
  130. float x = polygon->vertices[ii];
  131. float y = polygon->vertices[ii + 1];
  132. if (x < self->minX) self->minX = x;
  133. if (y < self->minY) self->minY = y;
  134. if (x > self->maxX) self->maxX = x;
  135. if (y > self->maxY) self->maxY = y;
  136. }
  137. }
  138. self->count++;
  139. }
  140. }
  141. int/*bool*/spSkeletonBounds_aabbContainsPoint (spSkeletonBounds* self, float x, float y) {
  142. return x >= self->minX && x <= self->maxX && y >= self->minY && y <= self->maxY;
  143. }
  144. int/*bool*/spSkeletonBounds_aabbIntersectsSegment (spSkeletonBounds* self, float x1, float y1, float x2, float y2) {
  145. float m, x, y;
  146. if ((x1 <= self->minX && x2 <= self->minX) || (y1 <= self->minY && y2 <= self->minY) || (x1 >= self->maxX && x2 >= self->maxX)
  147. || (y1 >= self->maxY && y2 >= self->maxY)) return 0;
  148. m = (y2 - y1) / (x2 - x1);
  149. y = m * (self->minX - x1) + y1;
  150. if (y > self->minY && y < self->maxY) return 1;
  151. y = m * (self->maxX - x1) + y1;
  152. if (y > self->minY && y < self->maxY) return 1;
  153. x = (self->minY - y1) / m + x1;
  154. if (x > self->minX && x < self->maxX) return 1;
  155. x = (self->maxY - y1) / m + x1;
  156. if (x > self->minX && x < self->maxX) return 1;
  157. return 0;
  158. }
  159. int/*bool*/spSkeletonBounds_aabbIntersectsSkeleton (spSkeletonBounds* self, spSkeletonBounds* bounds) {
  160. return self->minX < bounds->maxX && self->maxX > bounds->minX && self->minY < bounds->maxY && self->maxY > bounds->minY;
  161. }
  162. spBoundingBoxAttachment* spSkeletonBounds_containsPoint (spSkeletonBounds* self, float x, float y) {
  163. int i;
  164. for (i = 0; i < self->count; ++i)
  165. if (spPolygon_containsPoint(self->polygons[i], x, y)) return self->boundingBoxes[i];
  166. return 0;
  167. }
  168. spBoundingBoxAttachment* spSkeletonBounds_intersectsSegment (spSkeletonBounds* self, float x1, float y1, float x2, float y2) {
  169. int i;
  170. for (i = 0; i < self->count; ++i)
  171. if (spPolygon_intersectsSegment(self->polygons[i], x1, y1, x2, y2)) return self->boundingBoxes[i];
  172. return 0;
  173. }
  174. spPolygon* spSkeletonBounds_getPolygon (spSkeletonBounds* self, spBoundingBoxAttachment* boundingBox) {
  175. int i;
  176. for (i = 0; i < self->count; ++i)
  177. if (self->boundingBoxes[i] == boundingBox) return self->polygons[i];
  178. return 0;
  179. }