mirror of
https://github.com/jhasse/poly2tri.git
synced 2024-11-30 01:03:30 +01:00
added earclip algorithm for benchmarking
This commit is contained in:
parent
fe3a7a3967
commit
f5f3fc3b42
@ -35,17 +35,17 @@ case class Point(val x: Float, val y: Float) {
|
|||||||
// Pointers to next and previous points in Monontone Mountain
|
// Pointers to next and previous points in Monontone Mountain
|
||||||
var next, prev: Point = null
|
var next, prev: Point = null
|
||||||
|
|
||||||
def -(p: Point) = Point(x - p.x, y - p.y)
|
@inline def -(p: Point) = Point(x - p.x, y - p.y)
|
||||||
def +(p: Point) = Point(x + p.x, y + p.y)
|
@inline def +(p: Point) = Point(x + p.x, y + p.y)
|
||||||
def +(f: Float) = Point(x + f, y + f)
|
@inline def +(f: Float) = Point(x + f, y + f)
|
||||||
def -(f: Float) = Point(x - f, y - f)
|
@inline def -(f: Float) = Point(x - f, y - f)
|
||||||
def *(f: Float) = Point(x * f, y * f)
|
@inline def *(f: Float) = Point(x * f, y * f)
|
||||||
def /(a: Float) = Point(x / a, y / a)
|
@inline def /(a: Float) = Point(x / a, y / a)
|
||||||
def cross(p: Point) = x * p.y - y * p.x
|
@inline def cross(p: Point) = x * p.y - y * p.x
|
||||||
def dot(p: Point) = x * p.x + y * p.y
|
@inline def dot(p: Point) = x * p.x + y * p.y
|
||||||
def length = Math.sqrt(x * x + y * y).toFloat
|
@inline def length = Math.sqrt(x * x + y * y).toFloat
|
||||||
def normalize = this / length
|
@inline def normalize = this / length
|
||||||
def <(p: Point) = (x < p.x)
|
@inline def <(p: Point) = (x < p.x)
|
||||||
def !(p: Point) = !(p.x == x && p.y == y)
|
@inline def !(p: Point) = !(p.x == x && p.y == y)
|
||||||
override def clone = Point(x, y)
|
@inline override def clone = Point(x, y)
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
|
|
||||||
var tesselator: Triangulator = null
|
var tesselator: Triangulator = null
|
||||||
var segments: ArrayBuffer[Segment] = null
|
var segments: ArrayBuffer[Segment] = null
|
||||||
|
val earClip = new EarClip
|
||||||
|
|
||||||
var quit = false
|
var quit = false
|
||||||
var debug = false
|
var debug = false
|
||||||
@ -63,9 +64,6 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
var drawSegs = true
|
var drawSegs = true
|
||||||
var hiLighter = 0
|
var hiLighter = 0
|
||||||
|
|
||||||
var earClipResults = new Array[Triangle](14)
|
|
||||||
val earClip = new EarClip
|
|
||||||
|
|
||||||
def init(container: GameContainer) {
|
def init(container: GameContainer) {
|
||||||
poly
|
poly
|
||||||
earClipPoly
|
earClipPoly
|
||||||
@ -292,6 +290,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
val polyX = Array(400f, 500f, 520f, 460f, 580f, 480f, 360f, 360f, 300f, 200f, 120f, 200f, 340f, 208f, 180f, 300f)
|
val polyX = Array(400f, 500f, 520f, 460f, 580f, 480f, 360f, 360f, 300f, 200f, 120f, 200f, 340f, 208f, 180f, 300f)
|
||||||
val polyY = Array(472f, 392f, 272f, 232f, 212f, 152f, 172f, 52f, 112f, 32f, 92f, 72f, 272f, 212f, 352f, 312f)
|
val polyY = Array(472f, 392f, 272f, 232f, 212f, 152f, 172f, 52f, 112f, 32f, 92f, 72f, 272f, 212f, 352f, 312f)
|
||||||
|
|
||||||
|
val earClipResults = new Array[Triangle](14)
|
||||||
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
||||||
val t1 = System.nanoTime
|
val t1 = System.nanoTime
|
||||||
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
||||||
@ -304,6 +303,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
val polyX = Array(200f, 300f, 400f, 500f, 600f, 600f, 500f, 400f, 300f, 200f, 110f, 110f)
|
val polyX = Array(200f, 300f, 400f, 500f, 600f, 600f, 500f, 400f, 300f, 200f, 110f, 110f)
|
||||||
val polyY = Array(110f, 200f, 110f, 200f, 110f, 200f, 300f, 200f, 300f, 200f, 300f, 200f)
|
val polyY = Array(110f, 200f, 110f, 200f, 110f, 200f, 300f, 200f, 300f, 200f, 300f, 200f)
|
||||||
|
|
||||||
|
val earClipResults = new Array[Triangle](14)
|
||||||
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
||||||
val t1 = System.nanoTime
|
val t1 = System.nanoTime
|
||||||
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
||||||
@ -327,6 +327,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
val polyX = Array(350f, 379f, 469f, 397f, 423f, 350f, 277f, 303f, 231f, 321f)
|
val polyX = Array(350f, 379f, 469f, 397f, 423f, 350f, 277f, 303f, 231f, 321f)
|
||||||
val polyY = Array(75f, 161f, 161f, 215f, 301f, 250f, 301f,215f, 161f, 161f)
|
val polyY = Array(75f, 161f, 161f, 215f, 301f, 250f, 301f,215f, 161f, 161f)
|
||||||
|
|
||||||
|
val earClipResults = new Array[Triangle](14)
|
||||||
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
for(i <- 0 until earClipResults.size) earClipResults(i) = new Triangle
|
||||||
val t1 = System.nanoTime
|
val t1 = System.nanoTime
|
||||||
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
earClip.triangulatePolygon(polyX, polyY, polyX.size, earClipResults)
|
||||||
|
@ -39,7 +39,7 @@ class Segment(var p: Point, var q: Point) {
|
|||||||
var above, below: Trapezoid = null
|
var above, below: Trapezoid = null
|
||||||
|
|
||||||
// This can be adjusted accordingly
|
// This can be adjusted accordingly
|
||||||
val MAX_MPOINTS = 25
|
val MAX_MPOINTS = 20
|
||||||
// Montone mountain points
|
// Montone mountain points
|
||||||
val mPoints = new Array[Point](MAX_MPOINTS)
|
val mPoints = new Array[Point](MAX_MPOINTS)
|
||||||
// mPoints index counter
|
// mPoints index counter
|
||||||
@ -52,8 +52,8 @@ class Segment(var p: Point, var q: Point) {
|
|||||||
val b = p.y - (p.x * slope)
|
val b = p.y - (p.x * slope)
|
||||||
|
|
||||||
// Determines if this segment lies above the given point
|
// Determines if this segment lies above the given point
|
||||||
def > (point: Point) = (point.y < Math.round(slope * point.x + b))
|
@inline def > (point: Point) = (point.y < Math.round(slope * point.x + b))
|
||||||
// Determines if this segment lies below the given point
|
// Determines if this segment lies below the given point
|
||||||
def < (point: Point) = (point.y > Math.round(slope * point.x + b))
|
@inline def < (point: Point) = (point.y > Math.round(slope * point.x + b))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -49,17 +49,22 @@ class Triangulator(segments: ArrayBuffer[Segment]) {
|
|||||||
|
|
||||||
// Build the trapezoidal map and query graph
|
// Build the trapezoidal map and query graph
|
||||||
def process {
|
def process {
|
||||||
|
|
||||||
val t1 = System.nanoTime
|
val t1 = System.nanoTime
|
||||||
var i = 0
|
var i = 0
|
||||||
|
|
||||||
while(i < segmentList.size) {
|
while(i < segmentList.size) {
|
||||||
|
|
||||||
val s = segmentList(i)
|
val s = segmentList(i)
|
||||||
var traps = queryGraph.followSegment(s)
|
var traps = queryGraph.followSegment(s)
|
||||||
|
|
||||||
// Remove trapezoids from trapezoidal Map
|
// Remove trapezoids from trapezoidal Map
|
||||||
var j = 0
|
var j = 0
|
||||||
while(j < traps.size) {
|
while(j < traps.size) {
|
||||||
trapezoidalMap.remove(traps(j))
|
trapezoidalMap.remove(traps(j))
|
||||||
j += 1
|
j += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
j = 0
|
j = 0
|
||||||
while(j < traps.size) {
|
while(j < traps.size) {
|
||||||
val t = traps(j)
|
val t = traps(j)
|
||||||
@ -91,9 +96,11 @@ class Triangulator(segments: ArrayBuffer[Segment]) {
|
|||||||
}
|
}
|
||||||
j += 1
|
j += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
trapezoidalMap.reset
|
trapezoidalMap.reset
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
coreTime = System.nanoTime - t1
|
coreTime = System.nanoTime - t1
|
||||||
|
|
||||||
// Mark outside trapezoids
|
// Mark outside trapezoids
|
||||||
|
Loading…
Reference in New Issue
Block a user