Пример #1
0
        List <Segment0> FindPrimaryLines(Boundary b)
        {
            string          s        = b.theString;
            Point           curPt    = b.p1;
            List <Segment0> theLines = new List <Segment0>();

            for (int i = 0; i < s.Length; i++)
            {
                ModuleBoundary1.GetPositionOfLinePoint((int)b.p1.X, (int)b.p1.Y, s, i, out int newX, out int newY);
                curPt = new Point(newX, newY);
                for (int j = i + 2; j <= s.Length; j++)
                {
                    string ss = s.Substring(i, j - i);
                    if (!ModuleBoundary1.IsStraightLine(ss) || j == s.Length)
                    {
                        string lineString = ss;
                        if (j < s.Length)
                        {
                            lineString = ss.Substring(0, ss.Length - 1);
                        }
                        ModuleBoundary1.GetPositionOfLinePoint((int)curPt.X, (int)curPt.Y, lineString, 100, out newX, out newY);
                        Point newPt = new Point(newX, newY);
                        if (lineString.Length > 2)
                        {
                            theLines.Add(new Segment0 {
                                p1 = curPt, p2 = newPt
                            });
                            curPt = newPt;
                            i     = j - 2;
                        }
                        break;
                    }
                }
            }
            return(theLines);
        }
Пример #2
0
        public override void Fire()
        {
            Init();  //be sure to leave this here

            foreach (Neuron n in na.Neurons())
            {
                n.SetValueInt(0);
                n.Model = Neuron.modelType.FloatValue;
            }

            ModuleView naSource = theNeuronArray.FindAreaByLabel("ModuleBoundary1");

            if (naSource == null)
            {
                return;
            }

            strokes.Clear();
            lines.Clear();

            ModuleBoundary1 mlf = (ModuleBoundary1)naSource.TheModule;

            if (mlf.boundaries != null)
            {
                DeletePreviousStrokes();
                List <Boundary> theLines = mlf.xx;//.boundaries;
                for (int i = 0; i < theLines.Count; i++)
                {
                    List <Segment0> temp = FindPrimaryLines(theLines[i]);
                    for (int j = 0; j < temp.Count; j++)
                    {
                        lines.Add(temp[j]);
                    }
                }
                //sort the line endpoints (bounding rectangle)
                for (int i = 0; i < lines.Count; i++)
                {
                    Segment0 theLine = lines[i];
                    if (theLine.p1.X > theLine.p2.X || (theLine.p1.X == theLine.p2.X & theLine.p1.Y > theLine.p2.Y))
                    {
                        Point tempPoint = theLine.p2;
                        theLine.p2 = theLine.p1;
                        theLine.p1 = tempPoint;
                    }
                }

                //process the lines into strokes
                for (int i = 0; i < lines.Count; i++)
                {
                    List <Segment0> linesOfSameSlope = new List <Segment0>();
                    if (!lines[i].handled)
                    {
                        //create a list of lines of same slope
                        Segment0 l1 = lines[i];
                        l1.handled = true;
                        for (int j = i + 1; j < lines.Count; j++)
                        {
                            Segment0 l2 = lines[j];
                            {
                                Angle diff = Math.Min(2 * PI - Abs(l1.angle - l2.angle), Abs(l1.angle - l2.angle));
                                if (diff < Utils.Rad(15) && !l2.handled)
                                {
                                    if (linesOfSameSlope.Count == 0)
                                    {
                                        linesOfSameSlope.Add(l1);
                                    }
                                    linesOfSameSlope.Add(l2);
                                    l2.handled = true;
                                }
                            }
                        }
                        //join colinear lines
                        for (int j = 0; j < linesOfSameSlope.Count; j++)
                        {
                            for (int k = j + 1; k < linesOfSameSlope.Count; k++)
                            {
                                Segment0 l11 = linesOfSameSlope[j];
                                Segment0 l21 = linesOfSameSlope[k];
                                double   d1  = Utils.DistancePointToLine(l11.p1, l21.p1, l21.p2);
                                double   d2  = Utils.DistancePointToLine(l11.p2, l21.p1, l21.p2);
                                double   d3  = Utils.DistancePointToLine(l21.p1, l11.p1, l11.p2);
                                double   d4  = Utils.DistancePointToLine(l21.p2, l11.p1, l11.p2);
                                double   min = Min(new double[] { d1, d2, d3, d4 });
                                if (min <= .5)
                                {
                                    //find the two furthest points to be new endpoints
                                    Point[] pts = new Point[] { l11.p1, l11.p2, l21.p1, l21.p2 };
                                    GetFurthestPoints(pts);
                                    if (Distance(pts[3], pts[2]) < 11)
                                    {
                                        l11.p1 = pts[0];
                                        l11.p2 = pts[1];
                                        linesOfSameSlope.RemoveAt(k);
                                        break;
                                    }
                                }
                            }
                        }

                        //now look for strokes
                        for (int j = 0; j < linesOfSameSlope.Count; j++)
                        {
                            for (int k = j + 1; k < linesOfSameSlope.Count; k++)
                            {
                                Segment0 l11  = linesOfSameSlope[j];
                                Segment0 l21  = linesOfSameSlope[k];
                                double[] d    = new double[4];
                                Point[]  near = new Point[4];
                                d[0] = Utils.FindDistanceToSegment(l11.p1, l21.p1, l21.p2, out near[0]);
                                d[1] = Utils.FindDistanceToSegment(l11.p2, l21.p1, l21.p2, out near[1]);
                                d[2] = Utils.FindDistanceToSegment(l21.p1, l11.p1, l11.p2, out near[2]);
                                d[3] = Utils.FindDistanceToSegment(l21.p2, l11.p1, l11.p2, out near[3]);
                                double min = Min(d);
                                if (min < 8) //TODO make relative to length
                                {
                                    CreateCenteredStroke(l11, l21);
                                    linesOfSameSlope.RemoveAt(k);
                                    linesOfSameSlope.RemoveAt(j);
                                    j--;
                                    break;
                                }
                            }
                        }
                    }
                }
                //find stokes which likely connect and connect them.
                for (int i = 0; i < strokes.Count; i++)
                {
                    for (int j = i + 1; j < strokes.Count; j++)
                    {
                        ConnectStrokes(strokes[i], strokes[j]);
                    }
                }
            }

            for (int i = 0; i < strokes.Count; i++)
            {
                SaveThing(strokes[i]);
            }
            FindClustersOfAbsoluteStrodkes();
            ConvertClusterToRelativeStrokes();
            MatchClustersAgainstStoredShapes();
            if (dlg != null)
            {
                UpdateDialog();
            }
        }