Exemple #1
0
        public void getTree()
        {
            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\"));
            if (tree == null)
            {
                RTree.RTree <DataPoint> tree = SaveDataController.ReadFromBinaryFile <RTree.RTree <DataPoint> >(System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\", fileName));
                //not created before
                tree.locker = new System.Threading.ReaderWriterLock();
                if (tree.Count == 0)
                {
                    //query database
                    DATNEntities entities = new DATNEntities();
                    var          query    = from p in entities.DataPoints
                                            select p;
                    List <DataPoint> listItem = query.ToList();
                    tree = new RTree.RTree <DataPoint>(listItem.Count > 9 ? 9 : listItem.Count / 2, 2);
                    int count = 0;
                    foreach (DataPoint p in listItem)
                    {
                        count++;
                        Debug.WriteLine("INTOP - " + count);
                        RTree.Rectangle rect = new RTree.Rectangle((float)p.rating, (float)p.star, (float)p.rating, (float)p.star, 0, 0);
                        tree.Add(rect, p);
                    }
                    SaveDataController.WriteToBinaryFile(System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\", fileName), tree);
                }

                this.tree  = tree;
                treeHelper = new TreeHelper(tree);
            }
        }
Exemple #2
0
 /// <summary>
 /// Checks if the two-dimensional rectangle is completely inside
 /// the two-dimensional circle.
 /// </summary>
 /// <remarks>
 /// A rectangle is inside a circle if all 4 corners are inside the
 /// circle.
 /// </remarks>
 /// <param name="r">The rectangle.</param>
 /// <returns>True if the rectangle is inside the circle; false
 /// otherwise.</returns>
 public bool Contains(Rectangle r)
 {
     return Contains(new Point(r.min[0], r.min[1])) &&
         Contains(new Point(r.min[0], r.max[1])) &&
         Contains(new Point(r.max[0], r.min[1])) &&
         Contains(new Point(r.max[0], r.max[1]));
 }
 public static bool Intersect(double x1, double y1, double x2, double y2, Circle cir)
 {
     Rectangle rec = new Rectangle((float)x1,
         (float)y1,
         (float)x2,
         (float)y2,
         0,
         0);
     return Intersect(rec, cir);
 }
        public static bool Intersect(Rectangle rec, Circle cir)
        {
            if (rec == null || cir == null)
                throw new ArgumentNullException();

            if (rec.distance(cir.Center) == 0)
                return true;
            if (rec.distance(cir.Center) > cir.Radius)
                return false;
            return true;
        }
Exemple #5
0
        public MBRModel <DataPoint> getRoot()
        {
            //getTree();
            //get root after add to tree
            RTree.Rectangle bounds     = tree.getBounds();
            DataPoint       upperRight = new DataPoint(bounds.get(0).GetValueOrDefault().max, bounds.get(1).GetValueOrDefault().max);
            DataPoint       lowerLeft  = new DataPoint(bounds.get(0).GetValueOrDefault().min, bounds.get(1).GetValueOrDefault().min);

            //retrun root
            return(new MBRModel <DataPoint>(lowerLeft, upperRight, tree.getNode(tree.getRootNodeId())));
        }
Exemple #6
0
        public MBRModel <WeightVector> getRoot()
        {
            //getTree();
            //get root after add to tree
            RTree.Rectangle bounds     = tree.getBounds();
            WeightVector    upperRight = new WeightVector(bounds.get(0).GetValueOrDefault().max, bounds.get(1).GetValueOrDefault().max);
            WeightVector    lowerLeft  = new WeightVector(bounds.get(0).GetValueOrDefault().min, bounds.get(1).GetValueOrDefault().min);

            //retrun root
            return(new MBRModel <WeightVector>(lowerLeft, upperRight));
        }
Exemple #7
0
        private static void BuildTree()
        {
            Stopwatch watch = new Stopwatch ();
            watch.Start ();

            sensable_tree = new RTree<Sensable> (sensables.Count, sensables.Count);

            foreach (Sensable sensable in sensables) {
                var pos = sensable.transform.position;
                var rect = new Rectangle (new float[]{ pos.x, pos.y, pos.z }, new float[]{ pos.x, pos.y, pos.z });
                sensable_tree.Add (rect, sensable);
            }

            watch.Stop ();
            if (watch.Elapsed.TotalMilliseconds > 0)
                UnityEngine.Debug.Log ("BUILD TREE: " + watch.Elapsed.TotalMilliseconds.ToString ());
        }
Exemple #8
0
        static List<Rectangle> getPoint(string w, int d)
        {
            List<Rectangle> pnts = new List<Rectangle>();
            int[] hist = Util.hist(w);

            for (int i = 0; i <= level; i++)
            {
                int[] m = Util.summarize_hist(hist, i);
                Point p = null;
                if (exact)
                    p = new Point(m, m.Length);
                else

                    p = new Point(m[0], m[0 + m.Length / 2]);
                Rectangle r = new Rectangle(p);

                pnts.Add(r.extend(d));
            }
            return pnts;
        }
Exemple #9
0
            /**
             * Calculate the area by which this rectangle would be enlarged if
             * added to the passed rectangle. Neither rectangle is altered.
             *
             * @param r Rectangle to union with this rectangle, in order to
             *          compute the difference in area of the union and the
             *          original rectangle
             */
            public float enlargement(Rectangle r)
            {
            float enlargedArea = (Math.Max(max[0], r.max[0]) - Math.Min(min[0], r.min[0])) *
                                 (Math.Max(max[1], r.max[1]) - Math.Min(min[1], r.min[1]));

            return enlargedArea - area();
            }
Exemple #10
0
 /**
  * Determine whether an edge of this rectangle overlies the equivalent
  * edge of the passed rectangle
  */
 public bool edgeOverlaps(Rectangle r)
 {
     for (int i = 0; i < DIMENSIONS; i++)
     {
         if (min[i] == r.min[i] || max[i] == r.max[i])
         {
             return true;
         }
     }
     return false;
 }
Exemple #11
0
        /**
         * Return the furthst possible distance between this rectangle and
         * the passed rectangle.
         *
         * Find the distance between this rectangle and each corner of the
         * passed rectangle, and use the maximum.
         *
         */
        public float furthestDistance(Rectangle r)
        {
            float distanceSquared = 0;

            for (int i = 0; i < DIMENSIONS; i++)
            {
                distanceSquared += Math.Max(r.min[i], r.max[i]);
            #warning possible didn't convert properly
                //distanceSquared += Math.Max(distanceSquared(i, r.min[i]), distanceSquared(i, r.max[i]));
            }

            return (float)Math.Sqrt(distanceSquared);
            }
Exemple #12
0
 /**
  * Find the the union of this rectangle and the passed rectangle.
  * Neither rectangle is altered
  * 
  * @param r The rectangle to union with this rectangle
  */
 internal Rectangle union(Rectangle r)
 {
     Rectangle union = this.copy();
     union.add(r);
     return union;
 }
Exemple #13
0
 /**
  * Return the distance between this rectangle and the passed rectangle.
  * If the rectangles overlap, the distance is zero.
  *
  * @param r Rectangle to find the distance to
  *
  * @return distance between this rectangle and the passed rectangle
  */
 public float distance(Rectangle r)
 {
     float distanceSquared = 0;
     for (int i = 0; i < DIMENSIONS; i++)
     {
         float greatestMin = Math.Max(min[i], r.min[i]);
         float leastMax = Math.Min(max[i], r.max[i]);
         if (greatestMin > leastMax)
         {
             distanceSquared += ((greatestMin - leastMax) * (greatestMin - leastMax));
         }
     }
     return (float)Math.Sqrt(distanceSquared);
 }
Exemple #14
0
        public static void Draw()
        {
            canvas.Width = canvas.Width;
            context.Save();
            context.Translate(-Offset.X, -Offset.Y);

            context.Save();
            context.StrokeStyle = "black";
            var bigBox = 60;
            var rect = new Rectangle(Offset.X, Offset.Y, Offset.X + canvas.Width, Offset.Y + canvas.Height);

            for (int x = 0; x < gameSize; x += bigBox)
            {
                for (int y = 0; y < gameSize; y += bigBox)
                {
                    context.StrokeRect(x * squareSize, y * squareSize, squareSize * bigBox, squareSize * bigBox);
                }
            }
            context.Restore();


            foreach (var playerCluster in clusters)
            {
                var vector2s = playerCluster.Players.Select(a => new Vector2(a.X, a.Y));

                var box = BoundingBox.CreateFromPoints(vector2s);

                var center = new Vector2(box.Min.X + ((box.Max.X - box.Min.X) / 2), box.Min.Y + ((box.Max.Y - box.Min.Y) / 2));

                var polyRect = new Rectangle(box.Min.X * squareSize, box.Min.Y * squareSize, box.Max.X * squareSize, box.Max.Y * squareSize);


                if (!rect.intersects(polyRect))
                {
                    continue;
                }

                var vecs = vector2s.OrderBy(a =>
                {
                    return Math.Atan2(a.Y - center.Y, a.X - center.X);
                });


                context.Save();
                context.StrokeStyle = context.FillStyle = playerCluster.Color;
                context.LineWidth = 6;
                var lastPlayer = vecs[0];
                context.BeginPath();
                context.MoveTo(lastPlayer.X * squareSize + squareSize / 2, lastPlayer.Y * squareSize + squareSize / 2);
                for (int index = 0; index < vecs.Length; index++)
                {
                    var player = vecs[index];
                    context.FillRect(player.X * squareSize - (squareSize) / 2, player.Y * squareSize - (squareSize) / 2, squareSize*2, squareSize*2);
                    context.LineTo(player.X * squareSize + squareSize / 2, player.Y * squareSize + squareSize / 2);
                }
                context.ClosePath();
                if (drawLines)
                {
                    context.Stroke();
                }
                context.Restore();

                context.Save();
                context.Font = "30px Arial";
                if (vecs.Length > 2)
                {
                    context.FillText(vecs.Length.ToString(), center.X * squareSize + squareSize / 2, center.Y * squareSize + squareSize / 2);
                }
                context.Restore();
            }
            context.Restore();

        }
 /// <summary>
 /// Returns a list of features that intersect with the specified rectangle.
 /// </summary>
 /// <param name="r">The intersection rectangle.</param>
 /// <returns></returns>
 public FdoFeature[] Intersects(Rectangle r)
 {
     return _tree.Intersects(r).ToArray();
 }
            public Group(int cap, Rectangle head)
            {
                if (cap < 1)
                    throw new ArgumentOutOfRangeException();

                capacity = cap;
                entries = new List<Rectangle>();

                if (head == null)
                    return;

                entries.Add(head);
                mbr = head;
            }
            public bool Add(Rectangle entry)
            {
                if (entries.Count == capacity)
                    return false;

                entries.Add(entry);

                if (mbr == null)
                    mbr = entry;
                else
                    mbr.add(entry);

                return true;
            }
Exemple #18
0
 /**
  * Determine whether this rectangle intersects the passed rectangle
  *
  * @param r The rectangle that might intersect this rectangle
  *
  * @return true if the rectangles intersect, false if they do not intersect
  */
 public bool intersects(Rectangle r)
 {
     // Every dimension must intersect. If any dimension
     // does not intersect, return false immediately.
     for (int i = 0; i < DIMENSIONS; i++)
     {
         if (max[i] < r.min[i] || min[i] > r.max[i])
         {
             return false;
         }
     }
     return true;
 }
 /// <summary>
 /// Returns a list of features that contains the specified rectangle.
 /// </summary>
 /// <param name="r">The r.</param>
 /// <returns></returns>
 public FdoFeature[] Contains(Rectangle r)
 {
     return _tree.Contains(r).ToArray();
 }
Exemple #20
0
 /**
  * Computes the union of this rectangle and the passed rectangle, storing
  * the result in this rectangle.
  *
  * @param r Rectangle to add to this rectangle
  */
 public void add(Rectangle r)
 {
 for (int i = 0; i < DIMENSIONS; i++)
 {
     if (r.min[i] < min[i])
     {
         min[i] = r.min[i];
     }
     if (r.max[i] > max[i])
     {
         max[i] = r.max[i];
     }
 }
 }
Exemple #21
0
        /// <summary>
        /// Pick the seeds used to split a node.
        /// Select two entries to be the first elements of the groups
        /// </summary>
        /// <param name="n"></param>
        /// <param name="newRect"></param>
        /// <param name="newId"></param>
        /// <param name="newNode"></param>
        private void pickSeeds(Node <T> n, Rectangle newRect, int newId, Node <T> newNode)
        {
            // Find extreme rectangles along all dimension. Along each dimension,
            // find the entry whose rectangle has the highest low side, and the one
            // with the lowest high side. Record the separation.
            float maxNormalizedSeparation = 0;
            int   highestLowIndex         = 0;
            int   lowestHighIndex         = 0;

            // for the purposes of picking seeds, take the MBR of the Node&lt;T&gt; to include
            // the new rectangle aswell.
            n.mbr.add(newRect);

            if (log.IsDebugEnabled)
            {
                log.Debug("pickSeeds(): NodeId = " + n.nodeId + ", newRect = " + newRect);
            }

            for (int d = 0; d < Rectangle.DIMENSIONS; d++)
            {
                float tempHighestLow      = newRect.min[d];
                int   tempHighestLowIndex = -1; // -1 indicates the new rectangle is the seed

                float tempLowestHigh      = newRect.max[d];
                int   tempLowestHighIndex = -1;

                for (int i = 0; i < n.entryCount; i++)
                {
                    float tempLow = n.entries[i].min[d];
                    if (tempLow >= tempHighestLow)
                    {
                        tempHighestLow      = tempLow;
                        tempHighestLowIndex = i;
                    }
                    else
                    {  // ensure that the same index cannot be both lowestHigh and highestLow
                        float tempHigh = n.entries[i].max[d];
                        if (tempHigh <= tempLowestHigh)
                        {
                            tempLowestHigh      = tempHigh;
                            tempLowestHighIndex = i;
                        }
                    }

                    // PS2 [Adjust for shape of the rectangle cluster] Normalize the separations
                    // by dividing by the widths of the entire set along the corresponding
                    // dimension
                    float normalizedSeparation = (tempHighestLow - tempLowestHigh) / (n.mbr.max[d] - n.mbr.min[d]);

                    if (normalizedSeparation > 1 || normalizedSeparation < -1)
                    {
                        log.Error("Invalid normalized separation");
                    }

                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Entry " + i + ", dimension " + d + ": HighestLow = " + tempHighestLow +
                                  " (index " + tempHighestLowIndex + ")" + ", LowestHigh = " +
                                  tempLowestHigh + " (index " + tempLowestHighIndex + ", NormalizedSeparation = " + normalizedSeparation);
                    }

                    // PS3 [Select the most extreme pair] Choose the pair with the greatest
                    // normalized separation along any dimension.
                    if (normalizedSeparation > maxNormalizedSeparation)
                    {
                        maxNormalizedSeparation = normalizedSeparation;
                        highestLowIndex         = tempHighestLowIndex;
                        lowestHighIndex         = tempLowestHighIndex;
                    }
                }
            }

            // highestLowIndex is the seed for the new node.
            if (highestLowIndex == -1)
            {
                newNode.addEntry(newRect, newId);
            }
            else
            {
                newNode.addEntryNoCopy(n.entries[highestLowIndex], n.ids[highestLowIndex]);
                n.entries[highestLowIndex] = null;

                // move the new rectangle into the space vacated by the seed for the new node
                n.entries[highestLowIndex] = newRect;
                n.ids[highestLowIndex]     = newId;
            }

            // lowestHighIndex is the seed for the original node.
            if (lowestHighIndex == -1)
            {
                lowestHighIndex = highestLowIndex;
            }

            entryStatus[lowestHighIndex] = ENTRY_STATUS_ASSIGNED;
            n.entryCount = 1;
            n.mbr.set(n.entries[lowestHighIndex].min, n.entries[lowestHighIndex].max);
        }
