-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathICurveCache.java
288 lines (257 loc) · 9.37 KB
/
ICurveCache.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
/*---
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;
/**
Point cache of a curve for proximity search.
@author Satoru Sugihara
*/
public class ICurveCache{
public ICurveGeo curve;
public int resolution;
public int deg;
public IVec[] pts; // for 3d search
public IVec2[] pts2; // for 2d search
public ICurveCache(ICurveGeo crv, int resolution){
curve = crv;
this.resolution = resolution;
deg = curve.deg();
//init();
}
public ICurveCache(ICurveGeo crv){
this(crv, IConfig.curveCacheResolution);
}
public void init(){
if(deg==1){
pts = new IVec[curve.cpNum()];
for(int i=0; i<pts.length; i++){ pts[i] = curve.cp(i).get(); }
}
else{
int epnum = curve.epNum() ;
int num = (epnum-1)*resolution+1;
pts = new IVec[num];
for(int i=0; i<epnum; i++){
for(int j=0; j<resolution; j++){
if(i<epnum-1 || j==0){
pts[i*resolution + j] = curve.pt(curve.u(i,(double)j/resolution));
}
}
}
}
}
public void init2(){
if(deg==1){
pts2 = new IVec2[curve.cpNum()];
for(int i=0; i<pts2.length; i++){ pts2[i] = curve.cp(i).get().to2d(); }
}
else{
int epnum = curve.epNum() ;
int num = (epnum-1)*resolution+1;
pts2 = new IVec2[num];
for(int i=0; i<epnum; i++){
for(int j=0; j<resolution; j++){
if(i<epnum-1 || j==0){
pts2[i*resolution + j] = curve.pt(curve.u(i,(double)j/resolution)).to2d();
}
}
}
}
}
/** approximate invert projection from 3D location to interanl parameter U (closest point on curve) */
public double u(IVec pt){
if(pts==null) init();
int idx = closest(pt);
if(deg==1){
if(idx==0){
double r = pt.ratioOnSegment(pts[idx],pts[idx+1]);
if(r<0){ r=0; } else if(r>1.0){ r=1.0; } // inside segment // added 20120826!
return curve.u(idx,r);
}
else if(idx==pts.length-1){
double r = pt.ratioOnSegment(pts[idx-1],pts[idx]);
if(r<0){ r=0; } else if(r>1.0){ r=1.0; } // inside segment // added 20120826!
return curve.u(idx-1,r);
}
else{
double r1 = pt.ratioOnSegment(pts[idx-1],pts[idx]);
double r2 = pt.ratioOnSegment(pts[idx],pts[idx+1]);
if(r1<0){ r1=0; } else if(r1>1.0){ r1=1.0; } // inside segment
if(r2<0){ r2=0; } else if(r2>1.0){ r2=1.0; } // inside segment
double dist1 = pt.dist(pts[idx-1].sum(pts[idx],r1));
double dist2 = pt.dist(pts[idx].sum(pts[idx+1],r2));
if(dist1<=dist2) return curve.u(idx-1,r1);
return curve.u(idx,r2);
}
}
else if(resolution==1){
if(idx==0){
return recursiveSearch(pt, curve.u(idx,0), pts[idx], curve.u(idx+1,0), pts[idx+1], 0);
//return recursiveSearch(pt, curve.u(idx,0), curve.u(idx,0.5));
}
else if(idx==pts.length-1){
return recursiveSearch(pt, curve.u(idx-1,0), pts[idx-1], curve.u(idx,0), pts[idx], 0);
//return recursiveSearch(pt, curve.u(idx-1,0.5), curve.u(idx,0));
}
else{
int idx2 = closerIndexOnLine(pt,idx);
return recursiveSearch(pt, curve.u(idx2,0), pts[idx2],
curve.u(idx2+1,0), pts[idx2+1], 0);
// not sure about 0.5 but it'd be faster
//if(idx2<idx) return recursiveSearch(pt, curve.u(idx2,0.5), curve.u(idx2+1,0));
//else return recursiveSearch(pt, curve.u(idx,0), curve.u(idx,0.5));
}
}
else{
if(idx==0){
return recursiveSearch(pt, curve.u(idx,0), pts[idx],
curve.u(idx,1.0/resolution), pts[idx+1], 0);
//return recursiveSearch(pt, curve.u(idx,0), curve.u(idx,0.5));
}
else if(idx==pts.length-1){
int epIdx = idx/resolution;
double epFrac = (double)(idx%resolution)/resolution;
return recursiveSearch(pt, curve.u(epIdx,epFrac-1.0/resolution), pts[idx-1],
curve.u(epIdx,epFrac), pts[idx], 0);
//return recursiveSearch(pt, curve.u(idx-1,0.5), curve.u(idx,0));
}
else{
int idx2 = closerIndexOnLine(pt,idx);
int epIdx = idx2/resolution;
double epFrac = (double)(idx2%resolution)/resolution;
return recursiveSearch(pt, curve.u(epIdx,epFrac), pts[idx2],
curve.u(epIdx,epFrac+1.0/resolution),pts[idx2+1], 0);
// not sure about 0.5 but it'd be faster
//if(idx2<idx) return recursiveSearch(pt, curve.u(idx2,0.5), curve.u(idx2+1,0));
//else return recursiveSearch(pt, curve.u(idx,0), curve.u(idx,0.5));
}
}
}
/** approximate invert projection from 2D location to interanl parameter U */
public double u(IVec2 pt){
if(pts2==null) init2();
int idx = closest(pt);
if(deg==1){
if(idx==0){
double r = pt.ratioOnSegment(pts2[idx],pts2[idx+1]);
return curve.u(idx,r);
}
else if(idx==pts2.length-1){
double r = pt.ratioOnSegment(pts2[idx-1],pts2[idx]);
return curve.u(idx-1,r);
}
else{
double r1 = pt.ratioOnSegment(pts2[idx-1],pts2[idx]);
double r2 = pt.ratioOnSegment(pts2[idx],pts2[idx+1]);
if(r1<0){ r1=0; } else if(r1>1.0){ r1=1.0; } // inside segment
if(r2<0){ r2=0; } else if(r2>1.0){ r2=1.0; } // inside segment
double dist1 = pt.dist(pts2[idx-1].sum(pts2[idx],r1));
double dist2 = pt.dist(pts2[idx].sum(pts2[idx+1],r2));
if(dist1<=dist2) return curve.u(idx-1,r1);
return curve.u(idx,r2);
}
}
else if(resolution==1){
if(idx==0){
return recursiveSearch(pt, curve.u(idx,0), pts2[idx], curve.u(idx+1,0), pts2[idx+1], 0);
}
else if(idx==pts2.length-1){
return recursiveSearch(pt, curve.u(idx-1,0), pts2[idx-1], curve.u(idx,0), pts2[idx], 0);
}
else{
int idx2 = closerIndexOnLine(pt,idx);
return recursiveSearch(pt, curve.u(idx2,0), pts2[idx2],
curve.u(idx2+1,0), pts2[idx2+1], 0);
}
}
else{
if(idx==0){
return recursiveSearch(pt, curve.u(idx,0), pts2[idx],
curve.u(idx,1.0/resolution), pts2[idx+1], 0);
}
else if(idx==pts2.length-1){
int epIdx = idx/resolution;
double epFrac = (double)(idx%resolution)/resolution;
return recursiveSearch(pt, curve.u(epIdx,epFrac-1.0/resolution), pts2[idx-1],
curve.u(epIdx,epFrac), pts2[idx], 0);
}
else{
int idx2 = closerIndexOnLine(pt,idx);
int epIdx = idx2/resolution;
double epFrac = (double)(idx2%resolution)/resolution;
return recursiveSearch(pt, curve.u(epIdx,epFrac), pts2[idx2],
curve.u(epIdx,epFrac+1.0/resolution),pts2[idx2+1], 0);
}
}
}
public int closerIndexOnLine(IVec pt, int index){
double dist1 = pt.distToSegment(pts[index-1], pts[index]);
double dist2 = pt.distToSegment(pts[index], pts[index+1]);
if(dist1<=dist2) return index-1;
return index;
}
public int closerIndexOnLine(IVec2 pt, int index){
double dist1 = pt.distToSegment(pts2[index-1], pts2[index]);
double dist2 = pt.distToSegment(pts2[index], pts2[index+1]);
if(dist1<=dist2) return index-1;
return index;
}
public double recursiveSearch(IVec pt, double minU, IVec minPt, double maxU, IVec maxPt, int depthCount){
if(minPt==null){ minPt = curve.pt(minU); }
if(maxPt==null){ maxPt = curve.pt(maxU); }
double dist1 = pt.dist(minPt);
double dist2 = pt.dist(maxPt);
depthCount++;
if(dist1<=dist2){
if(dist1 < IConfig.tolerance || minPt.dist(maxPt) < IConfig.tolerance ||
depthCount >= IConfig.cacheRecursionMaxDepth){ return minU; }
return recursiveSearch(pt, minU, minPt, (minU+maxU)/2, null, depthCount);
}
if(dist2 < IConfig.tolerance || minPt.dist(maxPt) < IConfig.tolerance ||
depthCount >= IConfig.cacheRecursionMaxDepth){ return maxU; }
return recursiveSearch(pt, (minU+maxU)/2, null, maxU, maxPt, depthCount);
}
public double recursiveSearch(IVec2 pt, double minU, IVec2 minPt, double maxU, IVec2 maxPt, int depthCount){
if(minPt==null){ minPt = curve.pt(minU).to2d(); }
if(maxPt==null){ maxPt = curve.pt(maxU).to2d(); }
double dist1 = pt.dist(minPt);
double dist2 = pt.dist(maxPt);
depthCount++;
if(dist1<=dist2){
if(dist1 < IConfig.tolerance || minPt.dist(maxPt) < IConfig.tolerance ||
depthCount >= IConfig.cacheRecursionMaxDepth){ return minU; }
return recursiveSearch(pt, minU, minPt, (minU+maxU)/2, null, depthCount);
}
if(dist2 < IConfig.tolerance || minPt.dist(maxPt) < IConfig.tolerance ||
depthCount >= IConfig.cacheRecursionMaxDepth){ return maxU; }
return recursiveSearch(pt, (minU+maxU)/2, null, maxU, maxPt, depthCount);
}
public int closest(IVec pt){
int minIdx=-1;
double dist=0, minDist=0;
for(int i=0; i<pts.length; i++){
dist = pt.dist(pts[i]);
if(i==0 || dist<minDist){ minDist = dist; minIdx = i; }
}
return minIdx;
}
public int closest(IVec2 pt){
int minIdx=-1;
double dist=0, minDist=0;
for(int i=0; i<pts2.length; i++){
dist = pt.dist(pts2[i]);
if(i==0 || dist<minDist){ minDist = dist; minIdx = i; }
}
return minIdx;
}
}