Преглед изворни кода

cleanup Vector2D (now has IntX and IntY)

Alexander Hendrich пре 10 година
родитељ
комит
7b1dbdfdaf

+ 3 - 3
bbiwarg/Detectors/Fingers/FingerDetector.cs

@@ -178,9 +178,9 @@ namespace bbiwarg.Detectors.Fingers
 
         private bool fingerSliceDepthTest(FingerSlice fingerSlice)
         {
-            Int16 depthStart = depthImage.getDepthAt((int)fingerSlice.Start.X, (int)fingerSlice.Start.Y);
-            Int16 depthMid = depthImage.getDepthAt((int)fingerSlice.Mid.X, (int)fingerSlice.Mid.Y);
-            Int16 depthEnd = depthImage.getDepthAt((int)fingerSlice.End.X, (int)fingerSlice.End.Y);
+            Int16 depthStart = depthImage.getDepthAt(fingerSlice.Start.IntX, fingerSlice.Start.IntY);
+            Int16 depthMid = depthImage.getDepthAt(fingerSlice.Mid.IntX, fingerSlice.Mid.IntY);
+            Int16 depthEnd = depthImage.getDepthAt(fingerSlice.End.IntX, fingerSlice.End.IntY);
             return (depthStart > depthMid && depthMid < depthEnd);
         }
 

+ 1 - 1
bbiwarg/Detectors/Palm/PalmDetector.cs

@@ -129,7 +129,7 @@ namespace bbiwarg.Detectors.Palm
                 return new Point(0, 0);
             Vector2D direction = (finger.Hand - finger.Tip).normalize();
             Vector2D pos = finger.Hand + 20 * direction;
-            return new Point(HelperFunctions.thresholdRange<int>(0, width - 1, (int)pos.X), HelperFunctions.thresholdRange<int>(0, height - 1, (int)pos.Y));
+            return new Point(HelperFunctions.thresholdRange<int>(0, width - 1, pos.IntX), HelperFunctions.thresholdRange<int>(0, height - 1, pos.IntY));
         }
 
         private void buildPointingHandMask()

+ 1 - 1
bbiwarg/Detectors/Touch/TouchDetector.cs

@@ -40,7 +40,7 @@ namespace bbiwarg.Detectors.Touch
                     float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);
                     Vector2D tep = new Vector2D(x,y);
 
-                    touchImage.setTouchAt((int)tep.X, (int)tep.Y, TouchImageState.touchDetected);
+                    touchImage.setTouchAt(tep.IntX, tep.IntY, TouchImageState.touchDetected);
                     TouchEvent touchEvent = new TouchEvent(tep, floodValue, finger);
                     touchEvents.Add(touchEvent);
                 }

+ 1 - 1
bbiwarg/Detectors/Touch/TouchTracker.cs

@@ -49,7 +49,7 @@ namespace bbiwarg.Detectors.Touch
                 }
                 if (tracked)
                 {
-                    touchImage.setTouchAt((int)te.Position.X, (int)te.Position.Y, TouchImageState.touchTracked);
+                    touchImage.setTouchAt(te.Position.IntX, te.Position.IntY, TouchImageState.touchTracked);
                     trackedTouchEvents.Add(te);
                     //Console.WriteLine("touch tracked at x:" + te.getX() + " y:" + te.getY() + " [floodValue:" + te.getFloodValue() + "]");
                 }

+ 0 - 279
bbiwarg/Utility/Vector.cs

