Skip to content

Commit 91f0341

Browse files
authored
Merge branch 'master' into feature/cell-index
2 parents 3cea372 + 0b6e08c commit 91f0341

File tree

118 files changed

+7135
-2472
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+7135
-2472
lines changed

.github/workflows/go.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will checkout, build, and run the tests for the Go S2 project.
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go Build and Test
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
pull_request:
10+
branches: [ "master" ]
11+
12+
jobs:
13+
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v5
21+
with:
22+
go-version-file: 'go.mod'
23+
24+
- name: Build
25+
run: go build -v ./...
26+
27+
- name: Test
28+
run: go test -v ./...

README.md

Lines changed: 131 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ S2 is a library for spherical geometry that aims to have the same robustness,
44
flexibility, and performance as the best planar geometry libraries.
55

66
This is a library for manipulating geometric shapes. Unlike many geometry
7-
libraries, S2 is primarily designed to work with _spherical geometry_, i.e.,
7+
libraries, S2 is primarily designed to work with *spherical geometry*, i.e.,
88
shapes drawn on a sphere rather than on a planar 2D map. (In fact, the name S2
99
is derived from the mathematical notation for the unit sphere **.) This makes
1010
it especially suitable for working with geographic data.
@@ -92,109 +92,150 @@ This library is principally a port of the
9292
where it makes sense. We detail the progress of this port below relative to that
9393
C++ library.
9494

95+
Legend:
96+
97+
* ✅ - Feature Complete
98+
* 🟡 - Mostly Complete
99+
* ❌ - Not available
100+
95101
## [ℝ¹](https://godoc.org/github.com/golang/geo/r1) - One-dimensional Cartesian coordinates
96102

97-
Full parity with C++.
103+
C++ Type | Go
104+
:--------- | ---
105+
R1Interval | ✅
98106

99107
## [ℝ²](https://godoc.org/github.com/golang/geo/r2) - Two-dimensional Cartesian coordinates
100108

101-
Full parity with C++.
109+
C++ Type | Go
110+
:------- | ---
111+
R2Point | ✅
112+
R2Rect | ✅
102113

103114
## [ℝ³](https://godoc.org/github.com/golang/geo/r3) - Three-dimensional Cartesian coordinates
104115

105-
Full parity with C++.
116+
C++ Type | Go
117+
:------------ | ---
118+
R3Vector | ✅
119+
R3ExactVector | ✅
120+
Matrix3x3 | ✅
106121

107122
## [](https://godoc.org/github.com/golang/geo/s1) - Circular Geometry
108123

109-
Full parity with C++.
124+
C++ Type | Go
125+
:----------- | ---
126+
S1Angle | ✅
127+
S1ChordAngle | ✅
128+
S1Interval | ✅
110129

111130
## [](https://godoc.org/github.com/golang/geo/s2) - Spherical Geometry
112131

