...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2017 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 TestContainsVertexQueryUndetermined(t *testing.T) {
    24  	q := NewContainsVertexQuery(parsePoint("1:2"))
    25  	q.AddEdge(parsePoint("3:4"), 1)
    26  	q.AddEdge(parsePoint("3:4"), -1)
    27  	if got := q.ContainsVertex(); got != 0 {
    28  		t.Errorf("ContainsVertex() = %v, want 0 for vertex with undetermined containment", got)
    29  	}
    30  }
    31  
    32  func TestContainsVertexQueryContainedWithDuplicates(t *testing.T) {
    33  	// The Ortho reference direction points approximately due west.
    34  	// Containment is determined by the unmatched edge immediately clockwise.
    35  	q := NewContainsVertexQuery(parsePoint("0:0"))
    36  	q.AddEdge(parsePoint("3:-3"), -1)
    37  	q.AddEdge(parsePoint("1:-5"), 1)
    38  	q.AddEdge(parsePoint("2:-4"), 1)
    39  	q.AddEdge(parsePoint("1:-5"), -1)
    40  	if got := q.ContainsVertex(); got != 1 {
    41  		t.Errorf("ContainsVertex() = %v, want 1 for vertex that is contained", got)
    42  	}
    43  }
    44  
    45  func TestContainsVertexQueryNotContainedWithDuplicates(t *testing.T) {
    46  	// The Ortho reference direction points approximately due west.
    47  	// Containment is determined by the unmatched edge immediately clockwise.
    48  	q := NewContainsVertexQuery(parsePoint("1:1"))
    49  	q.AddEdge(parsePoint("1:-5"), 1)
    50  	q.AddEdge(parsePoint("2:-4"), -1)
    51  	q.AddEdge(parsePoint("3:-3"), 1)
    52  	q.AddEdge(parsePoint("1:-5"), -1)
    53  	if got := q.ContainsVertex(); got != -1 {
    54  		t.Errorf("ContainsVertex() = %v, want -1 for vertex that is not contained", got)
    55  	}
    56  }
    57  
    58  func TestContainsVertexQueryMatchesLoopContainment(t *testing.T) {
    59  	// Check that the containment function defined is compatible with Loop
    60  	loop := RegularLoop(parsePoint("89:-179"), s1.Angle(10)*s1.Degree, 1000)
    61  	for i := 1; i <= loop.NumVertices(); i++ {
    62  		q := NewContainsVertexQuery(loop.Vertex(i))
    63  		q.AddEdge(loop.Vertex(i-1), -1)
    64  		q.AddEdge(loop.Vertex(i+1), 1)
    65  		if got, want := q.ContainsVertex() > 0, loop.ContainsPoint(loop.Vertex(i)); got != want {
    66  			t.Errorf("ContainsVertex() = %v, loop.ContainsPoint(%v) = %v, should be the same", got, loop.Vertex(i), want)
    67  		}
    68  	}
    69  }
    70  

View as plain text