Skip to content

Commit 8fba6fd

Browse files
committed
28: MarkEdgeSideOfTriangle
1 parent 342b599 commit 8fba6fd

File tree

9 files changed

+161
-80
lines changed

9 files changed

+161
-80
lines changed

Poly2Tri.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<Compile Include="Triangulation\Sets\PointSet.cs" />
7474
<Compile Include="Triangulation\ITriangulatable.cs" />
7575
<Compile Include="Triangulation\TriangulationAlgorithm.cs" />
76-
<Compile Include="Triangulation\TriangulationConstraint.cs" />
76+
<Compile Include="x_notused\TriangulationConstraint.cs" />
7777
<Compile Include="Triangulation\TriangulationContext.cs" />
7878
<Compile Include="Triangulation\TriangulationDebugContext.cs" />
7979
<Compile Include="Triangulation\TriangulationMode.cs" />

Triangulation/Delaunay/DelaunayTriangle.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,12 @@ public void MarkEdgeConstraint(int index, bool value)
194194
case 1:
195195
this.C1 = value;
196196
break;
197-
default:
197+
case 2:
198198
this.C2 = value;
199199
break;
200+
default:
201+
//may be -1
202+
break;
200203
}
201204
}
202205
public void ClearAllEdgeDelaunayMarks()
@@ -508,10 +511,7 @@ public void MarkEdge(List<DelaunayTriangle> tList)
508511
}
509512
}
510513
}
511-
public void MarkConstrainedEdge(int index)
512-
{
513-
MarkEdgeConstraint(index, true);
514-
}
514+
515515

516516
public void SelectAndMarkConstrainedEdge(DTSweepConstraint edge)
517517
{
@@ -522,13 +522,9 @@ public void SelectAndMarkConstrainedEdge(DTSweepConstraint edge)
522522
/// Mark edge as constrained
523523
/// </summary>
524524
public void SelectAndMarkConstrainedEdge(TriangulationPoint p, TriangulationPoint q)
525-
{
526-
int i = EdgeIndex(p, q);
527-
if (i != -1)
528-
{
529-
//MarkConstrainedEdge(i, true);
530-
MarkEdgeConstraint(i, true);
531-
}
525+
{
526+
MarkEdgeConstraint(EdgeIndex(p, q), true);
527+
532528
}
533529

534530
public double Area()
@@ -552,7 +548,7 @@ public TriangulationPoint Centroid()
552548
/// <returns>index of the shared edge or -1 if edge isn't shared</returns>
553549
public int EdgeIndex(TriangulationPoint p1, TriangulationPoint p2)
554550
{
555-
int i1 = InternalIndexOf(p1);// Points.IndexOf(p1);
551+
int i1 = InternalIndexOf(p1);
556552
int i2 = InternalIndexOf(p2);
557553

558554
// Points of this triangle in the edge p1-p2

Triangulation/Delaunay/Sweep/DTSweep.cs

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,41 @@ private static void Sweep(DTSweepContext tcx)
110110

111111
node = PointEvent(tcx, point);
112112

113-
if (point.HasEdges)
113+
var internalEdgeList = point.GetInternalEdgeList();
114+
115+
if (internalEdgeList != null)
114116
{
115-
foreach (DTSweepConstraint e in point.GetEdgeIter())
117+
int j = internalEdgeList.Count;
118+
119+
if (tcx.IsDebugEnabled)
116120
{
117-
if (tcx.IsDebugEnabled)
121+
for (int m = 0; m < j; ++m)
118122
{
123+
var e = internalEdgeList[m];
119124
tcx.DTDebugContext.ActiveConstraint = e;
125+
EdgeEvent(tcx, e, node);
126+
}
127+
}
128+
else
129+
{
130+
for (int m = 0; m < j; ++m)
131+
{
132+
EdgeEvent(tcx, internalEdgeList[m], node);
120133
}
121-
EdgeEvent(tcx, e, node);
122134
}
123135
}
136+
137+
//if (point.HasEdges)
138+
//{
139+
// foreach (DTSweepConstraint e in point.GetEdgeIter())
140+
// {
141+
// if (tcx.IsDebugEnabled)
142+
// {
143+
// tcx.DTDebugContext.ActiveConstraint = e;
144+
// }
145+
// EdgeEvent(tcx, e, node);
146+
// }
147+
//}
124148
tcx.Update(null);
125149
}
126150
}
@@ -296,7 +320,7 @@ private static void EdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, Advanc
296320

297321
if (tcx.IsDebugEnabled) { tcx.DTDebugContext.PrimaryTriangle = node.Triangle; }
298322

299-
if (IsEdgeSideOfTriangle(node.Triangle, edge.P, edge.Q)) return;
323+
if (MarkEdgeSideOfTriangle(node.Triangle, edge.P, edge.Q)) return;
300324

301325
// For now we will do all needed filling
302326
// TODO: integrate with flip process might give some better performance
@@ -312,7 +336,7 @@ private static void EdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, Advanc
312336
}
313337
}
314338

