2 import java.util.Scanner;
3 import java.util.StringTokenizer;
5 /*=============================================================================
9 * =============================================================================
11 * Copyright (C) Stanford University, 2006. All Rights Reserved.
12 * Author: Chi Cao Minh
14 * =============================================================================
16 * For the license of bayes/sort.h and bayes/sort.c, please see the header
19 * ------------------------------------------------------------------------
21 * For the license of kmeans, please see kmeans/LICENSE.kmeans
23 * ------------------------------------------------------------------------
25 * For the license of ssca2, please see ssca2/COPYRIGHT
27 * ------------------------------------------------------------------------
29 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
30 * header of the files.
32 * ------------------------------------------------------------------------
34 * For the license of lib/rbtree.h and lib/rbtree.c, please see
35 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
37 * ------------------------------------------------------------------------
39 * Unless otherwise noted, the following license applies to STAMP files:
41 * Copyright (c) 2007, Stanford University
42 * All rights reserved.
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions are
48 * * Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
51 * * Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in
53 * the documentation and/or other materials provided with the
56 * * Neither the name of Stanford University nor the names of its
57 * contributors may be used to endorse or promote products derived
58 * from this software without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
61 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
63 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
65 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
66 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
67 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
68 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
69 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
70 * THE POSSIBILITY OF SUCH DAMAGE.
72 * =============================================================================
78 Vector_t wallVectorPtr; /* contains source/destination pairs to route */
79 Vector_t srcVectorPtr; /* obstacles */
80 Vector_t dstVectorPtr; /* destinations */
82 public int GRID_POINT_FULL;
83 public int GRID_POINT_EMPTY;
88 GRID_POINT_EMPTY = -1;
92 /* =============================================================================
94 * =============================================================================
95 maze_t* maze_alloc ();
97 public static Maze alloc()
99 Maze mazePtr = new Maze();
101 mazePtr.gridPtr = null;
102 mazePtr.workQueuePtr = Queue_t.queue_alloc(1024);
103 mazePtr.wallVectorPtr = Vector_t.vector_alloc(1);
104 mazePtr.srcVectorPtr = Vector_t.vector_alloc(1);
105 mazePtr.dstVectorPtr = Vector_t.vector_alloc(1);
111 /* =============================================================================
113 * =============================================================================
114 void maze_free (maze_t* mazePtr);
116 public static void free(Maze m)
121 /* =============================================================================
123 * =============================================================================
125 private void addToGrid(Grid gridPtr,Vector_t vectorPtr,String type)
128 int n = vectorPtr.vector_getSize();
130 for(i = 0; i < n; i++) {
131 Coordinate coordinatePtr = (Coordinate)vectorPtr.vector_at(i);
132 if(!gridPtr.isPointValid(coordinatePtr.x,coordinatePtr.y,coordinatePtr.z))
134 System.out.println("Error: " + type + " (" + coordinatePtr.x +
135 ", " + coordinatePtr.y +
136 ", " + coordinatePtr.z);
140 gridPtr.addPath(vectorPtr);
142 /* =============================================================================
144 * -- Return number of path to route
145 * =============================================================================
146 long maze_read (maze_t* mazePtr, char* inputFileName);
148 public int readMaze(String inputFileName)
153 // FileInputStream in = new FileInputStream(inputFileName);
154 //TODO remove this and uncomment the line above.
155 Scanner in = new Scanner(new File(inputFileName));
163 boolean isParseError = false;
164 List_t workListPtr = List_t.alloc(1); // List.alloc(Coordinate.comparePair);
167 //TODO change this back as well
168 // while((line = in.readLine()) != null) {
169 while(in.hasNextLine()){
170 line = in.nextLine();
171 //TODO remove the above
174 int[] xy = new int[6]; // equivalent to x1,y1,z1,x2,y2,z2
177 StringTokenizer tok = new StringTokenizer(line);
179 if((numToken = tok.countTokens()) < 1 ) {
183 code = tok.nextToken();
185 if(code.equals("#")) {
189 for(int i=0;i<numToken-1;i++) {
190 xy[i] = Integer.parseInt(tok.nextToken());
193 if(code.equals("d")) {
194 /* dimensions (format: d x y z) */
202 if(width < 1 || height < 1 || depth <1)
205 }else if(code.equals("p")) { /* paths (format: p x1 y1 z1 x2 y2 z2) */
210 Coordinate srcPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
211 Coordinate dstPtr = Coordinate.alloc(xy[3],xy[4],xy[5]);
213 if(Coordinate.isEqual(srcPtr,dstPtr)) {
217 Pair coordinatePairPtr = Pair.alloc(srcPtr,dstPtr);
218 boolean status = workListPtr.insert(coordinatePairPtr);
219 srcVectorPtr.vector_pushBack(srcPtr);
220 dstVectorPtr.vector_pushBack(dstPtr);
224 }else if(code.equals("w")) {
225 /* walls (format: w x y z) */
229 Coordinate wallPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
230 wallVectorPtr.vector_pushBack(wallPtr);
236 if(isParseError) {/* Error */
237 System.out.println("Error: line " + lineNumber + " of " + inputFileName + "invalid");
241 /* iterate over lines in put file */
245 * Initialize grid contents
247 if(width < 1 || height < 1 || depth < 1) {
248 System.out.println("Error : Invalid dimensions ( " + width + ", " + height + ", "+ depth + ")");
252 Grid gridPtr = Grid.alloc(width,height,depth);
253 this.gridPtr = gridPtr;
254 addToGrid(gridPtr,wallVectorPtr,"wall");
255 addToGrid(gridPtr,srcVectorPtr, "source");
256 addToGrid(gridPtr,dstVectorPtr, "destination");
257 System.out.println("Maze dimensions = " + width + " x " + height + " x " + depth);
258 System.out.println("Paths to route = " + workListPtr.getSize());
261 * Initialize work queue
263 List_Iter it = new List_Iter();
264 it.reset(workListPtr);
265 while(it.hasNext(workListPtr)) {
266 Pair coordinatePairPtr = (Pair)it.next(workListPtr);
267 workQueuePtr.queue_push(coordinatePairPtr);
270 List_t.free(workListPtr);
272 return srcVectorPtr.vector_getSize();
273 } //TODO remove this parenthesis (goes with Try) and the below
276 System.out.println("The File is non-existant: " + inputFileName);
283 /* =============================================================================
285 * =============================================================================
286 bool_t maze_checkPaths (maze_t* mazePtr, list_t* pathListPtr, bool_t doPrintPaths);
288 public boolean checkPaths(List_t pathVectorListPtr,boolean doPrintPaths)
293 Grid testGridPtr = Grid.alloc(gridPtr.width,gridPtr.height,gridPtr.depth);
294 testGridPtr.addPath(wallVectorPtr);
297 int numSrc = srcVectorPtr.vector_getSize();
298 // System.out.println("numSrc = " +numSrc);
300 for(i = 0; i < numSrc; i++) {
301 Coordinate srcPtr = (Coordinate)srcVectorPtr.vector_at(i);
302 testGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
305 /* Mark destinations */
306 int numdst = dstVectorPtr.vector_getSize();
307 for(i = 0; i < numdst; i++) {
308 Coordinate dstPtr = (Coordinate)dstVectorPtr.vector_at(i);
309 testGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,0);
312 // testGridPtr.print();
314 /* Make sure path is contiguous and does not overlap */
316 List_Iter it = new List_Iter();
317 it.reset(pathVectorListPtr);
319 int height = gridPtr.height;
320 int width = gridPtr.width;
321 int area = height * width;
323 while(it.hasNext(pathVectorListPtr)) {
324 Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
325 int numPath = pathVectorPtr.vector_getSize();
327 for(i = 0; i < numPath; i++) {
329 Vector_t pointVectorPtr = (Vector_t)pathVectorPtr.vector_at(i);
331 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
333 int z=prevGridPointIndex/area;
334 int index2d=prevGridPointIndex%area;
338 if(testGridPtr.getPoint(x,y,z) != 0) {
342 Coordinate prevCoordinate = new Coordinate();
343 prevCoordinate.x = x;
344 prevCoordinate.y = y;
345 prevCoordinate.z = z;
348 int numPoint = pointVectorPtr.vector_getSize();
351 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
352 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
353 Coordinate currCoordinate = new Coordinate();
355 z=currGridPointIndex/area;
356 index2d=currGridPointIndex%area;
360 currCoordinate.x = x;
361 currCoordinate.y = y;
362 currCoordinate.z = z;
364 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
365 System.out.println("you there?");
369 prevCoordinate = currCoordinate;
370 int xx = currCoordinate.x;
371 int yy = currCoordinate.y;
372 int zz = currCoordinate.z;
373 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY) {
376 testGridPtr.setPoint(xx,yy,zz,id);
380 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
381 z=lastGridPointIndex/area;
382 index2d=lastGridPointIndex%area;
385 if(testGridPtr.getPoint(x,y,z) != 0) {
388 } /* iterate over pathVector */
389 } /* iterate over pathVectorList */
392 System.out.println("\nRouted Maze:");
401 /* =============================================================================
405 * =============================================================================