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.
|
|
|
|
|
|
|
|
package math
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2019-12-29 15:04:32 +01:00
|
|
|
func cross(v0x, v0y, v1x, v1y float32) float32 {
|
|
|
|
return v0x*v1y - v0y*v1x
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func triangleCross(pt0, pt1, pt2 Point) float32 {
|
2019-12-29 15:04:32 +01:00
|
|
|
return cross(pt1.X-pt0.X, pt1.Y-pt0.Y, pt2.X-pt1.X, pt2.Y-pt1.Y)
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func adjacentIndices(indices []uint16, idx int) (uint16, uint16, uint16) {
|
|
|
|
return indices[(idx+len(indices)-1)%len(indices)], indices[idx], indices[(idx+1)%len(indices)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func InTriangle(pt, pt0, pt1, pt2 Point) bool {
|
2019-12-28 18:37:53 +01:00
|
|
|
if pt.X <= pt0.X && pt.X <= pt1.X && pt.X <= pt2.X {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pt.X >= pt0.X && pt.X >= pt1.X && pt.X >= pt2.X {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pt.Y <= pt0.Y && pt.Y <= pt1.Y && pt.Y <= pt2.Y {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pt.Y >= pt0.Y && pt.Y >= pt1.Y && pt.Y >= pt2.Y {
|
|
|
|
return false
|
|
|
|
}
|
2019-12-29 15:04:32 +01:00
|
|
|
c0 := cross(pt.X-pt0.X, pt.Y-pt0.Y, pt1.X-pt0.X, pt1.Y-pt0.Y)
|
|
|
|
c1 := cross(pt.X-pt1.X, pt.Y-pt1.Y, pt2.X-pt1.X, pt2.Y-pt1.Y)
|
|
|
|
c2 := cross(pt.X-pt2.X, pt.Y-pt2.Y, pt0.X-pt2.X, pt0.Y-pt2.Y)
|
2019-12-28 06:57:50 +01:00
|
|
|
return (c0 <= 0 && c1 <= 0 && c2 <= 0) || (c0 >= 0 && c1 >= 0 && c2 >= 0)
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func Triangulate(pts []Point) []uint16 {
|
|
|
|
if len(pts) < 3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentIndices []uint16
|
|
|
|
|
2019-12-28 12:33:53 +01:00
|
|
|
// Split pts into the two point groups if there are the same points.
|
2019-12-27 03:26:16 +01:00
|
|
|
for i := range pts {
|
2019-12-28 12:33:53 +01:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
}
|
2019-12-27 03:26:16 +01:00
|
|
|
currentIndices = append(currentIndices, uint16(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
var indices []uint16
|
|
|
|
|
|
|
|
// Triangulation by Ear Clipping.
|
|
|
|
// https://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf
|
|
|
|
for len(currentIndices) >= 3 {
|
|
|
|
// Calculate cross-products and remove unneeded vertices.
|
|
|
|
cs := make([]float32, len(currentIndices))
|
2019-12-28 08:22:18 +01:00
|
|
|
idxToRemove := -1
|
|
|
|
|
|
|
|
// Determine the direction of the polygon from the upper-left point.
|
|
|
|
var upperLeft int
|
2019-12-27 03:26:16 +01:00
|
|
|
for i := range currentIndices {
|
|
|
|
i0, i1, i2 := adjacentIndices(currentIndices, i)
|
|
|
|
pt0 := pts[i0]
|
|
|
|
pt1 := pts[i1]
|
|
|
|
pt2 := pts[i2]
|
|
|
|
c := triangleCross(pt0, pt1, pt2)
|
|
|
|
if c == 0 {
|
2019-12-28 08:22:18 +01:00
|
|
|
idxToRemove = i
|
2019-12-27 03:26:16 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
cs[i] = c
|
2019-12-28 08:22:18 +01:00
|
|
|
|
|
|
|
if pts[currentIndices[upperLeft]].X > pts[currentIndices[i]].X {
|
|
|
|
upperLeft = i
|
|
|
|
} else if pts[currentIndices[upperLeft]].X == pts[currentIndices[i]].X &&
|
|
|
|
pts[currentIndices[upperLeft]].Y > pts[currentIndices[i]].Y {
|
|
|
|
upperLeft = i
|
|
|
|
}
|
2019-12-27 03:26:16 +01:00
|
|
|
}
|
2019-12-28 08:22:18 +01:00
|
|
|
if idxToRemove != -1 {
|
|
|
|
currentIndices = append(currentIndices[:idxToRemove], currentIndices[idxToRemove+1:]...)
|
2019-12-27 03:26:16 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-28 08:22:18 +01:00
|
|
|
clockwise := cs[upperLeft] < 0
|
|
|
|
|
|
|
|
idx := -1
|
2019-12-27 03:26:16 +01:00
|
|
|
index:
|
|
|
|
for i := range currentIndices {
|
|
|
|
c := cs[i]
|
|
|
|
if c == 0 {
|
|
|
|
panic("math: cross value must not be 0")
|
|
|
|
}
|
|
|
|
if c < 0 && !clockwise || c > 0 && clockwise {
|
|
|
|
// The angle is more than 180 degrees. This is not an ear.
|
|
|
|
continue
|
|
|
|
}
|
2019-12-29 15:00:53 +01:00
|
|
|
|
|
|
|
i0, i1, i2 := adjacentIndices(currentIndices, i)
|
|
|
|
pt0 := pts[i0]
|
|
|
|
pt1 := pts[i1]
|
|
|
|
pt2 := pts[i2]
|
2019-12-27 03:26:16 +01:00
|
|
|
for j := range currentIndices {
|
|
|
|
if l := len(currentIndices); j == (i+l-1)%l || j == i || j == (i+1)%l {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if InTriangle(pts[currentIndices[j]], pt0, pt1, pt2) {
|
|
|
|
// If the triangle includes another point, the triangle is not an ear.
|
|
|
|
continue index
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The angle is less than 180 degrees. This is an ear.
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if idx < 0 {
|
|
|
|
// TODO: This happens when there is self-crossing.
|
|
|
|
panic(fmt.Sprintf("math: there is no ear in the polygon: %v", pts))
|
|
|
|
}
|
|
|
|
i0, i1, i2 := adjacentIndices(currentIndices, idx)
|
|
|
|
indices = append(indices, i0, i1, i2)
|
|
|
|
currentIndices = append(currentIndices[:idx], currentIndices[idx+1:]...)
|
|
|
|
}
|
|
|
|
return indices
|
|
|
|
}
|