113-
Approximately ~40% complete.
114-
115-
**Complete** These files have full parity with the C++ implementation.
116-
117-
* Cap
118-
* Cell
119-
* CellID
120-
* CellUnion
121-
* ContainsVertexQuery
122-
* ConvexHullQuery
123-
* CrossingEdgeQuery
124-
* LatLng
125-
* matrix3x3
126-
* Metric
127-
* PaddedCell
128-
* Point
129-
* PointCompression
130-
* Region
131-
* RegionCoverer
132-
* RegionUnion
133-
* s2edge_clipping
134-
* s2edge_crosser
135-
* s2edge_crossings
136-
* s2edge_distances
137-
* edgeVectorShape
138-
* laxLoop
139-
* laxPolyline
140-
* s2projections - Helpers for projecting points between R2 and S2.
141-
* s2rect_bounder
142-
* s2stuv.go (s2coords.h in C++) - This file is a collection of helper and
143-
conversion methods to and from ST-space, UV-space, and XYZ-space.
144-
* s2wedge_relations
145-
* ShapeIndex
146-
* idSetLexicon,sequenceLexicon
147-
148-
**Mostly Complete** Files that have almost all of the features of the original
149-
C++ code, and are reasonably complete enough to use in live code. Up to date
150-
listing of the incomplete methods are documented at the end of each file.
151-
152-
* EdgeQuery/Closest/Furthest - missing Project, GetEdge
153-
* ContainsPointQuery - missing visit edges
154-
* laxPolygon
155-
* Loop - Loop is mostly complete now. Missing Project, Distance, Union, etc.
156-
* Polyline - Missing InitTo... methods, NearlyCoversPolyline
157-
* Rect (AKA s2latlngrect in C++) - Missing Centroid, InteriorContains.
158-
* s2_test.go (AKA s2testing and s2textformat in C++) - Missing Fractal test
159-
shape generation. This file is a collection of testing helper methods.
160-
* s2edge_distances - Missing Intersection
161-
162-
**In Progress** Files that have some work done, but are probably not complete
163-
enough for general use in production code.
164-
165-
* CellIndex - A queryable index of CellIDs.
166-
* Polygon - Polygons with multiple loops are supported. It fully implements
167-
Shape and Region, but it's missing most other methods. (Area, Centroid,
168-
Distance, Projection, Intersection, Union, Contains, Normalized, etc.)
169-
* PolylineSimplifier - Initial work has begun on this.
170-
* s2predicates.go - This file is a collection of helper methods used by other
171-
parts of the library.
172-
* s2shapeutil - Initial elements added. Missing VisitCrossings.
173-
174-
**Not Started Yet.** These files (and their associated unit tests) have
175-
dependencies on most of the In Progress files before they can begin to be
176-
started.
177-
178-
* BooleanOperation - used when assembling polygons and loops.
179-
* Builder - This is a robust tool for creating the various Shape types from
180-
collection of simpler S2 types.
181-
* BuilderClosedSetNormalizer
182-
* BuilderFindPolygonDegneracies
183-
* BuilderGraph
184-
* BuilderLayers
185-
* BuilderSnapFunctions
186-
* BuilderTesting
187-
* Centroids
188-
* ClosestPointQuery
189-
* EdgeTesselator
190-
* LoopMeasures
191-
* PointIndex
192-
* PointRegion
193-
* PointUtil
194-
* PolygonMeasures
195-
* RegionIntersection
196-
* RegionTermIndexer
197-
* ShapeIndexRegion - Allows ShapeIndexes to be used as Regions for things like
132+
### Basic Types
133+
134+
C++ Type | Go
135+
:------------------- | ---
136+
S2Cap | ✅
137+
S2Cell | ✅
138+
S2CellId | ✅
139+
S2CellIdVector | ❌
140+
S2CellIndex | 🟡
141+
S2CellUnion | ✅
142+
S2Coords | ✅
143+
S2DensityTree | ❌
144+
S2DistanceTarget | ✅
145+
S2EdgeVector | ✅
146+
S2LatLng | ✅
147+
S2LatLngRect | ✅
148+
S2LaxLoop | 🟡
149+
S2LaxPolygon | 🟡
150+
S2LaxPolyline | 🟡
151+
S2Loop | ✅
152+
S2PaddedCell | ✅
153+
S2Point | ✅
154+
S2PointIndex | ❌
155+
S2PointSpan | ❌
156+
S2PointRegion | ❌
157+
S2PointVector | ✅
158+
S2Polygon | 🟡
159+
S2Polyline | ✅
160+
S2R2Rect | ❌
161+
S2Region | ✅
162+
S2RegionCoverer | ✅
163+
S2RegionIntersection | ❌
164+
S2RegionUnion | ✅
165+
S2Shape | ✅
166+
S2ShapeIndex | ✅
167+
S2ShapeIndexRegion | ❌
168+
EncodedLaxPolygon | ❌
169+
EncodedLaxPolyline | ❌
170+
EncodedShapeIndex | ❌
171+
EncodedStringVector | ❌
172+
EncodedUintVector | ❌
173+
IdSetLexicon | ❌
174+
ValueSetLexicon | ❌
175+
SequenceLexicon | ❌
176+
LaxClosedPolyline | ❌
177+
VertexIDLaxLoop | ❌
178+
179+
### Query Types
180+
181+
C++ Type | Go
182+
:------------------- | ---
183+
S2ChainInterpolation | ❌
184+
S2ClosestCell | ❌
185+
S2FurthestCell | ❌
186+
S2ClosestEdge | ✅
187+
S2FurthestEdge | ✅
188+
S2ClosestPoint | ❌
189+
S2FurthestPoint | ❌
190+
S2ContainsPoint | ✅
191+
S2ContainsVertex | ✅
192+
S2ConvexHull | ✅
193+
S2CrossingEdge | ✅
194+
S2HausdorffDistance | ❌
195+
S2ShapeNesting | ❌
196+
197+
### Supporting Types
198+
199+
C++ Type | Go
200+
:------------------------------- | ---
201+
S2BooleanOperation | ❌
202+
S2BufferOperation | ❌
203+
S2Builder | ❌
204+
S2BuilderClosedSetNormalizer | ❌
205+
S2BuilderFindPolygonDegeneracies | ❌
206+
S2BuilderGraph | ❌
207+
S2BuilderLayers | ❌
208+
S2BuilderSnapFunctions | ❌
209+
S2BuilderTesting | ❌
210+
S2Builderutil\* | ❌
211+
S2Coder | ❌
212+
S2EdgeClipping | ✅
213+
S2EdgeCrosser | ✅
214+
S2EdgeCrossings | ✅
215+
S2EdgeDistances | ✅
216+
S2EdgeTessellator | ✅
217+
S2LoopMeasures | ❌
218+
S2Measures | ✅
219+
S2MemoryTracker | ❌
220+
S2Metrics | ❌
221+
S2PointUtil | 🟡
222+
S2PolygonBuilder | ❌
223+
S2PolylineAlignment | ❌
224+
S2PolylineMeasures | ✅
225+
S2PolylineSimplifier | ❌
226+
S2Predicates | ✅
227+
S2Projections | ❌
228+
S2rectBounder | ❌
229+
S2RegionTermIndexer | ❌
230+
S2ShapeIndexMeasures | ❌
231+
S2ShapeIndexUtil\* | 🟡
232+
S2ShapeMeasures | ❌
233+
S2ShapeUtil\* | 🟡
234+
S2Stats | ❌
235+
S2Testing | ✅
236+
S2TextFormat | ✅
237+
S2WedgeRelations | ✅
238+
S2WindingOperation | ❌
198239

