-
Notifications
You must be signed in to change notification settings - Fork 34
/
I2DMapFieldGeo.java
237 lines (189 loc) · 6.93 KB
/
I2DMapFieldGeo.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
/*---
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;
/**
2D vector filed defined by a IMap
@author Satoru Sugihara
*/
public class I2DMapFieldGeo extends IFieldGeo implements I2DFieldI{
public IMap map;
public IDoubleMap dmap; // cast from map
public IVec corner;
public double width,height;
public int mapWidth, mapHeight;
public int interpolationRange = 5;
public I2DMapFieldGeo(IMap m, IVec cnr, double wid, double hei){
map = m;
corner = cnr;
width = wid;
height = hei;
//linearDecay(0);
if(map instanceof IDoubleMap){
dmap = (IDoubleMap)map;
mapWidth = dmap.getWidth();
mapHeight = dmap.getHeight();
if(dmap instanceof IImageMap){
dmap.flipV(); // upside down
}
}
else{
mapWidth = IMap.defaultDensityWidth;
mapHeight = IMap.defaultDensityHeight;
}
}
public double udif(int uidx, int vidx){
if(dmap!=null){
return dmap.map[uidx+1][vidx] - dmap.map[uidx][vidx];
}
return map.get( (double)(uidx+1)/mapWidth, (double)vidx/mapHeight ) -
map.get( (double)uidx/mapWidth, (double)vidx/mapHeight );
}
public double vdif(int uidx, int vidx){
if(dmap!=null){
return dmap.map[uidx+1][vidx] - dmap.map[uidx][vidx];
}
return map.get( (double)(uidx+1)/mapWidth, (double)vidx/mapHeight ) -
map.get( (double)uidx/mapWidth, (double)vidx/mapHeight );
}
public IVec2 dif(int uidx, int vidx){
if(uidx<0) uidx=0; else if(uidx>=mapWidth-1) uidx=mapWidth-2;
if(vidx<0) vidx=0; else if(vidx>=mapHeight-1) vidx=mapHeight-2;
// interpolation of 4 cells
double udif1=0, udif2=0, vdif1=0, vdif2=0;
if(dmap!=null){
udif1 = dmap.map[uidx+1][vidx] - dmap.map[uidx][vidx];
udif2 = dmap.map[uidx+1][vidx+1] - dmap.map[uidx][vidx+1];
vdif1 = dmap.map[uidx][vidx+1] - dmap.map[uidx][vidx];
vdif2 = dmap.map[uidx+1][vidx+1] - dmap.map[uidx+1][vidx];
}
else{
udif1 = map.get( (double)(uidx+1)/mapWidth, (double)vidx/mapHeight ) -
map.get( (double)uidx/mapWidth, (double)vidx/mapHeight );
udif2 = map.get( (double)(uidx+1)/mapWidth, (double)(vidx+1)/mapHeight ) -
map.get( (double)uidx/mapWidth, (double)(vidx+1)/mapHeight );
vdif1 = map.get( (double)(uidx)/mapWidth, (double)(vidx+1)/mapHeight ) -
map.get( (double)uidx/mapWidth, (double)(vidx)/mapHeight );
vdif2 = map.get( (double)(uidx+1)/mapWidth, (double)(vidx+1)/mapHeight ) -
map.get( (double)(uidx+1)/mapWidth, (double)(vidx)/mapHeight );
}
return new IVec2((udif1+udif2)/2, (vdif1+vdif2)/2);
}
IVec2 interpolateDif(int uidx, int vidx){
double weight=0;
double u=0;
double v=0;
IVec2 vec = new IVec2();
for(int i=uidx-interpolationRange; i<uidx+interpolationRange; i++){
for(int j=vidx-interpolationRange; j<vidx+interpolationRange; j++){
vec.add(dif(i,j));
weight += 1 - Math.abs(i-uidx)/(interpolationRange+1);
}
}
vec.div(weight);
return vec;
}
/** get original field value out of surface parameter uv */
public IVec2I get(IVecI pos, double u, double v){
/*
int uidx1 = (int)(u*mapWidth);
int vidx1 = (int)(v*mapHeight);
if(uidx1<0) uidx1=0; else if(uidx1>=mapWidth){ uidx1=mapWidth-1; }
if(vidx1<0) vidx1=0; else if(vidx1>=mapHeight){ vidx1=mapHeight-1; }
*/
int uidx2 = (int)(u*mapWidth-0.5);
int vidx2 = (int)(v*mapHeight-0.5);
if(uidx2<0) uidx2=0; else if(uidx2>=mapWidth-1){ uidx2=mapWidth-2; }
if(vidx2<0) vidx2=0; else if(vidx2>=mapHeight-1){ vidx2=mapHeight-2; }
//double udif = udif(uidx2, vidx1);
//double vdif = vdif(uidx1, vidx2);
return interpolateDif(uidx2,vidx2);
//return dif( uidx2, vidx2);
/*
double udif=0, vdif=0;
if(dmap!=null){
udif = dmap.map[uidx2+1][vidx1] - dmap.map[uidx2][vidx1];
vdif = dmap.map[uidx1][vidx2+1] - dmap.map[uidx1][vidx2];
}
else{
udif =
map.get( (double)(uidx2+1)/mapWidth, (double)vidx1/mapHeight ) -
map.get( (double)uidx2/mapWidth, (double)vidx1/mapHeight );
vdif =
map.get((double)uidx1/mapWidth, (double)(vidx2+1)/mapHeight) -
map.get((double)uidx1/mapWidth, (double)vidx2/mapHeight);
}
*/
//return new IVec2(udif, vdif);
}
/** get original field value out of surface parameter uv */
public IVec2I get(IVecI pos, IVecI vel, double u, double v){
return get(pos,u,v);
}
/** get 3D vector field value */
public IVec2I get(IVecI v){ return get(v,(IVecI)null); }
/** get 3D vector field value */
public IVec2I get(IVecI pos, IVecI vel){
double u = (pos.x()-corner.x)/width;
double v = (pos.y()-corner.y)/height;
if(u<0 || u>1 || v<0 || v>1){
return new IVec2(0,0); // no decay
}
double r = intensity;
/*
if(decay == Decay.Linear){
double dist = surface.pt(uv).to2d().dist(pos.to2d());
if(dist >= threshold &&
(uv.x<=0||uv.y<=0||uv.x>=1.0||uv.y>=1.0)) return new IVec2(); // zero
if(threshold>0) r *= (threshold-dist)/threshold;
}
else if(decay == Decay.Gaussian){
double dist = surface.pt(uv).to2d().dist(pos.to2d());
if(threshold>0) r *= Math.exp(-2*dist*dist/(threshold*threshold));
}
*/
IVec2I vec = get(pos, vel, u, v);
/*
if(bidirectional && vec.get().dot(vel.to2d()) < 0){ r=-r; }
if(constantIntensity){
double len = vec.len();
if(len<IConfig.tolerance){ return vec.zero(); }
return vec.len(r);
}
*/
return vec.mul(r);
}
/*
public I2DSurfaceFieldGeo constantIntensity(boolean b){ super.constantIntensity(b); return this; }
public I2DSurfaceFieldGeo bidirectional(boolean b){ super.bidirectional(b); return this; }
public I2DSurfaceFieldGeo noDecay(){ super.noDecay(); return this; }
public I2DSurfaceFieldGeo linearDecay(double threshold){
super.linearDecay(threshold); return this;
}
public I2DSurfaceFieldGeo linear(double threshold){
super.linear(threshold); return this;
}
public I2DSurfaceFieldGeo gaussianDecay(double threshold){
super.gaussianDecay(threshold); return this;
}
public I2DSurfaceFieldGeo gaussian(double threshold){
super.gaussian(threshold); return this;
}
public I2DSurfaceFieldGeo gauss(double threshold){ super.gauss(threshold); return this; }
public I2DSurfaceFieldGeo threshold(double t){ super.threshold(t); return this; }
*/
public I2DMapFieldGeo intensity(double i){ super.intensity(i); return this; }
public void del(){
}
}