public Intersection(Vector2 position, VNode aIn, VNode bIn, VNode aOut, VNode bOut)
 {
     this.aIn = aIn;
     this.bIn = bIn;
     this.aOut = aOut;
     this.bOut = bOut;
     this.Position = position;
 }
        private static VNode ProcessDataEvent(VDataEvent e, VNode Root, VoronoiGraph VG, double ys, out VDataNode[] CircleCheckList)
        {
            if (Root == null)
            {
                Root = new VDataNode(e.DataPoint);
                CircleCheckList = new VDataNode[] { (VDataNode)Root };
                return Root;
            }
            //Знаходимо вузол для заміщення
            VNode C = VNode.FindDataNode(Root, ys, e.DataPoint[0]);
            //Створюємо піддерево з одним ребром, але двома VEdgeNodes
            VoronoiEdge VE = new VoronoiEdge();
            VE.LeftData = ((VDataNode)C).DataPoint;
            VE.RightData = e.DataPoint;
            VE.VVertexA = Fortune.VVUnkown;
            VE.VVertexB = Fortune.VVUnkown;
            VG.Edges.Add(VE);

            VNode SubRoot;
            if (Math.Abs(VE.LeftData[1] - VE.RightData[1]) < 1e-10)
            {
                if (VE.LeftData[0] < VE.RightData[0])
                {
                    SubRoot = new VEdgeNode(VE, false);
                    SubRoot.Left = new VDataNode(VE.LeftData);
                    SubRoot.Right = new VDataNode(VE.RightData);
                }
                else
                {
                    SubRoot = new VEdgeNode(VE, true);
                    SubRoot.Left = new VDataNode(VE.RightData);
                    SubRoot.Right = new VDataNode(VE.LeftData);
                }
                CircleCheckList = new VDataNode[] { (VDataNode)SubRoot.Left, (VDataNode)SubRoot.Right };
            }
            else
            {
                SubRoot = new VEdgeNode(VE, false);
                SubRoot.Left = new VDataNode(VE.LeftData);
                SubRoot.Right = new VEdgeNode(VE, true);
                SubRoot.Right.Left = new VDataNode(VE.RightData);
                SubRoot.Right.Right = new VDataNode(VE.LeftData);
                CircleCheckList = new VDataNode[] { (VDataNode)SubRoot.Left, (VDataNode)SubRoot.Right.Left, (VDataNode)SubRoot.Right.Right };
            }

            //"Застосовуємо" піддерево
            if (C.Parent == null)
                return SubRoot;
            C.Parent.Replace(C, SubRoot);
            return Root;
        }
        private static VNode ProcessCircleEvent(VCircleEvent e, VNode Root, VoronoiGraph VG, double ys, out VDataNode[] CircleCheckList)
        {
            VDataNode a, b, c;
            VEdgeNode e1, e2;
            b = e.NodeN;
            a = VNode.LeftDataNode(b);
            c = VNode.RightDataNode(b);
            if (a == null || b.Parent == null || c == null || !a.DataPoint.Equals(e.NodeL.DataPoint) || !c.DataPoint.Equals(e.NodeR.DataPoint))
            {
                CircleCheckList = new VDataNode[] { };
                return Root; // повертаємось, бо графік змінився
            }
            e1 = (VEdgeNode)b.Parent;
            CircleCheckList = new VDataNode[] { a, c };
            //Створюємо нову вершину
            Vector VNew = new Vector(e.Center[0], e.Center[1]);
            VG.Vertizes.Add(VNew);
            //2. виясняємо, чи а або с знаходяться у віддаленій частині дерева (інший - брат b), і призначаємо нову вершину
            if (e1.Left == b) // c - брат
            {
                e2 = VNode.EdgeToRightDataNode(a);
                // замінюємо e1 правим нащадком
                e1.Parent.Replace(e1, e1.Right);
            }
            else // a - брат
            {
                e2 = VNode.EdgeToRightDataNode(b);

                // замінюємо e1 лівим нащадком
                e1.Parent.Replace(e1, e1.Left);
            }
            e1.Edge.AddVertex(VNew);
            e2.Edge.AddVertex(VNew);

            //Замінюємо e2 новим ребром
            VoronoiEdge VE = new VoronoiEdge();
            VE.LeftData = a.DataPoint;
            VE.RightData = c.DataPoint;
            VE.AddVertex(VNew);
            VG.Edges.Add(VE);

            VEdgeNode VEN = new VEdgeNode(VE, false);
            VEN.Left = e2.Left;
            VEN.Right = e2.Right;
            if (e2.Parent == null)
                return VEN;
            e2.Parent.Replace(e2, VEN);
            return Root;
        }