199240
### Encode/Decode
200241

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/golang/geo
22

3-
go 1.12
3+
go 1.21
4+
5+
require github.com/google/go-cmp v0.7.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
2+
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=

r1/interval.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ func (i Interval) ApproxEqual(other Interval) bool {
165165

166166
// DirectedHausdorffDistance returns the Hausdorff distance to the given interval. For two
167167
// intervals x and y, this distance is defined as
168-
// h(x, y) = max_{p in x} min_{q in y} d(p, q).
168+
//
169+
// h(x, y) = max_{p in x} min_{q in y} d(p, q).
169170
func (i Interval) DirectedHausdorffDistance(other Interval) float64 {
170171
if i.IsEmpty() {
171172
return 0

r3/precisevector.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ import (
2020
)
2121

2222
const (
23-
// prec is the number of bits of precision to use for the Float values.
24-
// To keep things simple, we use the maximum allowable precision on big
25-
// values. This allows us to handle all values we expect in the s2 library.
26-
prec = big.MaxPrec
23+
// MaxPrec is the number of bits of precision to use for the Float values.
24+
// To keep things simple, we match the limit used in the C++ library.
25+
// This allows us to handle all values we expect in the s2 library.
26+
MaxPrec = 64 << 20 // see C++'s util/math/exactfloat/exactfloat.h
27+
28+
// MaxExp is the maximum exponent supported. If a value has an exponent larger than
29+
// this, it is replaced by infinity (with the appropriate sign).
30+
MaxExp = 200 * 1000 * 1000 // About 10**(60 million)
31+
32+
// MinExp is the minimum exponent supported. If a value has an exponent less than
33+
// this, it is replaced by zero (with the appropriate sign).
34+
MinExp = -MaxExp // About 10**(-60 million)
2735
)
2836

2937
// define some commonly referenced values.
@@ -37,28 +45,28 @@ var (
3745
// are integer multiples of integer powers of 2.
3846
func precStr(s string) *big.Float {
3947
// Explicitly ignoring the bool return for this usage.
40-
f, _ := new(big.Float).SetPrec(prec).SetString(s)
48+
f, _ := new(big.Float).SetPrec(MaxPrec).SetString(s)
4149
return f
4250
}
4351

4452
func precInt(i int64) *big.Float {
45-
return new(big.Float).SetPrec(prec).SetInt64(i)
53+
return new(big.Float).SetPrec(MaxPrec).SetInt64(i)
4654
}
4755

4856
func precFloat(f float64) *big.Float {
49-
return new(big.Float).SetPrec(prec).SetFloat64(f)
57+
return new(big.Float).SetPrec(MaxPrec).SetFloat64(f)
5058
}
5159

5260
func precAdd(a, b *big.Float) *big.Float {
53-
return new(big.Float).SetPrec(prec).Add(a, b)
61+
return new(big.Float).SetPrec(MaxPrec).Add(a, b)
5462
}
5563

5664
func precSub(a, b *big.Float) *big.Float {
57-
return new(big.Float).SetPrec(prec).Sub(a, b)
65+
return new(big.Float).SetPrec(MaxPrec).Sub(a, b)
5866
}
5967

6068
func precMul(a, b *big.Float) *big.Float {
61-
return new(big.Float).SetPrec(prec).Mul(a, b)
69+
return new(big.Float).SetPrec(MaxPrec).Mul(a, b)
6270
}
6371

6472
// PreciseVector represents a point in ℝ³ using high-precision values.
@@ -196,3 +204,8 @@ func (v PreciseVector) SmallestComponent() Axis {
196204
}
197205
return ZAxis
198206
}
207+
208+
// IsZero reports if this vector is exactly 0 efficiently.
209+
func (v PreciseVector) IsZero() bool {
210+
return v.X.Sign() == 0 && v.Y.Sign() == 0 && v.Z.Sign() == 0
211+
}

0 commit comments

Comments
 (0)