001: /*
002: * $RCSfile: Orientation.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 Orientation {
066:
067: /**
068: * determine the outer polygon and the orientation of the polygons; the
069: * default orientation is CCW for the outer-most polygon, and CW for the
070: * inner polygons. the polygonal loops are referenced by loops[i1,..,i2-1].
071: */
072: static void adjustOrientation(Triangulator triRef, int i1, int i2) {
073:
074: double area;
075: int i, outer;
076: int ind;
077:
078: if (i1 >= i2)
079: System.out
080: .println("Orientation:adjustOrientation Problem i1>=i2 !!!");
081:
082: if (triRef.numLoops >= triRef.maxNumPolyArea) {
083: // System.out.println("Orientation:adjustOrientation Expanding polyArea array .");
084: triRef.maxNumPolyArea = triRef.numLoops;
085: double old[] = triRef.polyArea;
086: triRef.polyArea = new double[triRef.maxNumPolyArea];
087: if (old != null)
088: System
089: .arraycopy(old, 0, triRef.polyArea, 0,
090: old.length);
091: }
092:
093: // for each contour, compute its signed area, i.e., its orientation. the
094: // contour with largest area is assumed to be the outer-most contour.
095: for (i = i1; i < i2; ++i) {
096: ind = triRef.loops[i];
097: triRef.polyArea[i] = polygonArea(triRef, ind);
098: }
099:
100: // determine the outer-most contour
101: area = Math.abs(triRef.polyArea[i1]);
102: outer = i1;
103: for (i = i1 + 1; i < i2; ++i) {
104: if (area < Math.abs(triRef.polyArea[i])) {
105: area = Math.abs(triRef.polyArea[i]);
106: outer = i;
107: }
108: }
109:
110: // default: the outer contour is referenced by loops[i1]
111: if (outer != i1) {
112: ind = triRef.loops[i1];
113: triRef.loops[i1] = triRef.loops[outer];
114: triRef.loops[outer] = ind;
115:
116: area = triRef.polyArea[i1];
117: triRef.polyArea[i1] = triRef.polyArea[outer];
118: triRef.polyArea[outer] = area;
119: }
120:
121: // adjust the orientation
122: if (triRef.polyArea[i1] < 0.0)
123: triRef.swapLinks(triRef.loops[i1]);
124: for (i = i1 + 1; i < i2; ++i) {
125: if (triRef.polyArea[i] > 0.0)
126: triRef.swapLinks(triRef.loops[i]);
127: }
128: }
129:
130: /**
131: * This function computes twice the signed area of a simple closed polygon.
132: */
133: static double polygonArea(Triangulator triRef, int ind) {
134: int hook = 0;
135: int ind1, ind2;
136: int i1, i2;
137: double area = 0.0, area1 = 0;
138:
139: ind1 = ind;
140: i1 = triRef.fetchData(ind1);
141: ind2 = triRef.fetchNextData(ind1);
142: i2 = triRef.fetchData(ind2);
143: area = Numerics.stableDet2D(triRef, hook, i1, i2);
144:
145: ind1 = ind2;
146: i1 = i2;
147: while (ind1 != ind) {
148: ind2 = triRef.fetchNextData(ind1);
149: i2 = triRef.fetchData(ind2);
150: area1 = Numerics.stableDet2D(triRef, hook, i1, i2);
151: area += area1;
152: ind1 = ind2;
153: i1 = i2;
154: }
155:
156: return area;
157: }
158:
159: /**
160: * Determine the orientation of the polygon. The default orientation is CCW.
161: */
162: static void determineOrientation(Triangulator triRef, int ind) {
163: double area;
164:
165: // compute the polygon's signed area, i.e., its orientation.
166: area = polygonArea(triRef, ind);
167:
168: // adjust the orientation (i.e., make it CCW)
169: if (area < 0.0) {
170: triRef.swapLinks(ind);
171: triRef.ccwLoop = false;
172: }
173:
174: }
175:
176: }
|