Skip to content

Instantly share code, notes, and snippets.

@johnfredcee
Last active December 31, 2018 09:43
Show Gist options
  • Save johnfredcee/58f3552dee49eb054366c3142e8c3947 to your computer and use it in GitHub Desktop.
Save johnfredcee/58f3552dee49eb054366c3142e8c3947 to your computer and use it in GitHub Desktop.
Naive mesh representation
import euclid
from euclid import Vector3
# naive first attempt at a mesh representation
# a straight list of vertices
class Mesh:
def __init__(self, vertices=[]):
"""Initialises a vertex with a list of faces. One 3-tuple of vertices per face."""
self.vertex_list = vertices
def __len__(self):
"""Returns the number of vertice in a mesh"""
return len(self.vertex_list)
def vertices(self):
"""Iterate over the vertices in the mesh"""
for v in [item for sublist in self.vertices for item in sublist]:
yield v
def faces(self):
"""Iterate over the faces in the mesh"""
face_iter = iter(self.vertex_list)
for face in face_iter:
yield face
def __str__(self):
"""Print a readable description of the mesh"""
result = "Mesh with {0} vertices and {1} faces\n".format(
len(self.vertex_list), len(self.vertex_list) / 3)
for face_index, face in enumerate(self.faces()):
a, b, c = face[0], face[1], face[2]
result += "Face {0} :({1}, {2}, {3})\n".format(face_index, a, b, c)
return result
if __name__ == "__main__":
cube_vertex = [
# face vertices
Vector3(-1.0, -1.0, -1.0),
Vector3(-1.0, 1.0, -1.0),
Vector3(1.0, 1.0, -1.0),
Vector3(1.0, -1.0, -1.0),
Vector3(-1.0, -1.0, 1.0),
Vector3(-1.0, 1.0, 1.0),
Vector3(1.0, 1.0, 1.0),
Vector3(1.0, -1.0, 1.0),
]
cube_indices = [
# Front face
0, 1, 2,
0, 2, 3,
# Back face
4, 6, 5,
4, 7, 6,
# Left face
4, 5, 1,
4, 1, 0,
# Right face
3, 2, 6,
3, 6, 7,
# Top face
1, 5, 6,
1, 6, 2,
# Bottom face
4, 0, 3,
4, 3, 7
]
cube_face_iter = iter(cube_indices)
cube_vertex_list = []
for (ia, ib, ic) in zip(cube_face_iter, cube_face_iter, cube_face_iter):
cube_face = (cube_vertex[ia], cube_vertex[ib], cube_vertex[ic])
cube_vertex_list.append(cube_face)
cube_mesh = Mesh(vertices = cube_vertex_list)
print(cube_mesh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment