github.com/liucxer/courier@v1.7.1/h3/polygon_test.go (about)

     1  package h3
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  var sfVerts = []GeoCoord{
    10  	{0.659966917655, -2.1364398519396}, {0.6595011102219, -2.1359434279405},
    11  	{0.6583348114025, -2.1354884206045}, {0.6581220034068, -2.1382437718946},
    12  	{0.6594479998527, -2.1384597563896}, {0.6599990002976, -2.1376771158464},
    13  }
    14  
    15  func Test_pointInsideGeofence(t *testing.T) {
    16  	geofence := Geofence{6, sfVerts}
    17  
    18  	inside := GeoCoord{0.659, -2.136}
    19  	somewhere := GeoCoord{1, 2}
    20  
    21  	var bbox BBox
    22  	bboxFrom(&geofence, &bbox)
    23  
    24  	require.True(t, !pointInside(&geofence, &bbox, &sfVerts[0]), "contains exact")
    25  	require.True(t, pointInside(&geofence, &bbox, &sfVerts[4]), "contains exact 4")
    26  	require.True(t, pointInside(&geofence, &bbox, &inside), "contains point inside")
    27  	require.True(t, !pointInside(&geofence, &bbox, &somewhere), "contains somewhere else")
    28  }
    29  
    30  func Test_pointInsideGeofenceTransmeridian(t *testing.T) {
    31  	verts := []GeoCoord{{0.01, -M_PI + 0.01},
    32  		{0.01, M_PI - 0.01},
    33  		{-0.01, M_PI - 0.01},
    34  		{-0.01, -M_PI + 0.01},
    35  	}
    36  
    37  	transMeridianGeofence := Geofence{4, verts}
    38  	eastPoint := GeoCoord{0.001, -M_PI + 0.001}
    39  	eastPointOutside := GeoCoord{0.001, -M_PI + 0.1}
    40  	westPoint := GeoCoord{0.001, M_PI - 0.001}
    41  	westPointOutside := GeoCoord{0.001, M_PI - 0.1}
    42  
    43  	var bbox BBox
    44  	bboxFrom(&transMeridianGeofence, &bbox)
    45  	require.True(t, pointInside(&transMeridianGeofence, &bbox, &westPoint),
    46  		"contains point to the west of the antimeridian")
    47  	require.True(t, pointInside(&transMeridianGeofence, &bbox, &eastPoint),
    48  		"contains point to the east of the antimeridian")
    49  	require.True(t, !pointInside(&transMeridianGeofence, &bbox, &westPointOutside),
    50  		"does not contain outside point to the west of the antimeridian")
    51  	require.True(t, !pointInside(&transMeridianGeofence, &bbox, &eastPointOutside),
    52  		"does not contain outside point to the east of the antimeridian")
    53  }
    54  
    55  func Test_bboxesFromGeoPolygon(t *testing.T) {
    56  	t.Run("no hole", func(t *testing.T) {
    57  		verts := []GeoCoord{{0.8, 0.3}, {0.7, 0.6}, {1.1, 0.7}, {1.0, 0.2}}
    58  		geofence := Geofence{4, verts}
    59  		polygon := GeoPolygon{geofence, 0, nil}
    60  		expected := BBox{1.1, 0.7, 0.7, 0.2}
    61  		result := make([]BBox, 1)
    62  
    63  		bboxesFromGeoPolygon(&polygon, result)
    64  		require.True(t, bboxEquals(&result[0], &expected), "Got expected bbox")
    65  	})
    66  
    67  	t.Run("with hole", func(t *testing.T) {
    68  		verts := []GeoCoord{{0.8, 0.3}, {0.7, 0.6}, {1.1, 0.7}, {1.0, 0.2}}
    69  		geofence := Geofence{numVerts: 4, verts: verts}
    70  
    71  		// not a real hole, but doesn't matter for the test
    72  		holeVerts := []GeoCoord{{0.9, 0.3}, {0.9, 0.5}, {1.0, 0.7}, {0.9, 0.3}}
    73  		holeGeofence := Geofence{numVerts: 4, verts: holeVerts}
    74  		polygon := GeoPolygon{geofence, 1, []Geofence{holeGeofence}}
    75  		expected := BBox{1.1, 0.7, 0.7, 0.2}
    76  		expectedHole := BBox{1.0, 0.9, 0.7, 0.3}
    77  
    78  		result := make([]BBox, 2)
    79  		bboxesFromGeoPolygon(&polygon, result)
    80  
    81  		require.True(t, bboxEquals(&result[0], &expected), "Got expected bbox")
    82  		require.True(t, bboxEquals(&result[1], &expectedHole), "Got expected hole bbox")
    83  	})
    84  }
    85  
    86  func Test_isClockwiseGeofence(t *testing.T) {
    87  	verts := []GeoCoord{{0, 0}, {0.1, 0.1}, {0, 0.1}}
    88  	geofence := Geofence{3, verts}
    89  
    90  	require.True(t, isClockwise(&geofence), "Got true for clockwise geofence")
    91  }
    92  
    93  func Test_isClockwiseGeofenceTransmeridian(t *testing.T) {
    94  	verts := []GeoCoord{
    95  		{0.4, M_PI - 0.1},
    96  		{0.4, -M_PI + 0.1},
    97  		{-0.4, -M_PI + 0.1},
    98  		{-0.4, M_PI - 0.1},
    99  	}
   100  	
   101  	geofence := Geofence{4, verts};
   102  	require.True(t, isClockwise(&geofence), "Got true for clockwise geofence");
   103  }