瀏覽代碼

Started PalmDetection.

Daniel Kauth 11 年之前
父節點
當前提交
2e59d990d4

+ 43 - 0
bbiwarg/DataSource/Palm.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
+{
+    class Palm
+    {
+        private Vector upperLeft, upperRight, lowerRight, lowerLeft;
+
+        public Palm(Vector upperLeft, Vector upperRight, Vector lowerRight, Vector lowerLeft)
+        {
+            this.upperLeft = upperLeft;
+            this.upperRight = upperRight;
+            this.lowerRight = lowerRight;
+            this.lowerLeft = lowerLeft;
+        }
+
+        public Vector getUpperLeft()
+        {
+            return upperLeft;
+        }
+
+        public Vector getUpperRight()
+        {
+            return upperRight;
+        }
+
+        public Vector getLowerRight()
+        {
+            return lowerRight;
+        }
+
+        public Vector getLowerLeft()
+        {
+            return lowerLeft;
+        }
+    }
+}

+ 39 - 0
bbiwarg/DataSource/PalmDetection.cs

@@ -0,0 +1,39 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+using MathNet.Numerics.LinearAlgebra.Single;
+
+namespace bbiwarg.DataSource
+{
+    class PalmDetection
+    {
+        private IInputProvider input;
+        private IVideoHandle videoHandle;
+        private ForeFingerDetection foreFingerDetection;
+
+        public PalmDetection(IInputProvider input, IVideoHandle videoHandle)
+        {
+            this.input = input;
+            this.videoHandle = videoHandle;
+            this.foreFingerDetection = new ForeFingerDetection(videoHandle);
+        }
+
+        public Palm getPalm(uint handIndex)
+        {
+            DenseVector palmMiddle = (DenseVector) input.getPalmPosition3D(handIndex);
+            DenseVector thumbPosition = (DenseVector) input.getFingerTipPositions3D(handIndex)[0];
+            DenseVector foreFingerPosition = (DenseVector)foreFingerDetection.getForeFingerPosition3D(handIndex);
+
+            DenseVector palmToThumb_2 = (thumbPosition - palmMiddle) / 2.0f;
+            DenseVector palmToForeFinger = foreFingerPosition - palmMiddle;
+
+            return new Palm(palmMiddle + palmToThumb_2 + palmToForeFinger,
+                            palmMiddle - palmToThumb_2 + palmToForeFinger,
+                            palmMiddle - palmToThumb_2 - palmToForeFinger / 2.0f,
+                            palmMiddle + palmToThumb_2 - palmToForeFinger / 2.0f);
+        }
+    }
+}

+ 15 - 0
bbiwarg/DataSource/VectorExtender.cs

