/// <summary>
        /// finish the wrapping and cretae the intersection
        /// </summary>
        public void FinalizeIntersection()
        {
            // finalize the intersection polygon
            Polygon sq = this.CreateSquarePolygon(this.WrapInitial.Value, this.WrapFinal.Value).Inflate(1.0);
            Polygon interPoly = this.CreateIntersectionPolygon(sq);

            // check poly not null
            if (interPoly != null)
            {
                // inflate the inter poly
                interPoly = interPoly.Inflate(0.05);

                // retreive all exits involved in this intersection
                Dictionary<IAreaSubtypeWaypointId, ITraversableWaypoint> exits = this.IntersectionExits(sq);

                // make sure the inter contains an exit
                if (exits.Count > 0)
                {
                    // make stopped exits, necessarily these are arbiter waypoints not perimeter points
                    List<ArbiterStoppedExit> ases = this.CreateStoppedExits(exits.Values);

                    // construct intersection id
                    ITraversableWaypoint[] exitArray = new ITraversableWaypoint[exits.Count];
                    exits.Values.CopyTo(exitArray, 0);
                    ArbiterIntersectionId aii = new ArbiterIntersectionId(exitArray[0].Exits[0].InterconnectId);

                    // determine incoming lanes
                    Dictionary<ArbiterLane, LinePath.PointOnPath> incoming = this.DetermineIncoming(interPoly);

                    // create the intersection
                    ArbiterIntersection ai = new ArbiterIntersection(
                        interPoly,
                        ases,
                        this.DetermineInvolved(exits.Values, incoming),
                        incoming,
                        exits,
                        interPoly.Center,
                        aii,
                        arn,
                        this.IntersectionEntries(sq)
                        );

                    // create safety zones
                    this.CreateSafetyImplicitZones(ai);

                    // update poly
                    //this.UpdateIntersectionPolygon(ai);

                    /*List<Polygon> ps = new List<Polygon>();
                    foreach (ITraversableWaypoint itw in exits.Values)
                    {
                        foreach (ArbiterInterconnect ait in itw.Exits)
                        {
                            ps.Add(ait.TurnPolygon);
                        }
                    }
                    ai.IntersectionPolygon = this.GetIntersectionPolygon(ps, ai.AllExits, ai.AllEntries);*/
                    //ai.IntersectionPolygon = UrbanChallenge.Arbiter.Core.Common.Tools.PolygonToolkit.PolygonUnion(ps);
                    try
                    {
                        List<Polygon> ps = new List<Polygon>();
                        foreach (ITraversableWaypoint itw in exits.Values)
                        {
                            foreach (ArbiterInterconnect ait in itw.Exits)
                            {
                                ps.Add(ait.TurnPolygon);
                            }
                        }
                        ai.IntersectionPolygon = this.GetIntersectionPolygon(ps, ai.AllExits, ai.AllEntries);

                        if (ai.IntersectionPolygon.IsComplex)
                        {
                            EditorOutput.WriteLine("Intersection polygon complex, defaulting");
                            throw new Exception("complex polygon exception");
                        }
                    }
                    catch (Exception)
                    {
                        EditorOutput.WriteLine("Error in union polygon generation, using better default");

                        try
                        {
                            this.UpdateIntersectionPolygon(ai);
                        }
                        catch (Exception)
                        {
                            EditorOutput.WriteLine("Error in my simple polygon generation, plain default");
                            List<Coordinates> cs = new List<Coordinates>();
                            foreach (ITraversableWaypoint itw in ai.AllEntries.Values)
                                cs.Add(itw.Position);
                            foreach (ITraversableWaypoint itw in ai.AllExits.Values)
                            {
                                cs.Add(itw.Position);
                                foreach (ArbiterInterconnect aint in itw.Exits)
                                {
                                    cs.AddRange(aint.TurnPolygon);
                                }
                            }
                            ai.IntersectionPolygon = Polygon.GrahamScan(cs);
                        }
                    }

                    try
                    {
                        // add intersection
                        arn.ArbiterIntersections.Add(aii, ai);

                        // add to exit lookup
                        foreach (IAreaSubtypeWaypointId awi in exits.Keys)
                        {
                            if (arn.IntersectionLookup.ContainsKey(awi))
                                arn.IntersectionLookup[awi] = ai;
                            else
                                arn.IntersectionLookup.Add(awi, ai);
                        }

                        // add to display objects
                        arn.DisplayObjects.Add(ai);
                        rd.AddDisplayObject(ai);
                    }
                    catch (Exception e)
                    {
                        EditorOutput.WriteLine("Error adding intersection: " + aii.ToString());
                        EditorOutput.WriteLine("Error adding intersection: " + e.ToString());
                    }
                }
            }

            // reset the tool
            this.it.ResetIcons();
            this.WrapFinal = null;
            this.WrapInitial = null;
        }
        /// <summary>
        /// What to do when mouse button is lifted up
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            #region Left

            if (e.Button == MouseButtons.Left)
            {
                if (this.selected != null && this.selected is CarDisplayObject && this.isDragging)
                {
                    this.isDragging = false;
                    this.temporaryCoordinate = null;
                    Cursor.Current = Cursors.Default;
                }
                else if (this.selected != null && this.selected is SimObstacle && this.isDragging)
                {
                    this.isDragging = false;
                    this.temporaryCoordinate = null;
                    Cursor.Current = Cursors.Default;
                }
                // if the user is dragging
                else if (isDragging)
                {
                    isDragging = false;
                    Cursor.Current = Cursors.Default;
                }

                // redraw
                this.Invalidate();
            }

            #endregion

            #region Right

            if (e.Button == MouseButtons.Left)
            {
                if (this.selected != null && this.selected is SimObstacle && this.isDragging && this.temporaryCoordinate.HasValue)
                {
                    this.isDragging = false;
                    this.temporaryCoordinate = null;
                    Cursor.Current = Cursors.Default;
                }
            }

            #endregion

            base.OnMouseUp(e);
        }
