| •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.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 |
} |