View Javadoc

1   package cz.cuni.amis.pogamut.udk.communication.worldview.map;
2   
3   import java.util.Collection;
4   import java.util.Collections;
5   import java.util.HashMap;
6   import java.util.HashSet;
7   import java.util.Map;
8   import java.util.Set;
9   import java.util.Map.Entry;
10  
11  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
12  import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
13  import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.NavPoint;
14  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.Box;
15  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealMap;
16  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.MapInfo;
17  
18  //public class UDKMap implements DirectedGraph<NavPoint, NavPointNeighbourLink>,
19  //                                  WeightedGraph<NavPoint, NavPointNeighbourLink> {
20  /**
21   * Representation of map made from <tt>NavPoint</tt>s and <tt>NavPointNeighbourLink</tt>s.
22   * Doesn't react on changes of worldview, data is copied in constructor.
23   *
24   * @author Honza
25   */
26  public class UDKMap implements IUnrealMap<MapInfo> {
27  
28      private String name = "";
29      private MapInfo info;
30      //private Map<IWorldObjectId, NavPoint> navs;
31      // TODO: mit samostatnou tridu jako adapter pro JGraph
32      //       tohleto reservovat pro veci jako: jmeno mapy ... :-) ... a spoustu dalsi zajimavych veci
33      // Jakub: PROBOHA! String jako klic v mape je na zabiti! :-) ... pouzit cele UnrealId jako klic, pokud ty stringy jsou 
34      //        nevyhnutelne pouzivat jako klic Token a stringy prevadet pomoci tridy Tokens
35      private Map<String, Waypoint> waypoints = new HashMap<String, Waypoint>();
36  
37      public UDKMap(IWorldView worldView) {
38          Collection<NavPoint> navs = worldView.getAll(NavPoint.class).values();
39  
40          for (NavPoint nav : navs) {
41              waypoints.put(nav.getId().getStringId(), new Waypoint(nav));
42          }
43          // update ending waypoints of edges
44          for (Waypoint point : waypoints.values()) {
45              for (Waylink link : point.getOutgoingEdges()) {
46                  Waypoint end = waypoints.get(link.getEndId());
47                  link.setEnd(end);
48              }
49          }
50  
51      }
52  
53      public void addInfo(MapInfo info) {
54          this.info = info;
55      }
56  
57      public MapInfo getInfo() {
58          return info;
59      }
60  
61      public void setName(String name) {
62          this.name = name;
63      }
64  
65      public String getName() {
66          return name;
67      }
68      /*
69      public int inDegreeOf(NavPoint testedNav) {
70      if (!navs.containsValue(testedNav)) {
71      throw new RuntimeException("Nav is not in map");
72      }
73      return testedNav.getIncomingEdges().size();
74      }
75       */
76  
77      /*    public Set<NavPointNeighbourLink> incomingEdgesOf(NavPoint nav) {
78      if (!navs.containsValue(nav)) {
79      return null;
80      }
81  
82      return new HashSet<NavPointNeighbourLink>(navs.get(nav).getIncomingEdges().values());
83      }
84       */
85      public Collection<Waypoint> vertexSet() {
86          return Collections.unmodifiableCollection(waypoints.values());
87      }
88  
89      public Set<Waylink> edgeSet() {
90          Set<Waylink> edges = new HashSet<Waylink>();
91  
92          for (Waypoint nav : waypoints.values()) {
93              edges.addAll(nav.getOutgoingEdges());
94          }
95  
96          // remove duplicates, from one side to other
97          
98          return edges;
99      }
100 
101     public void printInfo() {
102         System.out.println("Printing info about map, vers: " + waypoints.size() + ", links: " + this.edgeSet().size());
103         for (Entry<String, Waypoint> entry : this.waypoints.entrySet()) {
104             System.out.println(" * " + entry.getKey() + " " + entry.getValue().getLocation());
105         }
106     }
107 
108 
109 
110     /*
111     public int outDegreeOf(NavPoint arg0) {
112     // TODO Auto-generated method stub
113     return 0;
114     }
115 
116     public Set<NavPointNeighbourLink> outgoingEdgesOf(NavPoint arg0) {
117     // TODO Auto-generated method stub
118     return null;
119     }
120 
121     public NavPointNeighbourLink addEdge(NavPoint arg0, NavPoint arg1) {
122     // TODO Auto-generated method stub
123     return null;
124     }
125 
126     public boolean addEdge(NavPoint arg0, NavPoint arg1,
127     NavPointNeighbourLink arg2) {
128     // TODO Auto-generated method stub
129     return false;
130     }
131 
132     public boolean addVertex(NavPoint arg0) {
133     // TODO Auto-generated method stub
134     return false;
135     }
136 
137     public boolean containsEdge(NavPoint arg0, NavPoint arg1) {
138     // TODO Auto-generated method stub
139     return false;
140     }
141 
142     public boolean containsVertex(NavPoint arg0) {
143     // TODO Auto-generated method stub
144     return false;
145     }
146      */
147 
148     /*
149     public Set<NavPointNeighbourLink> edgesOf(NavPoint arg0) {
150     // TODO Auto-generated method stub
151     return null;
152     }
153 
154     public Set<NavPointNeighbourLink> getAllEdges(NavPoint arg0,
155     NavPoint arg1) {
156     // TODO Auto-generated method stub
157     return null;
158     }
159 
160     public NavPointNeighbourLink getEdge(NavPoint arg0, NavPoint arg1) {
161     // TODO Auto-generated method stub
162     return null;
163     }
164 
165     public NavPoint getEdgeSource(NavPointNeighbourLink arg0) {
166     // TODO Auto-generated method stub
167     return null;
168     }
169 
170     public NavPoint getEdgeTarget(NavPointNeighbourLink arg0) {
171     // TODO Auto-generated method stub
172     return null;
173     }
174 
175     public double getEdgeWeight(NavPointNeighbourLink arg0) {
176     // TODO Auto-generated method stub
177     return 0;
178     }
179      */
180 
181     /*
182     public boolean removeAllEdges(
183     Collection<? extends NavPointNeighbourLink> arg0) {
184     // TODO Auto-generated method stub
185     return false;
186     }
187 
188     public Set<NavPointNeighbourLink> removeAllEdges(NavPoint arg0,
189     NavPoint arg1) {
190     // TODO Auto-generated method stub
191     return null;
192     }
193 
194     public boolean removeAllVertices(Collection<? extends NavPoint> arg0) {
195     // TODO Auto-generated method stub
196     return false;
197     }
198 
199     public boolean removeEdge(NavPointNeighbourLink arg0) {
200     // TODO Auto-generated method stub
201     return false;
202     }
203 
204     public NavPointNeighbourLink removeEdge(NavPoint arg0, NavPoint arg1) {
205     // TODO Auto-generated method stub
206     return null;
207     }
208 
209     public boolean removeVertex(NavPoint arg0) {
210     // TODO Auto-generated method stub
211     return false;
212     }
213 
214     public boolean containsEdge(NavPointNeighbourLink arg0) {
215     // TODO Auto-generated method stub
216     return false;
217     }
218      */
219 
220     /**
221      * Get smallest box that contains all waypoints of map.
222      * @return 
223      */
224     public Box getBox() {
225         double minX = Double.MAX_VALUE;
226         double maxX = Double.MIN_VALUE;
227 
228         double minY = Double.MAX_VALUE;
229         double maxY = Double.MIN_VALUE;
230 
231         double minZ = Double.MAX_VALUE;
232         double maxZ = Double.MIN_VALUE;
233 
234         Collection<Waypoint> navs = this.vertexSet();
235 
236         for (Waypoint nav : navs) {
237             Location loc = nav.getLocation();
238 
239             if (loc.x < minX) {
240                 minX = loc.x;
241             }
242             if (loc.x > maxX) {
243                 maxX = loc.x;
244             }
245 
246             if (loc.y < minY) {
247                 minY = loc.y;
248             }
249             if (loc.y > maxY) {
250                 maxY = loc.y;
251             }
252 
253             if (loc.z < minZ) {
254                 minZ = loc.z;
255             }
256             if (loc.z > maxZ) {
257                 maxZ = loc.z;
258             }
259         }
260 
261         return new Box(minX, minY, minZ, maxX, maxY, maxZ);
262     }
263 }