1from pykludge3d import *
2
3def cone( originX, originY, originZ, height, radius, sides, endcap ):
4	'''creates a cone
5	origin X,Y,Z specifies the location for the center of the cone's base
6	height specifies the height
7	radius specifies the radius of the cone's base
8	sides specifies how many sides the cone has
9	endcap determines if the cone should have an endcap (0 = no, 1 = yes)'''
10	if sides < 3:
11		return []
12	topPoint = vert_insert( 0, 0, height )
13	verts = []
14	angle = 360.0 / sides
15	for i in range( sides ):
16		verts.append( vert_insert( radius, 0, 0 ) )
17		rotate_verts( verts, 0, 0, -1.0 * angle )
18
19	for i in range( len( verts ) ):
20		poly_insert( [topPoint, verts[i], verts[(i+1)%len( verts )]] )
21
22	if endcap:
23		poly_insert( verts )
24
25	verts.append( topPoint )
26	translate_verts( verts, originX, originY, originZ )
27	return verts
28# end of cone
29
30
31def cylinder( originX, originY, originZ, height, topRadius, bottomRadius, sides, endcaps ):
32	'''creates a cylinder
33	origin X,Y,Z specifies the location for the center of the cylinder's base
34	height specifies the height
35	topRadius specifies the radius of the cylinder's top end
36	bottomRadius specifies the radius of the cylinder's bottom end
37	sides specifies how many sides the cylinder has
38	endcap determines if the cylinder should have endcaps (0 = no, 1 = yes)'''
39	if sides < 3:
40		return []
41	topVerts = []
42	bottomVerts = []
43	angle = 360.0 / sides
44	for i in range( sides ):
45		topVerts.append( vert_insert( topRadius, 0, height ) )
46		rotate_verts( topVerts, 0, 0, -1.0 * angle )
47		bottomVerts.append( vert_insert( bottomRadius, 0, 0 ) )
48		rotate_verts( bottomVerts, 0, 0, -1.0 * angle )
49
50	for i in range( len( topVerts ) ):
51		poly_insert( [bottomVerts[(i+1)%len( bottomVerts )], bottomVerts[i], topVerts[i], topVerts[(i+1)%len( topVerts )]] )
52
53	topVerts.reverse()
54	if endcaps:
55		poly_insert( topVerts )
56		poly_insert( bottomVerts )
57
58	verts = topVerts + bottomVerts
59	translate_verts( verts, originX, originY, originZ )
60	return verts
61# end of cylinder
62
63
64def cube( minX, minY, minZ, maxX, maxY, maxZ ):
65	'''creates a cuboid
66	min X,Y,Z specifies the coordinate for the cube's corner closest to
67	the origin (0,0,0)
68	max X,Y,Z specifies the coordinate for the cube's corner furthest from
69	the origin (0,0,0)'''
70	# cubes don't lend themselves to iterative construction, as cones and
71	# cylinders do.
72	#
73	# Arbitrary vertex naming:
74	# v8 ------ v7  v4 ------ v3
75	# |   Top   |   | Bottom  |
76	# |         |   |		  |
77	# v5 ------ v6  v1 ------ v2
78	#
79	# v1 will be at <minX, minY, minZ>, v7 will be at <maxX, maxY, maxZ>
80	#
81	v1 = vert_insert( minX, minY, minZ )
82	v2 = vert_insert( maxX, minY, minZ )
83	v3 = vert_insert( maxX, maxY, minZ )
84	v4 = vert_insert( minX, maxY, minZ )
85	v5 = vert_insert( minX, minY, maxZ )
86	v6 = vert_insert( maxX, minY, maxZ )
87	v7 = vert_insert( maxX, maxY, maxZ )
88	v8 = vert_insert( minX, maxY, maxZ )
89
90	poly_insert( [ v4, v3, v2, v1 ] ) # bottom
91	poly_insert( [ v5, v6, v7, v8 ] ) # top
92	poly_insert( [ v1, v2, v6, v5 ] ) # sides...
93	poly_insert( [ v2, v3, v7, v6 ] )
94	poly_insert( [ v3, v4, v8, v7 ] )
95	poly_insert( [ v4, v1, v5, v8 ] )
96
97	return [ v1, v2, v3, v4, v5, v6, v7, v8 ]
98# end of cube
99
100
101def sphere( originX, originY, originZ, radius, numSlices, numWedges ):
102	'''creates a sphere
103	origin X,Y,Z specifies the center of the sphere
104	radius specifies the radius
105	numSlices determines how many 'layers' will be used to make the sphere
106	(think 'wedding cake', or the latitude lines on a globe)
107	numWedges determines how many 'wedges' will be used to make the sphere
108	(think of an orange, or the longitude lines on a globe)'''
109	if numSlices < 5 or numWedges < 5:
110		return []
111	vert = vert_insert( 0, 0, radius )
112	slices = [[vert]]
113	verts = [vert]
114	sliceAngle = 180.0 / numSlices
115	wedgeAngle = 360.0 / numWedges
116	currSliceAngle = -90.0 + sliceAngle
117	while currSliceAngle < 89.9:
118		currSlice = []
119		for j in range( numWedges ):
120			vert = vert_insert( radius, 0, 0 )
121			verts.append( vert )
122			currSlice.append( vert )
123			# first, rotate the vert to its slice's position
124			rotate_verts( [vert], 0, currSliceAngle, 0 )
125			# then, rotate the slice
126			rotate_verts( currSlice, 0, 0, -1.0 * wedgeAngle )
127		slices.append( currSlice )
128		currSliceAngle += sliceAngle
129	vert = vert_insert( 0, 0, -1.0 * radius )
130	slices.append( [vert] )
131	verts.append( vert )
132
133	for j in range( len( slices ) - 1 ):
134		currSlice = slices[j]
135		nextSlice = slices[j+1]
136		for i in range( max( len(currSlice), len(nextSlice) ) ):
137			# it's ok to include the same vert twice (as would occur when
138			# building the top and bottom slices) in the list below, because
139			# poly_add_vertex will check to make sure that the same vertex
140			# does not get added to a poly more than once
141			poly_insert( [currSlice[(i+1)%len( currSlice )], \
142						  currSlice[i%len( currSlice )], \
143						  nextSlice[i%len( nextSlice )], \
144						  nextSlice[(i+1)%len( nextSlice )]] )
145
146	translate_verts( verts, originX, originY, originZ )
147	return verts
148# end of sphere
149
150register_function( __name__, cone )
151register_function( __name__, cylinder )
152register_function( __name__, cube )
153register_function( __name__, sphere )
154