Exemple #1
0
 public AxisAlignedBox2i(int fWidth, int fHeight)
 {
     Min = new Vector2i(0, 0);
     Max = new Vector2i(fWidth, fHeight);
 }
Exemple #2
0
 public AxisAlignedBox2i(Vector2i vMin, Vector2i vMax)
 {
     Min = new Vector2i(Math.Min(vMin.x, vMax.x), Math.Min(vMin.y, vMax.y));
     Max = new Vector2i(Math.Max(vMin.x, vMax.x), Math.Max(vMin.y, vMax.y));
 }
Exemple #3
0
 public void MoveMin(int fNewX, int fNewY)
 {
     Max.x = fNewX + (Max.x - Min.x);
     Max.y = fNewY + (Max.y - Min.y);
     Min   = new Vector2i(fNewX, fNewY);
 }
Exemple #4
0
 public AxisAlignedBox2i(int fCubeSize)
 {
     Min = new Vector2i(0, 0);
     Max = new Vector2i(fCubeSize, fCubeSize);
 }
        /// <summary>
        /// Remove point without locking / thread-safety
        /// </summary>
        public bool RemovePointUnsafe(T value, Vector2d pos)
        {
            Vector2i idx = Indexer.ToGrid(pos);

            return(remove_point(value, idx, false));
        }
Exemple #6
0
 public void MoveMin(Vector2i vNewMin)
 {
     Max.x = vNewMin.x + (Max.x - Min.x);
     Max.y = vNewMin.y + (Max.y - Min.y);
     Min   = vNewMin;
 }
Exemple #7
0
 public AxisAlignedBox2i(int xmin, int ymin, int xmax, int ymax)
 {
     Min = new Vector2i(xmin, ymin);
     Max = new Vector2i(xmax, ymax);
 }
Exemple #8
0
 public AxisAlignedBox2i(Vector2i vCenter)
 {
     Min = Max = vCenter;
 }
Exemple #9
0
 public AxisAlignedBox2i(bool bIgnore)
 {
     Min = new Vector2i(int.MaxValue, int.MaxValue);
     Max = new Vector2i(int.MinValue, int.MinValue);
 }
Exemple #10
0
 public bool Contains(ref Vector2i v)
 {
     return((Min.x <= v.x) && (Min.y <= v.y) &&
            (Max.x >= v.x) && (Max.y >= v.y));
 }
Exemple #11
0
        /// <summary>
        /// Remove segment without locking / thread-safety
        /// </summary>
        public bool RemoveSegmentUnsafe(T value, Vector2d center)
        {
            Vector2i idx = Indexer.ToGrid(center);

            return(remove_segment(value, idx, false));
        }
Exemple #12
0
        /// <summary>
        /// Variant of FindNearestInRadius that works with squared-distances
        /// </summary>
        public KeyValuePair <T, double> FindNearestInSquaredRadius(Vector2d query_pt, double radiusSqr, Func <T, double> distSqrF, Func <T, bool> ignoreF = null)
        {
            double   search_dist = Math.Sqrt(radiusSqr) + MaxExtent;
            Vector2i min_idx     = Indexer.ToGrid(query_pt - search_dist * Vector2d.One);
            Vector2i max_idx     = Indexer.ToGrid(query_pt + search_dist * Vector2d.One);

            double min_dist_sqr = double.MaxValue;
            T      nearest      = invalidValue;

            if (ignoreF == null)
            {
                ignoreF = (pt) => { return(false); }
            }
            ;

            for (int yi = min_idx.y; yi <= max_idx.y; yi++)
            {
                for (int xi = min_idx.x; xi <= max_idx.x; xi++)
                {
                    Vector2i idx = new Vector2i(xi, yi);
                    List <T> values;
                    if (Hash.TryGetValue(idx, out values) == false)
                    {
                        continue;
                    }
                    foreach (T value in values)
                    {
                        if (ignoreF(value))
                        {
                            continue;
                        }
                        double distSqr = distSqrF(value);
                        if (distSqr < radiusSqr && distSqr < min_dist_sqr)
                        {
                            nearest      = value;
                            min_dist_sqr = distSqr;
                        }
                    }
                }
            }

            return(new KeyValuePair <T, double>(nearest, min_dist_sqr));
        }

        void insert_segment(T value, Vector2i idx, bool threadsafe = true)
        {
            bool lockTaken = false;

            while (threadsafe == true && lockTaken == false)
            {
                spinlock.Enter(ref lockTaken);
            }

            List <T> values;

            if (Hash.TryGetValue(idx, out values))
            {
                values.Add(value);
            }
            else
            {
                Hash[idx] = new List <T>()
                {
                    value
                };
            }

            if (lockTaken)
            {
                spinlock.Exit();
            }
        }

        bool remove_segment(T value, Vector2i idx, bool threadsafe = true)
        {
            bool lockTaken = false;

            while (threadsafe == true && lockTaken == false)
            {
                spinlock.Enter(ref lockTaken);
            }

            List <T> values;
            bool     result = false;

            if (Hash.TryGetValue(idx, out values))
            {
                result = values.Remove(value);
            }

            if (lockTaken)
            {
                spinlock.Exit();
            }
            return(result);
        }
    }
