PolyQuaternionCurve.cpp 4.8 KB

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