CCPUSimpleSpline.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUSimpleSpline.h"
  23. #include "base/ccMacros.h"
  24. NS_CC_BEGIN
  25. //---------------------------------------------------------------------
  26. PUSimpleSpline::PUSimpleSpline()
  27. {
  28. // Set up matrix
  29. // Hermite polynomial
  30. _coeffs.m[0] = 2;
  31. _coeffs.m[1] = -2;
  32. _coeffs.m[2] = 1;
  33. _coeffs.m[3] = 1;
  34. _coeffs.m[4] = -3;
  35. _coeffs.m[5] = 3;
  36. _coeffs.m[6] = -2;
  37. _coeffs.m[7] = -1;
  38. _coeffs.m[8] = 0;
  39. _coeffs.m[9] = 0;
  40. _coeffs.m[10] = 1;
  41. _coeffs.m[11] = 0;
  42. _coeffs.m[12] = 1;
  43. _coeffs.m[13] = 0;
  44. _coeffs.m[14] = 0;
  45. _coeffs.m[15] = 0;
  46. _autoCalc = true;
  47. }
  48. //---------------------------------------------------------------------
  49. PUSimpleSpline::~PUSimpleSpline()
  50. {
  51. }
  52. //---------------------------------------------------------------------
  53. void PUSimpleSpline::addPoint(const Vec3& p)
  54. {
  55. _points.push_back(p);
  56. if (_autoCalc)
  57. {
  58. recalcTangents();
  59. }
  60. }
  61. //---------------------------------------------------------------------
  62. Vec3 PUSimpleSpline::interpolate(float t) const
  63. {
  64. // Currently assumes points are evenly spaced, will cause velocity
  65. // change where this is not the case
  66. // TODO: base on arclength?
  67. // Work out which segment this is in
  68. float fSeg = t * (_points.size() - 1);
  69. unsigned int segIdx = (unsigned int)fSeg;
  70. // Apportion t
  71. t = fSeg - segIdx;
  72. return interpolate(segIdx, t);
  73. }
  74. //---------------------------------------------------------------------
  75. Vec3 PUSimpleSpline::interpolate(unsigned int fromIndex, float t) const
  76. {
  77. // Bounds check
  78. CCASSERT (fromIndex < _points.size(), "fromIndex out of bounds");
  79. if ((fromIndex + 1) == _points.size())
  80. {
  81. // Duff request, cannot blend to nothing
  82. // Just return source
  83. return _points[fromIndex];
  84. }
  85. // Fast special cases
  86. if (t == 0.0f)
  87. {
  88. return _points[fromIndex];
  89. }
  90. else if(t == 1.0f)
  91. {
  92. return _points[fromIndex + 1];
  93. }
  94. // Real interpolation
  95. // Form a vector of powers of t
  96. float t2, t3;
  97. t2 = t * t;
  98. t3 = t2 * t;
  99. Vec4 powers(t3, t2, t, 1);
  100. // Algorithm is ret = powers * mCoeffs * Matrix4(point1, point2, tangent1, tangent2)
  101. const Vec3& point1 = _points[fromIndex];
  102. const Vec3& point2 = _points[fromIndex+1];
  103. const Vec3& tan1 = _tangents[fromIndex];
  104. const Vec3& tan2 = _tangents[fromIndex+1];
  105. Mat4 pt;
  106. pt.m[0] = point1.x;
  107. pt.m[1] = point1.y;
  108. pt.m[2] = point1.z;
  109. pt.m[3] = 1.0f;
  110. pt.m[4] = point2.x;
  111. pt.m[5] = point2.y;
  112. pt.m[6] = point2.z;
  113. pt.m[7] = 1.0f;
  114. pt.m[8] = tan1.x;
  115. pt.m[9] = tan1.y;
  116. pt.m[10] = tan1.z;
  117. pt.m[11] = 1.0f;
  118. pt.m[12] = tan2.x;
  119. pt.m[13] = tan2.y;
  120. pt.m[14] = tan2.z;
  121. pt.m[15] = 1.0f;
  122. Vec4 ret = pt * _coeffs * powers;
  123. return Vec3(ret.x, ret.y, ret.z);
  124. }
  125. //---------------------------------------------------------------------
  126. void PUSimpleSpline::recalcTangents(void)
  127. {
  128. // Catmull-Rom approach
  129. //
  130. // tangent[i] = 0.5 * (point[i+1] - point[i-1])
  131. //
  132. // Assume endpoint tangents are parallel with line with neighbour
  133. size_t i, numPoints;
  134. bool isClosed;
  135. numPoints = _points.size();
  136. if (numPoints < 2)
  137. {
  138. // Can't do anything yet
  139. return;
  140. }
  141. // Closed or open?
  142. if (_points[0] == _points[numPoints-1])
  143. {
  144. isClosed = true;
  145. }
  146. else
  147. {
  148. isClosed = false;
  149. }
  150. _tangents.resize(numPoints);
  151. for(i = 0; i < numPoints; ++i)
  152. {
  153. if (i ==0)
  154. {
  155. // Special case start
  156. if (isClosed)
  157. {
  158. // Use numPoints-2 since numPoints-1 is the last point and == [0]
  159. _tangents[i] = 0.5 * (_points[1] - _points[numPoints-2]);
  160. }
  161. else
  162. {
  163. _tangents[i] = 0.5 * (_points[1] - _points[0]);
  164. }
  165. }
  166. else if (i == numPoints-1)
  167. {
  168. // Special case end
  169. if (isClosed)
  170. {
  171. // Use same tangent as already calculated for [0]
  172. _tangents[i] = _tangents[0];
  173. }
  174. else
  175. {
  176. _tangents[i] = 0.5 * (_points[i] - _points[i-1]);
  177. }
  178. }
  179. else
  180. {
  181. _tangents[i] = 0.5 * (_points[i+1] - _points[i-1]);
  182. }
  183. }
  184. }
  185. //---------------------------------------------------------------------
  186. const Vec3& PUSimpleSpline::getPoint(unsigned short index) const
  187. {
  188. CCASSERT (index < _points.size(), "Point index is out of bounds!!");
  189. return _points[index];
  190. }
  191. //---------------------------------------------------------------------
  192. unsigned short PUSimpleSpline::getNumPoints(void) const
  193. {
  194. return (unsigned short)_points.size();
  195. }
  196. //---------------------------------------------------------------------
  197. void PUSimpleSpline::clear(void)
  198. {
  199. _points.clear();
  200. _tangents.clear();
  201. }
  202. //---------------------------------------------------------------------
  203. void PUSimpleSpline::updatePoint(unsigned short index, const Vec3& value)
  204. {
  205. CCASSERT (index < _points.size(), "Point index is out of bounds!!");
  206. _points[index] = value;
  207. if (_autoCalc)
  208. {
  209. recalcTangents();
  210. }
  211. }
  212. //---------------------------------------------------------------------
  213. void PUSimpleSpline::setAutoCalculate(bool autoCalc)
  214. {
  215. _autoCalc = autoCalc;
  216. }
  217. NS_CC_END