Exemple #22
0
        /// <summary>
        /// Split a node. Algorithm is taken pretty much verbatim from
        /// Guttman's original paper.
        /// </summary>
        /// <param name="n"></param>
        /// <param name="newRect"></param>
        /// <param name="newId"></param>
        /// <returns>return new Node&lt;T&gt; object.</returns>
        private Node <T> splitNode(Node <T> n, Rectangle newRect, int newId)
        {
            // [Pick first entry for each group] Apply algorithm pickSeeds to
            // choose two entries to be the first elements of the groups. Assign
            // each to a group.

            // debug code
            float initialArea = 0;

            if (log.IsDebugEnabled)
            {
                Rectangle union = n.mbr.union(newRect);
                initialArea = union.area();
            }

            for (int i = 0; i < maxNodeEntries; i++)
            {
                entryStatus[i] = initialEntryStatus[i];
            }

            Node <T> newNode = null;

            newNode = new Node <T>(getNextNodeId(), n.level, maxNodeEntries);
            nodeMap[newNode.nodeId] = newNode;

            pickSeeds(n, newRect, newId, newNode); // this also sets the entryCount to 1

            // [Check if done] If all entries have been assigned, stop. If one
            // group has so few entries that all the rest must be assigned to it in
            // order for it to have the minimum number m, assign them and stop.
            while (n.entryCount + newNode.entryCount < maxNodeEntries + 1)
            {
                if (maxNodeEntries + 1 - newNode.entryCount == minNodeEntries)
                {
                    // assign all remaining entries to original node
                    for (int i = 0; i < maxNodeEntries; i++)
                    {
                        if (entryStatus[i] == ENTRY_STATUS_UNASSIGNED)
                        {
                            entryStatus[i] = ENTRY_STATUS_ASSIGNED;
                            n.mbr.add(n.entries[i]);
                            n.entryCount++;
                        }
                    }
                    break;
                }
                if (maxNodeEntries + 1 - n.entryCount == minNodeEntries)
                {
                    // assign all remaining entries to new node
                    for (int i = 0; i < maxNodeEntries; i++)
                    {
                        if (entryStatus[i] == ENTRY_STATUS_UNASSIGNED)
                        {
                            entryStatus[i] = ENTRY_STATUS_ASSIGNED;
                            newNode.addEntryNoCopy(n.entries[i], n.ids[i]);
                            n.entries[i] = null;
                        }
                    }
                    break;
                }

                // [Select entry to assign] Invoke algorithm pickNext to choose the
                // next entry to assign. Add it to the group whose covering rectangle
                // will have to be enlarged least to accommodate it. Resolve ties
                // by adding the entry to the group with smaller area, then to the
                // the one with fewer entries, then to either. Repeat from S2
                pickNext(n, newNode);
            }

            n.reorganize(this);

            // check that the MBR stored for each Node&lt;T&gt; is correct.
            if (INTERNAL_CONSISTENCY_CHECKING)
            {
                if (!n.mbr.Equals(calculateMBR(n)))
                {
                    log.Error("Error: splitNode old Node<T> MBR wrong");
                }

                if (!newNode.mbr.Equals(calculateMBR(newNode)))
                {
                    log.Error("Error: splitNode new Node<T> MBR wrong");
                }
            }

            // debug code
            if (log.IsDebugEnabled)
            {
                float newArea            = n.mbr.area() + newNode.mbr.area();
                float percentageIncrease = (100 * (newArea - initialArea)) / initialArea;
                log.Debug("Node " + n.nodeId + " split. New area increased by " + percentageIncrease + "%");
            }

            return(newNode);
        }