@@ -10,6 +10,21 @@ namespace bbiwarg.DataSource
 {
     public static class VectorExtender
     {
+        public static Vector Cross(this Vector v, Vector other)
+        {
+            if ((v.Count != 3 || other.Count != 3))
+            {
+                string message = "Vectors must have a length of 3.";
+                throw new Exception(message);
+            }
+            Vector result = new DenseVector(3);
+            result[0] = v[1] * other[2] - v[2] * other[1];
+            result[1] = -v[0] * other[2] + v[2] * other[0];
+            result[2] = v[0] * other[1] - v[1] * other[0];
+
+            return result;
+        }
+        
         public static float x(this Vector v)
         {
             return v[0];

+ 59 - 3
bbiwarg/Graphics/Output.cs

@@ -18,14 +18,16 @@ namespace bbiwarg.Graphics
         private IInputProvider inputProvider;
         private IVideoHandle videoHandle;
         private ForeFingerDetection foreFingerDetection;
-        
-        private uint imageBufferId, pointBufferId;
+        private PalmDetection palmDetection;
+
+        private uint imageBufferId, pointBufferId, quadBufferId;
 
         public Output(IInputProvider inputProvider, IVideoHandle videoHandle)
         {
             this.inputProvider = inputProvider;
             this.videoHandle = videoHandle;
             this.foreFingerDetection = new ForeFingerDetection(videoHandle);
+            this.palmDetection = new PalmDetection(inputProvider, videoHandle);
         }
 
         protected override void OnLoad(EventArgs e)
@@ -66,7 +68,7 @@ namespace bbiwarg.Graphics
             GL.VertexPointer(3, VertexPointerType.Float, 3 * sizeof(float) + 4 * sizeof(byte), IntPtr.Zero);
             GL.ColorPointer(4, ColorPointerType.UnsignedByte, 3 * sizeof(float) + 4 * sizeof(byte), 3 * sizeof(float));
 
-            GL.PointSize(3.0f);
+            GL.PointSize(2.0f);
             GL.DrawArrays(PrimitiveType.Points, 0, videoHandle.getWidth() * videoHandle.getHeight());
 
             // draw points
@@ -140,6 +142,59 @@ namespace bbiwarg.Graphics
             GL.PointSize(10f);
             GL.DrawArrays(PrimitiveType.Points, 0, pointData.Length / (4 + 3));
 
+            // draw quads
+            float[] quadData = new float[4 * (4 + 3) * 1];
+
+            Palm palm = palmDetection.getPalm(1);
+            Vector ul = palm.getUpperLeft();
+            Vector ur = palm.getUpperRight();
+            Vector lr = palm.getLowerRight();
+            Vector ll = palm.getLowerLeft();
+
+            quadData[0] = ul.x();
+            quadData[1] = ul.y();
+            quadData[2] = -ul.z();
+
+            quadData[3] = y.R / 255.0f;
+            quadData[4] = y.G / 255.0f;
+            quadData[5] = y.B / 255.0f;
+            quadData[6] = 0.5f;
+
+            quadData[7] = ur.x();
+            quadData[8] = ur.y();
+            quadData[9] = -ur.z();
+
+            quadData[10] = y.R / 255.0f;
+            quadData[11] = y.G / 255.0f;
+            quadData[12] = y.B / 255.0f;
+            quadData[13] = 0.5f;
+
+            quadData[14] = lr.x();
+            quadData[15] = lr.y();
+            quadData[16] = -lr.z();
+
+            quadData[17] = y.R / 255.0f;
+            quadData[18] = y.G / 255.0f;
+            quadData[19] = y.B / 255.0f;
+            quadData[20] = 0.5f;
+
+            quadData[21] = ll.x();
+            quadData[22] = ll.y();
+            quadData[23] = -ll.z();
+
+            quadData[24] = y.R / 255.0f;
+            quadData[25] = y.G / 255.0f;
+            quadData[26] = y.B / 255.0f;
+            quadData[27] = 0.5f;
+
+            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
+            GL.BindBuffer(BufferTarget.ArrayBuffer, quadBufferId);
+            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(quadData.Length * sizeof(float)), quadData, BufferUsageHint.DynamicDraw);
+            GL.VertexPointer(3, VertexPointerType.Float, (4 + 3) * sizeof(float), IntPtr.Zero);
+            GL.ColorPointer(4, ColorPointerType.Float, (4 + 3) * sizeof(float), 3 * sizeof(float));
+
+            GL.DrawArrays(PrimitiveType.Quads, 0, quadData.Length / (4 + 3));
+
             sw.Stop();
             Console.WriteLine(sw.ElapsedMilliseconds);
 
@@ -159,6 +214,7 @@ namespace bbiwarg.Graphics
         {
             GL.GenBuffers(1, out imageBufferId);
             GL.GenBuffers(1, out pointBufferId);
+            GL.GenBuffers(1, out quadBufferId);
 
             GL.BindBuffer(BufferTarget.ArrayBuffer, imageBufferId);
             GL.BufferData(BufferTarget.ArrayBuffer, 

+ 2 - 0
bbiwarg/bbiwarg.csproj

@@ -59,6 +59,8 @@
   <ItemGroup>
     <Compile Include="DataSource\ForeFingerDetection.cs" />
     <Compile Include="DataSource\IInputProvider.cs" />
+    <Compile Include="DataSource\Palm.cs" />
+    <Compile Include="DataSource\PalmDetection.cs" />
     <Compile Include="DataSource\UVImage.cs" />
     <Compile Include="DataSource\ColorImage.cs" />
     <Compile Include="DataSource\ConfidenceImage.cs" />