-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBezierBoardSLinearInterpolationSurfaceModel.java
294 lines (208 loc) · 10.6 KB
/
BezierBoardSLinearInterpolationSurfaceModel.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
package cadcore;
import board.BezierBoard;
import java.awt.geom.Point2D;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import cadcore.MathUtils.Function;
class BezierBoardSLinearInterpolationSurfaceModel extends AbstractBezierBoardSurfaceModel
{
BezierSpline c1Spline = new BezierSpline();
BezierSpline c2Spline = new BezierSpline();
synchronized public Point3d getDeckAt(final BezierBoard brd,final double x, final double y)
{
final Function func = new Function(){public double f(double s){return getPointAt(brd,x,s,-90.0,90.0, true).y;};};
double s = MathUtils.RootFinder.getRoot(func, y);
return getPointAt(brd,x,s,-90.0,90.0, true);
}
synchronized public Point3d getBottomAt(final BezierBoard brd,final double x, final double y)
{
final Function func = new Function(){public double f(double s){return getPointAt(brd,x,s,90.0,270.0, true).y;};};
double s = MathUtils.RootFinder.getRoot(func, y);
return getPointAt(brd,x,s,90.0,270.0, true);
}
synchronized public Point3d getPointAt(final BezierBoard brd,double x, double s, double minAngle, double maxAngle, boolean useMinimumAngleOnSharpCorners)
{
if(x < 0.1)
x = 0.1;
if(x > brd.getLength()-0.1)
x = brd.getLength()-0.1;
BezierBoardCrossSection c1 = brd.getPreviousCrossSection(x);
BezierBoardCrossSection c2 = brd.getNextCrossSection(x);
//Scaling
double targetWidth = brd.getWidthAt(x);
double targetThickness = brd.getThicknessAtPos(x);
double c1Width = c1.getWidth();
double c1Thickness = c1.getThicknessAtPos(BezierSpline.ZERO);
double c2Width = c2.getWidth();
double c2Thickness = c2.getThicknessAtPos(BezierSpline.ZERO);
double c1ThicknessScale = targetThickness/c1Thickness;
double c1WidthScale = targetWidth/c1Width;
double c2ThicknessScale = targetThickness/c2Thickness;
double c2WidthScale = targetWidth/c2Width;
c1Spline.set(c1.getBezierSpline());
c1Spline.scale(c1ThicknessScale, c1WidthScale);
c2Spline.set(c2.getBezierSpline());
c2Spline.scale(c2ThicknessScale, c2WidthScale);
// double c1SplineWidth = c1Spline.getMaxX();
// double c2SplineWidth = c2Spline.getMaxX();
//
// double c1SplineThick = c1Spline.getMaxY();
// double c2SplineThick = c2Spline.getMaxY();
// System.out.printf("targetWidth: %f targetThickness: %f c1SplineWidth x2: %f c2SplineWidth x2: %f c1SplineThick: %f c2SplineThick: %f\n", targetWidth, targetThickness, c1SplineWidth*2.0, c2SplineWidth*2.0, c1SplineThick, c2SplineThick);
// System.out.printf("getSurfacePoint()\n");
// System.out.printf("Target width: %f thickness: %f\n", targetWidth, targetThickness);
// System.out.printf("C1 width: %f thickness: %f\n", c1Width, c1Thickness);
// System.out.printf("C2 width: %f thickness: %f\n", c2Width, c2Thickness);
// System.out.printf("C1 width scale: %f thickness scale: %f\n", c1WidthScale, c1ThicknessScale);
// System.out.printf("C2 width scale: %f thickness scale: %f\n", c2WidthScale, c2ThicknessScale);
double s1min = BezierSpline.ONE;
double s2min = BezierSpline.ONE;
double s1max = BezierSpline.ZERO;
double s2max = BezierSpline.ZERO;
if(minAngle > 0.0)
{
s1min = c1Spline.getSByNormalReverse(minAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
s2min = c2Spline.getSByNormalReverse(minAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
}
if(maxAngle < 270.0)
{
s1max = c1Spline.getSByNormalReverse(maxAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
s2max = c2Spline.getSByNormalReverse(maxAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
}
double current1S = ((s1max-s1min)*s) + s1min;
double current2S = ((s2max-s2min)*s) + s2min;
//Get the position first since we cheat with the crosssections at tip and tail
double pos1 = brd.getPreviousCrossSectionPos(x);
double pos2 = brd.getNextCrossSectionPos(x);
//New
Point2D.Double v1 = c1Spline.getPointByS(current1S);
Point2D.Double v2 = c2Spline.getPointByS(current2S);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getPointAt() v1:%f, %f\n", v1.x, v1.y);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getPointAt() v2:%f, %f\n", v2.x, v2.y);
//Get blended point
double d = (x - pos1)/(pos2 - pos1);
Point2D.Double ret = new Point2D.Double();
ret.x = ((1-d)*v1.x) + (d*v2.x);
ret.y = ((1-d)*v1.y) + (d*v2.y);
double rockerAtPos = brd.getRockerAtPos(x);
Point3d point = new Point3d(x, ret.x, ret.y + rockerAtPos);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getPointAt() x:%f, result: %f, %f, %f\n", x, point.x, point.y, point.z);
return point;
}
synchronized public Vector3d getNormalAt(final BezierBoard brd, double x, double s, double minAngle, double maxAngle, boolean useMinimumAngleOnSharpCorners)
{
if(x < 0.1)
x = 0.1;
if(x > brd.getLength()-0.1)
x = brd.getLength()-0.1;
final double X_OFFSET = 0.1;
final double S_OFFSET = 0.01;
boolean flipNormal = false;
//Get first blended point
Point3d pointS = getPointAt(brd, x, s, minAngle, maxAngle, useMinimumAngleOnSharpCorners);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getNormalAt() first point x:%f, result: %f, %f, %f\n", x, pointS.x, pointS.y, pointS.z);
//Get second blended point
BezierBoardCrossSection c1 = brd.getPreviousCrossSection(x);
BezierBoardCrossSection c2 = brd.getNextCrossSection(x);
//Scaling
double targetWidth = brd.getWidthAt(x);
double targetThickness = brd.getThicknessAtPos(x);
double c1Width = c1.getWidth();
double c1Thickness = c1.getThicknessAtPos(BezierSpline.ZERO);
double c2Width = c2.getWidth();
double c2Thickness = c2.getThicknessAtPos(BezierSpline.ZERO);
double c1ThicknessScale = targetThickness/c1Thickness;
double c1WidthScale = targetWidth/c1Width;
double c2ThicknessScale = targetThickness/c2Thickness;
double c2WidthScale = targetWidth/c2Width;
c1Spline.set(c1.getBezierSpline());
c1Spline.scale(c1ThicknessScale, c1WidthScale);
c2Spline.set(c2.getBezierSpline());
c2Spline.scale(c2ThicknessScale, c2WidthScale);
// System.out.printf("getNormalAt()\n");
// System.out.printf("Target width: %f thickness: %f\n", targetWidth, targetThickness);
// System.out.printf("C1 width: %f thickness: %f\n", c1Width, c1Thickness);
// System.out.printf("C2 width: %f thickness: %f\n", c2Width, c2Thickness);
// System.out.printf("C1 width scale: %f thickness scale: %f\n", c1WidthScale, c1ThicknessScale);
// System.out.printf("C2 width scale: %f thickness scale: %f\n", c2WidthScale, c2ThicknessScale);
double s1min = BezierSpline.ONE;
double s2min = BezierSpline.ONE;
double s1max = BezierSpline.ZERO;
double s2max = BezierSpline.ZERO;
if(minAngle > 0.0)
{
s1min = c1Spline.getSByNormalReverse(minAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
s2min = c2Spline.getSByNormalReverse(minAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
}
if(maxAngle < 270.0)
{
s1max = c1Spline.getSByNormalReverse(maxAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
s2max = c2Spline.getSByNormalReverse(maxAngle*MathUtils.DEG_TO_RAD, useMinimumAngleOnSharpCorners);
}
double current1S = ((s1max-s1min)*s) + s1min;
double current2S = ((s2max-s2min)*s) + s2min;
double current1SO = current1S+S_OFFSET;
double current2SO = current2S+S_OFFSET;
if(current1SO > 1.0 || current2SO > 1.0|| useMinimumAngleOnSharpCorners==false)
{
current1SO = current1S-S_OFFSET;
current2SO = current2S-S_OFFSET;
flipNormal = true;
}
//Get the position first since we cheat with the crosssections at tip and tail
double pos1 = brd.getPreviousCrossSectionPos(x);
double pos2 = brd.getNextCrossSectionPos(x);
Point2D.Double v1so = c1Spline.getPointByS(current1SO);
Point2D.Double v2so = c2Spline.getPointByS(current2SO);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getPointAt() v1:%f, %f\n", v1.x, v1.y);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getPointAt() v2:%f, %f\n", v2.x, v2.y);
double pSO = (x - pos1)/(pos2 - pos1);
Point2D.Double retSO = new Point2D.Double();
retSO.x = ((1-pSO)*v1so.x) + (pSO*v2so.x);
retSO.y = ((1-pSO)*v1so.y) + (pSO*v2so.y);
double rockerAtX = brd.getRockerAtPos(x);
Point3d pointSO = new Point3d(x, retSO.x, retSO.y + rockerAtX);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getNormalAt() second point x:%f, result: %f, %f, %f\n", x, pointSO.x, pointSO.y, pointSO.z);
//Get third blended point
double xo = x+X_OFFSET;
Point3d pointXO = getPointAt(brd, xo, s, minAngle, maxAngle, useMinimumAngleOnSharpCorners);
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getNormalAt() third point x:%f, result: %f, %f, %f\n", x, pointXO.x, pointXO.y, pointXO.z);
//Calculate normal
Vector3d vc = new Vector3d(0, pointS.y-pointSO.y, pointS.z-pointSO.z); //Vector across
// vc.normalize();
Vector3d vl = new Vector3d(xo-x, pointXO.y-pointS.y, pointXO.z-pointS.z); //Vector lengthwise
// vl.normalize();
Vector3d normalVec = new Vector3d();
normalVec.cross(vl,vc);
normalVec.normalize();
if(flipNormal == true)
{
normalVec.scale(-1.0);
}
// System.out.printf("BezierBoardSLinearInterpolationSurfaceModel.getNormalAt() third point x:%f, result: %f, %f, %f\n", x, pointXO.x, pointXO.y, pointXO.z);
return normalVec;
}
synchronized public double getCrosssectionAreaAt(final BezierBoard brd,final double x, int splits)
{
MathUtils.FunctionXY deckFunc = new MathUtils.FunctionXY(){
public Point2D.Double f(double s){
Point3d point = getPointAt(brd,x,s,-90.0,90.0,true);
return new Point2D.Double(point.y,point.z);
}
};
double deckIntegral = MathUtils.Integral.getIntegral(deckFunc, 0.0, 1.0, BezierBoard.AREA_SPLITS);
MathUtils.FunctionXY bottomFunc = new MathUtils.FunctionXY(){
public Point2D.Double f(double s){
Point3d point = getPointAt(brd,x,s,90.0,360.0,true);
return new Point2D.Double(point.y,point.z);
}
};
double bottomIntegral = MathUtils.Integral.getIntegral(bottomFunc, 0.0, 1.0, BezierBoard.AREA_SPLITS);
double area = deckIntegral-bottomIntegral;
area *= 2.0;
if(area < 0)
area = 0.0;
// System.out.printf("getCrosssectionAreaAt() x:%f area:%f\n", x, area);
return area;
}
}