diff --git a/Assets/Scripts/Editor/GeodesicMesh.cs b/Assets/Scripts/Editor/GeodesicMesh.cs index fc3f5d8..d6b74e3 100644 --- a/Assets/Scripts/Editor/GeodesicMesh.cs +++ b/Assets/Scripts/Editor/GeodesicMesh.cs @@ -7,26 +7,26 @@ namespace Metamesh { public class PrimaryIsoTriangle { - public int m; - public int n; - public List cartesian = new List(); - public List vertices = new List(); - public List max = new List(); - public List min = new List(); + private int m; + private int n; + private List cartesian = new List(); + private List vertices = new List(); + private List max = new List(); + private List min = new List(); public Dictionary vecToidx = new Dictionary(); - public Dictionary> vertByDist = new Dictionary>(); + private Dictionary> vertByDist = new Dictionary>(); public Dictionary> closestTo = new Dictionary>(); public List> innerFacets = new List>(); - public List> isoVecsABOB = new List>(); - public List> isoVecsOBOA = new List>(); - public List> isoVecsBAOA = new List>(); + public List> isoVecsABOB = new List>(); + public List> isoVecsOBOA = new List>(); + public List> isoVecsBAOA = new List>(); public List> vertexTypes = new List>(); - public float coau; - public float cobu; - public float coav; - public float cobv; + private float coau; + private float cobu; + private float coav; + private float cobv; //float PHI = (1 + Mathf.Sqrt(5f)) / 2; const float PHI = 1.618034f; @@ -133,7 +133,7 @@ namespace Metamesh var closestTo = new Dictionary>(); var vDist = this.vertByDist; - System.Action matchIdx = (f, fr, isoId, isoIdR) => + void MatchIdx(int f, int fr, string isoId, string isoIdR) { idx = f + "|" + isoId; idxR = fr + "|" + isoIdR; @@ -143,11 +143,11 @@ namespace Metamesh vecToidx[idxR] = indexCount; indexCount++; } - else if (vecToidx.ContainsKey(idx) && !(vecToidx.ContainsKey(idxR))) + else if (vecToidx.ContainsKey(idx) && !vecToidx.ContainsKey(idxR)) { vecToidx[idxR] = vecToidx[idx]; } - else if (vecToidx.ContainsKey(idxR) && !(vecToidx.ContainsKey(idx))) + else if (vecToidx.ContainsKey(idxR) && !vecToidx.ContainsKey(idx)) { vecToidx[idx] = vecToidx[idxR]; } @@ -160,7 +160,7 @@ namespace Metamesh { closestTo[vecToidx[idx]] = new List { verts[vDist[isoId][0]], vDist[isoId][1], vecToidx[idx] }; } - }; + } this.IDATA.edgematch = new List<(int, string, int, string)> @@ -234,7 +234,7 @@ namespace Metamesh obVec.y = i * (m1 + n1); isoId = abVec.x + "|" + abVec.y; isoIdR = obVec.x + "|" + obVec.y; - matchIdx(f, fr, isoId, isoIdR); + MatchIdx(f, fr, isoId, isoIdR); } } @@ -248,7 +248,7 @@ namespace Metamesh oaVec.y = i * n1; isoId = obVec.x + "|" + obVec.y; isoIdR = oaVec.x + "|" + oaVec.y; - matchIdx(f, fr, isoId, isoIdR); + MatchIdx(f, fr, isoId, isoIdR); } } @@ -264,7 +264,7 @@ namespace Metamesh abVec.y = n + (g - i) * m1; //reversed for BA isoId = oaVec.x + "|" + oaVec.y; isoIdR = abVec.x + "|" + abVec.y; - matchIdx(f, fr, isoId, isoIdR); + MatchIdx(f, fr, isoId, isoIdR); } } @@ -335,24 +335,24 @@ namespace Metamesh var m = this.m; var n = this.n; - var B = new IsoVector(-n, m + n); + var B = new Vector2Int(-n, m + n); for (var y = 1; y < m + n; y++) { - var point = new IsoVector(this.min[y], y); - var prev = new IsoVector(this.min[y - 1], y - 1); - var next = new IsoVector(this.min[y + 1], y + 1); - var pointR = point.Clone(); - var prevR = prev.Clone(); - var nextR = next.Clone(); + var point = new Vector2Int(this.min[y], y); + var prev = new Vector2Int(this.min[y - 1], y - 1); + var next = new Vector2Int(this.min[y + 1], y + 1); + var pointR = point; + var prevR = prev; + var nextR = next; - pointR.Rotate60About(B); - prevR.Rotate60About(B); - nextR.Rotate60About(B); + pointR = pointR.Rotate60About(B); + prevR = prevR.Rotate60About(B); + nextR = nextR.Rotate60About(B); - var maxPoint = new IsoVector(this.max[pointR.y], pointR.y); - var maxPrev = new IsoVector(this.max[pointR.y - 1], pointR.y - 1); - var maxLeftPrev = new IsoVector(this.max[pointR.y - 1] - 1, pointR.y - 1); + var maxPoint = new Vector2Int(this.max[pointR.y], pointR.y); + var maxPrev = new Vector2Int(this.max[pointR.y - 1], pointR.y - 1); + var maxLeftPrev = new Vector2Int(this.max[pointR.y - 1] - 1, pointR.y - 1); if (pointR.x != maxPoint.x || pointR.y != maxPoint.y) { @@ -361,30 +361,30 @@ namespace Metamesh // type2 //up this.vertexTypes.Add(new List { 1, 0, 0 }); - this.isoVecsABOB.Add(new List { point, maxPrev, maxLeftPrev }); + this.isoVecsABOB.Add(new List { point, maxPrev, maxLeftPrev }); //down this.vertexTypes.Add(new List { 1, 0, 0 }); - this.isoVecsABOB.Add(new List { point, maxLeftPrev, maxPoint }); + this.isoVecsABOB.Add(new List { point, maxLeftPrev, maxPoint }); } else if (pointR.y == nextR.y) { // type1 //up this.vertexTypes.Add(new List { 1, 1, 0 }); - this.isoVecsABOB.Add(new List { point, prev, maxPrev }); + this.isoVecsABOB.Add(new List { point, prev, maxPrev }); //down this.vertexTypes.Add(new List { 1, 0, 1 }); - this.isoVecsABOB.Add(new List { point, maxPrev, next }); + this.isoVecsABOB.Add(new List { point, maxPrev, next }); } else { // type 0 //up this.vertexTypes.Add(new List { 1, 1, 0 }); - this.isoVecsABOB.Add(new List { point, prev, maxPrev }); + this.isoVecsABOB.Add(new List { point, prev, maxPrev }); //down this.vertexTypes.Add(new List { 1, 0, 0 }); - this.isoVecsABOB.Add(new List { point, maxPrev, maxPoint }); + this.isoVecsABOB.Add(new List { point, maxPrev, maxPoint }); } } } @@ -392,20 +392,20 @@ namespace Metamesh public void MapABOBtoOBOA() { - var point = new IsoVector(0, 0); + var point = new Vector2Int(0, 0); for (var i = 0; i < this.isoVecsABOB.Count; i++) { - var temp = new List(); + var temp = new List(); for (var j = 0; j < 3; j++) { point.x = this.isoVecsABOB[i][j].x; point.y = this.isoVecsABOB[i][j].y; if (this.vertexTypes[i][j] == 0) { - point.RotateNeg120(this.m, this.n); + point = point.RotateNeg120(this.m, this.n); } - temp.Add(point.Clone()); + temp.Add(point); } this.isoVecsOBOA.Add(temp); @@ -414,20 +414,20 @@ namespace Metamesh public void MapABOBtoBAOA() { - var point = new IsoVector(0, 0); + var point = new Vector2Int(0, 0); for (var i = 0; i < this.isoVecsABOB.Count; i++) { - var temp = new List(); + var temp = new List(); for (var j = 0; j < 3; j++) { point.x = this.isoVecsABOB[i][j].x; point.y = this.isoVecsABOB[i][j].y; if (this.vertexTypes[i][j] == 1) { - point.Rotate120(this.m, this.n); + point = point.Rotate120(this.m, this.n); } - temp.Add(point.Clone()); + temp.Add(point); } this.isoVecsBAOA.Add(temp); @@ -454,13 +454,13 @@ namespace Metamesh var x = OA * this.coau + OB * this.cobu; var y = OA * this.coav + OB * this.cobv; - var mapped = new Dictionary>(); + //var mapped = new Dictionary(); string idx; for (var i = 0; i < this.cartesian.Count; i++) { var tempVec = x * this.cartesian[i].x + y * this.cartesian[i].y + O; - mapped[i] = new List { tempVec.x, tempVec.y, tempVec.z }; + //mapped[i] = tempVec; idx = faceNb + "|" + this.vertices[i].x + "|" + this.vertices[i].y; while (this.vecToidx[idx] >= geodesicData.vertex.Count) geodesicData.vertex.Add(Vector3.zero); @@ -470,19 +470,19 @@ namespace Metamesh public PrimaryIsoTriangle Build(int m, int n) { - var vertices = new List(); + var vertices = new List(); - var O = IsoVector.Zero(); - var A = new IsoVector(m, n); - var B = new IsoVector(-n, m + n); - vertices.AddRange(new IsoVector[] { O, A, B }); + var O = Vector2Int.zero; + var A = new Vector2Int(m, n); + var B = new Vector2Int(-n, m + n); + vertices.AddRange(new Vector2Int[] { O, A, B }); //max internal isoceles triangle vertices for (var iy = n; iy < m + 1; iy++) { for (var ix = 0; ix < m + 1 - iy; ix++) { - vertices.Add(new IsoVector(ix, iy)); + vertices.Add(new Vector2Int(ix, iy)); } } @@ -495,9 +495,9 @@ namespace Metamesh for (var i = 1; i < g; i++) { - vertices.Add(new IsoVector(i * m1, i * n1)); //OA - vertices.Add(new IsoVector(-i * n1, i * (m1 + n1))); //OB - vertices.Add(new IsoVector(m - i * (m1 + n1), n + i * m1)); // AB + vertices.Add(new Vector2Int(i * m1, i * n1)); //OA + vertices.Add(new Vector2Int(-i * n1, i * (m1 + n1))); //OB + vertices.Add(new Vector2Int(m - i * (m1 + n1), n + i * m1)); // AB } //lower rows vertices and their rotations @@ -506,9 +506,9 @@ namespace Metamesh { for (var ix = 0; ix < iy * ratio; ix++) { - vertices.Add(new IsoVector(ix, iy)); - vertices.Add(new IsoVector(ix, iy).Rotate120(m, n)); - vertices.Add(new IsoVector(ix, iy).RotateNeg120(m, n)); + vertices.Add(new Vector2Int(ix, iy)); + vertices.Add(new Vector2Int(ix, iy).Rotate120(m, n)); + vertices.Add(new Vector2Int(ix, iy).RotateNeg120(m, n)); } } } @@ -534,17 +534,17 @@ namespace Metamesh } //calculates the distance of a vertex from a given primary vertex - Func distFrom = (IsoVector vert, string primVert) => + int DistFrom(Vector2Int vert, string primVert) { - var v = vert.Clone(); + var v = vert; if (primVert == "A") { - v.RotateNeg120(m, n); + v = v.RotateNeg120(m, n); } if (primVert == "B") { - v.Rotate120(m, n); + v = v.Rotate120(m, n); } if (v.x < 0) @@ -553,7 +553,7 @@ namespace Metamesh } return v.x + v.y; - }; + } var cartesian = Enumerable.Repeat(Vector3.zero, len).ToList(); var distFromO = Enumerable.Repeat(0, len).ToArray(); @@ -566,10 +566,10 @@ namespace Metamesh var dist = -1; for (var i = 0; i < len; i++) { - cartesian[i] = vertices[i].ToCartesianOrigin(new IsoVector(0, 0), 0.5f); - distFromO[i] = distFrom(vertices[i], "O"); - distFromA[i] = distFrom(vertices[i], "A"); - distFromB[i] = distFrom(vertices[i], "B"); + cartesian[i] = vertices[i].ToCartesianOrigin(new Vector2Int(0, 0), 0.5f); + distFromO[i] = DistFrom(vertices[i], "O"); + distFromA[i] = DistFrom(vertices[i], "A"); + distFromB[i] = DistFrom(vertices[i], "B"); if (distFromO[i] == distFromA[i] && distFromA[i] == distFromB[i]) { diff --git a/Assets/Scripts/Extensions.cs b/Assets/Scripts/Extensions.cs new file mode 100644 index 0000000..3364cea --- /dev/null +++ b/Assets/Scripts/Extensions.cs @@ -0,0 +1,44 @@ +using UnityEngine; + +public static class Vector2IntExtensions +{ + public static Vector2Int Rotate60About(this Vector2Int vector, Vector2Int other) + { + var x = other.x + other.y - vector.y; + var y = vector.x + vector.y - other.x; + + return new Vector2Int(x, y); + } + + public static Vector2Int RotateNeg60About(this Vector2Int vector, Vector2Int other) + { + var x = vector.x + vector.y - other.x; + var y = other.x + other.y - vector.y; + + return new Vector2Int(x, y); + } + + public static Vector2Int Rotate120(this Vector2Int vector, int m, int n) + { + var x = m - vector.x - vector.y; + var y = n + vector.x; + + return new Vector2Int(x, y); + } + + public static Vector2Int RotateNeg120(this Vector2Int vector, int m, int n) + { + var x = vector.y - n; + var y = m + n - vector.x - vector.y; + + return new Vector2Int(x, y); + } + + public static Vector3 ToCartesianOrigin(this Vector2Int vector, Vector2Int origin, float isoGridSize) + { + Vector3 point = Vector3.zero; + point.x = origin.x + 2 * vector.x * isoGridSize + vector.y * isoGridSize; + point.y = origin.y + Mathf.Sqrt(3) * vector.y * isoGridSize; + return point; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Extensions.cs.meta b/Assets/Scripts/Extensions.cs.meta new file mode 100644 index 0000000..04380e9 --- /dev/null +++ b/Assets/Scripts/Extensions.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 06cb1f7cb5c9b794497584417b3aa86e \ No newline at end of file