1 /* =============================================================================
5 * =============================================================================
7 * Copyright (C) Stanford University, 2006. All Rights Reserved.
12 * University of California, Irvine
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 MOMENTUM_ZERO 0
76 #define MOMENTUM_POSX 1
77 #define MOMENTUM_POSY 2
78 #define MOMENTUM_POSZ 3
79 #define MOMENTUM_NEGX 4
80 #define MOMENTUM_NEGY 5
81 #define MOMENTUM_NEGZ 6
91 /* =============================================================================
93 * =============================================================================
94 * router_t* router_alloc (long xCost, long yCost, long zCost, long bendCost);
96 public static Router alloc(int xCost,int yCost,int zCost,int bendCost)
98 Router routerPtr = new Router();
101 Point MOVE_POSX = new Point(1,0,0,0,MOMENTUM_POSX);
102 Point MOVE_POSY = new Point(0,1,0,0,MOMENTUM_POSY);
103 Point MOVE_POSZ = new Point(0,0,1,0,MOMENTUM_POSZ);
104 Point MOVE_NEGX = new Point(-1,0,0,0,MOMENTUM_NEGX);
105 Point MOVE_NEGY = new Point(0,-1,0,0,MOMENTUM_NEGY);
106 Point MOVE_NEGZ = new Point(0,0,-1,0,MOMENTUM_NEGZ);
108 if(routerPtr != null) {
109 routerPtr.xCost = xCost;
110 routerPtr.yCost = yCost;
111 routerPtr.zCost = zCost;
112 routerPtr.bendCost = bendCost;
121 /* =============================================================================
123 * =============================================================================
124 * void router_free (router_t* routerPtr);
126 public static void free(Router routerPtr)
131 /* ============================================================================
133 * ============================================================================
135 private void PexpandToNeighbor(Grid myGridPtr,
136 int x,int y,int z, int value,Queue queuePtr)
138 if (myGridPtr.isPointValid(x,y,z)) {
139 int neighborGridPointIndex = myGridPtr.getPointIndex(x,y,z);
140 int neighborValue = myGridPtr.getPoint(neighborGridPointIndex);
141 if (neighborValue == GRID_POINT_EMPTY) {
142 myGridPtr.setPoint(neighborGridPointIndex,value);
143 queuePtr.queue_push(neighborGridPointIndex);
144 } else if (neighborValue != GRID_POINT_FULL) {
146 if (value < neighborValue) {
147 myGridPtr.setPoint(neighborGridPointIndex,value);
148 queuePtr.queue_push(neighborGridPointIndex);
155 /* ============================================================================
157 * ============================================================================
159 private boolean PdoExpansion (Router routerPtr,Grid myGridPtr,Queue queuePtr,
160 Coordinate srcPtr,Coordinate dstPtr)
162 int xCost = routerPtr.xCost;
163 int yCost = routerPtr.yCost;
164 int zCost = routerPtr.zCost;
167 * Potential Optimization: Make 'src' the one closet to edge.
168 * This will likely decrease the area of the emitted wave.
171 queuePtr.queue_clear();
173 int srcGridPointIndex = myGridPtr.getPointIndex(srcPtr.x,srcPtr.y,srcPtr.z);
174 queuePtr.queue_push(srcGridPointIndex);
175 myGridPtr.setPoint(srcPtr.x,srcPtr.y,srcPtr.z,0);
176 myGridPtr.setPoint(dstPtr.x,dstPtr.y,dstPtr.z,GRID_POINT_EMPTY);
177 int dstGridPointIndex = myGridPtr.getPointIndex(dstPtr.x,dstPtr.y,dstPtr.z);
178 boolean isPathFound = false;
180 while (queuePtr.queue_isEmpty()) {
181 int gridPointIndex = queuePtr.queue_pop();
182 if(gridPointIndex == dstGridPointIndex) {
187 int[] x = new int[1];
188 int[] y = new int[1];
189 int[] z = new int[1];
191 myGridPtr.getPointIndices(gridPointIndex,x,y,z);
192 int value = myGridPtr.getPoint(gridPointIndex);
197 * Potential Optimization: Only need to check 5 of these
199 PexpandToNeighbor(myGridPtr, x[0]+1, y[0], z[0], (value + xCost), queuePtr);
200 PexpandToNeighbor(myGridPtr, x[0]-1, y[0], z[0], (value + xCost), queuePtr);
201 PexpandToNeighbor(myGridPtr, x[0], y[0]+1, z[0], (value + xCost), queuePtr);
202 PexpandToNeighbor(myGridPtr, x[0], y[0]-1, z[0], (value + xCost), queuePtr);
203 PexpandToNeighbor(myGridPtr, x[0], y[0], z[0]+1, (value + xCost), queuePtr);
204 PexpandToNeighbor(myGridPtr, x[0], y[0], z[0]-1, (value + xCost), queuePtr);
206 } /* iterate over work queue */
212 /* ============================================================================
214 * ============================================================================
216 private void traceToNeighbor(Grid myGridPtr,
223 int x = currPtr.x + movePtr.x;
224 int y = currPtr.y + movePtr.y;
225 int z = currPtr.z + movePtr.z;
227 if (myGridPtr.isPointValid(x,y,z) &&
228 !myGridPtr.isPointEmpty(x,y,z) &&
229 !myGridPtr.isPointFull(x,y,z))
231 int value = myGridPtr.getPoint(x,y,z);
234 if (useMomentum && (currPtr.momentum != movePtr.momentum)) {
237 if ((value + b) <= nextPtr.value) { /* '=' favors neighbors over current */
241 nextPtr.value = value;
242 nextPtr.momentum = movePtr.momentum;
246 /* =============================================================================
248 * =============================================================================
251 private Vector_t PdoTraceback(Grid gridPtr,Grid myGridPtr,
252 Coordinate dstPtr, int bendCost)
254 Vector_t pointVectorPtr = Vector.vector_alloc(1);
256 Point next = new Point();
260 next.value = myGridPtr.getPoint(next.x,next.y,next.z);
261 next.momentum = MOMENTUM_ZERO;
264 int gridPointIndex = gridPtr.getPointIndex(next.x,next.y,next.z);
265 pointVectorPtr.vector_pushBack(gridPointIndex);
266 myGridPtr.setPoint(next.x,next.y,next.z,GRID_POINT_FULL);
268 /* Check if we are done */
269 if (next.value == 0) {
278 traceToNeighbor(myGridPtr,curr,MOVE_POSX,true, bendCost, next);
279 traceToNeighbor(myGridPtr,curr,MOVE_POSY,true, bendCost, next);
280 traceToNeighbor(myGridPtr,curr,MOVE_POSZ,true, bendCost, next);
281 traceToNeighbor(myGridPtr,curr,MOVE_NEGX,true, bendCost, next);
282 traceToNeighbor(myGridPtr,curr,MOVE_NEGY,true, bendCost, next);
283 traceToNeighbor(myGridPtr,curr,MOVE_NEGZ,true, bendCost, next);
286 * Because of bend costs, none of the neighbors may appear to be closer.
287 * In this case, pick a neighbor while ignoring momentum.
290 if ((curr.x == next.y) &&
291 (curr.y == next.y) &&
294 next.value = curr.value;
295 traceToNeighbor(myGridPtr,curr,MOVE_POSX,false, bendCost, next);
296 traceToNeighbor(myGridPtr,curr,MOVE_POSY,false, bendCost, next);
297 traceToNeighbor(myGridPtr,curr,MOVE_POSZ,false, bendCost, next);
298 traceToNeighbor(myGridPtr,curr,MOVE_NEGX,false, bendCost, next);
299 traceToNeighbor(myGridPtr,curr,MOVE_NEGY,false, bendCost, next);
300 traceToNeighbor(myGridPtr,curr,MOVE_NEGZ,false, bendCost, next);
302 if ((curr.x == next.x) &&
303 (curr.y == next.y) &&
306 pointVectorPtr.vector_free();
312 return pointVectorPtr;
315 /* =============================================================================
317 * =============================================================================
318 * void router_solve (void* argPtr);
320 public static void solve(Object argPtr)
322 // TM_THREAD_ENTER();
323 Solve_Arg routerArgPtr = (SolveArg) argPtr;
324 Router routerPtr = routerArgPtr.routerPtr;
325 Maze mazePtr = routerArgPtr.mazePtr;
326 Vector_t myPathVectorPtr = Vector.alloc(1);
328 Queue workQueuePtr = mazePtr.workqueuePtr;
329 Grid gridPtr = mazePtr.gridPtr;
330 Grid myGridPtr = Grid.alloc(gridPtr.width,gridPtr.height,gridPtr.depth);
331 int bendCost = routerPtr.bendCost;
332 Queue myExpansionQueuePtr = Queue.queue_alloc(-1);
335 * Iterate over work list to route each path. This involves an
336 * 'expansion' and 'tracback' phase for each source/destination pair.
339 Pair coordinatePairPtr;
343 if(workQueuePtr.queue_isEmpty()) {
344 coordinatePairPtr = null;
346 coordinatePairPtr = (Pair)workQueuePtr.queue_pop();
352 if (coordinatePairPtr = null) {
356 Coordinate srcPtr = coordinatePairPtr.firstPtr;
357 Coordinate dstPtr = coordinatePairPtr.secondPtr;
359 boolean success = false;
360 Vector_t pointvectorPtr = null;
364 Grid.copy(myGridPtr, gridPtr); /* ok if not most up-to-date */
365 if (PdoExpansion(routerPtr, myGridPtr, myExpansionQueuePtr,
367 pointVectorPtr = PdoTraceback(gridPtr,myGridPtr,dstPtr,bendCost);
369 if (pointVectorPtr) {
370 gridPtr.addPath(pointVectorPtr);
377 boolean status = myPathVectorPtr.vector_pushBack(pointVectorPtr);
382 * Add my paths to global list
384 List_t pathVectorListPtr = routerArgPtr.pathVectorListPtr;
387 pathVectorListPtr.insert(myPathVectorPtr);
393 /* =============================================================================
397 * =============================================================================