Changeset 9242


Ignore:
Timestamp:
Jul 4, 2014, 11:31:31 AM (10 years ago)
Author:
davies
Message:

Improvements + tests for polygon intersections in spatialInputUtils

Location:
trunk/anuga_core/source/anuga/utilities
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/utilities/spatialInputUtil.py

    r9233 r9242  
    352352   
    353353    #################################################################################
    354     def insert_intersection_point(intersectionPt, line_pts, point_movement_threshold):
     354    def insert_intersection_point(intersectionPt, line_pts, point_movement_threshold, verbose=False):
    355355        """
    356356            Add intersectionPt to line_pts, either by inserting it, or if a point on line_pts is
     
    381381            # account for the possibility of L1_pts having > 2 coordinates
    382382            # (riverWalls)
     383            if verbose:
     384                print '      Inserting new point'
    383385            dummyPt=copy.copy(L1_pts[tmp[1]])
    384386            L1_pts.insert(tmp[1]+1,dummyPt)
     
    390392                # Set 3rd coordinate as distance-weighted average of the others
    391393                d0=((L1_pts[tmp[1]][0]-L1_pts[tmp[1]+1][0])**2.+\
    392                    (L1_pts[tmp[1]][0]-L1_pts[tmp[1]+1][1])**2.)**0.5
     394                   (L1_pts[tmp[1]][1]-L1_pts[tmp[1]+1][1])**2.)**0.5
    393395                d1=((L1_pts[tmp[1]+2][0]-L1_pts[tmp[1]+1][0])**2.+\
    394396                   (L1_pts[tmp[1]+2][1]-L1_pts[tmp[1]+1][1])**2.)**0.5
     
    396398   
    397399        else:
     400            if verbose:
     401                print '      Shifting existing point'
    398402            # Move a point already on L1
    399403            L1_pts=shift_point_on_line(iP, L1_pts, tmp[1])
     
    435439    #######################################################################################################
    436440   
    437     def addIntersectionPtsToLines(L1,L2, point_movement_threshold=0.0, buf=1.0e-06, tol2 = 100,
     441    def addIntersectionPtsToLines(L1,L2, point_movement_threshold=0.0, buf=1.0e-05, tol2 = 100,
    438442                                  verbose=True, nameFlag=''):
    439443        """
     
    495499            # Insert the points into the line segments
    496500            for i in range(len(intersectionPts)):
    497                 L1_pts = insert_intersection_point(intersectionPts[i], L1_pts, point_movement_threshold)
    498                 L2_pts = insert_intersection_point(intersectionPts[i], L2_pts, point_movement_threshold)
     501                L1_pts = insert_intersection_point(intersectionPts[i], L1_pts, point_movement_threshold, verbose=verbose)
     502                L2_pts = insert_intersection_point(intersectionPts[i], L2_pts, point_movement_threshold, verbose=verbose)
    499503               
    500504            # Convert to the input format
     
    796800                                verbose=verbose, nameFlag='Bounding Pol intersects '+ n1)
    797801                riverWalls[n1]=Wkb2ListPts(rw1)
    798                 bounding_polygon=Wkb2ListPts(bp2,removeLast=True)
     802                # Since the bounding polygon is a loop, the first/last points are the same
     803                # If one of these was moved, the other should be moved too. Since we
     804                # will drop the last bounding_polygon point, we only need to worry about the first
     805                bounding_polygon=Wkb2ListPts(bp2,removeLast=False)
     806                if(bounding_polygon[-1] is not bounding_polygon[0]):
     807                    bounding_polygon[0]=bounding_polygon[-1]
     808                # Drop the last point
     809                bounding_polygon=bounding_polygon[:-1]
    799810   
    800811        # Clean intersections of bounding polygon and breaklines
     
    813824                                verbose=verbose, nameFlag='Bounding Pol intersects '+n1)
    814825                breakLines[n1]=Wkb2ListPts(bl1)
    815                 bounding_polygon=Wkb2ListPts(bp2, removeLast=True)
    816      
     826                # Since the bounding polygon is a loop, the first/last points are the same
     827                # If one of these was moved, the other should be moved too. Since we
     828                # will drop the last bp2 point, we only need to worry about the first
     829                bounding_polygon=Wkb2ListPts(bp2,removeLast=False)
     830                if(bounding_polygon[-1] is not bounding_polygon[0]):
     831                    bounding_polygon[0]=bounding_polygon[-1]
     832                # Drop the last point
     833                bounding_polygon=bounding_polygon[:-1]
     834
    817835        # Remove the extra 0.0 from bounding polygon [this cannot have 3 coordinates]
    818836        bounding_polygon = [ bounding_polygon[i][0:2] for i in range(len(bounding_polygon))]
     
    820838        for n1 in breakLines.keys():
    821839            breakLines[n1] = [breakLines[n1][i][0:2] for i in range(len(breakLines[n1]))]
     840
     841        # Check that all mesh-boundary points are inside the bounding polygon
     842        from anuga.geometry.polygon import outside_polygon
     843        for blCat in [riverWalls, breakLines]:
     844            for n1 in blCat.keys():
     845                l=len(blCat[n1])
     846                # Test every point -- means we can strip 3rd coordinate if needed
     847                for j in range(l):
     848                    isOut=outside_polygon(blCat[n1][j][0:2], bounding_polygon)
     849                    if(len(isOut)>0):
     850                        msg='Breakline/riverwall point '+str(blCat[n1][j][0:2]) +' on '+ n1+\
     851                            ' is outside the bounding polygon.\n'+\
     852                            'Check that it exceeds the bounding polygon by a distance < point_movement_threshold \n'+\
     853                            ' so it can be moved back onto the polygon'
     854                        print 'Polygon\n '
     855                        print bounding_polygon
     856                        print 'Line \n'
     857                        print blCat[n1]
     858                        raise Exception, msg
    822859     
    823860        return [bounding_polygon, breakLines, riverWalls]
     
    961998        raise ImportError, msg
    962999   
    963     def insert_intersection_point(intersectionPt, line_pts, point_movement_threshold):
     1000    def insert_intersection_point(intersectionPt, line_pts, point_movement_threshold,verbose=False):
    9641001        raise ImportError, msg
    9651002
  • trunk/anuga_core/source/anuga/utilities/test_spatialInputUtil.py

    r9219 r9242  
    362362            assert True
    363363
    364         #print 'After first add_iinersections'
    365364        #################################################################
    366365        # Fix the riverwall, and it should work
    367         riverWalls={ 'rw1': [[-0.01, 8., 2.],[10.01, 4., 3.]],
    368                      'rw2': [[5.3, -0.01, 1.], [10., 10.01, 2.]] }
     366        riverWalls={ 'rw1': [[-0.000001, 8., 2.],[10.0000001, 4., 3.]]
     367                      }
    369368        # This should work
    370369        newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
    371370                                breakLines, riverWalls, point_movement_threshold=0.02,\
    372371                                verbose=verbose)
     372
     373        # There should be several new points on breakLines + riverWalls
     374        assert newBL['bl1'][1]==newBL['bl2'][1]
     375        assert newRW['rw1'][2][0:2]==newBL['bl1'][2]
     376        assert newRW['rw1'][1][0:2]==newBL['bl2'][2]
     377        # rw1 x/y coords are close to y=8-3/10*x
     378        #     x/z coords are close to z=2+x/10
     379        assert numpy.allclose(newRW['rw1'][1][2], newRW['rw1'][1][0]/10.+2)
     380        assert numpy.allclose(newRW['rw1'][2][2], newRW['rw1'][2][0]/10.+2)
    373381
    374382        return
Note: See TracChangeset for help on using the changeset viewer.