001: /*
002: * $RCSfile: Simple.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:20 $
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 Simple {
066:
067: /**
068: * Handle a triangle or a quadrangle in a simple and efficient way. if the
069: * face is more complex than false is returned.
070: *
071: * warning: the correctness of this function depends upon the fact that
072: * `CleanPolyhedralFace' has not yet been executed; i.e., the
073: * vertex indices have not been changed since the execution of
074: * `CleanPolyhedron'! (otherwise, we would have to get the original
075: * indices via calls to `GetOriginal'...)
076: */
077: static boolean simpleFace(Triangulator triRef, int ind1) {
078: int ind0, ind2, ind3, ind4;
079: int i1, i2, i3, i0, i4;
080:
081: Point3f pq, pr, nr;
082:
083: double x, y, z;
084: int ori2, ori4;
085:
086: ind0 = triRef.fetchPrevData(ind1);
087: i0 = triRef.fetchData(ind0);
088:
089: if (ind0 == ind1) {
090: // this polygon has only one vertex! nothing to triangulate...
091: System.out
092: .println("***** polygon with only one vertex?! *****\n");
093: return true;
094: }
095:
096: ind2 = triRef.fetchNextData(ind1);
097: i2 = triRef.fetchData(ind2);
098: if (ind0 == ind2) {
099: // this polygon has only two vertices! nothing to triangulate...
100: System.out
101: .println("***** polygon with only two vertices?! *****\n");
102: return true;
103: }
104:
105: ind3 = triRef.fetchNextData(ind2);
106: i3 = triRef.fetchData(ind3);
107: if (ind0 == ind3) {
108: // this polygon is a triangle! let's triangulate it!
109: i1 = triRef.fetchData(ind1);
110: // triRef.storeTriangle(i1, i2, i3);
111: triRef.storeTriangle(ind1, ind2, ind3);
112: return true;
113: }
114:
115: ind4 = triRef.fetchNextData(ind3);
116: i4 = triRef.fetchData(ind4);
117: if (ind0 == ind4) {
118: // this polygon is a quadrangle! not too hard to triangulate it...
119: // we project the corners of the quadrangle onto one of the coordinate
120: // planes
121: triRef.initPnts(5);
122: i1 = triRef.fetchData(ind1);
123:
124: pq = new Point3f();
125: pr = new Point3f();
126: nr = new Point3f();
127: /*
128: System.out.println("ind0 " + ind0 + ", ind1 " + ind1 + ", ind2 " +
129: ind2 + ", ind3 " + ind3 + ", ind4 " + ind4);
130: System.out.println("i0 " + i0 +", i1 " + i1 + ", i2 " + i2 +
131: ", i3 " + i3 + ", i4 " + i4);
132:
133: System.out.println("vert[i1] " + triRef.vertices[i1] +
134: "vert[i2] " + triRef.vertices[i2] +
135: "vert[i3] " + triRef.vertices[i3]);
136: */
137:
138: Basic.vectorSub(triRef.vertices[i1], triRef.vertices[i2],
139: pq);
140: Basic.vectorSub(triRef.vertices[i3], triRef.vertices[i2],
141: pr);
142: Basic.vectorProduct(pq, pr, nr);
143:
144: // System.out.println("pq " + pq + " pr " + pr + " nr " + nr);
145: x = Math.abs(nr.x);
146: y = Math.abs(nr.y);
147: z = Math.abs(nr.z);
148: if ((z >= x) && (z >= y)) {
149: // System.out.println("((z >= x) && (z >= y))");
150: triRef.points[1].x = triRef.vertices[i1].x;
151: triRef.points[1].y = triRef.vertices[i1].y;
152: triRef.points[2].x = triRef.vertices[i2].x;
153: triRef.points[2].y = triRef.vertices[i2].y;
154: triRef.points[3].x = triRef.vertices[i3].x;
155: triRef.points[3].y = triRef.vertices[i3].y;
156: triRef.points[4].x = triRef.vertices[i4].x;
157: triRef.points[4].y = triRef.vertices[i4].y;
158: } else if ((x >= y) && (x >= z)) {
159: // System.out.println("((x >= y) && (x >= z))");
160: triRef.points[1].x = triRef.vertices[i1].z;
161: triRef.points[1].y = triRef.vertices[i1].y;
162: triRef.points[2].x = triRef.vertices[i2].z;
163: triRef.points[2].y = triRef.vertices[i2].y;
164: triRef.points[3].x = triRef.vertices[i3].z;
165: triRef.points[3].y = triRef.vertices[i3].y;
166: triRef.points[4].x = triRef.vertices[i4].z;
167: triRef.points[4].y = triRef.vertices[i4].y;
168: } else {
169: triRef.points[1].x = triRef.vertices[i1].x;
170: triRef.points[1].y = triRef.vertices[i1].z;
171: triRef.points[2].x = triRef.vertices[i2].x;
172: triRef.points[2].y = triRef.vertices[i2].z;
173: triRef.points[3].x = triRef.vertices[i3].x;
174: triRef.points[3].y = triRef.vertices[i3].z;
175: triRef.points[4].x = triRef.vertices[i4].x;
176: triRef.points[4].y = triRef.vertices[i4].z;
177: }
178: triRef.numPoints = 5;
179:
180: // find a valid diagonal
181: ori2 = Numerics.orientation(triRef, 1, 2, 3);
182: ori4 = Numerics.orientation(triRef, 1, 3, 4);
183:
184: /*
185: for(int i=0; i<5; i++)
186: System.out.println("point " + i + ", " + triRef.points[i]);
187: System.out.println("ori2 : " + ori2 + " ori4 : " + ori4);
188: */
189:
190: if (((ori2 > 0) && (ori4 > 0))
191: || ((ori2 < 0) && (ori4 < 0))) {
192:
193: // i1, i3 is a valid diagonal;
194: //
195: // encode as a 2-triangle strip: the triangles are (2, 3, 1)
196: // and (1, 3, 4).
197:
198: // triRef.storeTriangle(i1, i2, i3);
199: // triRef.storeTriangle(i1, i3, i4);
200: triRef.storeTriangle(ind1, ind2, ind3);
201: triRef.storeTriangle(ind1, ind3, ind4);
202: } else {
203: // i2, i4 has to be a valid diagonal. (if this is no valid
204: // diagonal then the corners of the quad form a figure of eight;
205: // shall we apply any heuristics in order to guess which diagonal
206: // is more likely to be the better choice? alternatively, we could
207: // return false and subject it to the standard triangulation
208: // algorithm. well, let's see how this brute-force solution works.)
209:
210: // encode as a 2-triangle strip: the triangles are (1, 2, 4)
211: // and (4, 2, 3).
212:
213: // triRef.storeTriangle(i2, i3, i4);
214: // triRef.storeTriangle(i2, i4, i1);
215: triRef.storeTriangle(ind2, ind3, ind4);
216: triRef.storeTriangle(ind2, ind4, ind1);
217: }
218: return true;
219: }
220:
221: return false;
222: }
223:
224: }
|