Debian Codegraph @

src/com/github/weisj/jsvg/geometry/mesh/CoonPatch.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2023 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.geometry.mesh;
23
24 import static com.github.weisj.jsvg.geometry.mesh.MeshUtil.p;
25 import static com.github.weisj.jsvg.geometry.util.GeometryUtil.midPoint;
26
27 import java.awt.*;
28 import java.awt.geom.Path2D;
29 import java.awt.geom.Point2D;
30
31 import org.jetbrains.annotations.NotNull;
32
33 public final class CoonPatch {
34 public Bezier north;
35 public Bezier east;
36 public Bezier south;
37 public Bezier west;
38
39 public final @NotNull CoonValues coonValues;
40
41 CoonPatch(Bezier north, Bezier east, Bezier south, Bezier west) {
42 this(north, east, south, west,
43 new CoonValues(p(0, 0), p(1, 0), p(1, 1), p(0, 1)));
44 }
45
46 public static @NotNull CoonPatch createUninitialized() {
47 return new CoonPatch(null, null, null, null);
48 }
49
50 CoonPatch(Bezier north, Bezier east, Bezier south, Bezier west, @NotNull CoonValues coonValues) {
51 this.north = north;
52 this.east = east;
53 this.south = south;
54 this.west = west;
55 this.coonValues = coonValues;
56 }
57
58 public @NotNull Shape toShape() {
59 Path2D p = new Path2D.Float(Path2D.WIND_EVEN_ODD);
60 p.moveTo(north.a.x, north.a.y);
61 north.appendTo(p);
62 east.appendTo(p);
63 south.appendTo(p);
64 west.appendTo(p);
65 p.closePath();
66 return p;
67 }
68
69 public Subdivided<CoonPatch> subdivide() {
70 Point2D.Float midNorthLinear = midPoint(north.a, north.d);
71 Point2D.Float midSouthLinear = midPoint(south.d, south.a);
72 Point2D.Float midWestLinear = midPoint(north.a, south.d);
73 Point2D.Float midEastLinear = midPoint(north.d, south.a);
74
75 Split<Bezier> northSplit = north.split();
76 Split<Bezier> southSplit = south.split();
77 Split<Bezier> westSplit = west.split();
78 Split<Bezier> eastSplit = east.split();
79
80 Bezier northLeft = northSplit.left;
81 Bezier northRight = northSplit.right;
82
83 Bezier southLeft = southSplit.right;
84 Bezier southRight = southSplit.left;
85
86 @SuppressWarnings({"SuspiciousNameCombination"}) Bezier westBottom = westSplit.left;
87 @SuppressWarnings({"SuspiciousNameCombination"}) Bezier westTop = westSplit.right;
88
89 @SuppressWarnings({"SuspiciousNameCombination"}) Bezier eastTop = eastSplit.left;
90 @SuppressWarnings({"SuspiciousNameCombination"}) Bezier eastBottom = eastSplit.right;
91
92 Bezier midNorthSouth = new Bezier(
93 midPoint(north.a, south.d),
94 midPoint(north.b, south.c),
95 midPoint(north.c, south.b),
96 midPoint(north.d, south.a));
97 Bezier midEastWest = new Bezier(
98 midPoint(east.a, west.d),
99 midPoint(east.b, west.c),
100 midPoint(east.c, west.b),
101 midPoint(east.d, west.a));
102
103 Split<Bezier> splitNorthSouth = Bezier.combine(
104 midEastWest,
105 Bezier.straightLine(northLeft.d, southLeft.a),
106 Bezier.straightLine(midNorthLinear, midSouthLinear)).split();
107
108 Split<Bezier> splitWestEast = Bezier.combine(
109 midNorthSouth,
110 Bezier.straightLine(westTop.a, eastTop.d),
111 Bezier.straightLine(midWestLinear, midEastLinear)).split();
112
113 Point2D.Float midNorthValue = midPoint(coonValues.north, coonValues.east);
114 Point2D.Float midWestValue = midPoint(coonValues.north, coonValues.west);
115 Point2D.Float midSouthValue = midPoint(coonValues.west, coonValues.south);
116 Point2D.Float midEastValue = midPoint(coonValues.east, coonValues.south);
117
118 Point2D.Float gridMidValue = midPoint(midSouthValue, midNorthValue);
119 CoonValues northWestWeights = new CoonValues(coonValues.north, midNorthValue, gridMidValue, midWestValue);
120 CoonValues northEastWeights = new CoonValues(midNorthValue, coonValues.east, midEastValue, gridMidValue);
121 CoonValues southWestWeights = new CoonValues(midWestValue, gridMidValue, midSouthValue, coonValues.west);
122 CoonValues southEastWeights = new CoonValues(gridMidValue, midEastValue, coonValues.south, midSouthValue);
123
124 CoonPatch northWest = new CoonPatch(
125 northLeft, splitNorthSouth.left, splitWestEast.left.inverse(), westTop,
126 northWestWeights);
127 CoonPatch northEast = new CoonPatch(
128 northRight, eastTop, splitWestEast.right.inverse(), splitNorthSouth.left.inverse(),
129 northEastWeights);
130 CoonPatch southWest = new CoonPatch(
131 splitWestEast.left, splitNorthSouth.right, southLeft, westBottom,
132 southWestWeights);
133 CoonPatch southEast = new CoonPatch(
134 splitWestEast.right, eastBottom, southRight, splitNorthSouth.right.inverse(),
135 southEastWeights);
136 return new Subdivided<>(northWest, northEast, southWest, southEast);
137 }
138
139 @Override
140 public String toString() {
141 return "CoonPatch{" +
142 "north=" + north +
143 ", east=" + east +
144 ", south=" + south +
145 ", west=" + west +
146 ", coonValues=" + coonValues +
147 '}';
148 }
149 }