forked from dikalo/lienzo-core
/
OrthogonalPolyLine.java
817 lines (690 loc) · 22.2 KB
/
OrthogonalPolyLine.java
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
/*
Copyright (c) 2014,2015 Ahome' Innovation Technologies. 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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ait.lienzo.client.core.shape;
import static com.ait.lienzo.shared.core.types.Direction.EAST;
import static com.ait.lienzo.shared.core.types.Direction.NONE;
import static com.ait.lienzo.shared.core.types.Direction.NORTH;
import static com.ait.lienzo.shared.core.types.Direction.SOUTH;
import static com.ait.lienzo.shared.core.types.Direction.WEST;
import java.util.Arrays;
import java.util.List;
import com.ait.lienzo.client.core.Attribute;
import com.ait.lienzo.client.core.Context2D;
import com.ait.lienzo.client.core.shape.json.validators.ValidationContext;
import com.ait.lienzo.client.core.shape.json.validators.ValidationException;
import com.ait.lienzo.client.core.types.BoundingBox;
import com.ait.lienzo.client.core.types.NFastDoubleArrayJSO;
import com.ait.lienzo.client.core.types.PathPartList;
import com.ait.lienzo.client.core.types.Point2D;
import com.ait.lienzo.client.core.types.Point2DArray;
import com.ait.lienzo.client.core.util.Geometry;
import com.ait.lienzo.shared.core.types.Direction;
import com.ait.lienzo.shared.core.types.ShapeType;
import com.google.gwt.json.client.JSONObject;
public class OrthogonalPolyLine extends AbstractDirectionalMultiPointShape<OrthogonalPolyLine>
{
private final PathPartList m_list = new PathPartList();
private Point2D m_tailOffsetPoint;
private Point2D m_headOffsetPoint;
public OrthogonalPolyLine(final Point2D start, final Point2D... points)
{
this(new Point2DArray(start, points));
}
public OrthogonalPolyLine(final Point2DArray points)
{
super(ShapeType.ORTHOGONAL_POLYLINE);
setControlPoints(points);
}
protected OrthogonalPolyLine(final JSONObject node, final ValidationContext ctx) throws ValidationException
{
super(ShapeType.ORTHOGONAL_POLYLINE, node, ctx);
}
public void correctTailWithOffset(Point2DArray points, double offset, Direction direction)
{
Point2D p0 = points.get(0);
Point2D p1 = points.get(1);
Point2D p = correctEndWithOffset(offset, direction, p0, p1, false);
m_tailOffsetPoint = p;
points.set(0, p);
}
public void correctHeadWithOffset(Point2DArray points, double offset, Direction direction)
{
int size = points.size();
Point2D p0 = points.get(size - 2);
Point2D p1 = points.get(size - 1);
Point2D p = correctEndWithOffset(offset, direction, p0, p1, true);
m_headOffsetPoint = p;
points.set(size - 1, p);
}
private static final Point2D correctEndWithOffset(double offset, final Direction direction, final Point2D p0, final Point2D p1, final boolean reverse)
{
Point2D target;
if (reverse)
{
target = p1;
offset = -offset;
}
else
{
target = p0;
}
switch (direction)
{
case NORTH:
return target.setY(target.getY() - offset);
case EAST:
return target.setX(target.getX() + offset);
case SOUTH:
return target.setY(target.getY() + offset);
case WEST:
return target.setX(target.getX() - offset);
case NONE:
default:
return target.add(p1.sub(p0).unit().mul(offset)); // unit vector in the direction of SE
}
}
private static final NFastDoubleArrayJSO getOrthogonalLinePoints(final Point2DArray points, Direction tailDirection, Direction headDirection, final double correction)
{
final NFastDoubleArrayJSO buffer = NFastDoubleArrayJSO.make();
if (points.size() == 2)
{
Point2D p0 = points.get(0);
Point2D p1 = points.get(1);
double p0x = p0.getX();
double p0y = p0.getY();
switch (tailDirection)
{
case NORTH:
p0y = p0y - correction;
addPoint(buffer, p0x, p0y);
break;
case SOUTH:
p0y = p0y + correction;
addPoint(buffer, p0x, p0y);
break;
case EAST:
p0x = p0x + correction;
addPoint(buffer, p0x, p0y);
break;
case WEST:
p0x = p0x - correction;
addPoint(buffer, p0x, p0y);
break;
case NONE:
tailDirection = getTailDirection(points);
break;
}
addHead(buffer, tailDirection, headDirection, p0, p1, correction);
return buffer;
}
Point2D p1;
int i = 0;
Direction direction = tailDirection;
if (tailDirection == NONE)
{
direction = getTailDirection(points);
p1 = points.get(0);
i = 1;
}
else
{
boolean tailAdded = addTail(buffer, points, tailDirection, correction);
if (tailAdded)
{
// need to get new direction, and update the start offset (i).
int size = buffer.size();
p1 = new Point2D(buffer.get(size - 2), buffer.get(size - 1));
direction = getDirection(buffer.get(size - 4), buffer.get(size - 3), p1.getX(), p1.getY());
i = 2;
}
else
{
p1 = points.get(0);
i = 1;
}
}
final int size = points.size();
for (; i < size - 1; i++)
{
Point2D p2 = points.get(i);
direction = getOrthogonalLinePointsAndDirection(buffer, direction, p1.getX(), p1.getY(), p2.getX(), p2.getY());
if (null == direction)
{
return null;
}
p1 = p2;
}
p1 = points.get(size - 2);
Point2D p2 = points.get(size - 1);
addHead(buffer, direction, headDirection, p1, p2, correction);
return buffer;
}
private static final boolean addTail(final NFastDoubleArrayJSO buffer, Point2DArray points, final Direction tailDirection, final double correction)
{
Point2D p0 = points.get(0);
Point2D p1 = points.get(1);
double p0x = p0.getX();
double p0y = p0.getY();
double p1x = p1.getX();
double p1y = p1.getY();
final double dx = (p1x - p0x);
final double dy = (p1y - p0y);
switch (tailDirection)
{
case SOUTH:
if (dy > correction)
{
return false;
}
break;
case NORTH:
if (dy < correction)
{
return false;
}
break;
case EAST:
if (dx > correction)
{
return false;
}
break;
case WEST:
if (dx < correction)
{
return false;
}
break;
default:
return false;
}
double offset = correction;
switch (tailDirection)
{
case NORTH:
offset = -offset;
case SOUTH:
{
double x = p0x;
double y = p0y + offset;
addPoint(buffer, x, y);
x = p1x;
addPoint(buffer, x, y);
addPoint(buffer, p1x, p1y);
return true;
}
case WEST:
offset = -offset;
case EAST:
{
double x = p0x + offset;
double y = p0y;
addPoint(buffer, x, y);
y = p1y;
addPoint(buffer, x, y);
x = p1x;
addPoint(buffer, p1x, p1y);
return true;
}
default:
return false;
}
}
private static final void addHead(NFastDoubleArrayJSO buffer, Direction lastDirection, Direction headDirection, Point2D p0, Point2D p1, final double correction)
{
double p0x = p0.getX();
double p0y = p0.getY();
double p1x = p1.getX();
double p1y = p1.getY();
// the delta tells us the quadrant we need move to
final double dx = (p1x - p0x);
final double dy = (p1y - p0y);
double x, y;
double correctionOffset = correction;
switch (headDirection)
{
case SOUTH:
correctionOffset = -correction;
case NORTH:
if (((headDirection == SOUTH) && (dy < correction)) || ((headDirection == NORTH) && (dy > -correction)))
{
// p1 located same side of the HEAD direction
if (lastDirection == headDirection)
{
x = p0x + (dx / 2);
y = p0y;
addPoint(buffer, x, y);
}
else
{
x = p0x;
}
y = p1y + correctionOffset;
addPoint(buffer, x, y);
x = p1x;
addPoint(buffer, x, y);
}
else
{
// p1 is opposite of p3, for the head direction
if (((dx > 0) && (lastDirection == WEST)) || ((dx < 0) && (lastDirection == EAST)))
{
x = p0x;
y = p0y + (dy / 2);
addPoint(buffer, x, y);
x = p1x;
addPoint(buffer, x, y);
}
else
{
x = p1x;
y = p0y;
addPoint(buffer, x, y);
}
}
break;
case EAST:
correctionOffset = -correction;
case WEST:
if (((headDirection == EAST) && (dx < correction)) || ((headDirection == WEST) && (dx > -correction)))
{
// p1 located same side of the HEAD direction
if (lastDirection == headDirection)
{
x = p0x;
y = p0y + (dy / 2);
addPoint(buffer, x, y);
}
else
{
y = p0y;
}
x = p1x + correctionOffset;
addPoint(buffer, x, y);
y = p1y;
addPoint(buffer, x, y);
}
else
{
// p1 is opposite of p3, for the head direction
if (((dy > 0) && (lastDirection == NORTH)) || ((dy < 0) && (lastDirection == SOUTH)))
{
x = p0x + (dx / 2);
y = p0y;
addPoint(buffer, x, y);
y = p1y;
addPoint(buffer, x, y);
}
else
{
x = p0x;
y = p1y;
addPoint(buffer, x, y);
}
}
break;
case NONE:
if (p0x == p1x || p0y == p1y)
{
// on same axis, so just draw straight line.
addPoint(buffer, p1x, p1y);
}
else
{
getOrthogonalLinePointsAndDirection(buffer, lastDirection, p0x, p0y, p1x, p1y);
}
return;
}
addPoint(buffer, p1x, p1y);
}
private static final Direction getTailDirection(final Point2DArray points)
{
Point2D p1 = points.get(0);
Point2D p2 = points.get(1);
double p1x = p1.getX();
double p1y = p1.getY();
double p2x = p2.getX();
double p2y = p2.getY();
final double dx = (p2x - p1x);
final double dy = (p2y - p1y);
if (dy < 0)
{
// if p2 is north, then always go north
return NORTH;
}
else if (dx == 0)
{
// if it's directly south, with same x, go south.
return SOUTH;
}
else if (dx > 0)
{
// if p2 is south and east, so go east
return EAST;
}
else
//if (dx > 0 )
{
// we know p2 is south and west, so go west.
return WEST;
}
}
private static final Direction getDirection(final double p1x, final double p1y, final double p2x, final double p2y)
{
final double dx = (p2x - p1x);
final double dy = (p2y - p1y);
if ((dx == 0) && (dy == 0))
{
return NONE;
}
if (dy < 0)
{
return NORTH;
}
else if (dy > 0)
{
return SOUTH;
}
else if (dx > 0)
{
return EAST;
}
else
//if (dx < 0 )
{
return WEST;
}
}
/**
* Draws an orthogonal line between two points, it uses the previous direction to determine the new direction. It
* will always attempt to continue the line in the same direction if it can do so, without requiring a corner.
*/
private static final Direction getOrthogonalLinePointsAndDirection(final NFastDoubleArrayJSO buffer, final Direction direction, final double p1x, final double p1y, final double p2x, final double p2y)
{
Direction next_direction;
switch (direction)
{
case NORTH:
if (p2y < p1y)
{
next_direction = NORTH;
}
else if (p2x > p1x)
{
next_direction = EAST;
}
else
{
next_direction = WEST;
}
break;
case SOUTH:
if (p2y > p1y)
{
next_direction = SOUTH;
}
else if (p2x > p1x)
{
next_direction = EAST;
}
else
{
next_direction = WEST;
}
break;
case EAST:
if (p2x > p1x)
{
next_direction = EAST;
}
else if (p2y < p1y)
{
next_direction = NORTH;
}
else
{
next_direction = SOUTH;
}
break;
case WEST:
if (p2x < p1x)
{
next_direction = WEST;
}
else if (p2y < p1y)
{
next_direction = NORTH;
}
else
{
next_direction = SOUTH;
}
break;
default:
return null;
}
if ((next_direction == SOUTH) || (next_direction == NORTH))
{
if (p1x != p2x)
{
buffer.push(p1x, p2y, p2x, p2y);
}
else
{
// points are already on a straight line, so don't try and apply an orthogonal line
buffer.push(p2x, p2y);
}
if (p1x < p2x)
{
return EAST;
}
else if (p1x > p2x)
{
return WEST;
}
else
{
return next_direction;
}
}
else
{
if (p1y != p2y)
{
buffer.push(p2x, p1y, p2x, p2y);
}
else
{
// points are already on a straight line, so don't try and apply an orthogonal line
buffer.push(p2x, p2y);
}
if (p1y > p2y)
{
return NORTH;
}
else if (p1y < p2y)
{
return SOUTH;
}
else
{
return next_direction;
}
}
}
private static final void addPoint(final NFastDoubleArrayJSO buffer, final double x, final double y)
{
buffer.push(x, y);
}
@Override
public BoundingBox getBoundingBox()
{
if (m_list.size() < 1)
{
if (false == parse(getAttributes()))
{
return new BoundingBox(0, 0, 0, 0);
}
}
return m_list.getBoundingBox();
}
@Override
protected boolean prepare(final Context2D context, final Attributes attr, final double alpha)
{
if (m_list.size() < 1)
{
if (false == parse(attr))
{
return false;
}
}
if (m_list.size() < 1)
{
return false;
}
context.path(m_list);
return true;
}
@Override
public OrthogonalPolyLine refresh()
{
m_list.clear();
return this;
}
@Override
protected void fill(Context2D context, Attributes attr, double alpha)
{
}
private final boolean parse(final Attributes attr)
{
Point2DArray points = attr.getControlPoints();
if (null != points)
{
points = points.noAdjacentPoints(); // this clones the points, so we are ok to mutate the elements (see head/tail offset)
if (points.size() > 1)
{
final double tailOffset = attr.getTailOffset();
final double headOffset = attr.getHeadOffset();
final double correction = attr.getCorrectionOffset();
final Direction headDirection = attr.getHeadDirection();
final Direction tailDirection = attr.getTailDirection();
if (tailOffset > 0)
{
correctTailWithOffset(points, tailOffset, tailDirection);
}
if (headOffset > 0)
{
correctHeadWithOffset(points, headOffset, headDirection);
}
final Point2D p1 = points.get(0);
final NFastDoubleArrayJSO linept = getOrthogonalLinePoints(points, tailDirection, headDirection, correction);
if (null != linept)
{
m_list.M(p1.getX(), p1.getY());
final double radius = getCornerRadius();
if (radius > 0)
{
Geometry.drawArcJoinedLines(m_list, Geometry.getPoints(linept, p1), radius);
}
else
{
drawLines(linept);
}
return true;
}
}
}
return false;
}
private final void drawLines(final NFastDoubleArrayJSO points)
{
final int size = points.size();
for (int i = 0; i < size; i += 2)
{
m_list.L(points.get(i), points.get(i + 1));
}
}
/**
* Returns this OrthogonalPolyLine's points.
*
* @return {@link Point2DArray}
*/
public Point2DArray getControlPoints()
{
return getAttributes().getControlPoints();
}
/**
* Sets this OrthogonalPolyLine's points.
*
* @param points {@link Point2DArray}
* @return this OrthogonalPolyLine
*/
public OrthogonalPolyLine setControlPoints(final Point2DArray points)
{
getAttributes().setControlPoints(points);
return refresh();
}
public double getCornerRadius()
{
return getAttributes().getCornerRadius();
}
public OrthogonalPolyLine setCornerRadius(final double radius)
{
getAttributes().setCornerRadius(radius);
return refresh();
}
@Override
public OrthogonalPolyLine setPoint2DArray(final Point2DArray points)
{
return setControlPoints(points);
}
@Override
public Point2DArray getPoint2DArray()
{
return getControlPoints();
}
@Override
public boolean isControlPointShape()
{
return true;
}
@Override
public Point2D getTailOffsetPoint()
{
return m_tailOffsetPoint;
}
@Override
public Point2D getHeadOffsetPoint()
{
return m_headOffsetPoint;
}
@Override
public List<Attribute> getBoundingBoxAttributes()
{
return getBoundingBoxAttributesComposed(Arrays.asList(Attribute.CONTROL_POINTS));
}
public static class OrthogonaPolylLineFactory extends AbstractDirectionalMultiPointShapeFactory<OrthogonalPolyLine>
{
public OrthogonaPolylLineFactory()
{
super(ShapeType.ORTHOGONAL_POLYLINE);
addAttribute(Attribute.CORNER_RADIUS);
addAttribute(Attribute.CONTROL_POINTS, true);
}
@Override
public OrthogonalPolyLine create(final JSONObject node, final ValidationContext ctx) throws ValidationException
{
return new OrthogonalPolyLine(node, ctx);
}
}
}