public List <RectangleObj> WindowQuery(RectangleObj window)
        {
            // Finds objects within a Rectangular range (window)
            List <RectangleObj> answer          = new List <RectangleObj>();
            Queue <NodeType>    searching_nodes = new Queue <NodeType>();

            searching_nodes.Enqueue(root);
            while (searching_nodes.Count > 0)
            {
                NodeType current_node = searching_nodes.Dequeue();
                if (current_node.GetBounds().IntersectsWith(window))
                {
                    answer.AddRange(current_node.GetRangeQueryObj(window));
                    if (current_node.HasChildren())
                    {
                        foreach (KeyValuePair <int, NodeType> child in current_node.getChildern())
                        {
                            if (!searching_nodes.Contains(child.Value))
                            {
                                searching_nodes.Enqueue(child.Value);
                            }
                        }
                    }
                }
            }
            return(answer);
        }
Exemple #2
0
        private void btnINNNext_Click(object sender, EventArgs e)
        {
            if (GetCurrentActionState() != ActionMode.IncrementalNN)
            {
                return;
            }
            List <RectangleObj> iin;

            if (type_partition)
            {
                iin = rect_database_partition.IncrementalNNFindNext();
            }
            else
            {
                iin = rect_database_cover.IncrementalNNFindNext();
            }

            if (iin.Count > 0)
            {
                RectangleObj result = iin[0];
                iin.RemoveAt(0);

                dbCanvas.ClearHollowRectList();
                dbCanvas.AddHollowRect(result);
                dbCanvas.AddHollowCircles(iin);
            }
            dbCanvas.RedrawCanvas();
        }
        public NodeType FindContainingField(RectangleObj rect, NodeType starting_node)
        {
            Point node_center = starting_node.getCenter();
            int   node_size   = starting_node.getNodeSize();

            if (starting_node.IsPointInNode(rect.rect_center))
            {
                bool contained_in_node = rect.ContainedByArea(node_center, node_size, node_size);
                if (contained_in_node)
                {
                    if (node_size <= 1 || !starting_node.HasChildren() || rect.importance_layer == starting_node.GetLayerlevel() || Math.Max(rect.rect_width, rect.rect_height) > node_size / 2)
                    {
                        return(starting_node);
                    }
                }
                NodeType child_node = starting_node.LookupChild(rect.rect_center);

                if (child_node != null)
                {
                    NodeType answer_node = FindContainingField(rect, child_node);
                    if (answer_node != null)
                    {
                        return(answer_node);
                    }
                }
                if (contained_in_node)
                {
                    return(starting_node);
                }
            }
            return(default(NodeType));
        }
        private List <ObjAndNode <NodeType> > FindNearestRectToPoint(Point p, NodeType node)
        {
            List <ObjAndNode <NodeType> >  answer_list     = new List <ObjAndNode <NodeType> >();
            SimplePriorityQueue <NodeType> searching_nodes = new SimplePriorityQueue <NodeType>();

            searching_nodes.Enqueue(node, 0);

            RectangleObj answer      = null;
            NodeType     answer_node = default(NodeType);

            double min_distance_sq = region_width * region_width;

            while (searching_nodes.Count > 0)
            {
                NodeType current_node = searching_nodes.Dequeue();
                Dictionary <RectangleObj, double> nearest_rect = current_node.GetNearestRectangle(p);
                if (nearest_rect.Count > 0)
                {
                    foreach (KeyValuePair <RectangleObj, double> entry in nearest_rect)
                    {
                        if (entry.Value <= min_distance_sq || entry.Value < 0.01)
                        {
                            min_distance_sq = entry.Value;
                            answer          = entry.Key;
                            answer_node     = current_node;
                            if (min_distance_sq < 0.01)
                            {
                                ObjAndNode <NodeType> ans = new ObjAndNode <NodeType>();
                                ans.node = answer_node;
                                ans.rect = answer;
                                answer_list.Add(ans);
                                answer = null;
                            }
                        }
                    }
                }
                if (current_node.HasChildren())
                {
                    foreach (KeyValuePair <int, NodeType> child in current_node.getChildern())
                    {
                        NodeType     child_node = child.Value;
                        RectangleObj field_rect = new RectangleObj();
                        field_rect.SetRectangleByCenter(child_node.getCenter(), child_node.getNodeSize(), child_node.getNodeSize());
                        double field_dist = field_rect.GetDistanceSqToPoint(p);
                        if (field_dist <= min_distance_sq)
                        {
                            searching_nodes.Enqueue(child_node, (float)field_dist);
                        }
                    }
                }
            }
            if (answer != null)
            {
                ObjAndNode <NodeType> ans = new ObjAndNode <NodeType>();
                ans.node = answer_node;
                ans.rect = answer;
                answer_list.Add(ans);
            }
            return(answer_list);
        }
