Sfoglia il codice sorgente

split up interface in inputProvider and videoHandle

Alexander Hendrich 11 anni fa
parent
commit
28d121771e

+ 43 - 0
bbiwarg/DataSource/IInputProvider.cs

@@ -0,0 +1,43 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using MathNet.Numerics.LinearAlgebra.Single;
+
+namespace bbiwarg.DataSource
+{
+    interface IInputProvider
+    {
+        void init();
+        void start();
+        void stop();
+        void updateFrame();
+        void releaseFrame();
+        float getFrameRate();
+        float getHFOV();
+        float getVFOV();
+        bool isActive();
+
+        DepthImage getDepthImage();
+        ConfidenceImage getConfidenceImage();
+        UVImage getUVImage();
+        ColorImage getColorImage();
+
+        /*
+         * all handIndices have to be 1 or 2
+         */
+        bool isHandOpen(uint handIndex);
+        Vector getPalmPosition3D(uint handIndex);
+        Vector getPalmPosition2D(uint handIndex);
+        Vector getTipPosition3D(uint handIndex);
+        Vector getForearmPosition3D(uint handIndex);
+        Vector getPalmNormal3D(uint handIndex);
+        DetectionStatus[] getFingerStatus(uint handIndex);
+        DetectionStatus getHandStatus(uint handIndex);
+        Vector[] getFingerTipPositions3D(uint handIndex);
+        Vector[] getFingerTipPositions2D(uint handIndex);
+        HandSide getHandSide(uint handIndex);
+    }
+}

+ 0 - 83
bbiwarg/DataSource/IVideoDataSource.cs

@@ -1,83 +0,0 @@
-using System;
-
-using MathNet.Numerics.LinearAlgebra.Single;
-
-namespace bbiwarg.DataSource
-{
-    public enum DetectionStatus
-    {
-        Inactive,
-        Detected,
-        Tracked
-    }
-
-    public enum HandSide
-    {
-        Unknown,
-        Left,
-        Right
-    }
-
-    /*
-     * Interface get data from a camera or a video file.
-     * Completely independent of iisu.
-     */
-    interface IVideoDataSource
-    {
-        /*
-         * Initializes the data source.
-         */
-        void init();
-
-        /*
-         * Starts the recording of data.
-         */
-        void start();
-        /*
-         * Stops the recording of data.
-         */
-        void stop();
-
-        /*
-         * Updates the data for the current frame.
-         * Needs to be called before any method to read data.
-         */
-        void updateFrame();
-        /*
-         * Lets the data source process the next frame.
-         */
-        void releaseFrame();
-
-        /*
-         * Returns true iff new data is generated.
-         * (e.g camera is running or video hasn't ended)
-         */
-        bool isActive();
-        int getFrameRate();
-        float getHFOV();
-        float getVFOV();
-        /**
-         * The depth is given by the distance to the camera in millimeters. 
-         */
-        DepthImage getDepthImage();
-        ColorImage getColorImage();
-        ConfidenceImage getConfidenceImage();
-        UVImage getUVImage();
-        ImageData getImageData();
-        /*
-         * all handIndices have to be 1 or 2
-         */
-        bool isHandOpen(uint handIndex);
-        Vector getPalmPosition3D(uint handIndex);
-        Vector getPalmPosition2D(uint handIndex);
-        Vector getTipPosition3D(uint handIndex);
-        Vector getForearmPosition3D(uint handIndex);
-        Vector getPalmNormal3D(uint handIndex);
-        DetectionStatus[] getFingerStatus(uint handIndex);
-        DetectionStatus getHandStatus(uint handIndex);
-        Vector[] getFingerTipPositions3D(uint handIndex);
-        Vector[] getFingerTipPositions2D(uint handIndex);
-        HandSide getHandSide(uint handIndex);
-        VertexArray getVertexArray();
-    }
-}

+ 36 - 0
bbiwarg/DataSource/IVideoHandle.cs

@@ -0,0 +1,36 @@
+using System;
+using System.Drawing;
+using MathNet.Numerics.LinearAlgebra.Single;
+
+using MathNet.Numerics.LinearAlgebra.Single;
+
+namespace bbiwarg.DataSource
+{
+    public enum DetectionStatus
+    {
+        Inactive,
+        Detected,
+        Tracked
+    }
+
+    public enum HandSide
+    {
+        Unknown,
+        Left,
+        Right
+    }
+
+    interface IVideoHandle
+    {
+        void nextFrame();
+
+        short getDepth(int x, int y);
+        short getConfidence(int x, int y);
+        Color getColor(int x, int y);
+
+        VertexArray getVertexArray();
+        //convert2Dto3D(float x, float y, int depth)
+
+        Vector getPalmPosition3D(uint handIndex);
+    }
+}

