public Tuple <CoverNode <T>, SpatialObj <T> > IncrementalNNFindNext()
        {
            if (incrNN_queue is null)
            {
                return(null);
            }

            while (incrNN_queue.Count > 0)
            {
                NodeOrObj current_element = incrNN_queue.Dequeue();
                while (incrNN_queue.Count > 0 && incrNN_queue.First().Equals(current_element))
                {
                    incrNN_queue.Dequeue();
                }

                if (current_element.IsObj())
                {
                    return(Tuple.Create(current_element.GetNode(), current_element.GetObj()));
                }
                else
                {
                    CoverNode <T> current_node = current_element.GetNode();
                    double        current_dist = current_node.GetDistanceToPointSq(incrNN_origin);

                    if (!current_node.IsEmpty())
                    {
                        foreach (SpatialObj <T> obj in current_node.GetAllObjects())
                        {
                            double distance = obj.boundingBox.GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj obj_nodeOrObj = new NodeOrObj();
                                obj_nodeOrObj.SetNode(current_node);
                                obj_nodeOrObj.SetObj(obj);
                                incrNN_queue.Enqueue(obj_nodeOrObj, (float)distance);
                            }
                        }
                    }
                    if (current_node.HasChildren())
                    {
                        foreach (CoverNode <T> child_node in current_node.GetChildren())
                        {
                            double distance = child_node.GetDistanceToPointSq(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj node_nodeOrObj = new NodeOrObj();
                                node_nodeOrObj.SetNode(child_node);
                                incrNN_queue.Enqueue(node_nodeOrObj, (float)distance);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Пример #2
0
        public List <RectangleObj> IncrementalNNFindNext()
        {
            // 1st entry is the object found, the rest are the bounding boxes of circular search steps.
            List <RectangleObj> answer = new List <RectangleObj>();
            Random rand = new Random();

            while (incrNN_queue.Count > 0)
            {
                NodeOrObj current_element = incrNN_queue.Dequeue();
                while (incrNN_queue.Count > 0 && incrNN_queue.First().Equals(current_element))
                {
                    incrNN_queue.Dequeue();
                }

                if (current_element.IsObj())
                {
                    double       dist      = current_element.GetObj().GetDistanceSqToPoint(incrNN_origin);
                    RectangleObj circleObj = new RectangleObj();
                    int          radius    = (int)(Math.Sqrt(dist) * 2.0);
                    circleObj.SetRectangleByCenter(incrNN_origin, radius, radius);
                    circleObj.rect_color = Color.Orange;
                    answer.Add(circleObj);

                    answer.Insert(0, current_element.GetObj());
                    return(answer);
                }
                else
                {
                    NodeType     current_node = current_element.GetNode();
                    double       current_dist = current_node.GetBounds().GetDistanceSqToPoint(incrNN_origin);
                    RectangleObj circleObj    = new RectangleObj();
                    int          radius       = (int)(Math.Sqrt(current_dist) * 2.0);
                    circleObj.SetRectangleByCenter(incrNN_origin, radius, radius);
                    Color col = Color.FromArgb(0, rand.Next(50, 256), rand.Next(50, 256));
                    circleObj.rect_color = col;

                    answer.Add(circleObj);
                    if (!current_node.IsEmpty())
                    {
                        foreach (RectangleObj obj in current_node.GetAllStoredObjs())
                        {
                            double distance = obj.GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj obj_nodeOrObj = new NodeOrObj();
                                obj_nodeOrObj.SetObj(obj);
                                incrNN_queue.Enqueue(obj_nodeOrObj, (float)distance);
                            }
                        }
                    }
                    if (current_node.HasChildren())
                    {
                        foreach (KeyValuePair <int, NodeType> child_node in current_node.getChildern())
                        {
                            double distance = child_node.Value.GetBounds().GetDistanceSqToPoint(incrNN_origin);
                            if (distance >= current_dist)
                            {
                                NodeOrObj node_nodeOrObj = new NodeOrObj();
                                node_nodeOrObj.SetNode(child_node.Value);
                                incrNN_queue.Enqueue(node_nodeOrObj, (float)distance);
                            }
                        }
                    }
                }
            }
            return(answer);
        }