Exemple #5
0
 private void DbCanvas_rectAdded(object sender, RectangleObj e)
 {
     if (GetCurrentActionState() == ActionMode.Add)
     {
         e.importance_layer = Convert.ToInt32(tbImportance.Text);
         if (type_partition)
         {
             rect_database_partition.AddRectangle(e);
         }
         else
         {
             rect_database_cover.AddRectangle(e);
         }
     }
     else if (GetCurrentActionState() == ActionMode.WindowQuery)
     {
         List <RectangleObj> objs;
         Stopwatch           watch = Stopwatch.StartNew();
         if (type_partition)
         {
             objs = rect_database_partition.WindowQuery(e);
         }
         else
         {
             objs = rect_database_cover.WindowQuery(e);
         }
         watch.Stop();
         tbStopwatch.Text = watch.ElapsedMilliseconds.ToString();
         if (objs.Count > 0)
         {
             dbCanvas.ClearHollowRectList();
             dbCanvas.AddHollowRects(objs);
         }
     }
     else // Range query
     {
         Point p      = e.rect_center;
         int   radius = Math.Max(e.rect_height, e.rect_width) / 2;
         List <RectangleObj> objs;
         Stopwatch           watch = Stopwatch.StartNew();
         if (type_partition)
         {
             objs = rect_database_partition.RangeQuery(p, radius);
         }
         else
         {
             objs = rect_database_cover.RangeQuery(p, radius);
         }
         watch.Stop();
         tbStopwatch.Text = watch.ElapsedMilliseconds.ToString();
         if (objs.Count > 0)
         {
             dbCanvas.ClearHollowRectList();
             dbCanvas.AddHollowRects(objs);
         }
     }
     UpdateCanvasBounds();
 }
 public PartitionNode(int node_layer, int size, Point center_coord, int cap)
 {
     Parents  = new Dictionary <int, PartitionNode>();
     Children = new Dictionary <int, PartitionNode>();
     layerNum = node_layer;
     bounds   = new RectangleObj();
     bounds.SetRectangleByCenter(center_coord, size, size);
     capacity = cap;
 }
Exemple #7
0
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if ((action_status == ActionMode.Add || action_status == ActionMode.RangeQuery || action_status == ActionMode.WindowQuery) && is_draggin)
            {
                PointF prevPos = ConvertCoordFromScreen(mouse_down_pos.X, mouse_down_pos.Y);
                PointF curPos  = ConvertCoordFromScreen(current_mouse_pos.X, current_mouse_pos.Y);

                Point pPos = new Point((int)prevPos.X, (int)prevPos.Y);
                Point cPos = new Point((int)curPos.X, (int)curPos.Y);

                RectangleObj newRectangle = new RectangleObj();
                newRectangle.SetRectangleByCoords(pPos, cPos);

                if (action_status == ActionMode.RangeQuery)
                {
                    int rad = Math.Max(newRectangle.rect_height, newRectangle.rect_width);
                    newRectangle.UpdateWH(rad, rad);
                }

                newRectangle.rect_color       = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                newRectangle.importance_layer = -1;

                OnRectangleAdded(newRectangle);

                is_draggin = false;
                RedrawCanvas();
            }
            else if (action_status == ActionMode.PointQuery || action_status == ActionMode.Remove || action_status == ActionMode.IncrementalNN)
            {
                PointF curPos = ConvertCoordFromScreen(e.Location.X, e.Location.Y);
                OnPointQueryRequested(new Point((int)curPos.X, (int)curPos.Y));
                if (action_status == ActionMode.IncrementalNN)
                {
                    incrementalSet = true;
                    incrementalPos = e.Location;
                }
                else
                {
                    incrementalSet = false;
                }
            }
            else if (action_status == ActionMode.Move)
            {
                is_draggin = false;
                OnPointQueryRequested(new Point(-1, -1));
                RedrawCanvas();
            }
        }