+ 361 - 399
bbiwarg/DataSource/IisuDataSource.cs → bbiwarg/DataSource/IisuInputProvider.cs

@@ -1,399 +1,361 @@
-using System;
-using System.Drawing;
-using System.Diagnostics;
-using System.Runtime.InteropServices;
-using Iisu;
-using MathNet.Numerics.LinearAlgebra.Single;
-using bbiwarg.Graphics;
-
-namespace bbiwarg.DataSource
-{
-    class IIsuDataSource: IVideoDataSource
-    {
-        private IHandle handle;
-        private IDevice device;
-        private string moviePath;
-        bool active;
-
-        // parameters
-        private IParameterHandle<float> frameRate;
-
-        // data
-        private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
-        private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
-        private IDataHandle<Iisu.Data.Vector2>[] palmPositions2D = new IDataHandle<Iisu.Data.Vector2>[2];
-        private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
-        private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
-        private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
-        private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
-        private IDataHandle<int>[] handStatus = new IDataHandle<int>[2];
-        private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
-        private IDataHandle<Iisu.Data.Vector2[]>[] fingerTipPositions2D = new IDataHandle<Iisu.Data.Vector2[]>[2];
-        private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
-        private IDataHandle<Iisu.Data.IImageData> depthImage;
-        private IDataHandle<Iisu.Data.IImageData> colorImage;
-        private IDataHandle<Iisu.Data.IImageData> confidenceImage;
-        private IDataHandle<Iisu.Data.IImageData> uvImage;
-        private IParameterHandle<float> hfov;
-        private IParameterHandle<float> vfov;
-
-        private ImageData currentImage;
-        private float maxU;
-        private float maxV;
-        private int width;
-        private int height;
-
-        /*
-         * Creates an Iisu data source.
-         * params:
-         *  moviePath: path to movie to be used as source
-         *             if empty the camera is used
-         */
-        public IIsuDataSource(string moviePath = "")
-        {
-            this.moviePath = moviePath;
-            active = false;
-        }
-
-        public void init()
-        {
-            handle = Iisu.Iisu.Context.CreateHandle();
-            
-            IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
-            if (moviePath.Length != 0)
-                conf.MoviePath = moviePath;
-
-            device = handle.InitializeDevice(conf);
-
-            // parameters
-            if (moviePath.Length != 0)
-                device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
-            else
-                device.RegisterParameterHandle<int>("SOURCE.DEPTHSENSE.AmplitudeThreshold").Value = 100; // confidence-threshhold
-
-            frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
-            hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
-            vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
-            maxU = (float)Math.Tan(hfov.Value / 2f);
-            maxV = (float)Math.Tan(vfov.Value / 2f);
-
-            // events
-            device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
-            
-            // data
-            depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
-            colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
-            confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
-            uvImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.REGISTRATION.UV.Image");
-
-            handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
-            handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
-
-            palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
-            palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
-
-            palmPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND1.PalmPosition2D");
-            palmPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND2.PalmPosition2D");
-
-            tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
-            tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
-
-            forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
-            forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
-
-            palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
-            palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
-
-            fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
-            fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
-
-            handStatus[0] = device.RegisterDataHandle<int>("CI.HAND1.Status");
-            handStatus[1] = device.RegisterDataHandle<int>("CI.HAND2.Status");
-
-            fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
-            fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
-            
-            fingerTipPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND1.FingerTipPositions2D");
-            fingerTipPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND2.FingerTipPositions2D");
-
-            handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
-            handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
-        }
-
-        private void onDeviceStatusChanged(string eventName, DeviceStatus status)
-        {
-            active = status.HasFlag(Iisu.DeviceStatus.Playing);
-        }
-
-        public void start()
-        {
-            device.Start();
-            updateFrame();
-        }
-
-        public void stop()
-        {
-            device.Stop(true);
-        }
-
-        public void updateFrame()
-        {
-            device.UpdateFrame(true);
-            createImageData();
-        }
-
-        public void releaseFrame()
-        {
-            device.ReleaseFrame();
-        }
-
-        public bool isActive()
-        {
-            return active;
-        }
-
-        public int getFrameRate()
-        {
-            return (int) frameRate.Value;
-        }
-
-        public float getHFOV() {
-            return hfov.Value;
-        }
-
-        public float getVFOV() {
-            return vfov.Value;
-        }
-
-        public DepthImage getDepthImage()
-        {
-            Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
-            int width = (int) imageInfos.Width;
-            int height = (int) imageInfos.Height;
-            int numBytes = (int) imageInfos.BytesRaw;
-
-            IntPtr imageData = depthImage.Value.Raw;
-            
-            short[] depthData = new short[width * height];
-            Marshal.Copy(imageData, depthData, 0, width * height);
-
-            return new DepthImage(width, height, depthData);
-        }
-
-        public ColorImage getColorImage()
-        {
-            Iisu.Data.IImageInfos imageInfos = colorImage.Value.ImageInfos;
-            int width = (int)imageInfos.Width;
-            int height = (int)imageInfos.Height;
-            int numBytes = (int)imageInfos.BytesRaw;
-
-            IntPtr imageData = colorImage.Value.Raw;
-
-            byte[] colorData = new byte[numBytes];
-            Marshal.Copy(imageData, colorData, 0, numBytes);
-
-            return new ColorImage(width, height, colorData);
-        }
-
-        public ConfidenceImage getConfidenceImage()
-        {
-            Iisu.Data.IImageInfos imageInfos = confidenceImage.Value.ImageInfos;
-            int width = (int)imageInfos.Width;
-            int height = (int)imageInfos.Height;
-            int numBytes = (int)imageInfos.BytesRaw;
-
-            IntPtr imageData = confidenceImage.Value.Raw;
-
-            short[] confidenceData = new short[width * height];
-            Marshal.Copy(imageData, confidenceData, 0, width * height);
-
-            return new ConfidenceImage(width, height, confidenceData);
-        }
-
-        public UVImage getUVImage()
-        {
-            Iisu.Data.IImageInfos imageInfos = uvImage.Value.ImageInfos;
-            int width = (int)imageInfos.Width;
-            int height = (int)imageInfos.Height;
-            int numBytes = (int)imageInfos.BytesRaw;
-
-            IntPtr imageData = uvImage.Value.Raw;
-
-            float[] uvData = new float[2 * width * height];
-            Marshal.Copy(imageData, uvData, 0, 2 * width * height);
-
-            return new UVImage(width, height, uvData);
-        }
-
-        public ImageData getImageData()
-        {
-            return currentImage;
-        }
-
-        private void createImageData() {
-            currentImage = new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
-            width = currentImage.getWidth();
-            height = currentImage.getHeight();
-        }
-
-        private void checkHandIndex(uint handIndex) 
-        {
-            if (handIndex < 1 || handIndex > 2)
-                throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
-        }
-
-        public bool isHandOpen(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return handOpen[handIndex - 1].Value;
-        }
-
-        public Vector getPalmPosition3D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
-        }
-
-        public Vector getPalmPosition2D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return new DenseVector(palmPositions2D[handIndex - 1].Value.ToArray());
-        }
-
-        public Vector getTipPosition3D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
-        }
-
-        public Vector getForearmPosition3D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
-        }
-
-        public Vector getPalmNormal3D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
-        }
-
-        public DetectionStatus[] getFingerStatus(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            int[] status = fingerStatus[handIndex - 1].Value;
-            DetectionStatus[] result = new DetectionStatus[status.Length];
-            for (int i = 0; i < status.Length; ++i)
-            {
-                switch (status[i])
-                {
-                    case 0:
-                        result[i] = DetectionStatus.Inactive;
-                        break;
-                    case 1:
-                        result[i] = DetectionStatus.Detected;
-                        break;
-                    case 2:
-                        result[i] = DetectionStatus.Tracked;
-                        break;
-                }
-            }
-            return result;
-        }
-
-        public DetectionStatus getHandStatus(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            int status = handStatus[handIndex - 1].Value;
-            DetectionStatus result = new DetectionStatus();
-            switch (status)
-            {
-                case 0:
-                    result = DetectionStatus.Inactive;
-                    break;
-                case 1:
-                    result = DetectionStatus.Detected;
-                    break;
-                case 2:
-                    result = DetectionStatus.Tracked;
-                    break;
-            }
-            return result;
-        }
-
-        public Vector[] getFingerTipPositions3D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
-            Vector[] results = new DenseVector[positions.Length];
-            for (int i = 0; i < positions.Length; ++i)
-                results[i] = new DenseVector(positions[i].ToArray());
-            return results;
-        }
-
-        public Vector[] getFingerTipPositions2D(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            Iisu.Data.Vector2[] positions = fingerTipPositions2D[handIndex - 1].Value;
-            Vector[] results = new DenseVector[positions.Length];
-            for (int i = 0; i < positions.Length; ++i)
-                results[i] = new DenseVector(positions[i].ToArray());
-            return results;
-        }
-
-        public HandSide getHandSide(uint handIndex)
-        {
-            checkHandIndex(handIndex);
-            int side = handSides[handIndex - 1].Value;
-            switch (side) 
-            {
-                case 1:
-                    return HandSide.Left;
-                case 2:
-                    return HandSide.Right;
-                default:
-                    return HandSide.Unknown;
-            }
-        }
-
-        public VertexArray getVertexArray()
-        {
-            Vertex[] vertices = new Vertex[width * height];
-            Color[] colors = new Color[width * height];
-
-            int index = 0;
-            for (int x = 0; x < width; x++)
-            {
-                for (int y = 0; y < height; y++)
-                {
-                    int depth = currentImage.getDepth(x, y);
-                    vertices[index] = create3DVertexFrom2D(x, y, depth);
-                    colors[index] = currentImage.getColor(x, y);
-
-                    index++;
-                }
-
-
-            }
-
-            return new VertexArray(vertices, colors);
-        }
-
-        private Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
-        {
-            float convertedDepth = depth / 1000f; // mm into m
-
-            float u = (pixelX / (float)width - 0.5f) * 2f;
-            float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
-
-            float relX = (u * maxU);
-            float relY = (v * maxV);
-
-            float z = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
-            float x = relX * z;
-            float y = relY * z;
-
-            return new Vertex(x, y, z);
-        }
-    }
-}
+using System;
+using System.Drawing;
+using System.Diagnostics;
+using System.Runtime.InteropServices;
+using Iisu;
+using MathNet.Numerics.LinearAlgebra.Single;
+using bbiwarg.Graphics;
+
+namespace bbiwarg.DataSource
+{
+    class IisuInputProvider : IInputProvider
+    {
+        private IHandle handle;
+        private IDevice device;
+        private string moviePath;
+        private bool active;
+        private bool sourceIsMovie;
+
+        //parameters
+        private IParameterHandle<float> frameRate;
+        private IParameterHandle<float> hfov;
+        private IParameterHandle<float> vfov;
+
+        //data
+        private IDataHandle<int>[] handStatus = new IDataHandle<int>[2];
+        private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
+        private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
+        private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
+        private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
+        private IDataHandle<Iisu.Data.Vector2>[] palmPositions2D = new IDataHandle<Iisu.Data.Vector2>[2];
+        private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
+        private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
+        private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
+        private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
+        private IDataHandle<Iisu.Data.Vector2[]>[] fingerTipPositions2D = new IDataHandle<Iisu.Data.Vector2[]>[2];
+        private IDataHandle<Iisu.Data.IImageData> depthImage;
+        private IDataHandle<Iisu.Data.IImageData> confidenceImage;
+        private IDataHandle<Iisu.Data.IImageData> uvImage;
+        private IDataHandle<Iisu.Data.IImageData> colorImage;
+
+        public IisuInputProvider(String moviePath = "") 
+        {
+            if (moviePath.Length != 0)
+            {
+                this.moviePath = moviePath;
+                sourceIsMovie = true;
+            }
+            else
+            {
+                sourceIsMovie = false;
+            }
+            active = false;
+        }
+
+        // control-methodes
+
+        public void init()
+        {
+            handle = Iisu.Iisu.Context.CreateHandle();
+
+            IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
+            if (sourceIsMovie)
+                conf.MoviePath = moviePath;
+
+            device = handle.InitializeDevice(conf);
+
+            // parameters
+            if (sourceIsMovie)
+                device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
+            else
+                device.RegisterParameterHandle<int>("SOURCE.DEPTHSENSE.AmplitudeThreshold").Value = 100; // confidence-threshhold
+
+            frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
+            hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
+            vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
+
+            // events
+            device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
+
+            // data
+            handStatus[0] = device.RegisterDataHandle<int>("CI.HAND1.Status");
+            handStatus[1] = device.RegisterDataHandle<int>("CI.HAND2.Status");
+
+            handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
+            handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
+
+            handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
+            handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
+            
+            fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
+            fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
+
+            palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
+            palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
+
+            palmPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND1.PalmPosition2D");
+            palmPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND2.PalmPosition2D");
+
+            tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
+            tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
+
+            forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
+            forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
+
+            palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
+            palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
+
+            fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
+            fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
+
+            fingerTipPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND1.FingerTipPositions2D");
+            fingerTipPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND2.FingerTipPositions2D");
+            
+            depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
+            colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
+            confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
+            uvImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.REGISTRATION.UV.Image");
+        }
+
+        private void onDeviceStatusChanged(string eventName, DeviceStatus status)
+        {
+            active = status.HasFlag(Iisu.DeviceStatus.Playing);
+        }
+
+        public void start()
+        {
+            device.Start();
+        }
+
+        public void stop()
+        {
+            device.Stop(true);
+        }
+
+        public void updateFrame()
+        {
+            device.UpdateFrame(true);
+        }
+
+        public void releaseFrame()
+        {
+            device.ReleaseFrame();
+        }
+
+        // getParameter-methodes
+
+        public float getFrameRate()
+        {
+            return frameRate.Value;
+        }
+
+        public float getHFOV()
+        {
+            return hfov.Value;
+        }
+
+        public float getVFOV()
+        {
+            return vfov.Value;
+        }
+
+        // getData-methodes
+
+        public bool isActive()
+        {
+            return active;
+        }
+
+        public DetectionStatus getHandStatus(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            int status = handStatus[handIndex - 1].Value;
+            DetectionStatus result = new DetectionStatus();
+            switch (status)
+            {
+                case 0:
+                    result = DetectionStatus.Inactive;
+                    break;
+                case 1:
+                    result = DetectionStatus.Detected;
+                    break;
+                case 2:
+                    result = DetectionStatus.Tracked;
+                    break;
+            }
+            return result;
+        }
+
+        public bool isHandOpen(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return handOpen[handIndex - 1].Value;
+        }
+
+        public HandSide getHandSide(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            int side = handSides[handIndex - 1].Value;
+            switch (side)
+            {
+                case 1:
+                    return HandSide.Left;
+                case 2:
+                    return HandSide.Right;
+                default:
+                    return HandSide.Unknown;
+            }
+        }
+
+        public DetectionStatus[] getFingerStatus(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            int[] status = fingerStatus[handIndex - 1].Value;
+            DetectionStatus[] result = new DetectionStatus[status.Length];
+            for (int i = 0; i < status.Length; ++i)
+            {
+                switch (status[i])
+                {
+                    case 0:
+                        result[i] = DetectionStatus.Inactive;
+                        break;
+                    case 1:
+                        result[i] = DetectionStatus.Detected;
+                        break;
+                    case 2:
+                        result[i] = DetectionStatus.Tracked;
+                        break;
+                }
+            }
+            return result;
+        }
+
+        public Vector getPalmPosition3D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return new DenseVector(swapXZYtoXYZ(palmPositions3D[handIndex - 1].Value.ToArray()));
+        }
+
+        public Vector getPalmPosition2D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return new DenseVector(palmPositions2D[handIndex - 1].Value.ToArray());
+        }
+
+        public Vector getTipPosition3D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return new DenseVector(swapXZYtoXYZ(tipPositions3D[handIndex - 1].Value.ToArray()));
+        }
+
+        public Vector getForearmPosition3D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return new DenseVector(swapXZYtoXYZ(forearmPositions3D[handIndex - 1].Value.ToArray()));
+        }
+
+        public Vector getPalmNormal3D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            return new DenseVector(swapXZYtoXYZ(palmNormals3D[handIndex - 1].Value.ToArray()));
+        }
+
+        public Vector[] getFingerTipPositions3D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
+            Vector[] results = new DenseVector[positions.Length];
+            for (int i = 0; i < positions.Length; ++i)
+                results[i] = new DenseVector(swapXZYtoXYZ(positions[i].ToArray()));
+            return results;
+        }
+
+        public Vector[] getFingerTipPositions2D(uint handIndex)
+        {
+            checkHandIndex(handIndex);
+            Iisu.Data.Vector2[] positions = fingerTipPositions2D[handIndex - 1].Value;
+            Vector[] results = new DenseVector[positions.Length];
+            for (int i = 0; i < positions.Length; ++i)
+                results[i] = new DenseVector(positions[i].ToArray());
+            return results;
+        }
+
+
+        public DepthImage getDepthImage()
+        {
+            Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
+            int width = (int)imageInfos.Width;
+            int height = (int)imageInfos.Height;
+            int numBytes = (int)imageInfos.BytesRaw;
+
+            IntPtr imageData = depthImage.Value.Raw;
+
+            short[] depthData = new short[width * height];
+            Marshal.Copy(imageData, depthData, 0, width * height);
+
+            return new DepthImage(width, height, depthData);
+        }
+
+        public ConfidenceImage getConfidenceImage()
+        {
+            Iisu.Data.IImageInfos imageInfos = confidenceImage.Value.ImageInfos;
+            int width = (int)imageInfos.Width;
+            int height = (int)imageInfos.Height;
+            int numBytes = (int)imageInfos.BytesRaw;
+
+            IntPtr imageData = confidenceImage.Value.Raw;
+
+            short[] confidenceData = new short[width * height];
+            Marshal.Copy(imageData, confidenceData, 0, width * height);
+
+            return new ConfidenceImage(width, height, confidenceData);
+        }
+
+        public UVImage getUVImage()
+        {
+            Iisu.Data.IImageInfos imageInfos = uvImage.Value.ImageInfos;
+            int width = (int)imageInfos.Width;
+            int height = (int)imageInfos.Height;
+            int numBytes = (int)imageInfos.BytesRaw;
+
+            IntPtr imageData = uvImage.Value.Raw;
+
+            float[] uvData = new float[2 * width * height];
+            Marshal.Copy(imageData, uvData, 0, 2 * width * height);
+
+            return new UVImage(width, height, uvData);
+        }
+
+        public ColorImage getColorImage()
+        {
+            Iisu.Data.IImageInfos imageInfos = colorImage.Value.ImageInfos;
+            int width = (int)imageInfos.Width;
+            int height = (int)imageInfos.Height;
+            int numBytes = (int)imageInfos.BytesRaw;
+
+            IntPtr imageData = colorImage.Value.Raw;
+
+            byte[] colorData = new byte[numBytes];
+            Marshal.Copy(imageData, colorData, 0, numBytes);
+
+            return new ColorImage(width, height, colorData);
+        }
+
+        // other
+
+        private void checkHandIndex(uint handIndex)
+        {
+            if (handIndex < 1 || handIndex > 2)
+                throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
+        }
+
+        private float[] swapXZYtoXYZ(float[] vector) 
+        {
+            float dummy = vector[1];
+            vector[1] = vector[2];
+            vector[2] = dummy;
+            return vector;
+        }
+
+    }
+}

