Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
/ geo Public archive
forked from golang/geo

Commit

Permalink
Fix inappropriate normalisation of s2.Points throughout.
Browse files Browse the repository at this point in the history
PointFromCoords normalizes, but that isn't often wanted.

Signed-off-by: David Symonds <[email protected]>
  • Loading branch information
rsned authored and dsymonds committed Jan 12, 2017
1 parent 11b3244 commit 07838e5
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 301 deletions.
4 changes: 2 additions & 2 deletions s2/cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ const (
)

var (
// centerPoint is the default center for S2Caps
centerPoint = Point{PointFromCoords(1.0, 0, 0).Normalize()}
// centerPoint is the default center for Caps
centerPoint = PointFromCoords(1.0, 0, 0)
)

// Cap represents a disc-shaped region defined by a center and radius.
Expand Down
47 changes: 23 additions & 24 deletions s2/cap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"math"
"testing"

"github.com/golang/geo/r3"
"github.com/golang/geo/s1"
)

Expand All @@ -32,18 +33,16 @@ var (
fullCap = FullCap()
defaultCap = EmptyCap()

xAxisPt = PointFromCoords(1, 0, 0)
yAxisPt = PointFromCoords(0, 1, 0)
xAxisPt = Point{r3.Vector{1, 0, 0}}
yAxisPt = Point{r3.Vector{0, 1, 0}}

xAxis = CapFromPoint(xAxisPt)
yAxis = CapFromPoint(yAxisPt)
xComp = xAxis.Complement()

hemi = CapFromCenterHeight(Point{PointFromCoords(1, 0, 1).Normalize()}, 1)
concave = CapFromCenterAngle(PointFromLatLng(LatLngFromDegrees(80, 10)),
s1.Angle(150.0)*s1.Degree)
tiny = CapFromCenterAngle(Point{PointFromCoords(1, 2, 3).Normalize()},
s1.Angle(tinyRad))
hemi = CapFromCenterHeight(PointFromCoords(1, 0, 1), 1)
concave = CapFromCenterAngle(PointFromLatLng(LatLngFromDegrees(80, 10)), s1.Angle(150.0)*s1.Degree)
tiny = CapFromCenterAngle(PointFromCoords(1, 2, 3), s1.Angle(tinyRad))
)