Exemple #8
0
        public bool StoreRectangle(RectangleObj rect)
        {
            // Only potential Objs then can sink further go into overflow
            bool overflow = Math.Max(rect.rect_width, rect.rect_height) <= getNodeSize() / 2 && rect.importance_layer > layerNum;

            if (overflow)
            {
                overflowObjs.Add(rect);
            }
            else
            {
                storedObjs.Add(rect);
            }
            return(IsOverflown());
        }
        public List <RectangleObj> FindNearestObj(Point p)
        {
            List <RectangleObj> results = new List <RectangleObj>();

            foreach (ObjAndNode <NodeType> entry in FindNearestRectToPoint(p, root))
            {
                RectangleObj rect1 = new RectangleObj(entry.rect);
                rect1.rect_color = Color.Red;
                RectangleObj rect2 = new RectangleObj(entry.node.GetBounds());
                rect2.rect_color = Color.Red;
                results.Add(rect1);
                results.Add(rect2);
            }
            return(results);
        }
        public void InitMoving(Point p)
        {
            RectangleObj obj  = new RectangleObj();
            NodeType     node = default(NodeType);

            foreach (ObjAndNode <NodeType> entry in FindNearestRectToPoint(p, root))
            {
                obj  = entry.rect;
                node = entry.node;
            }

            if (node != null)
            {
                moving_node = node;
                moving_obj  = obj;
            }
        }
        public RectangleObj FindNearestObjAndRemove(Point p)
        {
            RectangleObj obj  = new RectangleObj();
            NodeType     node = default(NodeType);

            foreach (ObjAndNode <NodeType> entry in FindNearestRectToPoint(p, root))
            {
                obj  = entry.rect;
                node = entry.node;
            }

            if (node != null)
            {
                return(RemoveRectangle(obj, node));
            }
            return(obj);
        }
Exemple #12
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (canvas_size < 0)
            {
                return;
            }

            int numRects = Convert.ToInt32(tbNumRects.Text);
            int randSeed = Convert.ToInt32(tbRandSeed.Text);

            Random rand = new Random(randSeed);

            List <RectangleObj> rects = new List <RectangleObj>();

            for (int i = 0; i < numRects; i++)
            {
                int          x1   = rand.Next(1, canvas_size);
                int          y1   = rand.Next(1, canvas_size);
                int          x2   = rand.Next(1, canvas_size);
                int          y2   = rand.Next(1, canvas_size);
                RectangleObj rect = new RectangleObj();
                rect.SetRectangleByCoords(new Point(x1, y1), new Point(x2, y2));
                rect.importance_layer = Convert.ToInt32(tbImportance.Text);
                rect.rect_color       = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
                rects.Add(rect);
            }

            Stopwatch watch = Stopwatch.StartNew();

            if (type_partition)
            {
                rect_database_partition.AddManyRectangles(rects);
            }
            else
            {
                rect_database_cover.AddManyRectangles(rects);
            }
            watch.Stop();
            tbStopwatch.Text = watch.ElapsedMilliseconds.ToString();

            UpdateCanvasBounds();
        }
Exemple #13
0
        public List <RectangleObj> GetRangeQueryObj(RectangleObj window)
        {
            List <RectangleObj> objs = new List <RectangleObj>();

            foreach (RectangleObj rect in storedObjs)
            {
                if (rect.ContainedByArea(window.rect_center, window.rect_width, window.rect_height))
                {
                    objs.Add(rect);
                }
            }
            foreach (RectangleObj rect in overflowObjs)
            {
                if (rect.ContainedByArea(window.rect_center, window.rect_width, window.rect_height))
                {
                    objs.Add(rect);
                }
            }
            return(objs);
        }
