2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.util.Scanner;
5 import java.util.StringTokenizer;
7 /*=============================================================================
11 * =============================================================================
13 * Copyright (C) Stanford University, 2006. All Rights Reserved.
14 * Author: Chi Cao Minh
16 * =============================================================================
18 * For the license of bayes/sort.h and bayes/sort.c, please see the header
21 * ------------------------------------------------------------------------
23 * For the license of kmeans, please see kmeans/LICENSE.kmeans
25 * ------------------------------------------------------------------------
27 * For the license of ssca2, please see ssca2/COPYRIGHT
29 * ------------------------------------------------------------------------
31 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
32 * header of the files.
34 * ------------------------------------------------------------------------
36 * For the license of lib/rbtree.h and lib/rbtree.c, please see
37 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
39 * ------------------------------------------------------------------------
41 * Unless otherwise noted, the following license applies to STAMP files:
43 * Copyright (c) 2007, Stanford University
44 * All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions are
50 * * Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
53 * * Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in
55 * the documentation and/or other materials provided with the
58 * * Neither the name of Stanford University nor the names of its
59 * contributors may be used to endorse or promote products derived
60 * from this software without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
63 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
65 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
72 * THE POSSIBILITY OF SUCH DAMAGE.
74 * =============================================================================
77 //#define GRID_POINT_FULL -2
78 //#define GRID_POINT_EMPTY -1
80 //TODO change these back and delete class variables.
84 private static int GRID_POINT_FULL;
85 private static int GRID_POINT_EMPTY;
89 Vector_t wallVectorPtr; /* contains source/destination pairs to route */
90 Vector_t srcVectorPtr; /* obstacles */
91 Vector_t dstVectorPtr; /* destinations */
95 //these were changed from #defines
97 GRID_POINT_EMPTY = -1;
101 /* =============================================================================
103 * =============================================================================
104 maze_t* maze_alloc ();
106 public static Maze alloc()
108 Maze mazePtr = new Maze();
110 if(mazePtr != null) {
111 mazePtr.gridPtr = null;
112 mazePtr.workQueuePtr = Queue_t.queue_alloc(1024);
113 mazePtr.wallVectorPtr = Vector_t.vector_alloc(1);
114 mazePtr.srcVectorPtr = Vector_t.vector_alloc(1);
115 mazePtr.dstVectorPtr = Vector_t.vector_alloc(1);
122 /* =============================================================================
124 * =============================================================================
125 void maze_free (maze_t* mazePtr);
127 public static void free(Maze m)
132 /* =============================================================================
134 * =============================================================================
136 private void addToGrid(Grid gridPtr,Vector_t vectorPtr,String type)
139 int n = vectorPtr.vector_getSize();
141 for(i = 0; i < n; i++) {
142 Coordinate coordinatePtr = (Coordinate)vectorPtr.vector_at(i);
143 if(!gridPtr.isPointValid(coordinatePtr.x,coordinatePtr.y,coordinatePtr.z))
145 System.out.println("Error: " + type + " (" + coordinatePtr.x +
146 ", " + coordinatePtr.y +
147 ", " + coordinatePtr.z);
151 gridPtr.addPath(vectorPtr);
153 /* =============================================================================
155 * -- Return number of path to route
156 * =============================================================================
157 long maze_read (maze_t* mazePtr, char* inputFileName);
159 public int readMaze(String inputFileName)
161 // FileInputStream in = new FileInputStream(inputFileName);
164 System.out.println("Opening file: " + inputFileName);
165 in = new Scanner(new File(inputFileName));
174 boolean isParseError = false;
175 List_t workListPtr = List_t.alloc(1); // List.alloc(Coordinate.comparePair);
178 while(in.hasNext()) {
180 line = in.nextLine();
182 int[] xy = new int[6]; // equivalent to x1,y1,z1,x2,y2,z2
185 StringTokenizer tok = new StringTokenizer(line);
187 if((numToken = tok.countTokens()) < 1 ) {
191 code = tok.nextToken();
193 if(code.equals("#")) {
197 for(int i=0;i<numToken-1;i++) {
198 xy[i] = Integer.parseInt(tok.nextToken());
201 if(code.equals("d")) {
202 /* dimensions (format: d x y z) */
210 if(width < 1 || height < 1 || depth <1)
213 }else if(code.equals("p")) { /* paths (format: p x1 y1 z1 x2 y2 z2) */
218 Coordinate srcPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
219 Coordinate dstPtr = Coordinate.alloc(xy[3],xy[4],xy[5]);
221 if(Coordinate.isEqual(srcPtr,dstPtr)) {
225 Pair coordinatePairPtr = Pair.alloc(srcPtr,dstPtr);
226 boolean status = workListPtr.insert(coordinatePairPtr);
228 System.out.println("LIST_INSERT????");
231 srcVectorPtr.vector_pushBack(srcPtr);
232 dstVectorPtr.vector_pushBack(dstPtr);
236 }else if(code.equals("w")) {
237 /* walls (format: w x y z) */
241 Coordinate wallPtr = Coordinate.alloc(xy[0],xy[1],xy[2]);
242 wallVectorPtr.vector_pushBack(wallPtr);
248 if(isParseError) {/* Error */
249 System.out.println("Error: line " + lineNumber + " of " + inputFileName + "invalid");
253 /* iterate over lines in put file */
259 * Initialize grid contents
261 if(width < 1 || height < 1 || depth < 1) {
262 System.out.println("Error : Invalid dimensions ( " + width + ", " + height + ", "+ depth + ")");
266 Grid gridPtr = Grid.alloc(width,height,depth);
267 this.gridPtr = gridPtr;
268 addToGrid(gridPtr,wallVectorPtr,"wall");
269 addToGrid(gridPtr,srcVectorPtr, "source");
270 addToGrid(gridPtr,dstVectorPtr, "destination");
271 System.out.println("Maze dimensions = " + width + " x " + height + " x " + depth);
272 System.out.println("Paths to route = " + workListPtr.getSize());
275 * Initialize work queue
277 List_Iter it = new List_Iter();
278 it.reset(workListPtr);
279 while(it.hasNext(workListPtr)) {
280 Pair coordinatePairPtr = (Pair)it.next(workListPtr);
281 workQueuePtr.queue_push(coordinatePairPtr);
284 List_t.free(workListPtr);
286 } catch (FileNotFoundException e) {
287 // TODO Auto-generated catch block
291 return srcVectorPtr.vector_getSize();
295 /* =============================================================================
297 * =============================================================================
298 bool_t maze_checkPaths (maze_t* mazePtr, list_t* pathListPtr, bool_t doPrintPaths);
300 public boolean checkPaths(List_t pathVectorListPtr,boolean doPrintPaths)
305 Grid testGridPtr = Grid.alloc(gridPtr.width,gridPtr.height,gridPtr.depth);
306 testGridPtr.addPath(wallVectorPtr);
309 int numSrc = srcVectorPtr.vector_getSize();
310 // System.out.println("numSrc = " +numSrc);
312 for(i = 0; i < numSrc; i++) {
313 Coordinate srcPtr = (Coordinate)srcVectorPtr.vector_at(i);
314 testGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
317 /* Mark destinations */
318 int numdst = dstVectorPtr.vector_getSize();
319 for(i = 0; i < numdst; i++) {
320 Coordinate dstPtr = (Coordinate)dstVectorPtr.vector_at(i);
321 testGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,0);
324 // testGridPtr.print();
326 /* Make sure path is contiguous and does not overlap */
328 List_Iter it = new List_Iter();
329 it.reset(pathVectorListPtr);
330 while(it.hasNext(pathVectorListPtr)) {
331 Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
332 int numPath = pathVectorPtr.vector_getSize();
334 for(i = 0; i < numPath; i++) {
336 Vector_t pointVectorPtr = (Vector_t)pathVectorPtr.vector_at(i);
338 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
339 int[] x = new int[1];
340 int[] y = new int[1];
341 int[] z = new int[1];
342 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
344 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
345 Grid.free(testGridPtr);
348 Coordinate prevCoordinate = new Coordinate();
352 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
353 prevCoordinate.x = x[0];
354 prevCoordinate.y = y[0];
355 prevCoordinate.z = z[0];
358 int numPoint = pointVectorPtr.vector_getSize();
361 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
362 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
363 Coordinate currCoordinate = new Coordinate();
367 gridPtr.getPointIndices(currGridPointIndex,x,y,z);
368 currCoordinate.x = x[0];
369 currCoordinate.y = y[0];
370 currCoordinate.z = z[0];
372 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
373 System.out.println("you there?");
374 Grid.free(testGridPtr);
378 prevCoordinate = currCoordinate;
379 int xx = currCoordinate.x;
380 int yy = currCoordinate.y;
381 int zz = currCoordinate.z;
382 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY) {
383 Grid.free(testGridPtr);
386 testGridPtr.setPoint(xx,yy,zz,id);
390 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
391 gridPtr.getPointIndices(lastGridPointIndex,x,y,z);
392 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
393 Grid.free(testGridPtr);
396 } /* iterate over pathVector */
397 } /* iterate over pathVectorList */
400 System.out.println("\nRouted Maze:");
404 Grid.free(testGridPtr);
410 /* =============================================================================
414 * =============================================================================