forked from microsoft/maker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
6859 lines (6854 loc) · 290 KB
/
index.js
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
/*
__________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________
________/\\\\____________/\\\\_____/\\\\\\\\\_____/\\\________/\\\__/\\\\\\\\\\\\\\\__/\\\\\\\\\\\________________________________________
_______\/\\\\\\________/\\\\\\___/\\\\/////\\\\__\/\\\_____/\\\//__\/\\\///////////__\/\\\///////\\\_______________/\\\___________________
_______\/\\\//\\\____/\\\//\\\__/\\\/____\///\\\_\/\\\__/\\\//_____\/\\\_____________\/\\\_____\/\\\______________\///____________________
_______\/\\\\///\\\/\\\/_\/\\\_\/\\\_______\/\\\_\/\\\\\\//\\\_____\/\\\\\\\\\\\_____\/\\\\\\\\\\\/________________/\\\__/\\\\\\\\\\______
_______\/\\\__\///\\\/___\/\\\_\/\\\\\\\\\\\\\\\_\/\\\//_\//\\\____\/\\\///////______\/\\\//////\\\_______________\/\\\_\/\\\//////_______
_______\/\\\____\///_____\/\\\_\/\\\/////////\\\_\/\\\____\//\\\___\/\\\_____________\/\\\____\//\\\______________\/\\\_\/\\\\\\\\\\______
_______\/\\\_____________\/\\\_\/\\\_______\/\\\_\/\\\_____\//\\\__\/\\\_____________\/\\\_____\//\\\_________/\\_\/\\\_\////////\\\______
_______\/\\\_____________\/\\\_\/\\\_______\/\\\_\/\\\______\//\\\_\/\\\\\\\\\\\\\\\_\/\\\______\//\\\__/\\\_\//\\\\\\___/\\\\\\\\\\______
_______\///______________\///__\///________\///__\///________\///__\///////////////__\///________\///__\///___\//////___\//////////_______
__________________________________________________________________________________________________________________________________________
__________________________________________________________________________________________________________________________________________
Maker.js
https://github.com/Microsoft/maker.js
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
*/
/**
* Root module for Maker.js.
*
* Example: get a reference to Maker.js
* ```
* var makerjs = require('makerjs');
* ```
*
*/
var MakerJs;
(function (MakerJs) {
/**
* Version info
*/
MakerJs.version = 'debug';
/**
* Enumeration of environment types.
*/
MakerJs.environmentTypes = {
BrowserUI: 'browser',
NodeJs: 'node',
WebWorker: 'worker',
Unknown: 'unknown'
};
/**
* @private
*/
function detectEnvironment() {
if (('global' in this) && ('process' in this)) {
return MakerJs.environmentTypes.NodeJs;
}
if (('window' in this) && ('document' in this)) {
return MakerJs.environmentTypes.BrowserUI;
}
if (('WorkerGlobalScope' in this) && ('self' in this)) {
return MakerJs.environmentTypes.WebWorker;
}
return MakerJs.environmentTypes.Unknown;
}
/**
* Current execution environment type, should be one of environmentTypes.
*/
MakerJs.environment = detectEnvironment();
//units
/**
* String-based enumeration of unit types: imperial, metric or otherwise.
* A model may specify the unit system it is using, if any. When importing a model, it may have different units.
* Unit conversion function is makerjs.units.conversionScale().
* Important: If you add to this, you must also add a corresponding conversion ratio in the unit.ts file!
*/
MakerJs.unitType = {
Centimeter: 'cm',
Foot: 'foot',
Inch: 'inch',
Meter: 'm',
Millimeter: 'mm'
};
/**
* Numeric rounding
*
* Example: round to 3 decimal places
* ```
* makerjs.round(3.14159, .001); //returns 3.142
* ```
*
* @param n The number to round off.
* @param accuracy Optional exemplar of number of decimal places.
*/
function round(n, accuracy) {
if (accuracy === void 0) { accuracy = .0000001; }
var places = 1 / accuracy;
return Math.round(n * places) / places;
}
MakerJs.round = round;
/**
* Create a string representation of a route array.
*
* @param route Array of strings which are segments of a route.
* @returns String of the flattened array.
*/
function createRouteKey(route) {
var converted = [];
for (var i = 0; i < route.length; i++) {
var element = route[i];
var newElement;
if (i % 2 === 0) {
newElement = '.' + element;
}
else {
newElement = JSON.stringify([element]);
}
converted.push(newElement);
}
return converted.join('');
}
MakerJs.createRouteKey = createRouteKey;
/**
* @private
*/
var clone = require('clone');
/**
* Clone an object.
*
* @param objectToClone The object to clone.
* @returns A new clone of the original object.
*/
function cloneObject(objectToClone) {
return clone(objectToClone);
}
MakerJs.cloneObject = cloneObject;
/**
* Copy the properties from one object to another object.
*
* Example:
* ```
* makerjs.extendObject({ abc: 123 }, { xyz: 789 }); //returns { abc: 123, xyz: 789 }
* ```
*
* @param target The object to extend. It will receive the new properties.
* @param other An object containing properties to merge in.
* @returns The original object after merging.
*/
function extendObject(target, other) {
if (target && other) {
for (var key in other) {
if (typeof other[key] !== 'undefined') {
target[key] = other[key];
}
}
}
return target;
}
MakerJs.extendObject = extendObject;
/**
* Test to see if a variable is a function.
*
* @param value The object to test.
* @returns True if the object is a function type.
*/
function isFunction(value) {
return typeof value === 'function';
}
MakerJs.isFunction = isFunction;
/**
* Test to see if a variable is a number.
*
* @param value The object to test.
* @returns True if the object is a number type.
*/
function isNumber(value) {
return typeof value === 'number';
}
MakerJs.isNumber = isNumber;
/**
* Test to see if a variable is an object.
*
* @param value The object to test.
* @returns True if the object is an object type.
*/
function isObject(value) {
return typeof value === 'object';
}
MakerJs.isObject = isObject;
/**
* @private
*/
var x = {};
/**
* @private
*/
function reflectName(value) {
for (var prop in x) {
delete x[prop];
return prop;
}
}
/**
* @private
*/
function hasNamedProperty(p, value) {
var prop = reflectName();
return (prop in p);
}
/**
* Test to see if an object implements the required properties of a point.
*
* @param item The item to test.
*/
function isPoint(item) {
return (Array.isArray(item) && item.length == 2 && !isNaN(item[0]) && !isNaN(item[1]));
}
MakerJs.isPoint = isPoint;
/**
* Test to see if an object implements the required properties of a path.
*
* @param item The item to test.
*/
function isPath(item) {
return item && item.type && item.origin;
}
MakerJs.isPath = isPath;
/**
* Test to see if an object implements the required properties of a line.
*
* @param item The item to test.
*/
function isPathLine(item) {
return isPath(item) && item.type == MakerJs.pathType.Line && item.end;
}
MakerJs.isPathLine = isPathLine;
/**
* Test to see if an object implements the required properties of a circle.
*
* @param item The item to test.
*/
function isPathCircle(item) {
return isPath(item) && item.type == MakerJs.pathType.Circle && hasNamedProperty(item, x.radius = null);
}
MakerJs.isPathCircle = isPathCircle;
/**
* Test to see if an object implements the required properties of an arc.
*
* @param item The item to test.
*/
function isPathArc(item) {
return isPath(item) && item.type == MakerJs.pathType.Arc && hasNamedProperty(item, x.radius = null) && hasNamedProperty(item, x.startAngle = null) && hasNamedProperty(item, x.endAngle = null);
}
MakerJs.isPathArc = isPathArc;
/**
* Test to see if an object implements the required properties of an arc in a bezier curve.
*
* @param item The item to test.
*/
function isPathArcInBezierCurve(item) {
return isPathArc(item) && hasNamedProperty(item, x.bezierData = null);
}
MakerJs.isPathArcInBezierCurve = isPathArcInBezierCurve;
/**
* String-based enumeration of all paths types.
*
* Examples: use pathType instead of string literal when creating a circle.
* ```
* var circle: IPathCircle = { type: pathType.Circle, origin: [0, 0], radius: 7 }; //typescript
* var circle = { type: pathType.Circle, origin: [0, 0], radius: 7 }; //javascript
* ```
*/
MakerJs.pathType = {
Line: "line",
Circle: "circle",
Arc: "arc",
BezierSeed: "bezier-seed"
};
/**
* Test to see if an object implements the required properties of a model.
*/
function isModel(item) {
return item && (item.paths || item.models);
}
MakerJs.isModel = isModel;
})(MakerJs || (MakerJs = {}));
//CommonJs
module.exports = MakerJs;
var MakerJs;
(function (MakerJs) {
var angle;
(function (angle) {
/**
* Ensures an angle is not greater than 360
*
* @param angleInDegrees Angle in degrees.
* @retiurns Same polar angle but not greater than 360 degrees.
*/
function noRevolutions(angleInDegrees) {
var revolutions = Math.floor(angleInDegrees / 360);
var a = angleInDegrees - (360 * revolutions);
return a < 0 ? a + 360 : a;
}
angle.noRevolutions = noRevolutions;
/**
* Convert an angle from degrees to radians.
*
* @param angleInDegrees Angle in degrees.
* @returns Angle in radians.
*/
function toRadians(angleInDegrees) {
return noRevolutions(angleInDegrees) * Math.PI / 180.0;
}
angle.toRadians = toRadians;
/**
* Convert an angle from radians to degrees.
*
* @param angleInRadians Angle in radians.
* @returns Angle in degrees.
*/
function toDegrees(angleInRadians) {
return angleInRadians * 180.0 / Math.PI;
}
angle.toDegrees = toDegrees;
/**
* Get an arc's end angle, ensured to be greater than its start angle.
*
* @param arc An arc path object.
* @returns End angle of arc.
*/
function ofArcEnd(arc) {
//compensate for values past zero. This allows easy compute of total angle size.
//for example 0 = 360
if (arc.endAngle < arc.startAngle) {
return 360 + arc.endAngle;
}
return arc.endAngle;
}
angle.ofArcEnd = ofArcEnd;
/**
* Get the angle in the middle of an arc's start and end angles.
*
* @param arc An arc path object.
* @param ratio Optional number between 0 and 1 specifying percentage between start and end angles. Default is .5
* @returns Middle angle of arc.
*/
function ofArcMiddle(arc, ratio) {
if (ratio === void 0) { ratio = .5; }
return arc.startAngle + ofArcSpan(arc) * ratio;
}
angle.ofArcMiddle = ofArcMiddle;
/**
* Total angle of an arc between its start and end angles.
*
* @param arc The arc to measure.
* @returns Angle of arc.
*/
function ofArcSpan(arc) {
var endAngle = angle.ofArcEnd(arc);
var a = MakerJs.round(endAngle - arc.startAngle);
if (a > 360) {
return noRevolutions(a);
}
else {
return a;
}
}
angle.ofArcSpan = ofArcSpan;
/**
* Angle of a line path.
*
* @param line The line path to find the angle of.
* @returns Angle of the line path, in degrees.
*/
function ofLineInDegrees(line) {
return noRevolutions(toDegrees(ofPointInRadians(line.origin, line.end)));
}
angle.ofLineInDegrees = ofLineInDegrees;
/**
* Angle of a line through a point, in degrees.
*
* @param pointToFindAngle The point to find the angle.
* @param origin Point of origin of the angle.
* @returns Angle of the line throught the point, in degrees.
*/
function ofPointInDegrees(origin, pointToFindAngle) {
return toDegrees(ofPointInRadians(origin, pointToFindAngle));
}
angle.ofPointInDegrees = ofPointInDegrees;
/**
* Angle of a line through a point, in radians.
*
* @param pointToFindAngle The point to find the angle.
* @param origin Point of origin of the angle.
* @returns Angle of the line throught the point, in radians.
*/
function ofPointInRadians(origin, pointToFindAngle) {
var d = MakerJs.point.subtract(pointToFindAngle, origin);
var x = d[0];
var y = d[1];
return Math.atan2(-y, -x) + Math.PI;
}
angle.ofPointInRadians = ofPointInRadians;
/**
* Mirror an angle on either or both x and y axes.
*
* @param angleInDegrees The angle to mirror.
* @param mirrorX Boolean to mirror on the x axis.
* @param mirrorY Boolean to mirror on the y axis.
* @returns Mirrored angle.
*/
function mirror(angleInDegrees, mirrorX, mirrorY) {
if (mirrorY) {
angleInDegrees = 360 - angleInDegrees;
}
if (mirrorX) {
angleInDegrees = (angleInDegrees < 180 ? 180 : 540) - angleInDegrees;
}
return angleInDegrees;
}
angle.mirror = mirror;
})(angle = MakerJs.angle || (MakerJs.angle = {}));
})(MakerJs || (MakerJs = {}));
var MakerJs;
(function (MakerJs) {
var point;
(function (point) {
/**
* Add two points together and return the result as a new point object.
*
* @param a First point.
* @param b Second point.
* @param subtract Optional boolean to subtract instead of add.
* @returns A new point object.
*/
function add(a, b, subtract) {
var newPoint = clone(a);
if (!b)
return newPoint;
for (var i = 2; i--;) {
if (subtract) {
newPoint[i] -= b[i];
}
else {
newPoint[i] += b[i];
}
}
return newPoint;
}
point.add = add;
/**
* Get the average of two points.
*
* @param a First point.
* @param b Second point.
* @returns New point object which is the average of a and b.
*/
function average(a, b) {
function avg(i) {
return (a[i] + b[i]) / 2;
}
return [avg(0), avg(1)];
}
point.average = average;
/**
* Clone a point into a new point.
*
* @param pointToClone The point to clone.
* @returns A new point with same values as the original.
*/
function clone(pointToClone) {
if (!pointToClone)
return point.zero();
return [pointToClone[0], pointToClone[1]];
}
point.clone = clone;
/**
* From an array of points, find the closest point to a given reference point.
*
* @param referencePoint The reference point.
* @param pointOptions Array of points to choose from.
* @returns The first closest point from the pointOptions.
*/
function closest(referencePoint, pointOptions) {
var smallest = {
index: 0,
distance: -1
};
for (var i = 0; i < pointOptions.length; i++) {
var distance = MakerJs.measure.pointDistance(referencePoint, pointOptions[i]);
if (smallest.distance == -1 || distance < smallest.distance) {
smallest.distance = distance;
smallest.index = i;
}
}
return pointOptions[smallest.index];
}
point.closest = closest;
/**
* Get a point from its polar coordinates.
*
* @param angleInRadians The angle of the polar coordinate, in radians.
* @param radius The radius of the polar coordinate.
* @returns A new point object.
*/
function fromPolar(angleInRadians, radius) {
return [
radius * Math.cos(angleInRadians),
radius * Math.sin(angleInRadians)
];
}
point.fromPolar = fromPolar;
/**
* Get a point on a circle or arc path, at a given angle.
* @param angleInDegrees The angle at which you want to find the point, in degrees.
* @param circle A circle or arc.
* @returns A new point object.
*/
function fromAngleOnCircle(angleInDegrees, circle) {
return add(circle.origin, fromPolar(MakerJs.angle.toRadians(angleInDegrees), circle.radius));
}
point.fromAngleOnCircle = fromAngleOnCircle;
/**
* Get the two end points of an arc path.
*
* @param arc The arc path object.
* @returns Array with 2 elements: [0] is the point object corresponding to the start angle, [1] is the point object corresponding to the end angle.
*/
function fromArc(arc) {
return [fromAngleOnCircle(arc.startAngle, arc), fromAngleOnCircle(arc.endAngle, arc)];
}
point.fromArc = fromArc;
/**
* @private
*/
var pathEndsMap = {};
pathEndsMap[MakerJs.pathType.Arc] = function (arc) {
return point.fromArc(arc);
};
pathEndsMap[MakerJs.pathType.Line] = function (line) {
return [line.origin, line.end];
};
pathEndsMap[MakerJs.pathType.BezierSeed] = pathEndsMap[MakerJs.pathType.Line];
/**
* Get the two end points of a path.
*
* @param pathContext The path object.
* @returns Array with 2 elements: [0] is the point object corresponding to the origin, [1] is the point object corresponding to the end.
*/
function fromPathEnds(pathContext, pathOffset) {
var result = null;
var fn = pathEndsMap[pathContext.type];
if (fn) {
result = fn(pathContext);
if (pathOffset) {
result = result.map(function (p) { return add(p, pathOffset); });
}
}
return result;
}
point.fromPathEnds = fromPathEnds;
/**
* @private
*/
function verticalIntersectionPoint(verticalLine, nonVerticalSlope) {
var x = verticalLine.origin[0];
var y = nonVerticalSlope.slope * x + nonVerticalSlope.yIntercept;
return [x, y];
}
/**
* Calculates the intersection of slopes of two lines.
*
* @param lineA First line to use for slope.
* @param lineB Second line to use for slope.
* @param options Optional IPathIntersectionOptions.
* @returns point of intersection of the two slopes, or null if the slopes did not intersect.
*/
function fromSlopeIntersection(lineA, lineB, options) {
if (options === void 0) { options = {}; }
var slopeA = MakerJs.measure.lineSlope(lineA);
var slopeB = MakerJs.measure.lineSlope(lineB);
if (MakerJs.measure.isSlopeEqual(slopeA, slopeB)) {
//check for overlap
options.out_AreOverlapped = MakerJs.measure.isLineOverlapping(lineA, lineB, options.excludeTangents);
return null;
}
var pointOfIntersection;
if (!slopeA.hasSlope) {
pointOfIntersection = verticalIntersectionPoint(lineA, slopeB);
}
else if (!slopeB.hasSlope) {
pointOfIntersection = verticalIntersectionPoint(lineB, slopeA);
}
else {
// find intersection by line equation
var x = (slopeB.yIntercept - slopeA.yIntercept) / (slopeA.slope - slopeB.slope);
var y = slopeA.slope * x + slopeA.yIntercept;
pointOfIntersection = [x, y];
}
return pointOfIntersection;
}
point.fromSlopeIntersection = fromSlopeIntersection;
/**
* @private
*/
var middleMap = {};
middleMap[MakerJs.pathType.Arc] = function (arc, ratio) {
var midAngle = MakerJs.angle.ofArcMiddle(arc, ratio);
return point.add(arc.origin, point.fromPolar(MakerJs.angle.toRadians(midAngle), arc.radius));
};
middleMap[MakerJs.pathType.Circle] = function (circle, ratio) {
return point.add(circle.origin, [-circle.radius, 0]);
};
middleMap[MakerJs.pathType.Line] = function (line, ratio) {
function ration(a, b) {
return a + (b - a) * ratio;
}
;
return [
ration(line.origin[0], line.end[0]),
ration(line.origin[1], line.end[1])
];
};
middleMap[MakerJs.pathType.BezierSeed] = function (seed, ratio) {
return MakerJs.models.BezierCurve.computePoint(seed, ratio);
};
/**
* Get the middle point of a path.
*
* @param pathContext The path object.
* @param ratio Optional ratio (between 0 and 1) of point along the path. Default is .5 for middle.
* @returns Point on the path, in the middle of the path.
*/
function middle(pathContext, ratio) {
if (ratio === void 0) { ratio = .5; }
var midPoint = null;
var fn = middleMap[pathContext.type];
if (fn) {
midPoint = fn(pathContext, ratio);
}
return midPoint;
}
point.middle = middle;
/**
* Create a clone of a point, mirrored on either or both x and y axes.
*
* @param pointToMirror The point to mirror.
* @param mirrorX Boolean to mirror on the x axis.
* @param mirrorY Boolean to mirror on the y axis.
* @returns Mirrored point.
*/
function mirror(pointToMirror, mirrorX, mirrorY) {
var p = clone(pointToMirror);
if (mirrorX) {
p[0] = -p[0];
}
if (mirrorY) {
p[1] = -p[1];
}
return p;
}
point.mirror = mirror;
/**
* Round the values of a point.
*
* @param pointContext The point to serialize.
* @param accuracy Optional exemplar number of decimal places.
* @returns A new point with the values rounded.
*/
function rounded(pointContext, accuracy) {
return [MakerJs.round(pointContext[0], accuracy), MakerJs.round(pointContext[1], accuracy)];
}
point.rounded = rounded;
/**
* Rotate a point.
*
* @param pointToRotate The point to rotate.
* @param angleInDegrees The amount of rotation, in degrees.
* @param rotationOrigin The center point of rotation.
* @returns A new point.
*/
function rotate(pointToRotate, angleInDegrees, rotationOrigin) {
if (rotationOrigin === void 0) { rotationOrigin = [0, 0]; }
var pointAngleInRadians = MakerJs.angle.ofPointInRadians(rotationOrigin, pointToRotate);
var d = MakerJs.measure.pointDistance(rotationOrigin, pointToRotate);
var rotatedPoint = fromPolar(pointAngleInRadians + MakerJs.angle.toRadians(angleInDegrees), d);
return add(rotationOrigin, rotatedPoint);
}
point.rotate = rotate;
/**
* Scale a point's coordinates.
*
* @param pointToScale The point to scale.
* @param scaleValue The amount of scaling.
* @returns A new point.
*/
function scale(pointToScale, scaleValue) {
var p = clone(pointToScale);
for (var i = 2; i--;) {
p[i] *= scaleValue;
}
return p;
}
point.scale = scale;
/**
* Distort a point's coordinates.
*
* @param pointToDistort The point to distort.
* @param scaleX The amount of x scaling.
* @param scaleY The amount of y scaling.
* @returns A new point.
*/
function distort(pointToDistort, scaleX, scaleY) {
return [pointToDistort[0] * scaleX, pointToDistort[1] * scaleY];
}
point.distort = distort;
/**
* Subtract a point from another point, and return the result as a new point. Shortcut to Add(a, b, subtract = true).
*
* @param a First point.
* @param b Second point.
* @returns A new point object.
*/
function subtract(a, b) {
return add(a, b, true);
}
point.subtract = subtract;
/**
* A point at 0,0 coordinates.
* NOTE: It is important to call this as a method, with the empty parentheses.
*
* @returns A new point.
*/
function zero() {
return [0, 0];
}
point.zero = zero;
})(point = MakerJs.point || (MakerJs.point = {}));
})(MakerJs || (MakerJs = {}));
var MakerJs;
(function (MakerJs) {
var path;
(function (path) {
/**
* @private
*/
function copyLayer(pathA, pathB) {
if (pathA && pathB && ('layer' in pathA)) {
pathB.layer = pathA.layer;
}
}
/**
* Create a clone of a path. This is faster than cloneObject.
*
* @param pathToClone The path to clone.
* @returns Cloned path.
*/
function clone(pathToClone) {
var result = null;
switch (pathToClone.type) {
case MakerJs.pathType.Arc:
var arc = pathToClone;
result = new MakerJs.paths.Arc(MakerJs.point.clone(arc.origin), arc.radius, arc.startAngle, arc.endAngle);
//carry extra props if this is an IPathArcInBezierCurve
if (MakerJs.isPathArcInBezierCurve(arc)) {
result.bezierData = arc.bezierData;
}
break;
case MakerJs.pathType.Circle:
var circle = pathToClone;
result = new MakerJs.paths.Circle(MakerJs.point.clone(circle.origin), circle.radius);
break;
case MakerJs.pathType.Line:
var line = pathToClone;
result = new MakerJs.paths.Line(MakerJs.point.clone(line.origin), MakerJs.point.clone(line.end));
break;
}
copyLayer(pathToClone, result);
return result;
}
path.clone = clone;
/**
* @private
*/
var mirrorMap = {};
mirrorMap[MakerJs.pathType.Line] = function (line, origin, mirrorX, mirrorY) {
return new MakerJs.paths.Line(origin, MakerJs.point.mirror(line.end, mirrorX, mirrorY));
};
mirrorMap[MakerJs.pathType.Circle] = function (circle, origin, mirrorX, mirrorY) {
return new MakerJs.paths.Circle(origin, circle.radius);
};
mirrorMap[MakerJs.pathType.Arc] = function (arc, origin, mirrorX, mirrorY) {
var startAngle = MakerJs.angle.mirror(arc.startAngle, mirrorX, mirrorY);
var endAngle = MakerJs.angle.mirror(MakerJs.angle.ofArcEnd(arc), mirrorX, mirrorY);
var xor = mirrorX != mirrorY;
return new MakerJs.paths.Arc(origin, arc.radius, xor ? endAngle : startAngle, xor ? startAngle : endAngle);
};
mirrorMap[MakerJs.pathType.BezierSeed] = function (seed, origin, mirrorX, mirrorY) {
var mirrored = mirrorMap[MakerJs.pathType.Line](seed, origin, mirrorX, mirrorY);
mirrored.type = MakerJs.pathType.BezierSeed;
mirrored.controls = seed.controls.map(function (c) { return MakerJs.point.mirror(c, mirrorX, mirrorY); });
return mirrored;
};
/**
* Create a clone of a path, mirrored on either or both x and y axes.
*
* @param pathToMirror The path to mirror.
* @param mirrorX Boolean to mirror on the x axis.
* @param mirrorY Boolean to mirror on the y axis.
* @returns Mirrored path.
*/
function mirror(pathToMirror, mirrorX, mirrorY) {
var newPath = null;
if (pathToMirror) {
var origin = MakerJs.point.mirror(pathToMirror.origin, mirrorX, mirrorY);
var fn = mirrorMap[pathToMirror.type];
if (fn) {
newPath = fn(pathToMirror, origin, mirrorX, mirrorY);
}
}
copyLayer(pathToMirror, newPath);
return newPath;
}
path.mirror = mirror;
/**
* @private
*/
var moveMap = {};
moveMap[MakerJs.pathType.Line] = function (line, origin) {
var delta = MakerJs.point.subtract(line.end, line.origin);
line.end = MakerJs.point.add(origin, delta);
};
/**
* Move a path to an absolute point.
*
* @param pathToMove The path to move.
* @param origin The new origin for the path.
* @returns The original path (for chaining).
*/
function move(pathToMove, origin) {
if (pathToMove) {
var fn = moveMap[pathToMove.type];
if (fn) {
fn(pathToMove, origin);
}
pathToMove.origin = origin;
}
return pathToMove;
}
path.move = move;
/**
* @private
*/
var moveRelativeMap = {};
moveRelativeMap[MakerJs.pathType.Line] = function (line, delta, subtract) {
line.end = MakerJs.point.add(line.end, delta, subtract);
};
moveRelativeMap[MakerJs.pathType.BezierSeed] = function (seed, delta, subtract) {
moveRelativeMap[MakerJs.pathType.Line](seed, delta, subtract);
seed.controls = seed.controls.map(function (c) { return MakerJs.point.add(c, delta, subtract); });
};
/**
* Move a path's origin by a relative amount.
*
* @param pathToMove The path to move.
* @param delta The x & y adjustments as a point object.
* @param subtract Optional boolean to subtract instead of add.
* @returns The original path (for chaining).
*/
function moveRelative(pathToMove, delta, subtract) {
if (pathToMove && delta) {
pathToMove.origin = MakerJs.point.add(pathToMove.origin, delta, subtract);
var fn = moveRelativeMap[pathToMove.type];
if (fn) {
fn(pathToMove, delta, subtract);
}
}
return pathToMove;
}
path.moveRelative = moveRelative;
/**
* Move some paths relatively during a task execution, then unmove them.
*
* @param pathsToMove The paths to move.
* @param deltas The x & y adjustments as a point object array.
* @param task The function to call while the paths are temporarily moved.
*/
function moveTemporary(pathsToMove, deltas, task) {
var subtract = false;
function move(pathToOffset, i) {
if (deltas[i]) {
moveRelative(pathToOffset, deltas[i], subtract);
}
}
pathsToMove.map(move);
task();
subtract = true;
pathsToMove.map(move);
}
path.moveTemporary = moveTemporary;
/**
* @private
*/
var rotateMap = {};
rotateMap[MakerJs.pathType.Line] = function (line, angleInDegrees, rotationOrigin) {
line.end = MakerJs.point.rotate(line.end, angleInDegrees, rotationOrigin);
};
rotateMap[MakerJs.pathType.Arc] = function (arc, angleInDegrees, rotationOrigin) {
arc.startAngle = MakerJs.angle.noRevolutions(arc.startAngle + angleInDegrees);
arc.endAngle = MakerJs.angle.noRevolutions(arc.endAngle + angleInDegrees);
};
rotateMap[MakerJs.pathType.BezierSeed] = function (seed, angleInDegrees, rotationOrigin) {
rotateMap[MakerJs.pathType.Line](seed, angleInDegrees, rotationOrigin);
seed.controls = seed.controls.map(function (c) { return MakerJs.point.rotate(c, angleInDegrees, rotationOrigin); });
};
/**
* Rotate a path.
*
* @param pathToRotate The path to rotate.
* @param angleInDegrees The amount of rotation, in degrees.
* @param rotationOrigin The center point of rotation.
* @returns The original path (for chaining).
*/
function rotate(pathToRotate, angleInDegrees, rotationOrigin) {
if (rotationOrigin === void 0) { rotationOrigin = [0, 0]; }
if (!pathToRotate || angleInDegrees == 0)
return pathToRotate;
pathToRotate.origin = MakerJs.point.rotate(pathToRotate.origin, angleInDegrees, rotationOrigin);
var fn = rotateMap[pathToRotate.type];
if (fn) {
fn(pathToRotate, angleInDegrees, rotationOrigin);
}
return pathToRotate;
}
path.rotate = rotate;
/**
* @private
*/
var scaleMap = {};
scaleMap[MakerJs.pathType.Line] = function (line, scaleValue) {
line.end = MakerJs.point.scale(line.end, scaleValue);
};
scaleMap[MakerJs.pathType.BezierSeed] = function (seed, scaleValue) {
scaleMap[MakerJs.pathType.Line](seed, scaleValue);
seed.controls = seed.controls.map(function (c) { return MakerJs.point.scale(c, scaleValue); });
};
scaleMap[MakerJs.pathType.Circle] = function (circle, scaleValue) {
circle.radius *= scaleValue;
};
scaleMap[MakerJs.pathType.Arc] = scaleMap[MakerJs.pathType.Circle];
/**
* Scale a path.
*
* @param pathToScale The path to scale.
* @param scaleValue The amount of scaling.
* @returns The original path (for chaining).
*/
function scale(pathToScale, scaleValue) {
if (!pathToScale || scaleValue == 1)
return pathToScale;
pathToScale.origin = MakerJs.point.scale(pathToScale.origin, scaleValue);
var fn = scaleMap[pathToScale.type];
if (fn) {
fn(pathToScale, scaleValue);
}
return pathToScale;
}
path.scale = scale;
/**
* @private
*/
var distortMap = {};
distortMap[MakerJs.pathType.Arc] = function (arc, scaleX, scaleY) {
return new MakerJs.models.EllipticArc(arc, scaleX, scaleY);
};
distortMap[MakerJs.pathType.Circle] = function (circle, scaleX, scaleY) {
var ellipse = new MakerJs.models.Ellipse(circle.radius * scaleX, circle.radius * scaleY);
ellipse.origin = MakerJs.point.distort(circle.origin, scaleX, scaleY);
return ellipse;
};
distortMap[MakerJs.pathType.Line] = function (line, scaleX, scaleY) {
return new MakerJs.paths.Line([line.origin, line.end].map(function (p) { return MakerJs.point.distort(p, scaleX, scaleY); }));
};
distortMap[MakerJs.pathType.BezierSeed] = function (seed, scaleX, scaleY) {
var d = MakerJs.point.distort;
return {
type: MakerJs.pathType.BezierSeed,
origin: d(seed.origin, scaleX, scaleY),
controls: seed.controls.map(function (c) { return d(c, scaleX, scaleY); }),
end: d(seed.end, scaleX, scaleY)
};
};
/**
* Distort a path - scale x and y individually.