aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulmach/orb/resample/line_string_test.go
blob: 60b3bd31c75d3036a2d484c3c03830ff255d74a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package resample

import (
	"testing"

	"github.com/paulmach/orb"
	"github.com/paulmach/orb/planar"
)

func TestResample(t *testing.T) {
	ls := orb.LineString{}
	Resample(ls, planar.Distance, 10) // should not panic

	ls = append(ls, orb.Point{0, 0})
	Resample(ls, planar.Distance, 10) // should not panic

	ls = append(ls, orb.Point{1.5, 1.5})
	ls = append(ls, orb.Point{2, 2})

	// resample to 0?
	result := Resample(ls, planar.Distance, 0)
	if len(result) != 0 {
		t.Error("down to zero should be empty line")
	}

	// resample to 1
	result = Resample(ls, planar.Distance, 1)
	answer := orb.LineString{{0, 0}}

	if !result.Equal(answer) {
		t.Error("down to 1 should be first point")
	}

	result = Resample(ls, planar.Distance, 2)
	answer = orb.LineString{{0, 0}, {2, 2}}
	if !result.Equal(answer) {
		t.Error("resample downsampling")
	}

	result = Resample(ls, planar.Distance, 5)
	answer = orb.LineString{{0, 0}, {0.5, 0.5}, {1, 1}, {1.5, 1.5}, {2, 2}}
	if !result.Equal(answer) {
		t.Error("resample upsampling")
		t.Log(result)
		t.Log(answer)
	}

	// round off error case, triggered on my laptop
	p1 := orb.LineString{{-88.145243, 42.321059}, {-88.145232, 42.325902}}
	p1 = Resample(p1, planar.Distance, 109)
	if len(p1) != 109 {
		t.Errorf("incorrect length: %v != 109", len(p1))
	}

	// duplicate points
	ls = orb.LineString{{1, 0}, {1, 0}, {1, 0}}
	ls = Resample(ls, planar.Distance, 10)
	if l := len(ls); l != 10 {
		t.Errorf("length incorrect: %d != 10", l)
	}

	expected := orb.Point{1, 0}
	for i := 0; i < len(ls); i++ {
		if !ls[i].Equal(expected) {
			t.Errorf("incorrect point: %v != %v", ls[i], expected)
		}
	}
}

func TestToInterval(t *testing.T) {
	ls := orb.LineString{{0, 0}, {0, 1}, {0, 10}}

	cases := []struct {
		name     string
		distance float64
		line     orb.LineString
		expected orb.LineString
	}{
		{
			name:     "same number of points",
			distance: 5.0,
			expected: orb.LineString{{0, 0}, {0, 5}, {0, 10}},
		},
		{
			name:     "dist less than 0",
			distance: -5.0,
			expected: nil,
		},
		{
			name:     "dist less than 0",
			distance: -5.0,
			expected: nil,
		},
		{
			name:     "return same if short line",
			distance: 5.0,
			line:     orb.LineString{{0, 0}},
			expected: orb.LineString{{0, 0}},
		},
	}

	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			in := ls.Clone()
			if tc.line != nil {
				in = tc.line
			}

			ls := ToInterval(in, planar.Distance, tc.distance)
			if !ls.Equal(tc.expected) {
				t.Errorf("incorrect point: %v != %v", ls, tc.expected)
			}
		})
	}
}

func TestLineStringResampleEdgeCases(t *testing.T) {
	ls := orb.LineString{{0, 0}}

	_, ret := resampleEdgeCases(ls, 10)
	if !ret {
		t.Errorf("should return true")
	}

	// duplicate points
	ls = append(ls, orb.Point{0, 0})
	ls, ret = resampleEdgeCases(ls, 10)
	if !ret {
		t.Errorf("should return true")
	}

	if l := len(ls); l != 10 {
		t.Errorf("should reset to suggested points: %v != 10", l)
	}

	ls, _ = resampleEdgeCases(ls, 5)
	if l := len(ls); l != 5 {
		t.Errorf("should shorten if necessary: %v != 5", l)
	}
}