315-
private static void FillEdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, AdvancingFrontNode node)
339+
static void FillEdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, AdvancingFrontNode node)
316340
{
317341
if (tcx.EdgeEvent.Right)
318342
{
@@ -324,7 +348,7 @@ private static void FillEdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, Ad
324348
}
325349
}
326350

327-
private static void FillRightConcaveEdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, AdvancingFrontNode node)
351+
static void FillRightConcaveEdgeEvent(DTSweepContext tcx, DTSweepConstraint edge, AdvancingFrontNode node)
328352
{
329353
Fill(tcx, node.Next);
330354
if (node.Next.Point != edge.P)
@@ -397,7 +421,7 @@ private static void FillRightAboveEdgeEvent(DTSweepContext tcx, DTSweepConstrain
397421
while (node.Next.Point.X < edge.P.X)
398422
{
399423
if (tcx.IsDebugEnabled) { tcx.DTDebugContext.ActiveNode = node; }
400-
// Check if next node is below the edge
424+
401425
Orientation o1 = TriangulationUtil.Orient2d(edge.Q, node.Next.Point, edge.P);
402426
if (o1 == Orientation.CCW)
403427
{
@@ -496,32 +520,37 @@ private static void FillLeftAboveEdgeEvent(DTSweepContext tcx, DTSweepConstraint
496520
}
497521
}
498522

499-
private static bool IsEdgeSideOfTriangle(DelaunayTriangle triangle, TriangulationPoint ep, TriangulationPoint eq)
523+
524+
private static bool MarkEdgeSideOfTriangle(DelaunayTriangle triangle, TriangulationPoint ep, TriangulationPoint eq)
500525
{
501-
int index = triangle.EdgeIndex(ep, eq);
502-
if (index == -1)
503-
{
504-
return false;
505-
}
506-
triangle.MarkConstrainedEdge(index);
507-
switch (index)
526+
switch (triangle.EdgeIndex(ep, eq))
508527
{
528+
509529
case 0:
510-
{
511-
triangle = triangle.N0;
512-
} break;
530+
{ //mark constraint
531+
triangle.C0 = true;
532+
//change triangle here
533+
if ((triangle = triangle.N0) != null) { triangle.SelectAndMarkConstrainedEdge(ep, eq); }
534+
return true;
535+
}
513536
case 1:
514537
{
515-
triangle = triangle.N1;
516-
} break;
517-
default:
538+
triangle.C1 = true;
539+
//change triangle here
540+
if ((triangle = triangle.N1) != null) { triangle.SelectAndMarkConstrainedEdge(ep, eq); }
541+
return true;
542+
}
543+
case 2:
518544
{
519-
triangle = triangle.N2;
520-
} break;
521-
}
522-
523-
if (triangle != null) triangle.SelectAndMarkConstrainedEdge(ep, eq);
524-
return true;
545+
triangle.C2 = true;
546+
//change triangle here
547+
if ((triangle = triangle.N2) != null) { triangle.SelectAndMarkConstrainedEdge(ep, eq); }
548+
return true;
549+
}
550+
default:
551+
//may be -1
552+
return false;
553+
}
525554
}
526555

527556
private static void EdgeEvent(DTSweepContext tcx, TriangulationPoint ep, TriangulationPoint eq, DelaunayTriangle triangle, TriangulationPoint point)
@@ -530,7 +559,7 @@ private static void EdgeEvent(DTSweepContext tcx, TriangulationPoint ep, Triangu
530559

531560
if (tcx.IsDebugEnabled) tcx.DTDebugContext.PrimaryTriangle = triangle;
532561

533-
if (IsEdgeSideOfTriangle(triangle, ep, eq)) return;
562+
if (MarkEdgeSideOfTriangle(triangle, ep, eq)) return;
534563

535564
p1 = triangle.PointCCWFrom(point);
536565
Orientation o1 = TriangulationUtil.Orient2d(eq, p1, ep);
@@ -575,7 +604,7 @@ private static void EdgeEvent(DTSweepContext tcx, TriangulationPoint ep, Triangu
575604
FlipEdgeEvent(tcx, ep, eq, triangle, point);
576605
}
577606
}
578-
607+
579608
/// <summary>
580609
/// In the case of a pointset with some constraint edges. If a triangle side is collinear
581610
/// with a part of the constraint we split the constraint into two constraints. This could