Example #4
0
        //Given seed probability P, find the best k nodes that can maximize influence spread
        public Tuple<List<int>, double> Greedy(int k, List<double> P)
        {
            HashSet<int> seeds = new HashSet<int> ();
            List<int> seedSet = new List<int> ();
            List<double> edgeW = new List<double> ();
            for (int h = 0; h < numS; ++h)
                edgeW.Add (1.0);

            //CELF Algorithm
            PriorityQueue<VNode> pq = new PriorityQueue<VNode> (numV+1, new VNodeComparer ());
            List<bool> update = new List<bool> ();
            for (int u = 0; u < numV; ++u)
            {
                VNode node = new VNode (u, numS);
                pq.Push (node);
                update.Add (false);
            }

            double total_gain = 0.0;
            for (int i = 0; i < k; ++i)
            {
                for (int u = 0; u < numV; ++u)
                    update [u] = false;
                int next = 0;
                double gain = 0;
                while (true)
                {
                    VNode node = pq.Pop ();
                    int max_u = node.id;

                    if (update [max_u])
                    {
                        next = max_u;
                        gain = node.val;
                        break;
                    }
                    else
                    {
                        double sum = 0;
                        if (i == 0)
                            sum = V2S[max_u].Count * P[max_u];
                        else
                        {
                            foreach (int sid in V2S[max_u])
                            sum += edgeW[sid] * P[max_u];
                        }
                        VNode n1 = new VNode (max_u, sum);
                        pq.Push (n1);
                        update [max_u] = true;
                    }
                }
                total_gain += gain;
                foreach (int sid in V2S[next])
                    edgeW [sid] = edgeW [sid] * (1 - P [next]);
                seeds.Add (next);
                seedSet.Add (next);
            }

            return new Tuple<List<int>, double> (seedSet, total_gain*numV/numS);
        }
Example #5
0
 protected internal virtual void PreSiblings(VNode vNode)
 {
 }
