File: vtkQuaternionInterpolator.cxx

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (430 lines) | stat: -rwxr-xr-x 13,205 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    $RCSfile: vtkQuaternionInterpolator.cxx,v $

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkQuaternionInterpolator.h"
#include "vtkObjectFactory.h"
#include "vtkMath.h"
#include <vtkstd/vector>

vtkCxxRevisionMacro(vtkQuaternionInterpolator, "$Revision: 1.5 $");
vtkStandardNewMacro(vtkQuaternionInterpolator);

//----------------------------------------------------------------------------
// PIMPL STL encapsulation for list of quaternions. The list is sorted on
// the spline paramter T (or Time) using a STL list.
// Here we define a quaternion class that includes extra information including
// a unit quaternion representation.
struct vtkQuaternion
{
  double Time;
  double Q[4];     //VTK's quaternion: unit rotation axis with angles in degrees
  double QUnit[4]; //Unit quaternion (i.e., normalized)

  vtkQuaternion()
    {
      this->Time = 0.0;
      this->Q[0] = this->Q[1] = this->Q[2] = this->Q[3] = 0.0;
      this->QUnit[0] = this->QUnit[1] = this->QUnit[2] = this->QUnit[3] = 0.0;
    }
  vtkQuaternion(double t, double q[4])
    {
      this->Time = t;
      this->Q[0] = this->QUnit[0] = q[0];
      this->Q[1] = this->QUnit[1] = q[1];
      this->Q[2] = this->QUnit[2] = q[2];
      this->Q[3] = this->QUnit[3] = q[3];

      // determine theta, sin(theta), cos(theta) for unit quaternion
      this->QUnit[0] *= vtkMath::DegreesToRadians(); //convert to radians
      vtkQuaternion::Normalize(this->QUnit);
    }
  static void Add(double q0[4], double q1[4], double q[4])
    {
      q[0] = q0[0] + q1[0];
      q[1] = q0[1] + q1[1];
      q[2] = q0[2] + q1[2];
      q[3] = q0[3] + q1[3];
    }
  static void Product(double q0[4], double q1[4], double q[4])
    {
      q[0] = q0[0]*q1[0] - q0[1]*q1[1] - q0[2]*q1[2] - q0[3]*q1[3];
      q[1] = q0[0]*q1[1] + q0[1]*q1[0] + q0[2]*q1[3] - q0[3]*q1[2];
      q[2] = q0[0]*q1[2] - q0[1]*q1[3] + q0[2]*q1[0] + q0[3]*q1[1];
      q[3] = q0[0]*q1[3] + q0[1]*q1[2] - q0[2]*q1[1] + q0[3]*q1[0];
    }
  static void Conjugate(double q[4], double qConj[4])
    {
      qConj[0] =  q[0];
      qConj[1] = -q[1];
      qConj[2] = -q[2];
      qConj[3] = -q[3];
    }
  static void Inverse(double q[4], double qInv[4])
    {
      vtkQuaternion::Conjugate(q,qInv);
      double norm2 = vtkQuaternion::Norm2(q);
      if ( norm2 != 0.0 )
        {
        qInv[0] /= norm2;
        qInv[1] /= norm2;
        qInv[2] /= norm2;
        qInv[3] /= norm2;
        }
    }
  static double Norm2(double q[4])
    {
      return (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
    }
  static double Normalize(double q[4])
    {
      double norm = sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
      if ( norm != 0.0 )
        {
        q[0] /= norm;
        q[1] /= norm;
        q[2] /= norm;
        q[3] /= norm;
        }
      return norm;
    }
  // convert a unit quaternion to a VTK quaternion (angle in degrees;unit axis)
  static void UnitToVTK(double q[4])
    {
      double vNorm = sqrt(q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
      if ( vNorm != 0.0 )
        {
        q[0] /= vNorm;
        q[1] /= vNorm;
        q[2] /= vNorm;
        q[3] /= vNorm;
        }
      q[0] *= vtkMath::RadiansToDegrees();
    }
  // compute unit vector where q is a unit quaternion
  static void UnitVector(double q[4], double &theta, double &sinTheta, 
                         double &cosTheta, double v[3])
    {
      double norm = sqrt(q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
      v[0] = q[1]/norm;
      v[1] = q[2]/norm;
      v[2] = q[3]/norm;
      int maxI = (q[1] > q[2] ? (q[1] > q[3] ? 1 : 3) : (q[2] > q[3] ? 2 : 3));
      if (q[maxI] != 0.0 )
        {
        sinTheta = q[maxI] / v[maxI-1];
        theta = asin(sinTheta);
        cosTheta = cos(theta);
        }
    }
  // log(q) where q is a unit (normalized) quaternion
  static void UnitLog(double q[4], double qLog[4])
    {
      double theta, sinTheta, cosTheta, v[3];
      vtkQuaternion::UnitVector(q,theta,sinTheta,cosTheta,v);
      qLog[0] = 0.0;
      qLog[1] = theta * v[0];
      qLog[2] = theta * v[1];
      qLog[3] = theta * v[2];
    }
  // exp(q) where q is a unit quaternion
  static void UnitExp(double q[4], double qExp[4])
    {
      double theta, sinTheta, cosTheta, v[3];
      vtkQuaternion::UnitVector(q,theta,sinTheta,cosTheta,v);
      qExp[0] = cosTheta;
      qExp[1] = sinTheta * v[0];
      qExp[2] = sinTheta * v[1];
      qExp[3] = sinTheta * v[2];
    }
};

// The list is arranged in increasing order in T
class vtkQuaternionList : public vtkstd::vector<vtkQuaternion> {};
typedef vtkQuaternionList::iterator QuaternionListIterator;

//----------------------------------------------------------------------------
vtkQuaternionInterpolator::vtkQuaternionInterpolator()
{
  // Set up the interpolation
  this->QuaternionList = new vtkQuaternionList;
  this->InterpolationType = INTERPOLATION_TYPE_SPLINE;
}

//----------------------------------------------------------------------------
vtkQuaternionInterpolator::~vtkQuaternionInterpolator()
{
  this->Initialize();
  delete this->QuaternionList;
}

//----------------------------------------------------------------------------
int vtkQuaternionInterpolator::GetNumberOfQuaternions()
{
    return this->QuaternionList->size();
}


//----------------------------------------------------------------------------
double vtkQuaternionInterpolator::GetMinimumT()
{
  if (this->QuaternionList->size() > 0)
    {
    return this->QuaternionList->front().Time;
    }
  else
    {
    return 0.0;
    }
}


//----------------------------------------------------------------------------
double vtkQuaternionInterpolator::GetMaximumT()
{
  if (this->QuaternionList->size() > 0)
    {
    return this->QuaternionList->back().Time;
    }
  else
    {
    return 0.0;
    }
}


//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::Initialize()
{
  // Wipe out old data
  this->QuaternionList->clear();
}


//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::AddQuaternion(double t, double q[4])
{
  int size = this->QuaternionList->size();

  // Check special cases: t at beginning or end of list
  if ( size <= 0 || t < this->QuaternionList->front().Time )
    {
    this->QuaternionList->insert(this->QuaternionList->begin(),vtkQuaternion(t,q));
    return;
    }
  else if ( t > this->QuaternionList->back().Time )
    {
    this->QuaternionList->push_back(vtkQuaternion(t,q));
    return;
    }
  else if ( size == 1 && t == this->QuaternionList->front().Time )
    {
    this->QuaternionList->front() = vtkQuaternion(t,q);
    return;
    }

  // Okay, insert in sorted order
  QuaternionListIterator iter = this->QuaternionList->begin();
  QuaternionListIterator nextIter = iter + 1;
  for (int i=0; i < (size-1); i++, ++iter, ++nextIter)
    {
    if ( t == iter->Time )
      {
      (*iter) = vtkQuaternion(t,q); //overwrite
      break;
      }
    else if ( t > iter->Time && t < nextIter->Time )
      {
      this->QuaternionList->insert(nextIter, vtkQuaternion(t,q));
      break;
      }
    }//for not in the right spot
  
  this->Modified();
}

//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::RemoveQuaternion(double t)
{
  if ( t < this->QuaternionList->front().Time ||
       t > this->QuaternionList->back().Time )
    {
    return;
    }

  QuaternionListIterator iter = this->QuaternionList->begin();
  for ( ; iter->Time != t && iter != this->QuaternionList->end(); ++iter )
    {
    }
  if ( iter != this->QuaternionList->end() )
    {
    this->QuaternionList->erase(iter);
    }
  
  this->Modified();
}

//----------------------------------------------------------------------------
//Interpolate using spherical linear interpolation between the quaternions q0
//and q1 to produce the output q. The parametric coordinate t is [0,1] and
//lies between (q0,q1).
void vtkQuaternionInterpolator::Slerp(double t, double q0[4], double q1[4], 
                                      double q[4])
{
  double theta = acos( vtkMath::Dot(q0+1,q1+1) );
  double t1 = sin((1.0-t)*theta)/sin(theta);
  double t2 = sin(t*theta)/sin(theta);
  q[0] = q0[0]*t1 + q1[0]*t2;
  q[1] = q0[1]*t1 + q1[1]*t2;
  q[2] = q0[2]*t1 + q1[2]*t2;
  q[3] = q0[3]*t1 + q1[3]*t2;
}


//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::InnerPoint(double q0[4], double q1[4], 
                                           double q2[4], double q[4])
{
   double qInv[4], qL[4], qR[4];
   vtkQuaternion::Inverse(q1,qInv);
   vtkQuaternion::Product(qInv,q2,qL);
   vtkQuaternion::Product(qInv,q0,qR);
  
   double qLLog[4], qRLog[4], qSum[4], qExp[4];
   vtkQuaternion::UnitLog(qL, qLLog);
   vtkQuaternion::UnitLog(qR, qRLog);
   vtkQuaternion::Add(qLLog,qRLog,qSum);
   qSum[1] /= -4.0;
   qSum[2] /= -4.0;
   qSum[3] /= -4.0;
   vtkQuaternion::UnitExp(qSum,qExp);
   vtkQuaternion::Product(q1,qExp,q);
}


//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::InterpolateQuaternion(double t, double q[4])
{
  // The quaternion may be clamped if it is outside the range specified
  if ( t <= this->QuaternionList->front().Time )
    {
    vtkQuaternion &Q = this->QuaternionList->front();
    q[0] = Q.Q[0]; q[1] = Q.Q[1]; q[2] = Q.Q[2]; q[3] = Q.Q[3];
    return;
    }

  else if ( t >= this->QuaternionList->back().Time )
    {
    vtkQuaternion &Q = this->QuaternionList->back();
    q[0] = Q.Q[0]; q[1] = Q.Q[1]; q[2] = Q.Q[2]; q[3] = Q.Q[3];
    return;
    }

  // Depending on the interpolation type we do the right thing.
  // The code above guarantees that there are at least two quaternions defined.
  int numQuats = this->GetNumberOfQuaternions();
  if ( this->InterpolationType == INTERPOLATION_TYPE_LINEAR || numQuats < 3 )
    {
    QuaternionListIterator iter = this->QuaternionList->begin();
    QuaternionListIterator nextIter = iter + 1;
    for ( ; nextIter != this->QuaternionList->end(); ++iter, ++nextIter)
      {
      if ( iter->Time <= t && t <= nextIter->Time )
        {
        double T = (t - iter->Time) / (nextIter->Time - iter->Time); 
        this->Slerp(T,iter->Q,nextIter->Q,q);
        break;
        }
      }
    }//if linear quaternion interpolation
  
  else // this->InterpolationType == INTERPOLATION_TYPE_SPLINE
    {
    QuaternionListIterator iter = this->QuaternionList->begin();
    QuaternionListIterator nextIter = iter + 1;
    QuaternionListIterator iter0, iter1, iter2, iter3;

    //find the interval
    double T=0.0;
    int i;
    for (i=0; nextIter != this->QuaternionList->end(); ++iter, ++nextIter, ++i)
      {
      if ( iter->Time <= t && t <= nextIter->Time )
        {
        T = (t - iter->Time) / (nextIter->Time - iter->Time); 
        break;
        }
      }
    
    double ai[4], bi[4], qc[4], qd[4];
    if ( i == 0 ) //initial interval
      {
      iter1 = iter;
      iter2 = nextIter;
      iter3 = nextIter + 1;

      ai[0] = iter1->QUnit[0]; //just duplicate first quaternion
      ai[1] = iter1->QUnit[1];
      ai[2] = iter1->QUnit[2];
      ai[3] = iter1->QUnit[3];
      
      this->InnerPoint(iter1->QUnit, iter2->QUnit, iter3->QUnit, bi);
      }
    else if ( i == (numQuats-2) ) //final interval
      {
      iter0 = iter - 1;
      iter1 = iter;
      iter2 = nextIter;

      this->InnerPoint(iter0->QUnit, iter1->QUnit, iter2->QUnit, ai);

      bi[0] = iter2->QUnit[0]; //just duplicate last quaternion
      bi[1] = iter2->QUnit[1];
      bi[2] = iter2->QUnit[2];
      bi[3] = iter2->QUnit[3];
      }
    else //in a middle interval somewhere
      {
      iter0 = iter - 1;
      iter1 = iter;
      iter2 = nextIter;
      iter3 = nextIter + 1;
      this->InnerPoint(iter0->QUnit, iter1->QUnit, iter2->QUnit, ai);
      this->InnerPoint(iter1->QUnit, iter2->QUnit, iter3->QUnit, bi);
      }

    // These three Slerp operations implement a Squad interpolation
    this->Slerp(T,iter1->QUnit,iter2->QUnit,qc);
    this->Slerp(T,ai,bi,qd);
    this->Slerp(2.0*T*(1.0-T),qc,qd,q);
    vtkQuaternion::UnitToVTK(q);
    }

  return;
}

//----------------------------------------------------------------------------
void vtkQuaternionInterpolator::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "There are " << this->GetNumberOfQuaternions()
     << " quaternions to be interpolated\n";
  
  os << indent << "Interpolation Type: " 
     << (this->InterpolationType == INTERPOLATION_TYPE_LINEAR ?
         "Linear\n" : "Spline\n");
}