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, double angle) {
110 Vector_t badVectorPtr = regionPtr.badVectorPtr; /* private */
111 List_t beforeListPtr = regionPtr.beforeListPtr; /* private */
112 List_t borderListPtr = regionPtr.borderListPtr; /* private */
115 yada.Assert(edgeMapPtr!=null);
117 coordinate centerCoordinate = elementPtr.element_getNewPoint();
120 * Remove the old triangles
123 List_Node it=beforeListPtr.head;
125 while (it.nextPtr!=null) {
127 element beforeElementPtr = (element)it.dataPtr;
128 meshPtr.TMmesh_remove(beforeElementPtr);
131 numDelta -= beforeListPtr.getSize();
134 * If segment is encroached, split it in half
137 if (elementPtr.element_getNumEdge() == 1) {
138 coordinate coordinates[]=new coordinate[2];
140 edge edgePtr = elementPtr.element_getEdge(0);
141 coordinates[0] = centerCoordinate;
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);
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);
153 boolean status = meshPtr.TMmesh_removeBoundary(elementPtr.element_getEdge(0));
155 status = meshPtr.TMmesh_insertBoundary(aElementPtr.element_getEdge(0));
157 status = meshPtr.TMmesh_insertBoundary(bElementPtr.element_getEdge(0));
164 * Insert the new triangles. These are contructed using the new
165 * point and the two points from the border segment.
168 it=borderListPtr.head;
169 while (it.nextPtr!=null) {
170 coordinate coordinates[]=new coordinate[3];
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);
184 numDelta += borderListPtr.getSize();
189 /* =============================================================================
191 * -- Return NULL if success, else pointer to encroached boundary
192 * =============================================================================
194 element TMgrowRegion(element centerElementPtr,
197 avltree edgeMapPtr) {
198 boolean isBoundary = false;
200 if(centerElementPtr.element_getNumEdge() == 1) {
204 List_t beforeListPtr = regionPtr.beforeListPtr;
205 List_t borderListPtr = regionPtr.borderListPtr;
206 Queue_t expandQueuePtr = regionPtr.expandQueuePtr;
208 beforeListPtr.clear();
209 borderListPtr.clear();
210 expandQueuePtr.queue_clear();
212 coordinate centerCoordinatePtr = centerElementPtr.element_getNewPoint();
214 expandQueuePtr.queue_push(centerElementPtr);
215 while (!expandQueuePtr.queue_isEmpty()) {
217 element currentElementPtr = (element) expandQueuePtr.queue_pop();
219 beforeListPtr.insert(currentElementPtr); /* no duplicates */
220 List_t neighborListPtr = currentElementPtr.element_getNeighborListPtr();
222 List_Node it=neighborListPtr.head;
223 while (it.nextPtr!=null) {
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;
234 /* Continue breadth-first search */
235 boolean isSuccess = expandQueuePtr.queue_push(neighborElementPtr);
236 yada.Assert(isSuccess);
239 /* This element borders region; save info for retriangulation */
240 edge borderEdgePtr = element.element_getCommonEdge(neighborElementPtr, currentElementPtr);
242 if (borderEdgePtr==null) {
245 borderListPtr.insert(borderEdgePtr); /* no duplicates */
246 if (!edgeMapPtr.contains(borderEdgePtr)) {
247 edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
250 } /* not visited before */
251 } /* for each neighbor */
253 } /* breadth-first search */
259 /* =============================================================================
261 * -- Returns net number of elements added to mesh
262 * =============================================================================
264 int TMregion_refine(element elementPtr, mesh meshPtr, double angle) {
266 avltree edgeMapPtr = null;
267 element encroachElementPtr = null;
269 elementPtr.element_isGarbage(); /* so we can detect conflicts */
272 edgeMapPtr = new avltree(0);
273 yada.Assert(edgeMapPtr!=null);
274 encroachElementPtr = TMgrowRegion(elementPtr,
279 if (encroachElementPtr!=null) {
280 encroachElementPtr.element_setIsReferenced(true);
281 numDelta += TMregion_refine(encroachElementPtr,
283 if (elementPtr.element_isGarbage()) {
292 * Perform retriangulation.
295 if (!elementPtr.element_isGarbage()) {
296 numDelta += TMretriangulate(elementPtr,
306 /* =============================================================================
308 * =============================================================================
310 void region_clearBad () {
311 badVectorPtr.vector_clear();
315 /* =============================================================================
316 * TMregion_transferBad
317 * =============================================================================
319 void region_transferBad(heap workHeapPtr) {
320 int numBad = badVectorPtr.vector_getSize();
322 for (int i = 0; i < numBad; i++) {
323 element badElementPtr = (element)badVectorPtr.vector_at(i);
324 if (badElementPtr.element_isGarbage()) {
326 boolean status = workHeapPtr.heap_insert(badElementPtr);