Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / vividsolutions / jts / geomgraph / index / SnapSimpleMCSweepLineIntersector.java @ 9178

History | View | Annotate | Download (5.13 KB)

1
/*
2
 * Created on 02-oct-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
*
46
* $Id: SnapSimpleMCSweepLineIntersector.java 9178 2006-12-04 19:30:23Z azabala $
47
* $Log$
48
* Revision 1.3  2006-12-04 19:30:23  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.1  2006/10/05 19:20:57  azabala
52
* first version in cvs
53
*
54
* Revision 1.1  2006/10/02 19:06:26  azabala
55
* First version in CVS
56
*
57
*
58
*/
59
package com.vividsolutions.jts.geomgraph.index;
60

    
61
import java.util.ArrayList;
62
import java.util.Collections;
63
import java.util.Iterator;
64
import java.util.List;
65

    
66
import com.vividsolutions.jts.geomgraph.Edge;
67
import com.vividsolutions.jts.geomgraph.index.MonotoneChain;
68
import com.vividsolutions.jts.geomgraph.index.MonotoneChainEdge;
69
import com.vividsolutions.jts.geomgraph.index.SegmentIntersector;
70
import com.vividsolutions.jts.geomgraph.index.SimpleMCSweepLineIntersector;
71
import com.vividsolutions.jts.geomgraph.index.SweepLineEvent;
72

    
73
public class SnapSimpleMCSweepLineIntersector extends
74
                SimpleMCSweepLineIntersector {
75
        
76
         List events = new ArrayList();
77
          // statistics information
78
          int nOverlaps;
79

    
80
        
81
          public SnapSimpleMCSweepLineIntersector() {
82
          }
83

    
84
          
85
          public void computeIntersections(List edges, 
86
                          SegmentIntersector si, 
87
                          boolean testAllSegments)
88
          {
89
            if (testAllSegments)
90
              add(edges, null);
91
            else
92
              add(edges);
93
            computeIntersections(si);
94
          }
95

    
96
          public void computeIntersections(List edges0, List edges1, SegmentIntersector si)
97
          {
98
            add(edges0, edges0);
99
            add(edges1, edges1);
100
            computeIntersections(si);
101
          }
102

    
103
          private void add(List edges)
104
          {
105
            for (Iterator i = edges.iterator(); i.hasNext(); ) {
106
              Edge edge = (Edge) i.next();
107
              // edge is its own group
108
              add(edge, edge);
109
            }
110
          }
111
          private void add(List edges, Object edgeSet)
112
          {
113
            for (Iterator i = edges.iterator(); i.hasNext(); ) {
114
              Edge edge = (Edge) i.next();
115
              add(edge, edgeSet);
116
            }
117
          }
118

    
119
          private void add(Edge edge, Object edgeSet)
120
          {
121
            MonotoneChainEdge mce = edge.getMonotoneChainEdge();
122
            int[] startIndex = mce.getStartIndexes();
123
            for (int i = 0; i < startIndex.length - 1; i++) {
124
              MonotoneChain mc = new MonotoneChain(mce, i);
125
              SweepLineEvent insertEvent = new SweepLineEvent(edgeSet, 
126
                              mce.getMinX(i), 
127
                              null, mc);
128
              events.add(insertEvent);
129
              events.add(new SweepLineEvent(edgeSet, mce.getMaxX(i), insertEvent, mc));
130
            }
131
          }
132

    
133
          /**
134
           * Because Delete Events have a link to their corresponding Insert event,
135
           * it is possible to compute exactly the range of events which must be
136
           * compared to a given Insert event object.
137
           */
138
          private void prepareEvents()
139
          {
140
            Collections.sort(events);
141
            for (int i = 0; i < events.size(); i++ )
142
            {
143
              SweepLineEvent ev = (SweepLineEvent) events.get(i);
144
              if (ev.isDelete()) {
145
                ev.getInsertEvent().setDeleteEventIndex(i);
146
              }
147
            }
148
          }
149

    
150
          private void computeIntersections(SegmentIntersector si)
151
          {
152
            nOverlaps = 0;
153
            prepareEvents();
154

    
155
            for (int i = 0; i < events.size(); i++ )
156
            {
157
              SweepLineEvent ev = (SweepLineEvent) events.get(i);
158
              if (ev.isInsert()) {
159
                processOverlaps(i, ev.getDeleteEventIndex(), ev, si);
160
              }
161
            }
162
          }
163

    
164
          private void processOverlaps(int start, int end, SweepLineEvent ev0, SegmentIntersector si)
165
          {
166
            MonotoneChain mc0 = (MonotoneChain) ev0.getObject();
167
            /**
168
             * Since we might need to test for self-intersections,
169
             * include current insert event object in list of event objects to test.
170
             * Last index can be skipped, because it must be a Delete event.
171
             */
172
            for (int i = start; i < end; i++ ) {
173
              SweepLineEvent ev1 = (SweepLineEvent) events.get(i);
174
              if (ev1.isInsert()) {
175
                MonotoneChain mc1 = (MonotoneChain) ev1.getObject();
176
                // don't compare edges in same group
177
                // null group indicates that edges should be compared
178
                if (ev0.edgeSet == null || (ev0.edgeSet != ev1.edgeSet)) {
179
                  mc0.computeIntersections(mc1, si);
180
                  nOverlaps++;
181
                }
182
              }
183
            }
184
          }
185

    
186
}
187