Changeset 26820 in osm for applications/editors/josm/plugins/reltoolbox/src/relcontext/actions
- Timestamp:
- 2011-10-09T22:52:46+02:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/TheRing.java
r26816 r26820 28 28 29 29 public void collide( TheRing other ) { 30 List<Node> intersectionNodes = new ArrayList<Node>();31 List<RingSegment> segmentsList1 = new ArrayList<RingSegment>(segments);32 List<RingSegment> segmentsList2 = new ArrayList<RingSegment>(other.segments);33 30 boolean collideNoted = false; 34 for( int i = 0; i < segments List1.size(); i++ ) {35 if( !segments List1.get(i).isReference() )36 for( int j = 0; j < segmentsList2.size(); j++ ) {31 for( int i = 0; i < segments.size(); i++ ) { 32 if( !segments.get(i).isReference() ) { 33 for( int j = 0; j < other.segments.size(); j++ ) { 37 34 // not colliding referencing nodes: they've already collided, and 38 35 // there should be no more than two ways passing through two points. 39 if( ! segmentsList1.get(i).isReference() ) {40 intersectionNodes.clear();36 if( !other.segments.get(j).isReference() ) { 37 List<Node> intersectionNodes = new ArrayList<Node>(); 41 38 boolean colliding = false; 42 List<Node> nodes1 = segments List1.get(i).getNodes();43 List<Node> nodes2 = segmentsList2.get(j).getNodes();39 List<Node> nodes1 = segments.get(i).getNodes(); 40 List<Node> nodes2 = other.segments.get(j).getNodes(); 44 41 for( int ni = 0; ni < nodes2.size(); ni++ ) { 45 42 if( nodes1.contains(nodes2.get(ni)) != colliding ) { … … 51 48 intersectionNodes.add(nodes2.get(nodes2.size() - 1)); 52 49 // when an intersection of two rings spans a ring's beginning 53 if( segments List1.get(i).isRing() && segmentsList2.get(j).isRing() && intersectionNodes.contains(nodes2.get(0)) && intersectionNodes.contains(nodes2.get(nodes2.size() - 1)) ) {50 if( segments.get(i).isRing() && other.segments.get(j).isRing() && intersectionNodes.contains(nodes2.get(0)) && intersectionNodes.contains(nodes2.get(nodes2.size() - 1)) ) { 54 51 intersectionNodes.remove(0); 55 52 intersectionNodes.remove(intersectionNodes.size() - 1); … … 57 54 intersectionNodes.remove(0); 58 55 } 59 if( !collideNoted && !intersectionNodes.isEmpty() ) { 60 System.out.println("Rings " + this + " and " + other + " collide."); 61 collideNoted = true; 62 } 63 System.out.print("Intersection nodes for segments " + segmentsList1.get(i) + " and " + segmentsList2.get(j) + ": "); 56 System.out.print("Intersection nodes for segments " + segments.get(i) + " and " + other.segments.get(j) + ": "); 64 57 for( Node inode : intersectionNodes ) 65 58 System.out.print(inode.getUniqueId() + ","); … … 73 66 ni++; 74 67 } 75 // boolean thisWayIsReversed = !intersectionNodes.isEmpty() && nodes1.indexOf(intersectionNodes.get(0)) > nodes1.indexOf(intersectionNodes.get(1)); 76 // now split both ways at control points and remove duplicate parts 77 ni = 0; 78 while( ni + 1 < intersectionNodes.size() ) { 79 if( !segmentsList1.get(i).isReferencingEqual(segmentsList2.get(j)) ) { 80 boolean[] isarc = new boolean[] { 81 segments.size() == 1 && !segments.get(0).isRing(), 82 other.segments.size() == 1 && !other.segments.get(0).isRing() 83 }; 84 RingSegment segment = splitRingAt(i, intersectionNodes.get(ni), intersectionNodes.get(ni + 1)); 85 RingSegment otherSegment = other.splitRingAt(j, intersectionNodes.get(ni), intersectionNodes.get(ni + 1)); 86 if( !isarc[0] && !isarc[1] ) { 87 if( segments.size() > 2 ) 68 if( intersectionNodes.size() > 1 ) { 69 if( !collideNoted ) { 70 System.out.println("Rings for ways " + source.getUniqueId() + " and " + other.source.getUniqueId() + " collide."); 71 collideNoted = true; 72 } 73 // now split both ways at control points and remove duplicate parts 74 boolean[] isarc = new boolean[] { 75 segments.size() == 1 && !segments.get(0).isRing(), 76 other.segments.size() == 1 && !other.segments.get(0).isRing() 77 }; 78 RingSegment segment = splitRingAt(i, intersectionNodes.get(0), intersectionNodes.get(1)); 79 RingSegment otherSegment = other.splitRingAt(j, intersectionNodes.get(0), intersectionNodes.get(1)); 80 if( !isarc[0] && !isarc[1] ) { 81 if( segments.size() > 2 && other.segments.size() > 2 ) 82 segment.makeReference(otherSegment); 83 else { 84 System.out.println("Starting complex procedure. Rings: " + this + " and " + other); 85 // this ring was a ring, and we're not sure "segment" is a correct segment 86 // actually, we're not sure always 87 if( segments.size() == 2 ) 88 segment = segments.get(0); 89 if( other.segments.size() == 2 ) 90 otherSegment = other.segments.get(0); 91 System.out.println("segment="+segment + ", otherSegment=" + otherSegment); 92 93 if( areSegmentsEqual(segment, otherSegment) ) 88 94 segment.makeReference(otherSegment); 95 else if( segments.size() == 2 && areSegmentsEqual(segments.get(1), otherSegment) ) 96 segments.get(1).makeReference(otherSegment); 97 else if( areSegmentsEqual(segment, other.segments.get(1)) ) 98 segment.makeReference(other.segments.get(1)); 99 else 100 segments.get(1).makeReference(other.segments.get(1)); 101 } 102 } else { 103 // 1. A ring is an arc. It should have only 2 segments after this 104 // 2. But it has one, so add otherSegment as the second. 105 // 3. determine which segment! 106 if( isarc[0] ) { 107 if( other.segments.size() > 2 ) 108 segments.add(new RingSegment(otherSegment)); 89 109 else { 90 // this ring was a ring, and we're not sure "segment" is a correct segment 91 if( segments.get(0).getNodes().size() == otherSegment.getNodes().size() 92 && (segments.get(0).getNodes().get(1).equals(otherSegment.getNodes().get(1))) 93 || (segments.get(0).getNodes().get(segments.get(0).getNodes().size() - 2).equals(otherSegment.getNodes().get(1))) ) 94 segments.get(0).makeReference(otherSegment); 95 else 96 segments.get(1).makeReference(otherSegment); 110 // choose between 2 segments 111 int segmentToAdd = whichSegmentIsCloser(segments.get(0), other.segments.get(0), other.segments.get(1)); 112 segments.add(new RingSegment(other.segments.get(segmentToAdd))); 97 113 } 98 114 } else { 99 // 1. A ring is an arc. It should have only 2 segments after this 100 // 2. But it has one, so add otherSegment as the second. 101 // 3. determine which segment! 102 if( isarc[0] ) { 103 if( other.segments.size() > 2 ) 104 segments.add(new RingSegment(otherSegment)); 105 else { 106 // choose between 2 segments 107 List<Node> testRing = new ArrayList<Node>(segments.get(0).getNodes()); 108 closePolygon(testRing, other.segments.get(0).getNodes()); 109 int segmentToAdd = segmentInsidePolygon(other.segments.get(1).getNodes().get(0), 110 other.segments.get(1).getNodes().get(1), testRing) ? 1 : 0; 111 segments.add(new RingSegment(other.segments.get(segmentToAdd))); 112 } 113 } else if( segments.size() > 2 ) 115 if( segments.size() > 2 ) 114 116 other.segments.add(new RingSegment(segment)); 115 117 else { 116 118 // choose between 2 segments 117 List<Node> testRing = new ArrayList<Node>(other.segments.get(0).getNodes()); 118 closePolygon(testRing, segments.get(0).getNodes()); 119 int segmentToAdd = segmentInsidePolygon(segments.get(1).getNodes().get(0), 120 segments.get(1).getNodes().get(1), testRing) ? 1 : 0; 119 int segmentToAdd = whichSegmentIsCloser(other.segments.get(0), segments.get(0), segments.get(1)); 121 120 other.segments.add(new RingSegment(segments.get(segmentToAdd))); 122 121 } 123 122 } 124 123 } 125 ni += 2;126 124 } 127 125 } 128 126 } 129 } 127 if( segments.get(i).isReference() ) 128 break; 129 } 130 } 131 } 132 133 private int whichSegmentIsCloser( RingSegment base, RingSegment test0, RingSegment test1 ) { 134 List<Node> testRing = new ArrayList<Node>(base.getNodes()); 135 closePolygon(testRing, test1.getNodes()); 136 return segmentInsidePolygon(test1.getNodes().get(0), test1.getNodes().get(1), testRing) ? 1 : 0; 137 } 138 139 private boolean areSegmentsEqual( RingSegment seg1, RingSegment seg2 ) { 140 List<Node> nodes1 = seg1.getNodes(); 141 List<Node> nodes2 = seg2.getNodes(); 142 int size = nodes1.size(); 143 if( size != nodes2.size() ) 144 return false; 145 boolean reverse = size > 1 && !nodes1.get(0).equals(nodes2.get(0)); 146 for( int i = 0; i < size; i++ ) 147 if( !nodes1.get(i).equals(nodes2.get(reverse ? size-1-i : i)) ) 148 return false; 149 return true; 130 150 } 131 151 … … 159 179 segments.add(pos++, thirdPart); 160 180 RingSegment result = isRing || secondPart == null ? segment : secondPart; 161 System.out.println("Returning segment " + result);181 // System.out.println("Returning segment " + result); 162 182 return result; 163 183 } … … 458 478 459 479 public void makeReference( RingSegment segment ) { 480 System.out.println(this + " was made a reference to " + segment); 460 481 this.nodes = null; 461 482 this.references = segment;
Note:
See TracChangeset
for help on using the changeset viewer.