forked from sghr/iGeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICompoundField.java
136 lines (113 loc) · 4.65 KB
/
ICompoundField.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
/*---
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;
/**
manage multiple fields; included field should be point / curve / surface based field because it needs to measure distance to check which is closest.
@author Satoru Sugihara
*/
public class ICompoundField extends I3DField{
//public enum CompundType{ Closest, Sum };
public ArrayList<IPointFieldGeo> pointFields;
public ArrayList<ICurveFieldGeo> curveFields;
public ArrayList<ISurfaceFieldGeo> surfaceFields;
public ArrayList<I3DFieldI> otherFields;
public ICompoundField(){
super(null);
pointFields = new ArrayList<IPointFieldGeo>();
curveFields = new ArrayList<ICurveFieldGeo>();
surfaceFields = new ArrayList<ISurfaceFieldGeo>();
otherFields = new ArrayList<I3DFieldI>();
}
public ICompoundField add(I3DFieldI field){
if(field instanceof I3DField){
//((I3DField)field).del(); // field is managed by this, not by server.
((I3DField)field).del(false); // field is managed by this, not by server.
I3DFieldI f = ((I3DField)field).field();
if(f==null){ otherFields.add(field); }
else if(f instanceof IPointFieldGeo){ pointFields.add((IPointFieldGeo)f); }
else if(f instanceof ICurveFieldGeo){ curveFields.add((ICurveFieldGeo)f); }
else if(f instanceof ISurfaceFieldGeo){ surfaceFields.add((ISurfaceFieldGeo)f); }
else{ otherFields.add(f); }
}
else if(field instanceof IPointFieldGeo){ pointFields.add((IPointFieldGeo)field); }
else if(field instanceof ICurveFieldGeo){ curveFields.add((ICurveFieldGeo)field); }
else if(field instanceof ISurfaceFieldGeo){ surfaceFields.add((ISurfaceFieldGeo)field); }
else{ otherFields.add(field); }
return this;
}
public ICompoundField remove(I3DFieldI field){
if(field instanceof I3DField){
I3DFieldI f = ((I3DField)field).field();
if(f==null){ otherFields.remove(field); }
else if(f instanceof IPointFieldGeo){ pointFields.remove((IPointFieldGeo)f); }
else if(f instanceof ICurveFieldGeo){ curveFields.remove((ICurveFieldGeo)f); }
else if(f instanceof ISurfaceFieldGeo){ surfaceFields.remove((ISurfaceFieldGeo)f); }
else{ otherFields.remove(f); }
}
else if(field instanceof IPointFieldGeo){ pointFields.remove((IPointFieldGeo)field); }
else if(field instanceof ICurveFieldGeo){ curveFields.remove((ICurveFieldGeo)field); }
else if(field instanceof ISurfaceFieldGeo){ surfaceFields.remove((ISurfaceFieldGeo)field); }
else{ otherFields.remove(field); }
return this;
}
@Override public IVecI get(IVecI pt){ return get(pt,null); }
@Override public IVecI get(IVecI pt, IVecI vel){
// find closest field
I3DFieldI closestField=null;
double minDist=0;
double dist=0;
//debug
IVecI closestPt=null;
for(int i=0; i<pointFields.size(); i++){
if(pointFields.get(i).pos!=null){
dist = pointFields.get(i).pos.dist(pt);
if(dist < minDist || closestField==null){
minDist = dist; closestField = pointFields.get(i);
//debug
//closestPt = pointFields.get(i).pos;
}
}
}
for(int i=0; i<curveFields.size(); i++){
dist = curveFields.get(i).curve.dist(pt);
if(dist < minDist || closestField==null){
minDist = dist; closestField = curveFields.get(i);
//debug
//closestPt = curveFields.get(i).curve.closePt(pt);
}
}
for(int i=0; i<surfaceFields.size(); i++){
dist = surfaceFields.get(i).surface.dist(pt);
if(dist < minDist || closestField==null){
minDist = dist; closestField = surfaceFields.get(i);
//debug
//closestPt = surfaceFields.get(i).surface.closePt(pt);
}
}
if(closestField==null){ return new IVec(); }
// debug
//debugLine = new ICurve(pt.dup(), closestPt).clr(1.0,0.5); //
//return closestField.get(pt);
return closestField.get(pt,vel);
}
/* // child fields are aready deleted when added
public void del(){ if(field!=null){ field.del(); } super.del(); }
public void del(boolean deleteGeometry){
if(deleteGeometry && field!=null) field.del();
super.del(deleteGeometry);
}
*/
}