<commit_before>using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;

namespace Captury
{
    //=================================
    // define captury class structures
    //=================================
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyJoint
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
        public byte[] name;
        public int parent;
        public float ox, oy, oz;
        public float rx, ry, rz;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyActor
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] name;
        public int id;
        public int numJoints;
        public IntPtr joints;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyPose
    {
        public int actor;
        public long timestamp;
        public int numValues;
        public IntPtr values;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyARTag
    {
        public int id;
        public float ox, oy, oz; // position
        public float nx, ny, nz; // normal
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct CapturyImage
    {
        public int width;
        public int height;
        public int camera;
        public ulong timestamp;
        public IntPtr data;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyTransform
    {
        public float rx; // rotation euler angles
        public float ry;
        public float rz;
        public float tx; // translation
        public float ty;
        public float tz;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyCamera
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] name;
        public int id;
        public float positionX;
        public float positionY;
        public float positionZ;
        public float orientationX;
        public float orientationY;
        public float orientationZ;
        public float sensorWidth;   // in mm
        public float sensorHeight;  // in mm
        public float focalLength;   // in mm
        public float lensCenterX;   // in mm
        public float lensCenterY;   // in mm
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] distortionModel;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
        public float distortion;

        // the following can be computed from the above values and are provided for convenience only
        // the matrices are stored column wise:
        // 0  3  6  9
        // 1  4  7 10
        // 2  5  8 11
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        float extrinsic;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
        float intrinsic;
    };

    //==========================================
    // internal structures that are more easy to use
    //==========================================
    [Serializable]
    public class CapturySkeletonJoint
    {
        public string name;
        public int parent;
        public Vector3 offset;
        public Vector3 orientation;
        public Transform transform;
    }

    [Serializable]
    public class CapturySkeleton
    {
        public string name;
        public int id;
        public int playerID = -1;
        public CapturySkeletonJoint[] joints;

        private GameObject reference;
        public GameObject mesh // reference to game object that is animated
        {
            get { return reference; }
            set
            {
                reference = value;
                if (reference == null)
                {
                    foreach (CapturySkeletonJoint j in joints)
                        j.transform = null;
                    return;
                }
                foreach (CapturySkeletonJoint j in joints)
                {
                    // check if the joint name matches a reference transform and assign it
                    ArrayList children = reference.transform.GetAllChildren();
                    foreach (Transform tra in children)
                    {
                        if (tra.name.EndsWith(j.name))
                        {
                            j.transform = tra;
                            continue;
                        }
                    }
                }
            }
        }
    }

    [Serializable]
    public class CapturyMarkerTransform
    {
        public Quaternion rotation;
        public Vector3 translation;
        public UInt64 timestamp;
        public float bestAccuracy;
        public bool consumed;
    }

    [Serializable]
    public class ARTag
    {
        public int id;
        public Vector3 translation;
        public Quaternion rotation;
    }

    //====================
    // the network plugin
    //====================
    [RequireComponent(typeof(CapturyOriginManager))]
    public class CapturyNetworkPlugin : MonoBehaviour
    {
        //=============================================
        // import the functions from RemoteCaptury dll
        //=============================================
        [DllImport("RemoteCaptury")]
        private static extern int Captury_connect(string ip, ushort port);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_disconnect();
        [DllImport("RemoteCaptury")]
        private static extern int Captury_getActors(out IntPtr actorData);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_startStreaming(int what);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_stopStreaming();
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getCurrentPose(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freePose(IntPtr pose);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_requestTexture(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getTexture(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeImage(IntPtr image);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_setRotationConstraint(int actorId, int jointIndex, IntPtr rotation, UInt64 timestamp, float weight);
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_getMarkerTransform(IntPtr actor, int jointIndex, IntPtr transform);
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_synchronizeTime();
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_getTime();
        [DllImport("RemoteCaptury")]
        private static extern Int64 Captury_getTimeOffset();
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getLastErrorMessage();
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeErrorMessage(IntPtr msg);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_getCameras(out IntPtr cameras);
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getCurrentARTags();
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeARTags(IntPtr arTags);

        // config settings will be overriden by CapturyConfigManager.Config if it can be loaded
        public string host = "127.0.0.1";
        public ushort port = 2101;
        public float scaleFactor = 0.001f; // mm to m
        public int actorCheckTimeout = 500; // in ms
        public bool streamARTags = false;

        // Events
        public delegate void SkeletonDelegate(CapturySkeleton skeleton);
        public event SkeletonDelegate SkeletonFound;
        public event SkeletonDelegate SkeletonLost;
        public delegate void CamerasChangedDelegate(Vector3[] positions, Quaternion[] rotations);
        public event CamerasChangedDelegate CamerasChanged;
        public delegate void ARTagsDetectedDelegate(ARTag[] artags);
        public event ARTagsDetectedDelegate ARTagsDetected;

        public Vector3[] cameraPositions;
        public Quaternion[] cameraOrientations;

        public ARTag[] arTags = new ARTag[0];

        /// <summary>
        /// Reference to <see cref="capturyOriginManager"/> which handles the origin of the coordinate system
        /// </summary>
        private CapturyOriginManager capturyOriginManager;

        /// <summary>
        /// Reference to the current <see cref="CapturyOrigin"/> in the scene which defines the origin of the coordinate system of all avatars
        /// </summary>
        private CapturyOrigin capturyOrigin;

        private string headJointName = "Head";

        // threading data for communication with server
        private Thread communicationThread;
        private Mutex communicationMutex = new Mutex();
        private bool communicationFinished = false;

        // internal variables
        private bool isConnected = false;
        private bool isSetup = false;

        // skeleton data from Captury
        private Dictionary<int, IntPtr> actorPointers = new Dictionary<int, IntPtr>();
        private Dictionary<int, int> actorFound = new Dictionary<int, int>();
        private Dictionary<int, CapturySkeleton> skeletons = new Dictionary<int, CapturySkeleton>();
        private Dictionary<int, CapturyMarkerTransform> headTransforms = new Dictionary<int, CapturyMarkerTransform>();
        private Dictionary<string, int> jointsWithConstraints = new Dictionary<string, int>();

        void Awake()
        {
            // load config
            CapturyConfig config = CapturyConfigManager.Config;
            if (config != null)
            {
                host = config.host;
                port = config.port;
                scaleFactor = config.scaleFactor;
                actorCheckTimeout = config.actorCheckTimeout;
                streamARTags = config.streamARTags;
                Debug.LogFormat("CapturyNetworkPlugin settings were overriden by values from {0}.", CapturyConfigManager.configFilePath);
            }

            // register to CapturyOrigin change event
            capturyOriginManager = GetComponent<CapturyOriginManager>();
            capturyOriginManager.CapturyOriginChanged += OnCapturyOriginChanged;
        }

        //=============================
        // this is run once at startup
        //=============================
        void Start()
        {
            // start the connection thread
            communicationThread = new Thread(lookForActors);
            communicationThread.Start();
        }

        //==========================
        // this is run once at exit
        //==========================
        void OnDisable()
        {
            communicationFinished = true;
            communicationThread.Join();
        }

        //============================
        // this is run once per frame
        //============================
        void Update()
        {
            // only perform if we are actually connected
            if (!isConnected)
                return;

            // make sure we lock access before doing anything
            //			Debug.Log ("Starting pose update...");
            communicationMutex.WaitOne();

            // fetch current pose for all skeletons
            foreach (KeyValuePair<int, CapturySkeleton> kvp in skeletons)
            {
                // get the actor id
                int actorID = kvp.Key;

                // check if the actor is mapped to something, if not, ignore
                if (skeletons[actorID].mesh == null)
                    continue;

                // get pointer to pose
                IntPtr poseData = Captury_getCurrentPose(actorPointers[actorID]);

                // check if we actually got data, if not, continue
                if (poseData == IntPtr.Zero)
                {
                    // something went wrong, get error message
                    IntPtr msg = Captury_getLastErrorMessage();
                    string errmsg = Marshal.PtrToStringAnsi(msg);
                    Debug.Log("Stream error: " + errmsg);
                    Captury_freeErrorMessage(msg);
                }
                else
                {

                    //Debug.Log("received pose for " + actorID);

                    // convert the pose
                    CapturyPose pose = (CapturyPose)Marshal.PtrToStructure(poseData, typeof(CapturyPose));

                    // get the data into a float array
                    float[] values = new float[pose.numValues * 6];
                    Marshal.Copy(pose.values, values, 0, pose.numValues * 6);

                    // now loop over all joints
                    Vector3 pos = new Vector3();
                    Vector3 rot = new Vector3();

                    // set origin offset based on CapturyOrigin, if existent. Otherwise keep world origin (0,0,0)
                    Vector3 offsetToOrigin = Vector3.zero;
                    if (capturyOrigin != null)
                    {
                        offsetToOrigin = capturyOrigin.OffsetToWorldOrigin;
                    }

                    for (int jointID = 0; jointID < skeletons[actorID].joints.Length; jointID++)
                    {
                        // ignore any joints that do not map to a transform
                        if (skeletons[actorID].joints[jointID].transform == null)
                            continue;

                        // set offset and rotation
                        int baseIndex = jointID * 6;
                        pos.Set(values[baseIndex + 0], values[baseIndex + 1], values[baseIndex + 2]);
                        rot.Set(values[baseIndex + 3], values[baseIndex + 4], values[baseIndex + 5]);

                        skeletons[actorID].joints[jointID].transform.position = ConvertPosition(pos) + offsetToOrigin;
                        skeletons[actorID].joints[jointID].transform.rotation = ConvertRotation(rot);
                    }

                    // finally, free the pose data again, as we are finished
                    Captury_freePose(poseData);
                }
            }

            // get artags
            IntPtr arTagData = Captury_getCurrentARTags();

            // check if we actually got data, if not, continue
            if (arTagData == IntPtr.Zero)
            {
                // something went wrong, get error message
                IntPtr msg = Captury_getLastErrorMessage();
                string errmsg = Marshal.PtrToStringAnsi(msg);
                Captury_freeErrorMessage(msg);
            }
            else
            {
                IntPtr at = arTagData;
                int num;
                for (num = 0; num < 100; ++num)
                {
                    CapturyARTag arTag = (CapturyARTag)Marshal.PtrToStructure(at, typeof(CapturyARTag));
                    if (arTag.id == -1)
                        break;
                    Array.Resize(ref arTags, num + 1);
                    arTags[num] = new ARTag();
                    arTags[num].id = arTag.id;
                    arTags[num].translation = ConvertPosition(new Vector3(arTag.ox, arTag.oy, arTag.oz));
                    arTags[num].rotation = ConvertRotation(Quaternion.LookRotation(new Vector3(arTag.nx, arTag.ny, arTag.nz)).eulerAngles);
                    at = new IntPtr(at.ToInt64() + Marshal.SizeOf(typeof(CapturyARTag)));
                }
                if (num != 0 && ARTagsDetected != null)
                    ARTagsDetected(arTags);
                else
                    Array.Resize(ref arTags, 0);

                Captury_freeARTags(arTagData);
            }

            communicationMutex.ReleaseMutex();
        }

        //================================================
        // This function continously looks for new actors
        // It runs in a separate thread
        //================================================
        void lookForActors()
        {
            while (!communicationFinished)
            {
                // wait for actorCheckTimeout ms before continuing
                //			Debug.Log ("Going to sleep...");
                Thread.Sleep(actorCheckTimeout);
                //			Debug.Log ("Waking up...");

                // now look for new data
<commit_msg>arTags with respect to CapturyOrigin<commit_after>using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using UnityEngine;

namespace Captury
{
    //=================================
    // define captury class structures
    //=================================
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyJoint
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
        public byte[] name;
        public int parent;
        public float ox, oy, oz;
        public float rx, ry, rz;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyActor
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] name;
        public int id;
        public int numJoints;
        public IntPtr joints;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyPose
    {
        public int actor;
        public long timestamp;
        public int numValues;
        public IntPtr values;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyARTag
    {
        public int id;
        public float ox, oy, oz; // position
        public float nx, ny, nz; // normal
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct CapturyImage
    {
        public int width;
        public int height;
        public int camera;
        public ulong timestamp;
        public IntPtr data;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyTransform
    {
        public float rx; // rotation euler angles
        public float ry;
        public float rz;
        public float tx; // translation
        public float ty;
        public float tz;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct CapturyCamera
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] name;
        public int id;
        public float positionX;
        public float positionY;
        public float positionZ;
        public float orientationX;
        public float orientationY;
        public float orientationZ;
        public float sensorWidth;   // in mm
        public float sensorHeight;  // in mm
        public float focalLength;   // in mm
        public float lensCenterX;   // in mm
        public float lensCenterY;   // in mm
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] distortionModel;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
        public float distortion;

        // the following can be computed from the above values and are provided for convenience only
        // the matrices are stored column wise:
        // 0  3  6  9
        // 1  4  7 10
        // 2  5  8 11
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        float extrinsic;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 9)]
        float intrinsic;
    };

    //==========================================
    // internal structures that are more easy to use
    //==========================================
    [Serializable]
    public class CapturySkeletonJoint
    {
        public string name;
        public int parent;
        public Vector3 offset;
        public Vector3 orientation;
        public Transform transform;
    }

    [Serializable]
    public class CapturySkeleton
    {
        public string name;
        public int id;
        public int playerID = -1;
        public CapturySkeletonJoint[] joints;

        private GameObject reference;
        public GameObject mesh // reference to game object that is animated
        {
            get { return reference; }
            set
            {
                reference = value;
                if (reference == null)
                {
                    foreach (CapturySkeletonJoint j in joints)
                        j.transform = null;
                    return;
                }
                foreach (CapturySkeletonJoint j in joints)
                {
                    // check if the joint name matches a reference transform and assign it
                    ArrayList children = reference.transform.GetAllChildren();
                    foreach (Transform tra in children)
                    {
                        if (tra.name.EndsWith(j.name))
                        {
                            j.transform = tra;
                            continue;
                        }
                    }
                }
            }
        }
    }

    [Serializable]
    public class CapturyMarkerTransform
    {
        public Quaternion rotation;
        public Vector3 translation;
        public UInt64 timestamp;
        public float bestAccuracy;
        public bool consumed;
    }

    [Serializable]
    public class ARTag
    {
        public int id;
        public Vector3 translation;
        public Quaternion rotation;
    }

    //====================
    // the network plugin
    //====================
    [RequireComponent(typeof(CapturyOriginManager))]
    public class CapturyNetworkPlugin : MonoBehaviour
    {
        //=============================================
        // import the functions from RemoteCaptury dll
        //=============================================
        [DllImport("RemoteCaptury")]
        private static extern int Captury_connect(string ip, ushort port);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_disconnect();
        [DllImport("RemoteCaptury")]
        private static extern int Captury_getActors(out IntPtr actorData);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_startStreaming(int what);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_stopStreaming();
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getCurrentPose(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freePose(IntPtr pose);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_requestTexture(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getTexture(IntPtr actor);
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeImage(IntPtr image);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_setRotationConstraint(int actorId, int jointIndex, IntPtr rotation, UInt64 timestamp, float weight);
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_getMarkerTransform(IntPtr actor, int jointIndex, IntPtr transform);
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_synchronizeTime();
        [DllImport("RemoteCaptury")]
        private static extern UInt64 Captury_getTime();
        [DllImport("RemoteCaptury")]
        private static extern Int64 Captury_getTimeOffset();
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getLastErrorMessage();
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeErrorMessage(IntPtr msg);
        [DllImport("RemoteCaptury")]
        private static extern int Captury_getCameras(out IntPtr cameras);
        [DllImport("RemoteCaptury")]
        private static extern IntPtr Captury_getCurrentARTags();
        [DllImport("RemoteCaptury")]
        private static extern void Captury_freeARTags(IntPtr arTags);

        // config settings will be overriden by CapturyConfigManager.Config if it can be loaded
        public string host = "127.0.0.1";
        public ushort port = 2101;
        public float scaleFactor = 0.001f; // mm to m
        public int actorCheckTimeout = 500; // in ms
        public bool streamARTags = false;

        // Events
        public delegate void SkeletonDelegate(CapturySkeleton skeleton);
        public event SkeletonDelegate SkeletonFound;
        public event SkeletonDelegate SkeletonLost;
        public delegate void CamerasChangedDelegate(Vector3[] positions, Quaternion[] rotations);
        public event CamerasChangedDelegate CamerasChanged;
        public delegate void ARTagsDetectedDelegate(ARTag[] artags);
        public event ARTagsDetectedDelegate ARTagsDetected;

        public Vector3[] cameraPositions;
        public Quaternion[] cameraOrientations;

        public ARTag[] arTags = new ARTag[0];

        /// <summary>
        /// Reference to <see cref="capturyOriginManager"/> which handles the origin of the coordinate system
        /// </summary>
        private CapturyOriginManager capturyOriginManager;

        /// <summary>
        /// Reference to the current <see cref="CapturyOrigin"/> in the scene which defines the origin of the coordinate system of all avatars
        /// </summary>
        private CapturyOrigin capturyOrigin;

        private static readonly string headJointName = "Head";
        public static string HeadJointName
        {
            get
            {
                return headJointName;
            }
        }

        // threading data for communication with server
        private Thread communicationThread;
        private Mutex communicationMutex = new Mutex();
        private bool communicationFinished = false;

        // internal variables
        private bool isConnected = false;
        private bool isSetup = false;

        // skeleton data from Captury
        private Dictionary<int, IntPtr> actorPointers = new Dictionary<int, IntPtr>();
        private Dictionary<int, int> actorFound = new Dictionary<int, int>();
        private Dictionary<int, CapturySkeleton> skeletons = new Dictionary<int, CapturySkeleton>();
        private Dictionary<int, CapturyMarkerTransform> headTransforms = new Dictionary<int, CapturyMarkerTransform>();
        private Dictionary<string, int> jointsWithConstraints = new Dictionary<string, int>();

        void Awake()
        {
            // load config
            CapturyConfig config = CapturyConfigManager.Config;
            if (config != null)
            {
                host = config.host;
                port = config.port;
                scaleFactor = config.scaleFactor;
                actorCheckTimeout = config.actorCheckTimeout;
                streamARTags = config.streamARTags;
                Debug.LogFormat("CapturyNetworkPlugin settings were overriden by values from {0}.", CapturyConfigManager.configFilePath);
            }

            // register to CapturyOrigin change event
            capturyOriginManager = GetComponent<CapturyOriginManager>();
            capturyOriginManager.CapturyOriginChanged += OnCapturyOriginChanged;
        }

        //=============================
        // this is run once at startup
        //=============================
        void Start()
        {
            // start the connection thread
            communicationThread = new Thread(lookForActors);
            communicationThread.Start();
        }

        //==========================
        // this is run once at exit
        //==========================
        void OnDisable()
        {
            communicationFinished = true;
            communicationThread.Join();
        }

        //============================
        // this is run once per frame
        //============================
        void Update()
        {
            // only perform if we are actually connected
            if (!isConnected)
                return;

            // make sure we lock access before doing anything
            //			Debug.Log ("Starting pose update...");
            communicationMutex.WaitOne();

            // set origin offset based on CapturyOrigin, if existent. Otherwise keep world origin (0,0,0)
            Vector3 offsetToOrigin = Vector3.zero;
            if (capturyOrigin != null)
            {
                offsetToOrigin = capturyOrigin.OffsetToWorldOrigin;
            }

            // fetch current pose for all skeletons
            foreach (KeyValuePair<int, CapturySkeleton> kvp in skeletons)
            {
                // get the actor id
                int actorID = kvp.Key;

                // check if the actor is mapped to something, if not, ignore
                if (skeletons[actorID].mesh == null)
                    continue;

                // get pointer to pose
                IntPtr poseData = Captury_getCurrentPose(actorPointers[actorID]);

                // check if we actually got data, if not, continue
                if (poseData == IntPtr.Zero)
                {
                    // something went wrong, get error message
                    IntPtr msg = Captury_getLastErrorMessage();
                    string errmsg = Marshal.PtrToStringAnsi(msg);
                    Debug.Log("Stream error: " + errmsg);
                    Captury_freeErrorMessage(msg);
                }
                else
                {

                    //Debug.Log("received pose for " + actorID);

                    // convert the pose
                    CapturyPose pose = (CapturyPose)Marshal.PtrToStructure(poseData, typeof(CapturyPose));

                    // get the data into a float array
                    float[] values = new float[pose.numValues * 6];
                    Marshal.Copy(pose.values, values, 0, pose.numValues * 6);

                    // now loop over all joints
                    Vector3 pos = new Vector3();
                    Vector3 rot = new Vector3();

                    for (int jointID = 0; jointID < skeletons[actorID].joints.Length; jointID++)
                    {
                        // ignore any joints that do not map to a transform
                        if (skeletons[actorID].joints[jointID].transform == null)
                            continue;

                        // set offset and rotation
                        int baseIndex = jointID * 6;
                        pos.Set(values[baseIndex + 0], values[baseIndex + 1], values[baseIndex + 2]);
                        rot.Set(values[baseIndex + 3], values[baseIndex + 4], values[baseIndex + 5]);

                        skeletons[actorID].joints[jointID].transform.position = ConvertPosition(pos) + offsetToOrigin;
                        skeletons[actorID].joints[jointID].transform.rotation = ConvertRotation(rot);
                    }

                    // finally, free the pose data again, as we are finished
                    Captury_freePose(poseData);
                }
            }

            // get artags
            IntPtr arTagData = Captury_getCurrentARTags();

            // check if we actually got data, if not, continue
            if (arTagData == IntPtr.Zero)
            {
                // something went wrong, get error message
                IntPtr msg = Captury_getLastErrorMessage();
                string errmsg = Marshal.PtrToStringAnsi(msg);
                Captury_freeErrorMessage(msg);
            }
            else
            {
                IntPtr at = arTagData;
                int num;
                for (num = 0; num < 100; ++num)
                {
                    CapturyARTag arTag = (CapturyARTag)Marshal.PtrToStructure(at, typeof(CapturyARTag));
                    if (arTag.id == -1)
                        break;
                    Array.Resize(ref arTags, num + 1);
                    arTags[num] = new ARTag();
                    arTags[num].id = arTag.id;
                    arTags[num].translation = ConvertPosition(new Vector3(arTag.ox, arTag.oy, arTag.oz)) + offsetToOrigin;
                    arTags[num].rotation = ConvertRotation(Quaternion.LookRotation(new Vector3(arTag.nx, arTag.ny, arTag.nz)).eulerAngles);
                    at = new IntPtr(at.ToInt64() + Marshal.SizeOf(typeof(CapturyARTag)));
                }
                if (num != 0 && ARTagsDetected != null)
                    ARTagsDetected(arTags);
                else
                    Array.Resize(ref arTags, 0);

                Captury_freeARTags(arTagData);
            }

            communicationMutex.ReleaseMutex();
        }

        //================================================
        // This function continously looks for new actors
        // It runs in a separate thread
        //================================================
        void lookForActors()
        {
            while (!communicationFinished)
            {
                // wait for actorCheckTimeout ms before continuing
                //			Debug.Log ("Going to sleep...");
                Thread.Sleep(actorCheckTimeout);
                //			Debug.Log ("Waking up...");

                // now look for new data

{'fraction_non_alphanumeric': 0.10675816798448456, 'fraction_numerical': 0.006146501566462778, 'mean_word_length': 1.8374534371825262, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5605423', 'n_tokens_mistral': 8355, 'n_tokens_neox': 7784, 'n_words': 2340}