SkeletonBounds.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /******************************************************************************
  2. * Spine Runtimes Software License
  3. * Version 2
  4. *
  5. * Copyright (c) 2013, Esoteric Software
  6. * All rights reserved.
  7. *
  8. * You are granted a perpetual, non-exclusive, non-sublicensable and
  9. * non-transferable license to install, execute and perform the Spine Runtimes
  10. * Software (the "Software") solely for internal use. Without the written
  11. * permission of Esoteric Software, you may not (a) modify, translate, adapt or
  12. * otherwise create derivative works, improvements of the Software or develop
  13. * new applications using the Software or (b) remove, delete, alter or obscure
  14. * any trademarks or any copyright, trademark, patent or other intellectual
  15. * property or proprietary rights notices on or in the Software, including
  16. * any copy thereof. Redistributions in binary or source form must include
  17. * this license and terms. THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  19. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  20. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTARE BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. *****************************************************************************/
  28. #include <spine/SkeletonBounds.h>
  29. #include <limits.h>
  30. #include <spine/extension.h>
  31. spPolygon* spPolygon_create (int capacity) {
  32. spPolygon* self = NEW(spPolygon);
  33. self->capacity = capacity;
  34. CONST_CAST(float*, self->vertices) = MALLOC(float, capacity);
  35. return self;
  36. }
  37. void spPolygon_dispose (spPolygon* self) {
  38. FREE(self->vertices);
  39. FREE(self);
  40. }
  41. int/*bool*/spPolygon_containsPoint (spPolygon* self, float x, float y) {
  42. int prevIndex = self->count - 2;
  43. int inside = 0;
  44. int i;
  45. for (i = 0; i < self->count; i += 2) {
  46. float vertexY = self->vertices[i + 1];
  47. float prevY = self->vertices[prevIndex + 1];
  48. if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
  49. float vertexX = self->vertices[i];
  50. if (vertexX + (y - vertexY) / (prevY - vertexY) * (self->vertices[prevIndex] - vertexX) < x) inside = !inside;
  51. }
  52. prevIndex = i;
  53. }
  54. return inside;
  55. }
  56. int/*bool*/spPolygon_intersectsSegment (spPolygon* self, float x1, float y1, float x2, float y2) {
  57. float width12 = x1 - x2, height12 = y1 - y2;
  58. float det1 = x1 * y2 - y1 * x2;
  59. float x3 = self->vertices[self->count - 2], y3 = self->vertices[self->count - 1];
  60. int i;
  61. for (i = 0; i < self->count; i += 2) {
  62. float x4 = self->vertices[i], y4 = self->vertices[i + 1];
  63. float det2 = x3 * y4 - y3 * x4;
  64. float width34 = x3 - x4, height34 = y3 - y4;
  65. float det3 = width12 * height34 - height12 * width34;
  66. float x = (det1 * width34 - width12 * det2) / det3;
  67. if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
  68. float y = (det1 * height34 - height12 * det2) / det3;
  69. if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) return 1;
  70. }
  71. x3 = x4;
  72. y3 = y4;
  73. }
  74. return 0;
  75. }
  76. /**/
  77. typedef struct {
  78. spSkeletonBounds super;
  79. int capacity;
  80. } _spSkeletonBounds;
  81. spSkeletonBounds* spSkeletonBounds_create () {
  82. return SUPER(NEW(_spSkeletonBounds));
  83. }
  84. void spSkeletonBounds_dispose (spSkeletonBounds* self) {
  85. int i;
  86. for (i = 0; i < SUB_CAST(_spSkeletonBounds, self)->capacity; ++i)
  87. if (self->polygons[i]) spPolygon_dispose(self->polygons[i]);
  88. FREE(self->polygons);
  89. FREE(self->boundingBoxes);
  90. FREE(self);
  91. }
  92. void spSkeletonBounds_update (spSkeletonBounds* self, spSkeleton* skeleton, int/*bool*/updateAabb) {
  93. int i;
  94. _spSkeletonBounds* internal = SUB_CAST(_spSkeletonBounds, self);
  95. if (internal->capacity < skeleton->slotCount) {
  96. spPolygon** newPolygons;
  97. FREE(self->boundingBoxes);
  98. self->boundingBoxes = MALLOC(spBoundingBoxAttachment*, skeleton->slotCount);
  99. newPolygons = CALLOC(spPolygon*, skeleton->slotCount);
  100. memcpy(newPolygons, self->polygons, internal->capacity);
  101. FREE(self->polygons);
  102. self->polygons = newPolygons;
  103. internal->capacity = skeleton->slotCount;
  104. }
  105. self->minX = (float)INT_MAX;
  106. self->minY = (float)INT_MAX;
  107. self->maxX = (float)INT_MIN;
  108. self->maxY = (float)INT_MIN;
  109. self->count = 0;
  110. for (i = 0; i < skeleton->slotCount; ++i) {
  111. spPolygon* polygon;
  112. spBoundingBoxAttachment* boundingBox;
  113. spSlot* slot = skeleton->slots[i];
  114. spAttachment* attachment = slot->attachment;
  115. if (!attachment || attachment->type != ATTACHMENT_BOUNDING_BOX) continue;
  116. boundingBox = (spBoundingBoxAttachment*)attachment;
  117. self->boundingBoxes[self->count] = boundingBox;
  118. polygon = self->polygons[self->count];
  119. if (!polygon || polygon->capacity < boundingBox->verticesCount) {
  120. if (polygon) spPolygon_dispose(polygon);
  121. self->polygons[self->count] = polygon = spPolygon_create(boundingBox->verticesCount);
  122. }
  123. polygon->count = boundingBox->verticesCount;
  124. spBoundingBoxAttachment_computeWorldVertices(boundingBox, skeleton->x, skeleton->y, slot->bone, polygon->vertices);
  125. if (updateAabb) {
  126. int ii = 0;
  127. for (; ii < polygon->count; ii += 2) {
  128. float x = polygon->vertices[ii];
  129. float y = polygon->vertices[ii + 1];
  130. if (x < self->minX) self->minX = x;
  131. if (y < self->minY) self->minY = y;
  132. if (x > self->maxX) self->maxX = x;
  133. if (y > self->maxY) self->maxY = y;
  134. }
  135. }
  136. ++self->count;
  137. }
  138. }
  139. int/*bool*/spSkeletonBounds_aabbContainsPoint (spSkeletonBounds* self, float x, float y) {
  140. return x >= self->minX && x <= self->maxX && y >= self->minY && y <= self->maxY;
  141. }
  142. int/*bool*/spSkeletonBounds_aabbIntersectsSegment (spSkeletonBounds* self, float x1, float y1, float x2, float y2) {
  143. float m, x, y;
  144. if ((x1 <= self->minX && x2 <= self->minX) || (y1 <= self->minY && y2 <= self->minY) || (x1 >= self->maxX && x2 >= self->maxX)
  145. || (y1 >= self->maxY && y2 >= self->maxY)) return 0;
  146. m = (y2 - y1) / (x2 - x1);
  147. y = m * (self->minX - x1) + y1;
  148. if (y > self->minY && y < self->maxY) return 1;
  149. y = m * (self->maxX - x1) + y1;
  150. if (y > self->minY && y < self->maxY) return 1;
  151. x = (self->minY - y1) / m + x1;
  152. if (x > self->minX && x < self->maxX) return 1;
  153. x = (self->maxY - y1) / m + x1;
  154. if (x > self->minX && x < self->maxX) return 1;
  155. return 0;
  156. }
  157. int/*bool*/spSkeletonBounds_aabbIntersectsSkeleton (spSkeletonBounds* self, spSkeletonBounds* bounds) {
  158. return self->minX < bounds->maxX && self->maxX > bounds->minX && self->minY < bounds->maxY && self->maxY > bounds->minY;
  159. }
  160. spBoundingBoxAttachment* spSkeletonBounds_containsPoint (spSkeletonBounds* self, float x, float y) {
  161. int i;
  162. for (i = 0; i < self->count; ++i)
  163. if (spPolygon_containsPoint(self->polygons[i], x, y)) return self->boundingBoxes[i];
  164. return 0;
  165. }
  166. spBoundingBoxAttachment* spSkeletonBounds_intersectsSegment (spSkeletonBounds* self, float x1, float y1, float x2, float y2) {
  167. int i;
  168. for (i = 0; i < self->count; ++i)
  169. if (spPolygon_intersectsSegment(self->polygons[i], x1, y1, x2, y2)) return self->boundingBoxes[i];
  170. return 0;
  171. }
  172. spPolygon* spSkeletonBounds_getPolygon (spSkeletonBounds* self, spBoundingBoxAttachment* boundingBox) {
  173. int i;
  174. for (i = 0; i < self->count; ++i)
  175. if (self->boundingBoxes[i] == boundingBox) return self->polygons[i];
  176. return 0;
  177. }