-
Notifications
You must be signed in to change notification settings - Fork 2
/
bridgingCurve.fs
2027 lines (1827 loc) · 82 KB
/
bridgingCurve.fs
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
FeatureScript ✨; /* Automatically generated version */
import(path : "onshape/std/containers.fs", version : "✨");
import(path : "onshape/std/coordSystem.fs", version : "✨");
import(path : "onshape/std/curveGeometry.fs", version : "✨");
import(path : "onshape/std/evaluate.fs", version : "✨");
import(path : "onshape/std/feature.fs", version : "✨");
import(path : "onshape/std/manipulator.fs", version : "✨");
import(path : "onshape/std/math.fs", version : "✨");
import(path : "onshape/std/matrix.fs", version : "✨");
import(path : "onshape/std/splineUtils.fs", version : "✨");
import(path : "onshape/std/surfaceGeometry.fs", version : "✨");
import(path : "onshape/std/valueBounds.fs", version : "✨");
import(path : "onshape/std/vector.fs", version : "✨");
import(path : "onshape/std/debug.fs", version : "✨");
/**
* Specifies how the bridging curve will match the vertex or edge at each side
* @value POSITION : The bridging curve will end at the provided vertex. Direction of the curve is unspecified
* @value TANGENCY : The bridging curve will end at the vertex and the curve will be tangent to the edge
* @value CURVATURE : The bridging curve will end at the vertex and the curve will have same curvature as the edge at the vertex
*/
export enum BridgingCurveMatchType
{
annotation { "Name" : "Match position" }
POSITION,
annotation { "Name" : "Match tangent" }
TANGENCY,
annotation { "Name" : "Match curvature" }
CURVATURE,
annotation { "Name" : "Match flow (G3)" }
G3
}
/** @internal */
export enum BridgingCurveMethod
{
annotation { "Name" : "Magnitude/bias" }
MAGNITUDE_BIAS, // The legacy method
annotation { "Name" : "Control points" }
CONTROL_POINTS
}
/**
* A `RealBoundSpec` for bias of a tangency/tangency bridge, defaulting to `0.5`.
*/
export const BIAS_BOUNDS =
{
(unitless) : [0.0001, 0.5, 0.9999]
} as RealBoundSpec;
const UI_SCALING = 0.5;
const ANGLE_RANGE = 30 * degree;
const SIDE_1_MANIPULATOR = "Side1Manip";
const SIDE_2_MANIPULATOR = "Side2Manip";
const ANGLE_1_MANIPULATOR = "Angle1Manip";
const ANGLE_2_MANIPULATOR = "Angle2Manip";
const MAGNITUDE_MANIPULATOR = "magnitudeManipulator";
const CENTRAL_MAGNITUDE_MANIPULATOR = "centralMagnitudeManipulator";
const BIAS_MANIPULATOR = "biasManipulator";
const DEFAULT_BIAS_MINIMUM = 0.25;
const DEFAULT_G1G1_SCALE = 2 / 3;
function magnitudeManipulatorId(side is number) returns string
{
return (side == 1 ? "Start" : "End") ~ " magnitude";
}
function curvatureManipulatorId(side is number) returns string
{
return (side == 1 ? "Start" : "End") ~ " curvature offset";
}
function g3ManipulatorId(side is number) returns string
{
return (side == 1 ? "Start" : "End") ~ " flow offset";
}
predicate methodIsControlPoints(definition is map)
{
definition.method == BridgingCurveMethod.CONTROL_POINTS
|| definition.match1 == BridgingCurveMatchType.G3
|| definition.match2 == BridgingCurveMatchType.G3;
}
/**
* Creates a curve between two points, optionally with matching of tangency or curvature to other curves at that point
*/
annotation { "Feature Type Name" : "Bridging curve",
"Editing Logic Function" : "bridgingCurveEditingLogic",
"Manipulator Change Function" : "bridgingCurveManipulator",
"UIHint" : UIHint.NO_PREVIEW_PROVIDED }
export const bridgingCurve = defineFeature(function(context is Context, id is Id, definition is map)
precondition
{
annotation { "Name" : "Preselection", "UIHint" : UIHint.ALWAYS_HIDDEN, "Filter" : EntityType.EDGE || EntityType.VERTEX || (EntityType.FACE && ConstructionObject.NO) || BodyType.MATE_CONNECTOR }
definition.preselectedEntities is Query;
annotation { "Name" : "Start", "Filter" : EntityType.EDGE || EntityType.VERTEX || (EntityType.FACE && ConstructionObject.NO) || BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 2 }
definition.side1 is Query;
annotation { "Name" : "Match", "UIHint" : UIHint.REMEMBER_PREVIOUS_VALUE, "Default" : BridgingCurveMatchType.TANGENCY }
definition.match1 is BridgingCurveMatchType;
if (definition.match1 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "Opposite direction", "UIHint" : UIHint.OPPOSITE_DIRECTION }
definition.flip1 is boolean;
}
annotation { "Name" : "End", "Filter" : EntityType.EDGE || EntityType.VERTEX || (EntityType.FACE && ConstructionObject.NO) || BodyType.MATE_CONNECTOR, "MaxNumberOfPicks" : 2 }
definition.side2 is Query;
annotation { "Name" : "Match", "Column Name" : "Second match", "UIHint" : UIHint.REMEMBER_PREVIOUS_VALUE, "Default" : BridgingCurveMatchType.TANGENCY }
definition.match2 is BridgingCurveMatchType;
if (definition.match2 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "Opposite direction", "UIHint" : UIHint.OPPOSITE_DIRECTION }
definition.flip2 is boolean;
}
if (definition.match1 != BridgingCurveMatchType.POSITION || definition.match2 != BridgingCurveMatchType.POSITION)
{
if (definition.match1 != BridgingCurveMatchType.G3 && definition.match2 != BridgingCurveMatchType.G3)
{
annotation { "Name" : "Method", "UIHint" : [UIHint.SHOW_LABEL, UIHint.REMEMBER_PREVIOUS_VALUE], "Default" : BridgingCurveMethod.CONTROL_POINTS }
definition.method is BridgingCurveMethod;
}
if (methodIsControlPoints(definition))
{
annotation { "Name" : "Edit control points", "UIHint" : UIHint.REMEMBER_PREVIOUS_VALUE }
definition.editControlPoints is boolean;
if (definition.editControlPoints)
{
annotation { "Group Name" : "Edit control points", "Collapsed By Default" : false, "Driving Parameter" : "editControlPoints" }
{
if (definition.match1 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "Start magnitude" }
isReal(definition.side1Magnitude, POSITIVE_REAL_BOUNDS);
if (definition.match1 == BridgingCurveMatchType.CURVATURE || definition.match1 == BridgingCurveMatchType.G3)
{
annotation { "Name" : "Start curvature offset" }
isReal(definition.side1CurvatureOffset, CLAMP_MAGNITUDE_REAL_BOUNDS);
}
if (definition.match1 == BridgingCurveMatchType.G3)
{
annotation { "Name" : "Start flow offset" }
isReal(definition.side1G3Offset, CLAMP_MAGNITUDE_REAL_BOUNDS);
}
}
if (definition.match2 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "End magnitude" }
isReal(definition.side2Magnitude, POSITIVE_REAL_BOUNDS);
if (definition.match2 == BridgingCurveMatchType.CURVATURE || definition.match2 == BridgingCurveMatchType.G3)
{
annotation { "Name" : "End curvature offset" }
isReal(definition.side2CurvatureOffset, CLAMP_MAGNITUDE_REAL_BOUNDS);
}
if (definition.match2 == BridgingCurveMatchType.G3)
{
annotation { "Name" : "End flow offset" }
isReal(definition.side2G3Offset, CLAMP_MAGNITUDE_REAL_BOUNDS);
}
}
}
}
}
else // Legacy controls
{
if (definition.match1 != BridgingCurveMatchType.POSITION || definition.match2 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "Magnitude" }
isReal(definition.magnitude, POSITIVE_REAL_BOUNDS);
}
if (definition.match1 != BridgingCurveMatchType.POSITION && definition.match2 != BridgingCurveMatchType.POSITION)
{
annotation { "Name" : "Bias" }
isReal(definition.bias, BIAS_BOUNDS);
}
}
}
annotation { "Name" : "side1HasVertex", "UIHint" : UIHint.ALWAYS_HIDDEN }
definition.side1HasVertex is boolean;
annotation { "Name" : "side2HasVertex", "UIHint" : UIHint.ALWAYS_HIDDEN }
definition.side2HasVertex is boolean;
if (!definition.side1HasVertex || !definition.side2HasVertex)
{
annotation { "Name" : "Edit edge positions", "UIHint" : UIHint.REMEMBER_PREVIOUS_VALUE }
definition.editEdgePositions is boolean;
if (definition.editEdgePositions)
{
annotation { "Group Name" : "Edge positions", "Collapsed By Default" : false, "Driving Parameter" : "editEdgePositions" }
{
if (!definition.side1HasVertex)
{
annotation { "Name" : "Start edge position" }
isReal(definition.startEdgeParameter, EDGE_PARAMETER_BOUNDS);
}
if (!definition.side2HasVertex)
{
annotation { "Name" : "End edge position" }
isReal(definition.endEdgeParameter, EDGE_PARAMETER_BOUNDS);
}
}
}
}
annotation { "Name" : "side1HasFace", "UIHint" : UIHint.ALWAYS_HIDDEN }
definition.side1HasFace is boolean;
annotation { "Name" : "side2HasFace", "UIHint" : UIHint.ALWAYS_HIDDEN }
definition.side2HasFace is boolean;
if ((definition.side1HasFace || definition.side2HasFace) && (definition.match1 != BridgingCurveMatchType.POSITION || definition.match2 != BridgingCurveMatchType.POSITION))
{
annotation { "Name" : "Edit tangency angles", "UIHint" : UIHint.REMEMBER_PREVIOUS_VALUE }
definition.editTangencyAngles is boolean;
if (definition.editTangencyAngles)
{
annotation { "Group Name" : "Edit angles", "Collapsed By Default" : false, "Driving Parameter" : "editTangencyAngles" }
{
if (definition.match1 != BridgingCurveMatchType.POSITION && definition.side1HasFace)
{
annotation { "Name" : "Start angle" }
isAngle(definition.startAngle, ANGLE_180_MINUS_180_BOUNDS);
}
if (definition.match2 != BridgingCurveMatchType.POSITION && definition.side2HasFace)
{
annotation { "Name" : "End angle" }
isAngle(definition.endAngle, ANGLE_180_MINUS_180_BOUNDS);
}
}
}
}
}
{
verifyNoMesh(context, definition, "side1");
verifyNoMesh(context, definition, "side2");
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V858_SM_FLAT_BUG_FIXES))
{
verifyNoSheetMetalFlatQuery(context, definition.side1, "side1", ErrorStringEnum.FLATTENED_SHEET_METAL_SKETCH_PROHIBTED);
verifyNoSheetMetalFlatQuery(context, definition.side2, "side2", ErrorStringEnum.FLATTENED_SHEET_METAL_SKETCH_PROHIBTED);
}
var remainingTransform = getRemainderPatternTransform(context,
{
"references" : qUnion([definition.side1, definition.side2])
});
const sideData = getSideDataAndAddManipulators(context, definition, true, {"id" : id});
var side1Data = sideData.side1;
var side2Data = sideData.side2;
if (!methodIsControlPoints(definition))
{
legacyBridgingCurve(context, id, definition, side1Data, side2Data);
}
else // Current code path
{
var side1 is BridgingSideData = getBridgingSideData(side1Data, definition.match1);
var side2 is BridgingSideData = getBridgingSideData(side2Data, definition.match2);
side1.speedScaleName = "side1Magnitude";
side2.speedScaleName = "side2Magnitude";
side1.speedScale = definition.side1Magnitude;
side2.speedScale = definition.side2Magnitude;
side1.curvatureOffsetScale = definition.side1CurvatureOffset;
side2.curvatureOffsetScale = definition.side2CurvatureOffset;
side1.g3OffsetScale = definition.side1G3Offset;
side2.g3OffsetScale = definition.side2G3Offset;
const controlPoints = computeBridgingControlPoints(context, side1, side2);
if (definition.editControlPoints &&
(definition.match1 != BridgingCurveMatchType.POSITION || definition.match2 != BridgingCurveMatchType.POSITION))
{
addManipulatorsForSide(context, id, definition, controlPoints, 1, side1.degree, definition.flip1);
addManipulatorsForSide(context, id, definition, controlPoints, 2, side2.degree, definition.flip2);
showControlPoints(context, id, controlPoints);
}
createBezierCurve(context, id, controlPoints);
}
showWire(context, id, qCreatedBy(id, EntityType.BODY));
transformResultIfNecessary(context, id, remainingTransform);
}, {
'match1' : BridgingCurveMatchType.TANGENCY,
'match2' : BridgingCurveMatchType.TANGENCY,
'flip1' : false,
'flip2' : false,
'method' : BridgingCurveMethod.MAGNITUDE_BIAS,
'preselectedEntities' : qNothing(),
'side1HasVertex' : false,
'side2HasVertex' : false,
'side1HasFace' : true,
'side2HasFace' : true,
'editEdgePositions' : false,
'editTangencyAngles' : false
}
);
/**
* Data type for side queries
*
* @type {{
* @field vertex {Query} : The vertex element on a side query @optional
* @field edge {Query} : The edge element on a side query @optional
* @field face {Query} : The face element on a side query @optional
* }}
*/
export type SideQueries typecheck canBeSideQueries;
predicate canBeSideQueries(value)
{
value is map;
value.vertex == undefined || value.vertex is Query;
value.edge == undefined || value.edge is Query;
value.face == undefined || value.face is Query;
value.position == undefined || value.position is Vector;
value.tangent == undefined || value.tangent is Vector;
}
/**
* Data type for precomputed side data
*
* @type {{
* @field point {Vector} : The position
* @field frame {EdgeCurvatureResult} : The coordSystem and curvature at the given position @optional
* }}
*/
export type SideData typecheck canBeSideData;
predicate canBeSideData(value)
{
value is map;
isLengthVector(value.point);
value.frame == undefined || value.frame is EdgeCurvatureResult;
// Data for g3 continuity with a curve
value.kPrime == undefined || value.kPrime is Vector;
// Data for G3 continuity with a surface
// the derivative of the second fundamental form in the tangent direction of the curve (w.r.t. arc length)
value.twoFFPrime == undefined || value.twoFFPrime is ValueWithUnits;
value.normalPrime is undefined || value.normalPrime is Vector;
}
/**
* Aggregation of both sides SideData
*
* @type {{
* @field side1 {SideData} : SideData map for start side
* @field side2 {SideData} : SideData map for end side
* }}
*/
export type TwoSidesData typecheck canBeTwoSidesData;
predicate canBeTwoSidesData(value)
{
value is map;
value.side1 is SideData;
value.side2 is SideData;
}
/**
* Data type for unified control points computation
*
* @type {{
* @field degree {number} : 0 for positional continuity (G0), 1 for tangent continuity (G1), 2 for curvature continuity (G2)
* @field position {Vector} : The position
* @field tangent {Vector} : @requiredif { `degree` >= 1 } The tangent direction vector
* @field speedScale {number} : How much to scale the default speed @optional
* @field curvatureDirection {Vector} : @requiredif { `degree` == 2 } The curvature direction vector
* @field curvature {ValueWithUnits} : @requiredif { `degree` == 2 } The curvature magnitude (inverse length units)
* @field curvatureOffsetScale {number} : How much to scale the default third control point offset @optional
* }}
*/
export type BridgingSideData typecheck canBeBridgingSideData;
predicate canBeBridgingSideData(value)
{
value is map;
value.degree == 0 || value.degree == 1 || value.degree == 2 || value.degree == 3;
isLengthVector(value.position);
if (value.degree >= 1) // G1 or higher
{
is3dDirection(value.tangent);
value.speedScale == undefined || value.speedScale is number;
}
if (value.degree >= 2) // G2
{
is3dDirection(value.curvatureDirection);
value.curvature is ValueWithUnits;
value.curvatureOffsetScale == undefined || value.curvatureOffsetScale is number;
}
if (value.degree >= 3) // G3
{
value.normal == undefined || value.normal is Vector;
value.kPrime == undefined || value.kPrime is Vector;
value.twoFFPrime == undefined || value.twoFFPrime is ValueWithUnits;
// Either kPrime (for curves) or twoFFPrime should be defined
value.kPrime != undefined || value.twoFFPrime != undefined;
value.g3OffsetScale == undefined || value.g3OffsetScale is number;
}
}
function makeSideQueries(context is Context, side is Query) returns SideQueries
{
const newVersion is boolean = isAtVersionOrLater(context, FeatureScriptVersionNumber.V2150_BRIDGING_CURVE_MC_TANGENTS);
var sideQueries is SideQueries = {} as SideQueries;
const mateConnector = qBodyType(side, BodyType.MATE_CONNECTOR);
if (size(evaluateQuery(context, mateConnector)) == 1)
{
const frame = evMateConnector(context, {
"mateConnector" : mateConnector
});
sideQueries.position = frame.origin;
if (newVersion)
sideQueries.tangent = frame.zAxis;
}
const vertex = qEntityFilter(side, EntityType.VERTEX)->qSubtraction(newVersion ? mateConnector : qNothing());
if (size(evaluateQuery(context, vertex)) == 1)
{
sideQueries.vertex = vertex;
sideQueries.position = evVertexPoint(context, {
"vertex" : vertex
});
}
const edge = qEntityFilter(side, EntityType.EDGE);
if (size(evaluateQuery(context, edge)) == 1)
{
sideQueries.edge = edge;
}
const face = qEntityFilter(side, EntityType.FACE);
if (size(evaluateQuery(context, face)) == 1)
{
sideQueries.face = face;
}
return sideQueries;
}
function checkFaceContainsEdge(context is Context, face is Query, edge is Query) returns boolean
{
var results = evEdgeCurvatures(context, {
"edge" : edge,
"parameters" : [0, 0.3, 0.7, 1]
});
for (var result in results)
{
if (isQueryEmpty(context, qContainsPoint(face, result.frame.origin)))
{
return false;
}
}
return true;
}
function checkSideIsValid(context is Context, sideQueries is SideQueries, sideName is string, sideErrorMessage is string)
{
if (sideQueries.position == undefined && sideQueries.edge == undefined && sideQueries.face == undefined)
{
throw regenError(sideErrorMessage, [sideName]);
}
// We need at least a vertex or an edge on each side.
if (sideQueries.position == undefined && sideQueries.edge == undefined)
{
throw regenError(ErrorStringEnum.BRIDGING_CURVE_VERTEX_OR_EDGE_ON_SIDE, [sideName]);
}
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V1920_CONNECTING_CURVE_BETTER_INPUT_CHECKS) && sideQueries.face != undefined)
{
// We need to make sure the face contains the vertex or edge
if (sideQueries.position != undefined)
{
if (isQueryEmpty(context, qContainsPoint(sideQueries.face, sideQueries.position)))
{
throw regenError(ErrorStringEnum.BRIDGING_CURVE_VERTEX_BELONG_TO_FACE, [sideName]);
}
}
else if (sideQueries.edge != undefined)
{
// Either the edge is a face edge or it's coincident with the face.
var adjacentEdges = qAdjacent(sideQueries.face, AdjacencyType.EDGE, EntityType.EDGE);
if (isQueryEmpty(context, qIntersection([adjacentEdges, sideQueries.edge])))
{
if (!checkFaceContainsEdge(context, sideQueries.face, sideQueries.edge))
{
throw regenError(ErrorStringEnum.BRIDGING_CURVE_EDGE_BELONG_TO_FACE, [sideName]);
}
}
}
}
}
// id should be {"id" : id} if addManip is true, {} otherwise.
// Returns { s}
function getSideDataAndAddManipulators(context is Context, definition is map, addManip is boolean, id is map) returns TwoSidesData
{
var side1Data;
var side2Data;
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V1914_BRIDGING_CONNECTING_CURVE))
{
const sideQueries1 = makeSideQueries(context, definition.side1);
const sideQueries2 = makeSideQueries(context, definition.side2);
checkSideIsValid(context, sideQueries1, "side1", ErrorStringEnum.BRIDGING_CURVE_NO_START_SELECTION);
checkSideIsValid(context, sideQueries2, "side2", ErrorStringEnum.BRIDGING_CURVE_NO_END_SELECTION);
if (addManip && definition.editEdgePositions && sideQueries1.position == undefined && sideQueries1.edge != undefined)
{
addTangentManipulator(context, id.id, SIDE_1_MANIPULATOR, sideQueries1.edge, definition.startEdgeParameter, "startEdgeParameter");
}
if (addManip && definition.editEdgePositions && sideQueries2.position == undefined && sideQueries2.edge != undefined)
{
addTangentManipulator(context, id.id, SIDE_2_MANIPULATOR, sideQueries2.edge, definition.endEdgeParameter, "endEdgeParameter");
}
var startParam = definition.editEdgePositions ? definition.startEdgeParameter : 0.5;
var endParam = definition.editEdgePositions ? definition.endEdgeParameter : 0.5;
const points = getPointsLocations(context, sideQueries1, startParam, sideQueries2, endParam, definition.editEdgePositions);
const point1 = points.point1;
const point2 = points.point2;
var startAngle = definition.editTangencyAngles ? definition.startAngle : 0 * degree;
var endAngle = definition.editTangencyAngles ? definition.endAngle : 0 * degree;
side1Data = getDataForSide(context, sideQueries1, definition.editEdgePositions, startParam, startAngle, definition.match1, definition.flip1, "side1", point1, point2);
side2Data = getDataForSide(context, sideQueries2, definition.editEdgePositions, endParam, endAngle, definition.match2, definition.flip2, "side2", point2, point1);
if (definition.editTangencyAngles)
{
const midLength = norm(side1Data.point - side2Data.point) / 2;
if (definition.match1 != BridgingCurveMatchType.POSITION && side1Data.frame != undefined && !isQueryEmpty(context, qEntityFilter(definition.side1, EntityType.FACE)))
{
const frame1 = side1Data.frame.frame;
if (addManip)
{
// getDataForSide returns the frame rotated by the angle, so we need to unrotate the rotation origin
addManipulators(context, id.id, {
(ANGLE_1_MANIPULATOR) : angularManipulator({
"axisOrigin" : frame1.origin,
"axisDirection" : frame1.xAxis,
"rotationOrigin" : frame1.origin + rotationMatrix3d(frame1.xAxis, - startAngle) * frame1.zAxis * midLength,
"angle" : definition.startAngle,
"primaryParameterId" : "startAngle"
})
});
}
}
if (definition.match2 != BridgingCurveMatchType.POSITION && side2Data.frame != undefined && !isQueryEmpty(context, qEntityFilter(definition.side2, EntityType.FACE)))
{
const frame2 = side2Data.frame.frame;
if (addManip)
{
// getDataForSide returns the frame rotated by the angle, so we need to unrotate the rotation origin
addManipulators(context, id.id, {
(ANGLE_2_MANIPULATOR) : angularManipulator({
"axisOrigin" : frame2.origin,
"axisDirection" : frame2.xAxis,
"rotationOrigin" : frame2.origin + rotationMatrix3d(frame2.xAxis, - endAngle) * frame2.zAxis * midLength,
"angle" : definition.endAngle,
"primaryParameterId" : "endAngle"
})
});
}
}
}
}
else
{
side1Data = getDataForSideDeprecated(context, definition.side1, definition.match1, definition.flip1, "side1", definition.side2);
side2Data = getDataForSideDeprecated(context, definition.side2, definition.match2, definition.flip2, "side2", definition.side1);
}
return {
"side1" : side1Data,
"side2" : side2Data
} as TwoSidesData;
}
function computeCurvature(curvatures is FaceCurvatureResult, v is Vector)
{
var denom = dot(v, v);
var c1 = dot(curvatures.minDirection, v) ^ 2;
var c2 = dot(curvatures.maxDirection, v) ^ 2;
var num = c1 * curvatures.minCurvature + c2 * curvatures.maxCurvature;
return num / denom;
}
function computeCurvatureDual(curvatures is FaceCurvatureResult, v is Vector) returns Vector
{
var v1 = curvatures.minDirection * dot(curvatures.minDirection, v);
var v2 = curvatures.maxDirection * dot(curvatures.maxDirection, v);
var fdual = v1 * curvatures.minCurvature + v2 * curvatures.maxCurvature;
return fdual;
}
function getPointLocationFromVertexOrParameter(context is Context, sideQueries is SideQueries, parameter is number, useParameter is boolean)
{
if (sideQueries.position != undefined)
{
return sideQueries.position;
}
const edge = sideQueries.edge;
const face = sideQueries.face;
if (edge != undefined && (useParameter || face != undefined))
{
return evEdgeTangentLine(context, {
"edge" : edge,
"parameter" : parameter
}).origin;
}
}
function getPointsLocations(context is Context, sideQueries1 is SideQueries, side1Parameter is number, sideQueries2 is SideQueries, side2Parameter is number, useParameter is boolean) returns map
{
var point1 = getPointLocationFromVertexOrParameter(context, sideQueries1, side1Parameter, useParameter);
var point2 = getPointLocationFromVertexOrParameter(context, sideQueries2, side2Parameter, useParameter);
const side1Edge = sideQueries1.edge;
const side2Edge = sideQueries2.edge;
// If point1 is undefined, this means side1Edge is not undefined. Same for point2.
if (point1 == undefined)
{
if (point2 == undefined)
{
// We only have an edge for each side
point1 = inferVertexFromEdge(context, side1Edge, side2Edge);
}
else
{
point1 = inferVertexFromPosition(context, side1Edge, point2);
}
point1 = evVertexPoint(context, {
"vertex" : point1
});
}
if (point2 == undefined)
{
point2 = evVertexPoint(context, {
"vertex" : inferVertexFromPosition(context, side2Edge, point1)
});
}
return {
"point1" : point1,
"point2" : point2
};
}
function getBridgingSideData(sideData is map, match is BridgingCurveMatchType) returns BridgingSideData
{
var result = { "position" : sideData.point };
result.degree = switch(match) { BridgingCurveMatchType.POSITION : 0, BridgingCurveMatchType.TANGENCY : 1, BridgingCurveMatchType.CURVATURE : 2, BridgingCurveMatchType.G3 : 3 };
if (result.degree >= 1)
{
result.tangent = curvatureFrameTangent(sideData.frame);
}
if (result.degree >= 2)
{
result.curvature = sideData.frame.curvature;
result.curvatureDirection = curvatureFrameNormal(sideData.frame);
}
if (result.degree >= 3)
{
result.normal = curvatureFrameNormal(sideData.frame);
result.normalPrime = sideData.normalPrime;
result.kPrime = sideData.kPrime;
result.twoFFPrime = sideData.twoFFPrime;
}
return result as BridgingSideData;
}
/** Returns an array of control points for a bridigng bezier curve given two side constraints */
export function computeBridgingControlPoints(context is Context, side1 is BridgingSideData, side2 is BridgingSideData) returns array
{
const degree = side1.degree + side2.degree + 1;
if (tolerantEquals(side1.position, side2.position))
throw regenError(ErrorStringEnum.BRIDGING_CURVE_POSITIONS_IDENTICAL);
if (degree == 1)
return [side1.position, side2.position];
const positionDiff = side2.position - side1.position;
const positionDistance = norm(positionDiff);
const positionDiffNormalized = positionDiff / positionDistance;
var targetSpeed;
if (degree == 2) // G0 - G1 case
{
if (side1.degree == 0)
targetSpeed = determineDefaultG0G1Speed(side1.position, side2.position, side2.tangent);
else
targetSpeed = determineDefaultG0G1Speed(side2.position, side1.position, side1.tangent);
}
else // All other cases
{
// Compute the tangent speeds using approachSpeed - how quickly two particles moving along the tangents
// approach each other (maximum of 2 if they go towards each other, min of -2 if they go away from each other)
var approachSpeed = 2;
if (side1.degree > 0)
approachSpeed += dot(side1.tangent, positionDiffNormalized) - 1;
if (side2.degree > 0)
approachSpeed += dot(side2.tangent, -positionDiffNormalized) - 1;
approachSpeed = max(0, approachSpeed);
// clamp the side degrees to G2 -- G3 control points are obtained via degree elevation
var effectiveDegree = min(side1.degree, 2) + min(side2.degree, 2) + 1;
// If approachSpeed is maximal, targetSpeed is positionDistance / degree (which gives the correct answer for a line),
// and approachSpeed is 0, targetSpeed is 2 * positionDistance / degree (which gets you close to a semicircle for degree 3
// with appropriate tangent constraints).
targetSpeed = positionDistance * (2 - approachSpeed / 2) / effectiveDegree;
}
var side1Control is array = [];
var side2Control is array = [];
{
var side1mod = side1;
var side2mod = side2;
side1mod.degree = min(side1mod.degree, 2);
side2mod.degree = min(side2mod.degree, 2);
var degreeMod = side1mod.degree + side2mod.degree + 1;
var control1 = computeSideControlPoints(context, side1mod, degreeMod, targetSpeed, side2mod);
var control2 = computeSideControlPoints(context, side2mod, degreeMod, targetSpeed, side1mod);
var controlsMod = concatenateArrays([control1, reverse(control2)]);
controlsMod = elevateBezierDegree(controlsMod, degree);
side1Control = computeG3Control(side1, degree, controlsMod);
side2Control = computeG3Control(side2, degree, reverse(controlsMod));
}
return concatenateArrays([side1Control, reverse(side2Control)]);
}
function computeSideControlPoints(context is Context, side is BridgingSideData, degree is number, targetSpeed is ValueWithUnits, otherSide is BridgingSideData) returns array
{
if (side.degree == 0)
return [side.position];
if (side.speedScale == undefined)
side.speedScale = 1;
else if (abs(side.speedScale) < TOLERANCE.zeroLength)
throw regenError(ErrorStringEnum.BRIDGING_CURVE_ZERO_SPEED_SCALE, [side.speedScaleName]);
const defaultSpeed = side.degree == 2 ? speedForCurvature(context, targetSpeed, side.curvature, degree) : targetSpeed;
const speed = defaultSpeed * side.speedScale;
var control = [side.position, side.position + side.tangent * speed];
if (side.degree == 1)
return control;
// Curvature case
if (side.curvatureOffsetScale == undefined)
side.curvatureOffsetScale = 1;
if (side.g3OffsetScale == undefined)
side.g3OffsetScale = 1;
// using k = ((degree - 1) / degree) * h / |t| ^ 2, where k is curvature, t is tangent vector, h is the minimum distance
var distance = (degree / (degree - 1)) * side.curvature * speed * speed;
var curvaturePoint = control[1] + distance * side.curvatureDirection;
var curvatureSpeed = defaultSpeed * sqrt(side.speedScale);
if (otherSide.degree == 0) // G0-G2 adjustment
{
const scale = 1.13041336; // Chosen so that a bridging curve between a line and a point 45 degrees off stays in the square
var d = dot(side.tangent, normalize(otherSide.position - (control[1] + scale * side.tangent * curvatureSpeed)));
d = clamp(d, -0.5, 0);
curvatureSpeed *= 1 + d;
}
curvaturePoint += side.tangent * curvatureSpeed * side.curvatureOffsetScale;
control = append(control, curvaturePoint);
return control;
}
// We are given the controls computed by clamping to G2 at each end and elevating the degree,
// and if we want G3 continuity we will reposition controlsIn[3] appropriately.
function computeG3Control(side is BridgingSideData, degree is number, controlsIn is array) returns array
{
// If side.twoFFPrime is defined, we have a face; otherwise we have an edge.
if (side.twoFFPrime != undefined)
{
return computeG3ControlForFace(side, degree, controlsIn);
}
else
{
return computeG3ControlForEdge(side, degree, controlsIn);
}
}
function computeG3ControlForEdge(side is BridgingSideData, degree is number, controlsIn is array) returns array
{
if (side.degree < 3)
{
return subArray(controlsIn, 0, side.degree + 1);
}
var control = subArray(controlsIn, 0, 4);
// G3. We'll muck with control[3] to match k', the derivative of the curvature wrt arc length.
// Zero out the component of k' perp to the tangent (the tangent component is predetermined)
// k' = c''' / |c'|^3 - 3 c'' (c' . c'') / |c'|^5 + 4 c' (c' . c'')^2 / |c'|^7 - c' (c' . c'') / |c'|^5 - c' (c'' . c'') / |c'5|
// restricting to relevant terms
// k' = c''' / |c'|^3 - 3 c'' (c . c') / |c'|^5
// so
// c''' Perp c' = (k' Perp c') |c'|^3 + 3 (c'' - c' (c'' . c') / c'^2) (c' . c'') / |c'| ^ 2
//
// c' = d * (c[1] - c[0])
// c'' = d * (d - 1) * (c[2] - 2 c[1] + c[0])
// c''' = d * (d - 1) * (d - 2) * (c[3] - 3 c[2] + 3 c[1] - c[0])
var c1 = degree * (control[1] - control[0]);
var cmag = norm(c1);
var c2 = degree * (degree - 1) * (control[2] - 2 * control[1] + control[0]);
var c3rhs = degree * (degree - 1) * (degree - 2) * ( -3 * control[2] + 3 * control[1] - control[0]);
var denom3 = degree * (degree - 1) * (degree - 2);
// c''' -> c''' - c'''.c' c' /c^2
var c3 = degree * (degree - 1) * (degree - 2) * control[3] + c3rhs;
c3 = c1 * dot(c3, c1) / cmag ^ 2;
c3 = c3 + 3 * (c2 - c1 * dot(c1, c2) / cmag ^ 2) * dot(c1, c2) / cmag ^ 2;
if (side.kPrime != undefined)
{
var kPrime = side.kPrime;
c3 = c3 + cmag ^ 3 * (kPrime - c1 * dot(c1, kPrime) / cmag ^ 2);
}
var thirdPt = (c3 - c3rhs) / denom3;
if (side.g3OffsetScale != undefined)
{
thirdPt += (side.g3OffsetScale - 1) * c1 / degree;
}
control[3] = thirdPt;
return control;
}
function computeG3ControlForFace(side is BridgingSideData, degree is number, controlsIn is array) returns array
{
if (side.degree < 3)
{
return subArray(controlsIn, 0, side.degree + 1);
}
var control = subArray(controlsIn, 0, 4);
var c1 = degree * (control[1] - control[0]);
var cmag = norm(c1);
var t = c1 / cmag;
var c2 = degree * (degree - 1) * (control[2] - 2 * control[1] + control[0]);
var c3rhs = degree * (degree - 1) * (degree - 2) * ( -3 * control[2] + 3 * control[1] - control[0]);
var denom3 = degree * (degree - 1) * (degree - 2);
// c''' -> c''' - c'''.c' c' /c^2
var c3 = degree * (degree - 1) * (degree - 2) * control[3] + c3rhs;
var k = c2 / cmag ^ 2 - t * dot(t, c2) / cmag ^ 2;
var dkds = c3 / cmag ^ 3 - 3 * c2 * dot(t, c2) / cmag ^ 4 + 4 * t * dot(t, c2) ^ 2 / cmag ^ 4
- t * dot(t, c3) / cmag^3 - t * dot(c2, c2) / cmag ^ 4;
// c3 * normal = twoFFPrime / cmag^3 + 3 ff(c', c'')
var n = side.normal;
var k3dotNormal = dot(dkds, n);
var target = side.twoFFPrime - 3 * dot(k, side.normalPrime);
var thirdPt = control[3] + n * (target - k3dotNormal) / denom3 * cmag ^ 3;
if (side.g3OffsetScale != undefined)
{
thirdPt += (side.g3OffsetScale - 1) * c1 / degree;
}
control[3] = thirdPt;
return control;
}
/**
* Given a curvature and a target distance, and a spline degree, figure out how far to go for the second control point
* so the third control point ends up 2 * targetDistance away from the first
*/
function speedForCurvature(context is Context, targetDistance is ValueWithUnits, curvature is ValueWithUnits, degree is number) returns ValueWithUnits
{
const handleNearZeroCurvature is boolean = isAtVersionOrLater(context, FeatureScriptVersionNumber.V2244_PROPAGATION_FIDELITY);
const handleSolveQuadraticTolerance = isAtVersionOrLater(context, FeatureScriptVersionNumber.V2353_BRIDGING_CURVE_QUADRATIC_SOLVE_APPX);
// Perpendicular distance for second point is d/(d-1) * curvature * speed^2.
// Let x be the speed and assume the tangent spacing of first and second control points is the same.
// Then the squared distance from point2 to the second control point is:
// (2 * x)^2 + d^2/(d-1)^2 * k^2 x^4
// And we would like that to be (2 * targetDistance)^2.
// So, letting y = x^2, we have a quadratic equation in y with:
const a = (curvature * degree / (degree - 1)) ^ 2;
const b = 4;
const c = -4 * targetDistance ^ 2;
// if curvature * targetDistance is small enough, solveQuadratic will return 0, which causes issues.
// To mitigate this we can use the approximation.
// We use 1e-7 because empirically this is where it starts breaking down. (see BEL-222599)
const useApproximation = tolerantEquals(curvature * meter, 0, 1e-6) || (handleSolveQuadraticTolerance && (tolerantEquals(curvature * targetDistance, 0, 1e-7)));
if (!handleNearZeroCurvature)
{
if (tolerantEquals(curvature * meter, 0))
return targetDistance;
}
else if (useApproximation)
{
// we use a large tolerance here as well as a linearization factor using
// the first and second derivatives in the Taylor series of sqrt()
// around b^2 applied to the discriminant b^2-4ac
const y = (-c / b) - a * ((c ^ 2) / (b ^ 3));
return sqrt(y);
}
const y = solveQuadratic(a, b, c)[0];
return sqrt(y);
}
function solveQuadratic(a, b, c) returns array
{
const discriminant = b ^ 2 - 4 * a * c;
if (discriminant < 0)
return [];
return [(-b + sqrt(discriminant)) / (2 * a), (-b - sqrt(discriminant)) / (2 * a)];
}
function addManipulatorsForSide(context is Context, id is Id, definition is map, controlPoints is array, side is number, degree is number, flip is boolean)
{
if (degree == 0)
return;
if (side == 2)
controlPoints = reverse(controlPoints);
const direction = normalize(controlPoints[1] - controlPoints[0]);
var magnitudeManipulator is Manipulator = linearManipulator({
"base" : controlPoints[0],
"direction" : direction * (flip ? -1 : 1),
"offset" : norm(controlPoints[1] - controlPoints[0]) * (flip ? -1 : 1),
"primaryParameterId" : "side" ~ side ~ "Magnitude"
});
addManipulators(context, id, {
magnitudeManipulatorId(side) : magnitudeManipulator
});
if (degree == 1)
return;
const base = curvatureBase(definition, controlPoints);
var curvatureManipulator is Manipulator = linearManipulator({
"base" : base,
"direction" : direction,
"offset" : dot(direction, controlPoints[2] - base),
"style" : ManipulatorStyleEnum.SECONDARY,
"primaryParameterId" : "side" ~ side ~ "CurvatureOffset"
});
addManipulators(context, id, {
curvatureManipulatorId(side) : curvatureManipulator
});
if (degree == 2)
return;
const g3param = "side" ~ side ~ "G3Offset";
const g3offset = definition[g3param];
const offsetVec = controlPoints[1] - controlPoints[0];
const g3Base = controlPoints[3] - offsetVec * g3offset;
var g3Manipulator is Manipulator = linearManipulator({
"base" : g3Base,
"direction" : direction,
"offset" : g3offset * norm(offsetVec),
"style" : ManipulatorStyleEnum.SECONDARY,
"primaryParameterId" : g3param
});
addManipulators(context, id, {
g3ManipulatorId(side) : g3Manipulator
});
}
function showControlPoints(context is Context, id is Id, controlPoints is array)
{
if (!isTopLevelId(id))
{
return;
}
const controlId = id + "controlPoints";
startFeature(context, controlId, {});
try
{
opPoint(context, controlId + 0 + "point", { "point" : controlPoints[0] });
for (var i = 1; i < size(controlPoints); i += 1)
{
opPoint(context, controlId + i + "point", { "point" : controlPoints[i] });
opFitSpline(context, controlId + i + "line", { "points" : [ controlPoints[i - 1], controlPoints[i] ] });
}
const edges = qCreatedBy(controlId, EntityType.EDGE);
const vertices = qCreatedBy(controlId, EntityType.VERTEX)->qBodyType(BodyType.POINT);
addDebugEntities(context, qUnion([vertices, edges]), DebugColor.MAGENTA);
}