2019-12-27 03:26:16 +01:00
|
|
|
// Copyright 2019 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-12-29 15:45:14 +01:00
|
|
|
package triangulate_test
|
2019-12-27 03:26:16 +01:00
|
|
|
|
|
|
|
import (
|
2019-12-29 15:54:31 +01:00
|
|
|
"math"
|
2019-12-27 03:26:16 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-29 15:45:14 +01:00
|
|
|
. "github.com/hajimehoshi/ebiten/vector/internal/triangulate"
|
2019-12-27 03:26:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsInTriangle(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Tri []Point
|
|
|
|
Pt Point
|
|
|
|
Out bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Tri: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 10},
|
|
|
|
{10, 10},
|
|
|
|
},
|
|
|
|
Pt: Point{1, 9},
|
|
|
|
Out: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tri: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 10},
|
|
|
|
{10, 10},
|
|
|
|
},
|
|
|
|
Pt: Point{8, 9},
|
|
|
|
Out: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tri: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 10},
|
|
|
|
{10, 10},
|
|
|
|
},
|
|
|
|
Pt: Point{10, 9},
|
|
|
|
Out: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tri: []Point{
|
|
|
|
{3, 5},
|
|
|
|
{2, 7},
|
|
|
|
{7, 7},
|
|
|
|
},
|
|
|
|
Pt: Point{3, 6},
|
|
|
|
Out: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Tri: []Point{
|
|
|
|
{3, 5},
|
|
|
|
{2, 7},
|
|
|
|
{7, 7},
|
|
|
|
},
|
|
|
|
Pt: Point{7, 6},
|
|
|
|
Out: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
got := InTriangle(tc.Pt, tc.Tri[0], tc.Tri[1], tc.Tri[2])
|
|
|
|
want := tc.Out
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("InTriangle(%v, %v, %v, %v): got: %t, want: %t", tc.Pt, tc.Tri[0], tc.Tri[1], tc.Tri[2], got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTriangulate(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
In []Point
|
|
|
|
Out []uint16
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
In: []Point{},
|
|
|
|
Out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
},
|
|
|
|
Out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 1},
|
|
|
|
},
|
|
|
|
Out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 0},
|
|
|
|
{1, 1},
|
|
|
|
},
|
|
|
|
Out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0.5, 0.5},
|
|
|
|
{1, 1},
|
|
|
|
},
|
|
|
|
Out: nil,
|
|
|
|
},
|
2019-12-27 19:17:37 +01:00
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0.5, 0.5},
|
|
|
|
{1.5, 1.5},
|
|
|
|
{1, 1},
|
|
|
|
},
|
|
|
|
Out: nil,
|
|
|
|
},
|
2019-12-27 03:26:16 +01:00
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 1},
|
|
|
|
{1, 1},
|
|
|
|
},
|
|
|
|
Out: []uint16{2, 0, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{1, 1},
|
|
|
|
{0, 1},
|
|
|
|
},
|
|
|
|
Out: []uint16{2, 0, 1},
|
|
|
|
},
|
2019-12-28 06:57:50 +01:00
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{1, 1},
|
|
|
|
{0, 1},
|
|
|
|
{0, 0.5},
|
|
|
|
},
|
|
|
|
Out: []uint16{2, 0, 1},
|
|
|
|
},
|
2019-12-27 03:26:16 +01:00
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 1},
|
|
|
|
{1, 1},
|
|
|
|
{1, 0},
|
|
|
|
},
|
|
|
|
Out: []uint16{3, 0, 1, 3, 1, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{2, 2},
|
|
|
|
{2, 7},
|
|
|
|
{7, 7},
|
|
|
|
{7, 6},
|
|
|
|
{3, 6},
|
|
|
|
{3, 5},
|
|
|
|
},
|
|
|
|
Out: []uint16{5, 0, 1, 1, 2, 3, 1, 3, 4, 5, 1, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{2, 2},
|
|
|
|
{2, 7},
|
|
|
|
{7, 7},
|
|
|
|
{7, 6},
|
|
|
|
{3, 6},
|
|
|
|
{3, 5},
|
|
|
|
{7, 5},
|
|
|
|
{7, 4},
|
|
|
|
{3, 4},
|
|
|
|
{3, 3},
|
|
|
|
},
|
2019-12-28 18:37:53 +01:00
|
|
|
Out: []uint16{9, 0, 1, 1, 2, 3, 1, 3, 4, 9, 1, 4, 8, 5, 6, 8, 6, 7},
|
2019-12-28 06:57:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 5},
|
|
|
|
{2, 5},
|
|
|
|
{3, 3},
|
|
|
|
{2, 2},
|
|
|
|
{3, 1},
|
|
|
|
{2, 0},
|
|
|
|
},
|
2019-12-28 18:37:53 +01:00
|
|
|
Out: []uint16{6, 0, 1, 6, 1, 2, 6, 2, 3, 4, 5, 6, 6, 3, 4},
|
2019-12-28 06:57:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 5},
|
|
|
|
{2, 5},
|
|
|
|
{2, 5},
|
|
|
|
{3, 3},
|
|
|
|
{2, 2},
|
|
|
|
{2, 2},
|
|
|
|
{3, 1},
|
|
|
|
{2, 0},
|
|
|
|
},
|
2019-12-28 12:33:53 +01:00
|
|
|
Out: []uint16{6, 7, 8, 6, 8, 0, 4, 0, 1, 4, 1, 3},
|
2019-12-27 03:26:16 +01:00
|
|
|
},
|
2019-12-28 08:22:18 +01:00
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 0},
|
|
|
|
{0, 1},
|
|
|
|
{1, 1},
|
|
|
|
{1, 0},
|
|
|
|
{2, 0},
|
|
|
|
},
|
|
|
|
Out: []uint16{3, 0, 1, 3, 1, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{2, 0},
|
|
|
|
{0, 0},
|
|
|
|
{1, 0},
|
|
|
|
{1, 1},
|
|
|
|
{2, 1},
|
|
|
|
},
|
|
|
|
Out: []uint16{4, 0, 2, 4, 2, 3},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{2, 0},
|
|
|
|
{2, 1},
|
|
|
|
{1, 1},
|
|
|
|
{1, 0},
|
|
|
|
{0, 0},
|
|
|
|
},
|
|
|
|
Out: []uint16{3, 0, 1, 3, 1, 2},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Butterfly
|
|
|
|
In: []Point{
|
|
|
|
{0, 2},
|
|
|
|
{1, 1},
|
|
|
|
{2, 2},
|
|
|
|
{2, 0},
|
|
|
|
{1, 1},
|
2019-12-28 12:33:53 +01:00
|
|
|
{0, 0},
|
2019-12-28 08:22:18 +01:00
|
|
|
},
|
2019-12-28 12:33:53 +01:00
|
|
|
Out: []uint16{3, 1, 2, 0, 4, 5},
|
2019-12-28 08:22:18 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 6},
|
|
|
|
{0, 9},
|
|
|
|
{6, 6},
|
|
|
|
{6, 3},
|
|
|
|
{9, 3},
|
|
|
|
{8, 0},
|
|
|
|
{6, 0},
|
|
|
|
{6, 3},
|
|
|
|
},
|
2019-12-28 12:33:53 +01:00
|
|
|
Out: []uint16{6, 3, 4, 6, 4, 5, 2, 7, 0, 2, 0, 1},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
In: []Point{
|
|
|
|
{0, 4},
|
|
|
|
{0, 6},
|
|
|
|
{2, 6},
|
|
|
|
{2, 5},
|
|
|
|
{3, 5},
|
|
|
|
{3, 4},
|
|
|
|
{4, 4},
|
|
|
|
{4, 2},
|
|
|
|
{6, 2},
|
|
|
|
{6, 1},
|
|
|
|
{5, 1},
|
|
|
|
{5, 0},
|
|
|
|
{4, 0},
|
|
|
|
{4, 2},
|
|
|
|
{2, 2},
|
|
|
|
{2, 3},
|
|
|
|
{1, 3},
|
|
|
|
{1, 4},
|
|
|
|
},
|
2019-12-28 18:37:53 +01:00
|
|
|
Out: []uint16{7, 8, 9, 7, 9, 10, 12, 7, 10, 12, 10, 11, 6, 13, 14, 6, 14, 15, 6, 15, 16, 6, 16, 17, 5, 0, 1, 1, 2, 3, 5, 1, 3, 5, 3, 4},
|
2019-12-28 08:22:18 +01:00
|
|
|
},
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
|
|
got := Triangulate(tc.In)
|
|
|
|
want := tc.Out
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("Triangulate(%v): got: %v, want: %v", tc.In, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 15:54:31 +01:00
|
|
|
|
|
|
|
var benchmarkPath []Point
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
const (
|
|
|
|
w = 640
|
|
|
|
h = 480
|
|
|
|
)
|
|
|
|
var p []Point
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
x := float32(i)
|
|
|
|
y := h/2 + 80*float32(math.Sin(2*math.Pi*float64(i)/40))
|
|
|
|
p = append(p, Point{X: x, Y: y})
|
|
|
|
}
|
|
|
|
p = append(p, Point{w, h}, Point{0, h})
|
|
|
|
benchmarkPath = p
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTriangulate(b *testing.B) {
|
|
|
|
for n := 0; n < b.N; n++ {
|
|
|
|
Triangulate(benchmarkPath)
|
|
|
|
}
|
|
|
|
}
|