+ 0 - 64
bbiwarg/DataSource/ImageData.cs

@@ -1,64 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Drawing;
-
-namespace bbiwarg.DataSource
-{
-    /**
-     * Gives access to all important data in depth image coordinates.
-     */
-    class ImageData
-    {
-        private DepthImage depthImage;
-        private ConfidenceImage confidenceImage;
-        private ColorImage colorImage;
-        private UVImage uvImage;
-
-        public ImageData(DepthImage depthImage, ConfidenceImage confidenceImage, ColorImage colorImage, UVImage uvImage)
-        {
-            this.depthImage = depthImage;
-            this.confidenceImage = confidenceImage;
-            this.colorImage = colorImage;
-            this.uvImage = uvImage;
-        }
-
-        public int getWidth()
-        {
-            return depthImage.getWidth();
-        }
-
-        public int getHeight()
-        {
-            return depthImage.getHeight();
-        }
-
-        public short getDepth(int x, int y)
-        {
-            return depthImage.getDepth(x, y);
-        }
-
-        public short getConfidence(int x, int y)
-        {
-            return confidenceImage.getConfidence(x, y);
-        }
-
-        public Color getColor(int x, int y)
-        {
-            float u = uvImage.getU(x, y);
-            float v = uvImage.getV(x, y);
-
-            if (u < 0 || v < 0)
-                return Color.Black;
-
-            int colorImageWidth = colorImage.getWidth();
-            int colorImageHeight = colorImage.getHeight();
-
-            int xInColorImage = (int) (u * colorImageWidth) % colorImageWidth;
-            int yInColorImage = (int) (v * colorImageHeight) % colorImageHeight;
-            return colorImage.getColor(xInColorImage, yInColorImage);
-        }
-    }
-}

