almost done with port
[IRC.git] / Robust / src / Benchmarks / SingleTM / Yada / region.java
1 /* =============================================================================
2  *
3  * region.c
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006.  All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  * 
15  * ------------------------------------------------------------------------
16  * 
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  * 
19  * ------------------------------------------------------------------------
20  * 
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  * 
33  * ------------------------------------------------------------------------
34  * 
35  * Unless otherwise noted, the following license applies to STAMP files:
36  * 
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  * 
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  * 
44  *     * Redistributions of source code must retain the above copyright
45  *       notice, this list of conditions and the following disclaimer.
46  * 
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
50  *       distribution.
51  * 
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.
55  * 
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.
67  *
68  * =============================================================================
69  */
70
71 public class region {
72   coordinate centerCoordinate;
73   Queue_t expandQueuePtr;
74   List_t beforeListPtr; /* before retriangulation; list to avoid duplicates */
75   List_t borderListPtr; /* edges adjacent to region; list to avoid duplicates */
76   Vector_t badVectorPtr;
77
78 /* =============================================================================
79  * Pregion_alloc
80  * =============================================================================
81  */
82   public region() {
83     expandQueuePtr = new Queue_t(-1);
84     beforeListPtr = new List_t(0);//PLIST_ALLOC(element_listCompare);
85     borderListPtr = new List_t(1);//PLIST_ALLOC(element_listCompareEdge);
86     badVectorPtr = new Vector_t(1);
87   }
88
89
90   /* =============================================================================
91    * TMaddToBadVector
92    * =============================================================================
93    */
94   public void TMaddToBadVector(Vector_t badVectorPtr, element badElementPtr) {
95     boolean status = badVectorPtr.vector_pushBack(badElementPtr);
96     yada.Assert(status);
97     badElementPtr.element_setIsReferenced(true);
98   }
99
100
101   /* =============================================================================
102    * TMretriangulate
103    * -- Returns net amount of elements added to mesh
104    * =============================================================================
105    */
106   public int TMretriangulate (element elementPtr,
107                               region regionPtr,
108                               mesh meshPtr,
109                               avltree edgeMapPtr) {
110     Vector_t badVectorPtr = regionPtr.badVectorPtr; /* private */
111     List_t beforeListPtr = regionPtr.beforeListPtr; /* private */
112     List_t borderListPtr = regionPtr.borderListPtr; /* private */
113     int numDelta = 0;
114     
115     yada.Assert(edgeMapPtr!=null);
116     
117     coordinate centerCoordinate = elementPtr.element_getNewPoint();
118     
119     /*
120      * Remove the old triangles
121      */
122     
123     List_Node it=beforeListPtr.head;
124
125     while (it.nextPtr!=null) {
126       it=it.nextPtr;
127       element beforeElementPtr = (element)it.nodePtr;
128       meshPtr.TMmesh_remove(beforeElementPtr);
129     }
130     
131     numDelta -= beforeListPtr.getSize();
132     
133     /*
134      * If segment is encroached, split it in half
135      */
136     
137     if (elementPtr.element_getNumEdge() == 1) {
138       coordinate coordinates[]=new coordinate[2];
139       
140       edge edgePtr = elementPtr.element_getEdge(0);
141       coordinates[0] = centerCoordinate;
142       
143       coordinates[1] = (coordinate)(edgePtr.firstPtr);
144       element aElementPtr = new element(coordinates, 2);
145       yada.Assert(aElementPtr);
146       meshPtr.TMmesh_insert(aElementPtr, edgeMapPtr);
147       
148       coordinates[1] = (coordinate)edgePtr.secondPtr;
149       element bElementPtr = new element(coordinates, 2);
150       yada.Assert(bElementPtr);
151       meshPtr.TMmesh_insert(bElementPtr, edgeMapPtr);
152       
153       boolean status = meshPtr.TMmesh_removeBoundary(elementPtr.element_getEdge(0));
154       yada.Assert(status);
155       status = mesPtr.TMmesh_insertBoundary(aElementPtr.element_getEdge(0));
156       yada.Assert(status);
157       status = meshPtr.TMmesh_insertBoundary(bElementPtr.element_getEdge(0));
158       yada.Assert(status);
159       
160       numDelta += 2;
161     }
162     
163     /*
164      * Insert the new triangles. These are contructed using the new
165      * point and the two points from the border segment.
166      */
167
168     it=borderListPtr.head;
169     while (it.nextPtr!=null) {
170       coordinate coordinates[]=new coordinates[3];
171       edge borderEdgePtr = (edge)it.dataPtr;
172       yada.Assert(borderEdgePtr);
173       coordinates[0] = centerCoordinate;
174       coordinates[1] = (coordinate)(borderEdgePtr.firstPtr);
175       coordinates[2] = (coordinate)(borderEdgePtr.secondPtr);
176       element afterElementPtr = new element(coordinates, 3);
177       yada.Assert(afterElementPtr!=null);
178       meshPtr.TMmesh_insert(afterElementPtr, edgeMapPtr);
179       if (afterElementPTr.element_isBad()) {
180         TMaddToBadVector(badVectorPtr, afterElementPtr);
181       }
182     }
183     numDelta += borderListPtr.getSize();
184     return numDelta;
185   }
186   
187
188   /* =============================================================================
189    * TMgrowRegion
190    * -- Return NULL if success, else pointer to encroached boundary
191    * =============================================================================
192    */
193   element TMgrowRegion(element centerElementPtr,
194                        region regionPtr,
195                        mesh meshPtr,
196                        avltree edgeMapPtr) {
197     boolean isBoundary = false;
198     
199     if (centerElementPtr.element_getNumEdge() == 1) {
200       isBoundary = true;
201     }
202   
203     List_t beforeListPtr = regionPtr.beforeListPtr;
204     List_t borderListPtr = regionPtr.borderListPtr;
205     Queue_t expandQueuePtr = regionPtr.expandQueuePtr;
206     
207     beforeListPtr.clear();
208     borderListPtr.clear();
209     expandQueuePtr.queue_clear();
210     
211     coordinate centerCoordinatePtr = centerElementPtr.element_getNewPoint();
212     
213     expandQueuePtr.queue_push(centerElementPtr);
214     while (!expandQueuePtr.queue_isEmpty()) {
215       
216       element currentElementPtr = (element) expandQueuePtr.queue_pop();
217       
218       beforeListPtr.insert(currentElementPtr); /* no duplicates */
219       List_t neighborListPtr = currentElementPtr.element_getNeighborListPtr();
220       
221       List_Node it=neighborListPtr.head;
222       while (it.nextPtr!=null) {
223         it=it.nextPtr;
224         element neighborElementPtr = (element)it.dataPtr;
225         neighborElementPtr.element_isGarbage(); /* so we can detect conflicts */
226         if (!beforeListPtr.find(neighborElementPtr)) {
227           if (neighborElementPtr.element_isInCircumCircle(centerCoordinatePtr)) {
228             /* This is part of the region */
229             if (!isBoundary && (neighborElementPtr.element_getNumEdge() == 1)) {
230               /* Encroached on mesh boundary so split it and restart */
231               return neighborElementPtr;
232             } else {
233               /* Continue breadth-first search */
234               boolean isSuccess = expandQueuePtr.queue_push(neighborElementPtr);
235               yada.Assert(isSuccess);
236             }
237           } else {
238             /* This element borders region; save info for retriangulation */
239             edge borderEdgePtr = neighborElementPtr.element_getCommonEdge(currentElementPtr);
240             if (!borderEdgePtr) {
241               TM_RESTART();
242             }
243             borderListPtr.insert(borderEdgePtr); /* no duplicates */
244             if (!edgeMapPtr.contains(borderEdgePtr)) {
245               edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
246             }
247           }
248         } /* not visited before */
249       } /* for each neighbor */
250       
251     } /* breadth-first search */
252     
253     return null;
254   }
255
256
257 /* =============================================================================
258  * TMregion_refine
259  * -- Returns net number of elements added to mesh
260  * =============================================================================
261  */
262   int TMregion_refine(element elementPtr, mesh meshPtr) {
263     int numDelta = 0;
264     avltree edgeMapPtr = null;
265     element encroachElementPtr = null;
266     
267     elementPtr.element_isGarbage(); /* so we can detect conflicts */
268     
269     while (true) {
270       edgeMapPtr = new avltree(1);
271       yada.Assert(edgeMapPtr!=null);
272       encroachElementPtr = TMgrowRegion(elementPtr,
273                                         this,
274                                         meshPtr,
275                                         edgeMapPtr);
276       
277       if (encroachElementPtr!=null) {
278         encroachElementPtr.element_setIsReferenced(true);
279         numDelta += TMregion_refine(encroachElementPtr,
280                                     meshPtr);
281         if (elementPtr.element_isGarbage()) {
282           break;
283         }
284       } else {
285         break;
286       }
287     }
288     
289     /*
290      * Perform retriangulation.
291      */
292     
293     if (!elementPtr.element_isGarbage()) {
294       numDelta += TMretriangulate(elementPtr,
295                                   this,
296                                   meshPtr,
297                                   edgeMapPtr);
298     }
299     
300     return numDelta;
301   }
302
303
304   /* =============================================================================
305    * Pregion_clearBad
306    * =============================================================================
307    */
308   void region_clearBad () {
309     badVectorPtr.vector_clear();
310   }
311
312
313 /* =============================================================================
314  * TMregion_transferBad
315  * =============================================================================
316  */
317   void region_transferBad(heap workHeapPtr) {
318     int numBad = badVectorPtr.vector_getSize();
319     
320     for (int i = 0; i < numBad; i++) {
321       element badElementPtr = (element)badVectorPtr.vector_at(i);
322       if (badElementPtr.element_isGarbage()) {
323       } else {
324         boolean status = workHeapPtr.heap_insert(badElementPtr);
325         yada.Assert(status);
326       }
327     }
328   }
329 }