CCPUSimpleSpline.cpp 6.6 KB

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