Exemplo n.º 3
0
 public DNode()
 {
     val = 1.0 / 0.0;
     prev = null;
     visited = false;
 }
        /// <summary>
        /// What to do when user clicks display
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            #region Hit Test

            // filter for vehicles or obstacles
            DisplayObjectFilter dof = delegate(IDisplayObject target)
            {
                // check if target is network object
                if (target is CarDisplayObject || target is SimObstacle)
                    return true;
                else
                    return false;
            };

            // perform the hit test over the filter
            HitTestResult htr = this.HitTest(transform.GetWorldPoint(new PointF(e.X, e.Y)), dof);

            // check current selection if need to set as not selected
            if (this.selected != null && (htr.Hit && !this.selected.Equals(htr.DisplayObject))
                || (!htr.Hit))
            {
                if (this.selected != null)
                {
                    // remove current selection
                    this.selected.Selected = SelectionType.NotSelected;
                }

                this.selected = null;
                this.Simulation.simEngine.SetPropertyGridDefault();
            }

            #endregion

            #region Left

            if (e.Button == MouseButtons.Left)
            {
                // check if we hit a vehicle
                if (htr.Hit &&  htr.DisplayObject is SimVehicle)
                {
                    // display obj
                    CarDisplayObject cdo = (CarDisplayObject)htr.DisplayObject;

                    // set the vehicles as selected
                    cdo.Selected = SelectionType.SingleSelected;
                    this.selected = cdo;
                    this.Simulation.simEngine.propertyGrid.SelectedObject = cdo;

                    // check if we can move the vehicle
                    if (!((SimVehicle)cdo).VehicleState.IsBound)
                    {
                        // set dragging
                        isDragging = true;
                        Cursor.Current = Cursors.Hand;

                        // set temp
                        this.temporaryCoordinate = cdo.Position;
                    }

                    // redraw
                    this.Invalidate();
                }
                // check if hit obstacle
                else if (htr.Hit && htr.DisplayObject is SimObstacle)
                {
                    // set selected
                    this.selected = htr.DisplayObject;
                    this.selected.Selected = SelectionType.SingleSelected;
                    this.Simulation.simEngine.propertyGrid.SelectedObject = this.selected;

                    // check if can move
                    if (((SimObstacle)htr.DisplayObject).MoveAllowed)
                    {
                        // set dragging
                        isDragging = true;
                        Cursor.Current = Cursors.Hand;

                        // set temp
                        this.temporaryCoordinate = ((SimObstacle)this.selected).Position;
                    }

                    // redraw
                    this.Invalidate();
                }
                else
                {
                    controlTag = new Point(e.X, e.Y);
                    isDragging = true;
                    Cursor.Current = Cursors.Hand;
                }
            }

            #endregion

            #region Right

            else if (e.Button == MouseButtons.Right)
            {
                if (htr.Hit && htr.DisplayObject is CarDisplayObject)
                {
                    this.selected = htr.DisplayObject;
                    this.Simulation.simEngine.propertyGrid.SelectedObject = this.selected;
                    this.vehicleContextMenuStrip1.Show(this, e.X, e.Y);
                    ((CarDisplayObject)this.selected).Selected = SelectionType.SingleSelected;
                }
                else if (htr.Hit && htr.DisplayObject is SimObstacle)
                {
                    // set selected
                    this.selected = htr.DisplayObject;
                    this.selected.Selected = SelectionType.SingleSelected;
                    this.Simulation.simEngine.propertyGrid.SelectedObject = this.selected;

                    // check if we can move the obstacle
                    if (((SimObstacle)htr.DisplayObject).MoveAllowed)
                    {
                        // set dragging
                        isDragging = true;
                        Cursor.Current = Cursors.Hand;

                        // set temp
                        this.temporaryCoordinate = transform.GetWorldPoint(new PointF(e.X, e.Y)) - ((SimObstacle)this.selected).Position;
                    }

                    // redraw
                    this.Invalidate();
                }
            }

            #endregion

            base.OnMouseDown(e);
            this.Invalidate();
        }