PolyQuaternionCurve.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "PolyQuaternionCurve.h"
  20. #include "PolyBezierCurve.h"
  21. #include "PolyLogger.h"
  22. using namespace Polycode;
  23. QuaternionCurve::QuaternionCurve(BezierCurve *wCurve, BezierCurve *xCurve, BezierCurve *yCurve, BezierCurve *zCurve) {
  24. generatePointsFromCurves(wCurve, xCurve, yCurve, zCurve);
  25. }
  26. QuaternionCurve::~QuaternionCurve() {
  27. }
  28. void QuaternionCurve::generatePointsFromCurves(BezierCurve *wCurve, BezierCurve *xCurve, BezierCurve *yCurve, BezierCurve *zCurve) {
  29. for(int i=0; i < wCurve->getNumControlPoints(); i++) {
  30. /*
  31. Quaternion newQuat(wCurve->getControlPoint(i)->p2.y,
  32. xCurve->getControlPoint(i)->p2.y,
  33. yCurve->getControlPoint(i)->p2.y,
  34. zCurve->getControlPoint(i)->p2.y);
  35. points.push_back(newQuat);
  36. recalcTangents();
  37. */
  38. Quaternion quat1(wCurve->getControlPoint(i)->p1.y,
  39. xCurve->getControlPoint(i)->p1.y,
  40. yCurve->getControlPoint(i)->p1.y,
  41. zCurve->getControlPoint(i)->p1.y);
  42. Quaternion quat2(wCurve->getControlPoint(i)->p2.y,
  43. xCurve->getControlPoint(i)->p2.y,
  44. yCurve->getControlPoint(i)->p2.y,
  45. zCurve->getControlPoint(i)->p2.y);
  46. Quaternion quat3(wCurve->getControlPoint(i)->p3.y,
  47. xCurve->getControlPoint(i)->p3.y,
  48. yCurve->getControlPoint(i)->p3.y,
  49. zCurve->getControlPoint(i)->p3.y);
  50. QuatTriple newTriple;
  51. newTriple.q1 = quat1;
  52. newTriple.q2 = quat2;
  53. newTriple.q3 = quat3;
  54. tPoints.push_back(newTriple);
  55. }
  56. }
  57. Quaternion QuaternionCurve::interpolate(Number t, bool useShortestPath)
  58. {
  59. // Work out which segment this is in
  60. Number fSeg = t * (tPoints.size() - 1);
  61. unsigned int segIdx = (unsigned int)fSeg;
  62. // Apportion t
  63. t = fSeg - segIdx;
  64. return interpolate(segIdx, t, useShortestPath);
  65. }
  66. //---------------------------------------------------------------------
  67. Quaternion QuaternionCurve::interpolate(unsigned int fromIndex, Number t,
  68. bool useShortestPath)
  69. {
  70. // Bounds check
  71. assert (fromIndex >= 0 && fromIndex < tPoints.size() &&
  72. "fromIndex out of bounds");
  73. if ((fromIndex + 1) == tPoints.size() && tPoints[fromIndex].q2 != tPoints[0].q2)
  74. {
  75. Logger::log("size\n");
  76. // Duff request, cannot blend to nothing
  77. // Just return source
  78. return points[fromIndex];
  79. }
  80. // Fast special cases
  81. if (t == 0.0f)
  82. {
  83. return tPoints[fromIndex].q2;
  84. }
  85. else if(t == 1.0f)
  86. {
  87. return tPoints[fromIndex + 1].q2;
  88. }
  89. Quaternion &p = tPoints[fromIndex].q2;
  90. Quaternion &q = tPoints[fromIndex+1].q2;
  91. Quaternion &a = tPoints[fromIndex].q3;
  92. Quaternion &b = tPoints[fromIndex+1].q1;
  93. if ((fromIndex + 1) == tPoints.size() && tPoints[fromIndex].q2 == tPoints[0].q2) {
  94. q = tPoints[1].q2;
  95. b = tPoints[1].q1;
  96. }
  97. // NB interpolate to nearest rotation
  98. return Quaternion::Squad(t, p, a, b, q, useShortestPath);
  99. }
  100. void QuaternionCurve::recalcTangents()
  101. {
  102. unsigned int i, numPoints;
  103. bool isClosed;
  104. numPoints = (unsigned int)points.size();
  105. if (numPoints < 2) {
  106. return;
  107. }
  108. tangents.resize(numPoints);
  109. if (points[0] == points[numPoints-1])
  110. {
  111. isClosed = true;
  112. }
  113. else
  114. {
  115. isClosed = false;
  116. }
  117. Quaternion invp, part1, part2, preExp;
  118. for(i = 0; i < numPoints; ++i)
  119. {
  120. Quaternion &p = points[i];
  121. invp = p.Inverse();
  122. if (i ==0)
  123. {
  124. // special case start
  125. part1 = (invp * points[i+1]).Log();
  126. if (isClosed)
  127. {
  128. // Use numPoints-2 since numPoints-1 == end == start == this one
  129. part2 = (invp * points[numPoints-2]).Log();
  130. }
  131. else
  132. {
  133. part2 = (invp * p).Log();
  134. }
  135. }
  136. else if (i == numPoints-1)
  137. {
  138. // special case end
  139. if (isClosed)
  140. {
  141. // Wrap to [1] (not [0], this is the same as end == this one)
  142. part1 = (invp * points[1]).Log();
  143. }
  144. else
  145. {
  146. part1 = (invp * p).Log();
  147. }
  148. part2 = (invp * points[i-1]).Log();
  149. }
  150. else
  151. {
  152. part1 = (invp * points[i+1]).Log();
  153. part2 = (invp * points[i-1]).Log();
  154. }
  155. preExp =(part1 + part2) * -0.25 ;
  156. tangents[i] = p * preExp.Exp();
  157. }
  158. }