-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluate.fs
1400 lines (1323 loc) · 54.8 KB
/
evaluate.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 */
// This module is part of the FeatureScript Standard Library and is distributed under the MIT License.
// See the LICENSE tab for the license text.
// Copyright (c) 2013-Present PTC Inc.
/**
* Evaluation functions return information about the topological entities in the context, like bounding boxes, tangent
* planes, projections, and collisions. Evaluation functions take a context and a map that specifies the
* computation to be performed and return a ValueWithUnits, a FeatureScript geometry type (like [Line] or [Plane]), or a special
* type like [DistanceResult]. They may also throw errors if a query fails to evaluate or the input is otherwise invalid.
*/
import(path : "onshape/std/containers.fs", version : "✨");
import(path : "onshape/std/context.fs", version : "✨");
import(path : "onshape/std/coordSystem.fs", version : "✨");
import(path : "onshape/std/curveGeometry.fs", version : "✨");
import(path : "onshape/std/feature.fs", version : "✨");
import(path : "onshape/std/mathUtils.fs", version : "✨");
import(path : "onshape/std/query.fs", version : "✨");
import(path : "onshape/std/string.fs", version : "✨");
import(path : "onshape/std/surfaceGeometry.fs", version : "✨");
import(path : "onshape/std/units.fs", version : "✨");
export import(path : "onshape/std/box.fs", version : "✨");
export import(path : "onshape/std/clashtype.gen.fs", version : "✨");
export import(path : "onshape/std/edgeconvexitytype.gen.fs", version : "✨");
export import(path : "onshape/std/smcornertype.gen.fs", version : "✨");
export import(path : "onshape/std/volumeaccuracy.gen.fs", version : "✨");
/**
* Find the centroid of an entity or group of entities. This is
* equivalent to the center of mass for a constant density object.
* Warning: This is an approximate value and it is not recommended to use this
* for modeling purposes that will be negatively affected in case the
* approximation changes. Consider using the center of a bounding box
* instead.
* @param arg {{
* @field entities {Query} : The entities to take the center of mass of.
* }}
*/
export function evApproximateCentroid(context is Context, arg is map) returns Vector
precondition
{
arg.entities is Query;
}
{
return @evApproximateCentroid(context, { "entities" : arg.entities });
}
/**
* The result of an [evApproximateMassProperties] call.
*
* @type {{
* @field mass {ValueWithUnits} : The total mass.
* @field centroid {Vector} : The center of mass with respect to the given reference frame, or with respect to the origin if a reference frame is not specified.
* @field inertia {MatrixWithUnits} : The 3D inertia tensor, with units of mass * length ^ 2. Evaluated with respect to the reference frame, or with respect to the centroid if a reference frame is not specified.
* @field volume {ValueWithUnits} : Total volume. Only returned for solid bodies.
* @field area {ValueWithUnits} : Total area. Only returned for faces.
* @field length {ValueWithUnits} : Total length. Only returned for edges.
* @field count {number} : Total count. Only returned for vertices.
* }}
*/
export type MassProperties typecheck canBeMassProperties;
/** @internal */
export predicate canBeMassProperties(value)
{
value is map;
value.mass is ValueWithUnits && value.mass.unit == MASS_UNITS;
value.volume == undefined || (value.volume is ValueWithUnits && value.volume.unit == VOLUME_UNITS);
value.area == undefined || (value.area is ValueWithUnits && value.area.unit == AREA_UNITS);
value.length == undefined || (value.length is ValueWithUnits && value.length.unit == LENGTH_UNITS);
value.count == undefined || value.count is number;
value.centroid is Vector && value.centroid[0].unit == LENGTH_UNITS;
value.inertia is MatrixWithUnits && value.inertia.unit == INERTIA_UNITS;
}
/**
* Calculates approximate mass properties of an entity or group of entities.
* Returns mass, centroid, inertia tensor, and volume/area/length/count for
* bodies/faces/edges/vertices, respectively.
* Warning: These are approximate values and it is not recommended to
* use them for modeling purposes that will be negatively affected in
* case the approximation changes.
* @param arg {{
* @field entities {Query} : The entities of which to compute mass properties. Only entities of the highest dimensionality will be considered.
* @field density {ValueWithUnits} : The density of the entities, with appropriate units.
* @eg `1 * kilogram / meter ^ 3` could be the density of 3D solid bodies
* @eg `1 * kilogram / meter ^ 2` could be the density of 2D faces or sheet bodies
* @eg `1 * kilogram / meter` could be the density of 1D edges or wire bodies
* @eg `1 * kilogram` could be the mass of each 0D vertex or point body
* @field referenceFrame {CoordSystem} : Optional coordinate system. Defaults to the centroid with world axes for the inertia tensor, and world coordinates for the centroid. @optional
* }}
*/
export function evApproximateMassProperties(context is Context, arg is map) returns MassProperties
precondition
{
arg.entities is Query;
arg.density is ValueWithUnits;
arg.referenceFrame == undefined || arg.referenceFrame is CoordSystem;
}
{
var massProperties = @evApproximateMassProperties(context, { "entities" : arg.entities, "referenceFrame" : arg.referenceFrame });
var density = arg.density;
var highestDimension = massProperties.highestDimension;
if (highestDimension == 0)
{
if (density.unit != MASS_UNITS)
throw "Density for a 0-dimensional entity must have units of mass.";
}
else
{
if ((density * meter ^ highestDimension).unit != MASS_UNITS)
throw "Density for a " ~ highestDimension ~ "-dimensional entity must have units of mass / length^" ~ highestDimension ~ ".";
}
var result = {};
if (highestDimension == 3)
{
result.volume = massProperties.volume * meter ^ 3;
result.mass = density * result.volume;
}
else if (highestDimension == 2)
{
result.area = massProperties.area * meter ^ 2;
result.mass = density * result.area;
}
else if (highestDimension == 1)
{
result.length = massProperties.length * meter;
result.mass = density * result.length;
}
else if (highestDimension == 0)
{
result.count = massProperties.count;
result.mass = density * result.count;
}
result.centroid = vector(massProperties.centroid) * meter;
result.inertia = (massProperties.inertia as Matrix) * meter ^ (highestDimension + 2) * density;
return result as MassProperties;
}
/**
* Return the total area of all the entities.
* If no matching 2D faces are found the total area will be zero.
* @param arg {{
* @field entities{Query}
* }}
*/
export function evArea(context is Context, arg is map) returns ValueWithUnits
precondition
{
arg.entities is Query;
}
{
var faces = qEntityFilter(arg.entities, EntityType.FACE);
var ownedFaces = qOwnedByBody(qFlattenedCompositeParts(arg.entities), EntityType.FACE);
return @evArea(context, { "faces" : qUnion([faces, ownedFaces]) });
}
/**
* If the query finds one entity with an axis -- a line, circle,
* plane, cylinder, cone, sphere, torus, mate connector, or revolved surface -- return
* the axis.
* Otherwise throw an exception.
* @param arg {{
* @field axis{Query}
* }}
*/
export function evAxis(context is Context, arg is map) returns Line
precondition
{
arg.axis is Query;
}
{
return @evAxis(context, arg);
}
/**
* Find a bounding box around an entity, optionally with respect
* to a given coordinate system. There is also an option to use
* a faster but less accurate method.
* @param arg {{
* @field topology{Query} : The entity to find the bounding box of.
* @field cSys{CoordSystem} : The coordinate system to use (if not the standard coordinate system). @optional
* @field tight{boolean} : Get the tightest possible bounding box. Defaults to `true`.
* @eg `true`for a bounding box precisely at the extents of the given entities (and no bigger).
* @eg `false` for a bounding box at least as big as the given entities, using a faster algorithm.
* @optional
* }}
*/
export function evBox3d(context is Context, arg is map) returns Box3d
precondition
{
arg.topology is Query;
arg.cSys == undefined || arg.cSys is CoordSystem;
arg.tight == undefined || arg.tight is boolean;
}
{
var result = @evBox(context, arg);
return box3d(result.minCorner, result.maxCorner);
}
/**
* Find collisions between tools and targets. Each collision is a
* map with field `type` of type [ClashType] and fields `target`,
* `targetBody`, `tool`, and `toolBody` of type [Query].
* @param context
* @param arg {{
* @field tools{Query} @field targets{Query}
* }}
* @returns {array}
*/
export function evCollision(context is Context, arg is map) returns array
precondition
{
arg.tools is Query;
arg.targets is Query;
if (arg.passOwners != undefined)
arg.passOwners is boolean;
}
{
if (arg.passOwners == undefined)
arg.passOwners = false;
return @evCollisionDetection(context, { "tools" : arg.tools, "targets" : arg.targets, "owners" : arg.passOwners });
}
/**
* Return the type of corner found at a vertex of a sheet metal model
* @param context
* @param arg {{
* @field vertex{Query}
* }}
* @throws {GBTErrorStringEnum.BAD_GEOMETRY} : The query does not evaluate to a single vertex
* @returns {{
* @field cornerType {SMCornerType} : the type of the corner
* @field primaryVertex {Query} : the vertex that defines the corner
* @field allVertices {array} : array of transient queries for all definition vertices associated with the corner
* }}
*/
export function evCornerType(context is Context, arg is map) returns map
precondition
{
arg.vertex is Query;
}
{
var data = @evCornerType(context, arg);
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V488_CLASSIFY_CORNER_RETURNS_MAP))
{
var allVertices = [];
if (data.allVertices != undefined)
{
for (var vert in data.allVertices)
{
allVertices = append(allVertices, qTransient(vert));
}
}
return {
"cornerType" : data.cornerType as SMCornerType,
"primaryVertex" : qTransient(data.primaryVertex),
"allVertices" : allVertices
};
}
else
{
return {
"cornerType" : data as SMCornerType,
"primaryVertex" : arg.vertex
};
}
}
/**
* Given a query for a curve, return a [Circle], [Ellipse], [Line], or [BSplineCurve]
* value for the curve. If the curve is none of these types, return
* a map with unspecified contents.
* @param arg {{
* @field edge{Query} : The curve to evaluate.
* @field returnBSplinesAsOther{boolean} : If true, do not return B-spline curves (to avoid the associated time
* cost). Default is false. @optional
* }}
* @throws {GBTErrorStringEnum.INVALID_INPUT} : The first resolved entity was not an edge.
* @throws {GBTErrorStringEnum.CANNOT_RESOLVE_ENTITIES} : Input entities are invalid or there are no input entities.
*/
export function evCurveDefinition(context is Context, arg is map) returns map
precondition
{
arg.edge is Query;
arg.returnBSplinesAsOther == undefined || arg.returnBSplinesAsOther is boolean;
}
{
return @evCurveDefinition(context, arg);
}
/**
* Given a query for a curve, return its approximation (or exact representation if possible) as a B-spline.
* The options `forceCubic` and `forceNonRational` may be used to restrict the type of spline that is returned,
* but even if these options are false, a cubic non-rational spline may be returned.
* @param arg {{
* @field edge{Query} : The curve to approximate.
* @field forceCubic{boolean} : If true, a cubic spline will be returned. Defaults to false. @optional
* @field forceNonRational{boolean} : If true, a non-rational spline will be returned. Defaults to false. @optional
* @field tolerance{number} : Specifies the desired approximation tolerance: the maximum distance (in meters) between
* the original curve and the returned spline representation. Default is 1e-6, minimum is
* 1e-8, and maximum is 1e-2. @optional
* }}
*/
export function evApproximateBSplineCurve(context is Context, arg is map) returns BSplineCurve
precondition
{
arg.edge is Query;
arg.forceCubic == undefined || arg.forceCubic is boolean;
arg.forceNonRational == undefined || arg.forceNonRational is boolean;
arg.tolerance == undefined || (arg.tolerance is number && arg.tolerance >= 1e-8 && arg.tolerance <= 1e-2);
}
{
return @evApproximateBSplineCurve(context, arg);
}
// =========== evDistance stuff ===========
/**
* The result of an [evDistance] call -- information about the extremal distance and the attaining point / line / entity.
*
* @type {{
* @field distance {ValueWithUnits} : The minimal or maximal distance.
* @field sides {array} : An array of 2 maps, containing information about where the extremum was found for each side. Each map has a:
*
* `point` (Vector) : The position in world space that is closest or farthest to the other side. The `distance` field is measured between the two values of `point`.
*
* `index` (integer) : the index into the line or point array or into the query results, if a query is passed in.
*
* `parameter` (number or length or array of two numbers) : If the `index` refers to an edge,
* the `parameter` is a number between 0 and 1 (unless `extend` for that side was passed in). It is in the form that
* [evEdgeTangentLine] and [evEdgeCurvature] consume (with `arcLengthParameterization` set to the same value that was passed into [evDistance]).
*
* If the `index` refers to a point, the `parameter` is 0.
*
* If the `index` refers to a [Line], the `parameter` is a length representing the distance along the direction.
*
* If the `index` refers to a face, the `parameter` is a 2D [Vector] in the form that [evFaceTangentPlane] consumes. If this face is a mesh or a plane, the parameter is a 2D [Vector] of zeroes.
*
* If the `index` refers to a [Plane], the `parameter` is a 2D [Vector] representing the lengths along the plane's x and y axes.
* }}
*/
export type DistanceResult typecheck canBeDistanceResult;
predicate canBeDistanceResult(value)
{
value is map;
isLength(value.distance);
value.sides is array;
size(value.sides) == 2;
for (var sideResult in value.sides)
{
sideResult is map;
isNonNegativeInteger(sideResult.index); // Index into either input array or results of input query evaluation
is3dLengthVector(sideResult.point);
// The parameter is either one number (for a curve) or an array of two (for a surface) or a 2D length vector (for a plane). For bodies or points, the parameter is 0.
// For lines, the parameter is a length representing the distance along the direction.
if (!(sideResult.parameter is number || isLength(sideResult.parameter) || is2dPoint(sideResult.parameter)))
{
sideResult.parameter is Vector || sideResult.parameter is MeshFaceParameter;
size(sideResult.parameter) == 2;
sideResult.parameter[0] is number;
sideResult.parameter[1] is number;
}
}
}
/**
* Computes the minimum or maximum distance between geometry on `side0` and geometry on `side1`. "Geometry" means entities, points, or lines.
* When the minimum or the maximum is not uniquely defined, ties will be broken arbitrarily.
*
* @example `evDistance(context, { "side0" : vector(1, 2, 3) * meter, "side1" : query }).distance`
* returns the minimum distance from any entity returned by `query` to the point `(1, 2, 3) meters`.
*
* @example `result = evDistance(context, { "side0" : qEverything(EntityType.VERTEX), "side1" : qEverything(EntityType.VERTEX), "maximum" : true })`
* computes the pair of vertices farthest apart. `qNthElement(qEverything(EntityType.VERTEX), result.sides[0].index)`
* queries for one of these vertices.
*
* @seealso [DistanceResult]
*
* @param context {Context}
* @param arg {{
* @field side0 : One of the following: A query, or a point (3D Length Vector), or a [Line], or a [Plane], or an array of points, or an array of [Line]s, or an array of [Plane]s.
* @eg `qNthElement(qEverything(EntityType.FACE), 0)` or `vector(1, 2, 3) * meter` or `line(vector(1, 0, 1) * meter, vector(1, 1, 1)` or `plane(vector(1,1,1) * meter, vector(0,0,1), vector(1,0,0))`.
* @field extendSide0 {boolean} : If `true` and side0 is a query, bodies will be ignored and edges and faces extended to
* their possibly infinite underlying surfaces. Defaults to `false`. @optional
* @field side1 : Like `side0`.
* @autocomplete `vector(0, 0, 0) * meter`
* @field extendSide1 {boolean} : Like `extendSide0`. @optional
* @field maximum {boolean} : If `true`, compute the maximum instead of the minimum. Defaults to `false`.
* Not allowed to be `true` if a line is passed in in either side or if either `extend` is true. @optional
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter returned for edges measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-calculate parameterization.
* This field only controls the parameter returned for edges. It does not control the
* parameter returned for points, [Line]s, faces, or [Plane]s.
* @optional
* @field useFaceParameter {boolean} : For Onshape internal use. @optional
* }}
*/
export function evDistance(context is Context, arg is map) returns DistanceResult
{
return @evDistance(context, arg);
}
// =========== end of evDistance stuff ===========
/**
* Map containing the results of one collision between a ray and an entity.
*
* @type {{
* @field entity {Query} : A query for the entity hit by the ray.
* @field entityType {EntityType} : The type of the entity.
* @field parameter : Parameters for where the ray hit the entity. A unitless 2-vector for a face, a number for an edge, else undefined.
* @field intersection {Vector} : Intersection point.
* @field distance {ValueWithUnits} : Distance of the intersection point from the ray origin.
* }}
*/
export type RaycastResult typecheck canBeRaycastResult;
/** @internal */
export predicate canBeRaycastResult(value)
{
value is map;
value.entity is Query;
value.entityType is EntityType;
if (value.entityType == EntityType.FACE)
isUnitlessVector(value.parameter) && size(value.parameter) == 2;
if (value.entityType == EntityType.EDGE)
value.parameter is number;
is3dLengthVector(value.intersection);
value.distance is ValueWithUnits && value.distance.unit == LENGTH_UNITS;
}
/**
* Detect intersections between a ray and the given entities.
* @param arg {{
* @field entities{Query} : A query for target entities. If bodies are provided, the result will contain intersections for individual entities owned by the body.
* @field ray{Line} : The ray to intersect the entities. Because the passed-in `Line` is interpreted as a ray,
* by default, intersections with entities "behind" the ray origin are not detected. `includeIntersectionsBehind`
* can be set to `true` if those intersections are desired.
* @eg `line(vector(0, 0, 0) * inch, vector(1, 0, 0))` specifies the positive x-axis
* @field closest{boolean} : Get only the closest intersection with any of the entities. Defaults to `true`.
* @autocomplete `true`
* @optional
* @field includeIntersectionsBehind{boolean} : Return intersections that are behind the ray origin.
* Defaults to `false`. Cannot be set to `true` if `closest` is `true`.
* @optional
* }}
* @returns {array} : An array of [RaycastResult]s for each intersection in front of the ray, ordered from closest to farthest.
*/
export function evRaycast(context is Context, arg is map) returns array
precondition
{
arg.entities is Query;
arg.ray is Line;
arg.closest == undefined || arg.closest is boolean;
arg.includeIntersectionsBehind == undefined || arg.includeIntersectionsBehind is boolean;
}
{
return @evRaycast(context, arg);
}
/**
* Return the convexity type of the given edge,
* `CONVEX`, `CONCAVE`, `SMOOTH`, or `VARIABLE`.
* If the edge is part of a body with inside and outside
* convex and concave have the obvious meanings.
* @param context
* @param arg {{
* @field edge{Query}
* }}
* @throws {GBTErrorStringEnum.TOO_MANY_ENTITIES_SELECTED} : The query evaluates to more than one entity
* @throws {GBTErrorStringEnum.BAD_GEOMETRY} : The query does not evaluate to a single edge.
*/
export function evEdgeConvexity(context is Context, arg is map) returns EdgeConvexityType
precondition
{
arg.edge is Query;
}
{
return @evEdgeConvexity(context, arg) as EdgeConvexityType;
}
/**
* The result of an [evEdgeCurvature] call -- a coordinate system for the Frenet frame and the curvature defined at a point
*
* @type {{
* @field frame {CoordSystem} : The frame. The Z vector is the tangent, the X vector is the normal and the Y vector is the binormal
* @field curvature {ValueWithUnits} : The curvature (inverse length units).
* }}
*/
export type EdgeCurvatureResult typecheck canBeEdgeCurvatureResult;
predicate canBeEdgeCurvatureResult(value)
{
value is map;
value.curvature is ValueWithUnits;
value.curvature.unit == ({ "meter" : -1 } as UnitSpec);
}
/**
* Return a Frenet frame along an edge, with curvature.
* If the curve has zero curvature at an evaluated point then the returned normal and binormal are arbitrary
* and only the tangent is significant.
*
* @param arg {{
* @field edge {Query}: The curve to use @eg `qNthElement(qEverything(EntityType.EDGE), 1)`
* @field parameter {number}:
* A number in the range 0..1 indicating the point along the curve to evaluate the frame at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* The parameterization is identical to that used by [evEdgeTangentLines].
* Results obtained with arcLengthParameterization will have lower accuracy due to approximation.
* @optional
* @field face {Query} :
* If present, the edge orientation used is such that walking along the edge
* with "up" being the `face` normal will keep `face` to the left.
* Must be adjacent to `edge`.
* @optional
* }}
* @throws {GBTErrorStringEnum.NO_TANGENT_LINE} : A frame could not be calculated for the specified input.
*/
export function evEdgeCurvature(context is Context, arg is map) returns EdgeCurvatureResult
{
arg.parameters = [arg.parameter];
return evEdgeCurvatures(context, arg)[0];
}
/**
* Return Frenet frames along an edge, with curvature.
* If the curve has zero curvature at an evaluated point then the returned normal and binormal are arbitrary
* and only the tangent is significant.
*
* @param arg {{
* @field edge {Query}: The curve to use @eg `qNthElement(qEverything(EntityType.EDGE), 1)`
* @field parameters {array}:
* An array of numbers in the range 0..1 indicating points along
* the curve to evaluate frames at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* The parameterization is identical to that used by [evEdgeTangentLines].
* Results obtained with arcLengthParameterization will have lower accuracy due to approximation.
* @optional
* @field face {Query} :
* If present, the edge orientation used is such that walking along the edge
* with "up" being the `face` normal will keep `face` to the left.
* Must be adjacent to `edge`.
* @optional
* }}
* @returns {array} : An array of [EdgeCurvatureResult]s.
* @throws {GBTErrorStringEnum.NO_TANGENT_LINE} : A frame could not be calculated for the specified input.
*/
export function evEdgeCurvatures(context is Context, arg is map) returns array
precondition
{
arg.edge is Query;
arg.parameters is array;
for (var i in arg.parameters)
i is number;
}
{
return @evEdgeCurvatures(context, arg);
}
/**
* Returns the tangent vector of a curvature frame
* @returns {Vector} : A unit 3D vector in world space.
*/
export function curvatureFrameTangent(curvatureResult is EdgeCurvatureResult) returns Vector
{
return curvatureResult.frame.zAxis;
}
/**
* Returns the normal vector of a curvature frame
* @returns {Vector} : A unit 3D vector in world space.
*/
export function curvatureFrameNormal(curvatureResult is EdgeCurvatureResult) returns Vector
{
return curvatureResult.frame.xAxis;
}
/**
* Returns the binormal vector of a curvature frame
* @returns {Vector} : A unit 3D vector in world space.
*/
export function curvatureFrameBinormal(curvatureResult is EdgeCurvatureResult) returns Vector
{
return yAxis(curvatureResult.frame);
}
/**
* Return one tangent [Line] to an edge.
* @param arg {{
* @field edge {Query}: The curve to use @eg `qNthElement(qEverything(EntityType.EDGE), 1)`
* @field parameter {number}:
* A number in the range 0..1 indicating a point along
* the curve to evaluate the tangent at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* @field face {Query} :
* If present, the edge orientation used is such that walking along the edge
* with "up" being the `face` normal will keep `face` to the left.
* Must be adjacent to `edge`.
* @optional
* }}
* @throws {GBTErrorStringEnum.NO_TANGENT_LINE} : A tangent line could not be evaluated for the given query.
*/
export function evEdgeTangentLine(context is Context, arg is map) returns Line
{
arg.parameters = [arg.parameter];
return evEdgeTangentLines(context, arg)[0];
}
/**
* Return tangent lines to a edge.
* @param arg {{
* @field edge {Query}: The curve to use @eg `qNthElement(qEverything(EntityType.EDGE), 1)`
* @field parameters {array}:
* An array of numbers in the range 0..1 indicating points along
* the curve to evaluate tangents at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* @field face {Query} :
* If present, the edge orientation used is such that walking along the edge
* with "up" being the `face` normal will keep `face` to the left.
* Must be adjacent to `edge`.
* @optional
* }}
* @returns {array} : An array of [Line]s.
* @throws {GBTErrorStringEnum.NO_TANGENT_LINE} : A tangent line could not be evaluated for the given query.
*/
export function evEdgeTangentLines(context is Context, arg is map) returns array
precondition
{
arg.edge is Query;
arg.parameters is array;
if (arg.arcLengthParameterization != undefined)
arg.arcLengthParameterization is boolean;
for (var i in arg.parameters)
i is number;
}
{
return @evEdgeTangentLines(context, arg);
}
/**
* Evaluate the derivative of the curvature vector with respect to arc length, that is,
* the third derivative of the curve with respect to arc length.
*
* @param arg {{
* @field edge {Query}: The curve to use @eg `qNthElement(qEverything(EntityType.EDGE), 1)`
* @field parameter {number}:
* A number in the range 0..1 indicating a point along
* the curve to evaluate the tangent at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* }}
* @throws {GBTErrorStringEnum.NO_TANGENT_LINE} : The curvature derivative could not be evaluated for the given query.
*/
export function evEdgeCurvatureDerivative(context is Context, arg is map) returns Vector
precondition
{
arg.edge is Query;
arg.parameter is number;
if (arg.arcLengthParameterization != undefined)
arg.arcLengthParameterization is boolean;
}
{
arg.parameters = [ arg.parameter ];
return @evEdgeCurvatureDerivatives(context, arg)[0];
}
/**
* Return the periodicity in primary and secondary direction of a face, returned in an array of booleans.
*
* A particular direction is periodic when the face's underlying surface definition is wrapped along that direction.
* For instance, if primary direction is periodic, the parameters `[0, v]` and `[1, v]` will prepresent the same point
* for all valid `v`. If the secondary direction is periodic, the parameters `[u, 0]` and `[u, 1]` represent the same
* point for all valid `u`.
* @param arg {{
* @field face{Query} : The face on which to evaluate periodicity
* @field trimmed{boolean} : If `true` (default), return trimmed face periodicity instead of the underlying surface's. @optional
* }}
*/
export function evFacePeriodicity(context is Context, arg is map) returns array
precondition
{
arg.face is Query;
arg.trimmed is boolean || arg.trimmed is undefined;
}
{
return @evFacePeriodicity(context, arg);
}
/**
* The result of an [evFaceCurvature] call -- principal directions and curvatures at a point.
*
* The curvature along a particular direction (in the tangent plane) is the inverse of the radius of curvature
* in that direction. This curvature is positive if the radius of curvature points away from the normal direction,
* negative if it points along the normal direction, or zero if there is no curvature in that direction. The
* principal curvatures at a point are the directions of minimal and maximal curvature along the surface at that
* point.
*
* @type {{
* @field minCurvature {ValueWithUnits} : The smaller of the two principal curvatures (inverse length units).
* @field maxCurvature {ValueWithUnits} : The larger of the two principal curvatures (inverse length units).
* @field minDirection {Vector} : A 3D unit vector corresponding to `minCurvature`.
* @field maxDirection {Vector} : A 3D unit vector corresponding to `maxCurvature`.
* }}
*/
export type FaceCurvatureResult typecheck canBeFaceCurvatureResult;
predicate canBeFaceCurvatureResult(value)
{
value is map;
value.minCurvature is ValueWithUnits;
value.minCurvature.unit == ({ "meter" : -1 } as UnitSpec);
value.maxCurvature is ValueWithUnits;
value.maxCurvature.unit == ({ "meter" : -1 } as UnitSpec);
is3dDirection(value.minDirection);
is3dDirection(value.maxDirection);
perpendicularVectors(value.minDirection, value.maxDirection);
}
/**
* Given a face, calculate and return principal curvatures at a point on that face,
* specified by its parameter-space coordinates.
*
* @example ```
* // Ellipsoid measuring 10in x 4in x 6in
* fEllipsoid(context, id + "ellipsoid", {
* "center" : vector(0, 0, 0) * inch,
* "radius" : vector(5 * inch, 2 * inch, 3 * inch)
* });
*
* const ellipseFace = qCreatedBy(id + "ellipsoid", EntityType.FACE);
* const topPoint = vector(0, 0, 3) * inch; // Point on top of ellipsoid
* const distanceResult = evDistance(context, { // Closest position to topPoint on ellipseFace
* "side0" : ellipseFace,
* "side1" : topPoint
* });
* var uvCoordinatesAtTopPoint = distanceResult.sides[0].parameter;
*
* var curvatureResult = evFaceCurvature(context, {
* "face" : ellipseFace,
* "parameter" : uvCoordinatesAtTopPoint
* });
* // curvatureResult is {
* // minCurvature: 3 * inch / (5 * inch)^2,
* // maxCurvature: 3 * inch / (2 * inch)^2,
* // minDirection: vector(1, 0, 0),
* // maxDirection: vector(0, 1, 0)
* // }
* ```
*
* @param context {Context}
* @param arg {{
* @field face {Query}: The face on which to evaluate the curvature. The face cannot be a mesh.
* @eg `qNthElement(qEverything(EntityType.FACE), 1)`
* @field parameter {Vector}: a 2d unitless parameter-space vector specifying the location on the face.
* The coordinates are relative to the parameter-space bounding box of the face.
* @eg `vector(0.5, 0.5)`
* }}
*/
export function evFaceCurvature(context is Context, arg is map) returns FaceCurvatureResult
{
arg.parameters = [arg.parameter];
return evFaceCurvatures(context, arg)[0];
}
/**
* Given a face, calculate and return an array of principal curvatures at points on that face,
* specified by its parameter-space coordinates.
* @param context {Context}
* @param arg {{
* @field face {Query}: A single face on which to evaluate the curvatures. The face cannot be a mesh.
* @eg `qNthElement(qEverything(EntityType.FACE), 1)`
* @field parameters {array}: an array of 2d unitless parameter-space vectors specifying locations on the face.
* The coordinates are relative to the parameter-space bounding box of the face.
* @eg `[ vector(0.5, 0.5), vector(0, 1) ]`
* }}
* @returns {array} : An array of [FaceCurvatureResult]s.
*/
export function evFaceCurvatures(context is Context, arg is map) returns array
precondition
{
arg.face is Query;
arg.parameters is array;
for (var uv in arg.parameters)
{
uv is Vector || uv is MeshFaceParameter;
@size(uv) == 2;
}
}
{
return @evFaceCurvatures(context, arg);
}
/**
* Given a face, calculate and return the derivative of the second fundamental form
* of the face in a given direction.
*
* The second fundamental form is a matrix that may be computed from the principal
* curvatures of a surface as
* ```
* const curvature = evFaceCurvature(context, { ... });
* const secondFF = - curvature.minCurvature * transpose(matrix([curvature.minDirection])) * matrix([curvature.minDirection])
* - curvature.maxCurvature * transpose(matrix([curvature.maxDirection])) * matrix([curvature.maxDirection]);
* ```
*
* @param context {Context}
* @param arg {{
* @field face {Query}: The face on which to evaluate the curvature. The face cannot be a mesh.
* @eg `qNthElement(qEverything(EntityType.FACE), 1)`
* @field parameter {Vector}: a 2d unitless parameter-space vector specifying the location on the face.
* The coordinates are relative to the parameter-space bounding box of the face.
* @eg `vector(0.5, 0.5)`
* @field direction {Vector}: a 3d unitless vector specifying a direction in the tangent
* plane of the face. It should be a unit vector perpendicular to the face's
* normal at the given point.
* }}
* @returns {MatrixWithUnits} : A 3x3 matrix with units of length ^ -2.
*/
export function evFaceCurvatureDerivative(context is Context, arg is map) returns MatrixWithUnits
precondition
{
arg.face is Query;
arg.parameter is Vector;
@size(arg.parameter) == 2;
arg.direction is Vector;
@size(arg.direction) == 3;
}
{
arg.parameters = [ arg.parameter ];
arg.directions = [ arg.direction ];
var result = @evFaceCurvatureDerivatives(context, arg)[0];
var resultWithUnits = matrix(result) * meter ^ -2 as MatrixWithUnits;
return resultWithUnits;
}
/**
* Return the surface normal of a face at a position on one of its edges.
*
* If the first result is not a face, throw an exception.
* @param arg {{
* @field edge{Query}
* @field face{Query}
* @field parameter {number}:
* A number in the range 0..1 indicating a point along
* the edge to evaluate the tangent at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* @field usingFaceOrientation{boolean}:
* If true, the edge orientation used is such that walking along the edge with "up" being the `face`
* normal will keep `face` to the left. If false, use the default orientation of the edge,
* which is the same orientation used by [evEdgeTangentLine]. Default is `false`.
* @optional
* }}
*/
export function evFaceNormalAtEdge(context is Context, arg is map) returns Vector
{
return evFaceTangentPlaneAtEdge(context, arg).normal;
}
/**
* Return a [Plane] tangent to face at a position on one of its edges.
*
* If the first result is not a face, throw an exception.
* @param arg {{
* @field edge{Query}
* @field face{Query}
* @field parameter {number}:
* A number in the range 0..1 indicating a point along
* the edge to evaluate the tangent at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* @field usingFaceOrientation{boolean}:
* If true, the edge orientation used is such that walking along the edge with "up" being the `face`
* normal will keep `face` to the left. If false, use the default orientation of the edge,
* which is the same orientation used by [evEdgeTangentLine]. Default is `false`.
* @optional
* }}
*/
export function evFaceTangentPlaneAtEdge(context is Context, arg is map) returns Plane
precondition
{
arg is map;
arg.edge is Query;
arg.face is Query;
arg.parameter is number;
arg.arcLengthParameterization is undefined || arg.arcLengthParameterization is boolean;
arg.usingFaceOrientation is undefined || arg.usingFaceOrientation is boolean;
}
{
if (isAtVersionOrLater(context, FeatureScriptVersionNumber.V1602_TANGENT_PLANES_AT_EDGE_BUILTIN))
{
arg.parameters = [arg.parameter];
return evFaceTangentPlanesAtEdge(context, arg)[0];
}
else
{
var edgeTangent = evEdgeTangentLine(context, {
"edge" : arg.edge,
"parameter" : arg.parameter,
"face" : (arg.usingFaceOrientation == true) ? arg.face : undefined,
"arcLengthParameterization" : arg.arcLengthParameterization
});
var distData = evDistance(context, {
"side0" : arg.face,
"side1" : edgeTangent.origin
});
var parameter = distData.sides[0].parameter;
var faceTangent = evFaceTangentPlane(context, {
"face" : arg.face,
"parameter" : parameter
});
return faceTangent;
}
}
/**
* Return an array of [Plane]s tangent to a face at an array of parameters on one of its edges. The x-direction of the
* plane is oriented with the tangent of the edge with respect to `usingFaceOrientation`.
*
* If the first result is not a face, throw an exception.
* @param arg {{
* @field edge{Query}
* @field face{Query}
* @field parameters {array}:
* An array of numbers in the range 0..1 indicating points along
* the edge to evaluate the tangent at.
* @field arcLengthParameterization {boolean} :
* If true (default), the parameter measures distance
* along the edge, so `0.5` is the midpoint.
* If false, use an arbitrary but faster-to-evaluate parameterization.
* @optional
* @field usingFaceOrientation{boolean}:
* If true, the edge orientation used is such that walking along the edge with "up" being the `face`
* normal will keep `face` to the left. If false, use the default orientation of the edge,
* which is the same orientation used by [evEdgeTangentLine]. Default is `false`.
* @optional
* }}
*/
export function evFaceTangentPlanesAtEdge(context is Context, arg is map)
precondition
{
arg is map;
arg.edge is Query;
arg.face is Query;
arg.parameters is array;
arg.arcLengthParameterization is undefined || arg.arcLengthParameterization is boolean;
arg.usingFaceOrientation is undefined || arg.usingFaceOrientation is boolean;
}
{
return @evFaceTangentPlanesAtEdge(context, arg);
}
/**
* Given a face, calculate and return a [Plane] tangent to that face,
* where the plane's origin is at the point specified by its parameter-space coordinates.
* @param context {Context}
* @param arg {{
* @field face {Query}: The face to evaluate. The face cannot be a mesh.
* @eg `qNthElement(qEverything(EntityType.FACE), 1)`
* @field parameter {Vector}: 2d unitless parameter-space vector specifying the location of tangency on the face. The coordinates are relative to the parameter-space bounding box of the face.
* @eg `vector(0.5, 0.5)` places the origin at the bounding box's center.
* }}
* @throws {GBTErrorStringEnum.NO_TANGENT_PLANE} : Could not find a tangent plane or there was a problem with face parameterization.