-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBezierBoardCrossSection.java
418 lines (329 loc) · 12.8 KB
/
BezierBoardCrossSection.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
416
417
418
package cadcore;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import boardcad.gui.jdk.BezierBoardDrawUtil;
import cadcore.BezierKnot;
import cadcore.BezierSpline;
import cadcore.MathUtils;
public class BezierBoardCrossSection implements Cloneable, Comparable {
double mPosition;
private BezierSpline mCrossSectionSpline;
private ArrayList<Point2D.Double> mCrossSectionGuidePoints;
public BezierBoardCrossSection() {
mCrossSectionSpline = new BezierSpline();
mCrossSectionGuidePoints = new ArrayList<Point2D.Double>();
}
public void setPosition(double pos) {
mPosition = pos;
}
public double getPosition() {
return mPosition;
}
public double getDeckAtPos(double pos) {
double deck = mCrossSectionSpline.getValueAtReverse(Math.abs(pos));
return deck;
}
public double getBottomAtPos(double pos) {
double bottom = mCrossSectionSpline.getValueAt(Math.abs(pos));
return bottom;
}
public double getThicknessAtPos(double pos) {
double thickness = getDeckAtPos(pos) - getBottomAtPos(pos);
return thickness;
}
public Point2D.Double getPointAtS(double s) {
Point2D.Double point = mCrossSectionSpline.getPointByS(s);
return point;
}
public Point2D.Double getNormalAtS(double s) {
double angle = mCrossSectionSpline.getNormalByS(s);
return new Point2D.Double(Math.cos(angle), Math.sin(angle));
}
public double getCenterThickness() {
return (mCrossSectionSpline.getControlPoint(
mCrossSectionSpline.getNrOfControlPoints() - 1).getPoints()[0].y - mCrossSectionSpline
.getControlPoint(0).getPoints()[0].y);
}
public double getWidth() {
return mCrossSectionSpline.getMaxX() * 2;
}
public double getReleaseAngle() {
Point2D.Double pos = getPointAtS(mCrossSectionSpline
.getSByNormalReverse(BezierBoardDrawUtil.TUCK_UNDER_DEFINITION_ANGLE
* MathUtils.DEG_TO_RAD)); // TODO: Bad dependency
BezierKnot point = mCrossSectionSpline.findBestMatch(pos);
if (point == null) {
return 0.0;
}
if (point.getEndPoint().distance(pos) > 0.9) {
return 0.0;
}
return point.getAngleBetweenTangents();
}
public double getTuckRadius() {
double sForApex = mCrossSectionSpline
.getSByNormalReverse(BezierBoardDrawUtil.APEX_DEFINITION_ANGLE
* MathUtils.DEG_TO_RAD); // TODO: Bad dependency
double sForTuck = mCrossSectionSpline
.getSByNormalReverse(BezierBoardDrawUtil.TUCK_UNDER_DEFINITION_ANGLE
* MathUtils.DEG_TO_RAD); // TODO: Bad dependency
Point2D.Double apexPos = getPointAtS(sForApex);
Point2D.Double tuckPos = getPointAtS(sForTuck);
if ((apexPos.x - tuckPos.x) < 0.18) {
return 0.0f;
}
int steps = 5;
double step = (sForTuck - sForApex) / steps;
double curvatureSum = 0.0;
for (int i = 0; i < steps; i++) {
double curvatureAtStep = mCrossSectionSpline
.getCurvatureByS(sForApex + i * step);
curvatureSum += curvatureAtStep;
}
double averageCurvature = curvatureSum / steps;
double radius = 1.0 / averageCurvature;
return radius;
}
public BezierSpline getBezierSpline() {
return mCrossSectionSpline;
}
public void setBezierSpline(BezierSpline spline) {
mCrossSectionSpline = spline;
}
public ArrayList<Point2D.Double> getGuidePoints() {
return mCrossSectionGuidePoints;
}
public void scale(double newThickness, double newWidth) {
double oldWidth = getWidth();
double oldThickness = getCenterThickness();
if (oldWidth < 0.1)
oldWidth = 0.1;
if (oldThickness < 0.1)
oldThickness = 0.1;
double newThicknessScale = Math.abs(newThickness / oldThickness);
double newWidtScale = Math.abs(newWidth / oldWidth);
if ((oldThickness * newThicknessScale) <= 0.1)
return;
if ((oldWidth * newWidtScale) <= 0.1)
return;
mCrossSectionSpline.scale(newThicknessScale, newWidtScale);
}
public int compareTo(Object other) {
BezierBoardCrossSection otherCrossSection = (BezierBoardCrossSection) other;
if (mPosition == otherCrossSection.mPosition)
return 0;
return (mPosition < otherCrossSection.mPosition) ? -1 : 1;
}
public boolean equals(Object other) {
BezierBoardCrossSection otherCrossSection = (BezierBoardCrossSection) other;
if (mPosition != otherCrossSection.mPosition)
return false;
if (mCrossSectionSpline.getNrOfControlPoints() != otherCrossSection.mCrossSectionSpline
.getNrOfControlPoints()) {
return false;
}
for (int i = 0; i < mCrossSectionSpline.getNrOfControlPoints(); i++) {
if (!mCrossSectionSpline.getControlPoint(i).equals(
otherCrossSection.mCrossSectionSpline.getControlPoint(i))) {
return false;
}
}
return true;
}
public synchronized BezierBoardCrossSection interpolate(
BezierBoardCrossSection target, double t) {
try {
// boolean hasChanged = false;
BezierBoardCrossSection interpolationClone = new BezierBoardCrossSection();
BezierBoardCrossSection interpolationTargetClone = new BezierBoardCrossSection();
// if(!interpolationClone.equals(this))
// {
interpolationClone.set(this);
// hasChanged = true;
// }
// if(!interpolationTargetClone.equals(target))
// {
// interpolationTargetClone.set(target);
//
// hasChanged = true;
//
// }
BezierBoardCrossSection interpolationSandboxCopy = new BezierBoardCrossSection();
BezierBoardCrossSection interpolationTargetSandboxCopy = new BezierBoardCrossSection();
// Create sandboxes to play in
interpolationSandboxCopy.set(this);
interpolationTargetSandboxCopy.set(target);
// Scale to same width and thickness
interpolationTargetSandboxCopy.scale(getCenterThickness(),
getWidth());
if (interpolationSandboxCopy.mCrossSectionSpline.getNrOfControlPoints() != interpolationTargetSandboxCopy.mCrossSectionSpline
.getNrOfControlPoints()) {
//
// To match a ControlPoint there are two cases that should match
//
// 1)The ControlPoint has moved
// The ControlPoint has roughly the same tangent angles and
// length but are located at a different position
// Properties to check(With priority)
// *Sequence of ControlPoints (index in ControlPointlist)
// *The ControlPoint has same continouancy
// *The tangent angle is similar
// *The tangent lengths are similar
// *Previous/next ControlPoint is similar (What if the
// previous/next ControlPoint has morphed?)
//
// 2)The ControlPoint has morphed
// The ControlPoint has roughly the same position, but the
// ControlPoints angle and length has changed
// Properties to check(With priority)
// *Sequence of ControlPoints (index in ControlPointlist)
// *Location is similar
// *Arc position is similar
// *Distance to previous/next ControlPoint is similar
// *Arc Distance to previous/next ControlPoint is similar
// *Previous/next ControlPoint is similar (What if the
// previous/next ControlPoint has morphed?)
BezierSpline mostControlPointsBezier;
BezierSpline otherBezier;
if (interpolationSandboxCopy.mCrossSectionSpline.getNrOfControlPoints() > interpolationTargetSandboxCopy.mCrossSectionSpline
.getNrOfControlPoints()) {
mostControlPointsBezier = interpolationSandboxCopy.mCrossSectionSpline;
otherBezier = interpolationTargetSandboxCopy.mCrossSectionSpline;
} else {
mostControlPointsBezier = interpolationTargetSandboxCopy.mCrossSectionSpline;
otherBezier = interpolationSandboxCopy.mCrossSectionSpline;
}
double scaleX = otherBezier.getMaxX()
/ mostControlPointsBezier.getMaxX();
double scaleY = otherBezier.getMaxY()
/ mostControlPointsBezier.getMaxY();
while (mostControlPointsBezier.getNrOfControlPoints() > otherBezier.getNrOfControlPoints()) {
BezierKnot worstMatchControlPoint = null;
double worstMatch = 0;
int worstMatchIndex = -1;
for (int i = 1; i < mostControlPointsBezier.getNrOfControlPoints() - 1; i++) {
BezierKnot currentControlPoint = mostControlPointsBezier
.getControlPoint(i);
double bestMatch = 10000000;
for (int j = 1; j < otherBezier.getNrOfControlPoints() - 1; j++) {
// Scaleing crosssection points by moving so cross
// sections have same size to better make use of the
// distance bewtween points
// Note that the tangents stay the same, scaleing
// them would change the angle which may introduce
// poor matching
BezierKnot otherControlPoint = (BezierKnot) otherBezier
.getControlPoint(j).clone();
otherControlPoint.setControlPointLocation(
otherControlPoint.getEndPoint().x * scaleX,
otherControlPoint.getEndPoint().y * scaleY);
double currentMatch = currentControlPoint
.compareTo(otherControlPoint);
if (currentMatch < bestMatch) {
bestMatch = currentMatch;
}
}
// DEBUG System.out.println("index: " + i + " match: " +
// bestMatch);
if (bestMatch > worstMatch) {
worstMatchControlPoint = currentControlPoint;
worstMatch = bestMatch;
worstMatchIndex = i;
}
}
// DEBUG System.out.println("Worst match index: " +
// worstMatchIndex);
BezierKnot newControlPoint = new BezierKnot();
int index = otherBezier.getSplitControlPoint(
worstMatchControlPoint.getPoints()[0],
newControlPoint);
// DEBUG System.out.println("Split ControlPoint: " + index);
if (index > 0) {
otherBezier.insert(index, newControlPoint);
Point2D.Double tmp = new Point2D.Double();
BezierKnot prev = otherBezier
.getControlPoint(index - 1);
BezierKnot next = otherBezier
.getControlPoint(index + 1);
BezierCurve tmpCurve = new BezierCurve(prev, next);
double ct = tmpCurve.getClosestT(worstMatchControlPoint.getPoints()[0]);
VecMath.subVector(prev.getPoints()[0],
prev.getPoints()[2], tmp);
VecMath.scaleVector(tmp, ct);
VecMath.addVector(prev.getPoints()[0], tmp,
prev.getPoints()[2]);
VecMath.subVector(next.getPoints()[1],
next.getPoints()[0], tmp);
VecMath.scaleVector(tmp, ct - 1);
VecMath.addVector(next.getPoints()[0], tmp,
next.getPoints()[1]);
} else {
return this;
}
}
}
BezierBoardCrossSection interpolated = new BezierBoardCrossSection();
interpolated.set(interpolationTargetSandboxCopy);
// }
for (int i = 0; i < interpolationTargetSandboxCopy.mCrossSectionSpline
.getNrOfControlPoints(); i++) {
BezierKnot a = interpolationSandboxCopy.mCrossSectionSpline
.getControlPoint(i);
BezierKnot b = interpolationTargetSandboxCopy.mCrossSectionSpline
.getControlPoint(i);
BezierKnot v = interpolated.mCrossSectionSpline
.getControlPoint(i);
// Calculate vectors between ControlPoints
for (int j = 0; j < 3; j++) {
VecMath.subVector(a.getPoints()[j], b.getPoints()[j],
v.getPoints()[j]);
VecMath.scaleVector(v.getPoints()[j], t);
VecMath.addVector(a.getPoints()[j], v.getPoints()[j],
v.getPoints()[j]);
}
}
return interpolated;
} catch (Exception e) {
System.out.println("Error occured in Brd::interpolate() "
+ e.toString());
return null;
}
}
public void set(BezierBoardCrossSection crossSection) {
// Remove extra control points
while (mCrossSectionSpline.getNrOfControlPoints() > crossSection.mCrossSectionSpline
.getNrOfControlPoints()) {
mCrossSectionSpline.remove(0);
}
// Add extra control points
while (mCrossSectionSpline.getNrOfControlPoints() < crossSection.mCrossSectionSpline
.getNrOfControlPoints()) {
mCrossSectionSpline.append(new BezierKnot());
}
int nrControlPoints = mCrossSectionSpline.getNrOfControlPoints();
for (int i = 0; i < nrControlPoints; i++) {
mCrossSectionSpline.getControlPoint(i).set(
crossSection.mCrossSectionSpline.getControlPoint(i));
}
}
public Object clone() {
BezierBoardCrossSection crossSection = null;
try {
crossSection = (BezierBoardCrossSection) super.clone();
} catch (CloneNotSupportedException e) {
System.out
.println("Exception in BezierBoardCrossSection::clone(): "
+ e.toString());
throw new Error("CloneNotSupportedException in CrossSection");
}
crossSection.mCrossSectionSpline = (BezierSpline) this.mCrossSectionSpline
.clone();
crossSection.mCrossSectionGuidePoints = new ArrayList<Point2D.Double>();
for (int i = 0; i < this.mCrossSectionGuidePoints.size(); i++) {
crossSection.mCrossSectionGuidePoints
.add((Point2D.Double) this.mCrossSectionGuidePoints.get(i)
.clone());
}
return crossSection;
}
}