Exemple #23
0
        private void intersects(Rectangle r, intproc v)
        {
            Node <T> rootNode = getNode(rootNodeId);

            intersects(r, v, rootNode);
        }
Exemple #24
0
        private bool delete(Rectangle r, int id)
        {
            // FindLeaf algorithm inlined here. Note the "official" algorithm
            // searches all overlapping entries. This seems inefficient to me,
            // as an entry is only worth searching if it contains (NOT overlaps)
            // the rectangle we are searching for.
            //
            // Also the algorithm has been changed so that it is not recursive.

            // FL1 [Search subtrees] If root is not a leaf, check each entry
            // to determine if it contains r. For each entry found, invoke
            // findLeaf on the Node&lt;T&gt; pointed to by the entry, until r is found or
            // all entries have been checked.
            parents.Clear();
            parents.Push(rootNodeId);

            parentsEntry.Clear();
            parentsEntry.Push(-1);
            Node <T> n          = null;
            int      foundIndex = -1; // index of entry to be deleted in leaf

            while (foundIndex == -1 && parents.Count > 0)
            {
                n = getNode(parents.Peek());
                int startIndex = parentsEntry.Peek() + 1;

                if (!n.isLeaf())
                {
                    deleteLog.Debug("searching Node<T> " + n.nodeId + ", from index " + startIndex);
                    bool contains = false;
                    for (int i = startIndex; i < n.entryCount; i++)
                    {
                        if (n.entries[i].contains(r))
                        {
                            parents.Push(n.ids[i]);
                            parentsEntry.Pop();
                            parentsEntry.Push(i); // this becomes the start index when the child has been searched
                            parentsEntry.Push(-1);
                            contains = true;
                            break; // ie go to next iteration of while()
                        }
                    }
                    if (contains)
                    {
                        continue;
                    }
                }
                else
                {
                    foundIndex = n.findEntry(r, id);
                }

                parents.Pop();
                parentsEntry.Pop();
            } // while not found

            if (foundIndex != -1)
            {
                n.deleteEntry(foundIndex, minNodeEntries);
                condenseTree(n);
                msize--;
            }

            // shrink the tree if possible (i.e. if root Node&lt;T%gt; has exactly one entry,and that
            // entry is not a leaf node, delete the root (it's entry becomes the new root)
            Node <T> root = getNode(rootNodeId);

            while (root.entryCount == 1 && treeHeight > 1)
            {
                root.entryCount = 0;
                rootNodeId      = root.ids[0];
                treeHeight--;
                root = getNode(rootNodeId);
            }

            return(foundIndex != -1);
        }
