...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2023 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  	"testing"
    19  
    20  	"github.com/golang/geo/s1"
    21  )
    22  
    23  func TestIdentitySnapper(t *testing.T) {
    24  	rad := s1.Angle(1.0)
    25  	i := NewIdentitySnapper(rad)
    26  
    27  	if i.SnapRadius() != rad {
    28  		t.Errorf("identSnap.SnapRadius() = %v, want %v", i.SnapRadius(), rad)
    29  	}
    30  
    31  	if i.MinVertexSeparation() != rad {
    32  		t.Errorf("identSnap.MinVertexSeparation() = %v, want %v", i.MinVertexSeparation(), rad)
    33  	}
    34  
    35  	if i.MinEdgeVertexSeparation() != 0.5*rad {
    36  		t.Errorf("identSnap.MinEdgeVertexSeparation() = %v, want %v", i.MinEdgeVertexSeparation(), 0.5*rad)
    37  	}
    38  
    39  	if i.MaxEdgeDeviation() != maxEdgeDeviationRatio {
    40  		t.Errorf("identSnap.SnapRadius() = %v, want %v", i.MaxEdgeDeviation(), maxEdgeDeviationRatio)
    41  	}
    42  
    43  	p := randomPoint()
    44  	if got := i.SnapPoint(p); !p.ApproxEqual(got) {
    45  		t.Errorf("identSnap.SnapPoint(%v) = %v, want %v", p, got, p)
    46  	}
    47  
    48  }
    49  
    50  func TestCellIDSnapperLevelToFromSnapRadius(t *testing.T) {
    51  	f := NewCellIDSnapper()
    52  	for level := 0; level <= MaxLevel; level++ {
    53  		radius := f.minSnapRadiusForLevel(level)
    54  		if got := f.levelForMaxSnapRadius(radius); got != level {
    55  			t.Errorf("levelForMaxSnapRadius(%v) = %v, want %v", radius, got, level)
    56  		}
    57  		if got, want := f.levelForMaxSnapRadius(0.999*radius), minInt(level+1, MaxLevel); got != want {
    58  			t.Errorf("levelForMaxSnapRadius(0.999*%v) = %v, want %v (level %d)", radius, got, want, level)
    59  		}
    60  	}
    61  
    62  	if got := f.levelForMaxSnapRadius(5); got != 0 {
    63  		t.Errorf("levelForMaxSnapRadius(5) = %v, want 0", got)
    64  	}
    65  	if got := f.levelForMaxSnapRadius(1e-30); got != MaxLevel {
    66  		t.Errorf("levelForMaxSnapRadius(1e-30) = %v, want 0", got)
    67  	}
    68  }
    69  
    70  func TestCellIDSnapperSnapPoint(t *testing.T) {
    71  	for iter := 0; iter < 1; iter++ {
    72  		for level := 0; level <= MaxLevel; level++ {
    73  			// This checks that points are snapped to the correct level, since
    74  			// CellID centers at different levels are always different.
    75  			f := CellIDSnapperForLevel(level)
    76  			p := randomCellIDForLevel(level).Point()
    77  			if got, want := f.SnapPoint(p), p; !got.ApproxEqual(want) {
    78  				t.Errorf("%v.SnapPoint(%v) = %v, want %v", f, p, got, want)
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func TestIntLatLngSnapperExponentToFromSnapRadius(t *testing.T) {
    85  	for exp := minIntSnappingExponent; exp <= maxIntSnappingExponent; exp++ {
    86  		sf := NewIntLatLngSnapper(exp)
    87  		radius := sf.minSnapRadiusForExponent(exp)
    88  		if got := sf.exponentForMaxSnapRadius(radius); got != exp {
    89  			t.Errorf("exponentForMaxSnapRadius(%v) = %v, want %v", radius, got, exp)
    90  		}
    91  		if got, want := sf.exponentForMaxSnapRadius(0.999*radius), minInt(exp+1, maxIntSnappingExponent); got != want {
    92  			t.Errorf("exponentForMaxSnapRadius(%v) = %v, want %v", 0.999*radius, got, want)
    93  		}
    94  	}
    95  	sf := IntLatLngSnapper{}
    96  	if got := sf.exponentForMaxSnapRadius(5); got != minIntSnappingExponent {
    97  		t.Errorf("exponentForMaxSnapRadius(5) = %v, want %v", got, minIntSnappingExponent)
    98  	}
    99  	if got := sf.exponentForMaxSnapRadius(1e-30); got != maxIntSnappingExponent {
   100  		t.Errorf("exponentForMaxSnapRadius(1e-30) = %v, want %v", got, maxIntSnappingExponent)
   101  	}
   102  }
   103  
   104  /*
   105  TODO(roberts): Uncomment when LatLng helpers are incorporated.
   106  func TestIntLatLngSnapperSnapPoint(t *testing.T) {
   107  	for iter := 0; iter < 1000; iter++ {
   108  		// Test that IntLatLngSnapper does not modify points that were
   109  		// generated using the LatLngFrom{E5,E6,E7} methods. This ensures
   110  		// that both functions are using bitwise-compatible conversion methods.
   111  		p := randomPoint()
   112  		ll := LatLngFromPoint(p)
   113  
   114  		p5 := LatLngFromE5(ll.Lat.E5(), ll.Lng.E5()).ToPoint()
   115  		if got := NewIntLatLngSnapper(5).SnapPoint(p5); !p5.ApproxEqual(got) {
   116  			t.Errorf("NewIntLatLngSnapper(5).SnapPoint(%v) = %v, want %v", p5, got, p5)
   117  		}
   118  		p6 := LatLngFromE6(ll.Lat.E6(), ll.Lng.E6()).ToPoint()
   119  		if got := NewIntLatLngSnapper(6).SnapPoint(p6); !p6.ApproxEqual(got) {
   120  			t.Errorf("NewIntLatLngSnapper(6).SnapPoint(%v) = %v, want %v", p6, got, p6)
   121  		}
   122  		p7 := LatLngFromE7(ll.Lat.E7(), ll.Lng.E7()).ToPoint()
   123  		if got := NewIntLatLngSnapper(7).SnapPoint(p7); !p7.ApproxEqual(got) {
   124  			t.Errorf("NewIntLatLngSnapper(7).SnapPoint(%v) = %v, want %v", p7, got, p7)
   125  		}
   126  
   127  		// Make sure that we're not snapping using some lower exponent.
   128  		p7not6 := LatLngFromE7(10*ll.Lat.E6()+1, 10*ll.Lng.E6()+1).ToPoint()
   129  		if got := NewIntLatLngSnapper(6).SnapPoint(p7not6); p7not6 == got {
   130  			t.Errorf("NewIntLatLngSnapper(6).SnapPoint(%v) = %v, want %v", p7not6, got, p7not6)
   131  		}
   132  	}
   133  }
   134  */
   135  
   136  // TODO(roberts): Differences from C++:
   137  // bunch of helper methods for these tests:
   138  // func TestCellIdSnapFunctionMinVertexSeparationSnapRadiusRatio(t *testing.T) {
   139  // func TestCellIdSnapFunctionMinEdgeVertexSeparationForLevel(t *testing.T) {
   140  // func TestCellIdSnapFunctionMinEdgeVertexSeparationAtMinSnapRadius(t *testing.T) {
   141  // func TestCellIdSnapFunctionMinEdgeVertexSeparationSnapRadiusRatio(t *testing.T) {
   142  // func TestIntLatLngSnapFunctionMinVertexSeparationSnapRadiusRatio(t *testing.T) {
   143  // func TestIntLatLngSnapFunctionMinEdgeVertexSeparationForLevel(t *testing.T) {
   144  // func TestIntLatLngSnapFunctionMinEdgeVertexSeparationSnapRadiusRatio(t *testing.T) {
   145  

View as plain text