forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxCurve.java
More file actions
1339 lines (1168 loc) · 37.1 KB
/
mxCurve.java
File metadata and controls
1339 lines (1168 loc) · 37.1 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) 2009-2012, JGraph Ltd
*/
package com.mxgraph.util;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
public class mxCurve
{
/**
* A collection of arrays of curve points
*/
protected Map<String, mxPoint[]> points;
// Rectangle just completely enclosing branch and label/
protected double minXBounds = 10000000;
protected double maxXBounds = 0;
protected double minYBounds = 10000000;
protected double maxYBounds = 0;
/**
* An array of arrays of intervals. These intervals define the distance
* along the edge (0 to 1) that each point lies
*/
protected Map<String, double[]> intervals;
/**
* The curve lengths of the curves
*/
protected Map<String, Double> curveLengths;
/**
* Defines the key for the central curve index
*/
public static String CORE_CURVE = "Center_curve";
/**
* Defines the key for the label curve index
*/
public static String LABEL_CURVE = "Label_curve";;
/**
* Indicates that an invalid position on a curve was requested
*/
public static mxLine INVALID_POSITION = new mxLine(new mxPoint(0, 0),
new mxPoint(1, 0));
/**
* Offset of the label curve from the curve the label curve is based on.
* If you wish to set this value, do so directly after creation of the curve.
* The first time the curve is used the label curve will be created with
* whatever value is contained in this variable. Changes to it after that point
* will have no effect.
*/
protected double labelBuffer = mxConstants.DEFAULT_LABEL_BUFFER;
/**
* The points this curve is drawn through. These are typically control
* points and are at distances from each other that straight lines
* between them do not describe a smooth curve. This class takes
* these guiding points and creates a finer set of internal points
* that visually appears to be a curve when linked by straight lines
*/
public List<mxPoint> guidePoints = new ArrayList<mxPoint>();
/**
* Whether or not the curve currently holds valid values
*/
protected boolean valid = false;
/**
*
*/
public void setLabelBuffer(double buffer)
{
labelBuffer = buffer;
}
/**
*
*/
public mxRectangle getBounds()
{
if (!valid)
{
createCoreCurve();
}
return new mxRectangle(minXBounds, minYBounds, maxXBounds - minXBounds,
maxYBounds - minYBounds);
}
/**
*
*/
public mxCurve()
{
}
/**
*
*/
public mxCurve(List<mxPoint> points)
{
boolean nullPoints = false;
for (mxPoint point : points)
{
if (point == null)
{
nullPoints = true;
break;
}
}
if (!nullPoints)
{
guidePoints = new ArrayList<mxPoint>(points);
}
}
/**
* Calculates the index of the lower point on the segment
* that contains the point <i>distance</i> along the
*/
protected int getLowerIndexOfSegment(String index, double distance)
{
double[] curveIntervals = getIntervals(index);
if (curveIntervals == null)
{
return 0;
}
int numIntervals = curveIntervals.length;
if (distance <= 0.0 || numIntervals < 3)
{
return 0;
}
if (distance >= 1.0)
{
return numIntervals - 2;
}
// Pick a starting index roughly where you expect the point
// to be
int testIndex = (int) (numIntervals * distance);
if (testIndex >= numIntervals)
{
testIndex = numIntervals - 1;
}
// The max and min indices tested so far
int lowerLimit = -1;
int upperLimit = numIntervals;
// It cannot take more than the number of intervals to find
// the correct segment
for (int i = 0; i < numIntervals; i++)
{
double segmentDistance = curveIntervals[testIndex];
double multiplier = 0.5;
if (distance < segmentDistance)
{
upperLimit = Math.min(upperLimit, testIndex);
multiplier = -0.5;
}
else if (distance > segmentDistance)
{
lowerLimit = Math.max(lowerLimit, testIndex);
}
else
{
// Values equal
if (testIndex == 0)
{
lowerLimit = 0;
upperLimit = 1;
}
else
{
lowerLimit = testIndex - 1;
upperLimit = testIndex;
}
}
int indexDifference = upperLimit - lowerLimit;
if (indexDifference == 1)
{
break;
}
testIndex = (int) (testIndex + indexDifference * multiplier);
if (testIndex == lowerLimit)
{
testIndex = lowerLimit + 1;
}
if (testIndex == upperLimit)
{
testIndex = upperLimit - 1;
}
}
if (lowerLimit != upperLimit - 1)
{
return -1;
}
return lowerLimit;
}
/**
* Returns a unit vector parallel to the curve at the specified
* distance along the curve. To obtain the angle the vector makes
* with (1,0) perform Math.atan(segVectorY/segVectorX).
* @param index the curve index specifying the curve to analyse
* @param distance the distance from start to end of curve (0.0...1.0)
* @return a unit vector at the specified point on the curve represented
* as a line, parallel with the curve. If the distance or curve is
* invalid, <code>mxCurve.INVALID_POSITION</code> is returned
*/
public mxLine getCurveParallel(String index, double distance)
{
mxPoint[] pointsCurve = getCurvePoints(index);
double[] curveIntervals = getIntervals(index);
if (pointsCurve != null && pointsCurve.length > 0
&& curveIntervals != null && distance >= 0.0 && distance <= 1.0)
{
// If the curve is zero length, it will only have one point
// We can't calculate in this case
if (pointsCurve.length == 1)
{
mxPoint point = pointsCurve[0];
return new mxLine(point.getX(), point.getY(), new mxPoint(1, 0));
}
int lowerLimit = getLowerIndexOfSegment(index, distance);
mxPoint firstPointOfSeg = pointsCurve[lowerLimit];
double segVectorX = pointsCurve[lowerLimit + 1].getX()
- firstPointOfSeg.getX();
double segVectorY = pointsCurve[lowerLimit + 1].getY()
- firstPointOfSeg.getY();
double distanceAlongSeg = (distance - curveIntervals[lowerLimit])
/ (curveIntervals[lowerLimit + 1] - curveIntervals[lowerLimit]);
double segLength = Math.sqrt(segVectorX * segVectorX + segVectorY
* segVectorY);
double startPointX = firstPointOfSeg.getX() + segVectorX
* distanceAlongSeg;
double startPointY = firstPointOfSeg.getY() + segVectorY
* distanceAlongSeg;
mxPoint endPoint = new mxPoint(segVectorX / segLength, segVectorY
/ segLength);
return new mxLine(startPointX, startPointY, endPoint);
}
else
{
return INVALID_POSITION;
}
}
/**
* Returns a section of the curve as an array of points
* @param index the curve index specifying the curve to analyse
* @param start the start position of the curve segment (0.0...1.0)
* @param end the end position of the curve segment (0.0...1.0)
* @return a sequence of point representing the curve section or null
* if it cannot be calculated
*/
public mxPoint[] getCurveSection(String index, double start, double end)
{
mxPoint[] pointsCurve = getCurvePoints(index);
double[] curveIntervals = getIntervals(index);
if (pointsCurve != null && pointsCurve.length > 0
&& curveIntervals != null && start >= 0.0 && start <= 1.0
&& end >= 0.0 && end <= 1.0)
{
// If the curve is zero length, it will only have one point
// We can't calculate in this case
if (pointsCurve.length == 1)
{
mxPoint point = pointsCurve[0];
return new mxPoint[] { new mxPoint(point.getX(), point.getY()) };
}
int lowerLimit = getLowerIndexOfSegment(index, start);
mxPoint firstPointOfSeg = pointsCurve[lowerLimit];
double segVectorX = pointsCurve[lowerLimit + 1].getX()
- firstPointOfSeg.getX();
double segVectorY = pointsCurve[lowerLimit + 1].getY()
- firstPointOfSeg.getY();
double distanceAlongSeg = (start - curveIntervals[lowerLimit])
/ (curveIntervals[lowerLimit + 1] - curveIntervals[lowerLimit]);
mxPoint startPoint = new mxPoint(firstPointOfSeg.getX()
+ segVectorX * distanceAlongSeg, firstPointOfSeg.getY()
+ segVectorY * distanceAlongSeg);
List<mxPoint> result = new ArrayList<mxPoint>();
result.add(startPoint);
double current = start;
current = curveIntervals[++lowerLimit];
while (current <= end)
{
mxPoint nextPointOfSeg = pointsCurve[lowerLimit];
result.add(nextPointOfSeg);
current = curveIntervals[++lowerLimit];
}
// Add whatever proportion of the last segment has to
// be added to make the exactly end distance
if (lowerLimit > 0 && lowerLimit < pointsCurve.length
&& end > curveIntervals[lowerLimit - 1])
{
firstPointOfSeg = pointsCurve[lowerLimit - 1];
segVectorX = pointsCurve[lowerLimit].getX()
- firstPointOfSeg.getX();
segVectorY = pointsCurve[lowerLimit].getY()
- firstPointOfSeg.getY();
distanceAlongSeg = (end - curveIntervals[lowerLimit - 1])
/ (curveIntervals[lowerLimit] - curveIntervals[lowerLimit - 1]);
mxPoint endPoint = new mxPoint(firstPointOfSeg.getX()
+ segVectorX * distanceAlongSeg, firstPointOfSeg.getY()
+ segVectorY * distanceAlongSeg);
result.add(endPoint);
}
mxPoint[] resultArray = new mxPoint[result.size()];
return result.toArray(resultArray);
}
else
{
return null;
}
}
/**
* Returns whether or not the rectangle passed in hits any part of this
* curve.
* @param rect the rectangle to detect for a hit
* @return whether or not the rectangle hits this curve
*/
public boolean intersectsRect(Rectangle rect)
{
// To save CPU, we can test if the rectangle intersects the entire
// bounds of this curve
if (!getBounds().getRectangle().intersects(rect))
{
return false;
}
mxPoint[] pointsCurve = getCurvePoints(mxCurve.CORE_CURVE);
if (pointsCurve != null && pointsCurve.length > 1)
{
mxRectangle mxRect = new mxRectangle(rect);
// First check for any of the curve points lying within the
// rectangle, then for any of the curve segments intersecting
// with the rectangle sides
for (int i = 1; i < pointsCurve.length; i++)
{
if (mxRect.contains(pointsCurve[i].getX(),
pointsCurve[i].getY())
|| mxRect.contains(pointsCurve[i - 1].getX(),
pointsCurve[i - 1].getY()))
{
return true;
}
}
for (int i = 1; i < pointsCurve.length; i++)
{
if (mxRect.intersectLine(pointsCurve[i].getX(),
pointsCurve[i].getY(), pointsCurve[i - 1].getX(),
pointsCurve[i - 1].getY()) != null)
{
return true;
}
}
}
return false;
}
/**
* Returns the point at which this curve intersects the boundary of
* the given rectangle, if it does so. If it does not intersect,
* null is returned. If it intersects multiple times, the first
* intersection from the start end of the curve is returned.
*
* @param index the curve index specifying the curve to analyse
* @param rect the whose boundary is to be tested for intersection
* with this curve
* @return the point at which this curve intersects the boundary of
* the given rectangle, if it does so. If it does not intersect,
* null is returned.
*/
public mxPoint intersectsRectPerimeter(String index, mxRectangle rect)
{
mxPoint result = null;
mxPoint[] pointsCurve = getCurvePoints(index);
if (pointsCurve != null && pointsCurve.length > 1)
{
int crossingSeg = intersectRectPerimeterSeg(index, rect);
if (crossingSeg != -1)
{
result = intersectRectPerimeterPoint(index, rect, crossingSeg);
}
}
return result;
}
/**
* Returns the distance from the start of the curve at which this
* curve intersects the boundary of the given rectangle, if it does
* so. If it does not intersect, -1 is returned.
* If it intersects multiple times, the first intersection from
* the start end of the curve is returned.
*
* @param index the curve index specifying the curve to analyse
* @param rect the whose boundary is to be tested for intersection
* with this curve
* @return the distance along the curve from the start at which
* the intersection occurs
*/
public double intersectsRectPerimeterDist(String index, mxRectangle rect)
{
double result = -1;
mxPoint[] pointsCurve = getCurvePoints(index);
double[] curveIntervals = getIntervals(index);
if (pointsCurve != null && pointsCurve.length > 1)
{
int segIndex = intersectRectPerimeterSeg(index, rect);
mxPoint intersectPoint = null;
if (segIndex != -1)
{
intersectPoint = intersectRectPerimeterPoint(index, rect,
segIndex);
}
if (intersectPoint != null)
{
double startSegX = pointsCurve[segIndex - 1].getX();
double startSegY = pointsCurve[segIndex - 1].getY();
double distToStartSeg = curveIntervals[segIndex - 1]
* getCurveLength(index);
double intersectOffsetX = intersectPoint.getX() - startSegX;
double intersectOffsetY = intersectPoint.getY() - startSegY;
double lenToIntersect = Math.sqrt(intersectOffsetX
* intersectOffsetX + intersectOffsetY
* intersectOffsetY);
result = distToStartSeg + lenToIntersect;
}
}
return result;
}
/**
* Returns a point to move the input rectangle to, in order to
* attempt to place the rectangle away from the curve. NOTE: Curves
* are scaled, the input rectangle should be also.
* @param index the curve index specifying the curve to analyse
* @param rect the rectangle that is to be moved
* @param buffer the amount by which the rectangle is to be moved,
* beyond the dimensions of the rect
* @return the point to move the top left of the input rect to
* , otherwise null if no point can be determined
*/
public mxPoint collisionMove(String index, mxRectangle rect, double buffer)
{
int hitSeg = intersectRectPerimeterSeg(index, rect);
// Could test for a second hit (the rect exit, unless the same
// segment is entry and exit) and allow for that in movement.
if (hitSeg == -1)
{
return null;
}
else
{
mxPoint[] pointsCurve = getCurvePoints(index);
double x0 = pointsCurve[hitSeg - 1].getX();
double y0 = pointsCurve[hitSeg - 1].getY();
double x1 = pointsCurve[hitSeg].getX();
double y1 = pointsCurve[hitSeg].getY();
double x = rect.getX();
double y = rect.getY();
double width = rect.getWidth();
double height = rect.getHeight();
// Whether the intersection is one of the horizontal sides of the rect
@SuppressWarnings("unused")
boolean horizIncident = false;
mxPoint hitPoint = mxUtils.intersection(x, y, x + width, y, x0, y0, x1, y1);
if (hitPoint != null)
{
horizIncident = true;
}
else
{
hitPoint = mxUtils.intersection(x + width, y, x + width, y + height,
x0, y0, x1, y1);
}
if (hitPoint == null)
{
hitPoint = mxUtils.intersection(x + width, y + height, x, y + height,
x0, y0, x1, y1);
if (hitPoint != null)
{
horizIncident = true;
}
else
{
hitPoint = mxUtils.intersection(x, y, x, y + height, x0, y0, x1, y1);
}
}
if (hitPoint != null)
{
}
}
return null;
}
/**
* Utility method to determine within which segment the specified rectangle
* intersects the specified curve
*
* @param index the curve index specifying the curve to analyse
* @param rect the whose boundary is to be tested for intersection
* with this curve
* @return the point at which this curve intersects the boundary of
* the given rectangle, if it does so. If it does not intersect,
* -1 is returned
*/
protected int intersectRectPerimeterSeg(String index, mxRectangle rect)
{
return intersectRectPerimeterSeg(index, rect, 1);
}
/**
* Utility method to determine within which segment the specified rectangle
* intersects the specified curve. This method specifies which segment to
* start searching at.
*
* @param index the curve index specifying the curve to analyse
* @param rect the whose boundary is to be tested for intersection
* with this curve
* @param startSegment the segment to start searching at. To start at the
* beginning of the curve, use 1, not 0.
* @return the point at which this curve intersects the boundary of
* the given rectangle, if it does so. If it does not intersect,
* -1 is returned
*/
protected int intersectRectPerimeterSeg(String index, mxRectangle rect,
int startSegment)
{
mxPoint[] pointsCurve = getCurvePoints(index);
if (pointsCurve != null && pointsCurve.length > 1)
{
for (int i = startSegment; i < pointsCurve.length; i++)
{
if (rect.intersectLine(pointsCurve[i].getX(),
pointsCurve[i].getY(), pointsCurve[i - 1].getX(),
pointsCurve[i - 1].getY()) != null)
{
return i;
}
}
}
return -1;
}
/**
* Returns the point at which this curve segment intersects the boundary
* of the given rectangle, if it does so. If it does not intersect,
* null is returned.
*
* @param curveIndex the curve index specifying the curve to analyse
* @param rect the whose boundary is to be tested for intersection
* with this curve
* @param indexSeg the segments on this curve being checked
* @return the point at which this curve segment intersects the boundary
* of the given rectangle, if it does so. If it does not intersect,
* null is returned.
*/
protected mxPoint intersectRectPerimeterPoint(String curveIndex,
mxRectangle rect, int indexSeg)
{
mxPoint result = null;
mxPoint[] pointsCurve = getCurvePoints(curveIndex);
if (pointsCurve != null && pointsCurve.length > 1 && indexSeg >= 0
&& indexSeg < pointsCurve.length)
{
double p1X = pointsCurve[indexSeg - 1].getX();
double p1Y = pointsCurve[indexSeg - 1].getY();
double p2X = pointsCurve[indexSeg].getX();
double p2Y = pointsCurve[indexSeg].getY();
result = rect.intersectLine(p1X, p1Y, p2X, p2Y);
}
return result;
}
/**
* Calculates the position of an absolute in terms relative
* to this curve.
*
* @param absPoint the point whose relative point is to calculated
* @param index the index of the curve whom the relative position is to be
* calculated from
* @return an mxRectangle where the x is the distance along the curve
* (0 to 1), y is the orthogonal offset from the closest segment on the
* curve and (width, height) is an additional Cartesian offset applied
* after the other calculations
*/
public mxRectangle getRelativeFromAbsPoint(mxPoint absPoint, String index)
{
// Work out which segment the absolute point is closest to
mxPoint[] currentCurve = getCurvePoints(index);
double[] currentIntervals = getIntervals(index);
int closestSegment = 0;
double closestSegDistSq = 10000000;
mxLine segment = new mxLine(currentCurve[0], currentCurve[1]);
for (int i = 1; i < currentCurve.length; i++)
{
segment.setPoints(currentCurve[i - 1], currentCurve[i]);
double segDistSq = segment.ptSegDistSq(absPoint);
if (segDistSq < closestSegDistSq)
{
closestSegDistSq = segDistSq;
closestSegment = i - 1;
}
}
// Get the distance (squared) from the point to the
// infinitely extrapolated line created by the closest
// segment. If that value is the same as the distance
// to the segment then an orthogonal offset from some
// point on the line will intersect the point. If they
// are not equal, an additional cartesian offset is
// required
mxPoint startSegPt = currentCurve[closestSegment];
mxPoint endSegPt = currentCurve[closestSegment + 1];
mxLine closestSeg = new mxLine(startSegPt, endSegPt);
double lineDistSq = closestSeg.ptLineDistSq(absPoint);
double orthogonalOffset = Math.sqrt(Math.min(lineDistSq,
closestSegDistSq));
double segX = endSegPt.getX() - startSegPt.getX();
double segY = endSegPt.getY() - startSegPt.getY();
double segDist = Math.sqrt(segX * segX + segY * segY);
double segNormX = segX / segDist;
double segNormY = segY / segDist;
// The orthogonal offset could be in one of two opposite vectors
// Try both solutions, one will be closer to one of the segment
// end points (unless the point is on the line)
double candidateOffX1 = (absPoint.getX() - segNormY * orthogonalOffset)
- endSegPt.getX();
double candidateOffY1 = (absPoint.getY() + segNormX * orthogonalOffset)
- endSegPt.getY();
double candidateOffX2 = (absPoint.getX() + segNormY * orthogonalOffset)
- endSegPt.getX();
double candidateOffY2 = (absPoint.getY() - segNormX * orthogonalOffset)
- endSegPt.getY();
double candidateDist1 = (candidateOffX1 * candidateOffX1)
+ (candidateOffY1 * candidateOffY1);
double candidateDist2 = (candidateOffX2 * candidateOffX2)
+ (candidateOffY2 * candidateOffY2);
double orthOffsetPointX = 0;
double orthOffsetPointY = 0;
if (candidateDist2 < candidateDist1)
{
orthogonalOffset = -orthogonalOffset;
}
orthOffsetPointX = absPoint.getX() - segNormY * orthogonalOffset;
orthOffsetPointY = absPoint.getY() + segNormX * orthogonalOffset;
double distAlongEdge = 0;
double cartOffsetX = 0;
double cartOffsetY = 0;
// Don't compare for exact equality, there are often rounding errors
if (Math.abs(closestSegDistSq - lineDistSq) > 0.0001)
{
// The orthogonal offset does not move the point onto the
// segment. Work out an additional cartesian offset that moves
// the offset point onto the closest end point of the
// segment
// Not exact distances, but the equation holds
double distToStartPoint = Math.abs(orthOffsetPointX
- startSegPt.getX())
+ Math.abs(orthOffsetPointY - startSegPt.getY());
double distToEndPoint = Math
.abs(orthOffsetPointX - endSegPt.getX())
+ Math.abs(orthOffsetPointY - endSegPt.getY());
if (distToStartPoint < distToEndPoint)
{
distAlongEdge = currentIntervals[closestSegment];
cartOffsetX = orthOffsetPointX - startSegPt.getX();
cartOffsetY = orthOffsetPointY - startSegPt.getY();
}
else
{
distAlongEdge = currentIntervals[closestSegment + 1];
cartOffsetX = orthOffsetPointX - endSegPt.getX();
cartOffsetY = orthOffsetPointY - endSegPt.getY();
}
}
else
{
// The point, when orthogonally offset, lies on the segment
// work out what proportion along the segment, and therefore
// the entire curve, the offset point lies.
double segmentLen = Math.sqrt((endSegPt.getX() - startSegPt.getX())
* (endSegPt.getX() - startSegPt.getX())
+ (endSegPt.getY() - startSegPt.getY())
* (endSegPt.getY() - startSegPt.getY()));
double offsetLen = Math.sqrt((orthOffsetPointX - startSegPt.getX())
* (orthOffsetPointX - startSegPt.getX())
+ (orthOffsetPointY - startSegPt.getY())
* (orthOffsetPointY - startSegPt.getY()));
double proportionAlongSeg = offsetLen / segmentLen;
double segProportingDiff = currentIntervals[closestSegment + 1]
- currentIntervals[closestSegment];
distAlongEdge = currentIntervals[closestSegment]
+ segProportingDiff * proportionAlongSeg;
}
if (distAlongEdge > 1.0)
{
distAlongEdge = 1.0;
}
return new mxRectangle(distAlongEdge, orthogonalOffset, cartOffsetX,
cartOffsetY);
}
/**
* Creates the core curve that is based on the guide points passed into
* this class instance
*/
protected void createCoreCurve()
{
// Curve is marked invalid until all of the error situations have
// been checked
valid = false;
if (guidePoints == null || guidePoints.isEmpty())
{
return;
}
for (int i = 0; i < guidePoints.size(); i++)
{
if (guidePoints.get(i) == null)
{
return;
}
}
// Reset the cached bounds value
minXBounds = minYBounds = 10000000;
maxXBounds = maxYBounds = 0;
mxSpline spline = new mxSpline(guidePoints);
// Need the rough length of the spline, so we can get
// more samples for longer edges
double lengthSpline = spline.getLength();
// Check for errors in the spline calculation or zero length curves
if (Double.isNaN(lengthSpline) || !spline.checkValues()
|| lengthSpline < 1)
{
return;
}
mxSpline1D splineX = spline.getSplineX();
mxSpline1D splineY = spline.getSplineY();
double baseInterval = 12.0 / lengthSpline;
double minInterval = 1.0 / lengthSpline;
// Store the last two spline positions. If the next position is
// very close to where the extrapolation of the last two points
// then double the interval. This diviation is terms the "flatness".
// There is a range where the interval is kept the same, any
// variation from this range of flatness invokes a proportional
// adjustment to try to reenter the range without
// over compensating
double interval = baseInterval;
// These deviations are only tested against either
// dimension individually, working out the correct
// distance is too computationally intensive
double minDeviation = 0.15;
double maxDeviation = 0.3;
double preferedDeviation = (maxDeviation + minDeviation) / 2.0;
// x1, y1 are the position two iterations ago, x2, y2
// the position on the last iteration
double x1 = -1.0;
double x2 = -1.0;
double y1 = -1.0;
double y2 = -1.0;
// Store the change in interval amount between iterations.
// If it changes the extrapolation calculation must
// take this into account.
double intervalChange = 1;
List<mxPoint> coreCurve = new ArrayList<mxPoint>();
List<Double> coreIntervals = new ArrayList<Double>();
boolean twoLoopsComplete = false;
for (double t = 0; t <= 1.5; t += interval)
{
if (t > 1.0)
{
// Use the point regardless of the accuracy,
t = 1.0001;
mxPoint endControlPoint = guidePoints
.get(guidePoints.size() - 1);
mxPoint finalPoint = new mxPoint(endControlPoint.getX(),
endControlPoint.getY());
coreCurve.add(finalPoint);
coreIntervals.add(t);
updateBounds(endControlPoint.getX(), endControlPoint.getY());
break;
}
// Whether or not the accuracy of the current point is acceptable
boolean currentPointAccepted = true;
double newX = splineX.getFastValue(t);
double newY = splineY.getFastValue(t);
// Check if the last points are valid (indicated by
// dissimilar values)
// Check we're not in the first, second or last run
if (x1 != -1.0 && twoLoopsComplete && t != 1.0001)
{
// Work out how far the new spline point
// deviates from the extrapolation created
// by the last two points
double diffX = Math.abs(((x2 - x1) * intervalChange + x2)
- newX);
double diffY = Math.abs(((y2 - y1) * intervalChange + y2)
- newY);
// If either the x or y of the straight line
// extrapolation from the last two points
// is more than the 1D deviation allowed
// go back and re-calculate with a smaller interval
// It's possible that the edge has curved too fast
// for the algorithmn. If the interval is
// reduced to less than the minimum permitted
// interval, it may be that it's impossible
// to get within the deviation because of
// the extrapolation overshoot. The minimum
// interval is set to draw correctly for the
// vast majority of cases.
if ((diffX > maxDeviation || diffY > maxDeviation)
&& interval != minInterval)
{
double overshootProportion = maxDeviation
/ Math.max(diffX, diffY);
if (interval * overshootProportion <= minInterval)
{
// Set the interval
intervalChange = minInterval / interval;
}
else
{
// The interval can still be reduced, half
// the interval and go back and redo
// this iteration
intervalChange = overshootProportion;
}
t -= interval;
interval *= intervalChange;
currentPointAccepted = false;
}
else if (diffX < minDeviation && diffY < minDeviation)
{
intervalChange = 1.4;
interval *= intervalChange;
}
else
{
// Try to keep the deviation around the prefered value
double errorRatio = preferedDeviation
/ Math.max(diffX, diffY);
intervalChange = errorRatio / 4.0;
interval *= intervalChange;
}
if (currentPointAccepted)
{
x1 = x2;
y1 = y2;
x2 = newX;
y2 = newY;
}
}
else if (x1 == -1.0)
{
x1 = x2 = newX;
y1 = y2 = newY;
}
else if (x1 == x2 && y1 == y2)
{
x2 = newX;
y2 = newY;
twoLoopsComplete = true;
}
if (currentPointAccepted)
{
mxPoint newPoint = new mxPoint(newX, newY);
coreCurve.add(newPoint);
coreIntervals.add(t);
updateBounds(newX, newY);
}
}
if (coreCurve.size() < 2)
{
// A single point makes no sense, leave the curve as invalid
return;
}
mxPoint[] corePoints = new mxPoint[coreCurve.size()];
int count = 0;
for (mxPoint point : coreCurve)
{
corePoints[count++] = point;
}
points = new Hashtable<String, mxPoint[]>();
curveLengths = new Hashtable<String, Double>();
points.put(CORE_CURVE, corePoints);
curveLengths.put(CORE_CURVE, lengthSpline);
double[] coreIntervalsArray = new double[coreIntervals.size()];
count = 0;
for (Double tempInterval : coreIntervals)
{
coreIntervalsArray[count++] = tempInterval.doubleValue();
}
intervals = new Hashtable<String, double[]>();
intervals.put(CORE_CURVE, coreIntervalsArray);
valid = true;
}