Exemple #1
0
        private Angle GetTotalAngleError(List <Thing> near)
        {
            if (lastLandmark == null)
            {
                return(10000);
            }
            Angle totalAngleError = 0;

            for (int i = 0; i < lastLandmark.References.Count; i++)
            {
                Segment s1 = Module2DModel.SegmentFromUKSThing(lastLandmark.References[i].T);
                foreach (Thing t1 in near)
                {
                    //does it match one of the segments presently near me?
                    Segment s2 = Module2DModel.SegmentFromUKSThing(t1);
                    if (s1.theColor == s2.theColor)
                    {
                        Angle m1    = s1.MidPoint.Theta;
                        Angle m2    = s2.MidPoint.Theta;
                        Angle angle = m1 - m2;
                        totalAngleError += Abs(angle);
                    }
                }
            }
            return(totalAngleError);
        }
Exemple #2
0
        public Thing CreateLandmark(List <Thing> near)
        {
            //a landmark is a collection of nearby segments
            //these must be cloned so they can be fixed in position rather than being adjusted relative to Sallie's position
            ModuleUKSN UKS = (ModuleUKSN)FindModuleByType(typeof(ModuleUKSN));

            if (UKS is null)
            {
                return(null);
            }
            Thing newLandmark = UKS.AddThing("Lm" + landmarkCount++.ToString(), "Landmark");

            foreach (Thing t in near)
            {
                Segment s  = Module2DModel.SegmentFromUKSThing(t);
                Thing   P1 = UKS.AddThing("lmp" + pointCount++.ToString(), "Point"); //These points are not included in the model and do don't move with poin of view
                P1.V = s.P1.Clone();
                Thing P2 = UKS.AddThing("lmp" + pointCount++.ToString(), "Point");
                P2.V = s.P2.Clone();
                Thing S = UKS.AddThing("l" + t.Label.ToString(), "SSegment");
                S.AddReference(P1);
                S.AddReference(P2);
                S.AddReference(t.References[2].T);//the color
                newLandmark.AddReference(S);
            }

            return(newLandmark);
        }
        Thing MostLikelyEvent(Thing currentEvent)
        {
            Thing      retVal = null;
            ModuleUKSN UKS    = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return(retVal);
            }

            Segment       s1     = Module2DModel.SegmentFromUKSThing(currentEvent.References[0].T);
            IList <Thing> events = UKS.Labeled("Event").Children;

            foreach (Thing t in events)
            {
                if (t.References.Count > 0)
                {
                    Thing lm = t.References[0].T;
                    if (lm.References.Count > 0)
                    {
                        Thing   lms = lm.References[0].T;
                        Segment s2  = Module2DModel.SegmentFromUKSThing(lms);

                        if (s1.MidPoint.Near(s2.MidPoint, 0.2f))
                        {
                            retVal = t;
                        }
                    }
                }
            }
            return(retVal);
        }
Exemple #4
0
        private void CorrectModelPosition(Module2DModel nmModel, ModuleBehavior nmBehavior, Thing best, List <Thing> near)
        {
            //locations are not very accurate and accumulate errors
            //whenever we encounter a landmark, we update the orientation/location of the model to correct errors
            Segment s2 = Module2DModel.SegmentFromUKSThing(near[0]);
            Segment s1 = null;

            foreach (Link l in best.References)
            {
                Segment s = Module2DModel.SegmentFromUKSThing(l.T);
                if (s.theColor == s2.theColor)
                {
                    s1 = s; break;
                }
            }
            if (s1 != null)
            {
                PointPlus m1     = s1.MidPoint;
                PointPlus m2     = s2.MidPoint;
                float     deltaX = m1.X - m2.X;
                float     deltaY = m1.Y - m2.Y;
                PointPlus a1     = new PointPlus()
                {
                    P = (Point)(s1.P1.V - s1.P2.V)
                };
                PointPlus a2 = new PointPlus()
                {
                    P = (Point)(s2.P1.V - s2.P2.V)
                };
                Angle deltaTheta = a1.Theta - a2.Theta;
                nmModel.Move(-deltaX, -deltaY);
                nmBehavior.TurnTo(deltaTheta);
            }
        }
