Revision 25339 trunk/extensions/extGraph/src/org/gvsig/graph/core/Network.java

View differences:

Network.java
61 61
import com.vividsolutions.jts.geom.LineSegment;
62 62
import com.vividsolutions.jts.geom.MultiLineString;
63 63

  
64
import edu.uci.ics.jung.algorithms.connectivity.BFSDistanceLabeler;
65

  
64 66
public class Network {
65 67
	protected FLyrVect lyrVect;
66 68

  
......
78 80

  
79 81
	private Hashtable velocities = null;
80 82

  
83
	private ArrayList<GvTurn> turnCosts = new ArrayList();
84

  
81 85
	public void reconstruyeTramo(int idArc) {
82
		GvNode pN1;
86
		GvNode pN1, pN2;
83 87
		int i;
84 88
		
85 89
		// Si encontramos un enlace con idEdge >= numOriginalEdges, lo cambiamos.
......
94 98
//			pN2 = &Nodos[Arcos[IndiceArcos[idTramo].idArco].idNodo2];
95 99
			GvEdge edge = graph.getEdgeByID(edgePair.getIdEdge());
96 100
			pN1 = graph.getNodeByID(edge.getIdNodeOrig());
101
			pN2 = graph.getNodeByID(edge.getIdNodeEnd());
97 102

  
98 103
			// Metemos idArco en los enlaces de Nodo1
99
			for (i=0; i< pN1.getOutputLinks().size(); i++)
100
			{
101
				GvEdge auxEdge = (GvEdge) pN1.getOutputLinks().get(i);
102
				if (auxEdge.getIdArc() == idArc)
103
				{
104
					if (auxEdge.getIdEdge() >= numOriginalEdges) 
105
					{
106
						pN1.getOutputLinks().set(i, graph.getEdgeByID(edgePair.getIdEdge()));
107
						break;
108
					}
109
				}
110
			}
104
//			for (i=0; i< pN1.getOutputLinks().size(); i++)
105
//			{
106
//				GvEdge auxEdge = (GvEdge) pN1.getOutputLinks().get(i);
107
//				if (auxEdge.getIdArc() == idArc)
108
//				{
109
//					if (auxEdge.getIdEdge() >= numOriginalEdges) 
110
//					{
111
//						pN1.getOutputLinks().set(i, graph.getEdgeByID(edgePair.getIdEdge()));
112
//						break;
113
//					}
114
//				}
115
//			}
116
			restoreConnectors(edge);
117
			
111 118
		}
112 119

  
113 120
		if (edgePair.idInverseEdge != -1)
......
117 124
			GvEdge edge = graph.getEdgeByID(edgePair.getIdInverseEdge());
118 125
			pN1 = graph.getNodeByID(edge.getIdNodeOrig());
119 126

  
120
			for (i=0; i< pN1.getOutputLinks().size(); i++)
121
			{
122
				if (edge.getIdArc() == idArc)
123
				{
124
					GvEdge auxEdge = (GvEdge) pN1.getOutputLinks().get(i);
125
					if (auxEdge.getIdEdge() >= numOriginalEdges) 
126
					{
127
						pN1.getOutputLinks().set(i, graph.getEdgeByID(edgePair.getIdInverseEdge()));
128
						break;
129
					}
130
				}
131
			}
127
//			for (i=0; i< pN1.getOutputLinks().size(); i++)
128
//			{
129
//				if (edge.getIdArc() == idArc)
130
//				{
131
//					GvEdge auxEdge = (GvEdge) pN1.getOutputLinks().get(i);
132
//					if (auxEdge.getIdEdge() >= numOriginalEdges) 
133
//					{
134
//						pN1.getOutputLinks().set(i, graph.getEdgeByID(edgePair.getIdInverseEdge()));
135
//						break;
136
//					}
137
//				}								
138
//			}
139
			restoreConnectors(edge);
132 140
		}
133 141

  
134 142
		int numEdges = graph.numEdges();
......
144 152

  
145 153
	}
146 154

  
155
	private void restoreConnectors(GvEdge edge) {
156
		GvNode pN1 = graph.getNodeByID(edge.getIdNodeOrig());
157
		GvNode pN2 = graph.getNodeByID(edge.getIdNodeEnd());
158
		for (int iCon = 0; iCon < pN1.getConnectors().size(); iCon++) {
159
			GvConnector c = pN1.getConnectors().get(iCon);
160
			if (c.getEdgeOut() != null) {
161
				if ((c.getEdgeOut().getIdEdge() >= numOriginalEdges) && (c.getEdgeOut().getIdArc() == edge.getIdArc())) {
162
					c.setEdgeOut(edge);
163
				}
164
			}
165
		}
166
		for (int iCon = 0; iCon < pN2.getConnectors().size(); iCon++) {
167
			GvConnector c = pN2.getConnectors().get(iCon);
168
			if (c.getEdgeIn() != null) {
169
				if ((c.getEdgeIn().getIdEdge() >= numOriginalEdges) && (c.getEdgeOut().getIdArc() == edge.getIdArc())) {
170
					c.setEdgeIn(edge);
171
				}
172
			}
173
		}
174
	}