+ 134 - 0
bbiwarg/DataSource/VideoHandle.cs

@@ -0,0 +1,134 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MathNet.Numerics.LinearAlgebra.Single;
+using bbiwarg.Graphics;
+
+namespace bbiwarg.DataSource
+{
+    class VideoHandle : IVideoHandle
+    {
+        private IInputProvider inputProvider;
+        private DepthImage depthImage;
+        private ConfidenceImage confidenceImage;
+        private UVImage uvImage;
+        private ColorImage colorImage;
+
+        private float maxU;
+        private float maxV;
+
+        public VideoHandle(IInputProvider inputProvider) {
+            this.inputProvider = inputProvider;
+            inputProvider.init();
+            inputProvider.start();
+            inputProvider.updateFrame();
+            createImageData();
+
+            maxU = (float)(Math.Tan(inputProvider.getHFOV() / 2f));
+            maxV = (float)(Math.Tan(inputProvider.getVFOV() / 2f));
+        }
+
+        ~VideoHandle() 
+        {
+            inputProvider.stop();
+        }
+
+        public void nextFrame() 
+        {
+            inputProvider.releaseFrame();
+            inputProvider.updateFrame();
+            createImageData();
+        }
+
+        private void createImageData() 
+        {
+            depthImage = inputProvider.getDepthImage();
+            confidenceImage = inputProvider.getConfidenceImage();
+            uvImage = inputProvider.getUVImage();
+            colorImage = inputProvider.getColorImage();
+        }
+
+        public short getDepth(int x, int y)
+        {
+            return depthImage.getDepth(x, y);
+        }
+
+        public short getConfidence(int x, int y)
+        {
+            return confidenceImage.getConfidence(x, y);
+        }
+
+        public Color getColor(int x, int y)
+        {
+            float u = uvImage.getU(x, y);
+            float v = uvImage.getV(x, y);
+
+            if (u < 0 || v < 0)
+                return Color.Black;
+
+            int colorImageWidth = colorImage.getWidth();
+            int colorImageHeight = colorImage.getHeight();
+
+            int xInColorImage = (int)(u * colorImageWidth) % colorImageWidth;
+            int yInColorImage = (int)(v * colorImageHeight) % colorImageHeight;
+            return colorImage.getColor(xInColorImage, yInColorImage);
+        }
+
+        public Vector getPalmPosition3D(uint handIndex)
+        {
+            //improved palm recognition or just return the value provided by inputprovider
+            return inputProvider.getPalmPosition3D(handIndex);
+        }
+
+        public VertexArray getVertexArray()
+        {
+            int width = depthImage.getWidth();
+            int height = depthImage.getHeight();
+
+            Vertex[] vertices = new Vertex[width * height];
+            Color[] colors = new Color[width * height];
+
+            int index = 0;
+            for (int x = 0; x < width; x++)
+            {
+                for (int y = 0; y < height; y++)
+                {
+                    int depth = depthImage.getDepth(x, y);
+                    vertices[index] = create3DVertexFrom2D(x, y, depth);
+                    colors[index] = getColor(x, y);
+
+                    index++;
+                }
+
+
+            }
+
+            return new VertexArray(vertices, colors);
+        }
+
+        public Vertex create3DVertexFrom2D(float pixelX, float pixelY, int depth)
+        {
+            int width = depthImage.getWidth();
+            int height = depthImage.getHeight();
+
+            float convertedDepth = depth / 1000f; // mm into m
+
+            float u = (pixelX / (float)width - 0.5f) * 2f;
+            float v = ((1 - pixelY / (float)height) - 0.5f) * 2f;
+
+            float relX = (u * maxU);
+            float relY = (v * maxV);
+
+            float z = convertedDepth / (float)Math.Sqrt(1 + relX * relX + relY * relY);
+            float x = relX * z;
+            float y = relY * z;
+
+            return new Vertex(x, y, z);
+        }
+        
+        
+    }
+}

