import Numeric import math import random # add inundation dir to your pythonpath from pmesh.mesh import Mesh from coordinate_transforms.geo_reference import Geo_reference WidtH = 200 # width of boudary in metres #random.uniform(0,1) #proper implemantation of random generator def create_mesh(maximum_triangle_area, depth, mesh_file=None, triangles_in_name = False): """ triangles_in_name, if True is used to append the number of triangles in the mesh to the mesh file name. """ WidtH = 200 # width of boudary in metres breadth = depth print "building footprint" print depth * breadth , "m^2" block = 625 BL = block**0.5 porosity = breadth/BL print porosity, " Building porosity" # create a mesh instance of class Mesh m = Mesh() # Boundary of problem outer_polygon = [[0,0],[5*WidtH,0],[5*WidtH,WidtH],[0,WidtH]] m.add_region_from_polygon(outer_polygon, tags={'wall':[0,2], 'wave':[3], 'back':[1]}) # inner polygons => building boundaries whs_front = 15/2 lhs_front = 15/2 whs_rear = 5/2 lhs_rear = 5/2 #Th = (45 *(3.14159/180)) # sets an initial rotation Th = 0 ForDep = (0.2*WidtH) + (BL-whs_front) RearDep = 1.2*WidtH Breadths = Numeric.arrayrange( -(BL/2), WidtH+(BL/2), (BL)) #Breadths = Numeric.arrayrange( (BL/2), 5*WidtH-(BL/2), (BL)) # For ortho-offset #print Breadths, "Breadths" Depths_first = Numeric.arrayrange( ForDep, RearDep, BL ) Depths_second = Numeric.arrayrange( RearDep+BL, 2.2*WidtH, BL ) #print Depths, "Depths" for i,D in enumerate(Depths_first): Breadths = Breadths + ((-1)**i)*(0.5*BL/2) #Used to offset buildings # First block of buildings for B in Breadths: wh1 = (-whs_front) * math.cos(Th) + (-lhs_front) * math.sin(Th) lh1 = (-lhs_front) * math.cos(Th) - (-whs_front) * math.sin(Th) wh2 = (+whs_front) * math.cos(Th) + (-lhs_front) * math.sin(Th) lh2 = (-lhs_front) * math.cos(Th) - (+whs_front) * math.sin(Th) wh3 = (+whs_front) * math.cos(Th) + (+lhs_front) * math.sin(Th) lh3 = (+lhs_front) * math.cos(Th) - (+whs_front) * math.sin(Th) wh4 = (-whs_front) * math.cos(Th) + (+lhs_front) * math.sin(Th) lh4 = (+lhs_front) * math.cos(Th) - (-whs_front) * math.sin(Th) polygon = [[D+wh1,B+lh1],[D+wh2,B+lh2],[D+wh3,B+lh3],[D+wh4,B+lh4]] m.add_hole_from_polygon(polygon, tags={'wall':[0,1,2,3]}) # Th = Th + (37.3 *(3.14159/180)) # keeps rotating individual buildings. for i,D in enumerate(Depths_second): Breadths = Breadths + ((-1)**i)*(0.5*BL/2) #Used to offset buildings # Second block of buildings for B in Breadths: wh1 = (-whs_rear) * math.cos(Th) + (-lhs_rear) * math.sin(Th) lh1 = (-lhs_rear) * math.cos(Th) - (-whs_rear) * math.sin(Th) wh2 = (+whs_rear) * math.cos(Th) + (-lhs_rear) * math.sin(Th) lh2 = (-lhs_rear) * math.cos(Th) - (+whs_rear) * math.sin(Th) wh3 = (+whs_rear) * math.cos(Th) + (+lhs_rear) * math.sin(Th) lh3 = (+lhs_rear) * math.cos(Th) - (+whs_rear) * math.sin(Th) wh4 = (-whs_rear) * math.cos(Th) + (+lhs_rear) * math.sin(Th) lh4 = (+lhs_rear) * math.cos(Th) - (-whs_rear) * math.sin(Th) polygon = [[D+wh1,B+lh1],[D+wh2,B+lh2],[D+wh3,B+lh3],[D+wh4,B+lh4]] m.add_hole_from_polygon(polygon, tags={'wall':[0,1,2,3]}) # measures apparent depth to inundating wave # App_breadth = depth*(math.sin(math.pi()*Th/180) + math.cos(math.pi()*Th/180))/BL m.generate_mesh(maximum_triangle_area=maximum_triangle_area) triangle_count = m.get_triangle_count() if mesh_file is None: return m, triangle_count else: if triangles_in_name is True: mesh_file = mesh_file[:-4] + '_Str_Off(hlf)_D=' + str(whs_front*2) + '_' + str(whs_rear*2) + '_' + str(triangle_count) \ + mesh_file[-4:] m.export_mesh_file(mesh_file) return mesh_file, triangle_count #------------------------------------------------------------- if __name__ == "__main__": _, triangle_count = create_mesh(100,15,mesh_file="test.tsh") print "triangle_count",triangle_count