Exemple #14
0
        public Dictionary <RectangleObj, double> GetNearestRectangle(Point p)
        {
            Dictionary <RectangleObj, double> answer = new Dictionary <RectangleObj, double>();

            double       min_dist     = -1.0;
            RectangleObj nearest_rect = null;

            foreach (RectangleObj rect in storedObjs)
            {
                double dist = rect.GetDistanceSqToPoint(p);
                if (dist <= min_dist || nearest_rect == null)
                {
                    nearest_rect = rect;
                    min_dist     = dist;
                    if (min_dist < 0.01)
                    {
                        answer.Add(nearest_rect, min_dist);
                    }
                }
            }
            foreach (RectangleObj rect in overflowObjs)
            {
                double dist = rect.GetDistanceSqToPoint(p);
                if (dist <= min_dist || nearest_rect == null)
                {
                    nearest_rect = rect;
                    min_dist     = dist;
                    if (min_dist < 0.01)
                    {
                        answer.Add(nearest_rect, min_dist);
                    }
                }
            }

            if (nearest_rect != null && answer.Count < 1)
            {
                answer.Add(nearest_rect, min_dist);
            }
            return(answer);
        }
Exemple #15
0
 public void DeleteRectangle(RectangleObj rect, bool overflow_only = false)
 {
     if (!overflow_only)
     {
         for (int i = 0; i < storedObjs.Count; i++)
         {
             if (rect.IsEqual(storedObjs[i]))
             {
                 storedObjs.RemoveAt(i);
                 return;
             }
         }
     }
     for (int i = 0; i < overflowObjs.Count; i++)
     {
         if (rect.IsEqual(overflowObjs[i]))
         {
             overflowObjs.RemoveAt(i);
             return;
         }
     }
 }
Exemple #16
0
        public CoverNode(int size, Point center_coord, int cap, CoverNode node_parent, double p_value)
        {
            children        = new Dictionary <int, CoverNode>();
            bounds          = new RectangleObj();
            original_bounds = new RectangleObj();
            capacity        = cap;
            parent          = node_parent;
            double expanded_size = (1.0 + p_value) * size;

            original_bounds.SetRectangleByCenter(center_coord, size, size);
            bounds.SetRectangleByCenter(center_coord, (int)expanded_size, (int)expanded_size);
            pVal = p_value;

            if (node_parent == null)
            {
                layerNum = 0;
            }
            else
            {
                layerNum = node_parent.layerNum + 1;
            }
        }
        public NodeType AddRectangle(RectangleObj rect, bool reorganize = true)
        {
            NodeType deepest_field = FindContainingField(rect, root);

            if (deepest_field.IsFull() && !deepest_field.HasAllChildren())
            {
                PartitionField(deepest_field);
                NodeType node = AddRectangle(rect);
                if (reorganize)
                {
                    ReorganizeOverflownNodes();
                }
                if (node != null)
                {
                    return(node);
                }
            }
            else
            {
                bool overflown = deepest_field.StoreRectangle(rect);
            }
            return(deepest_field);
        }
