V-ART
hermiteinterpolator.cpp
Go to the documentation of this file.
1 
6 #include <iostream>
7 
8 using namespace std;
9 
10 VART::HermiteInterpolator::HermiteInterpolator(float finalTime, float finalPosition)
11 {
12  positionVec.reserve(3);
13  timeVec.reserve(3);
14  timeVec.push_back(0.0f);
15  positionVec.push_back(0.0f); // shall be replaced upon activation
16  timeVec.push_back(finalTime);
17  positionVec.push_back(finalPosition);
18 }
19 
20 //float VART::HermiteInterpolator::GetValue(float linearIndex, float initialPos, float range) const
21 float VART::HermiteInterpolator::GetValue(float linearIndex) const
22 // virtual method
23 {
24  // find next position
25  int size = timeVec.size();
26  int next = 1;
27  bool notFinished = true;
28  while ((next < size) && notFinished)
29  {
30  if (timeVec[next] >= linearIndex)
31  notFinished = false;
32  else
33  ++next;
34  }
35 
36  // find previous position
37  int previous = next - 1;
38 
39  const float tension = 0.25;
40  float prevPos = positionVec[previous];
41  float nextPos = positionVec[next];
42  float befPrevPos = positionVec[WarpIndex(previous - 1)];
43  float aftNextPos = positionVec[WarpIndex(next + 1)];
44  // find previous tangent
45  float prevTg = tension * (nextPos - befPrevPos);
46  // find next tangent
47  float nextTg = tension * (prevPos - aftNextPos);
48  // find linear index relative to control times
49  float t = (linearIndex - timeVec[previous]) / (timeVec[next] - timeVec[previous]);
50  // interpolate
51  return Interpolate(prevPos, prevTg, nextPos, nextTg, t);
52 }
53 
55 {
56  int lastIndex = positionVec.size() - 1;
57  // repeat last values at the end
58  positionVec.push_back(positionVec[lastIndex]);
59  timeVec.push_back(timeVec[lastIndex]);
60  // add given values
61  positionVec[lastIndex] = pos;
62  timeVec[lastIndex] = time;
63 }
64 
65 float VART::HermiteInterpolator::Interpolate(float p1, float tg1, float p2, float tg2, float t)
66 // protected static
67 {
68  float t2 = t * t;
69  float t3 = t * t2;
70  float result = ((2 * t3) - (3 * t2) + 1) * p1
71  + ((3 * t2) - (2 * t3)) * p2
72  + (t3 - (2 * t2) + t) * tg1
73  + (t3 - t2) * tg2;
74  cout << p1 << " " << tg1 << " " << p2 << " " << tg2 << " " << t << " => " << result << "\n";
75  return result;
76 }
77 
79 // protected
80 {
81  int size = positionVec.size();
82  if (i < 0)
83  return size + i;
84  if (i >= size)
85  return i - size;
86  return i;
87 }
static float Interpolate(float p1, float tg1, float p2, float tg2, float t)
Hermite 1D interpolation.
void AddControlPosition(float pos, float time)
Adds a control position for interpolation.
virtual float GetValue(float linearIndex, float initialPos, float range) const
Evaluates a goal position for DOF interpolation.
HermiteInterpolator(float finalTime, float finalPosition)
Header file for V-ART class "HermiteInterpolator".
int WarpIndex(int i) const
Warp index around valid vector indices.