Skip to content

Commit

Permalink
Laves tiles and tweaks to debug vertex logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Feb 19, 2024
1 parent 0ce1912 commit 8486f1a
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 29 deletions.
12 changes: 4 additions & 8 deletions Assets/Scripts/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public class TestBase : MonoBehaviour

public void Build(ColorMethods colorMethod = ColorMethods.ByRole)
{
var debugVerts = poly.DebugVerts?.ToList();
var _random = new Random();
for (int i1 = 0; i1 < Op1Iterations; i1++)
{
Expand Down Expand Up @@ -86,7 +85,6 @@ public void Build(ColorMethods colorMethod = ColorMethods.ByRole)
}

ModifyPostOp();
// debugVerts = poly.Vertices.Select(v => v.Position).ToList();

if (SpherizeAmount > 0) poly.Spherize(new OpParams(SpherizeAmount));
if (FastConicalize)
Expand All @@ -110,10 +108,7 @@ public void Build(ColorMethods colorMethod = ColorMethods.ByRole)

var meshData = poly.BuildMeshData(colorMethod: colorMethod);
var mesh = poly.BuildUnityMesh(meshData);
if (poly.DebugVerts == null) poly.DebugVerts = new List<Vector3>();
if (debugVerts != null) poly.DebugVerts.AddRange(debugVerts);
debugVerts = poly.Vertices.Select(v => v.Position).ToList();
poly.DebugVerts = debugVerts;

if (Application.isPlaying)
{
gameObject.GetComponent<MeshFilter>().mesh = mesh;
Expand All @@ -136,7 +131,8 @@ private void OnValidate()

private void OnDrawGizmos()
{
if (poly==null || poly.DebugVerts == null) return;
if (poly==null) return;

if (debugFaces)
{
for (var f = 0; f < poly.Faces.Count; f++)
Expand All @@ -157,7 +153,7 @@ private void OnDrawGizmos()
}
}

if (debugVerts)
if (debugVerts && poly.DebugVerts != null)
{
for (int i = 0; i < poly.DebugVerts.Count; i++)
{
Expand Down
166 changes: 161 additions & 5 deletions Packages/Polyhydra/com.ixxy.polyhydra/Core/Runtime/Generators/Grids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ public enum GridShapes

public enum GridTypes
{
// Regular
Triangular = 0,
Square = 1,
Hexagonal = 2,

// Archimedean, uniform or semiregular
SnubTrihexagonal = 3,
ElongatedTriangular = 4, // 3.3.3.4.4
SnubSquare = 5, // 3.3.4.3.4
Expand All @@ -32,9 +34,21 @@ public enum GridTypes
TruncatedTrihexagonal = 9, // 4.6.12
TruncatedSquare = 10, // 4.8.8

// Laves or Catalan
TetrakisSquare = 34, // Dual of TruncatedSquare
CairoPentagonal = 35, // Dual of SnubSquare
Rhombille = 36, // Dual of Trihexagonal
TriakisTriangular = 37, // Dual of TruncatedHexagonal
DeltoidalTrihexagonal = 38, // Dual of Rhombitrihexagonal
Kisrhombille = 39, // Dual of TruncatedTrihexagonal
FloretPentagonal = 40, // Dual of SnubTrihexagonal
PrismaticPentagonal = 41, // Dual of ElongatedTriangular

// Durer
Durer1 = 16,
Durer2 = 17,

// 2-Uniform
DissectedRhombitrihexagonal = 18, // 3.3.3.3.3.3;3.3.4.3.4
DissectedTruncatedHexagonal1 = 19, // 3.4.6.4;3.3.4.3.4
DissectedTruncatedHexagonal2 = 20, // 3.4.6.4;3.3.3.4.4
Expand Down Expand Up @@ -234,11 +248,12 @@ public static PolyMesh BuildGridFromTileDef(TileDef tiledef)
}
}
poly.Append(tiledef.tile, tileOffset);
newFaceRoles.AddRange(
tiledef.roleSet[
y % roleCountY] [(x + colOffset) % roleCountX
].GetRange(rowOffset, tileCount)
);

int rowRoleIndex = (y % roleCountY) % tiledef.roleSet.Count;
var rowRoles = tiledef.roleSet[rowRoleIndex];
int colRoleIndex = ((x + colOffset) % roleCountX) % rowRoles.Count;
var roles = rowRoles[colRoleIndex].GetRange(rowOffset, tileCount);
newFaceRoles.AddRange(roles);
}
}

Expand All @@ -261,6 +276,8 @@ public static PolyMesh BuildGridFromTileDef(TileDef tiledef)
poly = poly.Weld(0.01f);
break;
}

poly.DebugVerts = tiledef.tile.DebugVerts;
return poly;
}

Expand Down Expand Up @@ -666,6 +683,143 @@ public static PolyMesh Build(GridEnums.GridTypes type, GridEnums.GridShapes grid
}
};
break;

