forked from sghr/iGeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IArcGeo.java
189 lines (158 loc) · 6.23 KB
/
IArcGeo.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
/*---
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 an arc.
Implemented as a type of NURBS curve.
@author Satoru Sugihara
*/
public class IArcGeo extends ICurveGeo{
public static double maxSegmentAngle = Math.PI/8; //Math.PI/2;
public static int arcDeg(){ return 2; }
public static IVec4[] arcCP(IVec center, IVec normal, IVec startPt, double angle){
if(angle<0){
normal = normal.dup().neg();
angle = -angle;
}
int segmentNum = (int)(angle/maxSegmentAngle) + 1;
IVec4[] cpts = new IVec4[segmentNum*2+1];
IVec radiusVec = startPt.diff(center);
IVec bisectorVec = radiusVec.dup();
double radius = radiusVec.len();
double segmentAngle = angle/segmentNum;
double bisectorAngle = segmentAngle/2;
double bisectorLen = radius/Math.cos(bisectorAngle);
bisectorVec.len(bisectorLen);
bisectorVec.rot(normal, bisectorAngle);
for(int i=0; i<cpts.length; i++){
cpts[i] = center.to4d();
if(i%2==0){
cpts[i].add(radiusVec);
radiusVec.rot(normal, segmentAngle);
}
else{
cpts[i].add(bisectorVec);
cpts[i].w = Math.cos(bisectorAngle);
bisectorVec.rot(normal, segmentAngle);
}
}
return cpts;
}
public static IVec4[] arcCP(IVec center, IVec startPt, IVec endPt, boolean flipArcSide){
IVec dir1 = startPt.diff(center);
IVec dir2 = endPt.diff(center);
IVec normal = null;
if(dir1.isParallel(dir2)) normal = dir2.cross(dir1.cross(IVec.zaxis));
else normal = dir2.cross(dir1); // not dir1.dup().cross(dir2); ?
double angle = dir1.angle(dir2);
if(angle == 0 || angle == Math.PI){
if(dir1.x !=0 || dir1.y !=0) normal = dir1.cross(IVec.zaxis);
else normal = dir1.cross(IVec.xaxis);
}
if(flipArcSide){
angle = Math.PI*2 - angle;
normal.neg();
}
return arcCP(center, normal, startPt, angle);
}
public static IVec4[] arcCP(IVec center, IVec startPt, IVec midPt, IVec endPt, IVec normal){
IVec dir1 = startPt.diff(center);
IVec dir2 = endPt.diff(center);
IVec mdir = midPt.diff(center);
double angle = dir1.angle(dir2,normal);
double mangle = dir1.angle(mdir,normal);
if( angle>0 && mangle<0 ) angle = -(Math.PI*2 - angle);
else if( angle<0 && mangle>0) angle = Math.PI*2 + angle;
if(angle<0){ normal.neg(); angle = -angle; }
normal.neg(); // why?
return arcCP(center, normal, startPt, angle);
}
public static double[] arcKnots(double angle){
if(angle < 0) angle = -angle;
// angle cannot be negative
int segmentNum= (int)(angle/(maxSegmentAngle)) + 1;
double segmentAngle = angle/segmentNum;
double[] knots = new double[(segmentNum+1)*2+2];
int k=0;
double a=0.;
knots[k]=a; k++;
knots[k]=a; k++;
knots[k]=a; k++;
for(; k<knots.length-1; k++){
a+=segmentAngle;
knots[k] = a; k++;
knots[k] = a;
}
knots[k] = a;
for(k=0; k<knots.length; k++) knots[k] /= a; // normalize
return knots;
}
public IVecI center;
public IVecI normal;
public IVecI startPt;
public IDoubleI angle;
public IArcGeo(){}
public IArcGeo(IVecI center, IVecI normal, IVecI startPt, double angle){
init(center,normal,startPt,new IDouble(angle));
}
public IArcGeo(IVecI center, IVecI normal, IVecI startPt, IDoubleI angle){
init(center,normal,startPt,angle);
}
public IArcGeo(IVecI center, IVecI startPt, IVecI endPt, IBoolI flipArcSide){
init(center,startPt,endPt,flipArcSide);
}
public IArcGeo(IVecI center, IVecI startPt, IVecI endPt, boolean flipArcSide){
init(center,startPt,endPt,new IBool(flipArcSide));
}
public IArcGeo(IVecI center, IVecI startPt, IVecI midPt, IVecI endPt, IVecI normal){
init(center,startPt,midPt,endPt,normal);
}
public void init(IVecI center, IVecI normal, IVecI startPt, IDoubleI angle){
this.center = center;
this.normal = normal;
this.startPt = startPt;
this.angle = angle;
IVec4[] cpts = arcCP(center.get(),normal.get(),startPt.get(),angle.x());
super.init(cpts,arcDeg(),arcKnots(angle.x()));
}
public void init(IVecI center, IVecI startPt, IVecI endPt, IBoolI flipArcSide){
IVec dir1 = startPt.get().diff(center);
IVec dir2 = endPt.get().diff(center);
if(dir1.isParallel(dir2)) normal = dir2.cross(dir1.cross(IVec.zaxis));
else normal = dir2.cross(dir1); // not dir1.cross(dir2); ?
double a = dir1.angle(dir2);
if(a == 0 || a == Math.PI){
if(dir1.x !=0 || dir1.y !=0) normal = dir1.cross(IVec.zaxis);
else normal = dir1.cross(IVec.xaxis);
}
if(flipArcSide.x()){
a = Math.PI*2 - a;
normal.neg();
}
init(center,normal,startPt,new IDouble(a));
}
public void init(IVecI center, IVecI startPt, IVecI midPt, IVecI endPt, IVecI normal){
IVec dir1 = startPt.get().diff(center);
IVec dir2 = endPt.get().diff(center);
IVec mdir = midPt.get().diff(center);
double ang = dir1.angle(dir2,normal);
double mangle = dir1.angle(mdir,normal);
if( ang>0 && mangle<0 ) ang = -(Math.PI*2 - ang);
else if( ang<0 && mangle>0) ang = Math.PI*2 + ang;
if(ang<0){ normal.neg(); ang = -ang; }
normal.neg(); // why?
init(center,normal,startPt,new IDouble(ang));
}
}