mirror of
https://github.com/jhasse/poly2tri.git
synced 2024-12-02 10:13:29 +01:00
updated code
This commit is contained in:
parent
328d8e4962
commit
c03ee9e0ff
@ -76,7 +76,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
var hiLighter = 0
|
var hiLighter = 0
|
||||||
var drawEarClip = false
|
var drawEarClip = false
|
||||||
var drawCDT = true
|
var drawCDT = true
|
||||||
var drawcdtMesh = true
|
var drawcdtMesh = false
|
||||||
|
|
||||||
val nazcaMonkey = "data/nazca_monkey.dat"
|
val nazcaMonkey = "data/nazca_monkey.dat"
|
||||||
val bird = "data/bird.dat"
|
val bird = "data/bird.dat"
|
||||||
@ -109,10 +109,12 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
|
|
||||||
def render(container: GameContainer, g: Graphics) {
|
def render(container: GameContainer, g: Graphics) {
|
||||||
|
|
||||||
g.drawString("'1-6' to cycle models", 10, 540)
|
g.drawString("'1-6' to cycle models", 10, 520)
|
||||||
g.drawString("'SPACE' to turn on debug info", 10, 552)
|
g.drawString("'SPACE' to show Seidel debug info", 10, 532)
|
||||||
g.drawString("'m' to show trapezoidal map (debug mode)", 10, 564)
|
g.drawString("'m' to show trapezoidal map (Seidel debug mode)", 10, 544)
|
||||||
g.drawString("'e' to switch Seidel / EarClip", 10, 576)
|
g.drawString("'e' to switch Seidel / EarClip", 10, 556)
|
||||||
|
g.drawString("'d' to switch CDT / Seidel", 10, 568)
|
||||||
|
g.drawString("'c' to how CDT mesh", 10, 580)
|
||||||
|
|
||||||
g.scale(scaleFactor, scaleFactor)
|
g.scale(scaleFactor, scaleFactor)
|
||||||
g.translate(deltaX, deltaY)
|
g.translate(deltaX, deltaY)
|
||||||
@ -267,7 +269,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
|
|||||||
if(c == '6') selectModel(i18)
|
if(c == '6') selectModel(i18)
|
||||||
if(c == 's') drawSegs = !drawSegs
|
if(c == 's') drawSegs = !drawSegs
|
||||||
if(c == 'c') drawcdtMesh = !drawcdtMesh
|
if(c == 'c') drawcdtMesh = !drawcdtMesh
|
||||||
//if(c == 'e') {drawEarClip = !drawEarClip; selectModel(currentModel)}
|
if(c == 'e') {drawEarClip = !drawEarClip; drawCDT = false; selectModel(currentModel)}
|
||||||
}
|
}
|
||||||
|
|
||||||
def selectModel(model: String) {
|
def selectModel(model: String) {
|
||||||
|
@ -131,19 +131,21 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
|
|
||||||
// Implement sweep-line
|
// Implement sweep-line
|
||||||
private def sweep {
|
private def sweep {
|
||||||
|
|
||||||
for(i <- 1 until points.size) {
|
for(i <- 1 until points.size) {
|
||||||
val point = points(i)
|
val point = points(i)
|
||||||
// Process Point event
|
// Process Point event
|
||||||
var triangle = pointEvent(point)
|
var triangle = pointEvent(point)
|
||||||
// Process edge events
|
// Process edge events
|
||||||
point.edges.foreach(e => triangle = edgeEvent(e, triangle))
|
point.edges.foreach(e => triangle = edgeEvent(e, triangle))
|
||||||
if(i == CDT.clearPoint) {cleanTri = triangle; /*mesh.debug += cleanTri*/}
|
if(i == CDT.clearPoint) {cleanTri = triangle; mesh.debug += cleanTri}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Final step in the sweep-line CDT algo
|
// Final step in the sweep-line CDT algo
|
||||||
// Clean exterior triangles
|
// Clean exterior triangles
|
||||||
private def finalization {
|
private def finalization {
|
||||||
|
|
||||||
mesh.map.foreach(m => m.markEdges)
|
mesh.map.foreach(m => m.markEdges)
|
||||||
mesh clean cleanTri
|
mesh clean cleanTri
|
||||||
//mesh.map.foreach(m => m.edges.foreach(e => if(e) mesh.debug += m))
|
//mesh.map.foreach(m => m.edges.foreach(e => if(e) mesh.debug += m))
|
||||||
@ -172,10 +174,10 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
// EdgeEvent
|
// EdgeEvent
|
||||||
private def edgeEvent(edge: Segment, triangle: Triangle): Triangle = {
|
private def edgeEvent(edge: Segment, triangle: Triangle): Triangle = {
|
||||||
|
|
||||||
// STEP 1: Locate the first intersected triangle
|
// Locate the first intersected triangle
|
||||||
val firstTriangle = triangle.locateFirst(edge)
|
val firstTriangle = triangle.locateFirst(edge)
|
||||||
|
|
||||||
// STEP 2: Remove intersected triangles
|
// Remove intersected triangles
|
||||||
if(firstTriangle != null && !firstTriangle.contains(edge)) {
|
if(firstTriangle != null && !firstTriangle.contains(edge)) {
|
||||||
|
|
||||||
// Collect intersected triangles
|
// Collect intersected triangles
|
||||||
@ -206,24 +208,24 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
val point2 = if(ahead) edge.p else edge.q
|
val point2 = if(ahead) edge.p else edge.q
|
||||||
|
|
||||||
// Collect points left and right of edge
|
// Collect points left and right of edge
|
||||||
tList.foreach(t => {
|
tList.foreach(t => {
|
||||||
t.points.foreach(p => {
|
t.points.foreach(p => {
|
||||||
if(p != edge.q && p != edge.p) {
|
if(p != edge.q && p != edge.p) {
|
||||||
if(t.orient(point1, point2, p) > 0 ) {
|
if(t.orient(point1, point2, p) > 0 ) {
|
||||||
// Keep duplicate points out
|
// Keep duplicate points out
|
||||||
if(!lPoints.contains(p)) {
|
if(!lPoints.contains(p)) {
|
||||||
lPoints += p
|
lPoints += p
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Keep duplicate points out
|
|
||||||
if(!rPoints.contains(p))
|
|
||||||
rPoints += p
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
})
|
// Keep duplicate points out
|
||||||
|
if(!rPoints.contains(p))
|
||||||
|
rPoints += p
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// STEP 3: Triangulate empty areas.
|
// Triangulate empty areas.
|
||||||
val T1 = new ArrayBuffer[Triangle]
|
val T1 = new ArrayBuffer[Triangle]
|
||||||
triangulate(lPoints.toArray, List(point1, point2), T1)
|
triangulate(lPoints.toArray, List(point1, point2), T1)
|
||||||
val T2 = new ArrayBuffer[Triangle]
|
val T2 = new ArrayBuffer[Triangle]
|
||||||
@ -234,6 +236,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
val qNode = aFront.locatePoint(edge.q)
|
val qNode = aFront.locatePoint(edge.q)
|
||||||
val pNode = aFront.locatePoint(edge.p)
|
val pNode = aFront.locatePoint(edge.p)
|
||||||
|
|
||||||
|
// TODO: Make this more robust....
|
||||||
|
|
||||||
for(t <- tList) {
|
for(t <- tList) {
|
||||||
|
|
||||||
if(qNode.triangle == t) {
|
if(qNode.triangle == t) {
|
||||||
@ -265,8 +269,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update neighbors
|
// Update neighbors
|
||||||
updateNeighbors(nTriangles, T1)
|
edgeNeighbors(nTriangles, T1)
|
||||||
updateNeighbors(nTriangles, T2)
|
edgeNeighbors(nTriangles, T2)
|
||||||
|
|
||||||
// Mark constrained edges
|
// Mark constrained edges
|
||||||
T1.first.mark(point1, point2)
|
T1.first.mark(point1, point2)
|
||||||
@ -303,16 +307,15 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
|
|
||||||
val endPoints = if(ahead) List(point2, point1) else List(point1, point2)
|
val endPoints = if(ahead) List(point2, point1) else List(point1, point2)
|
||||||
|
|
||||||
// STEP 3: Triangulate empty areas.
|
// Triangulate empty areas.
|
||||||
val T = new ArrayBuffer[Triangle]
|
val T = new ArrayBuffer[Triangle]
|
||||||
triangulate(points.toArray, endPoints, T)
|
triangulate(points.toArray, endPoints, T)
|
||||||
|
|
||||||
// Update advancing front
|
// Update advancing front
|
||||||
|
|
||||||
aFront -= (first, node.prev, T.first)
|
aFront -= (first, node.prev, T.first)
|
||||||
|
|
||||||
// Update neighbors
|
// Update neighbors
|
||||||
updateNeighbors(nTriangles, T)
|
edgeNeighbors(nTriangles, T)
|
||||||
|
|
||||||
// Mark constrained edge
|
// Mark constrained edge
|
||||||
T.first mark(edge.p, edge.q)
|
T.first mark(edge.p, edge.q)
|
||||||
@ -322,14 +325,14 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
} else {
|
} else {
|
||||||
// Mark constrained edge
|
// Mark constrained edge
|
||||||
firstTriangle mark(edge.p, edge.q)
|
firstTriangle mark(edge.p, edge.q)
|
||||||
triangle
|
triangle
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update neigbor pointers for edge event
|
// Update neigbor pointers for edge event
|
||||||
// Inneficient, but it works well...
|
// Inneficient, but it works well...
|
||||||
def updateNeighbors(nTriangles: ArrayBuffer[Triangle], T: ArrayBuffer[Triangle]) {
|
def edgeNeighbors(nTriangles: ArrayBuffer[Triangle], T: ArrayBuffer[Triangle]) {
|
||||||
|
|
||||||
for(t1 <- nTriangles)
|
for(t1 <- nTriangles)
|
||||||
for(t2 <- T)
|
for(t2 <- T)
|
||||||
@ -443,13 +446,12 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
private def legalization(t1: Triangle, node: Node): Node = {
|
private def legalization(t1: Triangle, node: Node): Node = {
|
||||||
|
|
||||||
val t2 = node.triangle
|
val t2 = node.triangle
|
||||||
|
val point = t1.points(0)
|
||||||
val oPoint = t2 oppositePoint t1
|
val oPoint = t2 oppositePoint t1
|
||||||
|
|
||||||
if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0))) {
|
if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0))) {
|
||||||
|
|
||||||
// Flip edges and rotate everything clockwise
|
// Flip edges and rotate everything clockwise
|
||||||
val point = t1.points(0)
|
|
||||||
t1.legalize(oPoint)
|
t1.legalize(oPoint)
|
||||||
t2.legalize(oPoint, point)
|
t2.legalize(oPoint, point)
|
||||||
|
|
||||||
@ -469,9 +471,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
// Update neighbor
|
// Update neighbor
|
||||||
node.triangle.markNeighbor(t1)
|
t2.markNeighbor(t1)
|
||||||
// Update advancing front
|
// Update advancing front
|
||||||
aFront.insert(t1.points(0), t1, node)
|
aFront.insert(point, t1, node)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user