seems correct now
[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, double angle) {
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.dataPtr;
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, angle);
145       yada.Assert(aElementPtr!=null);
146       meshPtr.TMmesh_insert(aElementPtr, edgeMapPtr);
147       
148       coordinates[1] = (coordinate)edgePtr.secondPtr;
149       element bElementPtr = new element(coordinates, 2, angle);
150       yada.Assert(bElementPtr!=null);
151       meshPtr.TMmesh_insert(bElementPtr, edgeMapPtr);
152       
153       boolean status = meshPtr.TMmesh_removeBoundary(elementPtr.element_getEdge(0));
154       yada.Assert(status);
155       status = meshPtr.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 coordinate[3];
171       it=it.nextPtr;
172       edge borderEdgePtr = (edge)it.dataPtr;
173       yada.Assert(borderEdgePtr!=null);
174       coordinates[0] = centerCoordinate;
175       coordinates[1] = (coordinate)(borderEdgePtr.firstPtr);
176       coordinates[2] = (coordinate)(borderEdgePtr.secondPtr);
177       element afterElementPtr = new element(coordinates, 3, angle);
178       yada.Assert(afterElementPtr!=null);
179       meshPtr.TMmesh_insert(afterElementPtr, edgeMapPtr);
180       if (afterElementPtr.element_isBad()) {
181         TMaddToBadVector(badVectorPtr, afterElementPtr);
182       }
183     }
184     numDelta += borderListPtr.getSize();
185     return numDelta;
186   }
187   
188
189   /* =============================================================================
190    * TMgrowRegion
191    * -- Return NULL if success, else pointer to encroached boundary
192    * =============================================================================
193    */
194   element TMgrowRegion(element centerElementPtr,
195                        region regionPtr,
196                        mesh meshPtr,
197                        avltree edgeMapPtr) {
198     boolean isBoundary = false;
199     
200     if(centerElementPtr.element_getNumEdge() == 1) {
201       isBoundary = true;
202     }
203   
204     List_t beforeListPtr = regionPtr.beforeListPtr;
205     List_t borderListPtr = regionPtr.borderListPtr;
206     Queue_t expandQueuePtr = regionPtr.expandQueuePtr;
207     
208     beforeListPtr.clear();
209     borderListPtr.clear();
210     expandQueuePtr.queue_clear();
211     
212     coordinate centerCoordinatePtr = centerElementPtr.element_getNewPoint();
213     
214     expandQueuePtr.queue_push(centerElementPtr);
215     while (!expandQueuePtr.queue_isEmpty()) {
216       
217       element currentElementPtr = (element) expandQueuePtr.queue_pop();
218       
219       beforeListPtr.insert(currentElementPtr); /* no duplicates */
220       List_t neighborListPtr = currentElementPtr.element_getNeighborListPtr();
221       
222       List_Node it=neighborListPtr.head;
223       while (it.nextPtr!=null) {
224         it=it.nextPtr;
225         element neighborElementPtr = (element)it.dataPtr;
226         neighborElementPtr.element_isGarbage(); /* so we can detect conflicts */
227         if (beforeListPtr.find(neighborElementPtr)==null) {
228           if (neighborElementPtr.element_isInCircumCircle(centerCoordinatePtr)) {
229             /* This is part of the region */
230             if (!isBoundary && (neighborElementPtr.element_getNumEdge() == 1)) {
231               /* Encroached on mesh boundary so split it and restart */
232               return neighborElementPtr;
233             } else {
234               /* Continue breadth-first search */
235               boolean isSuccess = expandQueuePtr.queue_push(neighborElementPtr);
236               yada.Assert(isSuccess);
237             }
238           } else {
239             /* This element borders region; save info for retriangulation */
240             edge borderEdgePtr = element.element_getCommonEdge(neighborElementPtr, currentElementPtr);
241
242             if (borderEdgePtr==null) {
243               //              Thread.abort();
244             }
245             borderListPtr.insert(borderEdgePtr); /* no duplicates */
246             if (!edgeMapPtr.contains(borderEdgePtr)) {
247               edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
248             }
249           }
250         } /* not visited before */
251       } /* for each neighbor */
252       
253     } /* breadth-first search */
254     
255     return null;
256   }
257
258
259 /* =============================================================================
260  * TMregion_refine
261  * -- Returns net number of elements added to mesh
262  * =============================================================================
263  */
264   int TMregion_refine(element elementPtr, mesh meshPtr, double angle) {
265     int numDelta = 0;
266     avltree edgeMapPtr = null;
267     element encroachElementPtr = null;
268     
269     elementPtr.element_isGarbage(); /* so we can detect conflicts */
270     
271     while (true) {
272       edgeMapPtr = new avltree(0);
273       yada.Assert(edgeMapPtr!=null);
274       encroachElementPtr = TMgrowRegion(elementPtr,
275                                         this,
276                                         meshPtr,
277                                         edgeMapPtr);
278       
279       if (encroachElementPtr!=null) {
280         encroachElementPtr.element_setIsReferenced(true);
281         numDelta += TMregion_refine(encroachElementPtr,
282                                     meshPtr, angle);
283         if (elementPtr.element_isGarbage()) {
284           break;
285         }
286       } else {
287         break;
288       }
289     }
290     
291     /*
292      * Perform retriangulation.
293      */
294     
295     if (!elementPtr.element_isGarbage()) {
296       numDelta += TMretriangulate(elementPtr,
297                                   this,
298                                   meshPtr,
299                                   edgeMapPtr, angle);
300     }
301     
302     return numDelta;
303   }
304
305
306   /* =============================================================================
307    * Pregion_clearBad
308    * =============================================================================
309    */
310   void region_clearBad () {
311     badVectorPtr.vector_clear();
312   }
313
314
315 /* =============================================================================
316  * TMregion_transferBad
317  * =============================================================================
318  */
319   void region_transferBad(heap workHeapPtr) {
320     int numBad = badVectorPtr.vector_getSize();
321     
322     for (int i = 0; i < numBad; i++) {
323       element badElementPtr = (element)badVectorPtr.vector_at(i);
324       if (badElementPtr.element_isGarbage()) {
325       } else {
326         boolean status = workHeapPtr.heap_insert(badElementPtr);
327         yada.Assert(status);
328       }
329     }
330   }
331 }