aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/Framework/Scenes/KeyframeMotion.cs')
-rw-r--r--OpenSim/Region/Framework/Scenes/KeyframeMotion.cs707
1 files changed, 707 insertions, 0 deletions
diff --git a/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs b/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs
new file mode 100644
index 0000000..5cfba39
--- /dev/null
+++ b/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs
@@ -0,0 +1,707 @@
1// Proprietary code of Avination Virtual Limited
2// (c) 2012 Melanie Thielker
3//
4
5using System;
6using System.Timers;
7using System.Collections;
8using System.Collections.Generic;
9using System.IO;
10using System.Diagnostics;
11using System.Reflection;
12using System.Threading;
13using OpenMetaverse;
14using OpenSim.Framework;
15using OpenSim.Region.Framework.Interfaces;
16using OpenSim.Region.Physics.Manager;
17using OpenSim.Region.Framework.Scenes.Serialization;
18using System.Runtime.Serialization.Formatters.Binary;
19using System.Runtime.Serialization;
20using Timer = System.Timers.Timer;
21using log4net;
22
23namespace OpenSim.Region.Framework.Scenes
24{
25 [Serializable]
26 public class KeyframeMotion
27 {
28 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
29
30 public enum PlayMode : int
31 {
32 Forward = 0,
33 Reverse = 1,
34 Loop = 2,
35 PingPong = 3
36 };
37
38 [Flags]
39 public enum DataFormat : int
40 {
41 Translation = 2,
42 Rotation = 1
43 }
44
45 [Serializable]
46 public struct Keyframe
47 {
48 public Vector3? Position;
49 public Quaternion? Rotation;
50 public Quaternion StartRotation;
51 public int TimeMS;
52 public int TimeTotal;
53 public Vector3 AngularVelocity;
54 };
55
56 private Vector3 m_serializedPosition;
57 private Vector3 m_basePosition;
58 private Quaternion m_baseRotation;
59
60 private Keyframe m_currentFrame;
61
62 private List<Keyframe> m_frames = new List<Keyframe>();
63
64 private Keyframe[] m_keyframes;
65
66 [NonSerialized()]
67 protected Timer m_timer = null;
68
69 // timer lock
70 [NonSerialized()]
71 private object m_onTimerLock;
72
73 // timer overrun detect
74 // prevents overlap or timer events threads frozen on the lock
75 [NonSerialized()]
76 private bool m_inOnTimer;
77
78 // skip timer events.
79 //timer.stop doesn't assure there aren't event threads still being fired
80 [NonSerialized()]
81 private bool m_timerStopped;
82
83 [NonSerialized()]
84 private bool m_isCrossing;
85
86 [NonSerialized()]
87 private bool m_waitingCrossing;
88
89 // retry position for cross fail
90 [NonSerialized()]
91 private Vector3 m_nextPosition;
92
93 [NonSerialized()]
94 private SceneObjectGroup m_group;
95
96 private PlayMode m_mode = PlayMode.Forward;
97 private DataFormat m_data = DataFormat.Translation | DataFormat.Rotation;
98
99 private bool m_running = false;
100 [NonSerialized()]
101 private bool m_selected = false;
102
103 private int m_iterations = 0;
104
105 private const double timerInterval = 50.0;
106
107 public DataFormat Data
108 {
109 get { return m_data; }
110 }
111
112 public bool Selected
113 {
114 set
115 {
116 if (m_group != null)
117 {
118 if (!value)
119 {
120 // Once we're let go, recompute positions
121 if (m_selected)
122 UpdateSceneObject(m_group);
123 }
124 else
125 {
126 // Save selection position in case we get moved
127 if (!m_selected)
128 {
129 StopTimer();
130 m_serializedPosition = m_group.AbsolutePosition;
131 }
132 }
133 }
134 m_isCrossing = false;
135 m_waitingCrossing = false;
136 m_selected = value;
137 }
138 }
139
140 private void StartTimer()
141 {
142 if (m_timer == null)
143 return;
144 m_timerStopped = false;
145 m_timer.Start();
146 }
147
148 private void StopTimer()
149 {
150 if (m_timer == null || m_timerStopped)
151 return;
152 m_timerStopped = true;
153 m_timer.Stop();
154 }
155
156 private void RemoveTimer()
157 {
158 if (m_timer == null)
159 return;
160 m_timerStopped = true;
161 m_timer.Stop();
162 m_timer.Elapsed -= OnTimer;
163 m_timer = null;
164 }
165
166
167 public static KeyframeMotion FromData(SceneObjectGroup grp, Byte[] data)
168 {
169 KeyframeMotion newMotion = null;
170
171 try
172 {
173 MemoryStream ms = new MemoryStream(data);
174 BinaryFormatter fmt = new BinaryFormatter();
175
176 newMotion = (KeyframeMotion)fmt.Deserialize(ms);
177
178 newMotion.m_group = grp;
179
180 if (grp != null && grp.IsSelected)
181 newMotion.m_selected = true;
182
183 newMotion.m_onTimerLock = new object();
184 newMotion.m_timerStopped = false;
185 newMotion.m_inOnTimer = false;
186 newMotion.m_isCrossing = false;
187 newMotion.m_waitingCrossing = false;
188 }
189 catch
190 {
191 newMotion = null;
192 }
193
194 return newMotion;
195 }
196
197 public void UpdateSceneObject(SceneObjectGroup grp)
198 {
199// lock (m_onTimerLock)
200 {
201 m_isCrossing = false;
202 m_waitingCrossing = false;
203 StopTimer();
204
205 if (grp == null)
206 return;
207
208 m_group = grp;
209 Vector3 grppos = grp.AbsolutePosition;
210 Vector3 offset = grppos - m_serializedPosition;
211 // avoid doing it more than once
212 // current this will happen draging a prim to other region
213 m_serializedPosition = grppos;
214
215 m_basePosition += offset;
216 m_currentFrame.Position += offset;
217
218 m_nextPosition += offset;
219
220 for (int i = 0; i < m_frames.Count; i++)
221 {
222 Keyframe k = m_frames[i];
223 k.Position += offset;
224 m_frames[i]=k;
225 }
226
227 if (m_running)
228 Start();
229 }
230 }
231
232 public KeyframeMotion(SceneObjectGroup grp, PlayMode mode, DataFormat data)
233 {
234 m_mode = mode;
235 m_data = data;
236
237 m_group = grp;
238 if (grp != null)
239 {
240 m_basePosition = grp.AbsolutePosition;
241 m_baseRotation = grp.GroupRotation;
242 }
243
244 m_onTimerLock = new object();
245 m_timerStopped = true;
246 m_inOnTimer = false;
247 m_isCrossing = false;
248 m_waitingCrossing = false;
249 }
250
251 public void SetKeyframes(Keyframe[] frames)
252 {
253 m_keyframes = frames;
254 }
255
256 public KeyframeMotion Copy(SceneObjectGroup newgrp)
257 {
258 StopTimer();
259
260 KeyframeMotion newmotion = new KeyframeMotion(null, m_mode, m_data);
261
262 newmotion.m_group = newgrp;
263
264 if (m_keyframes != null)
265 {
266 newmotion.m_keyframes = new Keyframe[m_keyframes.Length];
267 m_keyframes.CopyTo(newmotion.m_keyframes, 0);
268 }
269
270 newmotion.m_frames = new List<Keyframe>(m_frames);
271
272 newmotion.m_basePosition = m_basePosition;
273 newmotion.m_baseRotation = m_baseRotation;
274
275 if (m_selected)
276 newmotion.m_serializedPosition = m_serializedPosition;
277 else
278 {
279 if (m_group != null)
280 newmotion.m_serializedPosition = m_group.AbsolutePosition;
281 else
282 newmotion.m_serializedPosition = m_serializedPosition;
283 }
284
285 newmotion.m_currentFrame = m_currentFrame;
286
287 newmotion.m_iterations = m_iterations;
288 newmotion.m_running = m_running;
289
290 if (m_running && !m_waitingCrossing)
291 StartTimer();
292
293 return newmotion;
294 }
295
296 public void Delete()
297 {
298 m_running = false;
299 RemoveTimer();
300 m_isCrossing = false;
301 m_waitingCrossing = false;
302 m_frames.Clear();
303 m_keyframes = null;
304 }
305
306 public void Start()
307 {
308 m_isCrossing = false;
309 m_waitingCrossing = false;
310 if (m_keyframes != null && m_group != null && m_keyframes.Length > 0)
311 {
312 if (m_timer == null)
313 {
314 m_timer = new Timer();
315 m_timer.Interval = timerInterval;
316 m_timer.AutoReset = true;
317 m_timer.Elapsed += OnTimer;
318 }
319 else
320 {
321 StopTimer();
322 m_timer.Interval = timerInterval;
323 }
324
325 m_inOnTimer = false;
326 StartTimer();
327 m_running = true;
328 }
329 else
330 {
331 m_running = false;
332 RemoveTimer();
333 }
334 }
335
336 public void Stop()
337 {
338 m_running = false;
339 m_isCrossing = false;
340 m_waitingCrossing = false;
341
342 RemoveTimer();
343
344 m_basePosition = m_group.AbsolutePosition;
345 m_baseRotation = m_group.GroupRotation;
346
347 m_group.RootPart.Velocity = Vector3.Zero;
348 m_group.RootPart.AngularVelocity = Vector3.Zero;
349 m_group.SendGroupRootTerseUpdate();
350// m_group.RootPart.ScheduleTerseUpdate();
351 m_frames.Clear();
352 }
353
354 public void Pause()
355 {
356 m_running = false;
357 RemoveTimer();
358
359 m_group.RootPart.Velocity = Vector3.Zero;
360 m_group.RootPart.AngularVelocity = Vector3.Zero;
361 m_group.SendGroupRootTerseUpdate();
362// m_group.RootPart.ScheduleTerseUpdate();
363
364 }
365
366 private void GetNextList()
367 {
368 m_frames.Clear();
369 Vector3 pos = m_basePosition;
370 Quaternion rot = m_baseRotation;
371
372 if (m_mode == PlayMode.Loop || m_mode == PlayMode.PingPong || m_iterations == 0)
373 {
374 int direction = 1;
375 if (m_mode == PlayMode.Reverse || ((m_mode == PlayMode.PingPong) && ((m_iterations & 1) != 0)))
376 direction = -1;
377
378 int start = 0;
379 int end = m_keyframes.Length;
380// if (m_mode == PlayMode.PingPong && m_keyframes.Length > 1)
381// end = m_keyframes.Length - 1;
382
383 if (direction < 0)
384 {
385 start = m_keyframes.Length - 1;
386 end = -1;
387// if (m_mode == PlayMode.PingPong && m_keyframes.Length > 1)
388// end = 0;
389 }
390
391 for (int i = start; i != end ; i += direction)
392 {
393 Keyframe k = m_keyframes[i];
394
395 if (k.Position.HasValue)
396 {
397 k.Position = (k.Position * direction);
398// k.Velocity = (Vector3)k.Position / (k.TimeMS / 1000.0f);
399 k.Position += pos;
400 }
401 else
402 {
403 k.Position = pos;
404// k.Velocity = Vector3.Zero;
405 }
406
407 k.StartRotation = rot;
408 if (k.Rotation.HasValue)
409 {
410 if (direction == -1)
411 k.Rotation = Quaternion.Conjugate((Quaternion)k.Rotation);
412 k.Rotation = rot * k.Rotation;
413 }
414 else
415 {
416 k.Rotation = rot;
417 }
418
419/* ang vel not in use for now
420
421 float angle = 0;
422
423 float aa = k.StartRotation.X * k.StartRotation.X + k.StartRotation.Y * k.StartRotation.Y + k.StartRotation.Z * k.StartRotation.Z + k.StartRotation.W * k.StartRotation.W;
424 float bb = ((Quaternion)k.Rotation).X * ((Quaternion)k.Rotation).X + ((Quaternion)k.Rotation).Y * ((Quaternion)k.Rotation).Y + ((Quaternion)k.Rotation).Z * ((Quaternion)k.Rotation).Z + ((Quaternion)k.Rotation).W * ((Quaternion)k.Rotation).W;
425 float aa_bb = aa * bb;
426
427 if (aa_bb == 0)
428 {
429 angle = 0;
430 }
431 else
432 {
433 float ab = k.StartRotation.X * ((Quaternion)k.Rotation).X +
434 k.StartRotation.Y * ((Quaternion)k.Rotation).Y +
435 k.StartRotation.Z * ((Quaternion)k.Rotation).Z +
436 k.StartRotation.W * ((Quaternion)k.Rotation).W;
437 float q = (ab * ab) / aa_bb;
438
439 if (q > 1.0f)
440 {
441 angle = 0;
442 }
443 else
444 {
445 angle = (float)Math.Acos(2 * q - 1);
446 }
447 }
448
449 k.AngularVelocity = (new Vector3(0, 0, 1) * (Quaternion)k.Rotation) * (angle / (k.TimeMS / 1000));
450 */
451 k.TimeTotal = k.TimeMS;
452
453 m_frames.Add(k);
454
455 pos = (Vector3)k.Position;
456 rot = (Quaternion)k.Rotation;
457 }
458
459 m_basePosition = pos;
460 m_baseRotation = rot;
461
462 m_iterations++;
463 }
464 }
465
466 protected void OnTimer(object sender, ElapsedEventArgs e)
467 {
468 if (m_timerStopped) // trap events still in air even after a timer.stop
469 return;
470
471 if (m_inOnTimer) // don't let overruns to happen
472 {
473 m_log.Warn("[KeyFrame]: timer overrun");
474 return;
475 }
476
477 if (m_group == null)
478 return;
479
480 lock (m_onTimerLock)
481 {
482
483 m_inOnTimer = true;
484
485 bool update = false;
486
487 try
488 {
489 if (m_selected)
490 {
491 if (m_group.RootPart.Velocity != Vector3.Zero)
492 {
493 m_group.RootPart.Velocity = Vector3.Zero;
494 m_group.SendGroupRootTerseUpdate();
495// m_group.RootPart.ScheduleTerseUpdate();
496
497 }
498 m_inOnTimer = false;
499 return;
500 }
501
502 if (m_isCrossing)
503 {
504 // if crossing and timer running then cross failed
505 // wait some time then
506 // retry to set the position that evtually caused the outbound
507 // if still outside region this will call startCrossing below
508 m_isCrossing = false;
509 m_group.AbsolutePosition = m_nextPosition;
510 if (!m_isCrossing)
511 {
512 StopTimer();
513 m_timer.Interval = timerInterval;
514 StartTimer();
515 }
516 m_inOnTimer = false;
517 return;
518 }
519
520 if (m_frames.Count == 0)
521 {
522 GetNextList();
523
524 if (m_frames.Count == 0)
525 {
526 Stop();
527 m_inOnTimer = false;
528 return;
529 }
530
531 m_currentFrame = m_frames[0];
532 m_currentFrame.TimeMS += (int)timerInterval;
533
534 //force a update on a keyframe transition
535 update = true;
536 }
537
538 m_currentFrame.TimeMS -= (int)timerInterval;
539
540 // Do the frame processing
541 double steps = (double)m_currentFrame.TimeMS / timerInterval;
542
543 if (steps <= 0.0)
544 {
545 m_group.RootPart.Velocity = Vector3.Zero;
546 m_group.RootPart.AngularVelocity = Vector3.Zero;
547
548 m_nextPosition = (Vector3)m_currentFrame.Position;
549 m_group.AbsolutePosition = m_nextPosition;
550
551 // we are sending imediate updates, no doing force a extra terseUpdate
552// m_group.UpdateGroupRotationR((Quaternion)m_currentFrame.Rotation);
553
554 m_group.RootPart.RotationOffset = (Quaternion)m_currentFrame.Rotation;
555 m_frames.RemoveAt(0);
556 if (m_frames.Count > 0)
557 m_currentFrame = m_frames[0];
558
559 update = true;
560 }
561 else
562 {
563 float complete = ((float)m_currentFrame.TimeTotal - (float)m_currentFrame.TimeMS) / (float)m_currentFrame.TimeTotal;
564
565 Vector3 v = (Vector3)m_currentFrame.Position - m_group.AbsolutePosition;
566 Vector3 motionThisFrame = v / (float)steps;
567 v = v * 1000 / m_currentFrame.TimeMS;
568
569 if (Vector3.Mag(motionThisFrame) >= 0.05f)
570 {
571 // m_group.AbsolutePosition += motionThisFrame;
572 m_nextPosition = m_group.AbsolutePosition + motionThisFrame;
573 m_group.AbsolutePosition = m_nextPosition;
574
575 m_group.RootPart.Velocity = v;
576 update = true;
577 }
578
579 if ((Quaternion)m_currentFrame.Rotation != m_group.GroupRotation)
580 {
581 Quaternion current = m_group.GroupRotation;
582
583 Quaternion step = Quaternion.Slerp(m_currentFrame.StartRotation, (Quaternion)m_currentFrame.Rotation, complete);
584 step.Normalize();
585/* use simpler change detection
586 * float angle = 0;
587
588 float aa = current.X * current.X + current.Y * current.Y + current.Z * current.Z + current.W * current.W;
589 float bb = step.X * step.X + step.Y * step.Y + step.Z * step.Z + step.W * step.W;
590 float aa_bb = aa * bb;
591
592 if (aa_bb == 0)
593 {
594 angle = 0;
595 }
596 else
597 {
598 float ab = current.X * step.X +
599 current.Y * step.Y +
600 current.Z * step.Z +
601 current.W * step.W;
602 float q = (ab * ab) / aa_bb;
603
604 if (q > 1.0f)
605 {
606 angle = 0;
607 }
608 else
609 {
610 angle = (float)Math.Acos(2 * q - 1);
611 }
612 }
613
614 if (angle > 0.01f)
615 */
616 if(Math.Abs(step.X - current.X) > 0.001f
617 || Math.Abs(step.Y - current.Y) > 0.001f
618 || Math.Abs(step.Z - current.Z) > 0.001f)
619 // assuming w is a dependente var
620
621 {
622// m_group.UpdateGroupRotationR(step);
623 m_group.RootPart.RotationOffset = step;
624
625 //m_group.RootPart.UpdateAngularVelocity(m_currentFrame.AngularVelocity / 2);
626 update = true;
627 }
628 }
629 }
630
631 if (update)
632 m_group.SendGroupRootTerseUpdate();
633// m_group.RootPart.ScheduleTerseUpdate();
634
635
636 }
637 catch ( Exception ex)
638 {
639 // still happening sometimes
640 // lets try to see where
641 m_log.Warn("[KeyFrame]: timer overrun" + ex.Message);
642 }
643
644 finally
645 {
646 // make sure we do not let this frozen
647 m_inOnTimer = false;
648 }
649 }
650 }
651
652 public Byte[] Serialize()
653 {
654 StopTimer();
655 MemoryStream ms = new MemoryStream();
656
657 BinaryFormatter fmt = new BinaryFormatter();
658 SceneObjectGroup tmp = m_group;
659 m_group = null;
660 if (!m_selected && tmp != null)
661 m_serializedPosition = tmp.AbsolutePosition;
662 fmt.Serialize(ms, this);
663 m_group = tmp;
664 if (m_running && !m_waitingCrossing)
665 StartTimer();
666
667 return ms.ToArray();
668 }
669
670 public void StartCrossingCheck()
671 {
672 // timer will be restart by crossingFailure
673 // or never since crossing worked and this
674 // should be deleted
675 StopTimer();
676
677 m_isCrossing = true;
678 m_waitingCrossing = true;
679
680// to remove / retune to smoth crossings
681 if (m_group.RootPart.Velocity != Vector3.Zero)
682 {
683 m_group.RootPart.Velocity = Vector3.Zero;
684 m_group.SendGroupRootTerseUpdate();
685// m_group.RootPart.ScheduleTerseUpdate();
686 }
687 }
688
689 public void CrossingFailure()
690 {
691 m_waitingCrossing = false;
692
693 if (m_group != null)
694 {
695 m_group.RootPart.Velocity = Vector3.Zero;
696 m_group.SendGroupRootTerseUpdate();
697// m_group.RootPart.ScheduleTerseUpdate();
698
699 if (m_running && m_timer != null)
700 {
701 m_timer.Interval = 60000;
702 StartTimer();
703 }
704 }
705 }
706 }
707}