Skip to content

Instantly share code, notes, and snippets.

@Dan-Piker
Created March 18, 2023 22:16
Show Gist options
  • Save Dan-Piker/f46bce6ae129b582f6bdedb7f7e86a40 to your computer and use it in GitHub Desktop.
Save Dan-Piker/f46bce6ae129b582f6bdedb7f7e86a40 to your computer and use it in GitHub Desktop.
sqrt7 subdivision
KPlanktonMesh ma = mesh.ToKPlanktonMesh();
double[] weightsA = new double[3]{4 / 7f,2 / 7f,1 / 7f};
double[] weightsB = new double[3]{4 / 7f,1 / 7f,2 / 7f};
for(int k = 0;k < level;k++)
{
KPlanktonMesh mb = new KPlanktonMesh();
var hes = ma.Halfedges;
for(int i = 0;i < hes.Count;i++)
{
var next = hes[hes[i].NextHalfedge];
var nextnext = hes[next.NextHalfedge];
Point3d[] pts = new Point3d[3]{
ma.Vertices[hes[i].StartVertex].ToPoint3d(),
ma.Vertices[next.StartVertex].ToPoint3d(),
ma.Vertices[nextnext.StartVertex].ToPoint3d()};
Point3d p = new Point3d();
for(int j = 0;j < 3;j++) p += pts[j] * ((alternating && k % 2 == 0) ? weightsB[j] : weightsA[j]);
mb.Vertices.Add(p);
}
for(int i = 0;i < ma.Vertices.Count;i++)
{
Point3d p = ma.Vertices[i].ToPoint3d();
int[] neighbours = ma.Vertices.GetVertexNeighbours(i);
int n = neighbours.Length;
Point3d a = new Point3d();
for(int j = 0;j < n;j++) a += ma.Vertices[neighbours[j]].ToPoint3d();
double w = (16.0 - 4 * Math.Cos(2 * Math.PI / n)) / 49.0;
int v = mb.Vertices.Add((1 - w) * p + w * a / n);
int[] h = ma.Vertices.GetHalfedges(i);
for(int j = 0;j < h.Length;j++) mb.Faces.AddFace(h[j], v, h[(j + 1) % h.Length]);
}
for(int i = 0;i < hes.Count;i++)
mb.Faces.AddFace(i,
(alternating && k % 2 == 0) ? ma.Halfedges[ma.Halfedges.GetPairHalfedge(i)].NextHalfedge : ma.Halfedges.GetPairHalfedge(i),
ma.Halfedges[i].NextHalfedge);
for(int i = 0;i < ma.Faces.Count;i++)
{
int[] h = ma.Faces.GetHalfedges(i);
mb.Faces.AddFace(h[0], h[1], h[2]);
}
ma = mb;
}
A = ma.ToRhinoMesh();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment