Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / graph / core / TurnUtil.java @ 8264

History | View | Annotate | Download (3.79 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 8264 2006-10-23 18:51:42Z azabala $
47
* $Log$
48
* Revision 1.2  2006-10-23 18:51:42  azabala
49
* *** empty log message ***
50
*
51
* Revision 1.1  2006/10/20 19:54:01  azabala
52
* *** empty log message ***
53
*
54
*
55
*/
56
package com.iver.cit.gvsig.graph.core;
57

    
58
import com.iver.cit.gvsig.fmap.core.IFeature;
59
import com.vividsolutions.jts.geom.Coordinate;
60
import com.vividsolutions.jts.geom.Geometry;
61
import com.vividsolutions.jts.geom.LineString;
62
import com.vividsolutions.jts.geom.MultiLineString;
63

    
64
public class TurnUtil {
65
        public static final int TURN_RIGHT = 0;
66
        public static final int TURN_LEFT = 1;
67
        public static final int GO_STRAIGH_ON = 2;
68
        public static final int TURN_U = 4;
69
        
70
        
71
        public static boolean checkIsLine(Geometry geometry){
72
                if(geometry instanceof LineString || geometry instanceof MultiLineString)
73
                        return true;
74
                else
75
                        return false;
76
        }        
77
        
78
        
79
        
80
        public static final int getDirection(IFeature feature1, IFeature feature2){
81
                Geometry geom1 = feature1.getGeometry().toJTSGeometry();
82
                Geometry geom2 = feature2.getGeometry().toJTSGeometry();
83
                if(!checkIsLine(geom1))
84
                        return -1;
85
                if(!checkIsLine(geom2))
86
                        return -1;
87
            Coordinate[] coords1 = geom1.getCoordinates();
88
            Coordinate p0 = coords1[coords1.length -2];
89
            Coordinate p1 = coords1[coords1.length -1];
90
            Coordinate[] coords2 = geom2.getCoordinates();
91
            Coordinate p2 = coords2[1];
92
            
93
                 double dx = p1.x - p0.x;
94
             double dy = p1.y - p0.y;
95
             double angle1 = Math.atan2(dy, dx);
96
        
97
             dx = p2.x - p1.x;
98
             dy = p2.y - p1.y;
99
             double angle2 = Math.atan2(dy, dx);
100
          
101
             double turnAngle = angle1 - angle2;
102
             double deegreeAngle = Math.toDegrees(turnAngle);
103
            
104
             if (Math.abs(deegreeAngle) > 180){
105
                     if(deegreeAngle < 0){
106
                             deegreeAngle = 360 - Math.abs(deegreeAngle);
107
                     }else{
108
                             deegreeAngle = deegreeAngle - 360;
109
                     }
110
             }
111
            
112
             if(Math.abs(deegreeAngle) <= 30)
113
                     return GO_STRAIGH_ON;
114
             else if(deegreeAngle <= 0)
115
                     return TURN_LEFT;
116
             else
117
                     return TURN_RIGHT;
118
        }        
119
        
120
        
121
        /**
122
         * 
123
         * Code extracted from JUMP
124
         * 
125
           * Returns the angle between two vectors.
126
           * @param a1 the angle of one vector, between -Pi and Pi
127
           * @param a2 the angle of the other vector, between -Pi and Pi
128
           * @return the angle (in radians) between the two vectors, between 0 and Pi
129
           */
130
//          public static double diff(double a1, double a2) {
131
//              double da;
132
//
133
//              if (a1 < a2) {
134
//                  da = a2 - a1;
135
//              } else {
136
//                  da = a1 - a2;
137
//              }
138
//
139
//              if (da > Math.PI) {
140
//                  da = (2 * Math.PI) - da;
141
//              }
142
//              return da;
143
//          }
144
}
145