def read(self,fname): """ read an ascii point file """ import warnings pts = sg.Point3Array([]) col = sg.Color4Array([]) isptsfile = ('.pts' in fname) ispwnfile = ('.pwn' in fname) f = file(fname,"r") if isptsfile or ispwnfile: f.readline() # skip line number i = 0 lines = f.readlines() sep = None if ';' in lines[0]: sep = ';' elif ',' in lines[0]: sep = ',' elif '\t' in lines[0]: sep = '\t' for line in lines: if line[0] == '#': continue values = line.split(sep) try: pts.append(mt.Vector3(float(values[0]),float(values[1]),float(values[2]))) if len(values) > 3: if not isptsfile : col.append(sg.Color4(int(values[3]),int(values[4]),int(values[5]),0)) else : col.append(sg.Color4(int(values[4]),int(values[5]),int(values[6]),0)) except Exception,e: if isptsfile and len(values) == 4: #warnings.warn("Skip line "+str(i+isptsfile)+" in file '"+fname+"'.") pass else: if len(values) != 0: warnings.warn("Error in file '"+fname+"' at line "+str(i+isptsfile)) raise e i+=1
def read(self, fname): """ read an ascii point file """ import warnings pts = sg.Point3Array([]) col = sg.Color4Array([]) isptsfile = ('.pts' in fname) istxtfile = ('.txt' in fname) ispwnfile = ('.pwn' in fname) isxyzfile = ('.xyz' in fname) f = file(fname, "r") if isptsfile or ispwnfile: f.readline() # skip line number i = 0 lines = f.readlines() sep = None if not isxyzfile: if ';' in lines[0]: sep = ';' elif ',' in lines[0]: sep = ',' elif '\t' in lines[0]: sep = '\t' for line in lines: if line[0] == '#': continue if sep != ',': line = line.replace(',', '.') values = line.split(sep) try: pts.append( mt.Vector3(float(values[0]), float(values[1]), float(values[2]))) if len(values) > 3: if not isptsfile and not istxtfile: col.append( sg.Color4(int(values[3]), int(values[4]), int(values[5]), 0)) else: col.append( sg.Color4(int(values[4]), int(values[5]), int(values[6]), 0)) except Exception as e: if isptsfile and len(values) == 4: #warnings.warn("Skip line "+str(i+isptsfile)+" in file '"+fname+"'.") pass else: if len(values) != 0: warnings.warn("Error in file '" + fname + "' at line " + str(i + isptsfile)) raise e i += 1 f.close() center = pts.getCenter() if len(col) == len(pts): pointset = sg.PointSet(pts, col) else: pointset = sg.PointSet(pts) pointset = sg.Translated(-center, pointset) sc = sg.Scene() sc += pointset return sc
def multiply(positions, group): " return a scene by translated each group by a given position " # compute the bbox bbox = BoundingBox(group[0]) for shape in group: bbox += BoundingBox(shape) size = max(bbox.getSize()[0], bbox.getSize()[1]) scene = Scene() z_axis = math.Vector3(0, 0, 1) for pos in positions: angle = random.uniform(0, random.TWOPI) [ scene.add( Shape( Translated(pos * size, AxisRotated(z_axis, angle, s.geometry)), s.appearance)) for s in group ] return scene
def regular_sample(nx=4, ny=5, space_x=5, space_y=4): return (math.Vector3(x * space_x, (x % 2) * space_x / 2. + y * space_y, 0) for x in range(nx) for y in range(ny))
def sample(x_size=20, y_size=20, nb=20): """ return a list of transformation """ import random l = random.sample(xrange(x_size * y_size), nb) return (math.Vector3(t % y_size, t / x_size, 0.) for t in l)