-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd866eb
commit a9ad196
Showing
5 changed files
with
240 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/kasuga/lib/core/client/render/curve/CatmullromCurveTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package kasuga.lib.core.client.render.curve; | ||
|
||
import com.google.common.collect.Lists; | ||
import kasuga.lib.core.client.model.anim_json.CatmullRomUtils; | ||
import kasuga.lib.core.client.render.texture.Vec2f; | ||
import kasuga.lib.core.util.data_type.Pair; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class CatmullromCurveTemplate implements CurveTemplate { | ||
|
||
private final List<Pair<Vec2f, Vec2f[]>> anchors; | ||
private final List<Vec2f> points; | ||
private float left, right, step; | ||
public CatmullromCurveTemplate(float left, float right, float step, | ||
List<Vec2f> anchors) { | ||
this.anchors = new ArrayList<>(anchors.isEmpty() ? 0 : anchors.size() - 1); | ||
this.points = Lists.newArrayList(); | ||
this.left = left; | ||
this.right = right; | ||
this.step = step; | ||
boolean flag = compileAnchors(anchors, false); | ||
if (flag) compileCurve(); else compileLine(); | ||
} | ||
|
||
public boolean compileAnchors(List<Vec2f> anchors, boolean shouldClear) { | ||
if (shouldClear) this.anchors.clear(); | ||
if (anchors.size() < 3) return false; | ||
for (int i = 0; i < anchors.size() - 1; i++) { | ||
Vec2f p1 = anchors.get(i); | ||
Vec2f p2 = anchors.get(i + 1); | ||
Vec2f[] controllers; | ||
if (i == 0) { | ||
Vec2f p3 = anchors.get(i + 2); | ||
controllers = CatmullRomUtils.last3PointsToCRSPoints(p1, p2, p3); | ||
} else if (i == anchors.size() - 2) { | ||
Vec2f p0 = anchors.get(i - 1); | ||
controllers = CatmullRomUtils.first3PointsToCRSPoints(p0, p1, p2); | ||
} else { | ||
Vec2f p0 = anchors.get(i - 1); | ||
Vec2f p3 = anchors.get(i + 2); | ||
controllers = CatmullRomUtils.genDefaultCRSPoints(p0, p1, p2, p3); | ||
} | ||
this.anchors.add(Pair.of(p1, controllers)); | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<Vec2f> getPointList() { | ||
return points; | ||
} | ||
|
||
public void compileCurve() { | ||
points.clear(); | ||
float length = right - left; | ||
if (length * step < 0) return; | ||
Vec2f p0, p1; | ||
for (int i = 0; i < anchors.size() - 1; i++) { | ||
p0 = anchors.get(i).getFirst(); | ||
p1 = anchors.get(i + 1).getFirst(); | ||
float l = Math.min(left, right); | ||
float r = Math.max(left, right); | ||
if (p0.x() < l && p1.x() < l) continue; | ||
if (p0.x() > r && p1.x() > r) continue; | ||
float pl = Math.min(p0.x(), p1.x()); | ||
float pr = Math.max(p0.x(), p1.x()); | ||
} | ||
} | ||
|
||
public void compileLine() { | ||
|
||
} | ||
|
||
@Override | ||
public Pair<Float, Float> getRange() { | ||
return Pair.of(left, right); | ||
} | ||
|
||
@Override | ||
public void setLeft(float left) { | ||
this.left = left; | ||
} | ||
|
||
@Override | ||
public void setRight(float right) { | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
public float getStep() { | ||
return step; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/kasuga/lib/core/client/render/curve/CurveTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package kasuga.lib.core.client.render.curve; | ||
|
||
import kasuga.lib.core.client.render.texture.Vec2f; | ||
import kasuga.lib.core.util.data_type.Pair; | ||
|
||
import java.util.List; | ||
|
||
public interface CurveTemplate { | ||
List<Vec2f> getPointList(); | ||
Pair<Float, Float> getRange(); | ||
|
||
default Float getLeft() { | ||
return getRange().getFirst(); | ||
} | ||
|
||
default Float getRight() { | ||
return getRange().getSecond(); | ||
} | ||
void setLeft(float left); | ||
void setRight(float right); | ||
|
||
float getStep(); | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/kasuga/lib/core/client/render/curve/FunctionCurveTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package kasuga.lib.core.client.render.curve; | ||
|
||
import com.google.common.collect.Lists; | ||
import interpreter.Code; | ||
import interpreter.compute.data.Namespace; | ||
import interpreter.compute.infrastructure.Formula; | ||
import kasuga.lib.KasugaLib; | ||
import kasuga.lib.core.client.render.texture.Vec2f; | ||
import kasuga.lib.core.util.data_type.Pair; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FunctionCurveTemplate implements CurveTemplate { | ||
private final Namespace namespace; | ||
private Formula formula; | ||
private @NotNull String independentVar; | ||
private float left, right, step; | ||
private final List<Vec2f> cache; | ||
public FunctionCurveTemplate(String function, float left, float right, float step) { | ||
this.namespace = new Namespace(Code.root()); | ||
formula = namespace.decodeFormula(function); | ||
this.left = left; | ||
this.right = right; | ||
this.step = step; | ||
this.cache = Lists.newArrayList(); | ||
independentVar = "x"; | ||
} | ||
|
||
public void setIndependentVar(@NotNull String independentVar) { | ||
this.independentVar = independentVar; | ||
} | ||
|
||
public void setLeft(float left) { | ||
this.left = left; | ||
} | ||
|
||
public void setRight(float right) { | ||
this.right = right; | ||
} | ||
|
||
public void setStep(float step) { | ||
this.step = step; | ||
} | ||
|
||
public void setFunction(String formula) { | ||
this.formula = namespace.decodeFormula(formula); | ||
} | ||
|
||
@Override | ||
public List<Vec2f> getPointList() { | ||
return cache; | ||
} | ||
|
||
public void compile() { | ||
cache.clear(); | ||
float length = right - left; | ||
if (length * step < 0) { | ||
KasugaLib.MAIN_LOGGER.error("Predictions are not likely to be fulfilled."); | ||
return; | ||
} | ||
boolean flag = length < 0; | ||
for (float i = left; flag ? (i > this.right) : (i < right); i += step) { | ||
namespace.assign(independentVar, i); | ||
float result = formula.getResult(); | ||
getPointList().add(new Vec2f(i, result)); | ||
} | ||
} | ||
|
||
@Override | ||
public float getStep() { | ||
return step; | ||
} | ||
|
||
@Override | ||
public Pair<Float, Float> getRange() { | ||
return Pair.of(left, right); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters