...

Source file src/github.com/golang/geo/s2/lax_polygon_test_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  	"testing"
    19  )
    20  
    21  func TestLaxPolygonShapeEmptyPolygon(t *testing.T) {
    22  	shape := LaxPolygonFromPolygon((&Polygon{}))
    23  	if got, want := shape.numLoops, 0; got != want {
    24  		t.Errorf("shape.numLoops = %d, want %d", got, want)
    25  	}
    26  	if got, want := shape.numVertices(), 0; got != want {
    27  		t.Errorf("shape.numVertices() = %d, want %d", got, want)
    28  	}
    29  	if got, want := shape.NumEdges(), 0; got != want {
    30  		t.Errorf("shape.NumEdges() = %v, want %v", got, want)
    31  	}
    32  	if got, want := shape.NumChains(), 0; got != want {
    33  		t.Errorf("shape.NumChains() = %v, want %v", got, want)
    34  	}
    35  	if got, want := shape.Dimension(), 2; got != want {
    36  		t.Errorf("shape.Dimension() = %v, want %v", got, want)
    37  	}
    38  	if !shape.IsEmpty() {
    39  		t.Errorf("shape.IsEmpty() = false, want true")
    40  	}
    41  	if shape.IsFull() {
    42  		t.Errorf("shape.IsFull() = true, want false")
    43  	}
    44  	if shape.ReferencePoint().Contained {
    45  		t.Errorf("shape.ReferencePoint().Contained should be false")
    46  	}
    47  }
    48  
    49  func TestLaxPolygonFull(t *testing.T) {
    50  	shape := LaxPolygonFromPolygon(PolygonFromLoops([]*Loop{makeLoop("full")}))
    51  	if got, want := shape.numLoops, 1; got != want {
    52  		t.Errorf("shape.numLoops = %d, want %d", got, want)
    53  	}
    54  	if got, want := shape.numVertices(), 0; got != want {
    55  		t.Errorf("shape.numVertices() = %d, want %d", got, want)
    56  	}
    57  	if got, want := shape.NumEdges(), 0; got != want {
    58  		t.Errorf("shape.NumEdges() = %v, want %v", got, want)
    59  	}
    60  	if got, want := shape.NumChains(), 1; got != want {
    61  		t.Errorf("shape.NumChains() = %v, want %v", got, want)
    62  	}
    63  	if got, want := shape.Dimension(), 2; got != want {
    64  		t.Errorf("shape.Dimension() = %v, want %v", got, want)
    65  	}
    66  	if shape.IsEmpty() {
    67  		t.Errorf("shape.IsEmpty() = true, want false")
    68  	}
    69  	if !shape.IsFull() {
    70  		t.Errorf("shape.IsFull() = false, want true")
    71  	}
    72  	if !shape.ReferencePoint().Contained {
    73  		t.Errorf("shape.ReferencePoint().Contained = false, want true")
    74  	}
    75  }
    76  
    77  func TestLaxPolygonSingleVertexPolygon(t *testing.T) {
    78  	// Polygon doesn't support single-vertex loops, so we need to construct
    79  	// the LaxPolygon directly.
    80  	var loops [][]Point
    81  	loops = append(loops, parsePoints("0:0"))
    82  
    83  	shape := LaxPolygonFromPoints(loops)
    84  	if got, want := shape.numLoops, 1; got != want {
    85  		t.Errorf("shape.numLoops = %d, want %d", got, want)
    86  	}
    87  	if got, want := shape.numVertices(), 1; got != want {
    88  		t.Errorf("shape.numVertices() = %d, want %d", got, want)
    89  	}
    90  	if got, want := shape.NumEdges(), 1; got != want {
    91  		t.Errorf("shape.NumEdges() = %v, want %v", got, want)
    92  	}
    93  	if got, want := shape.NumChains(), 1; got != want {
    94  		t.Errorf("shape.NumChains() = %v, want %v", got, want)
    95  	}
    96  	if got, want := shape.Chain(0).Start, 0; got != want {
    97  		t.Errorf("shape.Chain(0).Start = %d, want %d", got, want)
    98  	}
    99  	if got, want := shape.Chain(0).Length, 1; got != want {
   100  		t.Errorf("shape.Chain(0).Length = %d, want %d", got, want)
   101  	}
   102  
   103  	edge := shape.Edge(0)
   104  	if loops[0][0] != edge.V0 {
   105  		t.Errorf("shape.Edge(0).V0 = %v, want %v", edge.V0, loops[0][0])
   106  	}
   107  	if loops[0][0] != edge.V1 {
   108  		t.Errorf("shape.Edge(0).V0 = %v, want %v", edge.V1, loops[0][0])
   109  	}
   110  	if edge != shape.ChainEdge(0, 0) {
   111  		t.Errorf("shape.Edge(0) should equal shape.ChainEdge(0, 0)")
   112  	}
   113  	if got, want := shape.Dimension(), 2; got != want {
   114  		t.Errorf("shape.Dimension() = %v, want %v", got, want)
   115  	}
   116  	if shape.IsEmpty() {
   117  		t.Errorf("shape.IsEmpty() = true, want false")
   118  	}
   119  	if shape.IsFull() {
   120  		t.Errorf("shape.IsFull() = true, want false")
   121  	}
   122  	if shape.ReferencePoint().Contained {
   123  		t.Errorf("shape.ReferencePoint().Contained = true, want false")
   124  	}
   125  }
   126  
   127  func TestLaxPolygonShapeSingleLoopPolygon(t *testing.T) {
   128  	vertices := parsePoints("0:0, 0:1, 1:1, 1:0")
   129  	lenVerts := len(vertices)
   130  	shape := LaxPolygonFromPolygon(PolygonFromLoops([]*Loop{LoopFromPoints(vertices)}))
   131  
   132  	if got, want := shape.numLoops, 1; got != want {
   133  		t.Errorf("shape.numLoops = %d, want %d", got, want)
   134  	}
   135  	if got, want := shape.numVertices(), lenVerts; got != want {
   136  		t.Errorf("shape.numVertices() = %d, want %d", got, want)
   137  	}
   138  	if got, want := shape.numLoopVertices(0), lenVerts; got != want {
   139  		t.Errorf("shape.numLoopVertices(0) = %d, want %d", got, want)
   140  	}
   141  	if got, want := shape.NumEdges(), lenVerts; got != want {
   142  		t.Errorf("shape.NumEdges() = %v, want %v", got, want)
   143  	}
   144  	if got, want := shape.NumChains(), 1; got != want {
   145  		t.Errorf("shape.NumChains() = %v, want %v", got, want)
   146  	}
   147  	if got, want := shape.Chain(0).Start, 0; got != want {
   148  		t.Errorf("shape.Chain(0).Start = %d, want %d", got, want)
   149  	}
   150  	if got, want := shape.Chain(0).Length, lenVerts; got != want {
   151  		t.Errorf("shape.Chain(0).Length = %d, want %d", got, want)
   152  	}
   153  	for i := 0; i < lenVerts; i++ {
   154  		if got, want := shape.loopVertex(0, i), vertices[i]; got != want {
   155  			t.Errorf("shape.loopVertex(%d) = %v, want %v", i, got, want)
   156  		}
   157  
   158  		edge := shape.Edge(i)
   159  		if got, want := vertices[i], edge.V0; got != want {
   160  			t.Errorf("shape.Edge(%d).V0 = %v, want %v", i, got, want)
   161  		}
   162  		if got, want := vertices[(i+1)%lenVerts], edge.V1; got != want {
   163  			t.Errorf("shape.Edge(%d).V1 = %v, want %v", i, got, want)
   164  		}
   165  		if got, want := shape.ChainEdge(0, i).V0, edge.V0; got != want {
   166  			t.Errorf("shape.ChainEdge(0, %d).V0 = %v, want %v", i, got, want)
   167  		}
   168  		if got, want := shape.ChainEdge(0, i).V1, edge.V1; got != want {
   169  			t.Errorf("shape.ChainEdge(0, %d).V1 = %v, want %v", i, got, want)
   170  		}
   171  	}
   172  	if got, want := shape.Dimension(), 2; got != want {
   173  		t.Errorf("shape.Dimension() = %v, want %v", got, want)
   174  	}
   175  	if shape.IsEmpty() {
   176  		t.Errorf("shape.IsEmpty() = true, want false")
   177  	}
   178  	if shape.IsFull() {
   179  		t.Errorf("shape.IsFull() = true, want false")
   180  	}
   181  	if containsBruteForce(shape, OriginPoint()) {
   182  		t.Errorf("containsBruteForce(%v, %v) = true, want false", shape, OriginPoint())
   183  	}
   184  }
   185  
   186  func TestLaxPolygonShapeMultiLoopPolygon(t *testing.T) {
   187  	// Test to make sure that the loops are oriented so that the interior of the
   188  	// polygon is always on the left.
   189  	loops := [][]Point{
   190  		parsePoints("0:0, 0:3, 3:3"), // CCW
   191  		parsePoints("1:1, 2:2, 1:2"), // CW
   192  	}
   193  	lenLoops := len(loops)
   194  	shape := LaxPolygonFromPoints(loops)
   195  	if got, want := shape.numLoops, lenLoops; got != want {
   196  		t.Errorf("shape.numLoops = %d, want %d", got, want)
   197  	}
   198  	if got, want := shape.NumChains(), lenLoops; got != want {
   199  		t.Errorf("shape.NumChains() = %v, want %v", got, want)
   200  	}
   201  
   202  	numVertices := 0
   203  	for i, loop := range loops {
   204  		if got, want := shape.numLoopVertices(i), len(loop); got != want {
   205  			t.Errorf("shape.numLoopVertices(%d) = %d, want %d", i, got, want)
   206  		}
   207  		if got, want := shape.Chain(i).Start, numVertices; got != want {
   208  			t.Errorf("shape.Chain(%d).Start = %d, want %d", i, got, want)
   209  		}
   210  		if got, want := shape.Chain(i).Length, len(loop); got != want {
   211  			t.Errorf("shape.Chain(%d).Length = %d, want %d", i, got, want)
   212  		}
   213  		for j, pt := range loop {
   214  			if pt != shape.loopVertex(i, j) {
   215  				t.Errorf("shape.loopVertex(%d, %d) = %v, want %v", i, j, shape.loopVertex(i, j), pt)
   216  			}
   217  			edge := shape.Edge(numVertices + j)
   218  			if pt != edge.V0 {
   219  				t.Errorf("shape.Edge(%d).V0 = %v, want %v", numVertices+j, edge.V0, pt)
   220  			}
   221  			if got, want := loop[(j+1)%len(loop)], edge.V1; got != want {
   222  				t.Errorf("shape.Edge(%d).V1 = %v, want %v", numVertices+j, got, want)
   223  			}
   224  		}
   225  		numVertices += len(loop)
   226  	}
   227  
   228  	if got, want := shape.numVertices(), numVertices; got != want {
   229  		t.Errorf("shape.numVertices() = %d, want %d", got, want)
   230  	}
   231  	if got, want := shape.NumEdges(), numVertices; got != want {
   232  		t.Errorf("shape.NumEdges() = %v, want %v", got, want)
   233  	}
   234  	if got, want := shape.Dimension(), 2; got != want {
   235  		t.Errorf("shape.Dimension() = %v, want %v", got, want)
   236  	}
   237  	if shape.IsEmpty() {
   238  		t.Errorf("shape.IsEmpty() = true, want false")
   239  	}
   240  	if shape.IsFull() {
   241  		t.Errorf("shape.IsFull() = true, want false")
   242  	}
   243  	if containsBruteForce(shape, OriginPoint()) {
   244  		t.Errorf("containsBruteForce(%v, %v) = true, want false", shape, OriginPoint())
   245  	}
   246  }
   247  
   248  func TestLaxPolygonShapeDegenerateLoops(t *testing.T) {
   249  	loops := [][]Point{
   250  		parsePoints("1:1, 1:2, 2:2, 1:2, 1:3, 1:2, 1:1"),
   251  		parsePoints("0:0, 0:3, 0:6, 0:9, 0:6, 0:3, 0:0"),
   252  		parsePoints("5:5, 6:6"),
   253  	}
   254  
   255  	shape := LaxPolygonFromPoints(loops)
   256  	if shape.ReferencePoint().Contained {
   257  		t.Errorf("%v.ReferencePoint().Contained() = true, want false", shape)
   258  	}
   259  }
   260  
   261  func TestLaxPolygonShapeInvertedLoops(t *testing.T) {
   262  	loops := [][]Point{
   263  		parsePoints("1:2, 1:1, 2:2"),
   264  		parsePoints("3:4, 3:3, 4:4"),
   265  	}
   266  	shape := LaxPolygonFromPoints(loops)
   267  
   268  	if !containsBruteForce(shape, OriginPoint()) {
   269  		t.Errorf("containsBruteForce(%v, %v) = false, want true", shape, OriginPoint())
   270  	}
   271  }
   272  
   273  // TODO(roberts): Remaining to port:
   274  // LaxPolygonManyLoopPolygon
   275  // LaxPolygonMultiLoopS2Polygon
   276  // LaxPolygonCompareToLoop once fractal testing is added.
   277  // LaxPolygonCoderWorks
   278  // LaxPolygonChainIteratorWorks
   279  // LaxPolygonChainVertexIteratorWorks
   280  //
   281  

View as plain text