Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/particles/quick_animation.cc @ 4654

Last change on this file since 4654 was 4654, checked in by bensch, 19 years ago

orxonox/trunk: speeded up the QuickAnimation-getValue-function.

File size: 5.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "quick_animation.h"
19
20#include "compiler.h"
21#include "debug.h"
22#ifndef NULL
23#define NULL 0
24#endif
25
26
27using namespace std;
28
29/**
30   \brief standard constructor
31*/
32QuickAnimation::QuickAnimation (void)
33{
34   this->setClassID(CL_QUICK_ANIMATION, "QuickAnimation");
35
36   // initialize the First KeyFrame (will be deleted if a new one gets created)
37   this->count = 0;
38   this->first = this->current = new QuickKeyFrame;
39   this->first->next = this->first->prev = this->first;
40   this->first->position = 0.0;
41   this->first->value = 0.0;
42}
43
44/**
45   \brief deletes all the deconstructor stuff
46*/
47QuickAnimation::~QuickAnimation (void)
48{
49  this->current = this->first;
50  QuickKeyFrame* delKF = this->first;
51
52  do
53  {
54    delKF = this->current;
55    this->current = this->current->next;
56    delete delKF;
57  }  while (this->current != this->first);
58
59}
60
61/**
62   \brief adds a new entry to the list of keyframes
63   \param position the position to add the key to
64   \param value the Value to set for the position
65   \returns false if the key existed already for a given position
66*/
67void QuickAnimation::addEntry(float position, float value)
68{
69  if (this->getName())
70  {
71    printf("ADDED KEYFRAME %d\n", this->count);
72  }
73
74  // create the new KeyFrame
75  QuickKeyFrame* newKey = new QuickKeyFrame;
76  newKey->position = position;
77  newKey->value = value;
78
79  // if we add a KeyFrame for the first Time:
80  if (unlikely (this->count == 0))
81  {
82    delete this->first;
83
84    newKey->next  = newKey;
85    newKey->prev  = newKey;
86    this->first   = newKey;
87    this->current = newKey;
88  }
89  // if the KeyFrame is in front of the FIRST keyFrame
90  else if (this->first->position > position)
91  {
92    newKey->next = this->first;
93    newKey->prev = this->first->prev;
94    newKey->prev->next = newKey;
95    newKey->next->prev = newKey;
96
97    this->first = newKey; // the new Key becomes the FIRST.
98  }
99  // if the KeyFrame is at the End of the Animation
100  else if (this->first->prev->position < position)
101  {
102    newKey->next = this->first;
103    newKey->prev = this->first->prev;
104    newKey->prev->next = newKey;
105    newKey->next->prev = newKey;
106  }
107  // if the Key is between two frames.
108  else
109  {
110    this->current = this->first;
111    do
112    {
113      if (this->current->position < position && this->current->next->position > position)
114        break;
115      // if it is the same as an already existing keyframe
116      else if (this->current->position == position)
117      {
118        this->current->value = value;
119        delete newKey;
120        return;
121      }
122      this->current = this->current->next;
123    } while (this->current != this->first);
124
125    newKey->next = this->current->next;
126    newKey->prev = this->current;
127    newKey->next->prev = newKey;
128    newKey->prev->next = newKey;
129  }
130  this->current = this->first;
131  ++this->count;
132}
133
134/**
135   \brief changes an entry in the region of position
136   \param position the Position of an existing keyframe
137   \param region a deviation of the existing keyframe (like a delta in witch to search for
138   \param value the new Value
139
140   \todo rimplement
141*/
142void QuickAnimation::changeEntry(float position, float value, float region)
143{
144//   this->current = this->first;
145//   do
146//   {
147//     if (this->current->position < position+region && this->current->position > position-region)
148//     {
149//       this->current->value = value;
150//       return;
151//     }
152//     this->current = this->current->next;
153//   }  while (this->current != this->first);
154//
155//   this->current = this->first;
156
157  this->addEntry(position, value);
158}
159
160/*
161  \param position The position where to find the Node to kill
162
163  bool QuickAnimation::removeEntry(float position)
164  {
165  this->current = this->first;
166  QuickKeyFrame* last =
167
168  while (this->current)
169  {
170  if (this->current->position == position)
171  {
172
173
174  }
175  this->current = this->current->next;
176  }
177  this->current = this->first;
178  }
179*/
180
181/**
182   \brief returns the value of the animation at a certain position
183   \param position the position to get the value from :)
184*/
185float QuickAnimation::getValue(float position)
186{
187  int counter = 0;
188  while (true)
189  {
190    counter++;
191    if (counter >= 10)
192    {
193      printf("FUCK!!! %f\n", position);
194      this->debug();
195      break;
196
197    }
198    // if we have a match
199    if (likely(this->current->position <= position && this->current->next->position >= position))
200      return this->current->value + (this->current->next->value - this->current->value)
201          * ((position-this->current->position) / (this->current->next->position -this->current->position));
202
203    else if (unlikely(this->first->prev->position < position))
204      return this->first->prev->value;
205    else if (unlikely(this->first->position > position))
206      return this->first->value;
207    else if(likely(this->current->next->position < position))
208      this->current = this->current->next;
209    else if (likely(this->current->position > position))
210      this->current = this->current->prev;
211  }
212}
213
214/**
215   \brief outputs some nice information about this class
216*/
217void QuickAnimation::debug(void)
218{
219  this->current = this->first;
220
221  PRINT(0)("QuickAnim(entries:%d)::(position, value)", this->count);
222  do
223    {
224      PRINT(0)("->(%f, %f)", this->current->position, this->current->value);
225      this->current = this->current->next;
226    } while(this->current != this->first);
227
228  PRINT(0)("\n");
229  this->current = this->first;
230}
Note: See TracBrowser for help on using the repository browser.