Skip to content

Commit 57d8d40

Browse files
committed
1.0.0
1 parent 70e9b37 commit 57d8d40

File tree

11 files changed

+158
-51
lines changed

11 files changed

+158
-51
lines changed

AddOns/UnityEngineAddOns.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static ITriangulator TriangulatorForType(TriangulatorType triangulator)
3939
}
4040
}
4141

42+
static bool skipCentroidTest = false; // For debugging
43+
4244

4345
#region Polygon
4446

@@ -75,12 +77,12 @@ public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, Colo
7577
Vector2[] _uv = new Vector2[vertexCount];
7678
Vector3[] _normals = new Vector3[vertexCount];
7779
Color[] _colors = new Color[vertexCount];
78-
int[] _triangles = new int[triangleCount * 3];
80+
List<int> _triangles = new List<int>(); // Size may vary
7981

82+
// Vertices.
8083
int index = 0;
8184
foreach (TriangleNet.Geometry.Vertex eachVertex in triangulatedMesh.Vertices)
8285
{
83-
8486
_vertices[index] = new Vector3(
8587
(float)eachVertex.x,
8688
(float)eachVertex.y,
@@ -94,21 +96,27 @@ public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, Colo
9496
index++;
9597
}
9698

97-
int cursor = 0;
99+
// Triangles.
98100
foreach (TriangleNet.Topology.Triangle eachTriangle in triangulatedMesh.Triangles)
99101
{
100-
Debug.Log(
101-
"ID: "+eachTriangle.id+
102-
" id: "+eachTriangle.ID+
103-
// " Region: "+eachTriangle.Region+
104-
" Area: "+eachTriangle.Area
105-
// " Boundary: "+eachTriangle.GetVertex(0).Boundary
102+
// Get vertices.
103+
Point P2 = eachTriangle.GetVertex(2);
104+
Point P1 = eachTriangle.GetVertex(1);
105+
Point P0 = eachTriangle.GetVertex(0);
106+
107+
// Get centroid.
108+
Vector2 centroid = new Vector2(
109+
(float)(P2.X + P1.X + P0.X) / 3.0f,
110+
(float)(P2.Y + P1.Y + P0.Y) / 3.0f
106111
);
107112

108-
_triangles[cursor] = eachTriangle.GetVertexID(2); // P2
109-
_triangles[cursor + 1] = eachTriangle.GetVertexID(1); // P1
110-
_triangles[cursor + 2] = eachTriangle.GetVertexID(0); // P0
111-
cursor += 3;
113+
// Add only if centroid contained.
114+
if (this_.ContainsPoint(centroid) || skipCentroidTest)
115+
{
116+
_triangles.Add(P2.ID);
117+
_triangles.Add(P1.ID);
118+
_triangles.Add(P0.ID);
119+
}
112120
}
113121

114122
// Create / setup mesh.
@@ -118,7 +126,7 @@ public static UnityEngine.Mesh Mesh(this EPPZ.Geometry.Model.Polygon this_, Colo
118126
mesh.normals = _normals;
119127
mesh.colors = _colors;
120128
mesh.subMeshCount = 1;
121-
mesh.SetTriangles(_triangles, 0);
129+
mesh.SetTriangles(_triangles.ToArray(), 0);
122130
mesh.name = name;
123131

124132
return mesh;

CHANGELOG.md

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

3+
* 1.0.0
4+
5+
+ Triangulation
6+
+ Resolved issue with inner triangles
7+
+ Skip triangles if centroid not contained by polygon
8+
+ `Source`
9+
+ `Mesh`
10+
+ Component to feed `MeshFilter` components
11+
+ `Polygon` / `Segment`
12+
+ Added `coordinates` to process `World` / `Local` coordinates
13+
314
* 0.9.7
415

516
+ Triangulation

Lines/GeometryLineRenderer.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public class GeometryLineRenderer : DirectLineRenderer
2222
{
2323

2424

25-
protected void DrawSegment(Segment segment, Color color)
26-
{ DrawLine(segment.a, segment.b, color); }
27-
28-
protected void DrawSegment(Segment segment, Color color, bool drawNormals)
25+
protected void DrawSegment(Segment segment, Color color, bool drawNormals = false)
2926
{
3027
DrawLine(segment.a, segment.b, color);
3128
if (drawNormals)
@@ -34,6 +31,16 @@ protected void DrawSegment(Segment segment, Color color, bool drawNormals)
3431
DrawLine(halfie, halfie + segment.normal * 0.1f, color);
3532
}
3633
}
34+
35+
protected void DrawSegmentWithTransform(Segment segment, Color color, Transform transform_, bool drawNormals = false)
36+
{
37+
DrawLineWithTransform(segment.a, segment.b, color, transform_);
38+
if (drawNormals)
39+
{
40+
Vector2 halfie = segment.a + ((segment.b - segment.a) / 2.0f);
41+
DrawLineWithTransform(halfie, halfie + segment.normal * 0.1f, color, transform_);
42+
}
43+
}
3744

3845
protected void DrawPolygon(Polygon polygon, Color color)
3946
{ polygon.EnumerateEdgesRecursive((Edge eachEdge) => DrawLine(eachEdge.a, eachEdge.b, color)); }

Lines/PolygonLineRenderer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ protected override void OnDraw()
4545

4646
if (polygon == null) return; // Only having polygon
4747

48-
DrawRect(polygon.bounds, boundsColor);
49-
DrawPolygon(polygon, lineColor, normals);
48+
if (polygonSource.coordinates == Source.Polygon.Coordinates.World)
49+
{
50+
DrawRect(polygon.bounds, boundsColor);
51+
DrawPolygon(polygon, lineColor, normals);
52+
}
53+
else
54+
{
55+
DrawRectWithTransform(polygon.bounds, boundsColor, this.transform);
56+
DrawPolygonWithTransform(polygon, lineColor, this.transform, normals);
57+
}
5058
}
5159
}
5260
}

