...

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

Documentation: github.com/golang/geo/s2

     1  // Copyright 2015 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/r3"
    22  )
    23  
    24  func TestCol(t *testing.T) {
    25  	tests := []struct {
    26  		have *matrix3x3
    27  		col  int
    28  		want Point
    29  	}{
    30  		{&matrix3x3{}, 0, OriginPoint()},
    31  		{
    32  			&matrix3x3{
    33  				{1, 2, 3},
    34  				{4, 5, 6},
    35  				{7, 8, 9},
    36  			},
    37  			0,
    38  			Point{r3.Vector{1, 4, 7}},
    39  		},
    40  		{
    41  			&matrix3x3{
    42  				{1, 2, 3},
    43  				{4, 5, 6},
    44  				{7, 8, 9},
    45  			},
    46  			2,
    47  			Point{r3.Vector{3, 6, 9}},
    48  		},
    49  	}
    50  
    51  	for _, test := range tests {
    52  		if got := test.have.col(test.col); !got.ApproxEqual(test.want) {
    53  			t.Errorf("%v.col(%d) = %v, want %v", test.have, test.col, got, test.want)
    54  		}
    55  	}
    56  }
    57  
    58  func TestRow(t *testing.T) {
    59  	tests := []struct {
    60  		have *matrix3x3
    61  		row  int
    62  		want Point
    63  	}{
    64  		{&matrix3x3{}, 0, OriginPoint()},
    65  		{
    66  			&matrix3x3{
    67  				{1, 2, 3},
    68  				{4, 5, 6},
    69  				{7, 8, 9},
    70  			},
    71  			0,
    72  			Point{r3.Vector{1, 2, 3}},
    73  		},
    74  		{
    75  			&matrix3x3{
    76  				{1, 2, 3},
    77  				{4, 5, 6},
    78  				{7, 8, 9},
    79  			},
    80  			2,
    81  			Point{r3.Vector{7, 8, 9}},
    82  		},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		if got := test.have.row(test.row); !got.ApproxEqual(test.want) {
    87  			t.Errorf("%v.row(%d) = %v, want %v", test.have, test.row, got, test.want)
    88  		}
    89  	}
    90  }
    91  
    92  func TestSetCol(t *testing.T) {
    93  	tests := []struct {
    94  		have  *matrix3x3
    95  		col   int
    96  		point Point
    97  		want  *matrix3x3
    98  	}{
    99  		{
   100  			&matrix3x3{},
   101  			0,
   102  			Point{r3.Vector{1, 1, 0}},
   103  			&matrix3x3{
   104  				{1, 0, 0},
   105  				{1, 0, 0},
   106  				{0, 0, 0},
   107  			},
   108  		},
   109  		{
   110  			&matrix3x3{
   111  				{1, 2, 3},
   112  				{4, 5, 6},
   113  				{7, 8, 9},
   114  			},
   115  			2,
   116  			Point{r3.Vector{1, 1, 0}},
   117  			&matrix3x3{
   118  				{1, 2, 1},
   119  				{4, 5, 1},
   120  				{7, 8, 0},
   121  			},
   122  		},
   123  	}
   124  
   125  	for _, test := range tests {
   126  		if got := test.have.setCol(test.col, test.point); !matricesApproxEqual(got, test.want) {
   127  			t.Errorf("%v.setCol(%d, %v) = %v, want %v", test.have, test.col, test.point, got, test.want)
   128  		}
   129  	}
   130  }
   131  
   132  func TestSetRow(t *testing.T) {
   133  	tests := []struct {
   134  		have  *matrix3x3
   135  		row   int
   136  		point Point
   137  		want  *matrix3x3
   138  	}{
   139  		{
   140  			&matrix3x3{},
   141  			0,
   142  			Point{r3.Vector{1, 1, 0}},
   143  			&matrix3x3{
   144  				{1, 1, 0},
   145  				{0, 0, 0},
   146  				{0, 0, 0},
   147  			},
   148  		},
   149  		{
   150  			&matrix3x3{
   151  				{1, 2, 3},
   152  				{4, 5, 6},
   153  				{7, 8, 9},
   154  			},
   155  			2,
   156  			Point{r3.Vector{1, 1, 0}},
   157  			&matrix3x3{
   158  				{1, 2, 3},
   159  				{4, 5, 6},
   160  				{1, 1, 0},
   161  			},
   162  		},
   163  	}
   164  	for _, test := range tests {
   165  		if got := test.have.setRow(test.row, test.point); !matricesApproxEqual(got, test.want) {
   166  			t.Errorf("%v.setRow(%d, %v) = %v, want %v", test.have, test.row, test.point, got, test.want)
   167  		}
   168  	}
   169  }
   170  
   171  func TestScale(t *testing.T) {
   172  	tests := []struct {
   173  		have  *matrix3x3
   174  		scale float64
   175  		want  *matrix3x3
   176  	}{
   177  		{
   178  			&matrix3x3{},
   179  			0,
   180  			&matrix3x3{},
   181  		},
   182  		{
   183  			&matrix3x3{
   184  				{1, 1, 1},
   185  				{1, 1, 1},
   186  				{1, 1, 1},
   187  			},
   188  			0,
   189  			&matrix3x3{},
   190  		},
   191  		{
   192  			&matrix3x3{
   193  				{1, 1, 1},
   194  				{1, 1, 1},
   195  				{1, 1, 1},
   196  			},
   197  			1,
   198  			&matrix3x3{
   199  				{1, 1, 1},
   200  				{1, 1, 1},
   201  				{1, 1, 1},
   202  			},
   203  		},
   204  		{
   205  			&matrix3x3{
   206  				{1, 1, 1},
   207  				{1, 1, 1},
   208  				{1, 1, 1},
   209  			},
   210  			5,
   211  			&matrix3x3{
   212  				{5, 5, 5},
   213  				{5, 5, 5},
   214  				{5, 5, 5},
   215  			},
   216  		},
   217  		{
   218  			&matrix3x3{
   219  				{-2, 2, -3},
   220  				{-1, 1, 3},
   221  				{2, 0, -1},
   222  			},
   223  			2.75,
   224  			&matrix3x3{
   225  				{-5.5, 5.5, -8.25},
   226  				{-2.75, 2.75, 8.25},
   227  				{5.5, 0, -2.75},
   228  			},
   229  		},
   230  	}
   231  
   232  	for _, test := range tests {
   233  		if got := test.have.scale(test.scale); !matricesApproxEqual(got, test.want) {
   234  			t.Errorf("%v.scale(%f) = %v, want %v", test.have, test.scale, got, test.want)
   235  		}
   236  	}
   237  }
   238  
   239  func TestMul(t *testing.T) {
   240  	tests := []struct {
   241  		have  *matrix3x3
   242  		point Point
   243  		want  Point
   244  	}{
   245  		{&matrix3x3{}, Point{}, Point{}},
   246  		{
   247  			&matrix3x3{
   248  				{1, 1, 1},
   249  				{1, 1, 1},
   250  				{1, 1, 1},
   251  			},
   252  			Point{},
   253  			Point{},
   254  		},
   255  		{
   256  			// Identity times something gives back the something
   257  			&matrix3x3{
   258  				{1, 0, 0},
   259  				{0, 1, 0},
   260  				{0, 0, 1},
   261  			},
   262  			Point{},
   263  			Point{},
   264  		},
   265  		{
   266  			// Identity times something gives back the something
   267  			&matrix3x3{
   268  				{1, 0, 0},
   269  				{0, 1, 0},
   270  				{0, 0, 1},
   271  			},
   272  			Point{r3.Vector{1, 2, 3}},
   273  			Point{r3.Vector{1, 2, 3}},
   274  		},
   275  		{
   276  			&matrix3x3{
   277  				{1, 2, 3},
   278  				{4, 5, 6},
   279  				{7, 8, 9},
   280  			},
   281  			Point{r3.Vector{1, 1, 1}},
   282  			Point{r3.Vector{6, 15, 24}},
   283  		},
   284  	}
   285  	for _, test := range tests {
   286  		if got := test.have.mul(test.point); !got.ApproxEqual(test.want) {
   287  			t.Errorf("%v.mul(%v) = %v, want %v", test.have, test.point, got, test.want)
   288  		}
   289  	}
   290  }
   291  
   292  func TestDet(t *testing.T) {
   293  	tests := []struct {
   294  		have *matrix3x3
   295  		want float64
   296  	}{
   297  		{
   298  			&matrix3x3{},
   299  			0,
   300  		},
   301  		{
   302  			// Matrix of all the same values has det of 0.
   303  			&matrix3x3{
   304  				{1, 1, 1},
   305  				{1, 1, 1},
   306  				{1, 1, 1},
   307  			},
   308  			0,
   309  		},
   310  		{
   311  			// Identity matrix has det of 1.
   312  			&matrix3x3{
   313  				{1, 0, 0},
   314  				{0, 1, 0},
   315  				{0, 0, 1},
   316  			},
   317  			1,
   318  		},
   319  		{
   320  			&matrix3x3{
   321  				{-2, 2, -3},
   322  				{-1, 1, 3},
   323  				{2, 0, -1},
   324  			},
   325  			18,
   326  		},
   327  		{
   328  			&matrix3x3{
   329  				{1, 2, 3},
   330  				{4, 5, 6},
   331  				{7, 8, 9},
   332  			},
   333  			0,
   334  		},
   335  		{
   336  			&matrix3x3{
   337  				{9, 8, 7},
   338  				{6, 5, 4},
   339  				{3, 2, 1},
   340  			},
   341  			0,
   342  		},
   343  		{
   344  			&matrix3x3{
   345  				{1.74, math.E, 42},
   346  				{math.Pi, math.Sqrt2, math.Ln10},
   347  				{3, math.SqrtPhi, 9.8976},
   348  			},
   349  			-56.838525224123096,
   350  		},
   351  	}
   352  
   353  	for _, test := range tests {
   354  		if got := test.have.det(); !float64Eq(got, test.want) {
   355  			t.Errorf("%v.det() = %v, want %v", test.have, got, test.want)
   356  		}
   357  	}
   358  }
   359  
   360  func TestTranspose(t *testing.T) {
   361  	tests := []struct {
   362  		have *matrix3x3
   363  		want *matrix3x3
   364  	}{
   365  		{&matrix3x3{}, &matrix3x3{}},
   366  		{
   367  			&matrix3x3{
   368  				{1, 2, 3},
   369  				{4, 5, 6},
   370  				{7, 8, 9},
   371  			},
   372  			&matrix3x3{
   373  				{1, 4, 7},
   374  				{2, 5, 8},
   375  				{3, 6, 9},
   376  			},
   377  		},
   378  		{
   379  			&matrix3x3{
   380  				{1, 0, 0},
   381  				{0, 2, 0},
   382  				{0, 0, 3},
   383  			},
   384  			&matrix3x3{
   385  				{1, 0, 0},
   386  				{0, 2, 0},
   387  				{0, 0, 3},
   388  			},
   389  		},
   390  		{
   391  			&matrix3x3{
   392  				{1, 2, 3},
   393  				{0, 4, 5},
   394  				{0, 0, 6},
   395  			},
   396  			&matrix3x3{
   397  				{1, 0, 0},
   398  				{2, 4, 0},
   399  				{3, 5, 6},
   400  			},
   401  		},
   402  		{
   403  			&matrix3x3{
   404  				{1, 1, 1},
   405  				{0, 0, 0},
   406  				{0, 0, 0},
   407  			},
   408  			&matrix3x3{
   409  				{1, 0, 0},
   410  				{1, 0, 0},
   411  				{1, 0, 0},
   412  			},
   413  		},
   414  	}
   415  
   416  	for _, test := range tests {
   417  		if got := test.have.transpose().transpose(); !matricesApproxEqual(got, test.have) {
   418  			t.Errorf("%v.transpose().transpose() = %v, want %v", test.have, got, test.have)
   419  		}
   420  
   421  		if got := test.have.transpose(); !matricesApproxEqual(got, test.want) {
   422  			t.Errorf("%v.transpose() = %v, want %v", test.have, got, test.want)
   423  		}
   424  
   425  	}
   426  }
   427  
   428  func TestString(t *testing.T) {
   429  	tests := []struct {
   430  		have *matrix3x3
   431  		want string
   432  	}{
   433  		{
   434  			&matrix3x3{
   435  				{1, 2, 3},
   436  				{4, 5, 6},
   437  				{7, 8, 9},
   438  			},
   439  			`[ 1.0000 2.0000 3.0000 ] [ 4.0000 5.0000 6.0000 ] [ 7.0000 8.0000 9.0000 ]`,
   440  		},
   441  		{
   442  			&matrix3x3{
   443  				{1, 4, 7},
   444  				{2, 5, 8},
   445  				{3, 6, 9},
   446  			},
   447  			`[ 1.0000 4.0000 7.0000 ] [ 2.0000 5.0000 8.0000 ] [ 3.0000 6.0000 9.0000 ]`,
   448  		},
   449  	}
   450  
   451  	for _, test := range tests {
   452  		if got := test.have.String(); got != test.want {
   453  			t.Errorf("%v.String() = %v, want %v", test.have, got, test.want)
   454  		}
   455  	}
   456  }
   457  
   458  func TestFrames(t *testing.T) {
   459  	z := PointFromCoords(0.2, 0.5, -3.3)
   460  	m := getFrame(z)
   461  
   462  	if !m.col(0).IsUnit() {
   463  		t.Errorf("col(0) of frame not unit length")
   464  	}
   465  	if !m.col(1).IsUnit() {
   466  		t.Errorf("col(1) of frame not unit length")
   467  	}
   468  	if !float64Eq(m.det(), 1) {
   469  		t.Errorf("determinant of frame = %v, want %v", m.det(), 1)
   470  	}
   471  
   472  	tests := []struct {
   473  		a Point
   474  		b Point
   475  	}{
   476  		{m.col(2), z},
   477  
   478  		{toFrame(m, m.col(0)), Point{r3.Vector{1, 0, 0}}},
   479  		{toFrame(m, m.col(1)), Point{r3.Vector{0, 1, 0}}},
   480  		{toFrame(m, m.col(2)), Point{r3.Vector{0, 0, 1}}},
   481  
   482  		{fromFrame(m, Point{r3.Vector{1, 0, 0}}), m.col(0)},
   483  		{fromFrame(m, Point{r3.Vector{0, 1, 0}}), m.col(1)},
   484  		{fromFrame(m, Point{r3.Vector{0, 0, 1}}), m.col(2)},
   485  	}
   486  
   487  	for _, test := range tests {
   488  		if !pointsApproxEqual(test.a, test.b, epsilon) {
   489  			t.Errorf("%v != %v", test.a, test.b)
   490  		}
   491  	}
   492  }
   493  

View as plain text