Beispiel #1
0
        /// <summary>
        /// Insert an item which is known to be contained in the tree rooted at
        /// the given QuadNode root.  Lower levels of the tree will be created
        /// if necessary to hold the item.
        /// </summary>
        private void InsertContained(Node <TItem> tree, Rectangle itemEnv, TItem item)
        {
            if (!DrawingExtensions.Contains(tree.Envelope, itemEnv))
            {
                throw new Exception();
            }

            /*
             * Do NOT create a new quad for zero-area envelopes - this would lead
             * to infinite recursion. Instead, use a heuristic of simply returning
             * the smallest existing quad containing the query
             */
            var itemEnvX = itemEnv.X;
            var itemEnvR = itemEnvX + itemEnv.Width;
            var itemEnvY = itemEnv.Y;
            var itemEnvB = itemEnvY + itemEnv.Height;
            var isZeroX  = DoubleBits.IsZeroWidth(itemEnvX, itemEnvR);
            var isZeroY  = DoubleBits.IsZeroWidth(itemEnvY, itemEnvB);
            NodeBase <TItem> node;

            if (isZeroX || isZeroY)
            {
                node = tree.Find(itemEnv);
            }
            else
            {
                node = tree.GetNode(itemEnv);
            }
            node.AddItem(item);
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static double TruncateToPowerOfTwo(double d)
        {
            var db = new DoubleBits(d);

            db.ZeroLowerBits(52);
            return(db.Double);
        }
Beispiel #3
0
        private void ComputeKey(int level, Rectangle itemEnv)
        {
            double quadSize = DoubleBits.PowerOf2(level);

            _pt.X = (Math.Floor(itemEnv.X / quadSize) * quadSize);
            _pt.Y = (Math.Floor(itemEnv.Y / quadSize) * quadSize);
            _env  = new Rectangle(_pt.X, _pt.Y, quadSize, quadSize);
        }
Beispiel #4
0
        public static int ComputeQuadLevel(Rectangle env)
        {
            var dx    = env.Width;
            var dy    = env.Height;
            var dMax  = dx > dy ? dx : dy;
            int level = DoubleBits.GetExponent(dMax) + 1;

            return(level);
        }
Beispiel #5
0
 /// <summary>
 /// This computes the number of common most-significant bits in the mantissa.
 /// It does not count the hidden bit, which is always 1.
 /// It does not determine whether the numbers have the same exponent - if they do
 /// not, the value computed by this function is meaningless.
 /// </summary>
 /// <param name="db"></param>
 /// <returns> The number of common most-significant mantissa bits.</returns>
 public virtual int NumCommonMantissaBits(DoubleBits db)
 {
     for (int i = 0; i < 52; i++)
     {
         if (GetBit(i) != db.GetBit(i))
         {
             return(i);
         }
     }
     return(52);
 }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static double MaximumCommonMantissa(double d1, double d2)
        {
            if (d1 == 0.0 || d2 == 0.0)
            {
                return(0.0);
            }

            var db1 = new DoubleBits(d1);
            var db2 = new DoubleBits(d2);

            if (db1.Exponent != db2.Exponent)
            {
                return(0.0);
            }

            int maxCommon = db1.NumCommonMantissaBits(db2);

            db1.ZeroLowerBits(64 - (12 + maxCommon));
            return(db1.Double);
        }
Beispiel #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static string ToBinaryString(double d)
        {
            var db = new DoubleBits(d);

            return(db.ToString());
        }
Beispiel #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public static int GetExponent(double d)
        {
            var db = new DoubleBits(d);

            return(db.Exponent);
        }