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];
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);
183 numDelta += borderListPtr.getSize();
188 /* =============================================================================
190 * -- Return NULL if success, else pointer to encroached boundary
191 * =============================================================================
193 element TMgrowRegion(element centerElementPtr,
196 avltree edgeMapPtr) {
197 boolean isBoundary = false;
199 if(centerElementPtr.element_getNumEdge() == 1) {
203 List_t beforeListPtr = regionPtr.beforeListPtr;
204 List_t borderListPtr = regionPtr.borderListPtr;
205 Queue_t expandQueuePtr = regionPtr.expandQueuePtr;
207 beforeListPtr.clear();
208 borderListPtr.clear();
209 expandQueuePtr.queue_clear();
211 coordinate centerCoordinatePtr = centerElementPtr.element_getNewPoint();
213 expandQueuePtr.queue_push(centerElementPtr);
214 while (!expandQueuePtr.queue_isEmpty()) {
216 element currentElementPtr = (element) expandQueuePtr.queue_pop();
218 beforeListPtr.insert(currentElementPtr); /* no duplicates */
219 List_t neighborListPtr = currentElementPtr.element_getNeighborListPtr();
221 List_Node it=neighborListPtr.head;
222 while (it.nextPtr!=null) {
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;
233 /* Continue breadth-first search */
234 boolean isSuccess = expandQueuePtr.queue_push(neighborElementPtr);
235 yada.Assert(isSuccess);
238 /* This element borders region; save info for retriangulation */
239 edge borderEdgePtr = element.element_getCommonEdge(neighborElementPtr, currentElementPtr);
241 if (borderEdgePtr==null) {
244 borderListPtr.insert(borderEdgePtr); /* no duplicates */
245 if (!edgeMapPtr.contains(borderEdgePtr)) {
246 edgeMapPtr.insert(borderEdgePtr, neighborElementPtr);
249 } /* not visited before */
250 } /* for each neighbor */
252 } /* breadth-first search */
258 /* =============================================================================
260 * -- Returns net number of elements added to mesh
261 * =============================================================================
263 int TMregion_refine(element elementPtr, mesh meshPtr, double angle) {
265 avltree edgeMapPtr = null;
266 element encroachElementPtr = null;
268 elementPtr.element_isGarbage(); /* so we can detect conflicts */
271 edgeMapPtr = new avltree(1);
272 yada.Assert(edgeMapPtr!=null);
273 encroachElementPtr = TMgrowRegion(elementPtr,
278 if (encroachElementPtr!=null) {
279 encroachElementPtr.element_setIsReferenced(true);
280 numDelta += TMregion_refine(encroachElementPtr,
282 if (elementPtr.element_isGarbage()) {
291 * Perform retriangulation.
294 if (!elementPtr.element_isGarbage()) {
295 numDelta += TMretriangulate(elementPtr,
305 /* =============================================================================
307 * =============================================================================
309 void region_clearBad () {
310 badVectorPtr.vector_clear();
314 /* =============================================================================
315 * TMregion_transferBad
316 * =============================================================================
318 void region_transferBad(heap workHeapPtr) {
319 int numBad = badVectorPtr.vector_getSize();
321 for (int i = 0; i < numBad; i++) {
322 element badElementPtr = (element)badVectorPtr.vector_at(i);
323 if (badElementPtr.element_isGarbage()) {
325 boolean status = workHeapPtr.heap_insert(badElementPtr);