
import Numeric
import math
import random
import polygon
import array
# add inundation dir to your pythonpath
from anuga.pmesh.mesh import Mesh
from anuga.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.2],[5*WidtH,-0.2],[5*WidtH,WidtH+0.2],[0,WidtH+0.2]]# rotated
    #outer_polygon = [[0,0],[5*WidtH,0],[5*WidtH,WidtH],[0,WidtH]]# normal
    m.add_region_from_polygon(outer_polygon, tags={'wall':[0,2], 'wave':[3], 'back':[1]})

    # inner polygons => building boundaries
    
    dhs = depth/2.0 
    bhs = breadth/2.0 
    #Th = (45 *(3.14159/180)) # sets an initial rotation      
    Th = 0
    ForDep = (0.2*WidtH) + (BL-dhs)
    RearDep = 1.2*WidtH 
        
    Breadths = Numeric.arrayrange( -(BL/2), WidtH+(BL/2), (BL))
    #Breadths = Numeric.arrayrange( 0, WidtH, (BL))
    #Breadths = Numeric.arrayrange( (BL/2), 5*WidtH-(BL/2), (BL)) # For ortho-offset
    print Breadths, "Breadths"
    Depths = Numeric.arrayrange( ForDep, RearDep, BL )
    print Depths, "Depths"

    for i,D in enumerate(Depths):
        Breadths = Breadths + ((-1)**i)*(1.0*BL/2) #Used to offset buildings
        for B in Breadths:
            dh1 = (-dhs) * math.cos(Th) + (-bhs) * math.sin(Th)
            bh1 = (-bhs) * math.cos(Th) - (-dhs) * math.sin(Th)
            dh2 = (+dhs) * math.cos(Th) + (-bhs) * math.sin(Th)
            bh2 = (-bhs) * math.cos(Th) - (+dhs) * math.sin(Th)
            dh3 = (+dhs) * math.cos(Th) + (+bhs) * math.sin(Th)
            bh3 = (+bhs) * math.cos(Th) - (+dhs) * math.sin(Th)
            dh4 = (-dhs) * math.cos(Th) + (+bhs) * math.sin(Th)
            bh4 = (+bhs) * math.cos(Th) - (-dhs) * math.sin(Th)
            house = [[D+dh1,B+bh1],[D+dh2,B+bh2],[D+dh3,B+bh3],[D+dh4,B+bh4]]
            walls = list(range(len(house)))
            m.add_hole_from_polygon(house, tags={'wall':walls})        
            #print "*********hole added*************"
            # Th = Th + (37.3 *(3.14159/180)) # keeps rotating individual buildings.
    
    m.generate_mesh(maximum_triangle_area=maximum_triangle_area)
    triangle_count = m.get_triangle_count()
    print 'mesh_file', mesh_file
    if mesh_file is None:    
        return m, triangle_count
    else:
        if triangles_in_name is True:
            mesh_file = mesh_file[:-4] + 'testdob' + str(depth) + '_' + 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 