case GridEnums.GridTypes.TetrakisSquare:
tile = Shapes.Polygon(4);
tile = tile.Rotate(Vector3.up, 45);
tile = tile.Kis(new OpParams());
xOffset = tile.Vertices[0].Position - tile.Vertices[1].Position;
yOffset = tile.Vertices[1].Position - tile.Vertices[2].Position;
roleSet = new List<List<List<Roles>>>
{
new List<List<Roles>>
{
new List<Roles>
{
Roles.ExistingAlt,
Roles.NewAlt,
Roles.ExistingAlt,
Roles.NewAlt,
}
}
};
break;
case GridEnums.GridTypes.CairoPentagonal:
var sqrt3 = Mathf.Sqrt(3);
float halfBaseLength = (sqrt3 - 1) / 2;
var points = new List<Vector3>
{
new (-halfBaseLength, 0, 0),
new (halfBaseLength, 0, 0),
new (0.5f + halfBaseLength,0, sqrt3 / 2),
new (0, 0, (sqrt3 / 2) + 0.5f),
new (-(0.5f + halfBaseLength), 0, sqrt3 / 2)
};
var oneTile = new PolyMesh(points);
tile = oneTile.Duplicate();
tile.Append(new PolyMesh(points.Select(p => new Vector3(p.x, 0, -p.z))));
oneTile.Transform(new Vector3(0, 0, -(halfBaseLength + (sqrt3 / 2) + 0.5f)));
tile.Append(oneTile.Rotate(Vector3.up, 90));
tile.Append(oneTile.Rotate(Vector3.up, -90));
xOffset = tile.Vertices[15].Position - tile.Vertices[11].Position;
yOffset = tile.Vertices[16].Position - tile.Vertices[8].Position;
roleSet = new List<List<List<Roles>>>
{
new()
{
new()
{
Roles.New,
Roles.NewAlt,
Roles.Existing,
Roles.ExistingAlt,
}
}
};
break;

case GridEnums.GridTypes.Rhombille:
// Create a rhombus by joining two triangles
var rhomb = Shapes.Polygon(3);
rhomb.Transform(Vector3.zero, new Vector3(0, 30, 0));
rhomb.ExtendFace(0, 0, 3);
var rhombusVerts = rhomb.Vertices.Select(v => v.Position).ToList();
rhomb = new PolyMesh(rhombusVerts);

// Move it so v0 is on the origin
rhomb.Transform(-rhomb.Vertices[0].Position);

// Create a tile by joining three rhombuses
tile = rhomb.Duplicate();
rhomb.Transform(Vector3.zero, new Vector3(0, 120, 0));
tile.Append(rhomb);
rhomb.Transform(Vector3.zero, new Vector3(0, 120, 0));
tile.Append(rhomb);

xOffset = tile.Vertices[2].Position - tile.Vertices[6].Position;
yOffset = tile.Vertices[10].Position - tile.Vertices[6].Position;
roleSet = new List<List<List<Roles>>>
{
new()
{
new()
{
Roles.Existing,
Roles.ExistingAlt,
Roles.New
}
}
};
offsetAlternateRows = false;
break;

case GridEnums.GridTypes.TriakisTriangular:
break;

case GridEnums.GridTypes.DeltoidalTrihexagonal:
break;

case GridEnums.GridTypes.Kisrhombille:
break;

case GridEnums.GridTypes.FloretPentagonal:
break;

case GridEnums.GridTypes.PrismaticPentagonal:
halfBaseLength = (Mathf.Sqrt(3) - 1) / 2;
points = new List<Vector3>
{
new (-0.5f, 0, 0),
new (0.5f, 0, 0),
new (0.5f,0, halfBaseLength * 2),
new (0, 0, halfBaseLength * 3),
new (-0.5f, 0, halfBaseLength* 2)
};
var pentile = new PolyMesh(points);
tile = pentile.Duplicate();
tile.Append(new PolyMesh(points.Select(p => new Vector3(p.x, 0, -p.z))));
xOffset = tile.Vertices[0].Position - tile.Vertices[1].Position;
yOffset = tile.Vertices[8].Position - tile.Vertices[2].Position;
tile.DebugVerts = tile.Vertices.Select(v => v.Position).ToList();
roleSet = new List<List<List<Roles>>>
{
new()
{
new()
{
Roles.NewAlt,
Roles.New,
},
new()
{
Roles.Existing,
Roles.ExistingAlt,
},
}
};
offsetAlternateRows = false;
break;

case GridEnums.GridTypes.Durer1:
tile = Shapes.Polygon(5);
tile = tile.Rotate(Vector3.up, 54);
Expand All @@ -686,6 +840,7 @@ public static PolyMesh Build(GridEnums.GridTypes type, GridEnums.GridShapes grid
}
};
break;

case GridEnums.GridTypes.Durer2:
tile = Shapes.Polygon(5);
tile = tile.Rotate(Vector3.up, 54);
Expand Down Expand Up @@ -1140,6 +1295,7 @@ public static PolyMesh ShapeWrap(PolyMesh grid, GridEnums.GridShapes gridShape,
vert.Position = newPos;
}
}

