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 * =============================================================================
75 //#define GRID_POINT_FULL -2
76 //#define GRID_POINT_EMPTY -1
78 //TODO change these back and delete class variables.
82 private static int GRID_POINT_FULL;
83 private static int GRID_POINT_EMPTY;
87 Vector_t wallVectorPtr; /* contains source/destination pairs to route */
88 Vector_t srcVectorPtr; /* obstacles */
89 Vector_t dstVectorPtr; /* destinations */
93 //these were changed from #defines
95 GRID_POINT_EMPTY = -1;
99 /* =============================================================================
101 * =============================================================================
102 maze_t* maze_alloc ();
106 Maze mazePtr = new Maze();
108 if(mazePtr != null) {
109 Vector_t vector_t = new Vector_t();
110 Queue_t queue_t = new Queue_t();
112 mazePtr.gridPtr = null;
113 mazePtr.workQueuePtr = queue_t.queue_alloc(1024);
114 mazePtr.wallVectorPtr = vector_t.vector_alloc(1);
115 mazePtr.srcVectorPtr = vector_t.vector_alloc(1);
116 mazePtr.dstVectorPtr = vector_t.vector_alloc(1);
123 /* =============================================================================
125 * =============================================================================
126 void maze_free (maze_t* mazePtr);
128 public static void free(Maze m)
133 /* =============================================================================
135 * =============================================================================
137 private void addToGrid(Grid gridPtr,Vector_t vectorPtr,String type)
140 int n = vectorPtr.vector_getSize();
142 for(i = 0; i < n; i++) {
143 Coordinate coordinatePtr = (Coordinate)vectorPtr.vector_at(i);
144 if(!gridPtr.isPointValid(coordinatePtr.x,coordinatePtr.y,coordinatePtr.z))
146 System.out.println("Error: " + type + " (" + coordinatePtr.x +
147 ", " + coordinatePtr.y +
148 ", " + coordinatePtr.z);
152 gridPtr.addPath(vectorPtr);
154 /* =============================================================================
156 * -- Return number of path to route
157 * =============================================================================
158 long maze_read (maze_t* mazePtr, char* inputFileName);
160 public int readMaze(String inputFileName)
166 Coordinate coordinate = new Coordinate();
167 // FileInputStream in = new FileInputStream(inputFileName);
168 //TODO remove this and uncomment the line above.
169 Scanner in = new Scanner(new File(inputFileName));
178 boolean isParseError = false;
179 List_t workListPtr = List_t.alloc(1); // List.alloc(Coordinate.comparePair);
182 //TODO change this back as well
183 // while((line = in.readLine()) != null) {
184 while(in.hasNextLine())
187 line = in.nextLine();
190 int[] xy = new int[6]; // equivalent to x1,y1,z1,x2,y2,z2
193 StringTokenizer tok = new StringTokenizer(line);
195 if((numToken = tok.countTokens()) < 1 ) {
199 code = tok.nextToken();
201 if(code.equals("#")) {
205 for(int i=0;i<numToken-1;i++) {
206 xy[i] = Integer.parseInt(tok.nextToken());
209 if(code.equals("d")) {
210 /* dimensions (format: d x y z) */
218 if(width < 1 || height < 1 || depth <1)
221 }else if(code.equals("p")) { /* paths (format: p x1 y1 z1 x2 y2 z2) */
226 Coordinate srcPtr = coordinate.alloc(xy[0],xy[1],xy[2]);
227 Coordinate dstPtr = coordinate.alloc(xy[3],xy[4],xy[5]);
229 if(Coordinate.isEqual(srcPtr,dstPtr)) {
233 Pair coordinatePairPtr = Pair.alloc(srcPtr,dstPtr);
234 boolean status = workListPtr.insert(coordinatePairPtr);
236 System.out.println("LIST_INSERT????");
239 srcVectorPtr.vector_pushBack(srcPtr);
240 dstVectorPtr.vector_pushBack(dstPtr);
244 }else if(code.equals("w")) {
245 /* walls (format: w x y z) */
249 Coordinate wallPtr = coordinate.alloc(xy[0],xy[1],xy[2]);
250 wallVectorPtr.vector_pushBack(wallPtr);
256 if(isParseError) {/* Error */
257 System.out.println("Error: line " + lineNumber + " of " + inputFileName + "invalid");
261 /* iterate over lines in put file */
265 * Initialize grid contents
267 if(width < 1 || height < 1 || depth < 1) {
268 System.out.println("Error : Invalid dimensions ( " + width + ", " + height + ", "+ depth + ")");
272 Grid grid = new Grid();
273 Grid gridPtr = grid.alloc(width,height,depth);
274 this.gridPtr = gridPtr;
275 addToGrid(gridPtr,wallVectorPtr,"wall");
276 addToGrid(gridPtr,srcVectorPtr, "source");
277 addToGrid(gridPtr,dstVectorPtr, "destination");
278 System.out.println("Maze dimensions = " + width + " x " + height + " x " + depth);
279 System.out.println("Paths to route = " + workListPtr.getSize());
282 * Initialize work queue
284 List_Iter it = new List_Iter();
285 it.reset(workListPtr);
286 while(it.hasNext(workListPtr)) {
287 Pair coordinatePairPtr = (Pair)it.next(workListPtr);
288 workQueuePtr.queue_push(coordinatePairPtr);
291 List_t.free(workListPtr);
293 return srcVectorPtr.vector_getSize();
294 } //TODO remove this parenthesis (goes with Try) and the below
297 System.out.println("The File is non-existant: " + inputFileName);
304 /* =============================================================================
306 * =============================================================================
307 bool_t maze_checkPaths (maze_t* mazePtr, list_t* pathListPtr, bool_t doPrintPaths);
309 public boolean checkPaths(List_t pathVectorListPtr,boolean doPrintPaths)
312 Grid grid = new Grid();
314 Grid testGridPtr = grid.alloc(gridPtr.width, gridPtr.height, gridPtr.depth);
315 testGridPtr.addPath(wallVectorPtr);
318 int numSrc = srcVectorPtr.vector_getSize();
319 // System.out.println("numSrc = " +numSrc);
321 for(i = 0; i < numSrc; i++) {
322 Coordinate srcPtr = (Coordinate)srcVectorPtr.vector_at(i);
323 testGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
326 /* Mark destinations */
327 int numdst = dstVectorPtr.vector_getSize();
328 for(i = 0; i < numdst; i++) {
329 Coordinate dstPtr = (Coordinate)dstVectorPtr.vector_at(i);
330 testGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,0);
333 /* Make sure path is contiguous and does not overlap */
335 List_Iter it = new List_Iter();
336 it.reset(pathVectorListPtr);
337 while(it.hasNext(pathVectorListPtr)) {
338 Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
339 int numPath = pathVectorPtr.vector_getSize();
341 for(i = 0; i < numPath; i++) {
343 Vector_t pointVectorPtr = (Vector_t)pathVectorPtr.vector_at(i);
345 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
346 int[] x = new int[1];
347 int[] y = new int[1];
348 int[] z = new int[1];
349 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
351 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
352 Grid.free(testGridPtr);
355 Coordinate prevCoordinate = new Coordinate();
359 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
360 prevCoordinate.x = x[0];
361 prevCoordinate.y = y[0];
362 prevCoordinate.z = z[0];
365 int numPoint = pointVectorPtr.vector_getSize();
368 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
369 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
370 Coordinate currCoordinate = new Coordinate();
374 gridPtr.getPointIndices(currGridPointIndex,x,y,z);
375 currCoordinate.x = x[0];
376 currCoordinate.y = y[0];
377 currCoordinate.z = z[0];
379 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
380 System.out.println("you there?");
381 Grid.free(testGridPtr);
385 prevCoordinate = currCoordinate;
386 int xx = currCoordinate.x;
387 int yy = currCoordinate.y;
388 int zz = currCoordinate.z;
389 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY) {
390 Grid.free(testGridPtr);
393 testGridPtr.setPoint(xx,yy,zz,id);
397 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
398 gridPtr.getPointIndices(lastGridPointIndex,x,y,z);
399 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
400 Grid.free(testGridPtr);
403 } /* iterate over pathVector */
404 } /* iterate over pathVectorList */
407 System.out.println("\nRouted Maze:");
411 Grid.free(testGridPtr);
416 /* =============================================================================
418 * Implemented for rblocks
419 * =============================================================================
421 public boolean checkPath(CoordPathWrapper singlePath, Grid localGrid, int id)
423 Grid testGridPtr = localGrid.alloc(localGrid.width, localGrid.height, localGrid.depth);
424 testGridPtr.copy(testGridPtr, localGrid);
425 Vector_t pointVectorPtr = singlePath.thePath;
426 Coordinate srcPtr = (Coordinate) singlePath.coordinatePair.first;
427 Coordinate dstPtr = (Coordinate) singlePath.coordinatePair.second;
429 /* mark source and destination */
430 testGridPtr.setPoint(srcPtr.x, srcPtr.y, srcPtr.z, 0);
431 testGridPtr.setPoint(dstPtr.x, dstPtr.y, dstPtr.z, 0);
433 /* makes sure path is contiguous and does not overlap */
435 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
436 int[] x = new int[1];
437 int[] y = new int[1];
438 int[] z = new int[1];
439 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
441 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
442 Grid.free(testGridPtr);
445 Coordinate prevCoordinate = new Coordinate();
449 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
450 prevCoordinate.x = x[0];
451 prevCoordinate.y = y[0];
452 prevCoordinate.z = z[0];
455 int numPoint = pointVectorPtr.vector_getSize();
458 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
459 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
460 Coordinate currCoordinate = new Coordinate();
464 gridPtr.getPointIndices(currGridPointIndex,x,y,z);
465 currCoordinate.x = x[0];
466 currCoordinate.y = y[0];
467 currCoordinate.z = z[0];
469 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
470 System.out.println("you there?");
471 Grid.free(testGridPtr);
475 prevCoordinate = currCoordinate;
476 int xx = currCoordinate.x;
477 int yy = currCoordinate.y;
478 int zz = currCoordinate.z;
479 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY)
481 Grid.free(testGridPtr);
486 testGridPtr.setPoint(xx,yy,zz,id);
490 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
491 gridPtr.getPointIndices(lastGridPointIndex,x,y,z);
492 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
493 Grid.free(testGridPtr);
503 /* =============================================================================
507 * =============================================================================