SkeletonBounds.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifdef SPINE_UE4
  30. #include "SpinePluginPrivatePCH.h"
  31. #endif
  32. #include <spine/SkeletonBounds.h>
  33. #include <spine/Skeleton.h>
  34. #include <spine/Bone.h>
  35. #include <spine/BoundingBoxAttachment.h>
  36. #include <spine/Slot.h>
  37. #include <float.h>
  38. using namespace spine;
  39. SkeletonBounds::SkeletonBounds() : _minX(0), _minY(0), _maxX(0), _maxY(0) {
  40. }
  41. void SkeletonBounds::update(Skeleton &skeleton, bool updateAabb) {
  42. Vector<Slot *> &slots = skeleton._slots;
  43. size_t slotCount = slots.size();
  44. _boundingBoxes.clear();
  45. for (size_t i = 0, n = _polygons.size(); i < n; ++i) {
  46. _polygonPool.add(_polygons[i]);
  47. }
  48. _polygons.clear();
  49. for (size_t i = 0; i < slotCount; i++) {
  50. Slot *slot = slots[i];
  51. if (!slot->getBone().isActive()) continue;
  52. Attachment *attachment = slot->getAttachment();
  53. if (attachment == NULL || !attachment->getRTTI().instanceOf(BoundingBoxAttachment::rtti)) continue;
  54. BoundingBoxAttachment *boundingBox = static_cast<BoundingBoxAttachment *>(attachment);
  55. _boundingBoxes.add(boundingBox);
  56. spine::Polygon *polygonP = NULL;
  57. size_t poolCount = _polygonPool.size();
  58. if (poolCount > 0) {
  59. polygonP = _polygonPool[poolCount - 1];
  60. _polygonPool.removeAt(poolCount - 1);
  61. } else
  62. polygonP = new(__FILE__, __LINE__) Polygon();
  63. _polygons.add(polygonP);
  64. Polygon &polygon = *polygonP;
  65. size_t count = boundingBox->getWorldVerticesLength();
  66. polygon._count = count;
  67. if (polygon._vertices.size() < count) {
  68. polygon._vertices.setSize(count, 0);
  69. }
  70. boundingBox->computeWorldVertices(*slot, polygon._vertices);
  71. }
  72. if (updateAabb)
  73. aabbCompute();
  74. else {
  75. _minX = FLT_MIN;
  76. _minY = FLT_MIN;
  77. _maxX = FLT_MAX;
  78. _maxY = FLT_MAX;
  79. }
  80. }
  81. bool SkeletonBounds::aabbcontainsPoint(float x, float y) {
  82. return x >= _minX && x <= _maxX && y >= _minY && y <= _maxY;
  83. }
  84. bool SkeletonBounds::aabbintersectsSegment(float x1, float y1, float x2, float y2) {
  85. float minX = _minX;
  86. float minY = _minY;
  87. float maxX = _maxX;
  88. float maxY = _maxY;
  89. if ((x1 <= minX && x2 <= minX) || (y1 <= minY && y2 <= minY) || (x1 >= maxX && x2 >= maxX) ||
  90. (y1 >= maxY && y2 >= maxY)) {
  91. return false;
  92. }
  93. float m = (y2 - y1) / (x2 - x1);
  94. float y = m * (minX - x1) + y1;
  95. if (y > minY && y < maxY) return true;
  96. y = m * (maxX - x1) + y1;
  97. if (y > minY && y < maxY) return true;
  98. float x = (minY - y1) / m + x1;
  99. if (x > minX && x < maxX) return true;
  100. x = (maxY - y1) / m + x1;
  101. if (x > minX && x < maxX) return true;
  102. return false;
  103. }
  104. bool SkeletonBounds::aabbIntersectsSkeleton(SkeletonBounds bounds) {
  105. return _minX < bounds._maxX && _maxX > bounds._minX && _minY < bounds._maxY && _maxY > bounds._minY;
  106. }
  107. bool SkeletonBounds::containsPoint(spine::Polygon *polygon, float x, float y) {
  108. Vector<float> &vertices = polygon->_vertices;
  109. int nn = polygon->_count;
  110. int prevIndex = nn - 2;
  111. bool inside = false;
  112. for (int ii = 0; ii < nn; ii += 2) {
  113. float vertexY = vertices[ii + 1];
  114. float prevY = vertices[prevIndex + 1];
  115. if ((vertexY < y && prevY >= y) || (prevY < y && vertexY >= y)) {
  116. float vertexX = vertices[ii];
  117. if (vertexX + (y - vertexY) / (prevY - vertexY) * (vertices[prevIndex] - vertexX) < x) {
  118. inside = !inside;
  119. }
  120. }
  121. prevIndex = ii;
  122. }
  123. return inside;
  124. }
  125. BoundingBoxAttachment *SkeletonBounds::containsPoint(float x, float y) {
  126. for (size_t i = 0, n = _polygons.size(); i < n; ++i)
  127. if (containsPoint(_polygons[i], x, y)) return _boundingBoxes[i];
  128. return NULL;
  129. }
  130. BoundingBoxAttachment *SkeletonBounds::intersectsSegment(float x1, float y1, float x2, float y2) {
  131. for (size_t i = 0, n = _polygons.size(); i < n; ++i)
  132. if (intersectsSegment(_polygons[i], x1, y1, x2, y2)) return _boundingBoxes[i];
  133. return NULL;
  134. }
  135. bool SkeletonBounds::intersectsSegment(spine::Polygon *polygon, float x1, float y1, float x2, float y2) {
  136. Vector<float> &vertices = polygon->_vertices;
  137. size_t nn = polygon->_count;
  138. float width12 = x1 - x2, height12 = y1 - y2;
  139. float det1 = x1 * y2 - y1 * x2;
  140. float x3 = vertices[nn - 2], y3 = vertices[nn - 1];
  141. for (size_t ii = 0; ii < nn; ii += 2) {
  142. float x4 = vertices[ii], y4 = vertices[ii + 1];
  143. float det2 = x3 * y4 - y3 * x4;
  144. float width34 = x3 - x4, height34 = y3 - y4;
  145. float det3 = width12 * height34 - height12 * width34;
  146. float x = (det1 * width34 - width12 * det2) / det3;
  147. if (((x >= x3 && x <= x4) || (x >= x4 && x <= x3)) && ((x >= x1 && x <= x2) || (x >= x2 && x <= x1))) {
  148. float y = (det1 * height34 - height12 * det2) / det3;
  149. if (((y >= y3 && y <= y4) || (y >= y4 && y <= y3)) && ((y >= y1 && y <= y2) || (y >= y2 && y <= y1))) {
  150. return true;
  151. }
  152. }
  153. x3 = x4;
  154. y3 = y4;
  155. }
  156. return false;
  157. }
  158. spine::Polygon *SkeletonBounds::getPolygon(BoundingBoxAttachment *attachment) {
  159. int index = _boundingBoxes.indexOf(attachment);
  160. return index == -1 ? NULL : _polygons[index];
  161. }
  162. float SkeletonBounds::getWidth() {
  163. return _maxX - _minX;
  164. }
  165. float SkeletonBounds::getHeight() {
  166. return _maxY - _minY;
  167. }
  168. void SkeletonBounds::aabbCompute() {
  169. float minX = FLT_MIN;
  170. float minY = FLT_MIN;
  171. float maxX = FLT_MAX;
  172. float maxY = FLT_MAX;
  173. for (size_t i = 0, n = _polygons.size(); i < n; ++i) {
  174. spine::Polygon *polygon = _polygons[i];
  175. Vector<float> &vertices = polygon->_vertices;
  176. for (int ii = 0, nn = polygon->_count; ii < nn; ii += 2) {
  177. float x = vertices[ii];
  178. float y = vertices[ii + 1];
  179. minX = MathUtil::min(minX, x);
  180. minY = MathUtil::min(minY, y);
  181. maxX = MathUtil::max(maxX, x);
  182. maxY = MathUtil::max(maxY, y);
  183. }
  184. }
  185. _minX = minX;
  186. _minY = minY;
  187. _maxX = maxX;
  188. _maxY = maxY;
  189. }