View Javadoc

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