| •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 |
|
| 25 |
import java.awt.geom.PathIterator; |
| 26 |
|
| 27 |
|
| 28 |
public final class PathLengthCalculator { |
| 29 |
|
| 30 |
private double x = 0; |
| 31 |
private double y = 0; |
| 32 |
private double xStart = x; |
| 33 |
private double yStart = y; |
| 34 |
|
| 35 |
public double segmentLength(int segmentType, double[] coords) { |
| 36 |
double segmentLength = 0; |
| 37 |
switch (segmentType) { |
| 38 |
case PathIterator.SEG_MOVETO: |
| 39 |
x = coords[0]; |
| 40 |
y = coords[1]; |
| 41 |
xStart = x; |
| 42 |
yStart = y; |
| 43 |
break; |
| 44 |
case PathIterator.SEG_LINETO: |
| 45 |
segmentLength = lineLength(x, y, coords[0], coords[1]); |
| 46 |
x = coords[0]; |
| 47 |
y = coords[1]; |
| 48 |
break; |
| 49 |
case PathIterator.SEG_QUADTO: |
| 50 |
segmentLength = quadraticParametricLength(x, y, coords[0], coords[1], coords[2], coords[3]); |
| 51 |
x = coords[2]; |
| 52 |
y = coords[3]; |
| 53 |
break; |
| 54 |
case PathIterator.SEG_CUBICTO: |
| 55 |
segmentLength = |
| 56 |
cubicParametricLength(x, y, coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); |
| 57 |
x = coords[4]; |
| 58 |
y = coords[5]; |
| 59 |
break; |
| 60 |
case PathIterator.SEG_CLOSE: |
| 61 |
segmentLength = lineLength(x, y, xStart, yStart); |
| 62 |
x = xStart; |
| 63 |
y = yStart; |
| 64 |
break; |
| 65 |
default: |
| 66 |
throw new IllegalStateException(); |
| 67 |
} |
| 68 |
return segmentLength; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
private double lineLength(double x1, double y1, double x2, double y2) { |
| 73 |
return GeometryUtil.lineLength(x1, y1, x2, y2); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
private double quadraticParametricLength(double ax, double ay, double bx, double by, double cx, double cy) { |
| 85 |
|
| 86 |
if (ax == cx && ay == cy) { |
| 87 |
|
| 88 |
if (ax == bx && ay == by) return 0; |
| 89 |
return lineLength(ax, ay, bx, by); |
| 90 |
} |
| 91 |
|
| 92 |
if ((ax == bx && ay == by) |
| 93 |
|| (cx == bx && cy == by)) { |
| 94 |
return lineLength(ax, ay, cx, cy); |
| 95 |
} |
| 96 |
double ax0 = bx - ax; |
| 97 |
double ay0 = by - ay; |
| 98 |
|
| 99 |
double ax1 = ax - 2 * bx + cx; |
| 100 |
double ay1 = ay - 2 * by + cy; |
| 101 |
|
| 102 |
if (ax1 != 0 || ay1 != 0) { |
| 103 |
double c = 4f * dot2D(ax1, ay1, ax1, ay1); |
| 104 |
double b = 8f * dot2D(ax0, ay0, ax1, ay1); |
| 105 |
double a = 4f * dot2D(ax0, ay0, ax0, ay0); |
| 106 |
double q = 4f * a * c - b * b; |
| 107 |
double twoCpB = 2.0 * c + b; |
| 108 |
double sumCBA = c + b + a; |
| 109 |
double l0 = (0.25 / c) * (twoCpB * Math.sqrt(sumCBA) - b * Math.sqrt(a)); |
| 110 |
if (q == 0.0) return l0; |
| 111 |
double l1 = (q / (8.0 * Math.pow(c, 1.5))) |
| 112 |
* (Math.log(2.0 * Math.sqrt(c * sumCBA) + twoCpB) - Math.log(2.0 * Math.sqrt(c * a) + b)); |
| 113 |
return l0 + l1; |
| 114 |
} else { |
| 115 |
return 2f * lineLength(0, 0, ax0, ay0); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
private double dot2D(double x1, double y1, double x2, double y2) { |
| 120 |
return x1 * x2 + y1 * y2; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
. |
| 126 |
|
| 127 |
²] |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
private double cubicParametricLength(double ax, double ay, double bx, double by, double cx, double cy, |
| 135 |
double dx, double dy) { |
| 136 |
|
| 137 |
final double[] nodes = { |
| 138 |
-0.9815606342467192, -0.9041172563704749, -0.7699026741943047, |
| 139 |
-0.5873179542866175, -0.3678314989981802, -0.1252334085114689, |
| 140 |
0.1252334085114689, 0.3678314989981802, 0.5873179542866175, |
| 141 |
0.7699026741943047, 0.9041172563704749, 0.9815606342467192 |
| 142 |
}; |
| 143 |
|
| 144 |
final double[] weights = { |
| 145 |
0.0471753363865118, 0.1069393259953184, 0.1600783285433462, |
| 146 |
0.2031674267230659, 0.2334925365383548, 0.2491470458134028, |
| 147 |
0.2491470458134028, 0.2334925365383548, 0.2031674267230659, |
| 148 |
0.1600783285433462, 0.1069393259953184, 0.0471753363865118 |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
double a1x = bx - ax; |
| 154 |
double a1y = by - ay; |
| 155 |
double a2x = cx - bx; |
| 156 |
double a2y = cy - by; |
| 157 |
double a3x = dx - cx; |
| 158 |
double a3y = dy - cy; |
| 159 |
|
| 160 |
double sum = 0; |
| 161 |
for (int i = 0; i < nodes.length; i++) { |
| 162 |
|
| 163 |
double t = (nodes[i] + 1.0) * 0.5; |
| 164 |
double mt = 1.0 - t; |
| 165 |
|
| 166 |
double dtx = a1x * mt * mt + 2.0 * a2x * mt * t + a3x * t * t; |
| 167 |
double dty = a1y * mt * mt + 2.0 * a2y * mt * t + a3y * t * t; |
| 168 |
sum += weights[i] * Math.sqrt(dtx * dtx + dty * dty); |
| 169 |
} |
| 170 |
|
| 171 |
return 3.0 * 0.5 * sum; |
| 172 |
} |
| 173 |
} |