Exemple #18
0
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (all_layer_bounds.Count < 1)
            {
                return;
            }

            foreach (LayerBounds bound in all_layer_bounds)
            {
                int   colval    = ((bound.layerNum) * 220) / num_layers;
                int   pen_width = Math.Max(1, 5 - bound.layerNum);
                Brush br        = new SolidBrush(Color.FromArgb(colval, colval, colval));
                Pen   pen       = new Pen(br, pen_width);

                PointF p1 = ConvertCoordToScreen(bound.bounds.X, bound.bounds.Y);
                PointF p2 = ConvertCoordToScreen(bound.bounds.Width, bound.bounds.Height);

                e.Graphics.DrawRectangle(pen, p1.X, p1.Y, p2.X, p2.Y);
            }

            if (is_draggin && action_status != ActionMode.Move)
            {
                Brush br = new SolidBrush(Color.Black);

                PointF prevPos = ConvertCoordFromScreen(mouse_down_pos.X, mouse_down_pos.Y);
                PointF curPos  = ConvertCoordFromScreen(current_mouse_pos.X, current_mouse_pos.Y);

                Point pPos = new Point((int)prevPos.X, (int)prevPos.Y);
                Point cPos = new Point((int)curPos.X, (int)curPos.Y);

                RectangleObj newRectangle = new RectangleObj();
                newRectangle.SetRectangleByCoords(pPos, cPos);
                if (action_status == ActionMode.RangeQuery)
                {
                    int rad = Math.Max(newRectangle.rect_width, newRectangle.rect_height);
                    newRectangle.UpdateWH(rad, rad);
                }

                prevPos = ConvertCoordToScreen(newRectangle.min_extent_X, newRectangle.min_extent_Y);
                curPos  = ConvertCoordToScreen(newRectangle.rect_width, newRectangle.rect_height);
                if (action_status == ActionMode.Add || action_status == ActionMode.WindowQuery)
                {
                    e.Graphics.DrawRectangle(new Pen(br), prevPos.X, prevPos.Y, curPos.X, curPos.Y);
                }
                else if (action_status == ActionMode.RangeQuery)
                {
                    e.Graphics.DrawEllipse(new Pen(br), prevPos.X, prevPos.Y, curPos.X, curPos.Y);
                }
            }
            if (rects_to_draw != null && rects_to_draw.Count > 0)
            {
                foreach (RectangleObj rect in rects_to_draw)
                {
                    Brush  br           = new SolidBrush(rect.rect_color);
                    PointF corner_coord = ConvertCoordToScreen(rect.min_extent_X, rect.min_extent_Y);
                    PointF wh_extent    = ConvertCoordToScreen(rect.rect_width, rect.rect_height);
                    e.Graphics.FillRectangle(br, corner_coord.X, corner_coord.Y, wh_extent.X, wh_extent.Y);
                }
            }
            if (hollow_rects_to_draw.Count > 0)
            {
                foreach (RectangleObj rect in hollow_rects_to_draw)
                {
                    Brush  br           = new SolidBrush(Color.Red);
                    PointF corner_coord = ConvertCoordToScreen(rect.min_extent_X, rect.min_extent_Y);
                    PointF wh_extent    = ConvertCoordToScreen(rect.rect_width, rect.rect_height);
                    Pen    pen          = new Pen(br, 3);
                    e.Graphics.DrawRectangle(pen, corner_coord.X, corner_coord.Y, wh_extent.X, wh_extent.Y);
                    pen.Dispose();
                }
            }
            if (incrementalSet)
            {
                Brush br = new SolidBrush(Color.Orange);
                e.Graphics.FillRectangle(br, incrementalPos.X, incrementalPos.Y, 5, 5);
            }
            if (hollow_circles.Count > 0)
            {
                foreach (RectangleObj rect in hollow_circles)
                {
                    PointF corner_coord = ConvertCoordToScreen(rect.min_extent_X, rect.min_extent_Y);
                    PointF wh_extent    = ConvertCoordToScreen(rect.rect_width, rect.rect_height);
                    Brush  br           = new SolidBrush(rect.rect_color);
                    Pen    pen          = new Pen(br, 2);
                    e.Graphics.DrawEllipse(pen, corner_coord.X, corner_coord.Y, wh_extent.X, wh_extent.Y);
                    pen.Dispose();
                }
            }
        }
 public void SetObj(RectangleObj o)
 {
     obj = o;
 }
 private RectangleObj RemoveRectangle(RectangleObj rect, NodeType node)
 {
     node.RemoveRectangle(rect);
     MergeEmptyChildren(node);
     return(rect);
 }
Exemple #21
0
 public bool RemoveRectangle(RectangleObj rect)
 {
     DeleteRectangle(rect, false);
     return(IsOverflown());
 }
 public void StopMoving()
 {
     moving_node = default(NodeType);
     moving_obj  = null;
 }
        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);
        }
Exemple #24
0
 public void AddHollowRect(RectangleObj rect)
 {
     hollow_rects_to_draw.Add(rect);
 }
Exemple #25
0
 protected virtual void OnRectangleAdded(RectangleObj e)
 {
     rectAdded?.Invoke(this, e);
 }