root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / topology / algorithms / SnapCGAlgorithms.java @ 8004
History | View | Annotate | Download (5.32 KB)
1 |
/*
|
---|---|
2 |
* Created on 06-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: SnapCGAlgorithms.java 8004 2006-10-09 19:10:56Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.1 2006-10-09 19:10:56 azabala
|
49 |
* First version in CVS
|
50 |
*
|
51 |
*
|
52 |
*/
|
53 |
package com.iver.cit.gvsig.topology.algorithms; |
54 |
|
55 |
import com.iver.cit.gvsig.topology.SnapLineIntersector; |
56 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
57 |
import com.vividsolutions.jts.algorithm.RobustDeterminant; |
58 |
import com.vividsolutions.jts.geom.Coordinate; |
59 |
|
60 |
public class SnapCGAlgorithms extends CGAlgorithms { |
61 |
/**
|
62 |
* Test whether a point lies on the line segments defined by a
|
63 |
* list of coordinates.
|
64 |
*
|
65 |
* @return true true if
|
66 |
* the point is a vertex of the line or lies in the interior of a line
|
67 |
* segment in the linestring
|
68 |
*/
|
69 |
public static boolean isOnLine(Coordinate p, Coordinate[] pt, double snapTol) { |
70 |
SnapLineIntersector lineIntersector = new SnapLineIntersector(snapTol);
|
71 |
for (int i = 1; i < pt.length; i++) { |
72 |
Coordinate p0 = pt[i - 1];
|
73 |
Coordinate p1 = pt[i]; |
74 |
lineIntersector.computeIntersection(p, p0, p1); |
75 |
if (lineIntersector.hasIntersection()) {
|
76 |
return true; |
77 |
} |
78 |
} |
79 |
return false; |
80 |
} |
81 |
public static boolean isPointInRing(Coordinate p, Coordinate[] ring, double snapTolerance) { |
82 |
/*
|
83 |
* For each segment l = (i-1, i), see if it crosses ray from test point in positive x direction.
|
84 |
*/
|
85 |
int crossings = 0; // number of segment/ray crossings |
86 |
|
87 |
SnapLineIntersector lineIntersector = new SnapLineIntersector(snapTolerance);
|
88 |
|
89 |
for (int i = 1; i < ring.length; i++) { |
90 |
int i1 = i - 1; |
91 |
Coordinate p1 = ring[i]; |
92 |
Coordinate p2 = ring[i1]; |
93 |
|
94 |
lineIntersector.computeIntersection(p, p1, p2); |
95 |
if (lineIntersector.hasIntersection()) {
|
96 |
crossings ++; |
97 |
} |
98 |
|
99 |
// if (((p1.y > (p.y - snapTolerance)) && (p2.y <= (p.y + snapTolerance))) ||
|
100 |
// ((p2.y > (p.y - snapTolerance)) && (p1.y <= (p.y) + snapTolerance))) {//si no se cumple, no pueden intersectar
|
101 |
//
|
102 |
// double x1 = p1.x - p.x;
|
103 |
// double y1 = p1.y - p.y;
|
104 |
// double x2 = p2.x - p.x;
|
105 |
// double y2 = p2.y - p.y;
|
106 |
// /*
|
107 |
// * segment straddles x axis, so compute intersection with x-axis.
|
108 |
// */
|
109 |
// double xInt = RobustDeterminant.signOfDet2x2(x1, y1, x2, y2) / (y2 - y1);
|
110 |
// //xsave = xInt;
|
111 |
// /*
|
112 |
// * crosses ray if strictly positive intersection.
|
113 |
// */
|
114 |
// if (xInt > 0.0) {
|
115 |
// crossings++;
|
116 |
// }
|
117 |
// }
|
118 |
} |
119 |
/*
|
120 |
* p is inside if number of crossings is odd.
|
121 |
*/
|
122 |
if ((crossings % 2) == 1) { |
123 |
return true; |
124 |
} |
125 |
else {
|
126 |
return false; |
127 |
} |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Computes the orientation of a point q to the directed line segment p1-p2.
|
132 |
* The orientation of a point relative to a directed line segment indicates
|
133 |
* which way you turn to get to q after travelling from p1 to p2.
|
134 |
*
|
135 |
* @return 1 if q is counter-clockwise from p1-p2
|
136 |
* @return -1 if q is clockwise from p1-p2
|
137 |
* @return 0 if q is collinear with p1-p2
|
138 |
*/
|
139 |
public static int computeOrientation(Coordinate p1, Coordinate p2, Coordinate q) { |
140 |
return orientationIndex(p1, p2, q);
|
141 |
} |
142 |
/**
|
143 |
* Returns the index of the direction of the point <code>q</code>
|
144 |
* relative to a
|
145 |
* vector specified by <code>p1-p2</code>.
|
146 |
*
|
147 |
* @param p1 the origin point of the vector
|
148 |
* @param p2 the final point of the vector
|
149 |
* @param q the point to compute the direction to
|
150 |
*
|
151 |
* @return 1 if q is counter-clockwise (left) from p1-p2
|
152 |
* @return -1 if q is clockwise (right) from p1-p2
|
153 |
* @return 0 if q is collinear with p1-p2
|
154 |
*/
|
155 |
public static int orientationIndex(Coordinate p1, Coordinate p2, Coordinate q) { |
156 |
// travelling along p1->p2, turn counter clockwise to get to q return 1,
|
157 |
// travelling along p1->p2, turn clockwise to get to q return -1,
|
158 |
// p1, p2 and q are colinear return 0.
|
159 |
double dx1 = p2.x - p1.x;
|
160 |
double dy1 = p2.y - p1.y;
|
161 |
double dx2 = q.x - p2.x;
|
162 |
double dy2 = q.y - p2.y;
|
163 |
return RobustDeterminant.signOfDet2x2(dx1, dy1, dx2, dy2);
|
164 |
} |
165 |
|
166 |
|
167 |
} |
168 |
|