Browse Source

-added getVertexArray
-added create3DVertexFrom2D

Alexander Hendrich 11 years ago
parent
commit
646f149cdb

+ 1 - 0
bbiwarg/DataSource/IVideoDataSource.cs

@@ -78,5 +78,6 @@ namespace bbiwarg.DataSource
         Vector[] getFingerTipPositions3D(uint handIndex);
         Vector[] getFingerTipPositions2D(uint handIndex);
         HandSide getHandSide(uint handIndex);
+        VertexArray getVertexArray();
     }
 }

+ 55 - 2
bbiwarg/DataSource/IisuDataSource.cs

@@ -1,8 +1,9 @@
 using System;
+using System.Drawing;
 using System.Runtime.InteropServices;
-
 using Iisu;
 using MathNet.Numerics.LinearAlgebra.Single;
+using bbiwarg.Graphics;
 
 namespace bbiwarg.DataSource
 {
@@ -35,6 +36,8 @@ namespace bbiwarg.DataSource
         private IParameterHandle<float> hfov;
         private IParameterHandle<float> vfov;
 
+        private ImageData currentImage;
+
         /*
          * Creates an Iisu data source.
          * params:
@@ -118,6 +121,7 @@ namespace bbiwarg.DataSource
         public void start()
         {
             device.Start();
+            updateFrame();
         }
 
         public void stop()
@@ -128,6 +132,7 @@ namespace bbiwarg.DataSource
         public void updateFrame()
         {
             device.UpdateFrame(true);
+            createImageData();
         }
 
         public void releaseFrame()
@@ -215,7 +220,11 @@ namespace bbiwarg.DataSource
 
         public ImageData getImageData()
         {
-            return new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
+            return currentImage;
+        }
+
+        private void createImageData() {
+            currentImage = new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
         }
 
         private void checkHandIndex(uint handIndex) 
@@ -337,5 +346,49 @@ namespace bbiwarg.DataSource
                     return HandSide.Unknown;
             }
         }
+
+        public VertexArray getVertexArray()
+        {
+            int width = currentImage.getWidth();
+            int height = currentImage.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 = 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)
+        {
+            int width = currentImage.getWidth();
+            int height = currentImage.getHeight();
+            float alphaX = hfov.Value;
+            float alphaY = vfov.Value;
+            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 = (float)(u * Math.Tan(alphaX / 2f));
+            float relY = (float)(v * Math.Tan(alphaY / 2f));
+
+            float z = (float)(convertedDepth / Math.Sqrt(1 + relX * relX + relY * relY));
+            float x = relX * z;
+            float y = relY * z;
+
+            return new Vertex(x, y, z );
+        }
     }
 }

+ 22 - 0
bbiwarg/DataSource/VertexArray.cs

@@ -0,0 +1,22 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using bbiwarg.Graphics;
+
+namespace bbiwarg.DataSource
+{
+    class VertexArray
+    {
+        private Vertex[] vertices;
+        private Color[] colors;
+
+        public VertexArray(Vertex[] vertices, Color[] colors)
+        {
+            this.vertices = vertices;
+            this.colors = colors;
+        }
+    }
+}

+ 14 - 13
bbiwarg/Graphics/Output.cs

@@ -16,7 +16,6 @@ namespace bbiwarg.Graphics
     {
         private IVideoDataSource source;
         private Point[,] depthPixels = new Point[320,240];
-        private bool initialized = false;
         private ImageData currentImage;
 
         private List<IGraphicElement> graphicElements = new List<IGraphicElement>();
@@ -24,18 +23,24 @@ namespace bbiwarg.Graphics
         private int IBOid = new int();
         private float[] vertices = new float[0];
         private uint[] triangles = new uint[0];
+
+        private float maxDelta = 0;
 
 
         public Output(IVideoDataSource source)
         {
-            this.source = source;
+            this.source = source;
+            currentImage = source.getImageData();
         }
 
         protected override void OnLoad(EventArgs e)
         {
             base.OnLoad(e);
             Title = "OutputTest";
-            GL.ClearColor(Color.Black);
+            GL.ClearColor(Color.Black);
+
+            initializeDepthPixels();
+            initBuffers();
         }
 
         protected override void OnRenderFrame(FrameEventArgs e)
@@ -44,24 +49,18 @@ namespace bbiwarg.Graphics
             GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
             Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, -Vector3.UnitZ, Vector3.UnitY);
             GL.MatrixMode(MatrixMode.Modelview);
-            GL.LoadMatrix(ref modelview);
-
+            GL.LoadMatrix(ref modelview);
+
+            source.releaseFrame();
             source.updateFrame();
             currentImage = source.getImageData();
 
-            if (!initialized) {
-                initializeDepthPixels();
-                initBuffers();
-                initialized = true;
-            }
-
             updateDepthPixels();
 
             updateBuffer();
             drawBuffer();
 
             SwapBuffers();
-            source.releaseFrame();
         }
 
         protected override void OnResize(EventArgs e)
@@ -180,6 +179,8 @@ namespace bbiwarg.Graphics
             result.Add(color.G / 255f);
             result.Add(color.B / 255f);
             return result;
-        }
+        }
+
+        
     }
 }

+ 1 - 1
bbiwarg/Main/OutputTest.cs

@@ -12,7 +12,7 @@ namespace bbiwarg.Main
     {
         static void Main(string[] args)
         {
-            IVideoDataSource source = new IIsuDataSource("..\\..\\videos\\10.skv");
+            IVideoDataSource source = new IIsuDataSource("..\\..\\videos\\2.skv");
             source.init();
             source.start();
 

+ 1 - 0
bbiwarg/bbiwarg.csproj

@@ -63,6 +63,7 @@
     <Compile Include="DataSource\DepthImage.cs" />
     <Compile Include="DataSource\IisuDataSource.cs" />
     <Compile Include="DataSource\IVideoDataSource.cs" />
+    <Compile Include="DataSource\VertexArray.cs" />
     <Compile Include="Graphics\IGraphicElement.cs" />
     <Compile Include="Graphics\Point.cs" />
     <Compile Include="Graphics\Vertex.cs" />