svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / core / TurnUtil.java @ 8215
History | View | Annotate | Download (3.71 KB)
1 |
/*
|
---|---|
2 |
* Created on 20-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: TurnUtil.java 8215 2006-10-20 19:54:01Z azabala $
|
47 |
* $Log$
|
48 |
* Revision 1.1 2006-10-20 19:54:01 azabala
|
49 |
* *** empty log message ***
|
50 |
*
|
51 |
*
|
52 |
*/
|
53 |
package com.iver.cit.gvsig.graph.core; |
54 |
|
55 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
56 |
import com.vividsolutions.jts.geom.Coordinate; |
57 |
import com.vividsolutions.jts.geom.Geometry; |
58 |
import com.vividsolutions.jts.geom.LineString; |
59 |
import com.vividsolutions.jts.geom.MultiLineString; |
60 |
|
61 |
public class TurnUtil { |
62 |
public static final int TURN_RIGHT = 0; |
63 |
public static final int TURN_LEFT = 1; |
64 |
public static final int GO_STRAIGH_ON = 2; |
65 |
public static final int TURN_U = 4; |
66 |
|
67 |
|
68 |
public static boolean checkIsLine(Geometry geometry){ |
69 |
if(geometry instanceof LineString || geometry instanceof MultiLineString) |
70 |
return true; |
71 |
else
|
72 |
return false; |
73 |
} |
74 |
|
75 |
|
76 |
|
77 |
public static final int getDirection(IFeature feature1, IFeature feature2){ |
78 |
Geometry geom1 = feature1.getGeometry().toJTSGeometry(); |
79 |
Geometry geom2 = feature2.getGeometry().toJTSGeometry(); |
80 |
if(!checkIsLine(geom1))
|
81 |
return -1; |
82 |
if(!checkIsLine(geom2))
|
83 |
return -1; |
84 |
Coordinate[] coords1 = geom1.getCoordinates();
|
85 |
Coordinate p0 = coords1[coords1.length -2];
|
86 |
Coordinate p1 = coords1[coords1.length -1];
|
87 |
Coordinate[] coords2 = geom2.getCoordinates();
|
88 |
Coordinate p2 = coords2[0];
|
89 |
|
90 |
double dx = p1.x - p0.x;
|
91 |
double dy = p1.y - p0.y;
|
92 |
double angle1 = Math.atan2(dy, dx); |
93 |
|
94 |
dx = p2.x - p1.x; |
95 |
dy = p2.y - p1.y; |
96 |
double angle2 = Math.atan2(dy, dx); |
97 |
|
98 |
double turnAngle = angle1 - angle2;
|
99 |
double deegreeAngle = Math.toDegrees(turnAngle); |
100 |
|
101 |
if (Math.abs(deegreeAngle) > 180){ |
102 |
if(deegreeAngle < 0){ |
103 |
deegreeAngle = 360 - Math.abs(deegreeAngle); |
104 |
}else{
|
105 |
deegreeAngle = deegreeAngle - 360;
|
106 |
} |
107 |
} |
108 |
|
109 |
if(Math.abs(deegreeAngle) <= 30) |
110 |
return GO_STRAIGH_ON;
|
111 |
else if(deegreeAngle <= 0) |
112 |
return TURN_LEFT;
|
113 |
else
|
114 |
return TURN_RIGHT;
|
115 |
} |
116 |
|
117 |
|
118 |
/**
|
119 |
*
|
120 |
* Code extracted from JUMP
|
121 |
*
|
122 |
* Returns the angle between two vectors.
|
123 |
* @param a1 the angle of one vector, between -Pi and Pi
|
124 |
* @param a2 the angle of the other vector, between -Pi and Pi
|
125 |
* @return the angle (in radians) between the two vectors, between 0 and Pi
|
126 |
*/
|
127 |
// public static double diff(double a1, double a2) {
|
128 |
// double da;
|
129 |
//
|
130 |
// if (a1 < a2) {
|
131 |
// da = a2 - a1;
|
132 |
// } else {
|
133 |
// da = a1 - a2;
|
134 |
// }
|
135 |
//
|
136 |
// if (da > Math.PI) {
|
137 |
// da = (2 * Math.PI) - da;
|
138 |
// }
|
139 |
// return da;
|
140 |
// }
|
141 |
} |
142 |
|