func TestCapBasicEmptyFullValid(t *testing.T) {
Expand Down Expand Up @@ -151,20 +150,20 @@ func TestCapContainsPoint(t *testing.T) {
// math optimizations that are permissible (FMA vs no FMA) that yield
// slightly different floating point results between gccgo and gc.
const epsilon = 1e-14
tangent := tiny.center.Cross(PointFromCoords(3, 2, 1).Vector).Normalize()
tangent := tiny.center.Cross(r3.Vector{3, 2, 1}).Normalize()
tests := []struct {
c Cap
p Point
want bool
}{
{xAxis, xAxisPt, true},
{xAxis, PointFromCoords(1, 1e-20, 0), false},
{xAxis, Point{r3.Vector{1, 1e-20, 0}}, false},
{yAxis, xAxis.center, false},
{xComp, xAxis.center, true},
{xComp.Complement(), xAxis.center, false},
{tiny, Point{tiny.center.Add(tangent.Mul(tinyRad * 0.99))}, true},
{tiny, Point{tiny.center.Add(tangent.Mul(tinyRad * 1.01))}, false},
{hemi, Point{PointFromCoords(1, 0, -(1 - epsilon)).Normalize()}, true},
{hemi, PointFromCoords(1, 0, -(1 - epsilon)), true},
{hemi, xAxisPt, true},
{hemi.Complement(), xAxisPt, false},
{concave, PointFromLatLng(LatLngFromDegrees(-70*(1-epsilon), 10)), true},
Expand Down Expand Up @@ -204,9 +203,9 @@ func TestCapInteriorIntersects(t *testing.T) {
}

func TestCapInteriorContains(t *testing.T) {
if hemi.InteriorContainsPoint(Point{PointFromCoords(1, 0, -(1 + epsilon)).Normalize()}) {
if hemi.InteriorContainsPoint(Point{r3.Vector{1, 0, -(1 + epsilon)}}) {
t.Errorf("hemi (%v) should not contain point just past half way(%v)", hemi,
Point{PointFromCoords(1, 0, -(1 + epsilon)).Normalize()})
Point{r3.Vector{1, 0, -(1 + epsilon)}})
}
}

Expand Down Expand Up @@ -304,7 +303,7 @@ func TestCapRectBounds(t *testing.T) {
},
{
"The eastern hemisphere.",
CapFromCenterAngle(PointFromCoords(0, 1, 0), s1.Radian*(math.Pi/2+2e-16)),
CapFromCenterAngle(Point{r3.Vector{0, 1, 0}}, s1.Radian*(math.Pi/2+2e-16)),
-90, 90, -180, 180, true,
},
{
Expand Down Expand Up @@ -364,34 +363,34 @@ func TestCapAddPoint(t *testing.T) {
{yAxis, yAxisPt, yAxis},

// Cap plus opposite point equals full.
{xAxis, PointFromCoords(-1, 0, 0), fullCap},
{yAxis, PointFromCoords(0, -1, 0), fullCap},
{xAxis, Point{r3.Vector{-1, 0, 0}}, fullCap},
{yAxis, Point{r3.Vector{0, -1, 0}}, fullCap},

// Cap plus orthogonal axis equals half cap.
{xAxis, PointFromCoords(0, 0, 1), CapFromCenterAngle(xAxisPt, s1.Angle(math.Pi/2.0))},
{xAxis, PointFromCoords(0, 0, -1), CapFromCenterAngle(xAxisPt, s1.Angle(math.Pi/2.0))},
{xAxis, Point{r3.Vector{0, 0, 1}}, CapFromCenterAngle(xAxisPt, s1.Angle(math.Pi/2.0))},
{xAxis, Point{r3.Vector{0, 0, -1}}, CapFromCenterAngle(xAxisPt, s1.Angle(math.Pi/2.0))},

// The 45 degree angled hemisphere plus some points.
{
hemi,
PointFromCoords(0, 1, -1),
CapFromCenterAngle(Point{PointFromCoords(1, 0, 1).Normalize()},
CapFromCenterAngle(Point{r3.Vector{1, 0, 1}},
s1.Angle(120.0)*s1.Degree),
},
{
hemi,
PointFromCoords(0, -1, -1),
CapFromCenterAngle(Point{PointFromCoords(1, 0, 1).Normalize()},
CapFromCenterAngle(Point{r3.Vector{1, 0, 1}},
s1.Angle(120.0)*s1.Degree),
},
{
hemi,
PointFromCoords(-1, -1, -1),
CapFromCenterAngle(Point{PointFromCoords(1, 0, 1).Normalize()},
CapFromCenterAngle(Point{r3.Vector{1, 0, 1}},
s1.Angle(math.Acos(-math.Sqrt(2.0/3.0)))),
},
{hemi, PointFromCoords(0, 1, 1), hemi},
{hemi, PointFromCoords(1, 0, 0), hemi},
{hemi, Point{r3.Vector{0, 1, 1}}, hemi},
{hemi, Point{r3.Vector{1, 0, 0}}, hemi},
}

for _, test := range tests {
Expand Down Expand Up @@ -672,8 +671,8 @@ func TestCapUnion(t *testing.T) {
t.Errorf("%v.Radius = %v, want %v", aUnionE, got, want)
}

p1 := Point{PointFromCoords(0, 0, 1).Normalize()}
p2 := Point{PointFromCoords(0, 1, 0).Normalize()}
p1 := Point{r3.Vector{0, 0, 1}}
p2 := Point{r3.Vector{0, 1, 0}}
// Two very large caps, whose radius sums to in excess of 180 degrees, and
// whose centers are not antipodal.
f := CapFromCenterAngle(p1, s1.Degree*150)
Expand Down
4 changes: 2 additions & 2 deletions s2/edgeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (r *RectBounder) AddPoint(b Point) {
// and B attains its minimum and maximum latitudes). To test whether AB
// crosses this plane, we compute a vector M perpendicular to this
// plane and then project A and B onto it.
m := n.Cross(PointFromCoords(0, 0, 1).Vector)
m := n.Cross(r3.Vector{0, 0, 1})
mA := m.Dot(r.a.Vector)
mB := m.Dot(b.Vector)

Expand Down Expand Up @@ -845,7 +845,7 @@ func clipDestination(a, b, scaledN, aTan, bTan pointUVW, scaleUV float64) (r2.Po
// Otherwise find the point B' where the line AB exits the face.
uv = scaledN.exitPoint(scaledN.exitAxis()).Mul(scaleUV)

p := pointUVW(PointFromCoords(uv.X, uv.Y, 1.0))
p := pointUVW(Point{r3.Vector{uv.X, uv.Y, 1.0}})

// Determine if the exit point B' is contained within the segment. We do this
// by computing the dot products with two inward-facing tangent vectors at A
Expand Down
Loading

0 comments on commit 07838e5

Please sign in to comment.