forked from qcad/qcad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShapeAlgorithms.js
More file actions
2162 lines (1831 loc) · 65.3 KB
/
Copy pathShapeAlgorithms.js
File metadata and controls
2162 lines (1831 loc) · 65.3 KB
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
/**
* Copyright (c) 2011-2017 by Andrew Mustun. All rights reserved.
*
* This file is part of the QCAD project.
*
* QCAD is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QCAD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QCAD.
*/
/**
* \class ShapeAlgorithms
* Various shape based algorithms.
*/
function ShapeAlgorithms() {
}
/**
* \return Array with only the circle shapes from the given shapes.
*/
ShapeAlgorithms.getCircleShapes = function(shapes) {
if (isNull(shapes)) {
return undefined;
}
var ret = [];
for (var i=0; i<shapes.length; i++) {
if (isCircleShape(shapes[i])) {
ret.push(shapes[i]);
}
}
return ret;
};
/**
* \return The shape of the given shapes that is closest to the given position.
*/
ShapeAlgorithms.getClosestShape = function(shapes, position) {
if (isNull(shapes)) {
return undefined;
}
var ret = undefined;
var minDist = undefined;
var circle = undefined;
for (var i=0; i<shapes.length; i++) {
var s = shapes[i];
var dist = s.getDistanceTo(position);
if (isNumber(dist) && (isNull(minDist) || dist<minDist)) {
minDist = dist;
ret = s;
}
}
return ret;
};
ShapeAlgorithms.getTangents = function(circle1, circle2) {
var offs1, offs2;
var circleCenter1 = circle1.getCenter();
var circleRadius1 = circle1.getRadius();
var circleCenter2 = circle2.getCenter();
var circleRadius2 = circle2.getRadius();
// create all four possible tangents:
var tangents = [];
var angle1 = circleCenter1.getAngleTo(circleCenter2);
var dist1 = circleCenter1.getDistanceTo(circleCenter2);
if (dist1<1.0e-6) {
return [];
}
// outer tangents:
var dist2 = circleRadius2 - circleRadius1;
if (dist1>dist2) {
var angle2 = Math.asin(dist2/dist1);
var angt1 = angle1 + angle2 + Math.PI/2.0;
var angt2 = angle1 - angle2 - Math.PI/2.0;
offs1 = new RVector();
offs2 = new RVector();
offs1.setPolar(circleRadius1, angt1);
offs2.setPolar(circleRadius2, angt1);
tangents.push(new RLine(circleCenter1.operator_add(offs1),
circleCenter2.operator_add(offs2)));
offs1.setPolar(circleRadius1, angt2);
offs2.setPolar(circleRadius2, angt2);
tangents.push(new RLine(circleCenter1.operator_add(offs1),
circleCenter2.operator_add(offs2)));
}
else {
tangents.push(undefined);
tangents.push(undefined);
}
// inner tangents:
var dist3 = circleRadius2 + circleRadius1;
if (dist1>dist3) {
var angle3 = Math.asin(dist3/dist1);
var angt3 = angle1 + angle3 + Math.PI/2.0;
var angt4 = angle1 - angle3 - Math.PI/2.0;
offs1 = new RVector();
offs2 = new RVector();
offs1.setPolar(circleRadius1, angt3);
offs2.setPolar(circleRadius2, angt3);
tangents.push(new RLine(circleCenter1.operator_subtract(offs1),
circleCenter2.operator_add(offs2)));
offs1.setPolar(circleRadius1, angt4);
offs2.setPolar(circleRadius2, angt4);
tangents.push(new RLine(circleCenter1.operator_subtract(offs1),
circleCenter2.operator_add(offs2)));
}
else {
tangents.push(undefined);
tangents.push(undefined);
}
return tangents;
};
/**
* \return Line that is orthogonal to line and tangential to circle.
*/
ShapeAlgorithms.getOrthogonalTangents = function(line, circle) {
var ret = [];
var auxLine1, auxLine2;
var ips, ips1, ips2;
var lineAngle = line.getAngle();
if (isCircleShape(circle) || isArcShape(circle)) {
// line parallel to line through center of circle:
auxLine1 = new RLine(circle.getCenter(), lineAngle, 100.0);
// intersections of parallel with circle:
ips1 = circle.getIntersectionPoints(auxLine1, false);
for (var i=0; i<ips1.length; i++) {
// candidate:
auxLine2 = new RLine(ips1[i], lineAngle+Math.PI/2, 100.0);
ips2 = line.getIntersectionPoints(auxLine2, false);
if (ips2.length===1) {
ret.push(new RLine(ips1[i], ips2[0]));
}
}
}
else if (isEllipseShape(circle)) {
var center = circle.getCenter();
// circle around ellipse:
var auxCircle = new RCircle(center, circle.getMajorRadius());
var foci = circle.getFoci();
auxLine1 = new RLine(foci[0], lineAngle, 100.0);
auxLine2 = new RLine(foci[1], lineAngle, 100.0);
ips1 = auxLine1.getIntersectionPoints(auxCircle, false);
ips2 = auxLine2.getIntersectionPoints(auxCircle, false);
var pointOfContact1 = undefined;
var pointOfContact2 = undefined;
if (ips1.length>=1 && ips2.length>=1) {
if (ips1[0].equalsFuzzy(ips2[0])) {
pointOfContact1 = ips1[0];
}
else {
auxLine1 = new RLine(ips1[0], ips2[0]);
ips = circle.getIntersectionPoints(auxLine1, false);
if (ips.length>=1) {
pointOfContact1 = ips[0];
}
}
}
if (ips1.length>=2 && ips2.length>=2) {
if (ips1[1].equalsFuzzy(ips2[1])) {
pointOfContact2 = ips1[1];
}
else {
auxLine2 = new RLine(ips1[1], ips2[1]);
ips = circle.getIntersectionPoints(auxLine2, false);
if (ips.length>=1) {
pointOfContact2 = ips[0];
}
}
}
if (!isNull(pointOfContact1)) {
var pointOnLine1 = line.getClosestPointOnShape(pointOfContact1, false);
ret.push(new RLine(pointOfContact1, pointOnLine1));
}
if (!isNull(pointOfContact2)) {
var pointOnLine2 = line.getClosestPointOnShape(pointOfContact2, false);
ret.push(new RLine(pointOfContact2, pointOnLine2));
}
}
return ret;
};
/**
* \return Parallels to this shape.
* \param distance Distance of first parallel or concentric arc or circle.
* \param number Number of offset shapes to generate.
* \param sidePosition RVector indicating what side of the shape the parallels
* should be RS.LeftHand or RS.RightHand or RS.BothSides.
*/
ShapeAlgorithms.getOffsetShapes = function(shape, distance, number, sidePosition) {
var side = isVector(sidePosition) ? RS.NoSide : sidePosition;
var pos = isVector(sidePosition) ? sidePosition : RVector.invalid;
return shape.getOffsetShapes(distance, number, side, pos);
};
ShapeAlgorithms.getOffsetLines = function(shape, distance, number, sidePosition) {
var side = isVector(sidePosition) ? RS.NoSide : sidePosition;
var pos = isVector(sidePosition) ? sidePosition : RVector.invalid;
return RShape.getOffsetLines(shape, distance, number, side, pos);
};
ShapeAlgorithms.getOffsetArcs = function(shape, distance, number, sidePosition) {
var side = isVector(sidePosition) ? RS.NoSide : sidePosition;
var pos = isVector(sidePosition) ? sidePosition : RVector.invalid;
return RShape.getOffsetArcs(shape, distance, number, side, pos);
};
/**
* \return Array of spline shapes representing the parallel curves to the given ellipse shape.
*/
ShapeAlgorithms.getOffsetEllipses = function(shape, distance, number, sidePosition) {
var side = isVector(sidePosition) ? RS.NoSide : sidePosition;
var pos = isVector(sidePosition) ? sidePosition : RVector.invalid;
return RShape.getOffsetEllipses(shape, distance, number, side, pos);
};
/**
* \return Intersection points between shape and other shapes.
*/
ShapeAlgorithms.getIntersectionPoints = function(shape, otherShapes, onShape, onOtherShapes) {
var intersections = [];
var i, k;
// treat start and end points as intersection points for open shapes:
if (onShape &&
!isCircleShape(shape) &&
!isFullEllipseShape(shape) &&
!isXLineShape(shape) &&
(!isPolylineShape(shape) || !shape.isGeometricallyClosed()) &&
(!isSplineShape(shape) || !shape.isClosed())) {
var sp = shape.getStartPoint();
sp.isStart = true;
intersections.push(sp);
if (!isRayShape(shape)) {
var ep = shape.getEndPoint()
ep.isEnd = true;
intersections.push(ep);
}
}
// find all intersection points:
for (i=0; i<otherShapes.length; i++) {
var otherShape = otherShapes[i];
if (isFunction(otherShape.data)) {
otherShape = otherShape.data();
}
var sol = shape.getIntersectionPoints(otherShape, onShape, false, true);
for (k=0; k<sol.length; k++) {
if (!onOtherShapes || otherShape.isOnShape(sol[k])) {
intersections.push(sol[k]);
}
}
}
var selfIntersectionPoints = shape.getSelfIntersectionPoints();
// add self intersection points to list:
if (selfIntersectionPoints.length!==0) {
intersections = intersections.concat(selfIntersectionPoints);
}
return intersections;
};
/**
* \return Array of shapes to extend or trim to.
*
* \param doc RDocument
* \param entityId ID of entity to exclude (typically clicked entity).
* \param shape Shape of (clicked) entity.
* \param extend True if entity is being extended.
*/
ShapeAlgorithms.getIntersectingShapes = function(doc, entityId, shape, extend) {
if (isNull(extend)) {
extend = false;
}
if (isNull(shape)) {
return [];
}
// find other shapes that potentially intersect with the chosen entity:
var ret = [];
// allow for error: especialy for ellipse segments bordering to tangential lines this is needed:
var otherEntityIds;
if (extend===true) {
// TODO: if we are extending, the 'rest' has to be queried instead
//otherEntityIds = document.queryIntersectedEntitiesXY(document.getBoundingBox().growXY(1.0e-2), true);
otherEntityIds = doc.queryAllVisibleEntities();
}
else {
if (isXLineShape(shape) || isRayShape(shape)) {
otherEntityIds = doc.queryAllEntities();
}
else {
otherEntityIds = doc.queryIntersectedEntitiesXY(shape.getBoundingBox().growXY(1.0e-2));
}
}
for (var i=0; i<otherEntityIds.length; i++) {
var otherEntity = doc.queryEntityDirect(otherEntityIds[i]);
// ignore intersection points of same entity
// self intersection points are handled elsewhere
var same = otherEntityIds[i]===entityId;
if (same && !isBlockReferenceEntity(otherEntity)) {
continue;
}
// TODO: if shape is arc, circle, ellipse or ellipse arc:
// entities with full bounding box outside full circle or full ellipse
// bounding box could be ignored.
var s = otherEntity.getShapes();
if (s.length!==0) {
if (!same) {
ret = ret.concat(s);
}
else {
// ignore same shape for block reference entities:
for (var k=0; k<s.length; k++) {
if (!shape.equals(s[k].data())) {
ret.push(s[k]);
}
}
}
}
}
return ret;
};
/**
* Breaks the closest segment in shape to position between two intersections
* with otherShapes or
* extends a shape to the next two (imaginary) intersections with otherShapes.
*
* \param extend True: extending instead of breaking out.
*
* \return Array of three new shapes which each might be undefined if its
* length would otherwise be 0.
* The first shape is the rest at the start of the shape.
* The second shape is the rest at the end of the shape.
* The third shape is the segment self in its new shape.
*/
ShapeAlgorithms.autoSplit = function(shape, otherShapes, position, extend) {
if (isNull(extend)) {
extend = false;
}
// get intersection points:
var ips = ShapeAlgorithms.getIntersectionPoints(shape, otherShapes, !extend, extend);
if (ips.length===0) {
// no intersections with other shapes or self,
// return whole shape as segment:
return [undefined, undefined, shape.clone()];
}
// convert circle to arc:
if (isCircleShape(shape)) {
var ap = shape.getCenter().getAngleTo(position);
var arc = new RArc(shape.getCenter(), shape.getRadius(), ap, ap, false);
var maxD = undefined;
var p = undefined;
for (var i=0; i<ips.length; i++) {
var ip = ips[i];
var d = arc.getDistanceFromStart(ip);
if (isNull(maxD) || d>maxD) {
maxD = d;
p = ip;
}
}
// no intersections:
if (isNull(p)) {
return [undefined, undefined, shape.clone()];
}
// angle at intersection point closest to end of arc is where we split the circle:
ap = shape.getCenter().getAngleTo(p);
shape = new RArc(shape.getCenter(), shape.getRadius(), ap, ap, false);
}
// find intersection points closest to position:
var cutDistances = ShapeAlgorithms.getClosestIntersectionPointDistances(shape, ips, position);
// distance along shape to clicked position:
//var dPosition = ;
// make sure direction of shape does not change in the process:
//intersectionPointDistances.sort();
var cutDist1 = undefined;
var cutDist2 = undefined;
var cutPos1 = undefined;
var cutPos2 = undefined;
if (!isNull(cutDistances) && cutDistances.length>1) {
cutDist1 = cutDistances[0][0];
cutDist2 = cutDistances[0][1];
cutPos1 = cutDistances[1][0];
cutPos2 = cutDistances[1][1];
}
// if we only have one cutting point (XLine, Ray), make it the first parameter:
if (isNull(cutDist1)) {
cutDist1 = cutDist2;
cutPos1 = cutPos2;
cutDist2 = undefined;
cutPos2 = undefined;
}
return ShapeAlgorithms.autoSplitManual(shape, cutDist1, cutDist2, cutPos1, cutPos2, position, extend);
};
ShapeAlgorithms.autoSplitManual = function(shape, cutDist1, cutDist2, cutPos1, cutPos2, position, extend) {
if (isNull(extend)) {
extend = false;
}
// if (!isCircleShape(shape) && !isFullEllipseShape(shape) &&
// !isXLineShape(shape) && !isRayShape(shape)) {
// if (isNull(cutDist1) || isNull(cutDist2)) {
// // abort if shape requires two intersection points:
// return undefined;
// }
// }
// if (isNull(cutDist2)) {
// cutDist2 = cutDist1;
// }
var dummy;
var distSegment;
// var cutPos1 = shape.getPointWithDistanceToStart(cutDist1);
// var cutPos2 = undefined;
// if (!isNull(cutDist2)) {
// cutPos2 = shape.getPointWithDistanceToStart(cutDist2);
// }
if (isNull(cutDist1) && !isNull(cutPos1)) {
cutDist1 = shape.getDistanceFromStart(cutPos1);
}
if (isNull(cutDist2) && !isNull(cutPos2)) {
cutDist2 = shape.getDistanceFromStart(cutPos2);
}
if (RMath.fuzzyCompare(cutDist1, 0.0) && shape.getStartPoint().equalsFuzzy(cutPos1) &&
RMath.fuzzyCompare(cutDist2, shape.getLength()) && shape.getEndPoint().equalsFuzzy(cutPos2)) {
return [undefined, undefined, shape.clone()];
}
var rest1 = undefined;
var rest2 = undefined;
var segment = undefined;
// lines:
if (isLineShape(shape)) {
rest1 = shape.clone();
rest2 = shape.clone();
if (cutDist1 < cutDist2) {
rest1.trimEndPoint(cutDist1);
rest2.trimStartPoint(cutDist2);
}
else {
rest1.trimEndPoint(cutDist2);
rest2.trimStartPoint(cutDist1);
}
segment = shape.clone();
segment.setStartPoint(cutPos1);
segment.setEndPoint(cutPos2);
if (rest1.getLength()<RS.PointTolerance) {
rest1 = undefined;
}
if (rest2.getLength()<RS.PointTolerance) {
rest2 = undefined;
}
}
// xlines:
else if (isXLineShape(shape)) {
var line = shape.getLineShape();
cutPos1 = line.getPointWithDistanceToStart(cutDist1);
if (isNull(cutDist2)) {
cutPos2 = undefined;
}
else {
cutPos2 = line.getPointWithDistanceToStart(cutDist2);
}
rest1 = undefined;
rest2 = undefined;
if (!isNull(cutDist1) && !isNull(cutDist2) && cutDist1 > cutDist2) {
dummy = cutDist1;
cutDist1 = cutDist2;
cutDist2 = dummy;
}
// if (!isNull(cutDist1) && !isNull(cutDist2)) {
// cutDist1 = cutDist2;
// cutDist2 = undefined;
// }
// <--------x---------------x--------->
// rest2 cp2 segment cp1 rest1
if (!isNull(cutDist1) && !isNull(cutDist2)) {
rest1 = new RRay(cutPos1, RVector.createPolar(1.0, shape.getDirection2()));
segment = new RLine(cutPos1, cutPos2);
rest2 = new RRay(cutPos2, RVector.createPolar(1.0, shape.getDirection1()));
}
// <-o--------------x----------------->
// pos segment cp1 rest1
// <----------------x-------------o--->
// rest1 cp1 segment pos
else if (!isNull(cutDist1)) {
rest1 = new RRay(cutPos1, RVector.createPolar(1.0, shape.getDirection2()));
segment = new RRay(cutPos1, RVector.createPolar(1.0, shape.getDirection1()));
distSegment = segment.getDistanceTo(position);
if (isNaN(distSegment)) {
dummy = rest1;
rest1 = segment;
segment = dummy;
}
rest2 = undefined;
}
}
// rays:
else if (isRayShape(shape)) {
rest1 = undefined;
rest2 = undefined;
if (!isNull(cutDist1) && !isNull(cutDist2) && Math.sign(cutDist1) !== Math.sign(cutDist2)) {
dummy = cutDist1;
cutDist1 = cutDist2;
cutDist2 = dummy;
}
// if (!cutDist1.isValid() && cutDist2.isValid()) {
// cutDist1 = cutDist2;
// cutDist2 = undefined;
// }
// <--------x-------o-------x---------
// rest2 cp2 segment cp1 rest1
if (isValidVector(cutPos1) && isValidVector(cutPos2)) {
rest1 = new RLine(shape.getBasePoint(), cutPos1);
segment = new RLine(cutPos1, cutPos2);
rest2 = new RRay(cutPos2, RVector.createPolar(1.0, shape.getDirection1()));
}
// <-------o--------x-----------------
// segment cp1 rest1
// <----------------x--------o--------
// rest1 cp1 segment
else if (isValidVector(cutPos1)) {
rest1 = new RLine(shape.getBasePoint(), cutPos1);
segment = new RRay(cutPos1, RVector.createPolar(1.0, shape.getDirection1()));
rest2 = undefined;
distSegment = segment.getDistanceTo(position);
if (isNaN(distSegment)) {
dummy = rest1;
rest1 = segment;
segment = dummy;
}
}
}
// arcs:
else if (isArcShape(shape)) {
rest1 = shape.clone();
rest2 = shape.clone();
rest1.trimEndPoint(cutDist1);
rest2.trimStartPoint(cutDist2);
segment = shape.clone();
//var l1 = segment.getLength();
segment.setStartAngle(segment.getCenter().getAngleTo(cutPos1));
//segment.trimStartPoint(cutDist1);
//var l2 = segment.getLength();
//segment.trimEndPoint(cutDist2 - (l1-l2));
segment.setEndAngle(segment.getCenter().getAngleTo(cutPos2));
if (!extend) {
var angleLength1 = rest1.getAngleLength(true);
var angleLength2 = rest2.getAngleLength(true);
if (angleLength1+angleLength2 > shape.getAngleLength()) {
rest1.trimEndPoint(cutDist2);
rest2.trimStartPoint(cutDist1);
segment.trimStartPoint(cutDist2);
segment.trimEndPoint(cutDist1);
angleLength1 = rest1.getAngleLength(true);
angleLength2 = rest2.getAngleLength(true);
}
if (angleLength1<1.0e-5) {
rest1 = undefined;
}
if (angleLength2<1.0e-5) {
rest2 = undefined;
}
}
}
// circles:
else if (isCircleShape(shape)) {
if (isNull(cutDist1) || isNull(cutDist2)) {
rest1 = undefined;
rest2 = undefined;
}
else {
var angle1 = shape.getCenter().getAngleTo(cutPos1);
var angle2 = shape.getCenter().getAngleTo(cutPos2);
rest1 = new RArc(
shape.getCenter(),
shape.getRadius(),
angle1, angle2,
false);
rest2 = undefined;
segment = new RArc(
shape.getCenter(),
shape.getRadius(),
angle2, angle1,
false);
if (!isNull(position)) {
var cursorAngle = shape.getCenter().getAngleTo(position);
if (RMath.isAngleBetween(cursorAngle, angle1, angle2, false)) {
rest1.setStartAngle(angle2);
rest1.setEndAngle(angle1);
segment.setStartAngle(angle1);
segment.setEndAngle(angle2);
}
}
var angleLength1 = rest1.getAngleLength(true);
if (angleLength1<RS.AngleTolerance) {
rest1 = undefined;
}
}
}
// ellipse arcs:
else if (isEllipseArcShape(shape)) {
rest1 = shape.clone();
rest2 = shape.clone();
rest1.trimEndPoint(cutPos1, cutPos1);
rest2.trimStartPoint(cutPos2, cutPos2);
segment = shape.clone();
segment.trimStartPoint(cutPos1, cutPos1);
segment.trimEndPoint(cutPos2, cutPos2);
var angleLength1 = rest1.getAngleLength(true);
var angleLength2 = rest2.getAngleLength(true);
if (angleLength1+angleLength2 > shape.getAngleLength()) {
rest1.trimEndPoint(cutPos2, cutPos2);
rest2.trimStartPoint(cutPos1, cutPos1);
segment.trimStartPoint(cutPos2, cutPos2);
segment.trimEndPoint(cutPos1, cutPos1);
angleLength1 = rest1.getAngleLength(true);
angleLength2 = rest2.getAngleLength(true);
}
if (angleLength1<1.0e-5) {
rest1 = undefined;
}
if (angleLength2<1.0e-5) {
rest2 = undefined;
}
}
// full ellipses:
else if (isFullEllipseShape(shape)) {
if (!isValidVector(cutPos1) || !isValidVector(cutPos2)) {
rest1 = undefined;
rest2 = undefined;
}
else {
var angle1 = shape.getParamTo(cutPos1);
var angle2 = shape.getParamTo(cutPos2);
rest1 = new REllipse(
shape.getCenter(),
shape.getMajorPoint(),
shape.getRatio(),
angle1, angle2,
false);
rest2 = undefined;
segment = new REllipse(
shape.getCenter(),
shape.getMajorPoint(),
shape.getRatio(),
angle2, angle1,
false);
if (!isNull(position)) {
var cursorAngle = shape.getParamTo(position);
if (RMath.isAngleBetween(cursorAngle, angle1, angle2, false)) {
rest1.setStartParam(angle2);
rest1.setEndParam(angle1);
segment.setStartParam(angle1);
segment.setEndParam(angle2);
}
}
var angleLength1 = rest1.getAngleLength();
if (angleLength1<RS.AngleTolerance) {
rest1 = undefined;
}
}
}
// polyline:
else if (isPolylineShape(shape)) {
var closed = shape.isGeometricallyClosed();
if (closed) {
shape.relocateStartPoint(cutDist1);
shape.convertToOpen();
cutDist2 -= cutDist1;
if (cutDist2<0.0) {
cutDist2 = shape.getLength() + cutDist2;
}
cutDist1 = 0.0;
}
rest1 = shape.clone();
rest2 = shape.clone();
segment = shape.clone();
if (closed) {
rest1.trimEndPoint(cutDist2);
segment = undefined;
rest2.trimStartPoint(cutDist2);
}
else {
// var l1 = shape.getLengthTo(cutDist1);
// if (l1<RS.PointTolerance && cutDist1.isEnd===true) {
// l1 = shape.getLength();
// }
// var l2 = shape.getLengthTo(cutDist2);
// if (l2<RS.PointTolerance && cutDist2.isEnd===true) {
// l2 = shape.getLength();
// }
// TODO: use real click point (position)
// if (l1 > l2) {
// rest1.trimEndPoint(cutDist2, cutDist2);
// segment.trimStartPoint(cutDist2, cutDist2);
// segment.trimEndPoint(cutDist1, cutDist1);
// rest2.trimStartPoint(cutDist1, cutDist1);
// }
// else {
rest1.trimEndPoint(cutDist1);
var l1 = segment.getLength();
segment.trimStartPoint(cutDist1);
var l2 = segment.getLength();
segment.trimEndPoint(cutDist2 - (l1-l2));
rest2.trimStartPoint(cutDist2);
// }
}
if (!isNull(segment)) {
if (segment.getLength()<RS.PointTolerance || (closed && RMath.fuzzyCompare(segment.getLength(), shape.getLength()))) {
segment = undefined;
}
}
if (!isNull(rest1)) {
if (rest1.getLength()<RS.PointTolerance || (closed && RMath.fuzzyCompare(rest1.getLength(), shape.getLength()))) {
rest1 = undefined;
}
}
if (!isNull(rest2)) {
if (rest2.getLength()<RS.PointTolerance || (closed && RMath.fuzzyCompare(rest2.getLength(), shape.getLength()))) {
rest2 = undefined;
}
}
if (isNull(segment) && !isNull(rest1) && !isNull(rest2)) {
var distRest1 = rest1.getDistanceTo(position);
var distRest2 = rest2.getDistanceTo(position);
if (distRest1<distRest2 || isNaN(distRest2)) {
segment = rest1;
rest1 = undefined;
}
else {
segment = rest2;
rest2 = undefined;
}
}
}
// spline:
else if (isSplineShape(shape)) {
rest1 = shape.clone();
rest2 = shape.clone();
segment = shape.clone();
var tAtCutPos1 = shape.getTAtDistance(cutDist1);
var tAtCutPos2 = shape.getTAtDistance(cutDist2);
var tMax = shape.getTMax();
if (shape.getStartPoint().equalsFuzzy(shape.getEndPoint())) {
if (RMath.fuzzyCompare(tAtCutPos1, shape.getTMax())) {
tAtCutPos1 = shape.getTMin();
}
}
if (tAtCutPos1 < tAtCutPos2) {
if (RMath.fuzzyCompare(tAtCutPos1, 0.0)) {
rest1 = undefined;
}
else {
rest1.trimEndPoint(cutDist1);
// positions are more precise but
// distances take into account possible self intersections:
rest1.setEndPoint(cutPos1);
}
var l1 = segment.getLength();
segment.trimStartPoint(cutDist1);
segment.setStartPoint(cutPos1);
var l2 = segment.getLength();
segment.trimEndPoint(cutDist2 - (l1-l2));
segment.setEndPoint(cutPos2);
if (RMath.fuzzyCompare(tAtCutPos2, tMax)) {
rest2 = undefined;
}
else {
rest2.trimStartPoint(cutDist2);
rest2.setStartPoint(cutPos2);
}
}
else {
if (RMath.fuzzyCompare(tAtCutPos1, 0.0)) {
rest1 = undefined;
}
else {
rest1.trimEndPoint(cutDist2);
rest1.setEndPoint(cutPos2);
}
var l1 = segment.getLength();
segment.trimStartPoint(cutDist2);
segment.setStartPoint(cutPos2);
var l2 = segment.getLength();
segment.trimEndPoint(cutDist1 - (l1-l2));
segment.setEndPoint(cutPos1);
if (RMath.fuzzyCompare(tAtCutPos2, tMax)) {
rest2 = undefined;
}
else {
rest2.trimStartPoint(cutDist1);
rest2.setStartPoint(cutPos1);
}
}
if (!isNull(segment)) {
if (!segment.isValid() || segment.getLength()<RS.PointTolerance) {
segment = undefined;
}
}
if (!isNull(rest1)) {
if (!rest1.isValid() || rest1.getLength()<RS.PointTolerance) {
rest1 = undefined;
}
}
if (!isNull(rest2)) {
if (!rest2.isValid() || rest2.getLength()<RS.PointTolerance) {
rest2 = undefined;
}
}
}
var ret = [];
// add new rest entities:
ret.push(rest1);
ret.push(rest2);
ret.push(segment);
return ret;
};
/**
* \return The two distances along the given shape identifying the
* intersections points closest to the given position.
*
* \param onShape True: only return intersections on the shape
* (for trimming, breaking, default).
* False: Also consider intersection points outside of shape (for extending).
* \param onOtherShapes True only return intersections on one of the other
* shapes (for extending).
*/
ShapeAlgorithms.getClosestIntersectionPointDistances = function(shape, intersections, position) {
// if (isNull(onShape)) {
// onShape = true;
// }
// if (isNull(onOtherShapes)) {
// onOtherShapes = false;
// }
// var i, k;
// // treat start and end points as intersection points for open shapes:
// if (onShape &&
// !isCircleShape(shape) &&
// !isFullEllipseShape(shape) &&
// !isXLineShape(shape) &&
// (!isPolylineShape(shape) || !shape.isGeometricallyClosed()) &&
// (!isSplineShape(shape) || !shape.isClosed())) {
// var sp = shape.getStartPoint();
// sp.isStart = true;
// intersections.push(sp);
// if (!isRayShape(shape)) {
// var ep = shape.getEndPoint()
// ep.isEnd = true;
// intersections.push(ep);
// }
// }
// // find all intersection points:
// for (i=0; i<otherShapes.length; i++) {
// var otherShape = otherShapes[i];