PolyBezierCurve.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyVector3.h"
  22. #include <vector>
  23. #define BUFFER_CACHE_PRECISION 100
  24. namespace Polycode {
  25. /**
  26. * A curve point defined by 3 positions.
  27. * @see BezierCurve
  28. */
  29. class _PolyExport BezierPoint : public PolyBase {
  30. public:
  31. /**
  32. * Constructor
  33. * @param p1x X position of the first handle point.
  34. * @param p1y Y position of the first handle point.
  35. * @param p1z Z position of the first handle point.
  36. * @param p2x X position of the middle point.
  37. * @param p2y Y position of the middle point.
  38. * @param p2z Z position of the middle point.
  39. * @param p3x X position of the second handle point.
  40. * @param p3y Y position of the second handle point.
  41. * @param p3z Z position of the second handle point.
  42. */
  43. BezierPoint(Number p1x, Number p1y, Number p1z, Number p2x, Number p2y, Number p2z, Number p3x, Number p3y, Number p3z);
  44. /**
  45. * First handle position.
  46. */
  47. Vector3 p1;
  48. /**
  49. * Middle position.
  50. */
  51. Vector3 p2;
  52. /**
  53. * Second handle position.
  54. */
  55. Vector3 p3;
  56. };
  57. /**
  58. * A Bezier curve. This class can be used to draw smooth curves or move things smoothly on curves. It's also used internally for skeletal animation. The curves are 3-dimensional, but convenience methods are added for 2d curves and there is a caching mechanism for 2d height data to speed up bezier curve usage in animation.
  59. A bezier curve consists of control points, each having 3 points: one middle point and two 'handles'. The middle point is the actual position of the control point and the two side points serve as vectors defining how the curve curves towards the next control points.
  60. */
  61. class _PolyExport BezierCurve : public PolyBase {
  62. public:
  63. /**
  64. * Default constructor.
  65. */
  66. BezierCurve();
  67. virtual ~BezierCurve();
  68. /**
  69. * Returns a control point by index.
  70. * @param index Index of the control point to return.
  71. * @return Control point at specified index.
  72. */
  73. BezierPoint *getControlPoint(unsigned int index);
  74. /**
  75. * Returns the total number of control points in the curve.
  76. * @return Total number of control points.
  77. */
  78. unsigned int getNumControlPoints();
  79. /**
  80. * @see addControlPoint3dWithHandles()
  81. */
  82. void addControlPoint(Number p1x, Number p1y, Number p1z, Number p2x, Number p2y, Number p2z, Number p3x, Number p3y, Number p3z);
  83. /**
  84. * Adds a new control point to the curve after the last point.
  85. * @param p1x X position of the first handle point.
  86. * @param p1y Y position of the first handle point.
  87. * @param p1z Z position of the first handle point.
  88. * @param p2x X position of the middle point.
  89. * @param p2y Y position of the middle point.
  90. * @param p2z Z position of the middle point.
  91. * @param p3x X position of the second handle point.
  92. * @param p3y Y position of the second handle point.
  93. * @param p3z Z position of the second handle point.
  94. */
  95. void addControlPoint3dWithHandles(Number p1x, Number p1y, Number p1z, Number p2x, Number p2y, Number p2z, Number p3x, Number p3y, Number p3z);
  96. /**
  97. * Adds a new control point to the curve with handles in the same place as the middle points.
  98. * @param x X position of the point and both handles.
  99. * @param y Y position of the point and both handles.
  100. * @param z Z position of the point and both handles.
  101. */
  102. void addControlPoint3d(Number x, Number y, Number z);
  103. /**
  104. * Adds a new control point to the curve after the last point using 2d coordinates. The Z value of the coordinates is set to 0.
  105. * @param p1x X position of the first handle point.
  106. * @param p1y Y position of the first handle point.
  107. * @param p2x X position of the middle point.
  108. * @param p2y Y position of the middle point.
  109. * @param p3x X position of the second handle point.
  110. * @param p3y Y position of the second handle point.
  111. */
  112. void addControlPoint2dWithHandles(Number p1x, Number p1y, Number p2x, Number p2y, Number p3x, Number p3y);
  113. /**
  114. * Adds a new control point to the curve with handles in the same place as the middle points using 2d coordinates.The Z value of the coordinates is set to 0.
  115. * @param x X position of the point and both handles.
  116. * @param y Y position of the point and both handles.
  117. */
  118. void addControlPoint2d(Number x, Number y);
  119. /**
  120. * Returns the height of the curve at a specified point on the curve. Heights are cached into a buffer with a finite cache precision to speed up the curve usage in animation. If you need to quickly get 2D height out of a curve and you don't care about total precision, use this method.
  121. * @param a Normalized (0-1) position along the curve.
  122. * @return Height value at specified position.
  123. */
  124. Number getHeightAt(Number a);
  125. /**
  126. * Returns the 3d point of the curve at a specified point on the curve.
  127. * @param a Normalized (0-1) position along the curve.
  128. * @return 3d point at specified position.
  129. */
  130. Vector3 getPointAt(Number a);
  131. /**
  132. * Returns the 3d point of the curve at a specified point between two points.
  133. * @param a Normalized (0-1) position between two points
  134. * @return 3d point at specified position.
  135. */
  136. Vector3 getPointBetween(Number a, BezierPoint *bp1, BezierPoint *bp2);
  137. void clearControlPoints();
  138. /**
  139. * Rebuilds the height cache buffers for 2d height curves.
  140. */
  141. void rebuildBuffers();
  142. /**
  143. * Removes (and deletes!) a gives point by pointer
  144. */
  145. void removePoint(BezierPoint *point);
  146. Number heightBuffer[BUFFER_CACHE_PRECISION];
  147. BezierPoint *insertPoint;
  148. std::vector<BezierPoint*> controlPoints;
  149. std::vector<Number> distances;
  150. void recalculateDistances();
  151. protected:
  152. bool buffersDirty;
  153. };
  154. }