175

  
147 176
	/**
148 177
	 * Closest ID to this point. -1 if out from tolerance.
149 178
	 * @param x
......
755 784
		// ////////////////////////////////////////////////////
756 785
		int i;
757 786
		// boolean encontrado = false;
758
		for (i = 0; i < pN1.getOutputLinks().size(); i++) {
759
			GvEdge aux = (GvEdge) pN1.getOutputLinks().get(i);
760
			if (aux.getIdEdge() == idEdge) {
761
				pN1.getOutputLinks().set(i, first);
787
//		for (i = 0; i < pN1.getOutputLinks().size(); i++) {
788
//			GvEdge aux = (GvEdge) pN1.getOutputLinks().get(i);
789
//			if (aux.getIdEdge() == idEdge) {
790
//				pN1.getOutputLinks().set(i, first);
791
//				// encontrado = true;
792
//				break;
793
//			}
794
//		} // for
795
		for (i = 0; i < pN1.getConnectors().size(); i++) {
796
			GvConnector c = pN1.getConnectors().get(i);
797
			if ((c.getEdgeOut() != null) && (c.getEdgeOut().getIdEdge()== idEdge)) {
798
				c.setEdgeOut(first);
762 799
				// encontrado = true;
763 800
				break;
764 801
			}
765 802
		} // for
766 803

  
767
		newNode.getOutputLinks().add(second);
804
		// Conector de entrada 
805
		GvConnector newCon = new GvConnector();
806
		newCon.setEdgeIn(first);
807
		newNode.getConnectors().add(newCon);
768 808

  
809
		// Conector de salida 
810
		GvConnector conOut = new GvConnector();
811
		newCon.setEdgeOut(second);
812
		newNode.getConnectors().add(conOut);
813
		
814
		// Y hacemos que el conector de entrada del idNodo2 tenga el arco nuevo
815
		// log("Y hacemos que el conector de entrada del idNodo2 tenga el arco nuevo");
816
		for (i = 0; i < pN2.getConnectors().size(); i++) {
817
			GvConnector c = pN2.getConnectors().get(i);
818
			if ((c.getEdgeIn() != null) && (c.getEdgeIn().getIdEdge()== idEdge)) {
819
				c.setEdgeIn(second);
820
				// encontrado = true;
821
				break;
822
			}
823
		} // for
769 824
	}
770 825

  
771 826
	public ArrayList getModifiedCosts() {
772 827
		return modifiedCosts;
773 828
		
774 829
	}
830
	
831
	private int[] BuscaNodosDeTramo(EdgePair pair) {
832
		int[] resul = new int[2];
833
		if ((pair.idEdge == -1) && (pair.idInverseEdge == -1))
834
		{
835
			return null; // Error: No existen esos arcos.
836
		}
775 837

  
776
	public GvTurn addTurnCost(int idArcOrigin, int idArcDestination, double newCost) {
838
		if (pair.idEdge != -1)
839
		{
840
			resul[0] = graph.getEdgeByID(pair.idEdge).getIdNodeOrig();
841
			resul[1]= graph.getEdgeByID(pair.idEdge).getIdNodeEnd();
842
		}
843
		else
844
		{
845
			resul[0] = graph.getEdgeByID(pair.idInverseEdge).getIdNodeOrig();
846
			resul[1]= graph.getEdgeByID(pair.idInverseEdge).getIdNodeEnd();
847
		}
848
		return resul;
849

  
850
	}
851

  
852
	public int addTurnCost(int idArcOrigin, int idArcDestination, double newCost) {
777 853
		GvTurn turnCost = new GvTurn(idArcOrigin, idArcDestination, newCost);
778
		return turnCost;
854
		turnCosts.add(turnCost); //useful to remove them one by one without iterating the whole graph.
855
		EdgePair edgePairFrom = getGraph().getEdgesByIdArc(idArcOrigin);
856
		EdgePair edgePairTo = getGraph().getEdgesByIdArc(idArcDestination);
857
		
858
		// We found the node that connects these arcs, 
859
		// and add the new turnCost to its list of turnCosts.
860
		GvNode searchedNode;
861
		int idNa1, idNa2, idNb1, idNb2, idNodoBuscado;
862
		int[] A, B;
863

  
864
		A = BuscaNodosDeTramo(edgePairFrom);
865
		if (A == null) return -1; // No existen arcos para ese idTramo
866
		
867

  
868
		B = BuscaNodosDeTramo(edgePairTo);
869
		if (B == null) return -1; // No existen arcos para ese idTramo
870

  
871
		idNa1 = A[0]; idNa2 = A[1];
872
		idNb1 = B[0]; idNb2 = B[1];
873

  
874
		// Buscamos el nodo que est? entre fromIdTramo y toIdTramo
875
		// y el arco. Al arco hay que cambiarle el destino
876
		if (idNa1 == idNb1)
877
			idNodoBuscado = idNa1;
878
		else
879
			if (idNa1 == idNb2)
880
				idNodoBuscado = idNa1;
881
			else
882
				if (idNa2 == idNb1)
883
					idNodoBuscado = idNa2;
884
				else
885
					if (idNa2 == idNb2)
886
						idNodoBuscado = idNa2;
887
					else // ERROR
888
						return -2; // esos tramos no conectan.
889

  
890
		// Podemos funcionar con idTramo en lugar de idArco porque cada CLink lleva dentro
891
		// el idTramo que lo origin?, y en el algoritmo podemos mirarlo.
892

  
893
		searchedNode = graph.getNodeByID(idNodoBuscado);
894
		searchedNode.addTurnCost(turnCost);			
895

  
896
		
897
		return 0; // everything is fine
779 898
	}
780 899
	/**
781 900
	 * Create, add and apply a new modified cost to the graph. 
......
825 944
		modifiedCost.setApplied(true);
826 945
		return modifiedCost;
827 946
	}
947
	
948
	/**
949
	 * Remove ALL turn costs
950
	 */
951
	public void removeTurnCosts() {
952
		for (int i=0; i < turnCosts.size(); i++) {
953
			GvTurn turn = turnCosts.get(i);
954
//			turn.getNode().getTurnCosts().remove(turn);
955
			turn.getNode().removeTurnCosts();
956
		}
957
		turnCosts = new ArrayList<GvTurn>();
958
	}
828 959

  
829 960
	/**
830 961
	 * Be careful about the ORDER!!!!

Also available in: Unified diff