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