Curve.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. #ifndef CURVE_H_
  2. #define CURVE_H_
  3. namespace gameplay
  4. {
  5. /**
  6. * Represents an n-dimensional curve.
  7. */
  8. class Curve
  9. {
  10. friend class Animation;
  11. friend class AnimationClip;
  12. friend class AnimationController;
  13. friend class MeshSkin;
  14. public:
  15. /**
  16. * Types of interpolation.
  17. *
  18. * Defines how the points in the curve are connected.
  19. *
  20. * Note: InterpolationType::BEZIER requires control points and InterpolationType::HERMITE requires tangents.
  21. */
  22. enum InterpolationType
  23. {
  24. /**
  25. * Bezier Interpolation.
  26. *
  27. * Requires that two control points are set for each segment.
  28. */
  29. BEZIER,
  30. /**
  31. * B-Spline Interpolation.
  32. *
  33. * Uses the points as control points, and the curve is guaranteed to only pass through the
  34. * first and last point.
  35. */
  36. BSPLINE,
  37. /**
  38. * Flat Interpolation.
  39. *
  40. * A form of Hermite interpolation that generates flat tangents for you. The tangents have a value equal to 0.
  41. */
  42. FLAT,
  43. /**
  44. * Hermite Interpolation.
  45. *
  46. * Requires that two tangents for each segment.
  47. */
  48. HERMITE,
  49. /**
  50. * Linear Interpolation.
  51. */
  52. LINEAR,
  53. /**
  54. * Smooth Interpolation.
  55. *
  56. * A form of Hermite interpolation that generates tangents for each segment based on the points prior to and after the segment.
  57. */
  58. SMOOTH,
  59. /**
  60. * Discrete Interpolation.
  61. */
  62. STEP,
  63. /**
  64. * Quadratic-In Interpolation.
  65. */
  66. QUADRATIC_IN,
  67. /**
  68. * Quadratic-Out Interpolation.
  69. */
  70. QUADRATIC_OUT,
  71. /**
  72. * Quadratic-In-Out Interpolation.
  73. */
  74. QUADRATIC_IN_OUT,
  75. /**
  76. * Quadratic-Out-In Interpolation.
  77. */
  78. QUADRATIC_OUT_IN,
  79. /**
  80. * Cubic-In Interpolation.
  81. */
  82. CUBIC_IN,
  83. /**
  84. * Cubic-Out Interpolation.
  85. */
  86. CUBIC_OUT,
  87. /**
  88. * Cubic-In-Out Interpolation.
  89. */
  90. CUBIC_IN_OUT,
  91. /**
  92. * Cubic-Out-In Interpolation.
  93. */
  94. CUBIC_OUT_IN,
  95. /**
  96. * Quartic-In Interpolation.
  97. */
  98. QUARTIC_IN,
  99. /**
  100. * Quartic-Out Interpolation.
  101. */
  102. QUARTIC_OUT,
  103. /**
  104. * Quartic-In-Out Interpolation.
  105. */
  106. QUARTIC_IN_OUT,
  107. /**
  108. * Quartic-Out-In Interpolation.
  109. */
  110. QUARTIC_OUT_IN,
  111. /**
  112. * Quintic-In Interpolation.
  113. */
  114. QUINTIC_IN,
  115. /**
  116. * Quintic-Out Interpolation.
  117. */
  118. QUINTIC_OUT,
  119. /**
  120. * Quintic-In-Out Interpolation.
  121. */
  122. QUINTIC_IN_OUT,
  123. /**
  124. * Quintic-Out-In Interpolation.
  125. */
  126. QUINTIC_OUT_IN,
  127. /**
  128. * Sine-In Interpolation.
  129. */
  130. SINE_IN,
  131. /**
  132. * Sine-Out Interpolation.
  133. */
  134. SINE_OUT,
  135. /**
  136. * Sine-In-Out Interpolation.
  137. */
  138. SINE_IN_OUT,
  139. /**
  140. * Sine-Out-In Interpolation.
  141. */
  142. SINE_OUT_IN,
  143. /**
  144. * Exponential-In Interpolation.
  145. */
  146. EXPONENTIAL_IN,
  147. /**
  148. * Exponential-Out Interpolation.
  149. */
  150. EXPONENTIAL_OUT,
  151. /**
  152. * Exponential-In-Out Interpolation.
  153. */
  154. EXPONENTIAL_IN_OUT,
  155. /**
  156. * Exponential-Out-In Interpolation.
  157. */
  158. EXPONENTIAL_OUT_IN,
  159. /**
  160. * Circular-In Interpolation.
  161. */
  162. CIRCULAR_IN,
  163. /**
  164. * Circular-Out Interpolation.
  165. */
  166. CIRCULAR_OUT,
  167. /**
  168. * Circular-In-Out Interpolation.
  169. */
  170. CIRCULAR_IN_OUT,
  171. /**
  172. * Circular-Out-In Interpolation.
  173. */
  174. CIRCULAR_OUT_IN,
  175. /**
  176. * Elastic-In Interpolation.
  177. */
  178. ELASTIC_IN,
  179. /**
  180. * Elastic-Out Interpolation.
  181. */
  182. ELASTIC_OUT,
  183. /**
  184. * Elastic-In-Out Interpolation.
  185. */
  186. ELASTIC_IN_OUT,
  187. /**
  188. * Elastic-Out-In Interpolation.
  189. */
  190. ELASTIC_OUT_IN,
  191. /**
  192. * Overshoot-In Interpolation.
  193. */
  194. OVERSHOOT_IN,
  195. /**
  196. * Overshoot-Out Interpolation.
  197. */
  198. OVERSHOOT_OUT,
  199. /**
  200. * Overshoot-In-Out Interpolation.
  201. */
  202. OVERSHOOT_IN_OUT,
  203. /**
  204. * Overshoot-Out-In Interpolation.
  205. */
  206. OVERSHOOT_OUT_IN,
  207. /**
  208. * Bounce-In Interpolation.
  209. */
  210. BOUNCE_IN,
  211. /**
  212. * Bounce-Out Interpolation.
  213. */
  214. BOUNCE_OUT,
  215. /**
  216. * Bounce-In-Out Interpolation.
  217. */
  218. BOUNCE_IN_OUT,
  219. /**
  220. * Bounce-Out-In Interpolation.
  221. */
  222. BOUNCE_OUT_IN
  223. };
  224. /**
  225. * Constructs a new curve and the specified parameters.
  226. *
  227. * @param pointCount The number of points in the curve.
  228. * @param componentCount The number of float component values per key value.
  229. */
  230. Curve(unsigned int pointCount, unsigned int componentCount);
  231. /**
  232. * Destructor.
  233. */
  234. ~Curve();
  235. /**
  236. * Gets the number of points in the curve.
  237. *
  238. * @return The number of points in the curve.
  239. */
  240. unsigned int getPointCount() const;
  241. /**
  242. * Gets the number of float component values per points.
  243. *
  244. * @return The number of float component values per point.
  245. */
  246. unsigned int getComponentCount() const;
  247. /**
  248. * Returns the start time for the curve.
  249. *
  250. * @return The curve's start time.
  251. */
  252. float getStartTime() const;
  253. /**
  254. * Returns the end time for the curve.
  255. *
  256. * @return The curve's end time.
  257. */
  258. float getEndTime() const;
  259. /**
  260. * Sets the given point values on the curve the curve.
  261. *
  262. * @param index The index of the point.
  263. * @param time The time for the key.
  264. * @param value The point to add.
  265. * @param type The curve interpolation type.
  266. */
  267. void setPoint(unsigned int index, float time, float* value, InterpolationType type);
  268. /**
  269. * Sets the given point on the curve for the specified index and the specified parameters.
  270. *
  271. * @param index The index of the point.
  272. * @param time The time of the point within the curve.
  273. * @param value The value of the point to copy the data from.
  274. * @param type The curve interpolation type.
  275. * @param inValue The tangent approaching the point.
  276. * @param outValue The tangent leaving the point.
  277. */
  278. void setPoint(unsigned int index, float time, float* value, InterpolationType type, float* inValue, float* outValue);
  279. /**
  280. * Sets the tangents for a point on the curve specified by the index.
  281. *
  282. * @param index The index of the point.
  283. * @param type The curve interpolation type.
  284. * @param inValue The tangent approaching the point.
  285. * @param outValue The tangent leaving the point.
  286. */
  287. void setTangent(unsigned int index, InterpolationType type, float* inValue, float* outValue);
  288. /**
  289. * Evaluates the curve at the given position value (between 0.0 and 1.0 inclusive).
  290. *
  291. * @param time The position to evaluate the curve at.
  292. * @param dst The evaluated value of the curve at the given time.
  293. */
  294. void evaluate(float time, float* dst) const;
  295. /**
  296. * Linear interpolation function.
  297. */
  298. static float lerp(float t, float from, float to);
  299. private:
  300. /**
  301. * Defines a single point within a curve.
  302. */
  303. class Point
  304. {
  305. public:
  306. /** The time of the point within the curve. */
  307. float time;
  308. /** The value of the point. */
  309. float* value;
  310. /** The value of the tangent when approaching this point (from the previous point in the curve). */
  311. float* inValue;
  312. /** The value of the tangent when leaving this point (towards the next point in the curve). */
  313. float* outValue;
  314. /** The type of interpolation to use between this point and the next point. */
  315. InterpolationType type;
  316. /**
  317. * Constructor.
  318. */
  319. Point();
  320. /**
  321. * Destructor.
  322. */
  323. ~Point();
  324. };
  325. /**
  326. * Constructor.
  327. */
  328. Curve();
  329. /**
  330. * Constructor.
  331. */
  332. Curve(const Curve& copy);
  333. /**
  334. * Bezier interpolation function.
  335. */
  336. void interpolateBezier(float s, Point* from, Point* to, float* dst) const;
  337. /**
  338. * Bspline interpolation function.
  339. */
  340. void interpolateBSpline(float s, Point* c0, Point* c1, Point* c2, Point* c3, float* dst) const;
  341. /**
  342. * Hermite interpolation function.
  343. */
  344. void interpolateHermite(float s, Point* from, Point* to, float* dst) const;
  345. /**
  346. * Hermite interpolation function.
  347. */
  348. void interpolateHermiteFlat(float s, Point* from, Point* to, float* dst) const;
  349. /**
  350. * Hermite interpolation function.
  351. */
  352. void interpolateHermiteSmooth(float s, unsigned int index, Point* from, Point* to, float* dst) const;
  353. /**
  354. * Linear interpolation function.
  355. */
  356. void interpolateLinear(float s, Point* from, Point* to, float* dst) const;
  357. /**
  358. * Quaternion interpolation function.
  359. */
  360. void interpolateQuaternion(float s, float* from, float* to, float* dst) const;
  361. /**
  362. * Determines the current keyframe to interpolate from based on the specified time.
  363. */
  364. int determineIndex(float time) const;
  365. /**
  366. * Sets the offset for the beginning of a Quaternion piece of data within the curve's value span at the specified
  367. * index. The next four components of data starting at the given index will be interpolated as a Quaternion.
  368. * This function will assert an error if the given index is greater than the component size subtracted by the four components required
  369. * to store a quaternion.
  370. *
  371. * @param index The index of the Quaternion rotation data.
  372. */
  373. void setQuaternionOffset(unsigned int index);
  374. /**
  375. * Gets the InterpolationType value for the given string ID
  376. *
  377. * @param interpolationId The string representation of the InterpolationType
  378. * @return the InterpolationType value; -1 if the string does not represent an InterpolationType.
  379. */
  380. static int getInterpolationType(const char* interpolationId);
  381. unsigned int _pointCount; // Number of points on the curve.
  382. unsigned int _componentCount; // Number of components on the curve.
  383. unsigned int _componentSize; // The component size (in bytes).
  384. unsigned int* _quaternionOffset; // Offset for the rotation component.
  385. Point* _points; // The points on the curve.
  386. };
  387. inline static float bezier(float eq0, float eq1, float eq2, float eq3, float from, float out, float to, float in);
  388. inline static float bspline(float eq0, float eq1, float eq2, float eq3, float c0, float c1, float c2, float c3);
  389. inline static float hermite(float h00, float h01, float h10, float h11, float from, float out, float to, float in);
  390. inline static float hermiteFlat(float h00, float h01, float from, float to);
  391. inline static float hermiteSmooth(float h00, float h01, float h10, float h11, float from, float out, float to, float in);
  392. inline static float lerpInl(float s, float from, float to);
  393. }
  394. #include "Curve.inl"
  395. #endif