Changeset 2617 in lmdz_wrf


Ignore:
Timestamp:
Jun 19, 2019, 3:53:19 AM (5 years ago)
Author:
lfita
Message:

Adding:

  • `crossingpoints_polys': Subroutine to provide the crossing points between two polygons
  • starting... `pile_polygons'
Location:
trunk/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/geometry_tools.py

    r2616 r2617  
    1717import generic_tools as gen
    1818import numpy.ma as ma
    19 import module_ForSci as sci
     19import module_ForSci as fsci
    2020
    2121errormsg = 'ERROR -- error -- ERROR -- error'
     
    15141514    return Npts, finalcutpolygon
    15151515
    1516 def cut_between_xpolygon_old(polygon, xval1, xval2, Nadd=20):
    1517     """ Function to cut a polygon between 2 given value of the x-axis
    1518       polygon: polygon to cut
    1519       xval1: first value to use to cut the polygon
    1520       xval2: first value to use to cut the polygon
    1521       Nadd: additional points to add to draw the line (20, default)
    1522     """
    1523     fname = 'cut_betwen_xpolygon'
    1524 
    1525     N = polygon.shape[0]
    1526 
    1527     ipt = None
    1528     ept = None
    1529 
    1530     dx = np.zeros((2), dtype=np.float)
    1531     dy = np.zeros((2), dtype=np.float)
    1532     icut = np.zeros((2), dtype=int)
    1533     ecut = np.zeros((2), dtype=int)
    1534     ipt = np.zeros((2,2), dtype=np.float)
    1535     ept = np.zeros((2,2), dtype=np.float)
    1536 
    1537     if xval1 > xval2:
    1538         print errormsg
    1539         print '  ' + fname + ': wrong between cut values !!'
    1540         print '     it is expected xval1 < xval2'
    1541         print '     values provided xval1: (', xval1, ')> xval2 (', xval2, ')'
    1542         quit(-1)
    1543 
    1544     xvals = [xval1, xval2]
    1545 
    1546     for ic in range(2):
    1547         xval = xvals[ic]
    1548         if type(polygon) == type(gen.mamat):
    1549             # Assuming clockwise polygons
    1550             for ip in range(N-1):
    1551                 if not polygon.mask[ip,0]:
    1552                     eep = ip + 1
    1553                     if eep == N: eep = 0
    1554      
    1555                     if (polygon[ip,1] <= xval and polygon[eep,1] > xval) or          \
    1556                       (polygon[ip,1] < xval and polygon[eep,1] >= xval):
    1557                         icut[ic] = ip
    1558                         dx[ic] = polygon[eep,1] - polygon[ip,1]
    1559                         dy[ic] = polygon[eep,0] - polygon[ip,0]
    1560                         dd = xval - polygon[ip,1]
    1561                         ipt[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
    1562 
    1563                     if (polygon[ip,1] >= yval and polygon[eep,1] < xval) or          \
    1564                       (polygon[ip,1] > yval and polygon[eep,1] <= xval):
    1565                         ecut[ic] = ip
    1566                         dx[ic] = polygon[eep,1] - polygon[ip,1]
    1567                         dy[ic] = polygon[eep,0] - polygon[ip,0]
    1568                         dd = xval - polygon[ip,1]
    1569                         ept[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
    1570         else:
    1571             # Assuming clockwise polygons
    1572             for ip in range(N-1):
    1573                 eep = ip + 1
    1574                 if eep == N: eep = 0
    1575      
    1576                 if (polygon[ip,1] <= xval and polygon[eep,1] > xval) or              \
    1577                   (polygon[ip,1] < xval and polygon[eep,1] >= xval):
    1578                     icut[ic] = ip
    1579                     dx[ic] = polygon[eep,1] - polygon[ip,1]
    1580                     dy[ic] = polygon[eep,0] - polygon[ip,0]
    1581                     dd = xval - polygon[ip,1]
    1582                     print 'Lluis ip', ip, 'poly:', polygon[ip,:], 'xval:', xval, 'ip+1', polygon[eep,:]
    1583                     print '  dx:', dx, 'dy:', dy, 'dd', dd
    1584                     ipt[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
    1585 
    1586                 if (polygon[ip,1] >= xval and polygon[eep,1] < xval) or              \
    1587                   (polygon[ip,1] > xval and polygon[eep,1] <= xval):
    1588                     ecut[ic] = ip
    1589                     dx[ic] = polygon[eep,1] - polygon[ip,1]
    1590                     dy[ic] = polygon[eep,0] - polygon[ip,0]
    1591                     dd = xval - polygon[ip,1]
    1592                     if dx[ic] == 0.:
    1593                         ept[ic,:] = [polygon[eep,0], xval]
    1594                     else:
    1595                         ept[ic,:] = [polygon[ip,0]+dy[ic]*dd/dx[ic], xval]
    1596 
    1597         if ipt is None or ept is None:
    1598             print errormsg
    1599             print '  ' + fname + ': no cutting for polygon at x=', xval, '!!'
    1600 
    1601     Npts = icut[1] - icut[0] + Nadd + ecut[0] - ecut[1]
    1602     cutpolygon = np.zeros((Npts,2), dtype=np.float)
    1603     cutpolygon[0,:] = ipt[0,:]
    1604     cutpolygon[1:icut[1]-icut[0]+1,:] = polygon[icut[0]+1:icut[1]+1,:]
    1605     iip = icut[1]-icut[0]
    1606 
    1607     # cutting lines
    1608     Nadd2 = int(Nadd/2)
    1609     cutlines = np.zeros((2,Nadd2,2), dtype=np.float)
    1610 
    1611     for ic in range(2):
    1612         print ic, 'Lluis ipt:', ipt[ic,:], 'ept:', ept[ic,:]
    1613         dx = (ept[ic,1] - ipt[ic,1])/(Nadd2-2)
    1614         dy = (ept[ic,0] - ipt[ic,0])/(Nadd2-2)
    1615         print '    dx:', dx, 'dy', dy
    1616         cutlines[ic,0,:] = ipt[ic,:]
    1617         for ip in range(1,Nadd2-1):
    1618             cutlines[ic,ip,:] = ipt[ic,:] + np.array([dy*ip,dx*ip])
    1619         cutlines[ic,Nadd2-1,:] = ept[ic,:]
    1620 
    1621     cutpolygon[iip:iip+Nadd2,:] = cutlines[1,:,:]
    1622     iip = iip + Nadd2
    1623     cutpolygon[iip:iip+(ecut[0]-ecut[1]),:] = polygon[ecut[1]+1:ecut[0]+1,:]
    1624     iip = iip + ecut[0]-ecut[1]
    1625     cutpolygon[iip:iip+Nadd2,:] = cutlines[0,::-1,:]
    1626 
    1627     cutpolygon = ma.masked_equal(cutpolygon, gen.fillValueF)
    1628 
    1629     return Npts, cutpolygon
     1516def pile_polygons(polyns, polygons):
     1517    """ Function to pile polygons one over the following one
     1518      polyns: ordered list of polygons. First over all. last below all
     1519      polygons: dictionary with the polygons
     1520    >>> pns = ['sqra', 'sqrb']
     1521    >>> polya = np.array([[-0.5, -0.75], [0.5, -0.75], [0.5, 0.75], [-0.5, 0.75]])
     1522    >>> polyb = np.array([[-0.75, -0.5], [0.75, -0.5], [0.75, 0.5], [-0.75, 0.5]])
     1523    >>> plgs = {'sqra': polya, 'sqrb': polyb}
     1524    >>> pile_polygons(pns, plgs)
     1525
     1526    """
     1527    fname = 'pile_polygons'
     1528
     1529    Npolys = len(polygons)
     1530    ipoly = polygons[polyns[Npolys-1]]
     1531    iNpts = ipoly.shape[0]
     1532    pilepolygons = ipoly
     1533
     1534    pilesortpolys = polyns[::-1]
     1535    pilesortpolys.remove(polyns[Npolys-1])
     1536    for polyn in pilesortpolys:
     1537        poly = polygons[polyn]
     1538        Npts = poly.shape[0]
     1539        print ' LLuis shapes ipoly', ipoly.shape, 'poly', poly.shape
     1540        Nint, inti, intp, pts = fsci.module_scientific.crossingpoints_polys(         \
     1541          nvertexa=iNpts, nvertexb=Npts, nvertexab=iNpts*Npts, polya=ipoly,          \
     1542          polyb=poly)
     1543        print Nint
     1544        for ip in range(Nint):
     1545            print ip, ':', inti[ip], intp[ip], '=', pts[ip]
     1546       
     1547    return pilepolygons
     1548
     1549#pns = ['sqra', 'sqrb']
     1550#polya = np.array([[-0.5, -0.75], [0.5, -0.75], [0.5, 0.75], [-0.5, 0.75]])
     1551#polyb = np.array([[-0.75, -0.5], [0.75, -0.5], [0.75, 0.5], [-0.75, 0.5]])
     1552#plgs = {'sqra': polya, 'sqrb': polyb}
     1553#pile_polygons(pns, plgs)
     1554
     1555
    16301556
    16311557####### ###### ##### #### ### ## #
  • trunk/tools/module_scientific.f90

    r2562 r2617  
    1717! clean_polygons: Subroutine to clean polygons from non-used paths, polygons only left as path since they are inner path of a hole
    1818! coincident_polygon: Subroutine to provide the intersection polygon between two polygons
     19! crossingpoints_polys: Subroutine to provide the crossing points between two polygons
    1920! distanceRK: Function to provide the distance between two points
    2021! FindMinimumR_K*: Function returns the location of the minimum in the section between Start and End.
     
    75077508  END SUBROUTINE multi_spaceweightcount_in3DRK3_slc3v3
    75087509
     7510  SUBROUTINE crossingpoints_polys(NvertexA, NvertexB, NvertexAB, polyA, polyB, Ncross, crossA, crossB,&
     7511    crosspoints)
     7512  ! Subroutine to provide the crossing points between two polygons
     7513
     7514    IMPLICIT NONE
     7515
     7516    INTEGER, INTENT(in)                                  :: NvertexA, NvertexB, NvertexAB
     7517    REAL(r_k), DIMENSION(NvertexA,2), INTENT(in)         :: polyA
     7518    REAL(r_k), DIMENSION(NvertexB,2), INTENT(in)         :: polyB
     7519    INTEGER, INTENT(out)                                 :: Ncross
     7520    INTEGER, DIMENSION(NvertexAB), INTENT(out)           :: crossA, crossB
     7521    REAL(r_k), DIMENSION(NvertexAB,2), INTENT(out)       :: crosspoints
     7522
     7523! Local
     7524    INTEGER                                              :: iA, iB, icoin, ii
     7525    REAL(r_k), DIMENSION(2,2)                            :: face1, face2
     7526    INTEGER                                              :: intersect
     7527    REAL(r_k), DIMENSION(2)                              :: intersectpt
     7528
     7529
     7530!!!!!!! variables
     7531! NvertexA: number of vertexs polygon A
     7532! NvertexB: number of vertexs polygon B
     7533! polyA: pairs of coordinates for the polygon A (clockwise)
     7534! polyB: pairs of coordinates for the polygon B (clockwise)
     7535! Ncross: number of crossing vertices
     7536! crossA: vertex of polygon A where there is a crossing
     7537! crossB: vertex of polygon B where there is a crossing
     7538! crosspoints: coordinates of the crossing points
     7539
     7540    fname = 'crossingpoints_polys'
     7541
     7542    Ncross = 0
     7543    crossA = 0
     7544    crossB = 0
     7545    crosspoints = 0.
     7546
     7547    ! Looping
     7548    DO iA=1, NvertexA-1
     7549      face1(1,:) = polyA(iA,:)
     7550      face1(2,:) = polyA(iA+1,:)
     7551      DO iB=1, NvertexB-1
     7552        face2(1,:) = polyB(iB,:)
     7553        face2(2,:) = polyB(iB+1,:)
     7554        CALL intersectfaces(face1, face2, intersect, intersectpt)
     7555        IF (intersect /= 0) THEN
     7556          Ncross = Ncross + 1
     7557          crossA(Ncross) = iA
     7558          crossB(Ncross) = iB
     7559          crosspoints(Ncross,:) = intersectpt
     7560        END IF
     7561      END DO
     7562      ! Last face B
     7563      iB = NvertexB
     7564      face2(1,:) = polyB(iB,:)
     7565      face2(2,:) = polyB(1,:)
     7566      CALL intersectfaces(face1, face2, intersect, intersectpt)
     7567      IF (intersect /= 0) THEN
     7568        Ncross = Ncross + 1
     7569        crossA(Ncross) = iA
     7570        crossB(Ncross) = iB
     7571        crosspoints(Ncross,:) = intersectpt
     7572      END IF
     7573    END DO
     7574
     7575    ! Last face A
     7576    iA = NvertexA
     7577    face1(1,:) = polyA(iA,:)
     7578    face1(2,:) = polyA(1,:)
     7579    DO iB=1, NvertexB-1
     7580      face2(1,:) = polyB(iB,:)
     7581      face2(2,:) = polyB(iB+1,:)
     7582      CALL intersectfaces(face1, face2, intersect, intersectpt)
     7583      IF (intersect /= 0) THEN
     7584        Ncross = Ncross + 1
     7585        crossA(Ncross) = iA
     7586        crossB(Ncross) = iB
     7587        crosspoints(Ncross,:) = intersectpt
     7588      END IF
     7589    END DO
     7590    ! Last face B
     7591    iB = NvertexB
     7592    face2(1,:) = polyB(iB,:)
     7593    face2(2,:) = polyB(1,:)
     7594    CALL intersectfaces(face1, face2, intersect, intersectpt)
     7595    IF (intersect /= 0) THEN
     7596      Ncross = Ncross + 1
     7597      crossA(Ncross) = iA
     7598      crossB(Ncross) = iB
     7599      crosspoints(Ncross,:) = intersectpt
     7600    END IF
     7601
     7602  END SUBROUTINE crossingpoints_polys
     7603
    75097604END MODULE module_scientific
Note: See TracChangeset for help on using the changeset viewer.