Lines/SegmentLineRenderer.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,29 @@ public class SegmentLineRenderer : GeometryLineRenderer
2525
public Color lineColor;
2626
public Color boundsColor;
2727
public bool normals = false;
28-
private Segment segment;
2928

29+
Segment segment;
30+
Source.Segment segmentSource;
3031

3132
void Start()
3233
{
3334
// Model reference.
34-
Source.Segment segmentSource = GetComponent<Source.Segment>();
35+
segmentSource = GetComponent<Source.Segment>();
3536
segment = segmentSource.segment;
3637
}
3738

3839
protected override void OnDraw()
3940
{
40-
DrawRect(segment.bounds, boundsColor);
41-
DrawSegment(segment, lineColor, normals);
41+
if (segmentSource.coordinates == Source.Segment.Coordinates.World)
42+
{
43+
DrawRect(segment.bounds, boundsColor);
44+
DrawSegment(segment, lineColor, normals);
45+
}
46+
else
47+
{
48+
DrawRectWithTransform(segment.bounds, boundsColor, this.transform);
49+
DrawSegmentWithTransform(segment, lineColor, this.transform, normals);
50+
}
4251
}
4352
}
4453
}

Model/Polygon.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static Polygon PolygonWithPointList(List<Vector2> pointList)
6666

6767
public static Polygon PolygonWithSource(Source.Polygon polygonSource)
6868
{
69-
Polygon rootPolygon = Polygon.PolygonWithPointTransforms(polygonSource.points);
69+
Polygon rootPolygon = Polygon.PolygonWithPointTransforms(polygonSource.points, polygonSource.coordinates);
7070

7171
// Collect sub-olygons if any.
7272
foreach (Transform eachChildTransform in polygonSource.gameObject.transform)
@@ -102,14 +102,19 @@ public static Polygon PolygonWithPolygon(Polygon polygon)
102102
return rootPolygon;
103103
}
104104