Example #6
0
            public virtual DocIdSet GetDocIdSet()
            {
                Debug.Assert(curVNode == null, "Called more than once?");
                if (m_termsEnum == null)
                {
                    return(null);
                }
                //advance
                if ((thisTerm = m_termsEnum.Next()) == null)
                {
                    return(null);// all done
                }

                curVNode = new VNode(null);
                curVNode.Reset(m_outerInstance.m_grid.WorldCell);

                Start();

                AddIntersectingChildren();

                while (thisTerm != null)//terminates for other reasons too!
                {
                    //Advance curVNode pointer
                    if (curVNode.children != null)
                    {
                        //-- HAVE CHILDREN: DESCEND

                        // LUCENENET NOTE: Must call this line before calling MoveNext()
                        // on the enumerator.

                        //if we put it there then it has something
                        PreSiblings(curVNode);

                        // LUCENENET IMPORTANT: Must not call this inline with Debug.Assert
                        // because the compiler removes Debug.Assert statements in release mode!!
                        bool hasNext = curVNode.children.MoveNext();
                        Debug.Assert(hasNext);

                        curVNode = curVNode.children.Current;
                    }
                    else
                    {
                        //-- NO CHILDREN: ADVANCE TO NEXT SIBLING
                        VNode parentVNode = curVNode.parent;
                        while (true)
                        {
                            if (parentVNode == null)
                            {
                                goto main_break;// all done
                            }
                            if (parentVNode.children.MoveNext())
                            {
                                //advance next sibling
                                curVNode = parentVNode.children.Current;
                                break;
                            }
                            else
                            {
                                //reached end of siblings; pop up
                                PostSiblings(parentVNode);
                                parentVNode.children = null;
                                //GC
                                parentVNode = parentVNode.parent;
                            }
                        }
                    }
                    //Seek to curVNode's cell (or skip if termsEnum has moved beyond)
                    curVNodeTerm.Bytes  = curVNode.cell.GetTokenBytes();
                    curVNodeTerm.Length = curVNodeTerm.Bytes.Length;
                    int compare = m_termsEnum.Comparer.Compare(thisTerm, curVNodeTerm);
                    if (compare > 0)
                    {
                        // leap frog (termsEnum is beyond where we would otherwise seek)
                        Debug.Assert(!m_context.AtomicReader.GetTerms(m_outerInstance.m_fieldName).GetIterator(null).SeekExact(curVNodeTerm), "should be absent");
                    }
                    else
                    {
                        if (compare < 0)
                        {
                            // Seek !
                            TermsEnum.SeekStatus seekStatus = m_termsEnum.SeekCeil(curVNodeTerm);
                            if (seekStatus == TermsEnum.SeekStatus.END)
                            {
                                break;// all done
                            }
                            thisTerm = m_termsEnum.Term;
                            if (seekStatus == TermsEnum.SeekStatus.NOT_FOUND)
                            {
                                continue; // leap frog
                            }
                        }
                        // Visit!
                        bool descend = Visit(curVNode.cell);
                        //advance
                        if ((thisTerm = m_termsEnum.Next()) == null)
                        {
                            break;// all done
                        }
                        if (descend)
                        {
                            AddIntersectingChildren();
                        }
                    }
                    ;
                }//main loop
                main_break : { }

                return(Finish());
            }
            /// <exception cref="System.IO.IOException"></exception>
            public virtual DocIdSet GetDocIdSet()
            {
                Debug.Assert(curVNode == null, "Called more than once?");
                if (termsEnum == null)
                {
                    return(null);
                }
                //advance
                if ((thisTerm = termsEnum.Next()) == null)
                {
                    return(null);
                }
                // all done
                curVNode = new VNode(null);
                curVNode.Reset(_enclosing.grid.WorldCell);
                Start();
                AddIntersectingChildren();
                while (thisTerm != null)
                {
                    //terminates for other reasons too!
                    //Advance curVNode pointer
                    if (curVNode.children != null)
                    {
                        //-- HAVE CHILDREN: DESCEND
                        Debug.Assert(curVNode.children.MoveNext());
                        //if we put it there then it has something
                        PreSiblings(curVNode);
                        curVNode = curVNode.children.Current;
                    }
                    else
                    {
                        //-- NO CHILDREN: ADVANCE TO NEXT SIBLING
                        VNode parentVNode = curVNode.parent;
                        while (true)
                        {
                            if (parentVNode == null)
                            {
                                goto main_break;
                            }
                            // all done
                            if (parentVNode.children.MoveNext())
                            {
                                //advance next sibling
                                curVNode = parentVNode.children.Current;
                                break;
                            }
                            else
                            {
                                //reached end of siblings; pop up
                                PostSiblings(parentVNode);
                                parentVNode.children = null;
                                //GC
                                parentVNode = parentVNode.parent;
                            }
                        }
                    }
                    //Seek to curVNode's cell (or skip if termsEnum has moved beyond)
                    curVNodeTerm.bytes  = curVNode.cell.GetTokenBytes().ToSByteArray();
                    curVNodeTerm.length = curVNodeTerm.bytes.Length;
                    int compare = termsEnum.Comparator.Compare(thisTerm, curVNodeTerm
                                                               );
                    if (compare > 0)
                    {
                        // leap frog (termsEnum is beyond where we would otherwise seek)
                        Debug.Assert(
                            !((AtomicReader)context.Reader).Terms(_enclosing.fieldName).Iterator(null).SeekExact(
                                curVNodeTerm, false), "should be absent"
                            );
                    }
                    else
                    {
                        if (compare < 0)
                        {
                            // Seek !
                            TermsEnum.SeekStatus seekStatus = termsEnum.SeekCeil(curVNodeTerm, true
                                                                                 );
                            if (seekStatus == TermsEnum.SeekStatus.END)
                            {
                                break;
                            }
                            // all done
                            thisTerm = termsEnum.Term;
                            if (seekStatus == TermsEnum.SeekStatus.NOT_FOUND)
                            {
                                continue;
                            }
                        }
                        // leap frog
                        // Visit!
                        bool descend = Visit(curVNode.cell);
                        //advance
                        if ((thisTerm = termsEnum.Next()) == null)
                        {
                            break;
                        }
                        // all done
                        if (descend)
                        {
                            AddIntersectingChildren();
                        }
                    }
                    ;
                }
main_break:
                ;
                //main loop
                return(Finish());
            }
