enough to parse
[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       edge borderEdgePtr = (edge)it.dataPtr;
172       yada.Assert(borderEdgePtr!=null);
173       coordinates[0] = centerCoordinate;
174       coordinates[1] = (coordinate)(borderEdgePtr.firstPtr);
175       coordinates[2] = (coordinate)(borderEdgePtr.secondPtr);
176       element afterElementPtr = new element(coordinates, 3, angle);
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)==null) {
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 = element.element_getCommonEdge(neighborElementPtr, currentElementPtr);
240
241             if (borderEdgePtr==null) {
242               Thread.abort();
243             }
244             borderListPtr.insert(borderEdgePtr); /* no duplicates */
245             if (!edgeMapPtr.contains(borderEdgePtr)) {
246               edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
247             }
248           }
249         } /* not visited before */
250       } /* for each neighbor */
251       
252     } /* breadth-first search */
253     
254     return null;
255   }
256
257
258 /* =============================================================================
259  * TMregion_refine
260  * -- Returns net number of elements added to mesh
261  * =============================================================================
262  */
263   int TMregion_refine(element elementPtr, mesh meshPtr, double angle) {
264     int numDelta = 0;
265     avltree edgeMapPtr = null;
266     element encroachElementPtr = null;
267     
268     elementPtr.element_isGarbage(); /* so we can detect conflicts */
269     
270     while (true) {
271       edgeMapPtr = new avltree(1);
272       yada.Assert(edgeMapPtr!=null);
273       encroachElementPtr = TMgrowRegion(elementPtr,
274                                         this,
275                                         meshPtr,
276                                         edgeMapPtr);
277       
278       if (encroachElementPtr!=null) {
279         encroachElementPtr.element_setIsReferenced(true);
280         numDelta += TMregion_refine(encroachElementPtr,
281                                     meshPtr, angle);
282         if (elementPtr.element_isGarbage()) {
283           break;
284         }
285       } else {
286         break;
287       }
288     }
289     
290     /*
291      * Perform retriangulation.
292      */
293     
294     if (!elementPtr.element_isGarbage()) {
295       numDelta += TMretriangulate(elementPtr,
296                                   this,
297                                   meshPtr,
298                                   edgeMapPtr, angle);
299     }
300     
301     return numDelta;
302   }
303
304
305   /* =============================================================================
306    * Pregion_clearBad
307    * =============================================================================
308    */
309   void region_clearBad () {
310     badVectorPtr.vector_clear();
311   }
312
313
314 /* =============================================================================
315  * TMregion_transferBad
316  * =============================================================================
317  */
318   void region_transferBad(heap workHeapPtr) {
319     int numBad = badVectorPtr.vector_getSize();
320     
321     for (int i = 0; i < numBad; i++) {
322       element badElementPtr = (element)badVectorPtr.vector_at(i);
323       if (badElementPtr.element_isGarbage()) {
324       } else {
325         boolean status = workHeapPtr.heap_insert(badElementPtr);
326         yada.Assert(status);
327       }
328     }
329   }
330 }