| •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.geometry.util; |
| 23 |
|
| 24 |
import java.awt.*; |
| 25 |
import java.awt.geom.*; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.NotNull; |
| 28 |
import org.jetbrains.annotations.Nullable; |
| 29 |
|
| 30 |
import com.github.weisj.jsvg.attributes.value.TransformValue; |
| 31 |
import com.github.weisj.jsvg.geometry.size.FloatInsets; |
| 32 |
import com.github.weisj.jsvg.renderer.RenderContext; |
| 33 |
|
| 34 |
public final class GeometryUtil { |
| 35 |
private static final float EPS = 0.0001f; |
| 36 |
|
| 37 |
private GeometryUtil() {} |
| 38 |
|
| 39 |
public static boolean approximatelyEqual(double a, double b) { |
| 40 |
return Math.abs(a - b) < EPS; |
| 41 |
} |
| 42 |
|
| 43 |
public static boolean approximatelyZero(double a) { |
| 44 |
return approximatelyEqual(a, 0); |
| 45 |
} |
| 46 |
|
| 47 |
public static boolean notablyGreater(double a, double b) { |
| 48 |
return a - b > EPS; |
| 49 |
} |
| 50 |
|
| 51 |
public static boolean approximatelyNegative(double a) { |
| 52 |
return a < EPS; |
| 53 |
} |
| 54 |
|
| 55 |
public static boolean approximatelyPositive(double a) { |
| 56 |
return a > -EPS; |
| 57 |
} |
| 58 |
|
| 59 |
public static double scaleXOfTransform(@Nullable AffineTransform at) { |
| 60 |
if (at == null) return 1; |
| 61 |
double sx = at.getScaleX(); |
| 62 |
double shy = at.getShearY(); |
| 63 |
return Math.sqrt(sx * sx + shy * shy); |
| 64 |
} |
| 65 |
|
| 66 |
public static double scaleYOfTransform(@Nullable AffineTransform at) { |
| 67 |
if (at == null) return 1; |
| 68 |
double sy = at.getScaleY(); |
| 69 |
double shx = at.getShearX(); |
| 70 |
return Math.sqrt(sy * sy + shx * shx); |
| 71 |
} |
| 72 |
|
| 73 |
public static @NotNull Point2D.Float midPoint(@NotNull Point2D.Float x, @NotNull Point2D.Float y) { |
| 74 |
return new Point2D.Float((x.x + y.x) / 2, (x.y + y.y) / 2); |
| 75 |
} |
| 76 |
|
| 77 |
public static @NotNull Point2D.Float lerp(float t, @NotNull Point2D.Float a, @NotNull Point2D.Float b) { |
| 78 |
return new Point2D.Float(lerp(t, b.x, a.x), lerp(t, b.y, a.y)); |
| 79 |
} |
| 80 |
|
| 81 |
public static float lerp(float t, float a, float b) { |
| 82 |
return (1 - t) * a + t * b; |
| 83 |
} |
| 84 |
|
| 85 |
public static double lerp(float t, double a, double b) { |
| 86 |
return (1 - t) * a + t * b; |
| 87 |
} |
| 88 |
|
| 89 |
public static double distanceSquared(@NotNull Point2D.Float p1, @NotNull Point2D.Float p2, float scaleX, |
| 90 |
float scaleY) { |
| 91 |
return distanceSquared(scaleX * p1.x, scaleY * p1.y, scaleX * p2.x, scaleY * p2.y); |
| 92 |
} |
| 93 |
|
| 94 |
public static double distanceSquared(@NotNull Point2D.Float p1, @NotNull Point2D.Float p2) { |
| 95 |
return distanceSquared(p1.x, p1.y, p2.x, p2.y); |
| 96 |
} |
| 97 |
|
| 98 |
public static double distanceSquared(double x1, double y1, double x2, double y2) { |
| 99 |
double dx = x2 - x1; |
| 100 |
double dy = y2 - y1; |
| 101 |
return dx * dx + dy * dy; |
| 102 |
} |
| 103 |
|
| 104 |
public static double pathLength(@NotNull Shape shape) { |
| 105 |
return pathLength(shape.getPathIterator(null)); |
| 106 |
} |
| 107 |
|
| 108 |
private static double pathLength(@NotNull PathIterator pathIterator) { |
| 109 |
PathLengthCalculator pathLengthCalculator = new PathLengthCalculator(); |
| 110 |
double length = 0; |
| 111 |
double[] args = new double[6]; |
| 112 |
while (!pathIterator.isDone()) { |
| 113 |
length += pathLengthCalculator.segmentLength(pathIterator.currentSegment(args), args); |
| 114 |
pathIterator.next(); |
| 115 |
} |
| 116 |
return length; |
| 117 |
} |
| 118 |
|
| 119 |
public static boolean isSingleClosedPath(@NotNull Shape shape) { |
| 120 |
PathIterator pathIterator = shape.getPathIterator(null); |
| 121 |
int numPaths = 0; |
| 122 |
int numClosedPaths = 0; |
| 123 |
float[] segment = new float[6]; |
| 124 |
while (!pathIterator.isDone()) { |
| 125 |
switch (pathIterator.currentSegment(segment)) { |
| 126 |
case PathIterator.SEG_MOVETO: |
| 127 |
numPaths++; |
| 128 |
break; |
| 129 |
case PathIterator.SEG_CLOSE: |
| 130 |
numClosedPaths++; |
| 131 |
break; |
| 132 |
default: |
| 133 |
break; |
| 134 |
} |
| 135 |
if (numPaths > 1 || numClosedPaths > 1) return false; |
| 136 |
pathIterator.next(); |
| 137 |
} |
| 138 |
return numPaths == 1 && numClosedPaths == 1; |
| 139 |
} |
| 140 |
|
| 141 |
public static double lineLength(double x1, double y1, double x2, double y2) { |
| 142 |
return Math.sqrt(GeometryUtil.distanceSquared(x1, y1, x2, y2)); |
| 143 |
} |
| 144 |
|
| 145 |
public static @NotNull Rectangle2D containingBoundsAfterTransform(@NotNull AffineTransform transform, |
| 146 |
@NotNull Rectangle2D rect) { |
| 147 |
if (transform.isIdentity()) return rect.getBounds2D(); |
| 148 |
Point2D.Double p1 = new Point2D.Double(rect.getX(), rect.getY()); |
| 149 |
Point2D.Double p2 = new Point2D.Double(rect.getX() + rect.getWidth(), rect.getY()); |
| 150 |
Point2D.Double p3 = new Point2D.Double(rect.getX(), rect.getY() + rect.getHeight()); |
| 151 |
Point2D.Double p4 = new Point2D.Double(rect.getX() + rect.getWidth(), rect.getY() + rect.getHeight()); |
| 152 |
Rectangle2D r1 = rect.getBounds2D(); |
| 153 |
r1.setFrameFromDiagonal(transform.transform(p1, p1), transform.transform(p2, p2)); |
| 154 |
Rectangle2D r2 = rect.getBounds2D(); |
| 155 |
r2.setFrameFromDiagonal(transform.transform(p3, p3), transform.transform(p4, p4)); |
| 156 |
Rectangle2D.union(r1, r2, r1); |
| 157 |
return r1; |
| 158 |
} |
| 159 |
|
| 160 |
public static float left(@NotNull Rectangle2D rect) { |
| 161 |
return (float) rect.getX(); |
| 162 |
} |
| 163 |
|
| 164 |
public static float top(@NotNull Rectangle2D rect) { |
| 165 |
return (float) rect.getY(); |
| 166 |
} |
| 167 |
|
| 168 |
public static float right(@NotNull Rectangle2D rect) { |
| 169 |
return (float) (rect.getX() + rect.getWidth()); |
| 170 |
} |
| 171 |
|
| 172 |
public static float bottom(@NotNull Rectangle2D rect) { |
| 173 |
return (float) (rect.getY() + rect.getHeight()); |
| 174 |
} |
| 175 |
|
| 176 |
public static @NotNull Rectangle2D grow(@NotNull Rectangle2D bounds, FloatInsets grow) { |
| 177 |
return new Rectangle2D.Double( |
| 178 |
bounds.getX() - grow.left(), |
| 179 |
bounds.getY() - grow.top(), |
| 180 |
bounds.getWidth() + grow.left() + grow.right(), |
| 181 |
bounds.getHeight() + grow.top() + grow.bottom()); |
| 182 |
} |
| 183 |
|
| 184 |
public static @NotNull Rectangle2D grow(@NotNull Rectangle2D bounds, double increase) { |
| 185 |
return new Rectangle2D.Double( |
| 186 |
bounds.getX() - increase, |
| 187 |
bounds.getY() - increase, |
| 188 |
bounds.getWidth() + 2 * increase, |
| 189 |
bounds.getHeight() + 2 * increase); |
| 190 |
} |
| 191 |
|
| 192 |
public static @NotNull FloatInsets max(@NotNull FloatInsets in1, @NotNull FloatInsets in2) { |
| 193 |
return new FloatInsets( |
| 194 |
Math.max(in1.top(), in2.top()), |
| 195 |
Math.max(in1.left(), in2.left()), |
| 196 |
Math.max(in1.bottom(), in2.bottom()), |
| 197 |
Math.max(in1.right(), in2.right())); |
| 198 |
} |
| 199 |
|
| 200 |
public static @NotNull FloatInsets min(@NotNull FloatInsets in1, @NotNull FloatInsets in2) { |
| 201 |
return new FloatInsets( |
| 202 |
Math.min(in1.top(), in2.top()), |
| 203 |
Math.min(in1.left(), in2.left()), |
| 204 |
Math.min(in1.bottom(), in2.bottom()), |
| 205 |
Math.min(in1.right(), in2.right())); |
| 206 |
} |
| 207 |
|
| 208 |
public static @NotNull FloatInsets overhangInsets(@NotNull Rectangle2D reference, @NotNull Rectangle2D bounds) { |
| 209 |
return new FloatInsets( |
| 210 |
Math.max(0, top(reference) - top(bounds)), |
| 211 |
Math.max(0, left(reference) - left(bounds)), |
| 212 |
Math.max(0, bottom(bounds) - bottom(reference)), |
| 213 |
Math.max(0, right(bounds) - right(reference))); |
| 214 |
} |
| 215 |
|
| 216 |
public static @NotNull String compactRepresentation(@NotNull Rectangle2D rect) { |
| 217 |
return "[" + rect.getX() + ", " + rect.getY() + ", " + rect.getWidth() + "x" + rect.getHeight() + "]"; |
| 218 |
} |
| 219 |
|
| 220 |
public static @NotNull Rectangle2D toIntegerBounds(@NotNull Rectangle2D in, @NotNull Rectangle2D out) { |
| 221 |
double minY = Math.floor(in.getMinY()); |
| 222 |
double minX = Math.floor(in.getMinX()); |
| 223 |
double maxX = Math.ceil(in.getMaxX()); |
| 224 |
double maxY = Math.ceil(in.getMaxY()); |
| 225 |
out.setFrame(minX, minY, maxX - minX, maxY - minY); |
| 226 |
return out; |
| 227 |
} |
| 228 |
|
| 229 |
public static @NotNull Rectangle2D adjustForAliasing(@NotNull Rectangle2D r) { |
| 230 |
return toIntegerBounds(r, r); |
| 231 |
} |
| 232 |
|
| 233 |
public static @NotNull AffineTransform createInverse(@NotNull AffineTransform at) { |
| 234 |
try { |
| 235 |
return at.createInverse(); |
| 236 |
} catch (NoninvertibleTransformException e) { |
| 237 |
throw new IllegalStateException(e); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
public static @Nullable AffineTransform toAwtTransform(@NotNull RenderContext context, |
| 242 |
@Nullable TransformValue transform) { |
| 243 |
if (transform == null) return null; |
| 244 |
return transform.get(context.measureContext()); |
| 245 |
} |
| 246 |
|
| 247 |
public static @NotNull Point2D lastPointOnPath(@NotNull PathIterator pathIterator) { |
| 248 |
Point2D lastPoint = new Point2D.Double(); |
| 249 |
Point2D lastMoveToPoint = new Point2D.Double(); |
| 250 |
double[] args = new double[6]; |
| 251 |
while (!pathIterator.isDone()) { |
| 252 |
switch (pathIterator.currentSegment(args)) { |
| 253 |
case PathIterator.SEG_MOVETO: |
| 254 |
lastPoint.setLocation(args[0], args[1]); |
| 255 |
lastMoveToPoint.setLocation(args[0], args[1]); |
| 256 |
break; |
| 257 |
case PathIterator.SEG_LINETO: |
| 258 |
lastPoint.setLocation(args[0], args[1]); |
| 259 |
break; |
| 260 |
case PathIterator.SEG_QUADTO: |
| 261 |
lastPoint.setLocation(args[2], args[3]); |
| 262 |
break; |
| 263 |
case PathIterator.SEG_CUBICTO: |
| 264 |
lastPoint.setLocation(args[4], args[5]); |
| 265 |
break; |
| 266 |
case PathIterator.SEG_CLOSE: |
| 267 |
lastPoint.setLocation(lastMoveToPoint.getX(), lastMoveToPoint.getY()); |
| 268 |
break; |
| 269 |
default: |
| 270 |
break; |
| 271 |
} |
| 272 |
pathIterator.next(); |
| 273 |
} |
| 274 |
return lastPoint; |
| 275 |
} |
| 276 |
|
| 277 |
public enum Space { |
| 278 |
USER, |
| 279 |
ROOT, |
| 280 |
DEVICE |
| 281 |
} |
| 282 |
|
| 283 |
public static @NotNull Rectangle2D convertBounds(@NotNull RenderContext context, @NotNull Rectangle2D r, |
| 284 |
@NotNull Space from, @NotNull Space to) { |
| 285 |
if (from == to) return r; |
| 286 |
Rectangle2D out = r; |
| 287 |
if (from == Space.USER) { |
| 288 |
if (to == Space.ROOT) { |
| 289 |
out = containingBoundsAfterTransform(context.userSpaceTransform(), r); |
| 290 |
} else if (to == Space.DEVICE) { |
| 291 |
out = containingBoundsAfterTransform(context.userSpaceTransform(), r); |
| 292 |
out = containingBoundsAfterTransform(context.rootTransform(), out); |
| 293 |
} |
| 294 |
} |
| 295 |
if (from == Space.ROOT) { |
| 296 |
if (to == Space.USER) { |
| 297 |
out = containingBoundsAfterTransform(createInverse(context.rootTransform()), r); |
| 298 |
} else if (to == Space.DEVICE) { |
| 299 |
out = containingBoundsAfterTransform(context.rootTransform(), r); |
| 300 |
} |
| 301 |
} |
| 302 |
if (from == Space.DEVICE) { |
| 303 |
if (to == Space.USER) { |
| 304 |
out = containingBoundsAfterTransform(createInverse(context.rootTransform()), r); |
| 305 |
out = containingBoundsAfterTransform(createInverse(context.userSpaceTransform()), out); |
| 306 |
} else if (to == Space.ROOT) { |
| 307 |
out = containingBoundsAfterTransform(createInverse(context.rootTransform()), r); |
| 308 |
} |
| 309 |
} |
| 310 |
return out; |
| 311 |
} |
| 312 |
|
| 313 |
public static @NotNull Rectangle2D userBoundsToDeviceBounds(@NotNull RenderContext context, |
| 314 |
@NotNull Rectangle2D r) { |
| 315 |
return convertBounds(context, r, Space.USER, Space.DEVICE); |
| 316 |
} |
| 317 |
|
| 318 |
public static @NotNull Point2D getLocation(@NotNull Rectangle2D r) { |
| 319 |
return new Point2D.Double(r.getX(), r.getY()); |
| 320 |
} |
| 321 |
|
| 322 |
public static boolean isValidRect(@NotNull Rectangle2D r) { |
| 323 |
return !Double.isNaN(r.getX()) |
| 324 |
&& !Double.isNaN(r.getY()) |
| 325 |
&& !Double.isNaN(r.getWidth()) |
| 326 |
&& !Double.isNaN(r.getHeight()); |
| 327 |
} |
| 328 |
|
| 329 |
public static @NotNull AffineTransform interpolate(@NotNull AffineTransform a, @NotNull AffineTransform b, |
| 330 |
float t) { |
| 331 |
return TransformUtil.interpolate(a, b, t); |
| 332 |
} |
| 333 |
} |