Example #8
0
        public static VoronoiGraph ComputeVoronoiGraph(IEnumerable <Vector2> datapoints)
        {
            var   pq             = new List <VEvent>();
            var   currentCircles = new Hashtable();
            var   vg             = new VoronoiGraph();
            VNode rootNode       = null;

            foreach (var v in datapoints)
            {
                pq.Add(new VDataEvent(v));
            }
            pq.Sort();
            while (pq.Count > 0)
            {
                var ve = pq.First();
                pq.Remove(ve);
                if (ve == null)
                {
                    return(null);
                }
                VDataNode[] circleCheckList;
                if (ve is VDataEvent)
                {
                    rootNode = VNode.ProcessDataEvent(ve as VDataEvent, rootNode, vg, ve.Y, out circleCheckList);
                }
                else if (ve is VCircleEvent)
                {
                    currentCircles.Remove(((VCircleEvent)ve).NodeN);
                    if (!((VCircleEvent)ve).Valid)
                    {
                        continue;
                    }
                    rootNode = VNode.ProcessCircleEvent(ve as VCircleEvent, rootNode, vg, out circleCheckList);
                }
                else
                {
                    throw new Exception("Got event of type " + ve.GetType() + "!");
                }
                foreach (var vd in circleCheckList)
                {
                    if (currentCircles.ContainsKey(vd))
                    {
                        ((VCircleEvent)currentCircles[vd]).Valid = false;
                        currentCircles.Remove(vd);
                    }
                    var vce = VNode.CircleCheckDataNode(vd, ve.Y);
                    if (vce != null)
                    {
                        pq.Add(vce);
                        pq.Sort();
                        currentCircles[vd] = vce;
                    }
                }
                if (ve is VDataEvent)
                {
                    var dp = ((VDataEvent)ve).DataPoint;
                    foreach (VCircleEvent vce in currentCircles.Values)
                    {
                        if (Dist(dp[0], dp[1], vce.CenterX, vce.CenterY) < vce.Y - vce.CenterY && Math.Abs(Dist(dp[0], dp[1], vce.CenterX, vce.CenterY) - (vce.Y - vce.CenterY)) > 1e-10)
                        {
                            vce.Valid = false;
                        }
                    }
                }
            }
            VNode.CleanUpTree(rootNode);
            foreach (var ve in vg.GetEdges().Select(edge => edge as VoronoiEdge))
            {
                if (Edge.IsUnknownVertex(ve.V2))
                {
                    ve.AddVertex(Edge.GetInfiniteVertex());
                    if (Math.Abs(ve.LeftData[1] - ve.RightData[1]) < 1e-10 && ve.LeftData[0] < ve.RightData[0])
                    {
                        var T = ve.LeftData;
                        ve.LeftData  = ve.RightData;
                        ve.RightData = T;
                    }
                }
            }

            var minuteEdges = new ArrayList();

            foreach (var ve in vg.GetEdges())
            {
                if (!ve.IsPartlyInfinite && ve.V1.Equals(ve.V2))
                {
                    minuteEdges.Add(ve);
                    foreach (var ve2 in vg.GetEdges())
                    {
                        if (ve2.V1.Equals(ve.V1))
                        {
                            ve2.V1 = ve.V1;
                        }
                        if (ve2.V2.Equals(ve.V1))
                        {
                            ve2.V2 = ve.V1;
                        }
                    }
                }
            }
            foreach (VoronoiEdge ve in minuteEdges)
            {
                vg.RemoveEdge(ve, false);
            }

            return(vg);
        }
        private static int SplitAtColisions(IList <VNode> previousLayer, int j, VNode current, VNode previous)
        {
            var startCollides =
                current.Index > previous.Index &&
                current.Index < previous.Index + previous.Length;

            if (startCollides)
            {
                var partAfter = previous.SplitAt(current.Index);
                j++;
                previousLayer.Insert(j, partAfter);
                previous = partAfter;
            }

            var endsCollides =
                current.Index + current.Length > previous.Index &&
                current.Index + current.Length < previous.Index + previous.Length;

            if (endsCollides)
            {
                var partAfter = previous.SplitAt(current.Index + current.Length);
                j++;
                previousLayer.Insert(j, partAfter);
            }
            return(j);
        }
Example #10
0
 public static VNode WithOnclick(this VNode node, Action onclick)
 {
     node.OnClick = onclick;
     return(node);
 }
            internal Cell cell;                    //not null (except initially before reset())

            /// <summary>Call <see cref="Reset(Cell)"/> after to set the cell.</summary>
            internal VNode(VNode parent)
            {
                // remember to call reset(cell) after
                this.parent = parent;
            }
            public LList(Vector2[] poly)
            {
                First = new VNode(poly[0], null, null);

                current = First;
                for (int i = 1; i < poly.Length; i++)
                {
                    Add(current, poly[i]);
                    current = current.Next;
                }
                current = First;
            }
 public VNode(Vector2 value, VNode next, VNode prev)
 {
     Value = value;
     Next = next;
     Prev = prev;
 }
            public Vector2[] ToArray()
            {
                List<Vector2> ret = new List<Vector2>();
                current = First;
                int timeout = 1000;
                bool starting = true;

                while (current != null)
                {
                    if (current.Prev != null && current.Value == current.Prev.Value)
                    {
                        current = current.Next;
                        if (current.Value == current.Next.Value && current.Value == current.Prev.Value) break;
                        continue;
                    }
                    if (!starting && current.Value == First.Value) break;
                    starting = false;

                    ret.Add(current.Value);
                    current = current.Next;

                    timeout--;
                    if (timeout <= 0) break;
                }
                return Simplify(ret.ToArray());
            }
 private void Add(VNode prev, Vector2 pos)
 {
     prev.Next = new VNode(pos, null, prev);
 }
