...

Source file src/github.com/golang/geo/s2/wedge_relations_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/r3"
    21  )
    22  
    23  func TestWedgeRelations(t *testing.T) {
    24  	// For simplicity, all of these tests use an origin of (0, 0, 1).
    25  	// This shouldn't matter as long as the lower-level primitives are
    26  	// implemented correctly.
    27  	ab1 := Point{r3.Vector{0, 0, 1}.Normalize()}
    28  
    29  	tests := []struct {
    30  		desc           string
    31  		a0, a1, b0, b1 Point
    32  		contains       bool
    33  		intersects     bool
    34  		relation       WedgeRel
    35  	}{
    36  		{
    37  			desc:       "Intersection in one wedge",
    38  			a0:         Point{r3.Vector{-1, 0, 10}},
    39  			a1:         Point{r3.Vector{1, 2, 10}},
    40  			b0:         Point{r3.Vector{0, 1, 10}},
    41  			b1:         Point{r3.Vector{1, -2, 10}},
    42  			contains:   false,
    43  			intersects: true,
    44  			relation:   WedgeProperlyOverlaps,
    45  		},
    46  		{
    47  			desc:       "Intersection in two wedges",
    48  			a0:         Point{r3.Vector{-1, -1, 10}},
    49  			a1:         Point{r3.Vector{1, -1, 10}},
    50  			b0:         Point{r3.Vector{1, 0, 10}},
    51  			b1:         Point{r3.Vector{-1, 1, 10}},
    52  			contains:   false,
    53  			intersects: true,
    54  			relation:   WedgeProperlyOverlaps,
    55  		},
    56  		{
    57  			desc:       "Normal containment",
    58  			a0:         Point{r3.Vector{-1, -1, 10}},
    59  			a1:         Point{r3.Vector{1, -1, 10}},
    60  			b0:         Point{r3.Vector{-1, 0, 10}},
    61  			b1:         Point{r3.Vector{1, 0, 10}},
    62  			contains:   true,
    63  			intersects: true,
    64  			relation:   WedgeProperlyContains,
    65  		},
    66  		{
    67  			desc:       "Containment with equality on one side",
    68  			a0:         Point{r3.Vector{2, 1, 10}},
    69  			a1:         Point{r3.Vector{-1, -1, 10}},
    70  			b0:         Point{r3.Vector{2, 1, 10}},
    71  			b1:         Point{r3.Vector{1, -5, 10}},
    72  			contains:   true,
    73  			intersects: true,
    74  			relation:   WedgeProperlyContains,
    75  		},
    76  		{
    77  			desc:       "Containment with equality on the other side",
    78  			a0:         Point{r3.Vector{2, 1, 10}},
    79  			a1:         Point{r3.Vector{-1, -1, 10}},
    80  			b0:         Point{r3.Vector{1, -2, 10}},
    81  			b1:         Point{r3.Vector{-1, -1, 10}},
    82  			contains:   true,
    83  			intersects: true,
    84  			relation:   WedgeProperlyContains,
    85  		},
    86  		{
    87  			desc:       "Containment with equality on both sides",
    88  			a0:         Point{r3.Vector{-2, 3, 10}},
    89  			a1:         Point{r3.Vector{4, -5, 10}},
    90  			b0:         Point{r3.Vector{-2, 3, 10}},
    91  			b1:         Point{r3.Vector{4, -5, 10}},
    92  			contains:   true,
    93  			intersects: true,
    94  			relation:   WedgeEquals,
    95  		},
    96  		{
    97  			desc:       "Disjoint with equality on one side",
    98  			a0:         Point{r3.Vector{-2, 3, 10}},
    99  			a1:         Point{r3.Vector{4, -5, 10}},
   100  			b0:         Point{r3.Vector{4, -5, 10}},
   101  			b1:         Point{r3.Vector{-2, -3, 10}},
   102  			contains:   false,
   103  			intersects: false,
   104  			relation:   WedgeIsDisjoint,
   105  		},
   106  		{
   107  			desc:       "Disjoint with equality on the other side",
   108  			a0:         Point{r3.Vector{-2, 3, 10}},
   109  			a1:         Point{r3.Vector{0, 5, 10}},
   110  			b0:         Point{r3.Vector{4, -5, 10}},
   111  			b1:         Point{r3.Vector{-2, 3, 10}},
   112  			contains:   false,
   113  			intersects: false,
   114  			relation:   WedgeIsDisjoint,
   115  		},
   116  		{
   117  			desc:       "Disjoint with equality on both sides",
   118  			a0:         Point{r3.Vector{-2, 3, 10}},
   119  			a1:         Point{r3.Vector{4, -5, 10}},
   120  			b0:         Point{r3.Vector{4, -5, 10}},
   121  			b1:         Point{r3.Vector{-2, 3, 10}},
   122  			contains:   false,
   123  			intersects: false,
   124  			relation:   WedgeIsDisjoint,
   125  		},
   126  		{
   127  			desc:       "B contains A with equality on one side",
   128  			a0:         Point{r3.Vector{2, 1, 10}},
   129  			a1:         Point{r3.Vector{1, -5, 10}},
   130  			b0:         Point{r3.Vector{2, 1, 10}},
   131  			b1:         Point{r3.Vector{-1, -1, 10}},
   132  			contains:   false,
   133  			intersects: true,
   134  			relation:   WedgeIsProperlyContained,
   135  		},
   136  
   137  		{
   138  			desc:       "B contains A with equality on the other side",
   139  			a0:         Point{r3.Vector{2, 1, 10}},
   140  			a1:         Point{r3.Vector{1, -5, 10}},
   141  			b0:         Point{r3.Vector{-2, 1, 10}},
   142  			b1:         Point{r3.Vector{1, -5, 10}},
   143  			contains:   false,
   144  			intersects: true,
   145  			relation:   WedgeIsProperlyContained,
   146  		},
   147  	}
   148  
   149  	for _, test := range tests {
   150  		test.a0 = Point{test.a0.Normalize()}
   151  		test.a1 = Point{test.a1.Normalize()}
   152  		test.b0 = Point{test.b0.Normalize()}
   153  		test.b1 = Point{test.b1.Normalize()}
   154  
   155  		if got := WedgeContains(test.a0, ab1, test.a1, test.b0, test.b1); got != test.contains {
   156  			t.Errorf("%s: WedgeContains(%v, %v, %v, %v, %v) = %t, want %t", test.desc, test.a0, ab1, test.a1, test.b0, test.b1, got, test.contains)
   157  		}
   158  		if got := WedgeIntersects(test.a0, ab1, test.a1, test.b0, test.b1); got != test.intersects {
   159  			t.Errorf("%s: WedgeIntersects(%v, %v, %v, %v, %v) = %t, want %t", test.desc, test.a0, ab1, test.a1, test.b0, test.b1, got, test.intersects)
   160  		}
   161  		if got := WedgeRelation(test.a0, ab1, test.a1, test.b0, test.b1); got != test.relation {
   162  			t.Errorf("%s: WedgeRelation(%v, %v, %v, %v, %v) = %v, want %v", test.desc, test.a0, ab1, test.a1, test.b0, test.b1, got, test.relation)
   163  		}
   164  	}
   165  }
   166  

View as plain text