Skip to content

Commit 1daa5fa

Browse files
author
Sreekanth Sivasankaran
committed
Introduce new APIs,
Polygon APIs, -Project, -ProjectToBoundary Edge Query APIs, -GetEdge -Project
1 parent 187b978 commit 1daa5fa

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

s2/edge_query.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,19 @@ func (e *EdgeQuery) processOrEnqueue(id CellID, indexCell *ShapeIndexCell) {
798798
})
799799
}
800800

801+
func (e *EdgeQuery) GetEdge(result EdgeQueryResult) Edge {
802+
return e.index.Shape(result.shapeID).Edge(int(result.edgeID))
803+
}
804+
805+
func (e *EdgeQuery) Project(point Point, result EdgeQueryResult) Point {
806+
if result.edgeID < 0 {
807+
return point
808+
}
809+
810+
edge := e.GetEdge(result)
811+
return Project(point, edge.V0, edge.V1)
812+
}
813+
801814
// TODO(roberts): Remaining pieces
802815
// GetEdge
803816
// Project

s2/polygon.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,21 @@ func (p *Polygon) decodeCompressed(d *decoder) {
11841184
p.initLoopProperties()
11851185
}
11861186

1187+
func (p *Polygon) Project(point *Point) Point {
1188+
if p.ContainsPoint(*point) {
1189+
return *point
1190+
}
1191+
return p.ProjectToBoundary(point)
1192+
}
1193+
1194+
func (p *Polygon) ProjectToBoundary(point *Point) Point {
1195+
options := NewClosestEdgeQueryOptions().MaxResults(1).IncludeInteriors(false)
1196+
q := NewClosestEdgeQuery(p.index, options)
1197+
target := NewMinDistanceToPointTarget(*point)
1198+
edges := q.FindEdges(target)
1199+
return q.Project(*point, edges[0])
1200+
}
1201+
11871202
// TODO(roberts): Differences from C++
11881203
// Centroid
11891204
// SnapLevel

s2/polygon_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,29 @@ func TestPolygonInvert(t *testing.T) {
11241124
}
11251125
}
11261126

1127+
func TestPolygonProject(t *testing.T) {
1128+
polygon := makePolygon(nearLoop0+nearLoop2, true)
1129+
point := PointFromLatLng(LatLngFromDegrees(1.1, 0))
1130+
projected := polygon.Project(&point)
1131+
if !projected.ApproxEqual(point) {
1132+
t.Errorf("projection %v failed for %v", projected, point)
1133+
}
1134+
1135+
point = PointFromLatLng(LatLngFromDegrees(5.1, -2))
1136+
projected = polygon.Project(&point)
1137+
expected := PointFromLatLng(LatLngFromDegrees(5, -2))
1138+
if !projected.ApproxEqual(expected) {
1139+
t.Errorf("projection %v failed for %v", projected, expected)
1140+
}
1141+
1142+
point = PointFromLatLng(LatLngFromDegrees(0, -3))
1143+
projected = polygon.Project(&point)
1144+
expected = PointFromLatLng(LatLngFromDegrees(0, -2))
1145+
if !projected.ApproxEqual(expected) {
1146+
t.Errorf("projection %v failed for %v", projected, expected)
1147+
}
1148+
}
1149+
11271150
// TODO(roberts): Remaining Tests
11281151
// TestInit
11291152
// TestMultipleInit

0 commit comments

Comments
 (0)