105-
public static Polygon PolygonWithPointTransforms(Transform[] pointTransforms)
105+
public static Polygon PolygonWithPointTransforms(Transform[] pointTransforms, Source.Polygon.Coordinates coordinates)
106106
{
107107
// Create points array.
108108
Vector2[] points = new Vector2[pointTransforms.Length];
109109
for (int index = 0; index < pointTransforms.Length; index++)
110110
{
111111
Transform eachPointTransform = pointTransforms[index];
112-
points[index] = eachPointTransform.position.xy();
112+
113+
if (coordinates == Source.Polygon.Coordinates.World)
114+
{ points[index] = eachPointTransform.position.xy(); }
115+
116+
if (coordinates == Source.Polygon.Coordinates.Local)
117+
{ points[index] = eachPointTransform.localPosition.xy(); }
113118
}
114119

115120
return Polygon.PolygonWithPoints(points);
@@ -155,15 +160,20 @@ public Polygon(int pointCount = 1)
155160

156161
public void UpdatePointPositionsWithSource(Source.Polygon polygonSource) // Assuming unchanged point count
157162
{
158-
UpdatePointPositionsWithTransforms(polygonSource.points);
163+
UpdatePointPositionsWithTransforms(polygonSource.points, polygonSource.coordinates);
159164
}
160165

161-
public void UpdatePointPositionsWithTransforms(Transform[] pointTransforms) // Assuming unchanged point count
166+
public void UpdatePointPositionsWithTransforms(Transform[] pointTransforms, Source.Polygon.Coordinates coordinates) // Assuming unchanged point count
162167
{
163168
for (int index = 0; index < pointTransforms.Length; index++)
164169
{
165170
Transform eachPointTransform = pointTransforms[index];
166-
_points[index] = eachPointTransform.position.xy();
171+
172+
if (coordinates == Source.Polygon.Coordinates.World)
173+
{ _points[index] = eachPointTransform.position.xy(); }
174+
175+
if (coordinates == Source.Polygon.Coordinates.Local)
176+
{ _points[index] = eachPointTransform.localPosition.xy(); }
167177
}
168178

169179
// Polygon calculations.

Model/Segment.cs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,16 @@ public void CalculatePerpendicular()
8585

8686
public static Segment SegmentWithSource(Source.Segment segmentSource)
8787
{
88-
return Segment.SegmentWithPointTransforms(segmentSource.points);
88+
return Segment.SegmentWithPointTransforms(segmentSource.points, segmentSource.coordinates);
8989
}
9090

91-
public static Segment SegmentWithPointTransforms(Transform[] pointTransforms) // Uses Transform.localPosition.xy()
91+
public static Segment SegmentWithPointTransforms(Transform[] pointTransforms, Source.Segment.Coordinates coordinates = Source.Segment.Coordinates.World) // Uses Transform.localPosition.xy()
9292
{
93-
return Segment.SegmentWithPoints(pointTransforms[0].position, pointTransforms[1].position);
93+
if (coordinates == Source.Segment.Coordinates.World)
94+
{ return Segment.SegmentWithPoints(pointTransforms[0].position, pointTransforms[1].position); }
95+
96+
// Source.Segment.Coordinates.Local
97+
return Segment.SegmentWithPoints(pointTransforms[0].localPosition, pointTransforms[1].localPosition);
9498
}
9599

96100
public static Segment SegmentWithPoints(Vector2 a_, Vector2 b_)
@@ -108,13 +112,21 @@ public static Segment SegmentWithPoints(Vector2 a_, Vector2 b_)
108112

109113
public void UpdateWithSource(Source.Segment segmentSource) // Assuming unchanged point count
110114
{
111-
UpdateWithTransforms(segmentSource.points);
115+
UpdateWithTransforms(segmentSource.points, segmentSource.coordinates);
112116
}
113117

114-
public void UpdateWithTransforms(Transform[] pointTransforms) // Assuming unchanged point count
118+
public void UpdateWithTransforms(Transform[] pointTransforms, Source.Segment.Coordinates coordinates = Source.Segment.Coordinates.World) // Assuming unchanged point count
115119
{
116-
a = pointTransforms[0].position;
117-
b = pointTransforms[1].position;
120+
if (coordinates == Source.Segment.Coordinates.World)
121+
{
122+
a = pointTransforms[0].position;
123+
b = pointTransforms[1].position;
124+
return;
125+
}
126+
127+
// Source.Segment.Coordinates.Local
128+
a = pointTransforms[0].localPosition;
129+
b = pointTransforms[1].localPosition;
118130
}
119131

120132
#endregion
-400 Bytes
Binary file not shown.
Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,68 @@
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
//
88
using UnityEngine;
9+
using System.Collections;
910
using System.Collections.Generic;
1011

1112

12-
namespace EPPZ.Geometry.Scenes
13+
namespace EPPZ.Geometry.Source
1314
{
1415

1516

1617
using AddOns;
17-
using Model;
18-
using Lines;
1918

2019

21-
/// <summary>
22-
/// 11. Polygon triangulation
23-
/// </summary>
24-
public class Controller_11 : MonoBehaviour
20+
public class Mesh : MonoBehaviour
2521
{
2622

27-
public Source.Polygon star;
28-
public TriangulatorType triangulator;
29-
public Color color;
30-
public MeshFilter meshFilter;
23+
24+
public TriangulatorType triangulator = TriangulatorType.Dwyer;
25+
public Color color = Color.white;
26+
27+
public enum UpdateMode { Awake, Update, LateUpdate };
28+
public UpdateMode update = UpdateMode.Awake;
29+
30+
Source.Polygon polygonSource;
31+
Model.Polygon polygon;
32+
MeshFilter meshFilter;
33+
34+
35+
void Awake()
36+
{
37+
polygonSource = GetComponent<Source.Polygon>();
38+
meshFilter = GetComponent<MeshFilter>();
39+
40+
if (meshFilter == null)
41+
{
42+
Debug.LogWarning("No <b>MeshFilter</b> component on \""+name+"\" (for <b>PolygonMesh</b> to use as output). Disabled <i>GameObject</i>.");
43+
gameObject.SetActive(false);
44+
}
45+
46+
if (polygonSource != null)
47+
{ polygon = polygonSource.polygon; }
48+
49+
if (update == UpdateMode.Awake)
50+
{ CreateMesh(); }
51+
}
3152

3253
void Update()
3354
{
34-
meshFilter.mesh = star.polygon.Mesh(color, triangulator);
55+
if (update == UpdateMode.Update)
56+
{ CreateMesh(); }
57+
}
58+
59+
void LateUpdate()
60+
{
61+
if (update == UpdateMode.LateUpdate)
62+
{ CreateMesh(); }
63+
}
64+
65+
void CreateMesh()
66+
{
67+
if (polygonSource != null)
68+
{ polygon = polygonSource.polygon; }
69+
70+
meshFilter.mesh = polygon.Mesh(color, triangulator);
3571
}
3672
}
37-
}
73+
}

Source/Polygon.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ public class Polygon : MonoBehaviour
2424
public float offset = 0.0f;
2525

2626
public enum UpdateMode { Awake, Update, LateUpdate };
27-
public UpdateMode update = UpdateMode.Awake;
27+
public UpdateMode update = UpdateMode.Awake;
28+
29+
public enum Coordinates { World, Local }
30+
public Coordinates coordinates = Coordinates.World;
2831

2932
Model.Polygon _polygon;
3033
Model.Polygon _offsetPolygon;

0 commit comments

Comments
 (0)