Exemple #25
0
 /**
  * Find the the union of this rectangle and the passed rectangle.
  * Neither rectangle is altered
  *
  * @param r The rectangle to union with this rectangle
  */
 public Rectangle union(Rectangle r)
 {
 Rectangle union = this.copy();
 union.add(r);
 return union;
 }
Exemple #26
0
            /**
             * Calculate the area by which this rectangle would be enlarged if
             * added to the passed rectangle. Neither rectangle is altered.
             *
             * @param r Rectangle to union with this rectangle, in order to
             *          compute the difference in area of the union and the
             *          original rectangle
             */
            internal float enlargement(Rectangle r)
            {
            float enlargedArea =1;
            for(int i=0;i<min.Length;i++)
                enlargedArea *= (Math.Max(max[i], r.max[i]) - Math.Min(min[i], r.min[i]));

            return enlargedArea - area();
            }
Exemple #27
0
 /**
  * Determine whether this rectangle is contained by the passed rectangle
  *
  * @param r The rectangle that might contain this rectangle
  *
  * @return true if the passed rectangle contains this rectangle, false if
  *         it does not
  */
 internal bool containedBy(Rectangle r)
 {
     for (int i = 0; i < DIMENSIONS; i++)
     {
         if (max[i] > r.max[i] || min[i] < r.min[i])
         {
             return false;
         }
     }
     return true;
 }
Exemple #28
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="rectangle">需要提取西南角和东北角经纬度坐标值的矩形框</param>
 public RectangleCoordinate(Rectangle rectangle)
 {
     this.swLongitude = rectangle.Min.ToList<float>()[0];
     this.swLatitude = rectangle.Min.ToList<float>()[1];
     this.neLongitude = rectangle.Max.ToList<float>()[0];
     this.neLatitude = rectangle.Max.ToList<float>()[1];
 }