opsu-dance/src/itdelatrisu/opsu/objects/curves/EqualDistanceMultiCurve.java

149 lines
4.6 KiB
Java
Raw Normal View History

/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu! is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
*/
package itdelatrisu.opsu.objects.curves;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.beatmap.HitObject;
import java.util.Iterator;
import java.util.LinkedList;
/**
2015-04-12 19:19:33 +02:00
* Representation of multiple curve with equidistant points.
* http://pomax.github.io/bezierinfo/#tracing
*
* @author fluddokt (https://github.com/fluddokt)
*/
public abstract class EqualDistanceMultiCurve extends Curve {
/** The angles of the first and last control points for drawing. */
private float startAngle, endAngle;
/** The number of points along the curve. */
private int ncurve;
/**
* Constructor.
* @param hitObject the associated HitObject
*/
public EqualDistanceMultiCurve(HitObject hitObject) {
this(hitObject, true);
}
/**
* Constructor.
* @param hitObject the associated HitObject
* @param scaled whether to use scaled coordinates
*/
public EqualDistanceMultiCurve(HitObject hitObject, boolean scaled) {
super(hitObject, scaled);
}
2015-04-12 19:19:33 +02:00
/**
* Initialize the curve points with equal distance.
* Must be called by inherited classes.
2015-04-12 19:19:33 +02:00
* @param curvesList a list of curves to join
*/
public void init(LinkedList<CurveType> curvesList){
// now try to creates points the are equidistant to each other
this.ncurve = (int) (hitObject.getPixelLength() / CURVE_POINTS_SEPERATION);
this.curve = new Vec2f[ncurve + 1];
float distanceAt = 0;
2015-04-12 19:19:33 +02:00
Iterator<CurveType> iter = curvesList.iterator();
int curPoint = 0;
2015-04-12 19:19:33 +02:00
CurveType curCurve = iter.next();
Vec2f lastCurve = curCurve.getCurvePoint()[0];
float lastDistanceAt = 0;
2015-04-12 19:19:33 +02:00
// length of Curve should equal pixel length (in 640x480)
float pixelLength = hitObject.getPixelLength() * HitObject.getXMultiplier();
// for each distance, try to get in between the two points that are between it
for (int i = 0; i < ncurve + 1; i++) {
int prefDistance = (int) (i * pixelLength / ncurve);
while (distanceAt < prefDistance) {
lastDistanceAt = distanceAt;
2015-04-12 19:19:33 +02:00
lastCurve = curCurve.getCurvePoint()[curPoint];
2015-04-14 04:56:07 +02:00
curPoint++;
2015-04-12 19:19:33 +02:00
if (curPoint >= curCurve.getCurvesCount()) {
if (iter.hasNext()) {
2015-04-12 19:19:33 +02:00
curCurve = iter.next();
curPoint = 0;
} else {
2015-04-12 19:19:33 +02:00
curPoint = curCurve.getCurvesCount() - 1;
if (lastDistanceAt == distanceAt) {
// out of points even though the preferred distance hasn't been reached
break;
}
}
}
2015-04-14 04:56:07 +02:00
distanceAt += curCurve.getCurveDistances()[curPoint];
}
2015-04-12 19:19:33 +02:00
Vec2f thisCurve = curCurve.getCurvePoint()[curPoint];
// interpolate the point between the two closest distances
if (distanceAt - lastDistanceAt > 1) {
float t = (prefDistance - lastDistanceAt) / (distanceAt - lastDistanceAt);
curve[i] = new Vec2f(Utils.lerp(lastCurve.x, thisCurve.x, t), Utils.lerp(lastCurve.y, thisCurve.y, t));
} else
curve[i] = thisCurve;
}
// if (hitObject.getRepeatCount() > 1) {
Vec2f c1 = curve[0];
int cnt = 1;
Vec2f c2 = curve[cnt++];
while (cnt <= ncurve && c2.cpy().sub(c1).len() < 1)
c2 = curve[cnt++];
this.startAngle = (float) (Math.atan2(c2.y - c1.y, c2.x - c1.x) * 180 / Math.PI);
c1 = curve[ncurve];
cnt = ncurve - 1;
c2 = curve[cnt--];
while (cnt >= 0 && c2.cpy().sub(c1).len() < 1)
c2 = curve[cnt--];
this.endAngle = (float) (Math.atan2(c2.y - c1.y, c2.x - c1.x) * 180 / Math.PI);
// }
}
@Override
public Vec2f pointAt(float t) {
float indexF = t * ncurve;
int index = (int) indexF;
if (index >= ncurve)
return curve[ncurve].cpy();
else {
Vec2f poi = curve[index];
Vec2f poi2 = curve[index + 1];
float t2 = indexF - index;
return new Vec2f(
Utils.lerp(poi.x, poi2.x, t2),
Utils.lerp(poi.y, poi2.y, t2)
);
}
}
@Override
public float getEndAngle() { return endAngle; }
@Override
public float getStartAngle() { return startAngle; }
}