001: /*
002: * $RCSfile: Clean.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:18 $
042: * $State: Exp $
043: */
044:
045: // ----------------------------------------------------------------------
046: //
047: // The reference to Fast Industrial Strength Triangulation (FIST) code
048: // in this release by Sun Microsystems is related to Sun's rewrite of
049: // an early version of FIST. FIST was originally created by Martin
050: // Held and Joseph Mitchell at Stony Brook University and is
051: // incorporated by Sun under an agreement with The Research Foundation
052: // of SUNY (RFSUNY). The current version of FIST is available for
053: // commercial use under a license agreement with RFSUNY on behalf of
054: // the authors and Stony Brook University. Please contact the Office
055: // of Technology Licensing at Stony Brook, phone 631-632-9009, for
056: // licensing information.
057: //
058: // ----------------------------------------------------------------------
059: package com.sun.j3d.utils.geometry;
060:
061: import java.io.*;
062: import java.util.*;
063: import javax.vecmath.*;
064:
065: class Clean {
066:
067: static void initPUnsorted(Triangulator triRef, int number) {
068: if (number > triRef.maxNumPUnsorted) {
069: triRef.maxNumPUnsorted = number;
070: triRef.pUnsorted = new Point2f[triRef.maxNumPUnsorted];
071: for (int i = 0; i < triRef.maxNumPUnsorted; i++)
072: triRef.pUnsorted[i] = new Point2f();
073: }
074: }
075:
076: static int cleanPolyhedralFace(Triangulator triRef, int i1, int i2) {
077: int removed;
078: int i, j, numSorted, index;
079: int ind1, ind2;
080:
081: initPUnsorted(triRef, triRef.numPoints);
082:
083: for (i = 0; i < triRef.numPoints; ++i)
084: triRef.pUnsorted[i].set(triRef.points[i]);
085:
086: // sort points according to lexicographical order
087: /*
088: System.out.println("Points : (Unsorted)");
089: for(i=0; i<triRef.numPoints; i++)
090: System.out.println( i + "pt ( " + triRef.points[i].x + ", " +
091: triRef.points[i].y + ")");
092: */
093:
094: // qsort(points, num_pnts, sizeof(point), &p_comp);
095: sort(triRef.points, triRef.numPoints);
096:
097: /*
098: System.out.println("Points : (Sorted)");
099: for(i=0; i<triRef.numPoints; i++)
100: System.out.println( i +"pt ( " + triRef.points[i].x + ", " +
101: triRef.points[i].y + ")");
102: */
103:
104: // eliminate duplicate vertices
105: i = 0;
106: for (j = 1; j < triRef.numPoints; ++j) {
107: if (pComp(triRef.points[i], triRef.points[j]) != 0) {
108: ++i;
109: triRef.points[i] = triRef.points[j];
110: }
111: }
112: numSorted = i + 1;
113: removed = triRef.numPoints - numSorted;
114:
115: /*
116: System.out.println("Points : (Sorted and eliminated)");
117: for(i=0; i<triRef.numPoints; i++)
118: System.out.println( i + "pt ( " + triRef.points[i].x + ", " +
119: triRef.points[i].y + ")");
120: */
121:
122: // renumber the vertices of the polygonal face
123: for (i = i1; i < i2; ++i) {
124: ind1 = triRef.loops[i];
125: ind2 = triRef.fetchNextData(ind1);
126: index = triRef.fetchData(ind2);
127: while (ind2 != ind1) {
128: j = findPInd(triRef.points, numSorted,
129: triRef.pUnsorted[index]);
130: triRef.updateIndex(ind2, j);
131: ind2 = triRef.fetchNextData(ind2);
132: index = triRef.fetchData(ind2);
133: }
134: j = findPInd(triRef.points, numSorted,
135: triRef.pUnsorted[index]);
136: triRef.updateIndex(ind2, j);
137: }
138:
139: triRef.numPoints = numSorted;
140:
141: return removed;
142: }
143:
144: static void sort(Point2f points[], int numPts) {
145: int i, j;
146: Point2f swap = new Point2f();
147:
148: for (i = 0; i < numPts; i++) {
149: for (j = i + 1; j < numPts; j++) {
150: if (pComp(points[i], points[j]) > 0) {
151: swap.set(points[i]);
152: points[i].set(points[j]);
153: points[j].set(swap);
154: }
155: }
156: }
157: /*
158: for (i = 0; i < numPts; i++) {
159: System.out.println("pt " + points[i]);
160: }
161: */
162: }
163:
164: static int findPInd(Point2f sorted[], int numPts, Point2f pnt) {
165: int i;
166:
167: for (i = 0; i < numPts; i++) {
168: if ((pnt.x == sorted[i].x) && (pnt.y == sorted[i].y)) {
169: return i;
170: }
171: }
172: return -1;
173: }
174:
175: static int pComp(Point2f a, Point2f b) {
176: if (a.x < b.x)
177: return -1;
178: else if (a.x > b.x)
179: return 1;
180: else {
181: if (a.y < b.y)
182: return -1;
183: else if (a.y > b.y)
184: return 1;
185: else
186: return 0;
187: }
188: }
189:
190: }
|