Changeset 959
- Timestamp:
- Feb 25, 2005, 2:36:07 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/alpha_shape
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/alpha_shape/alpha_shape.py
r956 r959 3 3 4 4 import exceptions 5 from Numeric import array, Float, divide_safe, sqrt 5 from Numeric import array, Float, divide_safe, sqrt, product 6 6 from load_mesh.loadASCII import load_points_file, export_boundary_file 7 7 import random … … 12 12 INF = pow(10,9) 13 13 14 def alpha_shape_via_files(point_file, boundary_file, alpha= None): 14 def alpha_shape_via_files(point_file, boundary_file, alpha= None): 15 15 16 16 point_dict = load_points_file(point_file) … … 70 70 return self.boundary 71 71 72 def set_boundary_type(self,flag=0 ):72 def set_boundary_type(self,flag=0,small=0.2): 73 73 """ 74 74 Use the flag to set constraints on the boundary … … 83 83 if flag>=1 : 84 84 #remove holes 85 bdry = self._remove_holes(bdry )85 bdry = self._remove_holes(bdry,small) 86 86 if flag>=2: 87 87 #remove sharp indents … … 420 420 """ 421 421 Given an alpha value, 422 return the edges on the boundary of the alpha-shape422 return the indices of edges on the boundary of the alpha-shape 423 423 """ 424 424 def reg_edge(k): … … 437 437 return filter(exp_vert, range(len(self.vertexinterval))) 438 438 439 440 def _remove_holes(self,bdry): 439 def _vertices_from_edges(self,elist): 440 """ 441 Returns the list of unique vertex labels from edges in elist 442 """ 443 444 v1 = [elist[k][0] for k in range(len(elist))] 445 v2 = [elist[k][1] for k in range(len(elist))] 446 v = v1+v2 447 v.sort() 448 vertices = [v[k] for k in range(len(v)) if v[k]!=v[k-1] ] 449 return vertices 450 451 452 def _remove_holes(self,bdry,small): 441 453 """ 442 454 Given the edges in bdry, find the largest exterior components. … … 452 464 453 465 # get a list of unique vertex labels: 454 v1 = [bdry[k][0] for k in range(len(bdry))] 455 v2 = [bdry[k][1] for k in range(len(bdry))] 456 v = v1+v2 457 v.sort() 458 verts = [v[k] for k in range(len(v)) if v[k]!=v[k-1] ] 466 verts = self._vertices_from_edges(bdry) 459 467 #print "verts ", verts, "\n" 460 468 … … 501 509 502 510 # discard the edges in the little components 503 # (i.e. those components with less than twenty percentof bdry points)504 cutoff = round( 0.2*len(verts))511 # (i.e. those components with less than 'small' fraction of bdry points) 512 cutoff = round(small*len(verts)) 505 513 littleind = [k for k in range(len(vptr)) if (vptr[k]<0 and vptr[k]>-cutoff)] 506 514 if littleind: … … 519 527 Given edges in bdry, test for acute-angle indents and remove them. 520 528 """ 529 # first find triangles not in alpha-shape 530 def tri_rad_gta(k): 531 return self.triradius[k]>self.alpha 532 533 extrind = filter(tri_rad_gta, range(len(self.triradius))) 534 #print "exterior triangles: ", extrind 535 536 # find triangles that share two edges with bdry 537 # i.e. triangles that have all verts in bdry. 538 verts = self._vertices_from_edges(bdry) 539 540 bdrytri = [] 541 for ind in extrind: 542 bvct = 0 543 for j in [0,1,2]: 544 if self.deltri[ind][j] in verts: 545 bvct +=1 546 if bvct==3: 547 bdrytri.append(ind) 548 549 #print "boundary triangles ", bdrytri 550 551 # test the bdrytri triangles for acute angles 552 probtri = [] 553 for tind in bdrytri: 554 tri = self.deltri[tind] 555 dx = array([self.points[tri[(i+1)%3],0] - self.points[tri[(i+2)%3],0] for i in [0,1,2]]) 556 dy = array([self.points[tri[(i+1)%3],1] - self.points[tri[(i+2)%3],1] for i in [0,1,2]]) 557 anglesign = array([(-dx[(i+1)%3]*dx[(i+2)%3]-dy[(i+1)%3]*dy[(i+2)%3]) for i in [0,1,2]]) 558 if product(anglesign) > 0: 559 probtri.append(tind) 560 561 #print "problem triangles ", probtri 562 563 # adjust the bdry edges of the probtri triangles 564 for pind in probtri: 565 tri = self.deltri[pind] 566 #print "triangle verts: ", tri 567 for i in [0,1,2]: 568 edga = (tri[(i+1)%3], tri[(i+2)%3]) 569 edgb = (tri[(i+2)%3], tri[(i+1)%3]) 570 if edga in bdry: 571 bdry.remove(edga) 572 elif edgb in bdry: 573 bdry.remove(edgb) 574 else: 575 bdry.append(edga) 576 577 newbdry = bdry 521 578 return newbdry 522 579 … … 525 582 Given edges in bdry, test for vertices with more than 4 incident edges. 526 583 Expand by adding back in associated triangles... 527 528 """ 584 """ 585 586 #verts = self._vertices_from_edges(bdry) 587 588 v1 = [bdry[k][0] for k in range(len(bdry))] 589 v2 = [bdry[k][1] for k in range(len(bdry))] 590 v = v1+v2 591 v.sort() 592 probv = [v[k] for k in range(len(v)) if (v[k]!=v[k-1] and v.count(k)>2) ] 593 594 # first find triangles not in alpha-shape 595 def tri_rad_gta(k): 596 return self.triradius[k]>self.alpha 597 598 extrind = filter(tri_rad_gta, range(len(self.triradius))) 599 #print "exterior triangles: ", extrind 600 601 # find exterior triangles that have a vertex in probv 602 603 bdrytri = [] 604 for ind in extrind: 605 bvct = 0 606 for j in [0,1,2]: 607 if self.deltri[ind][j] in probv: 608 bvct +=1 609 if bvct>0: 610 bdrytri.append(ind) 611 612 #print "boundary triangles ", bdrytri 613 614 615 616 newbdry = bdry 529 617 return newbdry 530 618 -
inundation/ga/storm_surge/alpha_shape/test_alpha_shape.py
r956 r959 97 97 98 98 99 def test_boundary (self):99 def test_boundary_1(self): 100 100 a = [0.0, 0.0] 101 101 b = [2.0, 0.0] … … 111 111 #print "result",result 112 112 answer = [(0, 3), (3, 6), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] 113 assert allclose(answer, result) 114 pass 115 116 117 def test_boundary_2(self): 118 a = [0.0, 0.0] 119 b = [2.0, 0.0] 120 c = [4.0, 0.0] 121 cd = [3.0,2.0] 122 d = [4.0, 4.0] 123 e = [2.0, 4.0] 124 f = [0.0, 4.0] 125 126 alpha = Alpha_Shape([a,b,c,cd,d,e,f]) 127 alpha.set_boundary_type(2) 128 result = alpha.get_boundary() 129 #print "result",result 130 result [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 0)] 113 131 assert allclose(answer, result) 114 132 pass
Note: See TracChangeset
for help on using the changeset viewer.