Exemple #1
0
        public bool MayIntersect(S2Cell cell)
        {
            // If the cap contains any cell vertex, return true.
            var vertices = new S2Point[4];

            for (var k = 0; k < 4; ++k)
            {
                vertices[k] = cell.GetVertex(k);
                if (Contains(vertices[k]))
                {
                    return(true);
                }
            }
            return(Intersects(cell, vertices));
        }
Exemple #2
0
        /**
         * Like the constructor above, but assumes that the cell's bounding rectangle
         * has been precomputed.
         *
         * @param cell
         * @param bound
         */

        public S2Loop(S2Cell cell, S2LatLngRect bound)
        {
            _bound         = bound;
            _numVertices   = 4;
            _vertices      = new S2Point[_numVertices];
            _vertexToIndex = null;
            _index         = null;
            _depth         = 0;
            for (var i = 0; i < 4; ++i)
            {
                _vertices[i] = cell.GetVertex(i);
            }
            InitOrigin();
            InitFirstLogicalVertex();
        }
        /**
         * Returns true if the edge and the cell (including boundary) intersect.
         */

        private static bool EdgeIntersectsCellBoundary(S2Point a, S2Point b, S2Cell cell)
        {
            var vertices = new S2Point[4];

            for (var i = 0; i < 4; ++i)
            {
                vertices[i] = cell.GetVertex(i);
            }
            for (var i = 0; i < 4; ++i)
            {
                var fromPoint = vertices[i];
                var toPoint   = vertices[(i + 1) % 4];
                if (LenientCrossing(a, b, fromPoint, toPoint))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #4
0
        public bool Contains(S2Cell cell)
        {
            // If the cap does not contain all cell vertices, return false.
            // We check the vertices before taking the Complement() because we can't
            // accurately represent the complement of a very small cap (a height
            // of 2-epsilon is rounded off to 2).
            var vertices = new S2Point[4];

            for (var k = 0; k < 4; ++k)
            {
                vertices[k] = cell.GetVertex(k);
                if (!Contains(vertices[k]))
                {
                    return(false);
                }
            }
            // Otherwise, return true if the complement of the cap does not intersect
            // the cell. (This test is slightly conservative, because technically we
            // want Complement().InteriorIntersects() here.)
            return(!Complement.Intersects(cell, vertices));
        }
        /**
         * If this method returns false, the region does not intersect the given cell.
         * Otherwise, either region intersects the cell, or the intersection
         * relationship could not be determined.
         */

        public bool MayIntersect(S2Cell cell)
        {
            if (NumVertices == 0)
            {
                return(false);
            }

            // We only need to check whether the cell contains vertex 0 for correctness,
            // but these tests are cheap compared to edge crossings so we might as well
            // check all the vertices.
            for (var i = 0; i < NumVertices; ++i)
            {
                if (cell.Contains(Vertex(i)))
                {
                    return(true);
                }
            }
            var cellVertices = new S2Point[4];

            for (var i = 0; i < 4; ++i)
            {
                cellVertices[i] = cell.GetVertex(i);
            }
            for (var j = 0; j < 4; ++j)
            {
                var crosser =
                    new EdgeCrosser(cellVertices[j], cellVertices[(j + 1) & 3], Vertex(0));
                for (var i = 1; i < NumVertices; ++i)
                {
                    if (crosser.RobustCrossing(Vertex(i)) >= 0)
                    {
                        // There is a proper crossing, or two vertices were the same.
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemple #6
0
        /**
         * Returns true if this rectangle intersects the given cell. (This is an exact
         * test and may be fairly expensive, see also MayIntersect below.)
         */

        public bool Intersects(S2Cell cell)
        {
            // First we eliminate the cases where one region completely contains the
            // other. Once these are disposed of, then the regions will intersect
            // if and only if their boundaries intersect.

            if (IsEmpty)
            {
                return(false);
            }
            if (Contains(cell.Center))
            {
                return(true);
            }
            if (cell.Contains(Center.ToPoint()))
            {
                return(true);
            }

            // Quick rejection test (not required for correctness).
            if (!Intersects(cell.RectBound))
            {
                return(false);
            }

            // Now check whether the boundaries intersect. Unfortunately, a
            // latitude-longitude rectangle does not have straight edges -- two edges
            // are curved, and at least one of them is concave.

            // Precompute the cell vertices as points and latitude-longitudes.
            var cellV  = new S2Point[4];
            var cellLl = new S2LatLng[4];

            for (var i = 0; i < 4; ++i)
            {
                cellV[i]  = cell.GetVertex(i); // Must be normalized.
                cellLl[i] = new S2LatLng(cellV[i]);
                if (Contains(cellLl[i]))
                {
                    return(true); // Quick acceptance test.
                }
            }

            for (var i = 0; i < 4; ++i)
            {
                var edgeLng = S1Interval.FromPointPair(
                    cellLl[i].Lng.Radians, cellLl[(i + 1) & 3].Lng.Radians);
                if (!_lng.Intersects(edgeLng))
                {
                    continue;
                }

                var a = cellV[i];
                var b = cellV[(i + 1) & 3];
                if (edgeLng.Contains(_lng.Lo))
                {
                    if (IntersectsLngEdge(a, b, _lat, _lng.Lo))
                    {
                        return(true);
                    }
                }
                if (edgeLng.Contains(_lng.Hi))
                {
                    if (IntersectsLngEdge(a, b, _lat, _lng.Hi))
                    {
                        return(true);
                    }
                }
                if (IntersectsLatEdge(a, b, _lat.Lo, _lng))
                {
                    return(true);
                }
                if (IntersectsLatEdge(a, b, _lat.Hi, _lng))
                {
                    return(true);
                }
            }
            return(false);
        }