Exemple #5
0
        private static void FindBestLandmarkMatch(ModuleUKSN UKS, ref Thing best, ref float bestDist, List <Thing> near)
        {
            //searching each spatial Event
            best     = null;
            bestDist = 1000;
            if (UKS == null)
            {
                return;
            }
            if (UKS.Labeled("Landmark") == null)
            {
                return;
            }

            //landmark segments are sorted, closest segment first
            //closer segments are more important
            List <Thing> landmarks = UKS.Labeled("Landmark").Children;

            foreach (Thing landmark in landmarks)
            {
                float totalDist = 0;
                //each reference in that landmark
                int foundCount = 0;  //must match several items to count as a hit
                int linkNumber = 0;
                foreach (Link L1 in landmark.References)
                {
                    Segment s1         = Module2DModel.SegmentFromUKSThing(L1.T);
                    float   d1         = (float)Utils.FindDistanceToSegment(s1);
                    int     nearNumber = 0;
                    foreach (Thing t1 in near)
                    {
                        //does it match one of the segments presently near me?
                        Segment s2 = Module2DModel.SegmentFromUKSThing(t1);
                        float   d2 = (float)Utils.FindDistanceToSegment(s2);
                        if (s1.theColor == s2.theColor)
                        {
                            foundCount++;
                            float dist = (float)Abs(d1 - d2);
                            dist      *= Abs(nearNumber - linkNumber) + 1;
                            totalDist += dist;
                        }
                        nearNumber++;
                    }
                    linkNumber++;
                }
                if (totalDist < bestDist && foundCount == near.Count)
                {
                    best     = landmark;
                    bestDist = totalDist;
                }
            }
        }
        void GoToLandmark(Thing landmark, Segment s)
        {
            Segment s1 = Module2DModel.SegmentFromUKSThing(landmark.References[0].T);

            Module2DModel.OrderSegment(s1);
            float     ratio  = s1.P1.R / (s1.Length);
            PointPlus target = new PointPlus
            {
                X = s.P1.X + (s.P2.X - s.P1.X) * ratio,
                Y = s.P1.Y + (s.P2.Y - s.P1.Y) * ratio,
            };

            MoveTo(target, .2f);
        }
