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 * =============================================================================
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;
78 /* =============================================================================
80 * =============================================================================
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);
90 /* =============================================================================
92 * =============================================================================
94 public void TMaddToBadVector(Vector_t badVectorPtr, element badElementPtr) {
95 boolean status = badVectorPtr.vector_pushBack(badElementPtr);
97 badElementPtr.element_setIsReferenced(true);
101 /* =============================================================================
103 * -- Returns net amount of elements added to mesh
104 * =============================================================================
106 public int TMretriangulate (element elementPtr,
109 avltree edgeMapPtr) {
110 Vector_t badVectorPtr = regionPtr.badVectorPtr; /* private */
111 List_t beforeListPtr = regionPtr.beforeListPtr; /* private */
112 List_t borderListPtr = regionPtr.borderListPtr; /* private */
116 yada.Assert(edgeMapPtr);
118 coordinate centerCoordinate = elementPtr.element_getNewPoint();
121 * Remove the old triangles
124 list_iter_reset(it, beforeListPtr);
125 while (list_iter_hasNext(it, beforeListPtr)) {
126 element beforeElementPtr = (element)list_iter_next(it, beforeListPtr);
127 meshPtr.TMmesh_remove(beforeElementPtr);
130 numDelta -= beforeListPtr.getSize();
133 * If segment is encroached, split it in half
136 if (elementPtr.element_getNumEdge() == 1) {
137 coordinate coordinates[]=new coordinate[2];
139 edge edgePtr = elementPtr.element_getEdge(0);
140 coordinates[0] = centerCoordinate;
142 coordinates[1] = (coordinate)(edgePtr.firstPtr);
143 element aElementPtr = new element(coordinates, 2);
144 yada.Assert(aElementPtr);
145 meshPtr.TMmesh_insert(aElementPtr, edgeMapPtr);
147 coordinates[1] = (coordinate)edgePtr.secondPtr;
148 element bElementPtr = new element(coordinates, 2);
149 yada.Assert(bElementPtr);
150 meshPtr.TMmesh_insert(bElementPtr, edgeMapPtr);
152 boolean status = meshPtr.TMmesh_removeBoundary(elementPtr.element_getEdge(0));
154 status = mesPtr.TMmesh_insertBoundary(aElementPtr.element_getEdge(0));
156 status = meshPtr.TMmesh_insertBoundary(bElementPtr.element_getEdge(0));
163 * Insert the new triangles. These are contructed using the new
164 * point and the two points from the border segment.
167 list_iter_reset(it, borderListPtr);
168 while (list_iter_hasNext(it, borderListPtr)) {
169 coordinate coordinates[]=new coordinates[3];
170 edge borderEdgePtr = (edge)list_iter_next(it, borderListPtr);
171 yada.Assert(borderEdgePtr);
172 coordinates[0] = centerCoordinate;
173 coordinates[1] = (coordinate)(borderEdgePtr.firstPtr);
174 coordinates[2] = (coordinate)(borderEdgePtr.secondPtr);
175 element afterElementPtr = new element(coordinates, 3);
176 yada.Assert(afterElementPtr!=null);
177 meshPtr.TMmesh_insert(afterElementPtr, edgeMapPtr);
178 if (afterElementPTr.element_isBad()) {
179 TMaddToBadVector(badVectorPtr, afterElementPtr);
182 numDelta += borderListPtr.getSize();
187 /* =============================================================================
189 * -- Return NULL if success, else pointer to encroached boundary
190 * =============================================================================
192 element TMgrowRegion(element centerElementPtr,
195 avltree edgeMapPtr) {
196 boolean isBoundary = false;
198 if (centerElementPtr.element_getNumEdge() == 1) {
202 List_t beforeListPtr = regionPtr.beforeListPtr;
203 List_t borderListPtr = regionPtr.borderListPtr;
204 Queue_t expandQueuePtr = regionPtr.expandQueuePtr;
206 beforeListPtr.clear();
207 borderListPtr.clear();
208 expandQueuePtr.queue_clear();
210 coordinate centerCoordinatePtr = centerElementPtr.element_getNewPoint();
212 expandQueuePtr.queue_push(centerElementPtr);
213 while (!expandQueuePtr.queue_isEmpty()) {
215 element currentElementPtr = (element) expandQueuePtr.queue_pop();
217 beforeListPtr.insert(currentElementPtr); /* no duplicates */
218 List_t neighborListPtr = currentElementPtr.element_getNeighborListPtr();
221 TMLIST_ITER_RESET(it, neighborListPtr);
222 while (TMLIST_ITER_HASNEXT(it, neighborListPtr)) {
223 element neighborElementPtr = (element)TMLIST_ITER_NEXT(it, neighborListPtr);
224 neighborElementPtr.element_isGarbage(); /* so we can detect conflicts */
225 if (!beforeListPtr.find(neighborElementPtr)) {
226 if (neighborElementPtr.element_isInCircumCircle(centerCoordinatePtr)) {
227 /* This is part of the region */
228 if (!isBoundary && (neighborElementPtr.element_getNumEdge() == 1)) {
229 /* Encroached on mesh boundary so split it and restart */
230 return neighborElementPtr;
232 /* Continue breadth-first search */
233 boolean isSuccess = expandQueuePtr.queue_push(neighborElementPtr);
234 yada.Assert(isSuccess);
237 /* This element borders region; save info for retriangulation */
238 edge borderEdgePtr = neighborElementPtr.element_getCommonEdge(currentElementPtr);
239 if (!borderEdgePtr) {
242 borderListPtr.insert(borderEdgePtr); /* no duplicates */
243 if (!edgeMapPtr.contains(borderEdgePtr)) {
244 edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
247 } /* not visited before */
248 } /* for each neighbor */
250 } /* breadth-first search */
256 /* =============================================================================
258 * -- Returns net number of elements added to mesh
259 * =============================================================================
261 int TMregion_refine(element elementPtr, mesh meshPtr) {
263 avltree edgeMapPtr = null;
264 element encroachElementPtr = null;
266 elementPtr.element_isGarbage(); /* so we can detect conflicts */
269 edgeMapPtr = new avltree(1);
270 yada.Assert(edgeMapPtr!=null);
271 encroachElementPtr = TMgrowRegion(elementPtr,
276 if (encroachElementPtr!=null) {
277 encroachElementPtr.element_setIsReferenced(true);
278 numDelta += TMregion_refine(regionPtr,
281 if (elementPtr.elementisGarbage()) {
290 * Perform retriangulation.
293 if (!elementPtr.element_isGarbage()) {
294 numDelta += TMretriangulate(elementPtr,
304 /* =============================================================================
306 * =============================================================================
308 void region_clearBad () {
309 badVectorPtr.vector_clear();
313 /* =============================================================================
314 * TMregion_transferBad
315 * =============================================================================
317 void region_transferBad(heap workHeapPtr) {
318 int numBad = badVectorPtr.vector_getSize();
320 for (int i = 0; i < numBad; i++) {
321 element badElementPtr = (element)badVectorPtr.vector_at(i);
322 if (badElementPtr.element_isGarbage()) {
324 boolean status = workHeapPtr.heap_insert(badElementPtr);