| •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.nodes.mesh; |
| 23 |
|
| 24 |
import static com.github.weisj.jsvg.geometry.util.GeometryUtil.lerp; |
| 25 |
|
| 26 |
import java.awt.*; |
| 27 |
import java.awt.geom.AffineTransform; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.geometry.mesh.CoonPatch; |
| 32 |
import com.github.weisj.jsvg.geometry.mesh.CoonValues; |
| 33 |
import com.github.weisj.jsvg.geometry.mesh.Subdivided; |
| 34 |
import com.github.weisj.jsvg.geometry.util.GeometryUtil; |
| 35 |
import com.github.weisj.jsvg.nodes.Stop; |
| 36 |
import com.github.weisj.jsvg.nodes.container.ContainerNode; |
| 37 |
import com.github.weisj.jsvg.nodes.prototype.spec.Category; |
| 38 |
import com.github.weisj.jsvg.nodes.prototype.spec.ElementCategories; |
| 39 |
import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent; |
| 40 |
import com.github.weisj.jsvg.renderer.output.Output; |
| 41 |
import com.github.weisj.jsvg.util.ColorUtil; |
| 42 |
|
| 43 |
@ElementCategories({ }) |
| 44 |
@PermittedContent( |
| 45 |
categories = Category.Descriptive, |
| 46 |
anyOf = {Stop.class } |
| 47 |
) |
| 48 |
public final class MeshPatch extends ContainerNode { |
| 49 |
public static final String TAG = "meshpatch"; |
| 50 |
private static final int MAX_DEPTH = 10; |
| 51 |
|
| 52 |
Color north; |
| 53 |
Color east; |
| 54 |
Color south; |
| 55 |
Color west; |
| 56 |
final @NotNull CoonPatch coonPatch = CoonPatch.createUninitialized(); |
| 57 |
|
| 58 |
@Override |
| 59 |
public @NotNull String tagName() { |
| 60 |
return TAG; |
| 61 |
} |
| 62 |
|
| 63 |
public void renderPath(@NotNull Output output) { |
| 64 |
if (!output.supportsColors()) { |
| 65 |
output.fillShape(coonPatch.toShape()); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
AffineTransform at = output.transform(); |
| 70 |
float scaleX = (float) GeometryUtil.scaleXOfTransform(at); |
| 71 |
float scaleY = (float) GeometryUtil.scaleYOfTransform(at); |
| 72 |
int depth = Math.max( |
| 73 |
Math.max(coonPatch.north.estimateStepCount(scaleX, scaleY), |
| 74 |
coonPatch.east.estimateStepCount(scaleX, scaleY)), |
| 75 |
Math.max(coonPatch.south.estimateStepCount(scaleX, scaleY), |
| 76 |
coonPatch.west.estimateStepCount(scaleX, scaleY))); |
| 77 |
renderPath(output, coonPatch, scaleX, scaleY, Math.min(MAX_DEPTH, depth)); |
| 78 |
} |
| 79 |
|
| 80 |
private void renderPath(@NotNull Output output, @NotNull CoonPatch patch, float scaleX, float scaleY, int depth) { |
| 81 |
CoonValues weights = patch.coonValues; |
| 82 |
|
| 83 |
|
| 84 |
if (depth == 0 || GeometryUtil.distanceSquared(weights.north, weights.south, scaleX, scaleY) |
| 85 |
* GeometryUtil.distanceSquared(weights.east, weights.west, scaleX, scaleY) < 0.000001) { |
| 86 |
float u = (weights.north.x + weights.east.x + weights.south.x + weights.west.x) / 4; |
| 87 |
float v = (weights.north.y + weights.east.y + weights.south.y + weights.west.y) / 4; |
| 88 |
output.setPaint(bilinearInterpolation(u, v)); |
| 89 |
Shape s = patch.toShape(); |
| 90 |
output.fillShape(s.getBounds2D()); |
| 91 |
} else { |
| 92 |
Subdivided<CoonPatch> patchSubdivided = patch.subdivide(); |
| 93 |
renderPath(output, patchSubdivided.northWest, scaleX, scaleY, depth - 1); |
| 94 |
renderPath(output, patchSubdivided.northEast, scaleX, scaleY, depth - 1); |
| 95 |
renderPath(output, patchSubdivided.southEast, scaleX, scaleY, depth - 1); |
| 96 |
renderPath(output, patchSubdivided.southWest, scaleX, scaleY, depth - 1); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
private @NotNull Color bilinearInterpolation(float dx, float dy) { |
| 101 |
float r = lerp(dy, lerp(dx, north.getRed(), east.getRed()), lerp(dx, west.getRed(), south.getRed())); |
| 102 |
float g = lerp(dy, lerp(dx, north.getGreen(), east.getGreen()), lerp(dx, west.getGreen(), south.getGreen())); |
| 103 |
float b = lerp(dy, lerp(dx, north.getBlue(), east.getBlue()), lerp(dx, west.getBlue(), south.getBlue())); |
| 104 |
float a = lerp(dy, lerp(dx, north.getAlpha(), east.getAlpha()), lerp(dx, west.getAlpha(), south.getAlpha())); |
| 105 |
return new Color(clampColor(r), clampColor(g), clampColor(b), clampColor(a)); |
| 106 |
} |
| 107 |
|
| 108 |
private int clampColor(float v) { |
| 109 |
return ColorUtil.clampColor((int) v); |
| 110 |
} |
| 111 |
} |