@@ -1,279 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace bbiwarg.Utility
-{
-    class Vector<T>
-    {
-        private T[] elements;
-
-        public T x { get { return elements[0]; } set { elements[0] = value; } }
-        public T y { get { return elements[1]; } set { elements[1] = value; } }
-        public T z { get { return elements[2]; } set { elements[2] = value; } }
-
-        public Vector(T x, T y)
-        {
-            elements = new T[2];
-            elements[0] = x;
-            elements[1] = y;
-        }
-
-        public Vector(T x, T y, T z)
-        {
-            elements = new T[3];
-            elements[0] = x;
-            elements[1] = y;
-            elements[2] = z;
-        }
-
-        public Vector(int numberOfElements)
-        {
-            this.elements = new T[numberOfElements];
-        }
-
-        public Vector(T[] elements)
-        {
-            this.elements = elements;
-        }
-
-        public Vector(Vector<T> vector)
-        {
-            this.elements = new T[vector.length()];
-            for (int i = 0; i < elements.Length; i++)
-            {
-                elements[i] = vector[i];
-            }
-        }
-
-        public Vector<T> copy()
-        {
-            Vector<T> newVector = new Vector<T>(this);
-            return newVector;
-        }
-
-        public T this[int index]
-        {
-            get
-            {
-                return elements[index];
-            }
-
-            set
-            {
-                elements[index] = value;
-            }
-        }
-
-        public int length()
-        {
-            return elements.Length;
-        }
-
-        public void add(Vector<T> summand)
-        {
-            for (int i = 0; i < elements.Length; i++)
-            {
-                elements[i] = (dynamic)elements[i] + summand[i];
-            }
-        }
-        public void subtract(Vector<T> subtrahend)
-        {
-            for (int i = 0; i < elements.Length; i++)
-            {
-                elements[i] = (dynamic)elements[i] - subtrahend[i];
-            }
-        }
-
-        public void multiply(T scalar)
-        {
-            for (int i = 0; i < elements.Length; i++)
-            {
-                elements[i] = (dynamic)scalar * elements[i];
-            }
-        }
-
-        public float norm()
-        {
-            return subNorm(elements.Length);
-        }
-
-        public float subNorm(int subLength)
-        {
-            T result = (dynamic)0;
-            for (int i = 0; i < subLength; i++)
-            {
-                result += (dynamic)elements[i] * elements[i];
-            }
-            return (float)Math.Sqrt((dynamic)result);
-        }
-
-        public float distance(Vector<T> vector)
-        {
-            return (vector - this).norm();
-        }
-
-        public float subDistance(Vector<T> vector, int subLength)
-        {
-            return (vector - this).subNorm(subLength);
-        }
-
-        public T sum()
-        {
-            T result = (dynamic)0;
-            for (int i = 0; i < this.length(); i++)
-            {
-                result += (dynamic)elements[i];
-            }
-            return result;
-        }
-
-        public Vector<T> normalize() 
-        {
-            float norm = this.norm();
-            Vector<T> result = new Vector<T>(this);
-            for (int i = 0; i < result.length(); i++)
-            {
-                result[i] = (result[i] / (dynamic)norm);
-            }
-            return result;
-        }
-
-        private static void checkLength(Vector<T> vector1, Vector<T> vector2)
-        {
-            if (vector1.length() != vector2.length())
-                throw new ArgumentException("The vectors must have the same length");
-        }
-
-        public static Vector<T> operator +(Vector<T> summand1, Vector<T> summand2)
-        {
-            checkLength(summand1, summand2);
-
-            Vector<T> result = new Vector<T>(summand1);
-            result.add(summand2);
-            return result;
-        }
-
-        public static Vector<T> operator -(Vector<T> minuend, Vector<T> subtrahend)
-        {
-            checkLength(minuend, subtrahend);
-
-            Vector<T> result = new Vector<T>(minuend);
-            result.subtract(subtrahend);
-            return result;
-        }
-
-        public static T operator *(Vector<T> vector1, Vector<T> vector2)
-        {
-            checkLength(vector1, vector2);
-
-            T result = (dynamic)0;
-            for (int i = 0; i < vector1.length(); i++)
-            {
-                result += (dynamic)vector1[i] * vector2[i];
-            }
-            return result;
-        }
-
-        public static Vector<T> operator *(T factor, Vector<T> vector1)
-        {
-            Vector<T> result = new Vector<T>(vector1);
-            result.multiply(factor);
-            return result;
-        }
-
-        public static bool operator ==(Vector<T> vector1, Vector<T> vector2)
-        {
-            checkLength(vector1, vector2);
-            bool result = true;
-            for (int i = 0; i < vector1.length(); i++)
-            {
-                if (vector1[i] != (dynamic)vector2[i])
-                {
-                    result = false;
-                    break;
-                }
-            }
-            return result;
-        }
-
-        public static bool operator !=(Vector<T> vector1, Vector<T> vector2)
-        {
-            return !(vector1 == vector2);
-        }
-
-        public static Vector<T> crossProduct(Vector<T> vector1, Vector<T> vector2)
-        {
-            if (vector1.length() != 3 || vector2.length() != 3)
-                throw new ArgumentException("The vectors' length should be 3");
-
-            Vector<T> result = new Vector<T>(vector1.length());
-            result[0] = (dynamic)vector1.y * vector2.z - (dynamic)vector1.z * vector2.y;
-            result[1] = (dynamic)vector1.z * vector2.x - (dynamic)vector1.x * vector2.z;
-            result[2] = (dynamic)vector1.x * vector2.y - (dynamic)vector1.y * vector2.x;
-
-            return result;
-        }
-
-        public static Vector<T> pointwiseMultiply(Vector<T> vector1, Vector<T> vector2)
-        {
-            checkLength(vector1, vector2);
-            Vector<T> result = new Vector<T>(vector1);
-            for (int i = 0; i < vector1.length(); i++)
-            {
-                result[i] = (dynamic)result[i] * vector2[i];
-            }
-            return result;
-        }
-
-        public static float distancePointPlane(Vector<T> point, Vector<T> planeA, Vector<T> planeB, Vector<T> planeC)
-        {
-            //TODO - Diese funktion funktioniert nur mit T = float, 
-            //die normalisierte Normale einer Ebene macht nur mit floats sinn (werte zwischen 0 und 1)
-            if (point.length() != 3 || planeA.length() != 3 || planeB.length() != 3 || planeC.length() != 3)
-                throw new ArgumentException("The vectors' length should be 3");
-
-            Vector<T> ab = planeB - planeA;
-            Vector<T> ac = planeC - planeA;
-            Vector<T> normal = crossProduct(ab, ac).normalize();
-            
-
-            Vector<T> temp = point - planeA;
-            temp = pointwiseMultiply(temp, normal);
-            temp = pointwiseMultiply(temp, normal);
-            T sum = temp.sum();
-
-            temp = pointwiseMultiply(normal, normal);
-            T sumR = temp.sum();
-
-            if (sumR == (dynamic)0)
-                throw new ArgumentException("the points do not clamp an Plane");
-
-            float distance = (sum * (dynamic)(-1)) / sumR;
-
-            if (distance < 0)
-                distance *= (float)(-1);
-
-            return distance;
-
-        }
-
-        public static Vector<float> projectToLine(Vector<float> p, Vector<float> direction, Vector<float> pointOnLine)
-        {
-            float px = p.x, py = p.y, dx = direction.x, dy = direction.y, ox = pointOnLine.x, oy = pointOnLine.y;
-            float diffx = px - ox;
-            float diffy = py - oy;
-
-            float diff_d = (diffx * dx + diffy * dy);
-            float d_d = (dx * dx + dy * dy);
-            float q = diff_d / d_d;
-
-            float newX = ox + q * dx;
-            float newY = oy + q * dy;
-
-            return new Vector<float>(newX, newY);
-        }
-    }
-}

