summaryrefslogtreecommitdiff
path: root/make.py
diff options
context:
space:
mode:
authorAnthony LaTorre <tlatorre9@gmail.com>2011-06-21 20:52:17 -0400
committerAnthony LaTorre <tlatorre9@gmail.com>2011-06-21 20:52:17 -0400
commitf04ecc51cd9d634a7c418a611946944b05c6bec4 (patch)
treeab4e1ed548d2db3e73b965d0b16edf4056984d3e /make.py
parent93ad18d540a351f628726bfff9bf16f3dcf9ceb3 (diff)
downloadchroma-f04ecc51cd9d634a7c418a611946944b05c6bec4.tar.gz
chroma-f04ecc51cd9d634a7c418a611946944b05c6bec4.tar.bz2
chroma-f04ecc51cd9d634a7c418a611946944b05c6bec4.zip
added rotate_extrude() and a number of functions to produce meshes of useful solid shapes. tried to speed up geometry.build() by replacing some for loops with np.fromiter() and imap().
Diffstat (limited to 'make.py')
-rw-r--r--make.py90
1 files changed, 52 insertions, 38 deletions
diff --git a/make.py b/make.py
index e69b28e..29f0a04 100644
--- a/make.py
+++ b/make.py
@@ -1,43 +1,57 @@
import numpy as np
from geometry import Mesh
+from transform import rotate
+
+def rotate_extrude(x, y, theta=np.pi/32):
+ x, y, = np.asarray(x), np.asarray(y)
+
+ if len(x.shape) != 1 or len(y.shape) != 1 or x.size != y.size:
+ raise ValueError('shape mismatch')
+
+ points = np.zeros((len(x),3), dtype=np.float32)
+ points[:,0] = x
+ points[:,1] = y
-def cylinder(radius=1.0, height=1.0, theta=np.pi/32):
angles = np.arange(0, 2*np.pi, theta)
- vertices = np.empty((2*len(angles) + 2, 3), dtype=np.float32)
- vertices[:2*len(angles),0] = radius*np.tile(np.cos(angles),2)
- vertices[:2*len(angles),1] = radius*np.tile(np.sin(angles),2)
- vertices[:len(angles),2] = height/2.0
- vertices[len(angles):2*len(angles),2] = -height/2.0
- vertices[-2] = [0,0,height/2.0]
- vertices[-1] = [0,0,-height/2.0]
-
- triangles = np.zeros((len(angles)*4, 3), dtype=np.uint32)
-
- # top
- triangles[:len(angles),0] = np.arange(len(angles))
- triangles[:len(angles),2] = np.roll(np.arange(len(angles)),1)
- triangles[:len(angles),1] = len(vertices) - 2
-
- # bottom
- triangles[len(angles):2*len(angles),0] = \
- np.arange(len(angles),2*len(angles))
- triangles[len(angles):2*len(angles),1] = \
- np.roll(np.arange(len(angles),2*len(angles)),1)
- triangles[len(angles):2*len(angles),2] = len(vertices) - 1
-
- # side
- triangles[2*len(angles):3*len(angles),0] = np.arange(len(angles))
- triangles[2*len(angles):3*len(angles),2] = \
- np.arange(len(angles),2*len(angles))
- triangles[2*len(angles):3*len(angles),1] = \
- np.roll(np.arange(len(angles),2*len(angles)),1)
-
- # side
- triangles[3*len(angles):4*len(angles),0] = np.arange(len(angles))
- triangles[3*len(angles):4*len(angles),2] = \
- np.roll(np.arange(len(angles),2*len(angles)),1)
- triangles[3*len(angles):4*len(angles),1] = \
- np.roll(np.arange(len(angles)),1)
-
- return Mesh(vertices, triangles)
+ vertices = np.empty((len(points)*len(angles), 3), dtype=np.float32)
+ triangles = np.zeros((len(angles)*(len(points)-1)*2, 3), dtype=np.int32)
+
+ step = len(points) - 1
+
+ for i, angle in enumerate(angles):
+ this_slice = i*len(points)
+ vertices[this_slice:this_slice+len(points)] = rotate(points, angle, (0,-1,0))
+
+ start = 2*i*step
+ next_slice = ((i+1) % len(angles))*len(points)
+
+ triangles[start:start+step,0] = np.arange(this_slice, this_slice+len(points)-1)
+ triangles[start:start+step,1] = np.arange(next_slice, next_slice+len(points)-1)
+ triangles[start:start+step,2] = np.arange(this_slice+1, this_slice+len(points))
+
+ triangles[start+step:start+2*step,0] = np.arange(next_slice, next_slice+len(points)-1)
+ triangles[start+step:start+2*step,2] = np.arange(this_slice+1, this_slice+len(points))
+ triangles[start+step:start+2*step,1] = np.arange(next_slice+1, next_slice+len(points))
+
+ return Mesh(vertices, triangles, remove_duplicate_vertices=True)
+
+def cube(size=1):
+ a = np.sqrt(size**2/2.0)
+ x = [0,a,a,0]
+ y = [-size/2.0,-size/2.0,size/2.0,size/2.0]
+ return rotate_extrude(x, y, np.pi/2)
+
+def cylinder(radius1=1, radius2=1, height=2, theta=np.pi/32):
+ x = [0,radius1,radius2,0]
+ y = [-height/2.0, -height/2.0, height/2.0, height/2.0]
+ return rotate_extrude(x, y, theta)
+
+def sphere(radius=1, theta=np.pi/32):
+ profile_angles = np.arange(-np.pi/2, np.pi/2+theta, theta)
+ return rotate_extrude(np.cos(profile_angles), np.sin(profile_angles), theta)
+
+def torus(radius=1, offset=3, phi=np.pi/32, theta=np.pi/32):
+ profile_angles = np.arange(0, 2*np.pi+phi, phi)
+ x, y = np.cos(profile_angles), np.sin(profile_angles)
+ return rotate_extrude(x + offset, y)