Esempio n. 1
0
        private void CreateCenteredStroke(Segment0 s1, Segment0 s2)
        {
            PointPlus p1 = new PointPlus(s1.p1);
            PointPlus p2 = new PointPlus(s1.p2);
            PointPlus p3 = new PointPlus(s2.p1);
            PointPlus p4 = new PointPlus(s2.p2);

            //transfer first point to origin
            PointPlus p10 = p1 - p1;
            PointPlus p20 = p2 - p1;
            PointPlus p30 = p3 - p1;
            PointPlus p40 = p4 - p1;

            //rotate so both segments are horizontal.
            Angle     a    = p20.Theta;
            PointPlus p10r = p10; p10r.Theta -= a;
            PointPlus p20r = p20; p20r.Theta -= a;
            PointPlus p30r = p30; p30r.Theta -= a;
            PointPlus p40r = p40; p40r.Theta -= a;
            double    minX = Min(new double[] { p10r.X, p20r.X, p30r.X, p40r.X });
            double    maxX = Max(new double[] { p10r.X, p20r.X, p30r.X, p40r.X });
            double    y    = (p30r.Y + p40r.Y) / 2;
            PointPlus pa   = new PointPlus((float)minX, (float)y / 2);
            PointPlus pb   = new PointPlus((float)maxX, (float)y / 2);

            pa.Theta += a;
            pb.Theta += a;
            pa        = pa + p1;
            pb        = pb + p1;

            Stroke theStroke = new Stroke
            {
                width = (float)y,
                p1    = pa.P,
                p2    = pb.P,
            };

            strokes.Add(theStroke);
        }
Esempio n. 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();
            }
        }