<commit_before>// Proprietary code of Avination Virtual Limited
// (c) 2012 <NAME>
//

using System;
using System.Timers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Physics.Manager;
using OpenSim.Region.Framework.Scenes.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Timer = System.Timers.Timer;
using log4net;

namespace OpenSim.Region.Framework.Scenes
{
    [Serializable]
    public class KeyframeMotion
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public enum PlayMode : int
        {
            Forward = 0,
            Reverse = 1,
            Loop = 2,
            PingPong = 3
        };

        [Flags]
        public enum DataFormat : int
        {
            Translation = 1,
            Rotation = 2
        }

        [Serializable]
        public struct Keyframe
        {
            public Vector3? Position;
            public Quaternion? Rotation;
            public Quaternion StartRotation;
            public int TimeMS;
            public int TimeTotal;
            public Vector3 AngularVelocity;
        };

        private Vector3 m_basePosition;
        private Quaternion m_baseRotation;
        private Vector3 m_serializedPosition;

        private Keyframe m_currentFrame;
        private List<Keyframe> m_frames = new List<Keyframe>();

        private Keyframe[] m_keyframes;

        [NonSerialized()]
        protected Timer m_timer = new Timer();

        [NonSerialized()]
        private SceneObjectGroup m_group;

        private PlayMode m_mode = PlayMode.Forward;
        private DataFormat m_data = DataFormat.Translation | DataFormat.Rotation;

        private bool m_running = false;
        [NonSerialized()]
        private bool m_selected = false;

        private int m_iterations = 0;

        private const double timerInterval = 50.0;

        public DataFormat Data
        {
            get { return m_data; }
        }

        public bool Selected
        {
            set
            {
                if (value)
                {
                    // Once we're let go, recompute positions
                    if (m_selected)
                        UpdateSceneObject(m_group);
                }
                else
                {
                    // Save selection position in case we get moved
                    if (!m_selected)
                        m_serializedPosition = m_group.AbsolutePosition;
                }
                m_selected = value; }
        }

        public static KeyframeMotion FromData(SceneObjectGroup grp, Byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);

            BinaryFormatter fmt = new BinaryFormatter();

            KeyframeMotion newMotion = (KeyframeMotion)fmt.Deserialize(ms);

            // This will be started when position is updated
            newMotion.m_timer = new Timer();
            newMotion.m_timer.Interval = (int)timerInterval;
            newMotion.m_timer.AutoReset = true;
            newMotion.m_timer.Elapsed += newMotion.OnTimer;