Example #16
0
        public VoronoiGraph ComputeVoronoiGraph()
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            var veronoiGraphBox = new VoronoiGraph();
            //Hashtable CurrentCircles = new Hashtable();
            var   CurrentCircles = new Dictionary <VDataNode, VCircleEvent>();
            VNode RootNode       = null;

            foreach (Vector v in Datapoints)
            {
                DataPq.Push(new VDataEvent(v));
            }

            while (DataPq.Count > 0)
            {
                var         vertexEvent = DataPq.Pop() as VEvent;
                VDataNode[] circleCheckList;

                if (vertexEvent is VDataEvent)
                {
                    RootNode = EventHandler.ProcessDataEvent(vertexEvent as VDataEvent, RootNode, veronoiGraphBox, vertexEvent.Y, out circleCheckList);
                }
                else if (vertexEvent is VCircleEvent)
                {
                    CurrentCircles.Remove(((VCircleEvent)vertexEvent).NodeN);
                    if (!((VCircleEvent)vertexEvent).Valid)
                    {
                        continue;
                    }
                    RootNode = EventHandler.ProcessCircleEvent(vertexEvent as VCircleEvent, RootNode, veronoiGraphBox, vertexEvent.Y, out circleCheckList);
                }
                else
                {
                    throw new Exception("Got event of type " + vertexEvent.GetType() + "!");
                }

                foreach (var vd in circleCheckList)
                {
                    if (CurrentCircles.ContainsKey(vd))
                    {
                        CurrentCircles[vd].Valid = false;
                        CurrentCircles.Remove(vd);
                    }

                    var vce = Methods.CircleCheckDataNode(vd, vertexEvent.Y);

                    if (vce != null)
                    {
                        DataPq.Push(vce);
                        CurrentCircles[vd] = vce;
                    }
                }

                if (vertexEvent is VDataEvent)
                {
                    var dp = ((VDataEvent)vertexEvent).DataPoint;
                    foreach (VCircleEvent vce in CurrentCircles.Values)
                    {
                        if (Methods.Distance(dp[0], dp[1], vce.Center[0], vce.Center[1]) < vce.Y - vce.Center[1] && Math.Abs(Methods.Distance(dp[0], dp[1], vce.Center[0], vce.Center[1]) - (vce.Y - vce.Center[1])) > 1e-10)
                        {
                            vce.Valid = false;
                        }
                    }
                }
            }

            Methods.CleanUpTree(RootNode);
            foreach (VoronoiEdge ve in veronoiGraphBox.Edges.Where(x => !x.Done && x.VVertexB == Constants.VVUnkown))
            {
                ve.AddVertex(Constants.VVInfinite);
                if (Math.Abs(ve.LeftData[1] - ve.RightData[1]) < 1e-10 && ve.LeftData[0] < ve.RightData[0])
                {
                    Vector T = ve.LeftData;
                    ve.LeftData  = ve.RightData;
                    ve.RightData = T;
                }
            }

            var minuteEdges = new List <VoronoiEdge>();

            foreach (VoronoiEdge ve in veronoiGraphBox.Edges.Where(ve => !ve.IsPartlyInfinite && ve.VVertexA.Equals(ve.VVertexB)))
            {
                minuteEdges.Add(ve);
                // prevent rounding errors from expanding to holes
                foreach (VoronoiEdge ve2 in veronoiGraphBox.Edges)
                {
                    if (ve2.VVertexA.Equals(ve.VVertexA))
                    {
                        ve2.VVertexA = ve.VVertexA;
                    }
                    if (ve2.VVertexB.Equals(ve.VVertexA))
                    {
                        ve2.VVertexB = ve.VVertexA;
                    }
                }
            }
            foreach (VoronoiEdge ve in minuteEdges)
            {
                veronoiGraphBox.Edges.Remove(ve);
            }


            stopWatch.Stop();


            return(veronoiGraphBox);
        }