Debian Codegraph @

src/com/github/weisj/jsvg/nodes/mesh/MeshPatch.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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({ /* None */})
44 @PermittedContent(
45 categories = Category.Descriptive,
46 anyOf = {Stop.class /* <script> */}
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 // Check if we have reached the limit of discernible colors. This happens if our color weights
83 // spectrum allows for less that approximately (1/255)^3, which is our "relative color-depth".
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 }