            return newMotion;
        }

        public void UpdateSceneObject(SceneObjectGroup grp)
        {
            m_group = grp;
            Vector3 offset = grp.AbsolutePosition - m_serializedPosition;

            m_basePosition += offset;
            m_currentFrame.Position += offset;
            for (int i = 0 ; i < m_frames.Count ; i++)
            {
                Keyframe k = m_frames[i];
                k.Position += offset;
                m_frames[i] = k;
            }

            if (m_running)
                Start();
        }

        public KeyframeMotion(SceneObjectGroup grp, PlayMode mode, DataFormat data)
        {
            m_mode = mode;
            m_data = data;

            m_group = grp;
            m_basePosition = grp.AbsolutePosition;
            m_baseRotation = grp.GroupRotation;

            m_timer.Interval = (int)timerInterval;
            m_timer.AutoReset = true;
            m_timer.Elapsed += OnTimer;
        }

        public void SetKeyframes(Keyframe[] frames)
        {
            m_keyframes = frames;
        }

        public void Start()
        {
            if (m_keyframes.Length > 0)
                m_timer.Start();
            m_running = true;
        }

        public void Stop()
        {
            // Failed object creation
            if (m_timer == null)
                return;
            m_timer.Stop();

            m_basePosition = m_group.AbsolutePosition;
            m_baseRotation = m_group.GroupRotation;

            m_group.RootPart.Velocity = Vector3.Zero;
            m_group.RootPart.UpdateAngularVelocity(Vector3.Zero);
            m_group.SendGroupRootTerseUpdate();

            m_frames.Clear();<commit_msg>Add a frame based watchdog function to keyframed motion<commit_after>// Proprietary code of Avination Virtual Limited
// (c) 2012 <NAME>
//

using System;
using System.Timers;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Region.Framework.Interfaces;
using OpenSim.Region.Physics.Manager;
using OpenSim.Region.Framework.Scenes.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using Timer = System.Timers.Timer;
using log4net;

namespace OpenSim.Region.Framework.Scenes
{
    [Serializable]
    public class KeyframeMotion
    {
        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public enum PlayMode : int
        {
            Forward = 0,
            Reverse = 1,
            Loop = 2,
            PingPong = 3
        };

        [Flags]
        public enum DataFormat : int
        {
            Translation = 1,
            Rotation = 2
        }

        [Serializable]
        public struct Keyframe
        {
            public Vector3? Position;
            public Quaternion? Rotation;
            public Quaternion StartRotation;
            public int TimeMS;
            public int TimeTotal;
            public Vector3 AngularVelocity;
        };

        private Vector3 m_basePosition;
        private Quaternion m_baseRotation;
        private Vector3 m_serializedPosition;

        private Keyframe m_currentFrame;
        private List<Keyframe> m_frames = new List<Keyframe>();

        private Keyframe[] m_keyframes;

        [NonSerialized()]
        protected Timer m_timer = new Timer();
        [NonSerialized()]
        protected bool m_frameHooked;
        [NonSerialized()]
        protected int frameCount = 0;

        [NonSerialized()]
        private SceneObjectGroup m_group;

        private PlayMode m_mode = PlayMode.Forward;
        private DataFormat m_data = DataFormat.Translation | DataFormat.Rotation;

        private bool m_running = false;
        [NonSerialized()]
        private bool m_selected = false;

        private int m_iterations = 0;

        private const double timerInterval = 50.0;

        public DataFormat Data
        {
            get { return m_data; }
        }

        public bool Selected
        {
            set
            {
                if (value)
                {
                    // Once we're let go, recompute positions
                    if (m_selected)
                        UpdateSceneObject(m_group);
                }
                else
                {
                    // Save selection position in case we get moved
                    if (!m_selected)
                        m_serializedPosition = m_group.AbsolutePosition;
                }
                m_selected = value; }
        }

        public static KeyframeMotion FromData(SceneObjectGroup grp, Byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);

            BinaryFormatter fmt = new BinaryFormatter();

            KeyframeMotion newMotion = (KeyframeMotion)fmt.Deserialize(ms);

            // This will be started when position is updated
            newMotion.m_timer = new Timer();
            newMotion.m_timer.Interval = (int)timerInterval;
            newMotion.m_timer.AutoReset = true;
            newMotion.m_timer.Elapsed += newMotion.OnTimer;

            return newMotion;
        }

        public void UpdateSceneObject(SceneObjectGroup grp)
        {
            m_group = grp;
            Vector3 offset = grp.AbsolutePosition - m_serializedPosition;

            m_basePosition += offset;
            m_currentFrame.Position += offset;
            for (int i = 0 ; i < m_frames.Count ; i++)
            {
                Keyframe k = m_frames[i];
                k.Position += offset;
                m_frames[i] = k;
            }

            if (m_running)
                Start();
        }

        public KeyframeMotion(SceneObjectGroup grp, PlayMode mode, DataFormat data)
        {
            m_mode = mode;
            m_data = data;

            m_group = grp;
            m_basePosition = grp.AbsolutePosition;
            m_baseRotation = grp.GroupRotation;

            m_timer.Interval = (int)timerInterval;
            m_timer.AutoReset = true;
            m_timer.Elapsed += OnTimer;
        }

        public void SetKeyframes(Keyframe[] frames)
        {
            m_keyframes = frames;
        }

        public void Start()
        {
            if (m_keyframes.Length > 0)
                m_timer.Start();
            m_running = true;
            if (!m_frameHooked)
            {
                m_group.Scene.EventManager.OnFrame += OnFrame;
                m_frameHooked = true;
            }
        }

        private void OnFrame()
        {
            frameCount++;
            if (frameCount >= 30)
            {
                frameCount = 0;
                if (m_running)
                    Start();
            }
        }

        public void Stop()
        {
            // Failed object creation
            if (m_timer == null)
                return;
            m_timer.Stop();

            m_basePosition = m_group.AbsolutePosition;
            m_baseRotation = m_group.GroupRotation;

            m_group.RootPart.Velocity = Vector3.Zero;
            m_group.RootPart.UpdateAngularVelocity(Vector3.Zero);
            m_group.SendGroupRootTerseUpdate();

            m_frames.Clear();
{'fraction_non_alphanumeric': 0.07335510965935604, 'fraction_numerical': 0.0048530097993467105, 'mean_word_length': 1.6079338038452178, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1078920', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2707, 'n_words': 672}