Quellcode durchsuchen

split up Line2D into Line2D and LineSegment2D

Alexander Hendrich vor 10 Jahren
Ursprung
Commit
240b985295

+ 6 - 7
bbiwarg/Detectors/Fingers/Finger.cs

@@ -4,8 +4,6 @@ using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
-using Emgu.CV;
-using Emgu.CV.Structure;
 using bbiwarg.Images;
 using bbiwarg.Utility;
 
@@ -16,7 +14,7 @@ namespace bbiwarg.Detectors.Fingers
         private FingerSliceTrail sliceTrail;
         public Vector2D Tip { get { return sliceTrail.Start.Mid; } }
         public Vector2D Hand { get { return sliceTrail.End.Mid; } }
-        public Line2D Line { get { return sliceTrail.Line; } }
+        public LineSegment2D LineSegment { get { return sliceTrail.LineSegment; } }
         public FingerSliceTrail SliceTrail { get { return sliceTrail; } private set { sliceTrail = value; } }
 
         public Finger(FingerSliceTrail sliceTrail)
@@ -26,7 +24,8 @@ namespace bbiwarg.Detectors.Fingers
 
         public float getSimilarity(Finger compareFinger)
         {
-            Line2D compareLine = compareFinger.Line;
+            LineSegment2D compareLineSegment = compareFinger.LineSegment;
+            Line2D compareLine = compareLineSegment.Line;
 
             //thresholds
             float thresholdMaxAngle = (float)(30 * Math.PI / 180); // 30°
@@ -34,15 +33,15 @@ namespace bbiwarg.Detectors.Fingers
             float thresholdMaxVerticalDistance = 40;
 
             //check angle
-            float angle = Line.getAngleBetween(compareLine);
+            float angle = LineSegment.Line.getAngleBetween(compareLine);
             float angleSimilarity = Math.Max(1 - angle / thresholdMaxAngle, 0);
 
             //check parallel distance
-            float parallelDistance = Line.getParallelDistanceTo(compareLine);
+            float parallelDistance = LineSegment.getParallelDistanceTo(compareLineSegment);
             float parallelDistanceSimilarity = Math.Max(1 - parallelDistance / thresholdMaxParallelDistance, 0);
 
             //check vertical distance
-            float verticalDistance = Line.getVerticalDistanceTo(compareLine);
+            float verticalDistance = LineSegment.getVerticalDistanceTo(compareLineSegment);
             float verticalDistanceSimilarity = Math.Max(1 - verticalDistance / thresholdMaxVerticalDistance, 0);
 
             return (angleSimilarity + parallelDistanceSimilarity + verticalDistanceSimilarity) / 3;

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

@@ -152,8 +152,8 @@ namespace bbiwarg.Detectors.Fingers
                     for (int i = fingers.Count - 1; i >= 0; i--)
                     {
                         Finger f = fingers[i];
-                        Line2D lineA = finger.Line;
-                        Line2D lineB = f.Line;
+                        Utility.LineSegment2D lineA = finger.LineSegment;
+                        Utility.LineSegment2D lineB = f.LineSegment;
 
                         if(lineA.getVerticalDistanceTo(lineB) < 5 && lineA.getParallelDistanceTo(lineB) < 5)
                         {

+ 1 - 1
bbiwarg/Detectors/Fingers/FingerSlice.cs

@@ -15,7 +15,7 @@ namespace bbiwarg.Detectors.Fingers
         public Vector2D Start { get { return start; } private set { start = value; } }
         public Vector2D Mid { get { return mid; } private set { mid = value; } }
         public Vector2D End { get { return end; } private set { end = value; } }
-        public Line2D Line { get { return new Line2D(Start, End); } }
+        public LineSegment2D Line { get { return new LineSegment2D(Start, End); } }
         public float Size { get { return Start.getDistanceTo(End); } }
 
         public FingerSlice(Vector2D start, Vector2D end) {

+ 1 - 1
bbiwarg/Detectors/Fingers/FingerSliceTrail.cs

@@ -14,7 +14,7 @@ namespace bbiwarg.Detectors.Fingers
         public FingerSlice End { get { return slices[slices.Count - 1]; } }
         public FingerSlice this[int index] { get { return slices[index]; } }
         public int NumSlices { get { return slices.Count; } }
-        public Line2D Line { get { return new Line2D(Start.Mid, End.Mid); } }
+        public LineSegment2D LineSegment { get { return new LineSegment2D(Start.Mid, End.Mid); } }
 
         public FingerSliceTrail(FingerSlice slice)
         {

+ 1 - 1
bbiwarg/Detectors/Fingers/FingerTracker.cs

@@ -63,7 +63,7 @@ namespace bbiwarg.Detectors.Fingers
                 }
                 if (tracked)
                 {
-                    fingerImage.drawLine(finger.Line, FingerImageState.fingerTracked);
+                    fingerImage.drawLine(finger.LineSegment, FingerImageState.fingerTracked);
                     trackedFingers.Add(finger);
                 }
             }

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

@@ -113,9 +113,9 @@ namespace bbiwarg.Detectors.Palm
             foreach (Finger f in fingers)
             {
 
-                if (f.Line.Length > maxLength)
+                if (f.LineSegment.Length > maxLength)
                 {
-                    maxLength = f.Line.Length;
+                    maxLength = f.LineSegment.Length;
                     longest = f;
                 }
             }

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

@@ -34,7 +34,7 @@ namespace bbiwarg.Detectors.Touch
                 if (floodValue > floodValueThreshold)
                 {
                     //correct touchEvent position
-                    Vector2D direction = finger.Line.Direction;
+                    Vector2D direction = finger.LineSegment.Line.Direction;
                     float directionFactor = 10;
                     float x = HelperFunctions.thresholdRange<float>(0, depthImage.getWidth() - 1, tipPoint.X + directionFactor * direction.X);
                     float y = HelperFunctions.thresholdRange<float>(0, depthImage.getHeight() - 1, tipPoint.Y + directionFactor * direction.Y);

+ 2 - 2
bbiwarg/Images/FingerImage.cs

@@ -36,10 +36,10 @@ namespace bbiwarg.Images
             for (int i = 0; i < trail.NumSlices; i++) {
                 drawLine(trail[i].Line, FingerImageState.fingerSlice);
             }
-            drawLine(finger.Line, state);
+            drawLine(finger.LineSegment, state);
         }
 
-        public void drawLine(Line2D line, FingerImageState state)
+        public void drawLine(Utility.LineSegment2D line, FingerImageState state)
         {
             image.Draw(new LineSegment2DF(line.P1, line.P2), new Gray((byte)state), 1);
         }

+ 1 - 1
bbiwarg/MainBBWIWARG.cs

@@ -12,7 +12,7 @@ namespace bbiwarg
     {
         static void Main(string[] args)
         {
-            IInputProvider inputProvider = new IisuInputProvider();//"..\\..\\videos\\touch\\4.skv");
+            IInputProvider inputProvider = new IisuInputProvider("..\\..\\videos\\touch\\4.skv");
             VideoHandle videoHandle = new VideoHandle(inputProvider);
             videoHandle.start();
 

+ 12 - 54
bbiwarg/Utility/Line2D.cs

@@ -15,24 +15,12 @@ namespace bbiwarg.Utility
 
     class Line2D
     {
-        public Vector2D P1 { get; private set; }
-        public Vector2D P2 { get; private set; }
+        public Vector2D PointOnLine { get; private set; }
         public Vector2D Direction { get; private set; }
-        public float Length { get { return P1.getDistanceTo(P2); }}
 
-        public Line2D(Vector2D p1, Vector2D p2)
-        {
-            setPoints(p1, p2);
-        }
-
-        private void setPoints(Vector2D p1, Vector2D p2)
-        {
-            //endpoints
-            P1 = p1;
-            P2 = p2;
-
-            //direction
-            Direction = (P1 - P2).normalize();
+        public Line2D(Vector2D pointOnLine, Vector2D direction) {
+            PointOnLine = pointOnLine;
+            Direction = direction.normalize();
         }
 
         public float getAngleBetween(Line2D line)
@@ -41,37 +29,11 @@ namespace bbiwarg.Utility
             return Math.Min(angle, 180 - angle);
         }
 
-        public float getParallelDistanceTo(Line2D line)
-        {
-            if (onSide(line.P1) != onSide(line.P2)) return 0;
-
-            Vector2D a1 = projectToLine(line.P1);
-            Vector2D a2 = projectToLine(line.P2);
-
-            float distanceA1 = a1.getDistanceTo(line.P1);
-            float distanceA2 = a2.getDistanceTo(line.P2);
-            return Math.Min(distanceA1, distanceA2);
-        }
-
-        public float getVerticalDistanceTo(Line2D line)
-        {
-            Vector2D a1 = projectToLine(line.P1);
-            Vector2D a2 = projectToLine(line.P2);
-
-            if (P1.isInBox(a1, a2) || P2.isInBox(a1, a2)) return 0;
-
-            float distanceP1A1 = P1.getDistanceTo(a1);
-            float distanceP1A2 = P1.getDistanceTo(a2);
-            float distanceP2A1 = P2.getDistanceTo(a1);
-            float distanceP2A2 = P2.getDistanceTo(a2);
-            return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
-        }
-
         public LineSide onSide(Vector2D point)
         {
             float yPerX = Direction.Y / Direction.X;
-            float xDiff = point.X - P1.X;
-            float newY = P1.Y + yPerX * xDiff;
+            float xDiff = point.X - PointOnLine.X;
+            float newY = PointOnLine.Y + yPerX * xDiff;
             if (newY < point.Y) return LineSide.above;
             else if (newY > point.Y) return LineSide.below;
             else return LineSide.onLine;
@@ -79,7 +41,7 @@ namespace bbiwarg.Utility
 
         public Vector2D projectToLine(Vector2D point)
         {
-            float px = point.X, py = point.Y, dx = Direction.X, dy = Direction.Y, ox = P1.X, oy = P1.Y;
+            float px = point.X, py = point.Y, dx = Direction.X, dy = Direction.Y, ox = PointOnLine.X, oy = PointOnLine.Y;
             float diffx = px - ox;
             float diffy = py - oy;
 
@@ -93,12 +55,12 @@ namespace bbiwarg.Utility
             return new Vector2D(newX, newY);
         }
 
-        public Vector2D intersection(Line2D line)
+        public Vector2D getIntersection(Line2D line)
         {
-            Vector2D p = P1;
-            Vector2D r = P2 - P1;
-            Vector2D q = line.P1;
-            Vector2D s = line.P2 - line.P1;
+            Vector2D p = PointOnLine;
+            Vector2D r = Direction;
+            Vector2D q = line.PointOnLine;
+            Vector2D s = line.Direction;
 
             float r_cross_s = r.cross(s);
             float q_p_cross_s = (q - p).cross(s);
@@ -110,9 +72,5 @@ namespace bbiwarg.Utility
             return p + t * r;
         }
 
-        public override string ToString()
-        {
-            return (int)P1.X + "|" + (int)P1.Y + " --- " + (int)P2.X + "|" + (int)P2.Y;
-        }
     }
 }

+ 63 - 0
bbiwarg/Utility/LineSegment2D.cs

@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace bbiwarg.Utility
+{
+
+    class LineSegment2D
+    {
+        public Vector2D P1 { get; private set; }
+        public Vector2D P2 { get; private set; }
+        public Line2D Line { get; private set; }
+        public float Length { get { return P1.getDistanceTo(P2); }}
+
+        public LineSegment2D(Vector2D p1, Vector2D p2)
+        {
+            setPoints(p1, p2);
+        }
+
+        private void setPoints(Vector2D p1, Vector2D p2)
+        {
+            //endpoints
+            P1 = p1;
+            P2 = p2;
+
+            //line
+            Line = new Line2D(P1, P1 - P2);
+        }
+
+        public float getParallelDistanceTo(LineSegment2D line)
+        {
+            if (Line.onSide(line.P1) != Line.onSide(line.P2)) return 0;
+
+            Vector2D a1 = Line.projectToLine(line.P1);
+            Vector2D a2 = Line.projectToLine(line.P2);
+
+            float distanceA1 = a1.getDistanceTo(line.P1);
+            float distanceA2 = a2.getDistanceTo(line.P2);
+            return Math.Min(distanceA1, distanceA2);
+        }
+
+        public float getVerticalDistanceTo(LineSegment2D line)
+        {
+            Vector2D a1 = Line.projectToLine(line.P1);
+            Vector2D a2 = Line.projectToLine(line.P2);
+
+            if (P1.isInBox(a1, a2) || P2.isInBox(a1, a2)) return 0;
+
+            float distanceP1A1 = P1.getDistanceTo(a1);
+            float distanceP1A2 = P1.getDistanceTo(a2);
+            float distanceP2A1 = P2.getDistanceTo(a1);
+            float distanceP2A2 = P2.getDistanceTo(a2);
+            return Math.Min(Math.Min(distanceP1A1, distanceP1A2), Math.Min(distanceP2A1, distanceP2A2));
+        }
+
+        public override string ToString()
+        {
+            return (int)P1.X + "|" + (int)P1.Y + " --- " + (int)P2.X + "|" + (int)P2.Y;
+        }
+    }
+}

+ 1 - 0
bbiwarg/bbiwarg.csproj

@@ -90,6 +90,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Utility\HelperFunctions.cs" />
     <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" />