def gen_indexed_knots_from_rib(filename): # Build a list of 16-wide lists: verts = [] def patch(knots): verts.append(knots) parse_rib(filename, patch) # Flatten the lists: verts = [c for v in verts for c in v] # The initial index buffer is just 0,1,2... knots = range(len(verts)) print "Vert count before welding: ", len(verts) if os.path.exists("weldcache"): print "Loading weldcache..." flatverts, knots = pickle.load(open("weldcache")) args = [iter(flatverts)] * 3 verts = [Point3(*v) for v in izip(*args)] else: verts, knots = weld(verts, knots) print "Saving weldcache..." flatverts = [c for v in verts for c in v] cache = (flatverts, knots) pickle.dump(cache, open("weldcache", "wb")) print "Vert count after welding: ", len(verts) print "Index count: ", len(knots) patchCount = len(knots) / 16 quadCount = 9 * patchCount print "Quad count: ", quadCount # Consider Renderman's 16-point (left-handed) patch. # This shows the 16 vertex indices and the 9 quad indices: # # 0----1----2------3 # | | | | # | 0 | 1 | 2 | # | | | | # 4----5----6------7 # | | | | # | 3 | 4 | 5 | # | | | | # 8----9----10----11 # | | | | # | 6 | 7 | 8 | # | | | | # 12---13---14----15 # # Build a 9-entry lookup table. # Maps from a quad index to the vertex indices at its 4 corners. cornerMap = [] for n in xrange(9): i = 4 * (n / 3) + (n % 3) cornerMap.append((i, 1 + i, 4 + i, 5 + i)) # Generate the vertex-to-quads map and compute facet normals. print "Building topology map..." quadNeighbors = [[] for v in verts] quadNormals = [] for q in xrange(quadCount): corners = [] for i in cornerMap[q % 9]: v = knots[i + (q / 9) * 16] quadNeighbors[v] += [q] corners.append(verts[v]) a, b, c, d = corners n = (c - a).cross(b - a).normalized() quadNormals.append(n) # Average together the facet normals. Creases are not supported. print "Computing smooth normals..." norms = [] for vertex, quads in izip(verts, quadNeighbors): normal = Vector3(0, 0, 0) for quad in quads: normal += quadNormals[quad] normal.normalize() norms.append(normal) return verts, norms, knots
def gen_indexed_knots_from_rib(filename): # Build a list of 16-wide lists: verts = [] def patch(knots): verts.append(knots) parse_rib(filename, patch) # Flatten the lists: verts = [c for v in verts for c in v] # The initial index buffer is just 0,1,2... knots = range(len(verts)) print "Vert count before welding: ", len(verts) if os.path.exists("weldcache"): print "Loading weldcache..." flatverts, knots = pickle.load( open("weldcache") ) args = [iter(flatverts)] * 3 verts = [Point3(*v) for v in izip(*args)] else: verts, knots = weld(verts, knots) print "Saving weldcache..." flatverts = [c for v in verts for c in v] cache = (flatverts, knots) pickle.dump( cache, open("weldcache", "wb") ) print "Vert count after welding: ", len(verts) print "Index count: ", len(knots) patchCount = len(knots) / 16 quadCount = 9 * patchCount print "Quad count: ", quadCount # Consider Renderman's 16-point (left-handed) patch. # This shows the 16 vertex indices and the 9 quad indices: # # 0----1----2------3 # | | | | # | 0 | 1 | 2 | # | | | | # 4----5----6------7 # | | | | # | 3 | 4 | 5 | # | | | | # 8----9----10----11 # | | | | # | 6 | 7 | 8 | # | | | | # 12---13---14----15 # # Build a 9-entry lookup table. # Maps from a quad index to the vertex indices at its 4 corners. cornerMap = [] for n in xrange(9): i = 4 * (n / 3) + (n % 3) cornerMap.append((i, 1+i, 4+i, 5+i)) # Generate the vertex-to-quads map and compute facet normals. print "Building topology map..." quadNeighbors = [[] for v in verts] quadNormals = [] for q in xrange(quadCount): corners = [] for i in cornerMap[q % 9]: v = knots[i + (q / 9) * 16] quadNeighbors[v] += [q] corners.append(verts[v]) a, b, c, d = corners n = (c - a).cross(b - a).normalized() quadNormals.append(n) # Average together the facet normals. Creases are not supported. print "Computing smooth normals..." norms = [] for vertex, quads in izip(verts, quadNeighbors): normal = Vector3(0, 0, 0) for quad in quads: normal += quadNormals[quad] normal.normalize() norms.append(normal) return verts, norms, knots
#!/usr/bin/python from sys import path as module_paths module_paths.append('./pymesh') module_paths.append('./pyctm') module_paths.append('./pyeuclid') from platform_open import * from export_ctm import * from weld import * import parametric, patch outfile = "teapot.ctm" print "Loading RIB file..." funcs = patch.load_rib("models/uteapot.rib") slices, stacks = 8, 8 print "Evaluating parametric functions..." verts, faces = parametric.multisurface(slices, stacks, funcs) print "Welding verts..." verts, faces = weld(verts, faces) export_ctm(verts, faces, outfile) platform_open(outfile)
GenerateCtm = False if GenerateCtm: from export_ctm import * funcs = patch.gen_funcs_from_rib(infile) slices, stacks = 8, 8 print "Evaluating parametric functions..." verts, faces = parametric.multisurface(slices, stacks, funcs) # Note that creases are not supported by weld(). print "Welding verts..." verts, faces = weld(verts, faces) export_raw(verts, faces, outfile) platform_open(outfile) else: def dump_tuples(headerFile, mylist): for v in mylist: s = [] for value in v[:]: if math.floor(value) == value: s.append(str(int(value))) else: s.append('%gf' % value) headerFile.write("\t{ %s, %s, %s },\n" % tuple(s))