| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.util; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.*; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
|
| 29 |
public final class ShapeUtil { |
| 30 |
|
| 31 |
private static final int NON_RECTILINEAR_TRANSFORM_MASK = |
| 32 |
AffineTransform.TYPE_GENERAL_TRANSFORM | AffineTransform.TYPE_GENERAL_ROTATION; |
| 33 |
|
| 34 |
private ShapeUtil() {} |
| 35 |
|
| 36 |
public static boolean isInvalidArea(@NotNull Rectangle2D area) { |
| 37 |
return area.isEmpty() || Double.isNaN(area.getWidth()) || Double.isNaN(area.getHeight()); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@NotNull |
| 46 |
public static Shape intersect(@NotNull Shape s1, @NotNull Shape s2, boolean keep1, boolean keep2) { |
| 47 |
if (s1 instanceof Rectangle && s2 instanceof Rectangle) { |
| 48 |
return ((Rectangle) s1).intersection((Rectangle) s2); |
| 49 |
} |
| 50 |
if (s1 instanceof Rectangle2D) { |
| 51 |
return intersectRectShape((Rectangle2D) s1, s2, keep1, keep2); |
| 52 |
} else if (s2 instanceof Rectangle2D) { |
| 53 |
return intersectRectShape((Rectangle2D) s2, s1, keep2, keep1); |
| 54 |
} |
| 55 |
return intersectByArea(s1, s2, keep1, keep2); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@NotNull |
| 64 |
private static Shape intersectRectShape(@NotNull Rectangle2D r, @NotNull Shape s, |
| 65 |
boolean keep1, boolean keep2) { |
| 66 |
if (s instanceof Rectangle2D) { |
| 67 |
Rectangle2D r2 = (Rectangle2D) s; |
| 68 |
Rectangle2D outputRect; |
| 69 |
if (!keep1) { |
| 70 |
outputRect = r; |
| 71 |
} else if (!keep2) { |
| 72 |
outputRect = r2; |
| 73 |
} else { |
| 74 |
outputRect = new Rectangle2D.Float(); |
| 75 |
} |
| 76 |
double x1 = Math.max(r.getX(), r2.getX()); |
| 77 |
double x2 = Math.min(r.getX() + r.getWidth(), r2.getX() + r2.getWidth()); |
| 78 |
double y1 = Math.max(r.getY(), r2.getY()); |
| 79 |
double y2 = Math.min(r.getY() + r.getHeight(), r2.getY() + r2.getHeight()); |
| 80 |
|
| 81 |
if (((x2 - x1) < 0) || ((y2 - y1) < 0)) |
| 82 |
|
| 83 |
outputRect.setFrameFromDiagonal(0, 0, 0, 0); |
| 84 |
else |
| 85 |
outputRect.setFrameFromDiagonal(x1, y1, x2, y2); |
| 86 |
return outputRect; |
| 87 |
} |
| 88 |
if (r.contains(s.getBounds2D())) { |
| 89 |
if (keep2) { |
| 90 |
s = cloneShape(s); |
| 91 |
} |
| 92 |
return s; |
| 93 |
} |
| 94 |
return intersectByArea(r, s, keep1, keep2); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@NotNull |
| 104 |
private static Shape intersectByArea(@NotNull Shape s1, @NotNull Shape s2, boolean keep1, boolean keep2) { |
| 105 |
Area a1; |
| 106 |
Area a2; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
if (!keep1 && (s1 instanceof Area)) { |
| 111 |
a1 = (Area) s1; |
| 112 |
} else if (!keep2 && (s2 instanceof Area)) { |
| 113 |
a1 = (Area) s2; |
| 114 |
s2 = s1; |
| 115 |
} else { |
| 116 |
a1 = new Area(s1); |
| 117 |
} |
| 118 |
|
| 119 |
if (s2 instanceof Area) { |
| 120 |
a2 = (Area) s2; |
| 121 |
} else { |
| 122 |
a2 = new Area(s2); |
| 123 |
} |
| 124 |
|
| 125 |
a1.intersect(a2); |
| 126 |
if (a1.isRectangular()) { |
| 127 |
return a1.getBounds2D(); |
| 128 |
} |
| 129 |
|
| 130 |
return a1; |
| 131 |
} |
| 132 |
|
| 133 |
public static @NotNull Shape transformShape(@NotNull Shape s, @NotNull AffineTransform transform) { |
| 134 |
if (transform.getType() > AffineTransform.TYPE_TRANSLATION) { |
| 135 |
return transformShape(transform, s); |
| 136 |
} else { |
| 137 |
return transformShape(transform.getTranslateX(), transform.getTranslateY(), s); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private static Shape transformShape(@NotNull AffineTransform tx, @NotNull Shape shape) { |
| 142 |
if (shape instanceof Rectangle2D && |
| 143 |
(tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0) { |
| 144 |
Rectangle2D rect = (Rectangle2D) shape; |
| 145 |
double[] matrix = new double[4]; |
| 146 |
matrix[0] = rect.getX(); |
| 147 |
matrix[1] = rect.getY(); |
| 148 |
matrix[2] = matrix[0] + rect.getWidth(); |
| 149 |
matrix[3] = matrix[1] + rect.getHeight(); |
| 150 |
tx.transform(matrix, 0, matrix, 0, 2); |
| 151 |
fixRectangleOrientation(matrix, rect); |
| 152 |
return new Rectangle2D.Double(matrix[0], matrix[1], |
| 153 |
matrix[2] - matrix[0], |
| 154 |
matrix[3] - matrix[1]); |
| 155 |
} |
| 156 |
|
| 157 |
if (tx.isIdentity()) { |
| 158 |
return cloneShape(shape); |
| 159 |
} |
| 160 |
|
| 161 |
return tx.createTransformedShape(shape); |
| 162 |
} |
| 163 |
|
| 164 |
private static void fixRectangleOrientation(double[] m, @NotNull Rectangle2D r) { |
| 165 |
if (r.getWidth() > 0 != (m[2] - m[0] > 0)) { |
| 166 |
double t = m[0]; |
| 167 |
m[0] = m[2]; |
| 168 |
m[2] = t; |
| 169 |
} |
| 170 |
if (r.getHeight() > 0 != (m[3] - m[1] > 0)) { |
| 171 |
double t = m[1]; |
| 172 |
m[1] = m[3]; |
| 173 |
m[3] = t; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private static @NotNull Shape transformShape(double tx, double ty, @NotNull Shape s) { |
| 178 |
if (s instanceof Rectangle2D) { |
| 179 |
Rectangle2D rect = (Rectangle2D) s; |
| 180 |
return new Rectangle2D.Double( |
| 181 |
rect.getX() + tx, |
| 182 |
rect.getY() + ty, |
| 183 |
rect.getWidth(), |
| 184 |
rect.getHeight()); |
| 185 |
} |
| 186 |
|
| 187 |
if (tx == 0 && ty == 0) { |
| 188 |
return ShapeUtil.cloneShape(s); |
| 189 |
} |
| 190 |
|
| 191 |
AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty); |
| 192 |
return mat.createTransformedShape(s); |
| 193 |
} |
| 194 |
|
| 195 |
private static Shape cloneShape(Shape s) { |
| 196 |
return new GeneralPath(s); |
| 197 |
} |
| 198 |
} |