1 // Copyright 2005 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Original author: ericv@google.com (Eric Veach)
16 // Converted to D:  madric@gmail.com (Vijay Nayar)
17 
18 /// A two-dimentional region over the unit sphere.
19 module s2.s2region;
20 
21 import s2.s2cap;
22 import s2.s2cell;
23 import s2.s2cell_id;
24 import s2.s2latlng_rect;
25 import s2.s2point;
26 
27 /**
28  * An S2Region represents a two-dimensional region over the unit sphere.
29  * It is an abstract interface with various concrete subtypes.
30  *
31  * The main purpose of this interface is to allow complex regions to be
32  * approximated as simpler regions.  So rather than having a wide variety
33  * of virtual methods that are implemented by all subtypes, the interface
34  * is restricted to methods that are useful for computing approximations.
35  */
36 interface S2Region {
37 
38   /**
39    * Returns a deep copy of the region.
40    *
41    * Note that each subtype of S2Region returns an object reference of its
42    * own type (e.g., S2Cap.clone() returns an S2Cap).
43    */
44   S2Region clone();
45 
46   /**
47    * Returns a bounding spherical cap that contains the region.  The bound may
48    * not be tight.
49    */
50   S2Cap getCapBound();
51 
52   /**
53    * Returns a bounding latitude-longitude rectangle that contains the region.
54    * The bound may not be tight.
55    */
56   S2LatLngRect getRectBound();
57 
58   /**
59    * Returns a small collection of S2CellIds whose union covers the region.
60    * The cells are not sorted, may have redundancies (such as cells that
61    * contain other cells), and may cover much more area than necessary.
62    *
63    * This method is not intended for direct use by client code.  Clients
64    * should typically use S2RegionCoverer::GetCovering, which has options to
65    * control the size and accuracy of the covering.  Alternatively, if you
66    * want a fast covering and don't care about accuracy, consider calling
67    * S2RegionCoverer::GetFastCovering (which returns a cleaned-up version of
68    * the covering computed by this method).
69    *
70    * GetCellUnionBound() implementations should attempt to return a small
71    * covering (ideally 4 cells or fewer) that covers the region and can be
72    * computed quickly.  The result is used by S2RegionCoverer as a starting
73    * point for further refinement.
74    *
75    * TODO(ericv): Remove the default implementation.
76    */
77   void getCellUnionBound(out S2CellId[] cellIds);
78 
79   /**
80    * Returns true if the region completely contains the given cell, otherwise
81    * returns false.
82    */
83   bool contains(in S2Cell cell);
84 
85   /**
86    * If this method returns false, the region does not intersect the given
87    * cell.  Otherwise, either region intersects the cell, or the intersection
88    * relationship could not be determined.
89    *
90    * Note that there is currently exactly one implementation of this method
91    * (S2LatLngRect::MayIntersect) that takes advantage of the semantics above
92    * to be more efficient.  For all other S2Region subtypes, this method
93    * returns true if the region intersect the cell and false otherwise.
94    */
95   bool mayIntersect(in S2Cell cell);
96 
97   /**
98    * Returns true if and only if the given point is contained by the region.
99    * The point 'p' is generally required to be unit length, although some
100    * subtypes may relax this restriction.
101    */
102   bool contains(in S2Point p);
103 
104   //////////////////////////////////////////////////////////////////////////
105   // Many S2Region subtypes also define the following non-virtual methods.
106   //////////////////////////////////////////////////////////////////////////
107 
108   /**
109    * Appends a serialized representation of the region to "encoder".
110    *
111    * The representation chosen is left up to the sub-classes but it should
112    * satisfy the following constraints:
113    * - It should encode a version number.
114    * - It should be deserializable using the corresponding Decode method.
115    * - Performance, not space, should be the chief consideration. Encode() and
116    *   Decode() should be implemented such that the combination is equivalent
117    *   to calling Clone().
118    *
119    * REQUIRES: "encoder" uses the default constructor, so that its buffer
120    *           can be enlarged as necessary by calling Ensure(int).
121    *
122    */
123   //void encode(ORangeT)(Encoder!ORangeT encoder) const;
124 
125   /**
126    * Decodes an S2Region encoded with Encode().  Note that this method
127    * requires that an S2Region object of the appropriate concrete type has
128    * already been constructed.  It is not possible to decode regions of
129    * unknown type.
130    *
131    * Whenever the Decode method is changed to deal with new serialized
132    * representations, it should be done so in a manner that allows for
133    * older versions to be decoded i.e. the version number in the serialized
134    * representation should be used to decide how to decode the data.
135    *
136    * Returns true on success.
137    */
138   //bool decode(IRangeT)(Decoder!IRangeT decoder);
139 
140   // Provides the same functionality as Decode, except that decoded regions
141   // are allowed to point directly into the Decoder's memory buffer rather
142   // than copying the data.  This method can be much faster for regions that
143   // have a lot of data (such as polygons), but the decoded region is only
144   // valid within the scope (lifetime) of the Decoder's memory buffer.
145   //
146   // bool DecodeWithinScope(Decoder* const decoder);
147 }