1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm.visitor.paint;
|
---|
3 |
|
---|
4 | import static java.awt.geom.Rectangle2D.OUT_BOTTOM;
|
---|
5 | import static java.awt.geom.Rectangle2D.OUT_LEFT;
|
---|
6 | import static java.awt.geom.Rectangle2D.OUT_RIGHT;
|
---|
7 | import static java.awt.geom.Rectangle2D.OUT_TOP;
|
---|
8 |
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Computes the part of a line that is visible in a given rectangle.
|
---|
14 | * Using int leads to overflow, so we need long int.
|
---|
15 | */
|
---|
16 | public class LineClip {
|
---|
17 | private Point p1, p2;
|
---|
18 | private final Rectangle clipBounds;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a new {@code LineClip}.
|
---|
22 | * @param p1 start point of the clipped line
|
---|
23 | * @param p2 end point of the clipped line
|
---|
24 | * @param clipBounds Clip bounds
|
---|
25 | */
|
---|
26 | public LineClip(Point p1, Point p2, Rectangle clipBounds) {
|
---|
27 | this.p1 = p1;
|
---|
28 | this.p2 = p2;
|
---|
29 | this.clipBounds = clipBounds;
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * run the clipping algorithm
|
---|
34 | * @return true if the some parts of the line lies within the clip bounds
|
---|
35 | */
|
---|
36 | public boolean execute() {
|
---|
37 | if (clipBounds == null) {
|
---|
38 | return false;
|
---|
39 | }
|
---|
40 | return cohenSutherland(p1.x, p1.y, p2.x, p2.y, clipBounds.x, clipBounds.y,
|
---|
41 | (long) clipBounds.x + clipBounds.width,
|
---|
42 | (long) clipBounds.y + clipBounds.height);
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @return start point of the clipped line
|
---|
47 | */
|
---|
48 | public Point getP1() {
|
---|
49 | return p1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * @return end point of the clipped line
|
---|
54 | */
|
---|
55 | public Point getP2() {
|
---|
56 | return p2;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Cohen–Sutherland algorithm.
|
---|
61 | * See <a href="https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm">Wikipedia article</a>
|
---|
62 | * @param x1 X coordinate of first point
|
---|
63 | * @param y1 Y coordinate of first point
|
---|
64 | * @param x2 X coordinate of second point
|
---|
65 | * @param y2 Y coordinate of second point
|
---|
66 | * @param xmin minimal X coordinate
|
---|
67 | * @param ymin minimal Y coordinate
|
---|
68 | * @param xmax maximal X coordinate
|
---|
69 | * @param ymax maximal Y coordinate
|
---|
70 | * @return true, if line is visible in the given clip region
|
---|
71 | */
|
---|
72 | private boolean cohenSutherland(long x1, long y1, long x2, long y2, long xmin, long ymin, long xmax, long ymax) {
|
---|
73 | int outcode0, outcode1, outcodeOut;
|
---|
74 | boolean accept = false;
|
---|
75 | boolean done = false;
|
---|
76 |
|
---|
77 | outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
|
---|
78 | outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
|
---|
79 |
|
---|
80 | do {
|
---|
81 | if ((outcode0 | outcode1) == 0) {
|
---|
82 | accept = true;
|
---|
83 | done = true;
|
---|
84 | } else if ((outcode0 & outcode1) > 0) {
|
---|
85 | done = true;
|
---|
86 | } else {
|
---|
87 | long x = 0;
|
---|
88 | long y = 0;
|
---|
89 | outcodeOut = outcode0 != 0 ? outcode0 : outcode1;
|
---|
90 | if ((outcodeOut & OUT_TOP) != 0) {
|
---|
91 | x = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1);
|
---|
92 | y = ymax;
|
---|
93 | } else if ((outcodeOut & OUT_BOTTOM) != 0) {
|
---|
94 | x = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1);
|
---|
95 | y = ymin;
|
---|
96 | } else if ((outcodeOut & OUT_RIGHT) != 0) {
|
---|
97 | y = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1);
|
---|
98 | x = xmax;
|
---|
99 | } else if ((outcodeOut & OUT_LEFT) != 0) {
|
---|
100 | y = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1);
|
---|
101 | x = xmin;
|
---|
102 | }
|
---|
103 | if (outcodeOut == outcode0) {
|
---|
104 | x1 = x;
|
---|
105 | y1 = y;
|
---|
106 | outcode0 = computeOutCode(x1, y1, xmin, ymin, xmax, ymax);
|
---|
107 | } else {
|
---|
108 | x2 = x;
|
---|
109 | y2 = y;
|
---|
110 | outcode1 = computeOutCode(x2, y2, xmin, ymin, xmax, ymax);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | while (!done);
|
---|
115 |
|
---|
116 | if (accept) {
|
---|
117 | p1 = new Point((int) x1, (int) y1);
|
---|
118 | p2 = new Point((int) x2, (int) y2);
|
---|
119 | return true;
|
---|
120 | }
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * The outcode of the point.
|
---|
126 | * We cannot use {@link Rectangle#outcode} since it does not work with long ints.
|
---|
127 | * @param x X coordinate
|
---|
128 | * @param y Y coordinate
|
---|
129 | * @param xmin minimal X coordinate
|
---|
130 | * @param ymin minimal Y coordinate
|
---|
131 | * @param xmax maximal X coordinate
|
---|
132 | * @param ymax maximal Y coordinate
|
---|
133 | * @return outcode
|
---|
134 | */
|
---|
135 | private static int computeOutCode(long x, long y, long xmin, long ymin, long xmax, long ymax) {
|
---|
136 | int code = 0;
|
---|
137 | if (y > ymax) {
|
---|
138 | code |= OUT_TOP;
|
---|
139 | } else if (y < ymin) {
|
---|
140 | code |= OUT_BOTTOM;
|
---|
141 | }
|
---|
142 | if (x > xmax) {
|
---|
143 | code |= OUT_RIGHT;
|
---|
144 | } else if (x < xmin) {
|
---|
145 | code |= OUT_LEFT;
|
---|
146 | }
|
---|
147 | return code;
|
---|
148 | }
|
---|
149 | }
|
---|