Triangulation/Delaunay/Sweep/DTSweepContext.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@ public class DTSweepContext : TriangulationContext
6262
public DTSweepContext()
6363
{
6464
}
65-
public override bool IsDebugEnabled
66-
{
67-
get
68-
{
69-
return base.IsDebugEnabled;
70-
}
71-
protected set
72-
{
73-
if (value && DebugContext == null)
74-
{
75-
DebugContext = new DTSweepDebugContext(this);
76-
}
77-
base.IsDebugEnabled = value;
78-
}
79-
}
65+
//public bool IsDebugEnabled
66+
//{
67+
// get
68+
// {
69+
// return base.IsDebugEnabled;
70+
// }
71+
// //protected set
72+
// //{
73+
// // if (value && DebugContext == null)
74+
// // {
75+
// // DebugContext = new DTSweepDebugContext(this);
76+
// // }
77+
// // base.IsDebugEnabled = value;
78+
// //}
79+
//}
8080

8181
public void RemoveFromList(DelaunayTriangle triangle)
8282
{

Triangulation/TriangulationContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public abstract class TriangulationContext
3838
{
3939

4040
public readonly List<DelaunayTriangle> Triangles = new List<DelaunayTriangle>();
41-
public readonly List<TriangulationPoint> Points = new List<TriangulationPoint>(200);
41+
public readonly List<TriangulationPoint> Points = new List<TriangulationPoint>(200);
4242
internal TriangulationContext()
4343
{
4444
}
@@ -70,7 +70,12 @@ public virtual void Clear()
7070
#endif
7171
}
7272

73-
public virtual bool IsDebugEnabled { get; protected set; }
73+
public bool IsDebugEnabled { get; private set; }
74+
75+
protected void SetDebugMode(bool enable)
76+
{
77+
this.IsDebugEnabled = enable;
78+
}
7479

7580
public DTSweepDebugContext DTDebugContext { get { return DebugContext as DTSweepDebugContext; } }
7681

Triangulation/TriangulationDebugContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ namespace Poly2Tri
3333
{
3434
public abstract class TriangulationDebugContext
3535
{
36-
protected TriangulationContext _tcx;
37-
36+
protected TriangulationContext _tcx;
3837
public TriangulationDebugContext(TriangulationContext tcx)
3938
{
4039
_tcx = tcx;

Triangulation/TriangulationMode.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@
2727
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2828
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2929
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30-
*/
31-
32-
namespace Poly2Tri {
33-
public enum TriangulationMode {
34-
Unconstrained,Constrained,Polygon
35-
}
36-
}
30+
*/
31+
32+
namespace Poly2Tri
33+
{
34+
public enum TriangulationMode
35+
{
36+
Unconstrained,
37+
Constrained,
38+
Polygon
39+
}
40+
}

Triangulation/TriangulationPoint.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,21 @@ public void AddEdge(DTSweepConstraint e)
6464
myEdges = new List<DTSweepConstraint>();
6565
}
6666
myEdges.Add(e);
67-
}
68-
public IEnumerable<DTSweepConstraint> GetEdgeIter()
67+
}
68+
//public IEnumerable<DTSweepConstraint> GetEdgeIter()
69+
//{
70+
// List<DTSweepConstraint> edges = this.myEdges;
71+
// if (edges != null)
72+
// {
73+
// foreach (var e in edges)
74+
// {
75+
// yield return e;
76+
// }
77+
// }
78+
//}
79+
internal List<DTSweepConstraint> GetInternalEdgeList()
6980
{
70-
List<DTSweepConstraint> edges = this.myEdges;
71-
if (edges != null)
72-
{
73-
foreach (var e in edges)
74-
{
75-
yield return e;
76-
}
77-
}
81+
return this.myEdges;
7882
}
7983
public bool HasEdges { get { return myEdges != null; } }
8084

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
///* Poly2Tri
2+
// * Copyright (c) 2009-2010, Poly2Tri Contributors
3+
// * http://code.google.com/p/poly2tri/
4+
// *
5+
// * All rights reserved.
6+
// *
7+
// * Redistribution and use in source and binary forms, with or without modification,
8+
// * are permitted provided that the following conditions are met:
9+
// *
10+
// * * Redistributions of source code must retain the above copyright notice,
11+
// * this list of conditions and the following disclaimer.
12+
// * * Redistributions in binary form must reproduce the above copyright notice,
13+
// * this list of conditions and the following disclaimer in the documentation
14+
// * and/or other materials provided with the distribution.
15+
// * * Neither the name of Poly2Tri nor the names of its contributors may be
16+
// * used to endorse or promote products derived from this software without specific
17+
// * prior written permission.
18+
// *
19+
// * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
// * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
// * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
// * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
// * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
// * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
// * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
// * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
// */
31+
///**
32+
// * Forces a triangle edge between two points p and q
33+
// * when triangulating. For example used to enforce
34+
// * Polygon Edges during a polygon triangulation.
35+
// *
36+
// * @author Thomas Åhlén, thahlen@gmail.com
37+
// */
38+
39+
//namespace Poly2Tri {
40+
// public class TriangulationConstraint {
41+
// public TriangulationPoint P;
42+
// public TriangulationPoint Q;
43+
// }
44+
//}

0 commit comments

Comments
 (0)