/// <summary>
        /// Creates the justified graph using a standard BFS algorithm.
        /// </summary>
        public void CreateJGraph()
        {
            #region setting the Nodes
            HashSet <UV> vertexPoints = new HashSet <UV>();
            foreach (Node node in this.JGNodes)
            {
                node.Clear();
            }
            Node.LineToUVGuide.Clear();
            this.JGNodes.Clear();
            foreach (JGEdge edge in this.edges)
            {
                this.JGNodes.Add(new Node(edge.P1));
                this.JGNodes.Add(new Node(edge.P2));
            }
            foreach (Node node in this.JGNodes)
            {
                vertexPoints.Add(node.Coordinates);
                node.Draw();
            }
            #endregion

            this.jgGraph = new JGGraph(vertexPoints);
            foreach (JGEdge edge in this.edges)
            {
                this.jgGraph.AddConnection(edge);
            }
        }
 private void CreateConvexGraph_Click(object sender, RoutedEventArgs e)
 {
     if ((string)this.CreateCovexGraph.Header == "Create Convex Graph")
     {
         this.Visibility = System.Windows.Visibility.Visible;
         #region clearing the nodes and edges in case of reset
         foreach (Node node in this.JGNodes)
         {
             node.Clear();
         }
         Node.LineToUVGuide.Clear();
         this.JGNodes.Clear();
         JGEdge.LineToEdgeGuide.Clear();
         foreach (JGEdge edge in this.edges)
         {
             edge.Clear();
         }
         this.edges.Clear();
         #endregion
         this._host.Menues.IsEnabled     = false;
         this._host.CommandReset.Click  += endBtn_Click;
         this._host.UIMessage.Text       = "Select points at the center of convex spaces";
         this._host.UIMessage.Visibility = System.Windows.Visibility.Visible;
         //this.JGraphMode = true;
         this._host.Cursor = Cursors.Pen;
         this._host.FloorScene.MouseLeftButtonDown += FloorScene_MouseLeftButtonDown;
     }
     else
     {
         this.Hide_show_Menu.IsEnabled = false;
         this.EditGraph.IsEnabled      = false;
         this.DrawJG.IsEnabled         = false;
         this.CreateCovexGraph.Header  = "Create Convex Graph";
         this.Children.Clear();
         //resetting the graph
         this.JGNodes.Clear();
         this.jgGraph = null;
         this.edges.Clear();
         this.nodeRemovingMode = false;
         edgeLine       = null;
         node1          = null;
         node2          = null;
         edgeRemoveMode = false;
         relatedEdges   = null;
         root           = null;
         JGHierarchy    = null;
     }
 }
 public void SetGraph(JGGraph _JGGraph, List <HashSet <JGVertex> > _hierarchy)
 {
     this.jgGraph     = _JGGraph;
     this.JGHierarchy = _hierarchy;
 }
 /// <summary>
 /// Clears this instance.
 /// </summary>
 public void Clear()
 {
     this._host = null;
     foreach (Node item in JGNodes)
     {
         item.Clear();
     }
     JGNodes.Clear();
     Node.LineToUVGuide.Clear();
     foreach (JGEdge item in edges)
     {
         item.Clear();
     }
     edges.Clear();
     JGEdge.LineToEdgeGuide.Clear();
     this.CreateCovexGraph.Click -= CreateConvexGraph_Click;
     this.Hide_show_Menu.Click   -= Hide_show_Menu_Click;
     this.AddVertex.Click        -= AddVertex_Click;
     this.RemoveVertex.Click     -= RemoveVertex_Click;
     this.MoveVertex.Click       -= MoveVertex_Click;
     this.AddEdge.Click          -= AddEdge_Click;
     this.RemoveEdge.Click       -= RemoveEdge_Click;
     this.DrawJG.Click           -= DrawJG_Click;
     this.JustifiedGraphMenu      = null;
     this.CreateCovexGraph        = null;
     this.EditGraph      = null;
     this.AddVertex      = null;
     this.RemoveVertex   = null;
     this.MoveVertex     = null;
     this.AddEdge        = null;
     this.RemoveEdge     = null;
     this.DrawJG         = null;
     this.Hide_show_Menu = null;
     if (this.JGNodes != null)
     {
         this.JGNodes.Clear();
         this.JGNodes = null;
     }
     this.jgGraph = null;
     if (this.edges != null)
     {
         this.edges.Clear();
         this.edges = null;
     }
     this.edgeLine = null;
     this.node1    = null;
     this.node2    = null;
     if (this.relatedEdges != null)
     {
         this.relatedEdges.Clear();
         this.relatedEdges = null;
     }
     this.root = null;
     if (this.JGHierarchy != null)
     {
         this.JGHierarchy.Clear();
         this.JGHierarchy = null;
     }
     Node.FloorScene   = null;
     Node.Transform    = null;
     JGEdge.FloorScene = null;
     JGEdge.Transform  = null;
 }
        private void endBtn_Click(object sender, RoutedEventArgs e)
        {
            this._host.CommandReset.Click  -= endBtn_Click;
            this._host.Menues.IsEnabled     = true;
            this._host.UIMessage.Visibility = Visibility.Hidden;
            //this.JGraphMode = false;
            this._host.Cursor = Cursors.Arrow;
            this._host.FloorScene.MouseLeftButtonDown -= FloorScene_MouseLeftButtonDown;
            try
            {
                #region Create Graph
                HashSet <UV>         pnts               = new HashSet <UV>();
                TriangleNet.Behavior behavior           = new Behavior();
                TriangleNet.Mesh     t_mesh             = new TriangleNet.Mesh();
                TriangleNet.Geometry.InputGeometry geom = new TriangleNet.Geometry.InputGeometry();
                foreach (Node node in JGNodes)
                {
                    TriangleNet.Data.Vertex vertex = new TriangleNet.Data.Vertex(node.Coordinates.U, node.Coordinates.V, 0);
                    geom.AddPoint(vertex);
                    pnts.Add(node.Coordinates);
                }
                t_mesh.Triangulate(geom);

                var graph = new JGGraph(pnts);
                foreach (var item in t_mesh.Triangles)
                {
                    UV  a    = null;
                    var vrtx = t_mesh.GetVertex(item.P0);
                    if (vrtx != null)
                    {
                        a = new UV(vrtx.X, vrtx.Y);
                    }
                    UV b = null;
                    vrtx = t_mesh.GetVertex(item.P1);
                    if (vrtx != null)
                    {
                        b = new UV(vrtx.X, vrtx.Y);
                    }
                    UV c = null;
                    vrtx = t_mesh.GetVertex(item.P2);
                    if (vrtx != null)
                    {
                        c = new UV(vrtx.X, vrtx.Y);
                    }
                    if (a != null && b != null)
                    {
                        graph.AddConnection(a, b);
                    }
                    if (a != null && c != null)
                    {
                        graph.AddConnection(a, c);
                    }
                    if (c != null && b != null)
                    {
                        graph.AddConnection(c, b);
                    }
                }
                #endregion
                #region Remove Edges with isovists at the ends that do not overlap
                this.edges = graph.ToEdges();
                Dictionary <int, Isovist> IsovistGuid = new Dictionary <int, Isovist>();
                foreach (JGVertex item in graph.Vertices)
                {
                    double x = double.NegativeInfinity;
                    foreach (JGVertex vertex in item.Connections)
                    {
                        var y = item.Point.DistanceTo(vertex.Point);
                        if (y > x)
                        {
                            x = y;
                        }
                    }
                    var isovist = CellularIsovistCalculator.GetIsovist(item.Point, x, BarrierType.Visual, this._host.cellularFloor);
                    IsovistGuid.Add(item.Point.GetHashCode(), isovist);
                }
                HashSet <JGEdge> visibleVertexes = new HashSet <JGEdge>();
                foreach (JGEdge item in this.edges)
                {
                    Isovist v1 = null;
                    IsovistGuid.TryGetValue(item.P1.GetHashCode(), out v1);
                    Isovist v2 = null;
                    IsovistGuid.TryGetValue(item.P2.GetHashCode(), out v2);
                    if (v1 != null && v2 != null)
                    {
                        if (v2.VisibleCells.Overlaps(v1.VisibleCells))
                        {
                            visibleVertexes.Add(item);
                        }
                    }
                }
                #endregion
                #region setting the edges
                JGEdge.LineToEdgeGuide.Clear();
                foreach (JGEdge edge in this.edges)
                {
                    edge.Clear();
                }
                this.edges = visibleVertexes.ToList <JGEdge>();
                foreach (JGEdge item in this.edges)
                {
                    item.Draw();
                }
                #endregion
                //cleaning up the used data
                t_mesh = null;
                graph  = null;
                geom.Clear();
                geom            = null;
                visibleVertexes = null;
                IsovistGuid     = null;
                //enabling edit mode
                this.EditGraph.IsEnabled      = true;
                this.DrawJG.IsEnabled         = true;
                this.Hide_show_Menu.IsEnabled = true;
                this.CreateCovexGraph.Header  = "Reset Convex Graph";
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Report());
            }
        }