- Timestamp:
- May 4, 2019, 10:14:47 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/geometry_tools.py
r2507 r2508 42 42 43 43 ## Shapes/objects 44 # buoy1: Function to draw a buoy as superposition of prism and section of ball 44 45 # circ_sec: Function union of point A and B by a section of a circle 45 46 # ellipse_polar: Function to determine an ellipse from its center and polar coordinates … … 310 311 p2 = lpoints[ip+1] 311 312 dps = dist_points(p1, p2) 312 jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, Np)313 jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, 'short', Np) 313 314 314 315 Np2 = N - (Npts-1)*Np … … 316 317 p2 = lpoints[0] 317 318 dps = dist_points(p1, p2) 318 jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., Np2)319 jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., 'short', Np2) 319 320 320 321 return jcirc_sec … … 346 347 dps = dist_points(p1, p2) 347 348 angle = np.arctan2(p2[0]-p1[0], p2[1]-p1[1]) + np.pi/2. 348 jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, Np)349 jcirc_sec[Np*ip:Np*(ip+1),:] = circ_sec(p1, p2, dps*radfrac, 'short', Np) 349 350 drand = Lrand*np.array([np.sin(angle), np.cos(angle)]) 350 351 for iip in range(Np*ip,Np*(ip+1)): … … 356 357 dps = dist_points(p1, p2) 357 358 angle = np.arctan2(p2[0]-p1[0], p2[1]-p1[1]) + np.pi/2. 358 jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., Np2)359 jcirc_sec[(Npts-1)*Np:N,:] = circ_sec(p1, p2, dps*3., 'short', Np2) 359 360 drand = Lrand*np.array([np.sin(angle), np.cos(angle)]) 360 361 for iip in range(Np*(Npts-1),N): … … 363 364 return jcirc_sec 364 365 366 def write_join_poly(polys, flname='join_polygons.dat'): 367 """ Function to write an ASCII file with the combination of polygons 368 polys: dictionary with the names of the different polygons 369 flname: name of the ASCII file 370 """ 371 fname = 'write_join_poly' 372 373 of = open(flname, 'w') 374 375 for polyn in polys.keys(): 376 vertices = polys[polyn] 377 Npts = vertices.shape[0] 378 for ip in range(Npts): 379 of.write(polyn+' '+str(vertices[ip,1]) + ' ' + str(vertices[ip,0]) + '\n') 380 381 of.close() 382 383 return 384 385 def read_join_poly(flname='join_polygons.dat'): 386 """ Function to read an ASCII file with the combination of polygons 387 flname: name of the ASCII file 388 """ 389 fname = 'read_join_poly' 390 391 of = open(flname, 'r') 392 393 polys = {} 394 polyn = '' 395 poly = [] 396 for line in of: 397 if len(line) > 1: 398 linevals = line.replace('\n','').split(' ') 399 if polyn != linevals[0]: 400 if len(poly) > 1: 401 polys[polyn] = np.array(poly) 402 polyn = linevals[0] 403 poly = [] 404 poly.append([np.float(linevals[2]), np.float(linevals[1])]) 405 else: 406 poly.append([np.float(linevals[2]), np.float(linevals[1])]) 407 408 of.close() 409 polys[polyn] = np.array(poly) 410 411 return polys 365 412 366 413 ####### ###### ##### #### ### ## # … … 442 489 return hyperbola_p, hyperbola_n 443 490 444 def circ_sec(ptA, ptB, radii, Nang=100):491 def circ_sec(ptA, ptB, radii, arc='short', Nang=100): 445 492 """ Function union of point A and B by a section of a circle 446 493 ptA= coordinates od the point A [yA, xA] 447 494 ptB= coordinates od the point B [yB, xB] 448 495 radii= radi of the circle to use to unite the points 496 arc= which arc to be used ('short', default) 497 'short': shortest angle between points 498 'long': largest angle between points 449 499 Nang= amount of angles to use 450 500 """ … … 471 521 472 522 # Getting the arc of the circle at the x-axis 473 dalpha = alpha/(Nang-1) 523 if arc == 'short': 524 dalpha = alpha/(Nang-1) 525 else: 526 dalpha = (2.*np.pi - alpha)/(Nang-1) 474 527 circ_sec = np.zeros((Nang,2), dtype=np.float) 475 528 for ia in range(Nang): … … 488 541 #print 'alpha:', alpha*180./np.pi, 'theta:', theta*180./np.pi, 'rotangle:', rotangle*180./np.pi 489 542 490 491 543 # rotating the arc along the x-axis 492 544 rotcirc_sec = rotate_polygon_2D(circ_sec, rotangle) … … 996 1048 return island1, islandsecs, islanddic 997 1049 998 def write_join_poly(polys, flname='join_polygons.dat'): 999 """ Function to write an ASCII file with the combination of polygons 1000 polys: dictionary with the names of the different polygons 1001 flname: name of the ASCII file 1002 """ 1003 fname = 'write_join_poly' 1004 1005 of = open(flname, 'w') 1006 1007 for polyn in polys.keys(): 1008 vertices = polys[polyn] 1009 Npts = vertices.shape[0] 1010 for ip in range(Npts): 1011 of.write(polyn+' '+str(vertices[ip,1]) + ' ' + str(vertices[ip,0]) + '\n') 1012 1013 of.close() 1014 1015 return 1016 1017 def read_join_poly(flname='join_polygons.dat'): 1018 """ Function to read an ASCII file with the combination of polygons 1019 flname: name of the ASCII file 1020 """ 1021 fname = 'read_join_poly' 1022 1023 of = open(flname, 'r') 1024 1025 polys = {} 1026 polyn = '' 1027 poly = [] 1028 for line in of: 1029 if len(line) > 1: 1030 linevals = line.replace('\n','').split(' ') 1031 if polyn != linevals[0]: 1032 if len(poly) > 1: 1033 polys[polyn] = np.array(poly) 1034 polyn = linevals[0] 1035 poly = [] 1036 poly.append([np.float(linevals[2]), np.float(linevals[1])]) 1037 else: 1038 poly.append([np.float(linevals[2]), np.float(linevals[1])]) 1039 1040 of.close() 1041 polys[polyn] = np.array(poly) 1042 1043 return polys 1050 def buoy1(height=5., width=10., bradii=1.75, bfrac=0.8, N=200): 1051 """ Function to draw a buoy as superposition of prism and section of ball 1052 height: height of the prism (5., default) 1053 width: width of the prism (10., default) 1054 bradii: radii of the ball (1.75, default) 1055 bfrac: fraction of the ball above the prism (0.8, default) 1056 N: total number of points of the buoy (200, default) 1057 1058 """ 1059 fname = 'buoy1' 1060 1061 buoy = np.zeros((N,2), dtype=np.float) 1062 1063 NNp = 0 1064 iip = 0 1065 # Base 1066 ix = -width/2. 1067 iy = 0. 1068 Np = 4 1069 dx = width/(Np-1) 1070 dy = 0. 1071 for ip in range(Np): 1072 buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] 1073 NNp = NNp + Np 1074 iip = NNp 1075 1076 # right lateral 1077 ix = width/2. 1078 iy = 0. 1079 Np = 2 1080 dx = 0. 1081 dy = height/(Np-1) 1082 for ip in range(Np): 1083 buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] 1084 NNp = NNp + Np 1085 iip = NNp 1086 1087 # right upper 1088 ix = width/2. 1089 iy = height 1090 Np = 4 1091 dx = -(width/2.-bradii*bfrac)/(Np-1) 1092 dy = 0. 1093 for ip in range(Np): 1094 buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] 1095 NNp = NNp + Np 1096 iip = NNp 1097 1098 # ball 1099 p1 = np.array([height, -bradii*bfrac]) 1100 p2 = np.array([height, bradii*bfrac]) 1101 Np = N - 2*(NNp) 1102 buoy[iip:iip+Np,:] = circ_sec(p2, p1, 2.*bradii, 'long', Np) 1103 NNp = NNp + Np 1104 iip = NNp 1105 1106 # left upper 1107 ix = -bradii*bfrac 1108 iy = height 1109 Np = 4 1110 dx = -(width/2.-bradii*bfrac)/(Np-1) 1111 dy = 0. 1112 for ip in range(Np): 1113 buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] 1114 NNp = NNp + Np 1115 iip = NNp 1116 1117 # left lateral 1118 ix = -width/2. 1119 iy = height 1120 Np = 2 1121 dx = 0. 1122 dy = -height/(Np-1) 1123 for ip in range(Np): 1124 buoy[iip+ip,:] = [iy+dy*ip,ix+dx*ip] 1125 NNp = NNp + Np 1126 iip = NNp 1127 1128 buoysecs = ['base'] 1129 buoydic = {'base': [buoy, '-', 'k', 1.5]} 1130 1131 return buoy, buoysecs, buoydic 1044 1132 1045 1133 ####### ####### ##### #### ### ## #
Note: See TracChangeset
for help on using the changeset viewer.