001: /*
002: * Copyright (c) 2005 by L. Paul Chew.
003: *
004: * Permission is hereby granted, without written agreement and without
005: * license or royalty fees, to use, copy, modify, and distribute this
006: * software and its documentation for any purpose, subject to the following
007: * conditions:
008: *
009: * The above copyright notice and this permission notice shall be included
010: * in all copies or substantial portions of the Software.
011: *
012: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
013: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
014: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
015: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
016: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
017: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
018: * DEALINGS IN THE SOFTWARE.
019: */
020:
021: package org.openjump.core.graph.delauneySimplexInsert;
022:
023: import java.util.AbstractSet;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.Collection;
027: import java.util.Collections;
028: import java.util.HashSet;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Set;
033:
034: /**
035: * A Simplex is an immutable set of vertices (usually Pnts).
036: *
037: * @author Paul Chew
038: *
039: * Created July 2005. Derived from an earlier, messier version.
040: */
041: class Simplex extends AbstractSet implements Set {
042:
043: private List vertices; // The simplex's vertices
044: private long idNumber; // The id number
045: private static long idGenerator = 0; // Used to create id numbers
046: public static boolean moreInfo = false; // True iff more info in toString
047:
048: /**
049: * Constructor.
050: * @param collection a Collection holding the Simplex vertices
051: * @throws IllegalArgumentException if there are duplicate vertices
052: */
053: public Simplex(Collection collection) {
054: this .vertices = Collections.unmodifiableList(new ArrayList(
055: collection));
056: this .idNumber = idGenerator++;
057: Set noDups = new HashSet(this );
058: if (noDups.size() != this .vertices.size())
059: throw new IllegalArgumentException(
060: "Duplicate vertices in Simplex");
061: }
062:
063: /**
064: * Constructor.
065: * @param vertices the vertices of the Simplex.
066: * @throws IllegalArgumentException if there are duplicate vertices
067: */
068: public Simplex(Object[] vertices) {
069: this (Arrays.asList(vertices));
070: }
071:
072: /**
073: * String representation.
074: * @return the String representation of this Simplex
075: */
076: public String toString() {
077: if (!moreInfo)
078: return "Simplex" + idNumber;
079: return "Simplex" + idNumber + super .toString();
080: }
081:
082: /**
083: * Dimension of the Simplex.
084: * @return dimension of Simplex (one less than number of vertices)
085: */
086: public int dimension() {
087: return this .vertices.size() - 1;
088: }
089:
090: /**
091: * True iff simplices are neighbors.
092: * Two simplices are neighbors if they are the same dimension and they share
093: * a facet.
094: * @param simplex the other Simplex
095: * @return true iff this Simplex is a neighbor of simplex
096: */
097: public boolean isNeighbor(Simplex simplex) {
098: HashSet h = new HashSet(this );
099: h.removeAll(simplex);
100: return (this .size() == simplex.size()) && (h.size() == 1);
101: }
102:
103: /**
104: * Report the facets of this Simplex.
105: * Each facet is a set of vertices.
106: * @return an Iterable for the facets of this Simplex
107: */
108: public List facets() {
109: List theFacets = new LinkedList();
110: for (Iterator it = this .iterator(); it.hasNext();) {
111: Object v = it.next();
112: Set facet = new HashSet(this );
113: facet.remove(v);
114: theFacets.add(facet);
115: }
116: return theFacets;
117: }
118:
119: /**
120: * Report the boundary of a Set of Simplices.
121: * The boundary is a Set of facets where each facet is a Set of vertices.
122: * @return an Iterator for the facets that make up the boundary
123: */
124: public static Set boundary(Set simplexSet) {
125: Set theBoundary = new HashSet();
126: for (Iterator it = simplexSet.iterator(); it.hasNext();) {
127: Simplex simplex = (Simplex) it.next();
128: for (Iterator otherIt = simplex.facets().iterator(); otherIt
129: .hasNext();) {
130: Set facet = (Set) otherIt.next();
131: if (theBoundary.contains(facet))
132: theBoundary.remove(facet);
133: else
134: theBoundary.add(facet);
135: }
136: }
137: return theBoundary;
138: }
139:
140: /* Remaining methods are those required by AbstractSet */
141:
142: /**
143: * @return Iterator for Simplex's vertices.
144: */
145: public Iterator iterator() {
146: return this .vertices.iterator();
147: }
148:
149: /**
150: * @return the size (# of vertices) of this Simplex
151: */
152: public int size() {
153: return this .vertices.size();
154: }
155:
156: /**
157: * @return the hashCode of this Simplex
158: */
159: public int hashCode() {
160: return (int) (idNumber ^ (idNumber >>> 32));
161: }
162:
163: /**
164: * We want to allow for different simplices that share the same vertex set.
165: * @return true for equal Simplices
166: */
167: public boolean equals(Object o) {
168: return (this == o);
169: }
170: }
|