Exemple #7
0
        private void FindPointsOfInterest()
        {
            Module2DModel nmModel = (Module2DModel)FindModuleByType(typeof(Module2DModel));

            if (nmModel == null)
            {
                return;
            }

            LBoundaries.Clear();
            RBoundaries.Clear();
            FindMonocularBoundaries(0, LBoundaries);
            FindMonocularBoundaries(1, RBoundaries);
            FindBinocularBoundaries();
            FindAreasOfColor();

            //curArea.t being null means this area is not in the model...just add it
            foreach (Area curArea in areas)
            {
                if (curArea.t == null && curArea.theColor != 0)
                {
                    //add the segment to the model
                    curArea.t = nmModel.AddSegmentFromVision(curArea.PL, curArea.PR, curArea.theColor, viewChanged == 0);
                }
            }

            for (int i = 0; i < areas.Count; i++)
            {
                Area curArea = areas[i];
                if (i < areas.Count - 1)
                {
                    Area nextArea = areas[i + 1];
                    //the case of two adjacent colored boundaries means there are adjoining or occluding areas
                    //if occluding, do not update the (possibly) hidden point(s)
                    if (curArea.theColor != 0 && nextArea.theColor != 0 && curArea.t != null && nextArea.t != null)
                    //&&                         curArea.PR.R == nextArea.PL.R && curArea.PR.Theta == nextArea.PL.Theta)
                    {
                        Segment curS = Module2DModel.SegmentFromUKSThing(curArea.t);
                        Module2DModel.OrderSegment(curS);
                        Segment nextS = Module2DModel.SegmentFromUKSThing(nextArea.t);
                        Module2DModel.OrderSegment(nextS);
                        //is aa.PL in front of prevSegment (the little correction hides an occlusion problem where the endpoints nearly match
                        if (nextArea.PL.Theta > curS.P1.Theta - Rad(2) && nextArea.PL.Theta < curS.P2.Theta + Rad(2))
                        {
                            float segDistAtPoint = curS.P1.R;
                            float dr1            = (curS.P2.R - curS.P1.R);
                            float dtp            = (curS.P1.Theta - nextArea.PL.Theta);
                            float dtt            = (curS.P1.Theta - curS.P2.Theta);
                            segDistAtPoint += dr1 * dtp / dtt;
                            if (nextArea.PL.R < segDistAtPoint)
                            {
                                curArea.PRHidden = true;
                            }
                        }
                        //is prevArea.PR in front of aaSegment
                        if (curArea.PR.Theta > nextS.P1.Theta - Rad(2) && curArea.PR.Theta < nextS.P2.Theta + Rad(2))
                        {
                            float segDistAtPoint = nextS.P1.R + (nextS.P2.R - nextS.P1.R) * (nextS.P1.Theta - curArea.PR.Theta) / (nextS.P1.Theta - nextS.P2.Theta);
                            if (curArea.PR.R < segDistAtPoint)
                            {
                                nextArea.PLHidden = true;
                            }
                        }
                    }
                }
            }

            //check for single boundaries and update them in the model
            foreach (Area curArea in areas)
            {
                //if the model data already exists, update points
                if (curArea.t != null)
                {
                    if (!curArea.PLHidden)
                    {
                        nmModel.UpdateEndpointFromVision(curArea.PL, curArea.theColor, viewChanged == 0);
                    }
                    if (!curArea.PRHidden)
                    {
                        nmModel.UpdateEndpointFromVision(curArea.PR, curArea.theColor, viewChanged == 0);
                    }
                }
            }
        }
        public override void Fire()
        {
            Init();  //be sure to leave this here

            if (GetNeuronValue("Auto") == 0)
            {
                return;
            }
            goToGoal = GetNeuronValue("Goal") == 1;

            ModuleBehavior mBehavior = (ModuleBehavior)FindModleu(typeof(ModuleBehavior));

            if (mBehavior == null)
            {
                return;
            }
            Module2DModel mModel = (Module2DModel)FindModleu(typeof(Module2DModel));

            if (mModel is null)
            {
                return;
            }
            ModuleEvent mEvent = (ModuleEvent)FindModleu(typeof(ModuleEvent));

            if (mEvent == null)
            {
                return;
            }
            ModuleUKSN UKS = (ModuleUKSN)FindModleu(typeof(ModuleUKSN));

            if (UKS == null)
            {
                return;
            }

            if (GetNeuronValue("ModuleBehavior", "Done") == 0)
            {
                return;
            }


            mModel.GetSegmentsFromUKS();
            IList <Thing> segments = mModel.GetUKSSegments();

            if (segments.Count == 0)
            {
                return;
            }
            Thing t = segments[0]; //TODO: this only works with just a single item in the UKS

            //figure out if any motion occured
            Segment s = Module2DModel.SegmentFromUKSThing(t);

            Module2DModel.OrderSegment(s);

            if (GetNeuronValue("E0") == 1)
            {
                GoToLandmark(UKS.Labeled("E0").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E1") == 1)
            {
                GoToLandmark(UKS.Labeled("E1").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }
            if (GetNeuronValue("E2") == 1)
            {
                GoToLandmark(UKS.Labeled("E2").References[0].T, s);
                doPush    = 2;
                doBackOff = true;
                return;
            }


            if (doPush != 0)
            {
                if (doPush == 2)
                {
                    Push(s);
                }
                doPush--;
                return;
            }
            if (doFaceSegment)
            {
                DoFaceSegment(s);
                doFaceSegment = false;
                return;
            }

            Segment s1;

            if (lastPosition == null) //create objects to keep track of the target and last position
            {
                s1                 = s.Clone();
                lastPosition       = mModel.AddSegmentToUKS(s1);
                lastPosition.Label = "LastPosition";
                lastPosition.RemoveParent(UKS.Labeled("Segment"));
                lastPosition.AddParent(UKS.Labeled("SSegment"));
                Module2DSim mSim = (Module2DSim)FindModleu(typeof(Module2DSim));
                if (mSim is null)
                {
                    return;
                }
                Segment motionTarget = mSim.GetMotionTarget();
                if (motionTarget == null)
                {
                    motionTarget          = new Segment();
                    motionTarget.P1       = new PointPlus(4, 1.5f);
                    motionTarget.P2       = new PointPlus(4, -2.5f);
                    motionTarget.theColor = (ColorInt)0xff;
                }
                endTarget       = mModel.AddSegmentToUKS(motionTarget);
                endTarget.Label = "EndTarget";
                //endTarget.RemoveParent(UKS.Labeled("Segment"));
                //endTarget.AddParent(UKS.Labeled("SSegment"));
            }
            else
            {
                s1 = Module2DModel.SegmentFromUKSThing(lastPosition);
            }

            //get motion from subtracting and then updating last position
            Angle rotation = s.Angle - s1.Angle;

            if (rotation > PI / 2)
            {
                rotation = PI - rotation;
            }
            if (rotation < -PI / 2)
            {
                rotation = PI + rotation;
            }
            Motion motion = new Motion()
            {
                P        = (Point)s.MidPoint.V - s1.MidPoint.V,
                rotation = rotation,
            };

            lastPosition.References[0].T.V = s.P1.Clone();
            lastPosition.References[1].T.V = s.P2.Clone();

            if (Abs(motion.R) > .01 || Abs(motion.rotation) > .05 && !goToGoal && !doBackOff)
            {
                //check for existing Event
                Thing currentEvent = MostLikelyEvent(t);
                if (currentEvent == null)
                {                //add new Event
                    Thing lm1 = mEvent.CreateLandmark(new List <Thing>()
                    {
                        t
                    });
                    Thing t1 = mEvent.CreateEvent(lm1);
                    Thing t3 = UKS.AddThing("m" + motionCount++, UKS.Labeled("Motion"), motion);
                    mEvent.AddOutcomePair(t1, UKS.GetOrAddThing("Push", "Action"), t3);
                }
                return;
            }

            if (doBackOff)
            {
                DoBackOff(s);
                doBackOff     = false;
                doFaceSegment = true;
                return;
            }

            if (goToGoal)
            {
                if (endTarget != null)
                {
                    Segment s2 = Module2DModel.SegmentFromUKSThing(endTarget);
                    GoToGoal(s, s2);
                    return;
                }
            }

            Explore(s);
        }
Exemple #9
0
        public override bool Draw(bool checkDrawTimer)
        {
            if (!base.Draw(checkDrawTimer))
            {
                return(false);
            }

            Module2DModel parent = (Module2DModel)base.ParentModule;

            theCanvas.Children.Clear();
            Point windowSize   = new Point(theCanvas.ActualWidth, theCanvas.ActualHeight);
            Point windowCenter = new Point(windowSize.X / 2, windowSize.Y / 2);

            float scale = (float)Math.Min(windowSize.X, windowSize.Y) / 12;

            if (scale == 0)
            {
                return(false);
            }
            TransformGroup tg = new TransformGroup();

            tg.Children.Add(new RotateTransform(90));
            tg.Children.Add(new ScaleTransform(scale, -scale, 0, 0));// windowCenter.X, windowCenter.Y));
            tg.Children.Add(new TranslateTransform(windowCenter.X, windowCenter.Y));
            theCanvas.RenderTransform = tg;

            //draw an origin point
            theCanvas.Children.Add(new Line
            {
                X1 = -.20,
                X2 = .20,
                Y1 = 0,
                Y2 = 0,
                StrokeThickness = 1 / scale,
                Stroke          = Brushes.Black
            });
            theCanvas.Children.Add(new Line
            {
                X1 = 0,
                X2 = 0,
                Y1 = -.20,
                Y2 = .20,
                StrokeThickness = 1 / scale,
                Stroke          = Brushes.Black
            });

            //draw possible points;
            try
            {
                foreach (Thing t in parent.GetUKSPoints() ?? Enumerable.Empty <Thing>())
                {
                    if (t.V is PointPlus P1 && !float.IsInfinity(P1.X) && !float.IsInfinity(P1.Y))
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P1.X,
                            X2 = P1.X,
                            Y1 = P1.Y,
                            Y2 = P1.Y,
                            StrokeThickness    = 3 / scale,
                            StrokeEndLineCap   = PenLineCap.Round,
                            StrokeStartLineCap = PenLineCap.Round,
                            // Stroke = new SolidColorBrush(P1.TheColor)
                            Stroke = new SolidColorBrush(Colors.Orange)
                        });
                    }
                }
            }
            catch { }
            //draw the objects'
            try
            {
                foreach (Thing t in parent.GetUKSSegments() ?? Enumerable.Empty <Thing>())
                {
                    Segment segment = Module2DModel.SegmentFromUKSThing(t);
                    if (float.IsNaN(segment.P1.R))
                    {
                        continue;
                    }
                    Color theColor = Utils.IntToColor(segment.theColor);
                    Point P1       = segment.P1.P;
                    Point P2       = segment.P2.P;
                    Point P1P      = P1 + (P2 - P1) * segment.P1.Conf / 4;       // .2;
                    Point P2P      = P1 + (P2 - P1) * (1 - segment.P2.Conf / 4); // .8;

                    theCanvas.Children.Add(new Line
                    {
                        X1 = P1.X,
                        X2 = P2.X,
                        Y1 = P1.Y,
                        Y2 = P2.Y,
                        StrokeThickness = 4 / scale,
                        Stroke          = new SolidColorBrush(theColor),
                    });

                    if (segment.P1.Conf != 0)
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P1.X,
                            X2 = P1P.X,
                            Y1 = P1.Y,
                            Y2 = P1P.Y,
                            StrokeThickness = 4 / scale,
                            Stroke          = new SolidColorBrush(Colors.White),
                        });
                    }
                    if (segment.P2.Conf != 0)
                    {
                        theCanvas.Children.Add(new Line
                        {
                            X1 = P2.X,
                            X2 = P2P.X,
                            Y1 = P2.Y,
                            Y2 = P2P.Y,
                            StrokeThickness = 4 / scale,
                            Stroke          = new SolidColorBrush(Colors.White),
                        });
                    }
                }
            }
            catch { }

            if (parent.imagining)
            {
                //draw any imagined objects
                for (int i = 0; i < parent.imagination.Count; i++)
                {
                    Color theColor = Utils.IntToColor(parent.imagination[i].theColor);
                    Point P1       = parent.imagination[i].P1.P;
                    Point P2       = parent.imagination[i].P2.P;
                    Point P1P      = P1 + (P2 - P1) * .2;
                    Point P2P      = P1 + (P2 - P1) * .8;

                    theCanvas.Children.Add(new Line
                    {
                        X1 = P1.X,
                        X2 = P2.X,
                        Y1 = P1.Y,
                        Y2 = P2.Y,
                        StrokeThickness = 4 / scale,
                        Stroke          = new SolidColorBrush(theColor),
                        Opacity         = .5
                    });
                }


                LinearGradientBrush lb = new LinearGradientBrush();
                lb.StartPoint = new Point(0, 1);
                lb.EndPoint   = new Point(0, 0);
                lb.GradientStops.Add(new GradientStop(Colors.Transparent, 0.0));
                lb.GradientStops.Add(new GradientStop(Colors.Transparent, 0.4));
                lb.GradientStops.Add(new GradientStop(Colors.White, .76));
                lb.GradientStops.Add(new GradientStop(Colors.White, 1.0));
                Rectangle r = new Rectangle()
                {
                    Width   = 40,
                    Height  = 40,
                    Opacity = .75,
                    Fill    = lb
                };
                Canvas.SetTop(r, -20);
                Canvas.SetLeft(r, -20);
                theCanvas.Children.Add(r);
            }
            return(true);
        }