+ 38 - 33
bbiwarg/Utility/Vector2D.cs

@@ -9,50 +9,52 @@ namespace bbiwarg.Utility
 {
     class Vector2D
     {
-        private float x;
-        private float y;
-        private float length;
-        public float X { get { return x; } private set { x = value; } }
-        public float Y { get { return y; } private set { y = value; } }
-        public float Length { get { return length; } private set { length = value; } }
+        public float X { get; private set; }
+        public float Y { get; private set; }
+        public int IntX { get { return (int)X; } }
+        public int IntY { get { return (int)Y; } }
+        public float Length { get { return (float)Math.Sqrt(X * X + Y * Y); } }
 
-        public Vector2D(float x, float y) {
-            setXY(x, y);
+        public Vector2D(float x, float y)
+        {
+            X = x;
+            Y = y;
         }
 
-        public Vector2D(Point point) {
-            setXY(point.X, point.Y);
+        public Vector2D(Point point)
+        {
+            X = point.X;
+            Y = point.Y;
         }
 
         public Vector2D(PointF point)
         {
-            setXY(point.X, point.Y);
+            X = point.X;
+            Y = point.Y;
         }
 
-        public static implicit operator PointF(Vector2D vec)
+        public float getDistanceTo(Vector2D point)
         {
-            return new PointF(vec.X, vec.Y);
-        }
-
-        private void setXY(float x, float y) {
-            X = x;
-            Y = y;
-            Length = (float)Math.Sqrt(X * X + Y * Y);
-        }
-
-        public float getDistanceTo(Vector2D point) {
             return (this - point).Length;
         }
 
-        public float getAngleBetween(Vector2D vector) {
+        public float getAngleBetween(Vector2D vector)
+        {
             return (float)Math.Acos(dotProduct(vector) / (Length * vector.Length));
         }
 
-        public float dotProduct(Vector2D vector) {
+        public float dotProduct(Vector2D vector)
+        {
             return X * vector.X + Y * vector.Y;
         }
 
-        public bool isInBox(Vector2D corner1, Vector2D corner2) {
+        public float cross(Vector2D v)
+        {
+            return X * v.Y - Y * v.X;
+        }
+
+        public bool isInBox(Vector2D corner1, Vector2D corner2)
+        {
             float minX = Math.Min(corner1.X, corner2.X);
             float maxX = Math.Max(corner1.X, corner2.X);
             float minY = Math.Min(corner1.Y, corner2.Y);
@@ -60,7 +62,8 @@ namespace bbiwarg.Utility
             return (minX <= X && X <= maxX && minY <= Y && Y <= maxY);
         }
 
-        public Vector2D normalize() {
+        public Vector2D normalize()
+        {
             return new Vector2D(X / Length, Y / Length);
         }
 
@@ -94,18 +97,20 @@ namespace bbiwarg.Utility
             return new Vector2D(vector1.X - vector2.X, vector1.Y - vector2.Y);
         }
 
-        public float cross(Vector2D v)
+        public static Vector2D mean(List<Vector2D> vectors)
         {
-            return X * v.Y - Y * v.X;
-        }
-
-        public static Vector2D mean(List<Vector2D> vectors) {
-            Vector2D meanVector = new Vector2D(0,0);
-            foreach (Vector2D vector in vectors) {
+            Vector2D meanVector = new Vector2D(0, 0);
+            foreach (Vector2D vector in vectors)
+            {
                 meanVector += vector;
             }
             return meanVector / vectors.Count;
         }
 
+        public static implicit operator PointF(Vector2D vec)
+        {
+            return new PointF(vec.X, vec.Y);
+        }
+
     }
 }

+ 0 - 1
bbiwarg/bbiwarg.csproj

@@ -92,7 +92,6 @@
     <Compile Include="Utility\Line2D.cs" />
     <Compile Include="Utility\LineSegment2D.cs" />
     <Compile Include="Utility\Quadrangle.cs" />
-    <Compile Include="Utility\Vector.cs" />
     <Compile Include="Utility\Vector2D.cs" />
     <Compile Include="VideoHandle.cs" />
   </ItemGroup>