Skip to content

Commit 6468fa5

Browse files
committed
0.9.0
1 parent 8f1ccc3 commit 6468fa5

File tree

4 files changed

+109
-100
lines changed

4 files changed

+109
-100
lines changed

AddOns/ClipperAddOns.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
using System.Collections.Generic;
1111
using UnityEngine;
1212

13-
using System.Linq;
14-
using ClipperLib;
15-
1613

1714
namespace EPPZ.Geometry.AddOns
1815
{
1916

2017

18+
using System.Linq;
19+
using ClipperLib;
20+
21+
2122
// Clipper definitions.
22-
using Path = List<IntPoint>;
23-
using Paths = List<List<IntPoint>>;
23+
using Path = List<ClipperLib.IntPoint>;
24+
using Paths = List<List<ClipperLib.IntPoint>>;
2425

2526

2627
public static class ClipperAddOns
Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,73 @@
55
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
66
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77
//
8-
// #define ADDONS_ENABLED
9-
#if ADDONS_ENABLED
108
using System;
119
using System.Collections;
1210
using System.Collections.Generic;
1311
using UnityEngine;
1412

15-
using System.Linq;
16-
using ClipperLib;
1713

18-
using TriangleNet.Algorithm;
19-
using TriangleNet.Geometry;
20-
using TriangleNet.Data;
21-
using TriangleNet.Tools;
22-
using MeshExplorer.Generators;
14+
namespace EPPZ.Geometry.AddOns
15+
{
2316

2417

18+
using System.Linq;
19+
using ClipperLib;
2520

26-
namespace EPPZ.Geometry
27-
{
28-
21+
using TriangleNet.Geometry;
2922

3023
// Clipper definitions.
31-
using Path = List<IntPoint>;
32-
using Paths = List<List<IntPoint>>;
24+
using Path = List<ClipperLib.IntPoint>;
25+
using Paths = List<List<ClipperLib.IntPoint>>;
3326

3427

35-
public static class Geometry_TriangleNet
28+
public static class TriangleNetAddOns
3629
{
3730

3831

3932
#region Polygon
4033

41-
public static InputGeometry InputGeometry(this Polygon this_)
34+
public static TriangleNet.Geometry.Polygon TriangleNetPolygon(this EPPZ.Geometry.Polygon this_)
4235
{
43-
InputGeometry geometry = new InputGeometry();
44-
int boundary;
36+
TriangleNet.Geometry.Polygon polygon = new TriangleNet.Geometry.Polygon();
4537

46-
// Add points.
47-
boundary = 1;
48-
int pointIndexOffset = 0;
49-
this_.EnumeratePolygons((Polygon eachPolygon) =>
38+
int boundary = 1;
39+
List<TriangleNet.Geometry.Vertex> vertices = new List<TriangleNet.Geometry.Vertex>();
40+
this_.EnumeratePolygons((EPPZ.Geometry.Polygon eachPolygon) =>
5041
{
51-
// Add points.
42+
// Collect vertices.
43+
vertices.Clear();
5244
eachPolygon.EnumeratePoints((Vector2 eachPoint) =>
5345
{
54-
geometry.AddPoint(
46+
vertices.Add(new Vertex(
5547
(double)eachPoint.x,
5648
(double)eachPoint.y,
57-
boundary);
49+
boundary
50+
));
5851
});
5952

60-
eachPolygon.EnumerateEdges((EPPZ.Geometry.Edge eachEdge) =>
61-
{
62-
int index_a = eachEdge.vertexA.index + pointIndexOffset;
63-
int index_b = eachEdge.vertexB.index + pointIndexOffset;
64-
geometry.AddSegment(index_a, index_b, boundary);
65-
});
53+
// Add controur.
54+
polygon.Add(new Contour(vertices.ToArray(), boundary));
6655

67-
pointIndexOffset += eachPolygon.vertexCount; // Track point offsets.
56+
// Track.
6857
boundary++;
6958
});
7059

71-
return geometry;
60+
return polygon;
7261
}
7362

7463
#endregion
7564

7665

77-
#region Voronoi
66+
#region Voronoi (beta 3)
7867

79-
public static Rect Bounds(this Voronoi this_)
68+
public static Rect Bounds(this TriangleNet.Voronoi.Legacy.SimpleVoronoi this_)
8069
{
8170
float xmin = float.MaxValue;
8271
float xmax = 0.0f;
8372
float ymin = float.MaxValue;
8473
float ymax = 0.0f;
85-
foreach (VoronoiRegion region in this_.Regions)
74+
foreach (TriangleNet.Voronoi.Legacy.VoronoiRegion region in this_.Regions)
8675
{
8776
foreach (Point eachPoint in region.Vertices)
8877
{
@@ -95,6 +84,25 @@ public static Rect Bounds(this Voronoi this_)
9584
return Rect.MinMaxRect(xmin, ymin, xmax, ymax);
9685
}
9786

87+
public static Paths ClipperPathsFromVoronoiRegions(List<TriangleNet.Voronoi.Legacy.VoronoiRegion> voronoiRegions, float scale = 1.0f)
88+
{
89+
Paths paths = new Paths();
90+
91+
foreach (TriangleNet.Voronoi.Legacy.VoronoiRegion eachRegion in voronoiRegions)
92+
{
93+
Path eachPath = new Path();
94+
foreach (Point eachPoint in eachRegion.Vertices)
95+
{
96+
eachPath.Add(new IntPoint(
97+
eachPoint.X * scale,
98+
eachPoint.Y * scale
99+
));
100+
}
101+
paths.Add(eachPath);
102+
}
103+
return paths;
104+
}
105+
98106
#endregion
99107

100108

@@ -117,26 +125,7 @@ public static Vector2[] PointsFromVertices(ICollection<Point> vertices)
117125
return points;
118126
}
119127

120-
public static Paths ClipperPathsFromVoronoiRegions(List<VoronoiRegion> voronoiRegions, float scale = 1.0f)
121-
{
122-
Paths paths = new Paths();
123-
foreach (VoronoiRegion eachRegion in voronoiRegions)
124-
{
125-
Path eachPath = new Path();
126-
foreach (Point eachPoint in eachRegion.Vertices)
127-
{
128-
eachPath.Add(new IntPoint(
129-
eachPoint.X * scale,
130-
eachPoint.Y * scale
131-
));
132-
}
133-
paths.Add(eachPath);
134-
}
135-
return paths;
136-
}
137-
138128
#endregion
139129

140130
}
141131
}
142-
#endif
Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,70 @@
55
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
66
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
77
//
8-
// #define ADDONS_ENABLED
9-
#if ADDONS_ENABLED
108
using System;
119
using System.Collections;
1210
using System.Collections.Generic;
1311
using UnityEngine;
1412

15-
using TriangleNet.Algorithm;
16-
using TriangleNet.Geometry;
17-
using TriangleNet.Data;
18-
using TriangleNet.Tools;
19-
using MeshExplorer.Generators;
2013

14+
namespace EPPZ.Geometry.AddOns
15+
{
2116

2217

23-
namespace EPPZ.Geometry
24-
{
18+
using TriangleNet.Geometry;
19+
using TriangleNet.Meshing;
20+
using TriangleNet.Meshing.Algorithm;
21+
using TriangleNet.Tools;
22+
23+
24+
public enum TriangulatorType { Incremental, Dwyer, SweepLine };
2525

2626

27-
public static class Geometry_UnityEngine
27+
public static class UnityEngineAddOns
2828
{
2929

3030

31+
static ITriangulator TriangulatorForType(TriangulatorType triangulator)
32+
{
33+
switch (triangulator)
34+
{
35+
case TriangulatorType.Incremental : return new Incremental();
36+
case TriangulatorType.Dwyer : return new Dwyer();
37+
case TriangulatorType.SweepLine : return new SweepLine();
38+
default : return new Dwyer();
39+
}
40+
}
41+
42+
3143
#region Polygon
3244

33-
public static UnityEngine.Mesh Mesh(this Polygon this_, string name = "")
34-
{ return this_.Mesh(Color.white, name); }
45+
public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Polygon this_, string name = "")
46+
{ return this_.Mesh(Color.white, TriangulatorType.Dwyer, name); }
3547

36-
public static UnityEngine.Mesh Mesh(this Polygon this_, Color color, string name = "")
48+
public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Polygon this_, TriangulatorType triangulator, string name = "")
49+
{ return this_.Mesh(Color.white, triangulator, name); }
50+
51+
public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Polygon this_, Color color, TriangulatorType triangulator, string name = "")
3752
{
3853
// Create geometry.
39-
InputGeometry geometry = this_.InputGeometry();
54+
TriangleNet.Geometry.Polygon polygon = this_.TriangleNetPolygon();
4055

4156
// Triangulate.
42-
TriangleNet.Behavior behavior = new TriangleNet.Behavior();
43-
behavior.Convex = false;
44-
behavior.NoHoles = false;
45-
// behavior.Algorithm = TriangleNet.TriangulationAlgorithm.Incremental; // SweepLine, Dwyer
46-
behavior.UseBoundaryMarkers = true;
47-
48-
TriangleNet.Mesh triangulatedMesh = new TriangleNet.Mesh(behavior);
49-
triangulatedMesh.Triangulate(geometry);
57+
ConstraintOptions options = new ConstraintOptions();
58+
// ConformingDelaunay
59+
// Convex
60+
// SegmentSplitting
61+
QualityOptions quality = new QualityOptions();
62+
// MinimumAngle
63+
// MaximumArea
64+
// UserTest
65+
// VariableArea
66+
// SteinerPoints
67+
IMesh triangulatedMesh = polygon.Triangulate(options, quality, TriangulatorForType(triangulator));
5068

5169
// Counts.
52-
int vertexCount = triangulatedMesh.vertices.Count;
53-
int triangleCount = triangulatedMesh.triangles.Count;
54-
55-
// Debug.Log("Mesh.vertexCount ("+vertexCount+")"); // NumberOfInputPoints
56-
// Debug.Log("Mesh.triangleCount ("+triangleCount+")"); // NumberOfInputPoints
70+
int vertexCount = triangulatedMesh.Vertices.Count;
71+
int triangleCount = triangulatedMesh.Triangles.Count;
5772

5873
// Mesh store.
5974
Vector3[] _vertices = new Vector3[vertexCount];
@@ -62,10 +77,9 @@ public static UnityEngine.Mesh Mesh(this Polygon this_, Color color, string name
6277
Color[] _colors = new Color[vertexCount];
6378
int[] _triangles = new int[triangleCount * 3];
6479

65-
foreach (KeyValuePair<int, TriangleNet.Data.Vertex> eachEntry in triangulatedMesh.vertices)
80+
int index = 0;
81+
foreach (TriangleNet.Geometry.Vertex eachVertex in triangulatedMesh.Vertices)
6682
{
67-
int index = eachEntry.Key;
68-
TriangleNet.Data.Vertex eachVertex = eachEntry.Value;
6983

7084
_vertices[index] = new Vector3(
7185
(float)eachVertex.x,
@@ -76,24 +90,24 @@ public static UnityEngine.Mesh Mesh(this Polygon this_, Color color, string name
7690
_uv[index] = _vertices[index];
7791
_normals[index] = Vector3.forward;
7892
_colors[index] = color;
93+
94+
index++;
7995
}
8096

8197
int cursor = 0;
82-
foreach (KeyValuePair<int, TriangleNet.Data.Triangle> eachPair in triangulatedMesh.triangles)
98+
foreach (TriangleNet.Topology.Triangle eachTriangle in triangulatedMesh.Triangles)
8399
{
84-
TriangleNet.Data.Triangle eachTriangle = eachPair.Value;
85-
86100
Debug.Log(
87101
"ID: "+eachTriangle.id+
88102
" id: "+eachTriangle.ID+
89-
" Region: "+eachTriangle.Region+
90-
" Area: "+eachTriangle.Area+
91-
" Boundary: "+eachTriangle.GetVertex(0).Boundary
103+
// " Region: "+eachTriangle.Region+
104+
" Area: "+eachTriangle.Area
105+
// " Boundary: "+eachTriangle.GetVertex(0).Boundary
92106
);
93107

94-
_triangles[cursor] = eachTriangle.P2;
95-
_triangles[cursor + 1] = eachTriangle.P1;
96-
_triangles[cursor + 2] = eachTriangle.P0;
108+
_triangles[cursor] = eachTriangle.GetVertexID(2); // P2
109+
_triangles[cursor + 1] = eachTriangle.GetVertexID(1); // P1
110+
_triangles[cursor + 2] = eachTriangle.GetVertexID(0); // P0
97111
cursor += 3;
98112
}
99113

@@ -112,7 +126,7 @@ public static UnityEngine.Mesh Mesh(this Polygon this_, Color color, string name
112126

113127
#endregion
114128

129+
115130
}
116131
}
117-
#endif
118132

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# eppz! `Geometry`
22

3+
* 0.9.0
4+
5+
+ Added `TriangleNetAddOns`
6+
+ Added `UnityEngineAdOns`
7+
38
* 0.8.3
49

510
+ Added `ClipperAddOns`

0 commit comments

Comments
 (0)