2015-04-12 16:25:03 +02:00
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
|
2015-04-13 04:33:20 +02:00
|
|
|
|
import org.newdawn.slick.SlickException;
|
|
|
|
|
|
2015-04-12 16:25:03 +02:00
|
|
|
|
/**
|
|
|
|
|
* Representation of a Centripetal Catmull–Rom spline.
|
2015-04-13 04:33:20 +02:00
|
|
|
|
* (Currently not technically Centripetal Catmull–Rom.)
|
2015-04-12 16:25:03 +02:00
|
|
|
|
* http://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
|
2015-04-13 04:33:20 +02:00
|
|
|
|
*
|
2015-04-12 16:25:03 +02:00
|
|
|
|
* @author fluddokt (https://github.com/fluddokt)
|
|
|
|
|
*/
|
2015-04-13 04:33:20 +02:00
|
|
|
|
public class CentripetalCatmullRom extends CurveType {
|
2015-04-12 19:19:33 +02:00
|
|
|
|
/** The time values of the Catmull curve. */
|
2015-04-13 04:33:20 +02:00
|
|
|
|
private float [] time;
|
|
|
|
|
|
2015-04-12 19:19:33 +02:00
|
|
|
|
/** The control points of the Catmull curve. */
|
2015-04-13 04:33:20 +02:00
|
|
|
|
private Vec2f[] points;
|
|
|
|
|
|
2015-04-12 19:19:33 +02:00
|
|
|
|
/**
|
|
|
|
|
* Constructor.
|
2015-04-13 04:33:20 +02:00
|
|
|
|
* @param points the control points of the curve
|
|
|
|
|
* @throws SlickException
|
2015-04-12 19:19:33 +02:00
|
|
|
|
*/
|
2015-04-13 04:33:20 +02:00
|
|
|
|
protected CentripetalCatmullRom(Vec2f[] points) throws SlickException {
|
2015-04-12 16:25:03 +02:00
|
|
|
|
if (points.length != 4)
|
2015-04-13 04:33:20 +02:00
|
|
|
|
throw new SlickException(String.format("Need exactly 4 points to initialize CentripetalCatmullRom, %d provided.", points.length));
|
2015-04-12 19:19:33 +02:00
|
|
|
|
|
2015-04-12 16:25:03 +02:00
|
|
|
|
this.points = points;
|
|
|
|
|
time = new float[4];
|
|
|
|
|
time[0] = 0;
|
|
|
|
|
float approxLength = 0;
|
2015-04-12 19:19:33 +02:00
|
|
|
|
for (int i = 1; i < 4; i++) {
|
2015-04-12 16:25:03 +02:00
|
|
|
|
float len = 0;
|
2015-04-12 19:19:33 +02:00
|
|
|
|
if (i > 0)
|
|
|
|
|
len = points[i].cpy().sub(points[i - 1]).len();
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
len += 0.0001f;
|
2015-04-12 16:25:03 +02:00
|
|
|
|
approxLength += len;
|
2015-04-12 19:19:33 +02:00
|
|
|
|
// time[i] = (float) Math.sqrt(len) + time[i-1];// ^(0.5)
|
2015-04-12 16:25:03 +02:00
|
|
|
|
time[i] = i;
|
|
|
|
|
}
|
2015-04-13 04:33:20 +02:00
|
|
|
|
|
2015-04-12 19:19:33 +02:00
|
|
|
|
init(approxLength / 2);
|
2015-04-12 16:25:03 +02:00
|
|
|
|
}
|
2015-04-12 19:19:33 +02:00
|
|
|
|
|
|
|
|
|
@Override
|
2015-04-13 04:33:20 +02:00
|
|
|
|
public Vec2f pointAt(float t) {
|
2015-04-12 19:19:33 +02:00
|
|
|
|
t = t * (time[2] - time[1]) + time[1];
|
2015-04-12 16:25:03 +02:00
|
|
|
|
|
2015-04-12 19:19:33 +02:00
|
|
|
|
Vec2f A1 = points[0].cpy().scale((time[1] - t) / (time[1] - time[0]))
|
|
|
|
|
.add(points[1].cpy().scale((t - time[0]) / (time[1] - time[0])));
|
|
|
|
|
Vec2f A2 = points[1].cpy().scale((time[2] - t) / (time[2] - time[1]))
|
|
|
|
|
.add(points[2].cpy().scale((t - time[1]) / (time[2] - time[1])));
|
|
|
|
|
Vec2f A3 = points[2].cpy().scale((time[3] - t) / (time[3] - time[2]))
|
|
|
|
|
.add(points[3].cpy().scale((t - time[2]) / (time[3] - time[2])));
|
|
|
|
|
|
|
|
|
|
Vec2f B1 = A1.cpy().scale((time[2] - t) / (time[2] - time[0]))
|
|
|
|
|
.add(A2.cpy().scale((t - time[0]) / (time[2] - time[0])));
|
|
|
|
|
Vec2f B2 = A2.cpy().scale((time[3] - t) / (time[3] - time[1]))
|
|
|
|
|
.add(A3.cpy().scale((t - time[1]) / (time[3] - time[1])));
|
|
|
|
|
|
|
|
|
|
Vec2f C = B1.cpy().scale((time[2] - t) / (time[2] - time[1]))
|
|
|
|
|
.add(B2.cpy().scale((t - time[1]) / (time[2] - time[1])));
|
2015-04-13 04:33:20 +02:00
|
|
|
|
|
2015-04-12 16:25:03 +02:00
|
|
|
|
return C;
|
|
|
|
|
}
|
|
|
|
|
}
|