-
Notifications
You must be signed in to change notification settings - Fork 0
/
IEdge.java
320 lines (255 loc) · 9.47 KB
/
IEdge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*---
iGeo - http://igeo.jp
Copyright (c) 2002-2013 Satoru Sugihara
This file is part of iGeo.
iGeo is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, version 3.
iGeo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with iGeo. If not, see <http://www.gnu.org/licenses/>.
---*/
package igeo;
import java.util.ArrayList;
/**
Class of an edge of polygon mesh.
@author Satoru Sugihara
*/
public class IEdge{
public IVertex[] vertices;
public ArrayList<IFace> faces;
public IEdge(IVertex v1, IVertex v2){
vertices = new IVertex[2];
vertices[0]=v1;
vertices[1]=v2;
faces = new ArrayList<IFace>();
v1.addEdge(this);
v2.addEdge(this);
}
public IEdge(IEdge e){
vertices = new IVertex[e.vertices.length];
for(int i=0; i<vertices.length; i++) vertices[i] = e.vertices[i];
faces = new ArrayList<IFace>();
for(int i=0; i<e.faces.size(); i++) faces.add(e.faces.get(i));
}
public IVertex getVertex(int i){ return vertex(i); }
public IVertex vertex(int i){ return vertices[i]; }
public int vertexNum(){ return vertices.length; } // should be always 2
public void addFace(IFace f){ faces.add(f); }
public IFace getFace(int i){ return face(i); }
public IFace face(int i){ return faces.get(i); }
public int faceNum(){ return faces.size(); }
public IEdge dup(){ return new IEdge(this); }
public void del(){
for(int i=0; i<faces.size(); i++){
faces.get(i).del();
i--;
}
vertices[0].del(this);
vertices[1].del(this);
//vertices[0].edges.remove(this);
//vertices[1].edges.remove(this);
}
public IVec nml(double param){ // 0 - 1.0
//return vertices[0].nrml().get().mul(1.-param).add(vertices[1].nrml().mul(param)).unit();
return vertices[0].nml().get().mul(1.-param).add(vertices[1].nml().mul(param)).unit();
}
//public IVec normal(double param){ return nrml(param); }
public IVec normal(double param){ return nml(param); }
//public IVec nrml(){ return nrml(0.5); }
public IVec nml(){ return nml(0.5); }
public IVec normal(){ return nml(); }
public IVec getPointOnEdge(double ratio){
// ratio 0-1 point of vertices[0] to vertices[1], 0.5 means mid point
return vertices[0].pos.get().sum(vertices[1].pos,ratio);
}
public IVec pos(double ratio){ return getPointOnEdge(ratio); }
public IVec mid(){ return getPointOnEdge(0.5); }
/** alias of mid */
public IVec center(){ return mid(); }
public boolean contains(IVertex v){
if(vertices[0]==v || vertices[1]==v) return true;
return false;
}
public boolean contains(IVertex v1, IVertex v2){
if(vertices[0]==v1 && vertices[1]==v2 || vertices[0]==v2 && vertices[1]==v1) return true;
return false;
}
public void replaceVertex(IVertex oldVertex, IVertex newVertex){
if(vertices[0]==oldVertex){ vertices[0] = newVertex; }
else if(vertices[1] ==oldVertex){ vertices[1] = newVertex; }
else{ IOut.err(" no such vertex on this edge"); }
}
public IVertex getOtherVertex(IVertex v){
if(vertices[0]==v) return vertices[1];
if(vertices[1]==v) return vertices[0];
IOut.err("no such vertex on this edge");
return null;
}
public IVertex getOtherVertex(IEdge edge){
if(edge.contains(vertices[0])) return vertices[1];
if(edge.contains(vertices[1])) return vertices[0];
return null;
}
public IVertex getSharedVertex(IEdge edge){
if(edge.contains(vertices[0])) return vertices[0];
if(edge.contains(vertices[1])) return vertices[1];
return null;
}
public IFace sharedFace(IEdge edge){
for(int i=0; i<faces.size(); i++){
for(int j=0; j<edge.faceNum(); j++){
if(faces.get(i).eq(edge.face(j))) return faces.get(i);
}
}
for(int i=0; i<faces.size(); i++){
if(faces.get(i).onFace(edge)) return faces.get(i);
}
return null;
}
/** edge sharing exact same pair of vertices */
public IEdge getOverlappingEdge(){
for(int i=0; i<vertices[0].edgeNum(); i++){
if(vertices[0].edge(i)!=this &&
vertices[0].edge(i).getOtherVertex(vertices[0]) == vertices[1]){
return vertices[0].edge(i);
}
}
return null;
}
/** edge sharing exact same pair of vertices */
public IEdge[] getOverlappingEdges(){
ArrayList<IEdge> overlapEdges = new ArrayList<IEdge>();
for(int i=0; i<vertices[0].edgeNum(); i++){
if(vertices[0].edge(i)!=this &&
vertices[0].edge(i).getOtherVertex(vertices[0]) == vertices[1]){
overlapEdges.add(vertices[0].edge(i));
}
}
return overlapEdges.toArray(new IEdge[overlapEdges.size()]);
}
public boolean hasSharedVertex(IEdge edge){ return getSharedVertex(edge)!=null; }
public boolean isConnected(IEdge edge){ return hasSharedVertex(edge); }
public IFace getOtherFace(IFace f){
for(int i=0; i<faces.size(); i++) if(faces.get(i)!=f) return faces.get(i);
IEdge[] overlappingEdge = getOverlappingEdges();
for(int i=0; i<overlappingEdge.length; i++){
for(int j=0; j<overlappingEdge[i].faceNum(); j++){
if(overlappingEdge[i].face(j)!=f){
return overlappingEdge[i].face(j);
}
}
}
return null;
}
public /*ArrayList<IFace>*/ IFace[] getOtherFaces(IFace f){
ArrayList<IFace> others = new ArrayList<IFace>();
for(int i=0; i<faces.size(); i++) if(faces.get(i)!=f) others.add(faces.get(i));
IEdge[] overlappingEdge = getOverlappingEdges();
for(int i=0; i<overlappingEdge.length; i++){
for(int j=0; j<overlappingEdge[i].faceNum(); j++){
if(overlappingEdge[i].face(j)!=f){
others.add(overlappingEdge[i].face(j));
}
}
}
//return others;
return others.toArray(new IFace[others.size()]); //
}
public boolean isOnEdge(IVertex v){
IVec diff = vertices[1].pos.get().diff(vertices[0].pos);
IVec d = v.pos.get().diff(vertices[0].pos);
if(!diff.isParallel(d)) return false;
double ip = diff.dot(d);
if(ip<0 || ip>diff.len2()) return false;
return true;
}
public ArrayList<ArrayList<IEdge>> traceEdge(int maxVertexNum){
ArrayList<ArrayList<IEdge>> loops = new ArrayList<ArrayList<IEdge>>();
ArrayList<IEdge> trace = new ArrayList<IEdge>();
traceEdge(trace, loops, maxVertexNum);
return loops;
}
public void traceEdge(ArrayList<IEdge> trace, ArrayList<ArrayList<IEdge>> loops, int depth){
IVertex vertex=null;
if(trace.size()>0){
vertex = getOtherVertex(trace.get(trace.size()-1));
if(vertex==null){
// nothing... ?
}
}
else{
vertex = vertices[1]; // vertices[0] is origin
}
if(trace.size()>1 && trace.get(0).vertices[0] == vertex){ // found a loop
ArrayList<IEdge> loop = new ArrayList<IEdge>();
for(int i=0; i<trace.size(); i++) loop.add(trace.get(i));
loop.add(this);
loops.add(loop);
return;
}
if(depth<=1){
//IOut.p("traceEdge: maximumDepth; return"); //
return; // max recursion
}
trace.add(this);
if(vertex!=null){ // is this necessary?
for(int i=0; i<vertex.edges.size(); i++){
if(vertex.edges.get(i) != this) vertex.edges.get(i).traceEdge(trace, loops, depth-1);
}
}
trace.remove(trace.size()-1);//
}
public String toString(){
//return "IEdge: "+String.valueOf(vertices[0].getIndex())+"-"+String.valueOf(vertices[1].getIndex());
if(vertices==null) return super.toString();
String s1 = "null";
if(vertices[0]!=null) s1=vertices[0].toString();
String s2 = "null";
if(vertices[1]!=null) s2=vertices[1].toString();
return "IEdge: "+s1+"-"+s2;
}
public boolean eq(IEdge e){ return eq(e,IConfig.tolerance); }
public boolean eq(IEdge e, double tolerance){
// direction does not matter?
if(vertices.length!=e.vertices.length) return false;
if(vertices[0]==e.vertices[0] && vertices[1]==e.vertices[1]) return true;
if(vertices[0]==e.vertices[1] && vertices[1]==e.vertices[0]) return true;
if(vertices[0].eq(e.vertices[0],tolerance) &&
vertices[1].eq(e.vertices[1],tolerance)) return true;
if(vertices[0].eq(e.vertices[1],tolerance) &&
vertices[1].eq(e.vertices[0],tolerance)) return true;
return false;
}
public IFace[] createFace(IMeshType creator){
ArrayList<ArrayList<IEdge>> loops = traceEdge(creator.getMaxVertexNum());
if(loops.size()==0) return null;
IFace fcs[] = new IFace[loops.size()];
for(int i=0; i<loops.size(); i++){
IEdge[] edgs = loops.get(i).toArray(new IEdge[loops.get(i).size()]);
fcs[i] = creator.createFace(edgs);
}
return fcs;
}
public IVertex[] subdivide(int divnum, ArrayList<IVertex> newVertexArray, ArrayList<IEdge> newEdgeArray){
IEdge[] newedges = new IEdge[divnum];
IVertex[] newvertices = new IVertex[divnum+1];
// original vertices are kept: don't delete
for(int i=0; i<=divnum; i++){
if(i==0){ newvertices[i] = vertices[0]; }
else if(i==divnum){ newvertices[i] = vertices[1]; }
else{
newvertices[i] = new IVertex(vertices[0].pos.sum(vertices[1].pos,
(double)i/divnum));
}
if(i>0){ newedges[i-1] = new IEdge(newvertices[i-1], newvertices[i]); }
}
this.del();
for(int i=0; i<newvertices.length; i++) newVertexArray.add(newvertices[i]);
for(int i=0; i<newedges.length; i++) newEdgeArray.add(newedges[i]);
return newvertices;
}
}