root / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / topology / triangulation / Simplex.java @ 8150
History | View | Annotate | Download (5.24 KB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2005 by L. Paul Chew.
|
3 |
*
|
4 |
* Permission is hereby granted, without written agreement and without
|
5 |
* license or royalty fees, to use, copy, modify, and distribute this
|
6 |
* software and its documentation for any purpose, subject to the following
|
7 |
* conditions:
|
8 |
*
|
9 |
* The above copyright notice and this permission notice shall be included
|
10 |
* in all copies or substantial portions of the Software.
|
11 |
*
|
12 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
13 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
14 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
15 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
16 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
17 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
18 |
* DEALINGS IN THE SOFTWARE.
|
19 |
*/
|
20 |
package com.iver.cit.gvsig.topology.triangulation; |
21 |
|
22 |
import java.util.AbstractSet; |
23 |
import java.util.ArrayList; |
24 |
import java.util.Arrays; |
25 |
import java.util.Collection; |
26 |
import java.util.Collections; |
27 |
import java.util.HashSet; |
28 |
import java.util.Iterator; |
29 |
import java.util.LinkedList; |
30 |
import java.util.List; |
31 |
import java.util.Set; |
32 |
|
33 |
|
34 |
/**
|
35 |
* A Simplex is an immutable set of vertices (usually Pnts).
|
36 |
*
|
37 |
* @author Paul Chew
|
38 |
*
|
39 |
* Created July 2005. Derived from an earlier, messier version.
|
40 |
*/
|
41 |
public class Simplex extends AbstractSet implements Set { |
42 |
|
43 |
private List vertices; // The simplex's vertices |
44 |
private long idNumber; // The id number |
45 |
private static long idGenerator = 0; // Used to create id numbers |
46 |
public static boolean moreInfo = false; // True iff more info in toString |
47 |
|
48 |
/**
|
49 |
* Constructor.
|
50 |
* @param collection a Collection holding the Simplex vertices
|
51 |
* @throws IllegalArgumentException if there are duplicate vertices
|
52 |
*/
|
53 |
public Simplex (Collection collection) { |
54 |
this.vertices = Collections.unmodifiableList(new ArrayList(collection)); |
55 |
this.idNumber = idGenerator++;
|
56 |
Set noDups = new HashSet(this); |
57 |
if (noDups.size() != this.vertices.size()) |
58 |
throw new IllegalArgumentException("Duplicate vertices in Simplex"); |
59 |
} |
60 |
|
61 |
/**
|
62 |
* Constructor.
|
63 |
* @param vertices the vertices of the Simplex.
|
64 |
* @throws IllegalArgumentException if there are duplicate vertices
|
65 |
*/
|
66 |
public Simplex (Object[] vertices) { |
67 |
this(Arrays.asList(vertices)); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* String representation.
|
72 |
* @return the String representation of this Simplex
|
73 |
*/
|
74 |
public String toString () { |
75 |
if (!moreInfo) return "Simplex" + idNumber; |
76 |
return "Simplex" + idNumber + super.toString(); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Dimension of the Simplex.
|
81 |
* @return dimension of Simplex (one less than number of vertices)
|
82 |
*/
|
83 |
public int dimension () { |
84 |
return this.vertices.size() - 1; |
85 |
} |
86 |
|
87 |
/**
|
88 |
* True iff simplices are neighbors.
|
89 |
* Two simplices are neighbors if they are the same dimension and they share
|
90 |
* a facet.
|
91 |
* @param simplex the other Simplex
|
92 |
* @return true iff this Simplex is a neighbor of simplex
|
93 |
*/
|
94 |
public boolean isNeighbor (Simplex simplex) { |
95 |
HashSet h = new HashSet(this); |
96 |
h.removeAll(simplex); |
97 |
return (this.size() == simplex.size()) && (h.size() == 1); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Report the facets of this Simplex.
|
102 |
* Each facet is a set of vertices.
|
103 |
* @return an Iterable for the facets of this Simplex
|
104 |
*/
|
105 |
public List facets () { |
106 |
List theFacets = new LinkedList(); |
107 |
for (Iterator it = this.iterator(); it.hasNext();) { |
108 |
Object v = it.next();
|
109 |
Set facet = new HashSet(this); |
110 |
facet.remove(v); |
111 |
theFacets.add(facet); |
112 |
} |
113 |
return theFacets;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Report the boundary of a Set of Simplices.
|
118 |
* The boundary is a Set of facets where each facet is a Set of vertices.
|
119 |
* @return an Iterator for the facets that make up the boundary
|
120 |
*/
|
121 |
public static Set boundary (Set simplexSet) { |
122 |
Set theBoundary = new HashSet(); |
123 |
for (Iterator it = simplexSet.iterator(); it.hasNext();) { |
124 |
Simplex simplex = (Simplex) it.next(); |
125 |
for (Iterator otherIt = simplex.facets().iterator(); otherIt.hasNext();) { |
126 |
Set facet = (Set) otherIt.next(); |
127 |
if (theBoundary.contains(facet)) theBoundary.remove(facet);
|
128 |
else theBoundary.add(facet);
|
129 |
} |
130 |
} |
131 |
return theBoundary;
|
132 |
} |
133 |
|
134 |
/* Remaining methods are those required by AbstractSet */
|
135 |
|
136 |
/**
|
137 |
* @return Iterator for Simplex's vertices.
|
138 |
*/
|
139 |
public Iterator iterator () { |
140 |
return this.vertices.iterator(); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* @return the size (# of vertices) of this Simplex
|
145 |
*/
|
146 |
public int size () { |
147 |
return this.vertices.size(); |
148 |
} |
149 |
|
150 |
/**
|
151 |
* @return the hashCode of this Simplex
|
152 |
*/
|
153 |
public int hashCode () { |
154 |
return (int)(idNumber^(idNumber>>>32)); |
155 |
} |
156 |
|
157 |
/**
|
158 |
* We want to allow for different simplices that share the same vertex set.
|
159 |
* @return true for equal Simplices
|
160 |
*/
|
161 |
public boolean equals (Object o) { |
162 |
return (this == o); |
163 |
} |
164 |
} |