...

Source file src/github.com/golang/geo/s2/edge_tessellator_test.go

Documentation: github.com/golang/geo/s2

     1  // Copyright 2018 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  package s2
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  
    21  	"github.com/golang/geo/r2"
    22  	"github.com/golang/geo/s1"
    23  )
    24  
    25  func TestEdgeTessellatorProjectedNoTessellation(t *testing.T) {
    26  	proj := NewPlateCarreeProjection(180)
    27  	tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
    28  
    29  	var vertices []r2.Point
    30  	vertices = tess.AppendProjected(PointFromCoords(1, 0, 0), PointFromCoords(0, 1, 0), vertices)
    31  
    32  	if len(vertices) != 2 {
    33  		t.Errorf("2 points which don't need tessellation should only end with 2 points. got %d points", len(vertices))
    34  	}
    35  }
    36  
    37  func TestEdgeTessellatorUnprojectedNoTessellation(t *testing.T) {
    38  	proj := NewPlateCarreeProjection(180)
    39  	tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
    40  
    41  	var vertices []Point
    42  	vertices = tess.AppendUnprojected(r2.Point{0, 30}, r2.Point{0, 50}, vertices)
    43  
    44  	if len(vertices) != 2 {
    45  		t.Errorf("2 points which don't need tessellation should only end with 2 points. got %d points", len(vertices))
    46  	}
    47  }
    48  
    49  func TestEdgeTessellatorUnprojectedWrapping(t *testing.T) {
    50  	// This tests that a projected edge that crosses the 180 degree meridian
    51  	// goes the "short way" around the sphere.
    52  	proj := NewPlateCarreeProjection(180)
    53  	tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
    54  
    55  	var vertices []Point
    56  	vertices = tess.AppendUnprojected(r2.Point{-170, 0}, r2.Point{170, 80}, vertices)
    57  	for i, v := range vertices {
    58  		if got := math.Abs(longitude(v).Degrees()); got < 170 {
    59  			t.Errorf("unprojected segment %d should be close to the meridian. got %v, want >= 170", i, got)
    60  		}
    61  	}
    62  }
    63  
    64  func TestEdgeTessellatorProjectedWrapping(t *testing.T) {
    65  	// This tests projecting a geodesic edge that crosses the 180 degree
    66  	// meridian.  This results in a set of vertices that may be non-canonical
    67  	// (i.e., absolute longitudes greater than 180 degrees) but that don't have
    68  	// any sudden jumps in value, which is convenient for interpolating them.
    69  	proj := NewPlateCarreeProjection(180)
    70  	tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
    71  
    72  	var vertices []r2.Point
    73  	vertices = tess.AppendProjected(PointFromLatLng(LatLngFromDegrees(0, -170)), PointFromLatLng(LatLngFromDegrees(0, 170)), vertices)
    74  	for i, v := range vertices {
    75  		if v.X > -170 {
    76  			t.Errorf("projected vertex %d should be close to the meridian, got %v, want <= -170 ", i, v.X)
    77  		}
    78  	}
    79  }
    80  
    81  func TestEdgeTessellatorUnprojectedWrappingMultipleCrossings(t *testing.T) {
    82  	// Tests an edge chain that crosses the 180 degree meridian multiple times.
    83  	// Note that due to coordinate wrapping, the last vertex of one edge may not
    84  	// exactly match the first edge of the next edge after unprojection.
    85  	proj := NewPlateCarreeProjection(180)
    86  	tess := NewEdgeTessellator(proj, 0.01*s1.Degree)
    87  
    88  	var vertices []Point
    89  	for lat := 1.0; lat <= 60; lat += 1.0 {
    90  		vertices = tess.AppendUnprojected(r2.Point{180 - 0.03*lat, lat},
    91  			r2.Point{-180 + 0.07*lat, lat}, vertices)
    92  		vertices = tess.AppendUnprojected(r2.Point{-180 + 0.07*lat, lat},
    93  			r2.Point{180 - 0.03*(lat+1), lat + 1}, vertices)
    94  	}
    95  
    96  	for i, v := range vertices {
    97  		if got := math.Abs(longitude(v).Degrees()); got < 175 {
    98  			t.Errorf("vertex %d should be close to the meridian, got %v", i, got)
    99  		}
   100  	}
   101  }
   102  
   103  func TestEdgeTessellatorProjectedWrappingMultipleCrossings(t *testing.T) {
   104  	// The following loop crosses the 180 degree meridian four times (twice in
   105  	// each direction).
   106  	loop := parsePoints("0:160, 0:-40, 0:120, 0:-80, 10:120, 10:-40, 0:160")
   107  	proj := NewPlateCarreeProjection(180)
   108  	tess := NewEdgeTessellator(proj, 1e-7*s1.Degree)
   109  
   110  	var vertices []r2.Point
   111  	for i := 0; i+1 < len(loop); i++ {
   112  		vertices = tess.AppendProjected(loop[i], loop[i+1], vertices)
   113  	}
   114  	if got, want := vertices[0], vertices[len(vertices)-1]; got != want {
   115  		t.Errorf("the first and last vertices should be the same. got %v, want %v", got, want)
   116  	}
   117  
   118  	// Note that the r2.Point coordinates are in (lng, lat) order.
   119  	minLng := vertices[0].X
   120  	maxLng := vertices[0].X
   121  	for _, v := range vertices {
   122  		minLng = math.Min(minLng, v.X)
   123  		maxLng = math.Max(maxLng, v.X)
   124  	}
   125  	if minLng != 160 {
   126  		t.Errorf("minLng = %v, want %v", minLng, 160)
   127  	}
   128  	if maxLng != 640 {
   129  		t.Errorf("maxLng = %v, want %v", maxLng, 640)
   130  	}
   131  }
   132  
   133  // TODO(roberts): Differences from C++
   134  // The DistStats accuracy by exhaustion test cases.
   135  

View as plain text