1 /*=============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
10 * =============================================================================
12 * For the license of bayes/sort.h and bayes/sort.c, please see the header
15 * ------------------------------------------------------------------------
17 * For the license of kmeans, please see kmeans/LICENSE.kmeans
19 * ------------------------------------------------------------------------
21 * For the license of ssca2, please see ssca2/COPYRIGHT
23 * ------------------------------------------------------------------------
25 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26 * header of the files.
28 * ------------------------------------------------------------------------
30 * For the license of lib/rbtree.h and lib/rbtree.c, please see
31 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
33 * ------------------------------------------------------------------------
35 * Unless otherwise noted, the following license applies to STAMP files:
37 * Copyright (c) 2007, Stanford University
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions are
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
47 * * Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in
49 * the documentation and/or other materials provided with the
52 * * Neither the name of Stanford University nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66 * THE POSSIBILITY OF SUCH DAMAGE.
68 * =============================================================================
71 //#define GRID_POINT_FULL -2
72 //#define GRID_POINT_EMPTY -1
74 //TODO change these back and delete class variables.
78 private static int GRID_POINT_FULL;
79 private static int GRID_POINT_EMPTY;
83 Vector_t wallVectorPtr; /* contains source/destination pairs to route */
84 Vector_t srcVectorPtr; /* obstacles */
85 Vector_t dstVectorPtr; /* destinations */
89 //these were changed from #defines
91 GRID_POINT_EMPTY = -1;
95 /* =============================================================================
97 * =============================================================================
98 maze_t* maze_alloc ();
102 Maze mazePtr = new Maze();
104 if(mazePtr != null) {
105 Vector_t vector_t = new Vector_t();
106 Queue_t queue_t = new Queue_t();
108 mazePtr.gridPtr = null;
109 mazePtr.workQueuePtr = queue_t.queue_alloc(1024);
110 mazePtr.wallVectorPtr = vector_t.vector_alloc(1);
111 mazePtr.srcVectorPtr = vector_t.vector_alloc(1);
112 mazePtr.dstVectorPtr = vector_t.vector_alloc(1);
119 /* =============================================================================
121 * =============================================================================
122 void maze_free (maze_t* mazePtr);
124 public static void free(Maze m)
129 /* =============================================================================
131 * =============================================================================
133 private void addToGrid(Grid gridPtr,Vector_t vectorPtr,String type)
136 int n = vectorPtr.vector_getSize();
138 for(i = 0; i < n; i++) {
139 Coordinate coordinatePtr = (Coordinate)vectorPtr.vector_at(i);
140 if(!gridPtr.isPointValid(coordinatePtr.x,coordinatePtr.y,coordinatePtr.z))
142 System.out.println("Error: " + type + " (" + coordinatePtr.x +
143 ", " + coordinatePtr.y +
144 ", " + coordinatePtr.z);
148 gridPtr.addPath(vectorPtr);
150 /* =============================================================================
152 * -- Return number of path to route
153 * =============================================================================
154 long maze_read (maze_t* mazePtr, char* inputFileName);
156 public int readMaze(String inputFileName)
158 Coordinate coordinate = new Coordinate();
159 FileInputStream in = new FileInputStream(inputFileName);
169 boolean isParseError = false;
170 List_t workListPtr = List_t.alloc(1); // List.alloc(Coordinate.comparePair);
173 while((line = in.readLine()) != null) {
175 int[] xy = new int[6]; // equivalent to x1,y1,z1,x2,y2,z2
178 StringTokenizer tok = new StringTokenizer(line);
180 if((numToken = tok.countTokens()) < 1 ) {
184 code = tok.nextToken();
186 if(code.equals("#")) {
190 for(int i=0;i<numToken-1;i++) {
191 xy[i] = Integer.parseInt(tok.nextToken());
194 if(code.equals("d")) {
195 /* dimensions (format: d x y z) */
203 if(width < 1 || height < 1 || depth <1)
206 }else if(code.equals("p")) { /* paths (format: p x1 y1 z1 x2 y2 z2) */
211 Coordinate srcPtr = coordinate.alloc(xy[0],xy[1],xy[2]);
212 Coordinate dstPtr = coordinate.alloc(xy[3],xy[4],xy[5]);
214 if(Coordinate.isEqual(srcPtr,dstPtr)) {
218 Pair coordinatePairPtr = Pair.alloc(srcPtr,dstPtr);
219 boolean status = workListPtr.insert(coordinatePairPtr);
221 System.out.println("LIST_INSERT????");
224 srcVectorPtr.vector_pushBack(srcPtr);
225 dstVectorPtr.vector_pushBack(dstPtr);
229 }else if(code.equals("w")) {
230 /* walls (format: w x y z) */
234 Coordinate wallPtr = coordinate.alloc(xy[0],xy[1],xy[2]);
235 wallVectorPtr.vector_pushBack(wallPtr);
241 if(isParseError) {/* Error */
242 System.out.println("Error: line " + lineNumber + " of " + inputFileName + "invalid");
246 /* iterate over lines in put file */
250 * Initialize grid contents
252 if(width < 1 || height < 1 || depth < 1) {
253 System.out.println("Error : Invalid dimensions ( " + width + ", " + height + ", "+ depth + ")");
257 Grid grid = new Grid();
258 Grid gridPtr = grid.alloc(width,height,depth);
259 this.gridPtr = gridPtr;
260 addToGrid(gridPtr,wallVectorPtr,"wall");
261 addToGrid(gridPtr,srcVectorPtr, "source");
262 addToGrid(gridPtr,dstVectorPtr, "destination");
263 System.out.println("Maze dimensions = " + width + " x " + height + " x " + depth);
264 System.out.println("Paths to route = " + workListPtr.getSize());
267 * Initialize work queue
269 List_Iter it = new List_Iter();
270 it.reset(workListPtr);
271 while(it.hasNext(workListPtr)) {
272 Pair coordinatePairPtr = (Pair)it.next(workListPtr);
273 workQueuePtr.queue_push(coordinatePairPtr);
276 List_t.free(workListPtr);
278 return srcVectorPtr.vector_getSize();
282 /* =============================================================================
284 * =============================================================================
285 bool_t maze_checkPaths (maze_t* mazePtr, list_t* pathListPtr, bool_t doPrintPaths);
287 public boolean checkPaths(List_t pathVectorListPtr,boolean doPrintPaths)
290 Grid grid = new Grid();
292 Grid testGridPtr = grid.alloc(gridPtr.width, gridPtr.height, gridPtr.depth);
293 testGridPtr.addPath(wallVectorPtr);
296 int numSrc = srcVectorPtr.vector_getSize();
297 // System.out.println("numSrc = " +numSrc);
299 for(i = 0; i < numSrc; i++) {
300 Coordinate srcPtr = (Coordinate)srcVectorPtr.vector_at(i);
301 testGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
304 /* Mark destinations */
305 int numdst = dstVectorPtr.vector_getSize();
306 for(i = 0; i < numdst; i++) {
307 Coordinate dstPtr = (Coordinate)dstVectorPtr.vector_at(i);
308 testGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,0);
311 /* Make sure path is contiguous and does not overlap */
313 List_Iter it = new List_Iter();
314 it.reset(pathVectorListPtr);
315 while(it.hasNext(pathVectorListPtr)) {
316 Vector_t pathVectorPtr = (Vector_t)it.next(pathVectorListPtr);
317 int numPath = pathVectorPtr.vector_getSize();
319 for(i = 0; i < numPath; i++) {
321 Vector_t pointVectorPtr = (Vector_t)pathVectorPtr.vector_at(i);
323 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
324 int[] x = new int[1];
325 int[] y = new int[1];
326 int[] z = new int[1];
327 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
329 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
330 Grid.free(testGridPtr);
333 Coordinate prevCoordinate = new Coordinate();
337 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
338 prevCoordinate.x = x[0];
339 prevCoordinate.y = y[0];
340 prevCoordinate.z = z[0];
343 int numPoint = pointVectorPtr.vector_getSize();
346 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
347 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
348 Coordinate currCoordinate = new Coordinate();
352 gridPtr.getPointIndices(currGridPointIndex,x,y,z);
353 currCoordinate.x = x[0];
354 currCoordinate.y = y[0];
355 currCoordinate.z = z[0];
357 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
358 System.out.println("you there?");
359 Grid.free(testGridPtr);
363 prevCoordinate = currCoordinate;
364 int xx = currCoordinate.x;
365 int yy = currCoordinate.y;
366 int zz = currCoordinate.z;
367 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY) {
368 Grid.free(testGridPtr);
371 testGridPtr.setPoint(xx,yy,zz,id);
375 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
376 gridPtr.getPointIndices(lastGridPointIndex,x,y,z);
377 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
378 Grid.free(testGridPtr);
381 } /* iterate over pathVector */
382 } /* iterate over pathVectorList */
385 System.out.println("\nRouted Maze:");
389 Grid.free(testGridPtr);
394 /* =============================================================================
396 * Implemented for rblocks
397 * =============================================================================
399 public boolean checkPath(CoordPathWrapper singlePath, Grid localGrid, int id)
401 Grid testGridPtr = localGrid.alloc(localGrid.width, localGrid.height, localGrid.depth);
402 testGridPtr.copy(testGridPtr, localGrid);
403 Vector_t pointVectorPtr = singlePath.thePath;
404 Coordinate srcPtr = (Coordinate) singlePath.coordinatePair.first;
405 Coordinate dstPtr = (Coordinate) singlePath.coordinatePair.second;
407 /* mark source and destination */
408 testGridPtr.setPoint(srcPtr.x, srcPtr.y, srcPtr.z, 0);
409 testGridPtr.setPoint(dstPtr.x, dstPtr.y, dstPtr.z, 0);
411 /* makes sure path is contiguous and does not overlap */
413 int prevGridPointIndex = ((Integer)pointVectorPtr.vector_at(0)).intValue();
414 int[] x = new int[1];
415 int[] y = new int[1];
416 int[] z = new int[1];
417 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
419 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
420 Grid.free(testGridPtr);
423 Coordinate prevCoordinate = new Coordinate();
427 gridPtr.getPointIndices(prevGridPointIndex,x,y,z);
428 prevCoordinate.x = x[0];
429 prevCoordinate.y = y[0];
430 prevCoordinate.z = z[0];
433 int numPoint = pointVectorPtr.vector_getSize();
436 for(j = 1; j< (numPoint - 1) ;j++) { /* no need to check endpoints */
437 int currGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
438 Coordinate currCoordinate = new Coordinate();
442 gridPtr.getPointIndices(currGridPointIndex,x,y,z);
443 currCoordinate.x = x[0];
444 currCoordinate.y = y[0];
445 currCoordinate.z = z[0];
447 if(!Coordinate.areAdjacent(currCoordinate,prevCoordinate)) {
448 System.out.println("you there?");
449 Grid.free(testGridPtr);
453 prevCoordinate = currCoordinate;
454 int xx = currCoordinate.x;
455 int yy = currCoordinate.y;
456 int zz = currCoordinate.z;
457 if(testGridPtr.getPoint(xx,yy,zz) != GRID_POINT_EMPTY)
459 Grid.free(testGridPtr);
464 testGridPtr.setPoint(xx,yy,zz,id);
468 int lastGridPointIndex = ((Integer)pointVectorPtr.vector_at(j)).intValue();
469 gridPtr.getPointIndices(lastGridPointIndex,x,y,z);
470 if(testGridPtr.getPoint(x[0],y[0],z[0]) != 0) {
471 Grid.free(testGridPtr);
481 /* =============================================================================
485 * =============================================================================