return grid;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ public void Transform(Matrix4x4 matrix)
public PolyMesh Rotate(Vector3 axis, float amount)
{
var copy = Duplicate();
if (amount == 0) return copy;
for (var i = 0; i < copy.Vertices.Count; i++)
{
var v = copy.Vertices[i];
Expand Down Expand Up @@ -1868,7 +1869,9 @@ public void Append(PolyMesh other, Matrix4x4 matrix)
public PolyMesh Duplicate()
{
// Export to face/vertex and rebuild
return new PolyMesh(ListVerticesByPoints(), ListFacesByVertexIndices(), FaceRoles, VertexRoles, FaceTags);
var poly = new PolyMesh(ListVerticesByPoints(), ListFacesByVertexIndices(), FaceRoles, VertexRoles, FaceTags);
poly.DebugVerts = DebugVerts?.ToList();
return poly;
}

public PolyMesh Duplicate(Matrix4x4 matrix)
Expand Down Expand Up @@ -3254,9 +3257,9 @@ public void AddRhombus(Halfedge edge, float angle)

var newVert1 = new Vertex(
Quaternion.AngleAxis(
-angle,
angle,
normal
) * (edge.Next.Vertex.Position - pivot) + pivot
) * (edge.Prev.Vertex.Position - pivot) + pivot
);
Vertices.Add(newVert1);
VertexRoles.Add(Roles.New);
Expand All @@ -3265,7 +3268,7 @@ public void AddRhombus(Halfedge edge, float angle)
var pivot2 = newVert1.Position;
var newVert2 = new Vertex(
Quaternion.AngleAxis(
-angle2,
angle2,
normal
) * (edge.Vertex.Position - pivot2) + pivot2
);
Expand All @@ -3274,10 +3277,10 @@ public void AddRhombus(Halfedge edge, float angle)

var verts = new List<Vertex>()
{
edge.Next.Vertex,
edge.Vertex,
newVert1,
edge.Prev.Vertex,
newVert2,
newVert1,
};
var result = Faces.Add(verts);
if (result)
Expand Down
26 changes: 16 additions & 10 deletions Packages/Polyhydra/com.ixxy.polyhydra/Core/Runtime/PolyMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,19 @@ public PolyMesh()
InitTags();
}

public PolyMesh(
IEnumerable<Vector2> verts
) : this()
public PolyMesh(IEnumerable<Vector3> verts) : this()
{
var faceIndices = new List<IEnumerable<int>>{Enumerable.Range(0, verts.Count())};
FaceRoles = Enumerable.Repeat(Roles.New, faceIndices.Count()).ToList();
FaceRoles = Enumerable.Repeat(Roles.New, faceIndices.Count).ToList();
FaceTags = Enumerable.Repeat(new HashSet<string>(), faceIndices.Count()).ToList();
VertexRoles = Enumerable.Repeat(Roles.New, verts.Count()).ToList();
InitIndexed(verts.Select(v => new Vector3(v.x, 0, v.y)), faceIndices);
InitIndexed(verts, faceIndices);
CullUnusedVertices();
}

public PolyMesh(IEnumerable<Vector2> verts) : this(verts.Select(v => new Vector3(v.x, 0, v.y)))
{ }

public PolyMesh(
IEnumerable<Vector3> verts,
IEnumerable<IEnumerable<int>> faceIndices
Expand Down Expand Up @@ -361,7 +362,7 @@ public static PolyMesh FromConwayString(string conwayString)

if (op==Operation.Canonicalize)
{
poly.Canonicalize(tokens[i].Item2, tokens[i].Item2);
poly.Canonicalize((int)tokens[i].Item2, (int)tokens[i].Item2);
}
else
{
Expand Down Expand Up @@ -454,8 +455,11 @@ public static PolyMesh GenerateConwaySeedShape(SeedShape seedShape, float param
break;
case SeedShape.Grid:
var typeString = paramInt.ToString();
var gridType = gridTypeMap[typeString];
poly = Grids.Build(gridType, GridEnums.GridShapes.Plane, 5, 5);
GridEnums.GridTypes gridType;
if (Enum.TryParse(typeString, out gridType))
{
poly = Grids.Build(gridType, GridEnums.GridShapes.Plane, 5, 5);
}
break;
}

Expand Down Expand Up @@ -666,6 +670,8 @@ public List<Halfedge> GetBoundaryLoop(Halfedge startHalfedge)

public void InitOctree()
{
// TODO Should we ignore non-boundary vertices?
// When would we ever weld those?
octree = new PointOctree<Vertex>(1, Vector3.zero, 1);
for (var i = 0; i < Vertices.Count; i++)
{
Expand Down Expand Up @@ -1681,8 +1687,8 @@ public PolyMesh AppyOperation(Operation op, OpParams p)
// TODO - iterations or tolerance?
// Mathf.FloorToInt(p.OriginalParamA),
// Mathf.FloorToInt(p.OriginalParamB)
p.OriginalParamA,
p.OriginalParamB
(int)p.OriginalParamA,
(int)p.OriginalParamB
);
break;
case Operation.Relax:
Expand Down

0 comments on commit 8486f1a

Please sign in to comment.