File: vtkPVKeyFrameCueManipulator.cxx

package info (click to toggle)
paraview 5.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 221,108 kB
  • ctags: 236,092
  • sloc: cpp: 2,416,026; ansic: 190,891; python: 99,856; xml: 81,001; tcl: 46,915; yacc: 5,039; java: 4,413; perl: 3,108; sh: 1,974; lex: 1,926; f90: 748; asm: 471; pascal: 228; makefile: 198; objc: 83; fortran: 31
file content (419 lines) | stat: -rw-r--r-- 12,629 bytes parent folder | download | duplicates (5)
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
/*=========================================================================

  Program:   ParaView
  Module:    vtkPVKeyFrameCueManipulator.cxx

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkPVKeyFrameCueManipulator.h"

#include "vtkCommand.h"
#include "vtkObjectFactory.h"
#include "vtkPVKeyFrame.h"

#include <vector>


//****************************************************************************
class vtkPVKeyFrameCueManipulatorObserver : public vtkCommand
{
public:
  static vtkPVKeyFrameCueManipulatorObserver* New()
    {
    return new vtkPVKeyFrameCueManipulatorObserver;
    }
  void SetKeyFrameAnimationCueManipulatorProxy(
    vtkPVKeyFrameCueManipulator* proxy)
    {
    this->KeyFrameAnimationCueManipulatorProxy = proxy;
    }

  virtual void Execute(vtkObject* obj, unsigned long event, void* calldata)
    {
    if (this->KeyFrameAnimationCueManipulatorProxy)
      {
      this->KeyFrameAnimationCueManipulatorProxy->ExecuteEvent(obj, event,
        calldata);
      }
    }
protected:
  vtkPVKeyFrameCueManipulatorObserver()
    {
    this->KeyFrameAnimationCueManipulatorProxy = 0;
    }
  vtkPVKeyFrameCueManipulator*
    KeyFrameAnimationCueManipulatorProxy;
};
//----------------------------------------------------------------------------
class vtkPVKeyFrameCueManipulatorInternals
{
public:
  typedef std::vector<vtkPVKeyFrame*> KeyFrameVector;
  KeyFrameVector KeyFrames;
};
//****************************************************************************
vtkStandardNewMacro(vtkPVKeyFrameCueManipulator);
//----------------------------------------------------------------------------
vtkPVKeyFrameCueManipulator::vtkPVKeyFrameCueManipulator()
{
  this->Internals = new vtkPVKeyFrameCueManipulatorInternals;
  this->Observer = vtkPVKeyFrameCueManipulatorObserver::New();
  this->Observer->SetKeyFrameAnimationCueManipulatorProxy(this);
  this->SendEndEvent = 0;
  this->LastAddedKeyFrameIndex = 0;
}

//----------------------------------------------------------------------------
vtkPVKeyFrameCueManipulator::~vtkPVKeyFrameCueManipulator()
{
  this->RemoveAllKeyFrames();

  delete this->Internals;
  this->Observer->Delete();
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::Initialize(vtkPVAnimationCue*)
{
  this->SendEndEvent = 1;
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::Finalize(vtkPVAnimationCue* cue)
{
  if (this->SendEndEvent)
    {
    this->UpdateValue(1.0, cue);
    }
}

//----------------------------------------------------------------------------
int vtkPVKeyFrameCueManipulator::AddKeyFrame(vtkPVKeyFrame* keyframe)
{
  int index = this->AddKeyFrameInternal(keyframe);
  if (index != -1)
    {
    keyframe->AddObserver(vtkCommand::ModifiedEvent, this->Observer);
    keyframe->Register(this);
    //this->UpdateKeyTimeDomains();
    }
  this->LastAddedKeyFrameIndex = index;
  this->Modified();
  return index;
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::RemoveKeyFrame(vtkPVKeyFrame* keyframe)
{
  if (this->RemoveKeyFrameInternal(keyframe))
    {
    keyframe->RemoveObservers(vtkCommand::ModifiedEvent, this->Observer);
    keyframe->UnRegister(this);
    //this->UpdateKeyTimeDomains();
    }
  this->Modified();
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::RemoveAllKeyFrames()
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator iter;
  for (iter = this->Internals->KeyFrames.begin();
    iter != this->Internals->KeyFrames.end();
    iter++)
    {
      (*iter)->RemoveObservers(vtkCommand::ModifiedEvent, this->Observer);
      (*iter)->UnRegister(this);
    }
  this->Internals->KeyFrames.clear();
  this->Modified();
}

//----------------------------------------------------------------------------
unsigned int vtkPVKeyFrameCueManipulator::GetNumberOfKeyFrames()
{
  return static_cast<unsigned int>(this->Internals->KeyFrames.size());
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetKeyFrame(double time)
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator iter;
  for (iter = this->Internals->KeyFrames.begin();
    iter != this->Internals->KeyFrames.end();
    iter++)
    {
    if ( (*iter)->GetKeyTime() == time)
      {
      return *iter;
      }
    }

  return NULL;
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetStartKeyFrame(double time)
{
  // we use the fact that we have maintained the vector in sorted order.
  vtkPVKeyFrame* proxy = NULL;
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator it = this->Internals->KeyFrames.begin();
  for (; it != this->Internals->KeyFrames.end(); it++)
    {
    double cur_time = (*it)->GetKeyTime();
    if ( cur_time == time)
      {
      return *it;
      }
    if ( cur_time > time)
      {
      return proxy;
      }
    proxy = *it;;
    }
  return NULL;
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetEndKeyFrame(double time)
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator it = this->Internals->KeyFrames.begin();
  for (; it != this->Internals->KeyFrames.end(); it++)
    {
    if ( (*it)->GetKeyTime() >= time)
      {
      return *it;
      }
    }
  return NULL;
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::UpdateValue(double currenttime,
                                              vtkPVAnimationCue* cue)
{
  if (!cue)
    {
    vtkErrorMacro("UpdateValue called with invalid arguments");
    return;
    }

  if (this->GetNumberOfKeyFrames() < 2)
    {
    //vtkErrorMacro("Too few keyframe to animate.");
    return;
    }

  vtkPVKeyFrame* startKF = this->GetStartKeyFrame(currenttime);
  vtkPVKeyFrame* endKF = this->GetEndKeyFrame(currenttime);
  if (endKF && startKF == NULL)
    {
    // This means that we are at a time location before the first keyframe in
    // this cue. In that case, simply duplicate the first key-frame as the
    // chosen one.
    endKF->UpdateValue(0, cue, endKF);
    this->InvokeEvent(vtkPVCueManipulator::StateModifiedEvent);
    }
  if (startKF && endKF)
    {
    // normalized time to the range between start key frame and end key frame.
    double ctime = 0;
    double tmin = startKF->GetKeyTime();
    double tmax = endKF->GetKeyTime();

    if (tmin != tmax)
      {
      ctime = (currenttime - tmin)/ (tmax-tmin);
      }
    startKF->UpdateValue(ctime, cue, endKF);
    this->InvokeEvent(vtkPVCueManipulator::StateModifiedEvent);
    }
  // check to see if the curtime has crossed the last key frame and if
  // we should make the state of the property as left by the last key frame.
  else if (this->SendEndEvent)
    {
    int num = this->GetNumberOfKeyFrames();
    vtkPVKeyFrame* lastKF = this->GetKeyFrameAtIndex(num-1);
    if (currenttime >= lastKF->GetKeyTime())
      {
      lastKF->UpdateValue(0, cue,lastKF);
      this->SendEndEvent = 0;
      this->InvokeEvent(vtkPVCueManipulator::StateModifiedEvent);
      }
    }
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetNextKeyFrame(
    vtkPVKeyFrame* keyFrame)
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator it = this->Internals->KeyFrames.begin();
  for (; it != this->Internals->KeyFrames.end(); it++)
    {
    if ( *it == keyFrame)
      {
      it++;
      if (it != this->Internals->KeyFrames.end())
        {
        return (*it);
        }
      break;
      }
    }
  return NULL;
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetPreviousKeyFrame(
    vtkPVKeyFrame* keyFrame)
{
  vtkPVKeyFrame* proxy = NULL;

  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator it = this->Internals->KeyFrames.begin();
  for (; it != this->Internals->KeyFrames.end(); it++)
    {
    if ( *it == keyFrame)
      {
      return proxy;
      }
    proxy = *it;
    }
  return NULL;
}

//----------------------------------------------------------------------------
vtkPVKeyFrame* vtkPVKeyFrameCueManipulator::GetKeyFrameAtIndex(int index)
{
  if (index < 0 || index >= static_cast<int>(this->GetNumberOfKeyFrames()))
    {
    vtkErrorMacro("Index beyond range");
    return NULL;
    }
  return this->Internals->KeyFrames[index];
}

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::ExecuteEvent(
    vtkObject* obj, unsigned long event, void* )
{
  vtkPVKeyFrame* keyframe = vtkPVKeyFrame::SafeDownCast(obj);

  if (keyframe && event == vtkCommand::ModifiedEvent)
    {
    // Check if the keyframe position has changed.
    vtkPVKeyFrame* prev = this->GetPreviousKeyFrame(keyframe);
    vtkPVKeyFrame* next = this->GetNextKeyFrame(keyframe);
    double keytime = keyframe->GetKeyTime();
    if ( (next && keytime > next->GetKeyTime()) ||
      (prev && keytime < prev->GetKeyTime()))
      {
      // Position of keyframe has changed.
      this->RemoveKeyFrameInternal(keyframe);
      this->AddKeyFrameInternal(keyframe);
      }
    }
  //this->UpdateKeyTimeDomains();
  this->Modified();
}

//----------------------------------------------------------------------------
int vtkPVKeyFrameCueManipulator::AddKeyFrameInternal(vtkPVKeyFrame* keyframe)
{
  double time = keyframe->GetKeyTime();
  int index = 0;
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator iter;
  for (iter = this->Internals->KeyFrames.begin();
    iter != this->Internals->KeyFrames.end();
    iter++, index++)
    {
    if ( *iter == keyframe)
      {
      vtkErrorMacro("Keyframe already exists");
      return -1;
      }
    if ( (*iter)->GetKeyTime() > time )
      {
      break;
      }
    }
  this->Internals->KeyFrames.insert(iter, keyframe);
  return index;
}

//----------------------------------------------------------------------------
int vtkPVKeyFrameCueManipulator::RemoveKeyFrameInternal(vtkPVKeyFrame* kf)
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator iter;
  for (iter = this->Internals->KeyFrames.begin();
    iter != this->Internals->KeyFrames.end();
    iter++)
    {
    if ( *iter == kf)
      {
      this->Internals->KeyFrames.erase(iter);
      return 1;
      }
    }
  return 0;
}

//----------------------------------------------------------------------------
#ifdef FIXME
void vtkPVKeyFrameCueManipulator::UpdateKeyTimeDomains()
{
  vtkPVKeyFrameCueManipulatorInternals::KeyFrameVector::
    iterator iter;
  unsigned int numFrames = this->Internals->KeyFrames.size();
  for (unsigned int cc=0; cc < numFrames; ++cc)
    {
    vtkPVKeyFrame* kf = this->Internals->KeyFrames[cc];
    vtkPVKeyFrame* prev = (cc>0)? this->Internals->KeyFrames[cc-1] : NULL;
    vtkPVKeyFrame* next = (cc+1<numFrames)? this->Internals->KeyFrames[cc+1]: NULL;
    double min = (prev)? prev->GetKeyTime() : 0.0;
    double max = (next)? next->GetKeyTime() : 1.0;
    vtkSMProperty* keyTimeProp = kf->GetProperty("KeyTime");
    if (!keyTimeProp)
      {
      vtkWarningMacro("KeyFrameProxy should have a KeyTime property.");
      continue;
      }
    vtkSMDoubleRangeDomain* dr = vtkSMDoubleRangeDomain::SafeDownCast(
      keyTimeProp->GetDomain("range"));
    if (dr)
      {
      int exists;
      if (dr->GetMinimum(0, exists) != min || !exists)
        {
        dr->AddMinimum(0, min);
        }
      if (dr->GetMaximum(0, exists) != max || !exists)
        {
        dr->AddMaximum(0, max);
        }
      }
    }
}
#endif

//----------------------------------------------------------------------------
void vtkPVKeyFrameCueManipulator::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "LastAddedKeyFrameIndex: " << this->LastAddedKeyFrameIndex
      << endl;
}