+ 8 - 41
bbiwarg/Graphics/Output.cs

@@ -15,9 +15,8 @@ namespace bbiwarg.Graphics
 {
     class Output : GameWindow
     {
-        private IVideoDataSource source;
+        private IVideoHandle source;
         private Point[] depthPixels;
-        private ImageData currentImage;
 
         private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
         private int VBOid = new int();
@@ -25,14 +24,11 @@ namespace bbiwarg.Graphics
         private float[] vertices = new float[0];
         private uint[] triangles = new uint[0];
 
-        private Point palmPoint;
-        private Point[] fingerPoints;
+        private Point palmPoint;
 
-
-        public Output(IVideoDataSource source)
+        public Output(IVideoHandle source)
         {
-            this.source = source;
-            currentImage = source.getImageData();
+            this.source = source;
         }
 
         protected override void OnLoad(EventArgs e)
@@ -53,9 +49,7 @@ namespace bbiwarg.Graphics
             GL.MatrixMode(MatrixMode.Modelview);
             GL.LoadMatrix(ref modelview);
 
-            source.releaseFrame();
-            source.updateFrame();
-            currentImage = source.getImageData();
+            source.nextFrame();
 
             updateDepthPixels();
 
@@ -94,15 +88,9 @@ namespace bbiwarg.Graphics
             }
 
             Vector palmPosition = source.getPalmPosition3D(1);
-            Vertex palmVertex = new Vertex(palmPosition[0], palmPosition[2], palmPosition[1]);
+            Vertex palmVertex = new Vertex(palmPosition[0], palmPosition[1], palmPosition[2]);
             palmPoint = new Point(palmVertex, Color.Yellow, 0.005f);
             graphicElements.Add(palmPoint);
-
-            fingerPoints = new Point[5];
-            for (int i = 0; i < 5; i++)
-            {
-                fingerPoints[i] = new Point(new Vertex(0f, 0f, 0f), Color.Yellow, 0.005f);
-            }
         }
 
         private void updateDepthPixels()
