Animation.cpp
Go to the documentation of this file.
1 #include "Animation.h"
2 #include "ImageItem.h"
3 
4 te::qt::widgets::Animation::Animation(QObject* target, const QByteArray& propertyName, QObject* parent)
5  : QPropertyAnimation(target, propertyName, parent)
6 {
8  ai->m_animation = this;
9 }
10 
12 
14 {
15  if(m_temporalAnimationExtent != period)
17 
18  AnimationItem* ai = (AnimationItem*)targetObject();
19 
20  // create route points in display projection
22 
24 }
25 
27 {
28  te::qt::widgets::AnimationItem* ai = static_cast<te::qt::widgets::AnimationItem*>(targetObject());
29  if (ai->pixmap().isNull())
30  {
32  if (ii->m_animationFiles.empty()) // out of animation time
33  {
34  ai->m_animationTime.clear();
35  ai->m_animationRoute.clear();
36  setStartValue(QPoint(0, 0));
37  setEndValue(QPoint(1, 1));
38  return;
39  }
40  }
41 
42  // total time duration
43  boost::posix_time::ptime iTime = m_temporalAnimationExtent.getInitialTimeInstant().getTimeInstant();
44  boost::posix_time::ptime fTime = m_temporalAnimationExtent.getFinalTimeInstant().getTimeInstant();
45  boost::posix_time::time_duration diff = fTime - iTime;
46  double totalSeconds = diff.total_seconds();
47 
48  size_t size = ai->m_animationTime.size();
49 
50  // set first animation point
51  setStartValue(ai->m_animationRoute[0]);
52 
53  boost::posix_time::ptime itime = ai->m_animationTime[0].getTimeInstant();
54  boost::posix_time::ptime ftime = ai->m_animationTime[(int)size-1].getTimeInstant();
55 
56  // if the initial time is greater than the total initial time,
57  // be stopped until the time be equal to the initial time this animation.
58  // To this add a key value.
59  ai->m_norInitialTime = 0;
60  if(itime > iTime)
61  {
62  // be stopped until the beginning of the animation time
63  diff = itime - iTime;
64  double seconds = diff.total_seconds();
65 
66  // normalizing the time
67  double t = seconds / totalSeconds;
68  ai->m_norInitialTime = t;
69  t *= .999999;
70  setKeyValueAt(t, ai->m_animationRoute[0]); // add a new key value
71  }
72 
73  for(size_t i = 1; i < size-1; ++i)
74  {
75  te::dt::TimeInstant tinstant = ai->m_animationTime[(int)i]; // animation time instant
76  boost::posix_time::ptime time = tinstant.getTimeInstant();
77  diff = time - iTime;
78  double seconds = diff.total_seconds();
79 
80  // normalizing the time
81  double t = seconds / totalSeconds;
82  setKeyValueAt(t, ai->m_animationRoute[(int)i]);
83  }
84 
85  // if the final time is shorter than the total final time,
86  // stop when reach the end of this animation
87  // To this add a key value.
88  ai->m_norFinalTime = 1;
89  if(ftime < fTime)
90  {
91  // be stopped when it reaches the end of its animation
92  diff = ftime - iTime;
93  double seconds = diff.total_seconds();
94 
95  // normalizing the time
96  double t = seconds / totalSeconds;
97  ai->m_norFinalTime = t;
98  setKeyValueAt(t, ai->m_animationRoute[(int)size-1]); // add a new key value
99  }
100 
101  // set last animation point
102  setEndValue(ai->m_animationRoute[(int)size-1]);
103 }
104 
106 {
107  AnimationItem* ai = (AnimationItem*)targetObject();
108 
109  // temporal animation extent
110  boost::posix_time::ptime iTime = m_temporalAnimationExtent.getInitialTimeInstant().getTimeInstant();
111  boost::posix_time::ptime fTime = m_temporalAnimationExtent.getFinalTimeInstant().getTimeInstant();
112  boost::posix_time::time_duration diff = fTime - iTime;
113  double totalSeconds = diff.total_seconds();
114 
115  // get curent time
116  int secs = qRound(totalSeconds * trel);
117  boost::posix_time::time_duration td = boost::posix_time::seconds(secs);
118  boost::posix_time::ptime curTime = iTime + td;
119 
120  if (curTime < iTime || curTime > fTime) // out of animation
121  return -1;
122 
123  size_t count = ai->m_animationTime.count();
124  int min = 0;
125  int max = (int)count - 1;
126 
127  while ((max - min) > 100)
128  {
129  int i = min + (max - min) / 2;
130  te::dt::TimeInstant tinstant = ai->m_animationTime[i];
131  boost::posix_time::ptime time = tinstant.getTimeInstant();
132 
133  if (time == curTime)
134  return i;
135  else if (time > curTime)
136  max = i;
137  else if (time < curTime)
138  min = i;
139  }
140 
141  for (int i = min; i <= max; ++i)
142  {
143  te::dt::TimeInstant tinstant = ai->m_animationTime[i];
144  boost::posix_time::ptime time = tinstant.getTimeInstant();
145 
146  if (time == curTime)
147  return i;
148  else if (time > curTime)
149  {
150  if (i == 0 || i == ((int)count - 1))
151  return i;
152  else
153  {
154  diff = time - curTime;
155  unsigned long long secs = abs(diff.total_seconds());
156 
157  tinstant = ai->m_animationTime[i - 1]; // before curTime
158  boost::posix_time::ptime btime = tinstant.getTimeInstant();
159  diff = btime - curTime;
160  unsigned long long bsecs = abs(diff.total_seconds());
161 
162  tinstant = ai->m_animationTime[i + 1]; // after curTime
163  boost::posix_time::ptime atime = tinstant.getTimeInstant();
164  diff = atime - curTime;
165  unsigned long long asecs = abs(diff.total_seconds());
166 
167  if (secs < bsecs && secs < asecs)
168  return i;
169  else if (bsecs < asecs)
170  return i - 1;
171  else
172  return i + 1;
173  }
174  }
175  }
176  return -1;
177 }
178 
180 {
181  int out = getClosestAnimationDataIndex(trel);
182  if (out == -1)
183  return out;
184 
185  int ret;
186 
187  AnimationItem* ai = (AnimationItem*)targetObject();
188  boost::posix_time::ptime iTime = m_temporalAnimationExtent.getInitialTimeInstant().getTimeInstant();
189  boost::posix_time::ptime fTime = m_temporalAnimationExtent.getFinalTimeInstant().getTimeInstant();
190  boost::posix_time::time_duration diff = fTime - iTime;
191  double totalSeconds = diff.total_seconds();
192 
193  te::dt::TimeInstant tinstant = ai->m_animationTime[out];
194  boost::posix_time::ptime time = tinstant.getTimeInstant();
195  diff = time - iTime;
196  double timeSeconds = diff.total_seconds();
197 
198  if (direction() == QAbstractAnimation::Forward)
199  {
200  if (timeSeconds / totalSeconds > trel)
201  ret = out;
202  else
203  ret = out + 1;
204  }
205  else
206  {
207  if (timeSeconds / totalSeconds < trel)
208  ret = out;
209  else
210  ret = out - 1;
211  }
212 
213  int count = ai->m_animationTime.count();
214 
215  if (ret >= count)
216  return count - 1;
217  if (ret <= 0)
218  return 0;
219 
220  return ret;
221 }
static te::dt::TimeDuration td(20, 30, 50, 11)
int getNextAnimationDataIndex(const double &trel)
It It gets the next animation data index. it is the end point, between two points of the trajectory...
Definition: Animation.cpp:179
double m_norInitialTime
Normalized initial time (between 0 and 1).
This file defines a abstarct class for a Image Item.
const boost::posix_time::ptime & getTimeInstant() const
It returns the boost time instant type.
Definition: TimeInstant.h:92
A class to represent time instant.
Definition: TimeInstant.h:55
Animation * m_animation
The animation this item.
double m_norFinalTime
Normalized final time (between 0 and 1).
int getClosestAnimationDataIndex(const double &trel)
It It gets the closest animation data index.
Definition: Animation.cpp:105
virtual void adjustDataToAnimationTemporalExtent()
Adjust data for a given time animation period.
TimeInstant getFinalTimeInstant() const
It gets the final time instant.
Definition: TimePeriod.cpp:50
TimeInstant getInitialTimeInstant() const
It gets the initial time instant.
Definition: TimePeriod.cpp:45
void setDataKeyValues()
It It sets the data key values.
Definition: Animation.cpp:26
virtual ~Animation()
Destructor It destructs a Animation.
A class to represent time period.
Definition: TimePeriod.h:54
This file defines a class for a Trajectory Animation.
QVector< QString > m_animationFiles
The png files in display projection. It contains only the portions to be animated.
Definition: ImageItem.h:197
An abstract class for Animation Item.
Definition: AnimationItem.h:64
QVector< te::dt::TimeInstant > m_animationTime
The animation time. It contains only the portions to be animated.
Animation(QObject *target, const QByteArray &propertyName, QObject *parent=0)
Constructor It constructs a Animation.
Definition: Animation.cpp:4
QVector< QPointF > m_animationRoute
It contains only the portions to be animated.
void adjustDataToAnimationTemporalExtent(const te::dt::TimePeriod &period)
It adjust animation data for a given period; /param period The temporal period.
Definition: Animation.cpp:13
te::dt::TimePeriod m_temporalAnimationExtent
Temporal animation extent. It can be a portion of the total or greater than the total.
Definition: Animation.h:111