-
Notifications
You must be signed in to change notification settings - Fork 34
/
ICircleGeo.java
415 lines (345 loc) · 12.8 KB
/
ICircleGeo.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*---
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;
/**
Geometry class of a circle.
Implemented as a type of NURBS curve.
@author Satoru Sugihara
*/
public class ICircleGeo extends ICurveGeo{
public static int circleDeg(){ return 2; }
public static double[] circleKnots(){
return new double[]{ 0.,0.,0.,.25,.25,.5,.5,.75,.75,1.,1.,1. };
}
public static IVec4[] circleCP(IVec center, double radius){
return circleCP(center,IVec.zaxis,null,radius,radius);
}
public static IVec4[] circleCP(IVec center, IVec normal, double radius){
return circleCP(center,normal,null,radius,radius);
}
public static IVec4[] circleCP(IVec center, IVec normal,
IVec rollDir, double radius){
return circleCP(center,normal,rollDir,radius,radius);
}
public static IVec4[] circleCP(IVec center, IVec normal, IVec rollDir,
double xradius, double yradius){
//IOut.err("center = "+center);
//IOut.err("normal = "+normal);
//IOut.err("rollDir = "+rollDir);
//IOut.err("xradius = "+xradius);
//IOut.err("yradius = "+yradius);
if(rollDir==null) rollDir=IVec.zaxis;
if(normal.cross(rollDir).len2()==0){
if(rollDir.cross(IVec.zaxis).len2()==0) rollDir = IVec.xaxis;
else rollDir = IVec.zaxis;
}
//IOut.p("normal = "+normal);
//IOut.p("rollDir = "+rollDir);
IVec y = normal.cross(rollDir);
IVec x = y.cross(normal);
//IOut.p("x = "+x);
//IOut.p("y = "+y);
x.len(xradius);
y.len(yradius);
IVec4[] cpts = new IVec4[9];
for(int i=0; i<cpts.length-1; i++) cpts[i] = center.to4d();
final double sqrt2 = Math.sqrt(2)/2;
cpts[0].add(x);
cpts[1].add(x).add(y).w = sqrt2;
cpts[2].add(y);
cpts[3].sub(x).add(y).w = sqrt2;
cpts[4].sub(x);
cpts[5].sub(x).sub(y).w = sqrt2;
cpts[6].sub(y);
cpts[7].add(x).sub(y).w = sqrt2;
cpts[8] = cpts[0].dup();
//for(int i=0; i<cpts.length-1; i++) IOut.p("cpts["+i+"]="+cpts[i]); //
return cpts;
}
public static IVec4[] ovalCP(IVec center, IVec xaxis, IVec yaxis){
IVec4[] cpts = new IVec4[9];
for(int i=0; i<cpts.length-1; i++){ cpts[i]=center.to4d(); }
final double sqrt2 = Math.sqrt(2)/2;
cpts[0].add(xaxis);
cpts[1].add(xaxis).add(yaxis).w=sqrt2;
cpts[2].add(yaxis);
cpts[3].sub(xaxis).add(yaxis).w=sqrt2;
cpts[4].sub(xaxis);
cpts[5].sub(xaxis).sub(yaxis).w=sqrt2;
cpts[6].sub(yaxis);
cpts[7].add(xaxis).sub(yaxis).w=sqrt2;
cpts[8] = cpts[0].dup();
return cpts;
}
/**
for some platform where regular circle cp with duplicates cannot be
shown properly. (ex. AI)
knots for these control points can be uniform.
*/
public static IVec[] circleCPApprox(IVec center,
IVec normal,
IVec rollDir,
double xradius,
double yradius){
if(rollDir==null) rollDir=IVec.zaxis;
if(normal.cross(rollDir).len2()==0){
if(rollDir.cross(IVec.zaxis).len2()==0) rollDir = IVec.xaxis;
else rollDir = IVec.zaxis;
}
IVec y = normal.cross(rollDir);
IVec x = y.cross(normal);
x.len(xradius);
y.len(yradius);
IVec[] cpts = new IVec[10];
for(int i=0; i<cpts.length-1; i++) cpts[i] = center.dup();
final double r = Math.sqrt(2) -1;
cpts[0].add(x);
cpts[1].add(x).add(y.dup().mul(r));
cpts[2].add(y).add(x.dup().mul(r));
cpts[3].add(y).sub(x.dup().mul(r));
cpts[4].sub(x).add(y.dup().mul(r));
cpts[5].sub(x).sub(y.dup().mul(r));
cpts[6].sub(y).sub(x.dup().mul(r));
cpts[7].sub(y).add(x.dup().mul(r));
cpts[8].add(x).sub(y.dup().mul(r));
cpts[9] = cpts[0].dup();
return cpts;
}
public static IVec[] ovalCPApprox(IVec center, IVec xaxis, IVec yaxis){
IVec[] cpts = new IVec[10];
final double r = Math.sqrt(2)-1.0;
for(int i=0; i<cpts.length-1; i++) cpts[i]=center.dup();
cpts[0].add(xaxis);
cpts[1].add(xaxis).add(yaxis.dup().mul(r));
cpts[2].add(yaxis).add(xaxis.dup().mul(r));
cpts[3].add(yaxis).sub(xaxis.dup().mul(r));
cpts[4].sub(xaxis).add(yaxis.dup().mul(r));
cpts[5].sub(xaxis).sub(yaxis.dup().mul(r));
cpts[6].sub(yaxis).sub(xaxis.dup().mul(r));
cpts[7].sub(yaxis).add(xaxis.dup().mul(r));
cpts[8].add(xaxis).sub(yaxis.dup().mul(r));
cpts[9] = cpts[0].dup();
return cpts;
}
public static ICircleGeo circumcircle(IVecI pt1, IVecI pt2, IVecI pt3){
IVec center = IVec.circumcenter(pt1.get(),pt2.get(),pt3.get());
double rad = center.dist(pt1);
IVec nml = pt1.get().nml(pt2,pt3);
return new ICircleGeo(center,nml,rad);
}
public IVecI center;
public IVecI normal;
public IDoubleI xradius, yradius;
public ICircleGeo(IVecI center, IVecI normal, IDoubleI radius){
this(center,normal,radius,radius,false);
}
public ICircleGeo(IVecI center, IVecI normal, double radius){
this(center,normal,new IDouble(radius),false);
}
public ICircleGeo(IVecI center, IVecI normal, IDoubleI xradius, IDoubleI yradius){
this(center,normal,xradius,yradius,false);
}
public ICircleGeo(IVecI center, IVecI normal, double xradius, double yradius){
this(center,normal,new IDouble(xradius),new IDouble(yradius),false);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, double radius){
this(center,normal,rollDir,new IDouble(radius),false);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, IDoubleI radius){
this(center,normal,rollDir,radius,radius,false);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, double xradius, double yradius){
this(center,normal,rollDir,new IDouble(xradius),new IDouble(yradius),false);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, IDoubleI xradius, IDoubleI yradius){
this(center,normal,rollDir,xradius,yradius,false);
}
public ICircleGeo(IVecI center, IVecI xradiusVec, IVecI yradiusVec){
this(center,xradiusVec,yradiusVec,false);
}
public ICircleGeo(IVecI center, IVecI normal, IDoubleI radius, boolean approx){
this(center,normal,radius,radius,approx);
}
public ICircleGeo(IVecI center, IVecI normal, double radius, boolean approx){
this(center,normal,new IDouble(radius),approx);
}
public ICircleGeo(IVecI center, IVecI normal, double xradius, double yradius, boolean approx){
this(center,normal,new IDouble(xradius),new IDouble(yradius),approx);
}
public ICircleGeo(IVecI center, IVecI normal, IDoubleI xradius, IDoubleI yradius, boolean approx){
super();
this.center = center;
this.normal = normal;
this.xradius = xradius;
this.yradius = yradius;
if(approx) initApprox();
else init();
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, double radius, boolean approx){
this(center,normal,rollDir,new IDouble(radius),approx);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, IDoubleI radius, boolean approx){
this(center,normal,rollDir,radius,radius,approx);
}
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, double xradius, double yradius, boolean approx){
this(center,normal,rollDir,new IDouble(xradius),new IDouble(yradius),approx);
}
/**
@param rollDir direction of start point
*/
public ICircleGeo(IVecI center, IVecI normal, IVecI rollDir, IDoubleI xradius, IDoubleI yradius, boolean approx){
if(approx){
IVec[] cpts = circleCPApprox(center.get(),normal.get(),rollDir.get(),xradius.x(),yradius.x());
super.init(cpts,circleDeg());
}
else{
IVec4[] cpts = circleCP(center.get(), normal.get(), null, xradius.x(), yradius.x());
super.init(cpts,circleDeg(),circleKnots());
}
}
public ICircleGeo(IVecI center, IVecI xradiusVec, IVecI yradiusVec, boolean approx){
if(approx){
IVec[] cpts = ovalCPApprox(center.get(),xradiusVec.get(),yradiusVec.get());
super.init(cpts,circleDeg());
}
else{
IVec4[] cpts = ovalCP(center.get(),xradiusVec.get(),yradiusVec.get());
super.init(cpts,circleDeg(),circleKnots());
}
}
public void init(){
IVec4[] cpts = circleCP(center.get(), normal.get(), null, xradius.x(), yradius.x());
super.init(cpts,circleDeg(),circleKnots());
}
public void initApprox(){
IVec[] cpts = circleCPApprox(center.get(),normal.get(),null,xradius.x(),yradius.x());
super.init(cpts,circleDeg());
}
public ICircleGeo update(IVecI center, IVecI normal, IDoubleI xradius, IDoubleI yradius, boolean approx){
this.center = center;
this.normal = normal;
this.xradius = xradius;
this.yradius = yradius;
if(approx){
IVec[] cpts = circleCPApprox(center.get(),normal.get(),null,xradius.x(),yradius.x());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg()); }
}
else{
IVec4[] cpts = circleCP(center.get(), normal.get(), null, xradius.x(), yradius.x());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg(),circleKnots()); }
}
return this;
}
public ICircleGeo update(IVecI center, IVecI normal, IVecI rollDir, IDoubleI xradius, IDoubleI yradius, boolean approx){
if(approx){
IVec[] cpts = circleCPApprox(center.get(),normal.get(),rollDir.get(),xradius.x(),yradius.x());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg()); }
}
else{
IVec4[] cpts = circleCP(center.get(), normal.get(), null, xradius.x(), yradius.x());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg(),circleKnots()); }
}
return this;
}
public ICircleGeo update(IVecI center, IVecI xradiusVec, IVecI yradiusVec, boolean approx){
if(approx){
IVec[] cpts = ovalCPApprox(center.get(),xradiusVec.get(),yradiusVec.get());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg()); }
}
else{
IVec4[] cpts = ovalCP(center.get(),xradiusVec.get(),yradiusVec.get());
if(cpts.length==cpNum()){ cps(cpts); }
else{ super.init(cpts,circleDeg(),circleKnots()); }
}
return this;
}
public ICircleGeo cps(IVecI[] cps){ // set control point XYZ by array keeping the original cp vector objet
for(int i=0; i<cpNum() && i<cps.length; i++){
cp(i).set(cps[i]);
}
return this;
}
public IVec center(){ return center.get(); }
public ICircleGeo center(IVecI c){
update(c, normal, xradius, yradius, false);
return this;
}
public IVec nml(){ return normal.get(); }
public ICircleGeo nml(IVec n){
update(center, normal, xradius, yradius, false);
return this;
}
public double radius(){ return xradius.x(); }
public ICircleGeo radius(double r){
IDouble rad = new IDouble(r);
update(center, normal, rad, rad, false);
return this;
}
public ICircleGeo radius(IDoubleI r){
update(center, normal, r, r, false);
return this;
}
public double xradius(){ return xradius.x(); }
public ICircleGeo xradius(double r){
update(center, normal, new IDouble(r), yradius, false);
return this;
}
public ICircleGeo xradius(IDoubleI r){
update(center, normal, r, yradius, false);
return this;
}
public double yradius(){ return xradius.x(); }
public ICircleGeo yradius(double r){
update(center, normal, xradius, new IDouble(r), false);
return this;
}
public ICircleGeo yradius(IDoubleI r){
update(center, normal, xradius, r, false);
return this;
}
/*
// for debug
public void pt(double u, IVec retval){
//super.pt(u,retval);
IOut.p("retval = "+retval); //
int index = basisFunction.index(u);
double n[] = basisFunction.eval(index, u);
double weight=0;
for(int i=0; i<=degree; i++){
IVec cpt=controlPoints[index-degree+i].get();
double w=1.;
if(!defaultWeights[index-degree+i]) w=((IVec4)cpt).w;
retval.x += cpt.x*w*n[i];
retval.y += cpt.y*w*n[i];
retval.z += cpt.z*w*n[i];
weight += w*n[i];
IOut.p("cpt["+i+"] = "+cpt);
IOut.p("i="+i+", w="+w+", n="+n[i]);
IOut.p("retval = "+retval);
IOut.p("weight = "+weight);
}
retval.x/=weight;
retval.y/=weight;
retval.z/=weight;
IOut.p("weight = "+weight);
IOut.p("u="+u+", retval = "+retval); //
}
*/
}