Exemple #13
0
 public int this[Vector2i ijk] {
     get { return(Buffer[ijk.x + ni * ijk.y]); }
     set { Buffer[ijk.x + ni * ijk.y] = value; }
 }
Exemple #14
0
 public AxisAlignedBox2i(Vector2i vCenter, int fHalfWidth, int fHalfHeight, int fHalfDepth)
 {
     Min = new Vector2i(vCenter.x - fHalfWidth, vCenter.y - fHalfHeight);
     Max = new Vector2i(vCenter.x + fHalfWidth, vCenter.y + fHalfHeight);
 }
Exemple #15
0
 public int Distance(Vector2i v)
 {
     return((int)Math.Sqrt(DistanceSquared(v)));
 }
Exemple #16
0
 public AxisAlignedBox2i(Vector2i vCenter, int fHalfSize)
 {
     Min = new Vector2i(vCenter.x - fHalfSize, vCenter.y - fHalfSize);
     Max = new Vector2i(vCenter.x + fHalfSize, vCenter.y + fHalfSize);
 }
Exemple #17
0
 //! relative translation
 public void Translate(Vector2i vTranslate)
 {
     Min += vTranslate;
     Max += vTranslate;
 }
        // The value of epsilon is used as a relative error when computing the
        // dimension of the point set.
        public static void GetInformation(IList <Vector2d> points, double epsilon, out Information info)
        {
            info = new Information();
            int numPoints = points.Count;

            if (numPoints == 0 || points == null || epsilon <= 0)
            {
                System.Diagnostics.Debug.Assert(false);
                return;
            }

            info.mExtremeCCW = false;

            // Compute the axis-aligned bounding box for the input points.  Keep track
            // of the indices into 'points' for the current min and max.
            int      j;
            Vector2i indexMin = Vector2i.Zero;
            Vector2i indexMax = Vector2i.Zero;

            for (j = 0; j < 2; ++j)
            {
                info.mMin[j] = points[0][j];
                info.mMax[j] = info.mMin[j];
                indexMin[j]  = 0;
                indexMax[j]  = 0;
            }

            int i;

            for (i = 1; i < numPoints; ++i)
            {
                for (j = 0; j < 2; ++j)
                {
                    if (points[i][j] < info.mMin[j])
                    {
                        info.mMin[j] = points[i][j];
                        indexMin[j]  = i;
                    }
                    else if (points[i][j] > info.mMax[j])
                    {
                        info.mMax[j] = points[i][j];
                        indexMax[j]  = i;
                    }
                }
            }

            // Determine the maximum range for the bounding box.
            info.mMaxRange   = info.mMax[0] - info.mMin[0];
            info.mExtreme[0] = indexMin[0];
            info.mExtreme[1] = indexMax[0];
            double range = info.mMax[1] - info.mMin[1];

            if (range > info.mMaxRange)
            {
                info.mMaxRange   = range;
                info.mExtreme[0] = indexMin[1];
                info.mExtreme[1] = indexMax[1];
            }

            // The origin is either the point of minimum x-value or point of
            // minimum y-value.
            info.mOrigin = points[info.mExtreme[0]];

            // Test whether the point set is (nearly) a point.
            if (info.mMaxRange < epsilon)
            {
                info.mDimension  = 0;
                info.mDirection0 = Vector2d.Zero;
                info.mDirection1 = Vector2d.Zero;
                for (j = 0; j < 2; ++j)
                {
                    info.mExtreme[j + 1] = info.mExtreme[0];
                }
                return;
            }

            // Test whether the point set is (nearly) a line segment.
            info.mDirection0 = points[info.mExtreme[1]] - info.mOrigin;
            info.mDirection0.Normalize();
            info.mDirection1 = -info.mDirection0.Perp;
            double maxDistance = (double)0;
            double maxSign     = (double)0;

            info.mExtreme[2] = info.mExtreme[0];
            for (i = 0; i < numPoints; ++i)
            {
                Vector2d diff     = points[i] - info.mOrigin;
                double   distance = info.mDirection1.Dot(diff);
                double   sign     = Math.Sign(distance);
                distance = Math.Abs(distance);
                if (distance > maxDistance)
                {
                    maxDistance      = distance;
                    maxSign          = sign;
                    info.mExtreme[2] = i;
                }
            }

            if (maxDistance < epsilon * info.mMaxRange)
            {
                info.mDimension  = 1;
                info.mExtreme[2] = info.mExtreme[1];
                return;
            }

            info.mDimension  = 2;
            info.mExtremeCCW = (maxSign > (double)0);
        }
        /// <summary>
        /// Insert point without locking / thread-safety
        /// </summary>
        public void InsertPointUnsafe(T value, Vector2d pos)
        {
            Vector2i idx = Indexer.ToGrid(pos);

            insert_point(value, idx, false);
        }