Skip to content

Commit bc21a83

Browse files
author
monkstone
committed
refactor from cube to box and mesh
1 parent 47a13f5 commit bc21a83

File tree

3 files changed

+146
-8
lines changed

3 files changed

+146
-8
lines changed

3DSketches/menger2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from pyprocessing import *
2-
from util.cube import Cube
2+
from util.box import Box
33

44
FOV = PI/3.0
55
angle = 0.0
66
ANGLE_STEP = PI / 180.0
77
menger = []
8-
9-
8+
109

1110
def setup():
1211
size(800,600)
@@ -23,9 +22,9 @@ def draw():
2322
angle = (angle + ANGLE_STEP) % TWO_PI
2423
rotateZ(angle)
2524
rotateY(angle)
26-
export_menger()
27-
#for cub in menger:
28-
# draw_cube(cub)
25+
#export_menger()
26+
for cub in menger:
27+
draw_cube(cub)
2928

3029

3130
def draw_cube(cube):
@@ -54,8 +53,8 @@ def create_menger(xx, yy, zz, sz):
5453
Create a recursive menger sponge using my_cube
5554
"""
5655
u = sz / 3.0
57-
if (sz < 10):
58-
menger.append(Cube(xx, yy, zz, sz))
56+
if (sz < 20):
57+
menger.append(Box.createAcube(xx, yy, zz, sz).toMesh())
5958
else:
6059
for i in xrange(-1, 2, 1):
6160
for j in xrange(-1, 2, 1):

3DSketches/util/box.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
box.py contains use lighweight Vec3D class to store indexed vector points
3+
by having public attributes (as does python default)
4+
"""
5+
from vec3d import Vec3D
6+
from mesh import Mesh
7+
8+
class Box(object):
9+
"""
10+
A lightweight convenience class to store box point data
11+
With a function to return indexed vector points
12+
"""
13+
14+
def __init__(self, x = 0, y = 0, z = 0, xd = 1, yd = 1, zd = 1):
15+
"""
16+
Initialise the new Box object
17+
"""
18+
self.xx= x
19+
self.yy = y
20+
self.zz = z
21+
self.xd = xd
22+
self.yd = yd
23+
self.zd = zd
24+
self.idx_array = [1, 0, 2, 3, 1, 2, 5, 4, 6, 7, 5, 6, 4, 5, 0, 1, 4, 0, 7, 6, 2, 2, 6, 3, 0, 5, 7, 2, 0, 7, 4, 1, 6, 6, 1, 3]
25+
self.normal_array = [0, 0, 1, 0, 0, -1, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0]
26+
def points(self):
27+
"""
28+
The unique corner points of box (front + back order)
29+
"""
30+
cpoints = []
31+
cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
32+
cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
33+
cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
34+
cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, -self.zd * 0.5 + self.zz) )
35+
cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
36+
cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, -self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
37+
cpoints.append( Vec3D(+self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
38+
cpoints.append( Vec3D(-self.xd * 0.5 + self.xx, +self.yd * 0.5 + self.yy, +self.zd * 0.5 + self.zz) )
39+
return cpoints
40+
41+
42+
def toMesh(self):
43+
return Mesh.meshFromVertices(self.points(), self.idx_array)
44+
45+
46+
@classmethod
47+
def boxFromMaxMin(clas, bmin, bmax):
48+
"""
49+
Alternative constructor creates an instance of from min and max Vec3D
50+
"""
51+
return Box((bmin.x + bmax.x/2.0), (bmin.y +bmax.y/2.0), (bmin.z + bmax.z/2.0), abs(bmin.x - bmax.x), abs(bmin.y - bmax.y), abs(bmin.z - bmax.z))
52+
53+
@classmethod
54+
def createAcube(clas, x, y, z, sz):
55+
"""
56+
Alternative constructor creates a special box, where side have equal length
57+
"""
58+
return Box(x, y, z, sz, sz, sz)

3DSketches/util/mesh.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""
2+
mesh.py contains use lighweight Vec3D class to store unique vector points
3+
and correspondindg mesh indices
4+
"""
5+
6+
class Mesh(object):
7+
"""
8+
A lightweight convenience class to store mesh point data
9+
With a function to return indexed vector points
10+
"""
11+
12+
def __init__(self, name = "mesh0"):
13+
"""
14+
Initialise the new Mesh object
15+
"""
16+
self.name = name
17+
self.vertex_index = []
18+
self.vertices = []
19+
20+
def setVertices(self, vertices):
21+
self.vertices = vertices
22+
23+
def setVertexIndex(self, idx):
24+
self.vertex_index = idx
25+
26+
def mesh_array(self):
27+
"""
28+
Returns array of Vec3D for use in pyprocessing
29+
"""
30+
temp = []
31+
for i in self.vertex_index:
32+
temp.append(self.vertices[i])
33+
return temp
34+
35+
def vert_string(self, pt):
36+
"""
37+
helper function that returns PovRAY vector string from a Vec3D
38+
"""
39+
return "\t<{0}, {1}, {2}>,\n".format(pt.x, pt.y, pt.z)
40+
41+
def vect_string(self, x, y, z):
42+
return "\t<{0}, {1}, {2}>,\n".format(x, y, z)
43+
44+
def index_string(self, idx):
45+
"""
46+
PovRAY formatted face indices as string
47+
"""
48+
temp = []
49+
temp.append("\tface_indices {\n")
50+
temp.append("\t{0:d},\n".format(int(len(idx)/3))) # need to coerce int?
51+
for i in xrange(0, len(idx), 3):
52+
temp.append(self.vect_string(idx[i], idx[i +1], idx[i +2]))
53+
temp.append("\t}\n")
54+
return "".join(temp)
55+
56+
def mesh2(self):
57+
"""
58+
Returns a string of type mesh2 array for use in PovRAY
59+
"""
60+
temp = []
61+
temp.append("mesh2{\n")
62+
temp.append("\tvertex_vectors {\n")
63+
temp.append("\t{0},\n".format(len(self.vertices)))
64+
for pt in self.points():
65+
temp.append(self.vert_string(pt))
66+
temp.append("\t}\n")
67+
temp.append(self.index_string(self.vertex_index))
68+
temp.append("}\n")
69+
return "".join(temp)
70+
71+
@classmethod
72+
def meshFromVertices(clas, vertices, vertex_index):
73+
"""
74+
Alternative constructor creates an instance of from vertex array and vertex indices
75+
"""
76+
temp = Mesh()
77+
temp.setVertexIndex(vertex_index)
78+
temp.setVertices(vertices)
79+
return temp
80+
81+

0 commit comments

Comments
 (0)