PolyQuaternionCurve.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(float t, bool useShortestPath)
  46. {
  47. // Work out which segment this is in
  48. float 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, float 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. Logger::log("ASDASDADS\n");
  83. q = tPoints[1].q2;
  84. b = tPoints[1].q1;
  85. }
  86. // NB interpolate to nearest rotation
  87. return Quaternion::Squad(t, p, a, b, q, useShortestPath);
  88. }
  89. void QuaternionCurve::recalcTangents(void)
  90. {
  91. unsigned int i, numPoints;
  92. bool isClosed;
  93. numPoints = (unsigned int)points.size();
  94. if (numPoints < 2) {
  95. return;
  96. }
  97. tangents.resize(numPoints);
  98. if (points[0] == points[numPoints-1])
  99. {
  100. isClosed = true;
  101. }
  102. else
  103. {
  104. isClosed = false;
  105. }
  106. Quaternion invp, part1, part2, preExp;
  107. for(i = 0; i < numPoints; ++i)
  108. {
  109. Quaternion &p = points[i];
  110. invp = p.Inverse();
  111. if (i ==0)
  112. {
  113. // special case start
  114. part1 = (invp * points[i+1]).Log();
  115. if (isClosed)
  116. {
  117. // Use numPoints-2 since numPoints-1 == end == start == this one
  118. part2 = (invp * points[numPoints-2]).Log();
  119. }
  120. else
  121. {
  122. part2 = (invp * p).Log();
  123. }
  124. }
  125. else if (i == numPoints-1)
  126. {
  127. // special case end
  128. if (isClosed)
  129. {
  130. // Wrap to [1] (not [0], this is the same as end == this one)
  131. part1 = (invp * points[1]).Log();
  132. }
  133. else
  134. {
  135. part1 = (invp * p).Log();
  136. }
  137. part2 = (invp * points[i-1]).Log();
  138. }
  139. else
  140. {
  141. part1 = (invp * points[i+1]).Log();
  142. part2 = (invp * points[i-1]).Log();
  143. }
  144. preExp =(part1 + part2) * -0.25 ;
  145. tangents[i] = p * preExp.Exp();
  146. }
  147. }