Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Dec 29, 2023
2 parents 01a3c16 + 0c56d1c commit 5f494e0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,67 @@ public void SplitEdge(int faceIndex, int edgeIndex)
SplitEdge(face.GetHalfedges()[edgeIndex % face.Sides]);
}


private PolyMesh SubdivideEdges(OpParams o)
{
int subdivisions = (int)o.GetValueA(this, 0);
subdivisions = subdivisions < 1 ? 1 : subdivisions;
subdivisions = subdivisions > 64 ? 64 : subdivisions;

var faceIndices = new List<int[]>();
var vertexPoints = new List<Vector3>();
var existingVertices = new Dictionary<Vertex, int>();
var newEdgeVertices = new Dictionary<(Guid, Guid)?, int[]>();
var vertexRoles = new List<Roles>();

for (var i = 0; i < Vertices.Count; i++)
{
var vert = Vertices[i];
vertexPoints.Add(vert.Position);
vertexRoles.Add(Roles.Existing);
existingVertices[vert] = i;
}

int vertexIndex = vertexPoints.Count;

for (int faceIndex = 0; faceIndex < Faces.Count; faceIndex++)
{
var face = Faces[faceIndex];
var firstEdge = face.Halfedge;
Halfedge edge = null;
var newFace = new List<int>();
while (edge != firstEdge)
{
edge ??= firstEdge;
bool hasPair = edge.Pair != null && newEdgeVertices.ContainsKey(edge.Pair.Name);
if (!newEdgeVertices.ContainsKey(edge.Name) && !hasPair)
{
newEdgeVertices[edge.Name] = new int[subdivisions];
for (int i = 0; i < subdivisions; i++)
{
vertexPoints.Add(edge.PointAlongEdge((1f / (subdivisions + 1)) * (i + 1)));
vertexRoles.Add(Roles.NewAlt);
newEdgeVertices[edge.Name][i] = vertexIndex++;
}
}
newFace.Add(existingVertices[edge.Vertex]);
if (!newEdgeVertices.ContainsKey(edge.Name) && newEdgeVertices.ContainsKey(edge.Pair.Name))
{
var verts = newEdgeVertices[edge.Pair.Name];
newFace.AddRange(verts.Reverse());
}
else
{
var verts = newEdgeVertices[edge.Name];
newFace.AddRange(verts);
}
edge = edge.Prev;
}
faceIndices.Add(newFace.ToArray());
}

return new PolyMesh(vertexPoints, faceIndices, FaceRoles, vertexRoles, FaceTags);
}
public void SplitEdges(IEnumerable<Halfedge> edgesToSplit)
{
foreach (var edge in edgesToSplit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,7 @@ public enum Operation

// Topology Manipulation

SubdivideEdges = 109,
FillHoles = 60,
// ExtendBoundaries = 61,
// ConnectFaces = 80,
Expand Down Expand Up @@ -1631,6 +1632,9 @@ public PolyMesh AppyOperation(Operation op, OpParams p)

// Topology Manipulation

case Operation.SubdivideEdges:
polyMesh = polyMesh.SubdivideEdges(p);
break;
case Operation.FillHoles:
polyMesh = polyMesh.FillHoles();
break;
Expand Down

0 comments on commit 5f494e0

Please sign in to comment.