@@ -117,30 +105,9 @@ namespace bbiwarg.Graphics
             }
             Vector palmPosition = source.getPalmPosition3D(1);
             palmPoint.position.x = palmPosition[0];
-            palmPoint.position.y = palmPosition[2];
-            palmPoint.position.z = palmPosition[1];
-
-            
-            Vector[] fingerPositions = source.getFingerTipPositions3D(1);
-            DetectionStatus[] fingerStatus = source.getFingerStatus(1);
-            for(int i=0; i<5; i++) {
-                if(fingerStatus[i] == DetectionStatus.Tracked) {
-                   fingerPoints[i].position.x = fingerPositions[i][0];
-                   fingerPoints[i].position.y = fingerPositions[i][2];
-                   fingerPoints[i].position.z = fingerPositions[i][1];
-                } 
-                else if(fingerStatus[i] == DetectionStatus.Detected) {
-                    graphicElements.Add(fingerPoints[i]);
-                    fingerPoints[i].position.x = fingerPositions[i][0];
-                    fingerPoints[i].position.y = fingerPositions[i][2];
-                    fingerPoints[i].position.z = fingerPositions[i][1];
-                }
-                else if(fingerStatus[i] == DetectionStatus.Inactive && graphicElements.IndexOf(fingerPoints[i]) > -1) {
-                    graphicElements.Remove(fingerPoints[i]);
-                }
-            }
+            palmPoint.position.y = palmPosition[1];
+            palmPoint.position.z = palmPosition[2];
 
