Created
December 29, 2017 17:21
-
-
Save zeffii/3be91a897a7591aa7937e84f0fc0a396 to your computer and use it in GitHub Desktop.
polyhedron2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from math import sqrt | |
class PolyMesh(): | |
def __init__(self, vertices, faces): | |
self.vertices = vertices | |
self.faces = faces | |
class PolyhedraMaker(): | |
def __init__(self): | |
from math import sqrt | |
polyhedra = [] | |
# Tetrahedron | |
polyhedra.append(PolyMesh( | |
[(1, 1, 1), (1, -1, -1), (-1, 1, -1), (-1, -1, 1)], | |
[[0, 3, 1], [0, 2, 3], [1, 3, 2], [0, 1, 2]])) | |
# Cube | |
polyhedra.append(PolyMesh( | |
[(i, j, k) for i in [1, -1] for j in [1, -1] for k in [1, -1]], | |
[[0, 2, 3, 1], [2, 6, 7, 3], [4, 5, 7, 6], | |
[0, 1, 5, 4], [0, 4, 6, 2], [1, 3, 7, 5]])) | |
# Octahedron | |
polyhedra.append(PolyMesh( | |
[(1, 0, 0), (-1, 0, 0), (0, 1, 0), (0, -1, 0), (0, 0, 1), (0, 0, -1)], | |
[[0, 3, 5], [0, 5, 2], [2, 5, 1], [1, 5, 3], | |
[0, 4, 3], [0, 2, 4], [1, 4, 2], [1, 3, 4]])) | |
# Dodecahedron | |
polyhedra.append(PolyMesh( | |
[k for row in | |
[[(0, i, j), (j, 0, i), (i, j, 0)] | |
for i in [(sqrt(5)-1)/2, -(sqrt(5)-1)/2] | |
for j in [(sqrt(5)+1)/2, -(sqrt(5)+1)/2]] for k in row] + | |
[(i, j, k) for i in [1, -1] for j in [1, -1] for k in [1, -1]], | |
[[0, 6, 14, 1, 12], [6, 0, 16, 4, 18], [6, 18, 11, 5, 14], | |
[0, 12, 2, 8, 16], [4, 16, 8, 17, 10], [12, 1, 7, 13, 2], | |
[18, 4, 10, 19, 11], [1, 14, 5, 15, 7], [2, 13, 3, 17, 8], | |
[5, 11, 19, 9, 15], [3, 9, 19, 10, 17], [3, 13, 7, 15, 9]])) | |
# Icosahedron | |
polyhedra.append(PolyMesh( | |
[k for row in [[(0, i, j), (j, 0, i), (i, j, 0)] | |
for i in [(sqrt(5)+1)/2, -(sqrt(5)+1)/2] | |
for j in [1, -1]] for k in row], | |
[[0, 1, 2], [0, 2, 3], [0, 3, 8], [0, 8, 4], [0, 4, 1], | |
[1, 4, 6], [1, 6, 5], [1, 5, 2], [2, 5, 7], [2, 7, 3], | |
[3, 7, 10], [3, 10, 8], [8, 10, 11], [8, 11, 4], [4, 11, 6], | |
[9, 5, 6], [9, 6, 11], [9, 11, 10], [9, 10, 7], [9, 7, 5]])) | |
self.polyhedra = polyhedra | |
def get_mesh(self, index): | |
poly = index % len(self.polyhedra) | |
vertices = self.polyhedra[poly].vertices | |
faces = self.polyhedra[poly].faces | |
return vertices, faces | |
def sv_main(polyIndex=0): | |
in_sockets = [ | |
['s', 'Polyhedra Index', polyIndex]] | |
polyhedron = PolyhedraMaker() | |
vertices, faces = polyhedron.get_mesh(polyIndex) | |
out_sockets = [ | |
['v', 'Verts', [vertices]], | |
['s', 'Faces', [faces]]] | |
return in_sockets, out_sockets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment