Ignore:
Timestamp:
2012-04-08T09:29:08+02:00 (13 years ago)
Author:
akks
Message:

'Utilsplugin2: all inside multipolygon selection - fixed'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/selection/NodeWayUtils.java

    r28028 r28222  
    359359            List<Node> polyNodes = way.getNodes();
    360360            // converts all points to EastNorth
    361             for (Node n: polyNodes) polyPoints.add(n.getEastNorth()); 
     361            for (Node n: polyNodes) polyPoints.add(n.getEastNorth());
     362            polyPoints.add(null); // next segment indicator
    362363        }
    363364       
     
    368369        for (Node n : searchNodes) {
    369370            //if (Geometry.nodeInsidePolygon(n, polyNodes)) {
    370             if (NodeWayUtils.isPointInsidePolygon(n.getEastNorth(), polyPoints)>0) {
     371            if (NodeWayUtils.isPointInsidePolygon(n.getEastNorth(), polyPoints)) {
    371372                newestNodes.add(n);
    372373            }
     
    392393        BBox box = way.getBBox();
    393394        List<Node> polyNodes = way.getNodes();
    394         List<EastNorth> polyPoints = new ArrayList<EastNorth>(polyNodes.size());
     395        List<EastNorth> polyPoints = new ArrayList<EastNorth>(polyNodes.size()+5);
    395396       
    396397        // converts all points to EastNorth
     
    402403        for (Node n : searchNodes) {
    403404            //if (Geometry.nodeInsidePolygon(n, polyNodes)) {
    404             if (NodeWayUtils.isPointInsidePolygon(n.getEastNorth(), polyPoints)>0) {
     405            if (NodeWayUtils.isPointInsidePolygon(n.getEastNorth(), polyPoints)) {
    405406                newestNodes.add(n);
    406407            }
     
    422423    }
    423424   
     425    public static boolean isPointInsidePolygon(EastNorth point, List<EastNorth> polygonPoints) {
     426        int n = getRayIntersectionsCount(point, polygonPoints);
     427        if (n<0) return true; // we are near node or near edge
     428        return (n%2==1);
     429    }
     430   
    424431    /**
    425432     * @return 0 =  not inside polygon, 1 = strictly inside, 2 = near edge, 3 = near vertex
    426433     */
    427     public static int isPointInsidePolygon(EastNorth point, List<EastNorth> polygonPoints) {
     434    public static int getRayIntersectionsCount(EastNorth point, List<EastNorth> polygonPoints) {
    428435        int n=polygonPoints.size();
    429         EastNorth oldPoint = polygonPoints.get(n-1);
     436        if (point==null) return 0;
     437        EastNorth oldPoint = null;
     438                //polygonPoints.get(n-1);
    430439        double n1,n2,n3,e1,e2,e3,d;
    431440        int interCount=0;
    432441       
    433442        for (EastNorth curPoint : polygonPoints) {
     443            if (oldPoint==null || curPoint==null) {
     444                oldPoint = curPoint;
     445                continue;
     446            }
    434447            n1 = curPoint.north(); n2 = oldPoint.north();  n3 =  point.north();
    435448            e1 = curPoint.east(); e2 = oldPoint.east();  e3 =  point.east();
    436449           
    437             if (Math.abs(n1-n3)<1e-5 && Math.abs(e1-e3)<1e-5) return 3; // vertex
    438             if (Math.abs(n2-n3)<1e-5 && Math.abs(e2-e3)<1e-5) return 3; // vertex
     450            if (Math.abs(n1-n3)<1e-5 && Math.abs(e1-e3)<1e-5) return -3; // vertex
     451            if (Math.abs(n2-n3)<1e-5 && Math.abs(e2-e3)<1e-5) return -3; // vertex
    439452           
    440453            // looking at oldPoint-curPoint segment
     
    445458                    if (d<-1e-5) {
    446459                        interCount++; // there is OX intersecthion at e = (e1n2-e2n1)/(n2-n1) >=0
    447                     } else if (d<=1e-5) return 2; // boundary detected
     460                    } else if (d<=1e-5) return -2; // boundary detected
    448461                }
    449462            } else if (n1 == n2) {
    450463                if (n1 == n3) {
    451464                    e1-=e3; e2-=e3;
    452                     if ((e1 <=0 && e2 >= 0) || (e1 >=0 && e2 <= 0)) return 2;// boundary detected
     465                    if ((e1 <=0 && e2 >= 0) || (e1 >=0 && e2 <= 0)) return -2;// boundary detected
    453466                }
    454467            } else {
     
    458471                    if (d>1e-5) {
    459472                        interCount++; // there is OX intersecthion at e = (e1n2-e2n1)/(n2-n1) >=0
    460                     } else if (d>=-1e-5) return 2; // boundary detected
     473                    } else if (d>=-1e-5) return -2; // boundary detected
    461474                }
    462475            }
    463476            oldPoint = curPoint;
    464477        }
    465        // System.out.printf("Intersected intercount %d %s\n",interCount, point.toString());
    466         if (interCount%2 == 1) return 1; else return 0;
     478        // System.out.printf("Intersected intercount %d %s\n",interCount, point.toString());
     479        return interCount;
    467480    }
    468481   
Note: See TracChangeset for help on using the changeset viewer.