-            
         }
 
         private void initBuffers()

+ 1 - 5
bbiwarg/Main/OutputTest.cs

@@ -12,14 +12,10 @@ namespace bbiwarg.Main
     {
         static void Main(string[] args)
         {
-            IVideoDataSource source = new IIsuDataSource("..\\..\\videos\\6.skv");
-            source.init();
-            source.start();
+            IVideoHandle source = new VideoHandle(new IisuInputProvider("..\\..\\videos\\6.skv"));
 
             Output output = new Output(source);
             output.Run(30);
-
-            source.stop();
         }
     }
 }

+ 4 - 3
bbiwarg/bbiwarg.csproj

@@ -56,14 +56,15 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="DataSource\ImageData.cs" />
+    <Compile Include="DataSource\IInputProvider.cs" />
     <Compile Include="DataSource\UVImage.cs" />
     <Compile Include="DataSource\ColorImage.cs" />
     <Compile Include="DataSource\ConfidenceImage.cs" />
     <Compile Include="DataSource\DepthImage.cs" />
-    <Compile Include="DataSource\IisuDataSource.cs" />
-    <Compile Include="DataSource\IVideoDataSource.cs" />
+    <Compile Include="DataSource\IisuInputProvider.cs" />
+    <Compile Include="DataSource\IVideoHandle.cs" />
     <Compile Include="DataSource\VertexArray.cs" />
+    <Compile Include="DataSource\VideoHandle.cs" />
     <Compile Include="Graphics\Rectangle.cs" />
     <Compile Include="Graphics\IGraphicElement.cs" />
     <Compile Include="Graphics\Point.cs" />