vector: Bug fix: Wrong rendering when there are some same points

Updates #845
This commit is contained in:
Hajime Hoshi 2019-12-28 20:33:53 +09:00
parent 6edb586f52
commit bb39e94e8c
2 changed files with 41 additions and 15 deletions

View File

@ -44,8 +44,21 @@ func Triangulate(pts []Point) []uint16 {
var currentIndices []uint16
// Remove duplicated points
// Split pts into the two point groups if there are the same points.
for i := range pts {
for j := 0; j < i; j++ {
if pts[i] == pts[j] {
is0 := Triangulate(pts[j:i])
for idx := range is0 {
is0[idx] += uint16(j)
}
is1 := Triangulate(append(pts[i:], pts[:j]...))
for idx := range is1 {
is1[idx] = uint16((int(is1[idx]) + i) % len(pts))
}
return append(is0, is1...)
}
}
currentIndices = append(currentIndices, uint16(i))
}
@ -61,12 +74,6 @@ func Triangulate(pts []Point) []uint16 {
// Determine the direction of the polygon from the upper-left point.
var upperLeft int
for i := range currentIndices {
next := (i + 1) % len(currentIndices)
if pts[currentIndices[i]] == pts[currentIndices[next]] {
idxToRemove = next
break
}
i0, i1, i2 := adjacentIndices(currentIndices, i)
pt0 := pts[i0]
pt1 := pts[i1]
@ -112,10 +119,6 @@ func Triangulate(pts []Point) []uint16 {
if l := len(currentIndices); j == (i+l-1)%l || j == i || j == (i+1)%l {
continue
}
jj := currentIndices[j]
if pts[i0] == pts[jj] || pts[i1] == pts[jj] || pts[i2] == pts[jj] {
continue
}
if InTriangle(pts[currentIndices[j]], pt0, pt1, pt2) {
// If the triangle includes another point, the triangle is not an ear.
continue index

View File

@ -213,7 +213,7 @@ func TestTriangulate(t *testing.T) {
{3, 1},
{2, 0},
},
Out: []uint16{8, 0, 1, 1, 2, 4, 1, 4, 5, 8, 1, 5, 8, 5, 7},
Out: []uint16{6, 7, 8, 6, 8, 0, 4, 0, 1, 4, 1, 3},
},
{
In: []Point{
@ -248,14 +248,14 @@ func TestTriangulate(t *testing.T) {
{
// Butterfly
In: []Point{
{0, 0},
{0, 2},
{1, 1},
{2, 2},
{2, 0},
{1, 1},
{0, 0},
},
Out: []uint16{5, 0, 1, 5, 3, 4},
Out: []uint16{3, 1, 2, 0, 4, 5},
},
{
In: []Point{
@ -268,7 +268,30 @@ func TestTriangulate(t *testing.T) {
{6, 0},
{6, 3},
},
Out: []uint16{7, 0, 1, 7, 1, 2, 7, 4, 5, 7, 5, 6},
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},
},
Out: []uint16{7, 8, 9, 7, 9, 10, 12, 7, 10, 12, 10, 11, 6, 13, 14, 6, 14, 15, 6, 15, 16, 17, 0, 1, 17, 1, 2, 16, 17, 2, 16, 2, 3, 16, 3, 4, 16, 4, 5, 6, 16, 5},
},
}
for _, tc := range tests {