OSDN Git Service

Remove usage of Vector#tmp() in Bezier and BSpline
authorXoppa <contact@xoppa.nl>
Tue, 12 Feb 2013 20:48:26 +0000 (21:48 +0100)
committerXoppa <contact@xoppa.nl>
Tue, 12 Feb 2013 20:48:26 +0000 (21:48 +0100)
gdx/src/com/badlogic/gdx/math/BSpline.java
gdx/src/com/badlogic/gdx/math/Bezier.java

index 7000793..df3c89a 100644 (file)
@@ -1,7 +1,24 @@
+/*******************************************************************************
+ * Copyright 2011 See AUTHORS file.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+
 package com.badlogic.gdx.math;
 
 import com.badlogic.gdx.utils.Array;
 
+/** @author Xoppa */
 public class BSpline<T extends Vector<T>> implements Path<T> {
        private final static float d6 = 1f / 6f;
        
@@ -10,13 +27,14 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
         * @param t The position (0<=t<=1) on the spline
         * @param points The control points
         * @param continuous If true the b-spline restarts at 0 when reaching 1
+        * @param tmp A temporary vector used for the calculation
         * @return The value of out */
-       public static <T extends Vector<T>> T cubic(final T out, final float t, final T[] points, final boolean continuous) {
+       public static <T extends Vector<T>> T cubic(final T out, final float t, final T[] points, final boolean continuous, final T tmp) {
                final int n = continuous ? points.length : points.length - 3;
                float u = t * n;
                int i = (t >= 1f) ? (n - 1) : (int)u;
                u -= (float)i;
-               return cubic(out, i, u, points, continuous);
+               return cubic(out, i, u, points, continuous, tmp);
        }
        
        /** Calculates the cubic b-spline value for the given span (i) at the given position (u).
@@ -25,16 +43,17 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
         * @param u The position (0<=u<=1) on the span
         * @param points The control points
         * @param continuous If true the b-spline restarts at 0 when reaching 1
+        * @param tmp A temporary vector used for the calculation
         * @return The value of out */
-       public static <T extends Vector<T>> T cubic(final T out, final int i, final float u, final T[] points, final boolean continuous) {
+       public static <T extends Vector<T>> T cubic(final T out, final int i, final float u, final T[] points, final boolean continuous, final T tmp) {
                final int n = points.length;
                final float dt = 1f - u;
                final float t2 = u * u;
                final float t3 = t2 * u;
                out.set(points[i]).mul((3f * t3 - 6f * t2 + 4f) * d6);
-               if (continuous || i > 0) out.add(points[(n+i-1)%n].tmp().mul(dt * dt * dt * d6));
-               if (continuous || i < (n - 1)) out.add(points[(i + 1)%n].tmp().mul((-3f * t3 + 3f * t2 + 3f * u + 1f) * d6));
-               if (continuous || i < (n - 2)) out.add(points[(i + 2)%n].tmp().mul(t3 * d6));
+               if (continuous || i > 0) out.add(tmp.set(points[(n+i-1)%n]).mul(dt * dt * dt * d6));
+               if (continuous || i < (n - 1)) out.add(tmp.set(points[(i + 1)%n]).mul((-3f * t3 + 3f * t2 + 3f * u + 1f) * d6));
+               if (continuous || i < (n - 2)) out.add(tmp.set(points[(i + 2)%n]).mul(t3 * d6));
                return out;
        }
        
@@ -44,13 +63,14 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
         * @param points The control points
         * @param degree The degree of the b-spline
         * @param continuous If true the b-spline restarts at 0 when reaching 1
+        * @param tmp A temporary vector used for the calculation
         * @return The value of out */
-       public static <T extends Vector<T>> T calculate(final T out, final float t, final T[] points, final int degree, final boolean continuous) {
+       public static <T extends Vector<T>> T calculate(final T out, final float t, final T[] points, final int degree, final boolean continuous, final T tmp) {
                final int n = continuous ? points.length : points.length - degree;
                float u = t * n;
                int i = (t >= 1f) ? (n - 1) : (int)u;
                u -= (float)i;
-               return calculate(out, i, u, points, degree, continuous);
+               return calculate(out, i, u, points, degree, continuous, tmp);
        }
        
        /** Calculates the n-degree b-spline value for the given span (i) at the given position (u).
@@ -60,10 +80,11 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
         * @param points The control points
         * @param degree The degree of the b-spline
         * @param continuous If true the b-spline restarts at 0 when reaching 1
+        * @param tmp A temporary vector used for the calculation
         * @return The value of out */
-       public static <T extends Vector<T>> T calculate(final T out, final int i, final float u, final T[] points, final int degree, final boolean continuous) {
+       public static <T extends Vector<T>> T calculate(final T out, final int i, final float u, final T[] points, final int degree, final boolean continuous, final T tmp) {
                switch(degree) {
-               case 3: return cubic(out, i, u, points, continuous);
+               case 3: return cubic(out, i, u, points, continuous, tmp);
                }
                return out;
        }
@@ -73,6 +94,7 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
        public int degree;
        public boolean continuous;
        public int spanCount;
+       private T tmp;
        
        public BSpline() { }
        public BSpline(final T[] controlPoints, final int degree, final boolean continuous) {
@@ -80,6 +102,8 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
        }
        
        public BSpline set(final T[] controlPoints, final int degree, final boolean continuous) {
+               if (tmp == null)
+                       tmp = controlPoints[0].cpy();
                this.controlPoints = controlPoints;
                this.degree = degree;
                this.continuous = continuous;
@@ -91,7 +115,7 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
                        knots.ensureCapacity(spanCount);
                }
                for (int i = 0; i < spanCount; i++)
-                       knots.add(calculate(controlPoints[0].cpy(), continuous ? i : (int)(i + 0.5f * degree), 0f, controlPoints, degree, continuous));
+                       knots.add(calculate(controlPoints[0].cpy(), continuous ? i : (int)(i + 0.5f * degree), 0f, controlPoints, degree, continuous, tmp));
                return this;
        }
 
@@ -106,7 +130,7 @@ public class BSpline<T extends Vector<T>> implements Path<T> {
        
        /** @return The value of the spline at position u of the specified span */ 
        public T valueAt(final T out, final int span, final float u) {
-               return calculate(out, continuous ? span : (span + (int)(degree*0.5f)), u, controlPoints, degree, continuous);
+               return calculate(out, continuous ? span : (span + (int)(degree*0.5f)), u, controlPoints, degree, continuous, tmp);
        }
        
        /** @return The span closest to the specified value */ 
index 3cc94f8..7a5c6ab 100644 (file)
@@ -29,9 +29,10 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
         * @param t The location (ranging 0..1) on the line.
         * @param p0 The start point.
         * @param p1 The end point.
+        * @param tmp A temporary vector to be used by the calculation.
         * @return The value specified by out for chaining */
-       public static <T extends Vector<T>> T linear(final T out, final float t, final T p0, final T p1) {
-               return out.set(p0).mul(1f - t).add(p1.tmp().mul(t)); // Could just use lerp...
+       public static <T extends Vector<T>> T linear(final T out, final float t, final T p0, final T p1, final T tmp) {
+               return out.set(p0).mul(1f - t).add(tmp.set(p1).mul(t)); // Could just use lerp...
        }
        
        /** Quadratic Bezier curve 
@@ -40,10 +41,11 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
         * @param p0 The first bezier point.
         * @param p1 The second bezier point.
         * @param p2 The third bezier point.
+        * @param tmp A temporary vector to be used by the calculation.
         * @return The value specified by out for chaining */
-       public static <T extends Vector<T>> T quadratic(final T out, final float t, final T p0, final T p1, final T p2) {
+       public static <T extends Vector<T>> T quadratic(final T out, final float t, final T p0, final T p1, final T p2, final T tmp) {
                final float dt = 1f - t;
-               return out.set(p0).mul(dt*dt).add(p1.tmp().mul(2*dt*t)).add(p2.tmp().mul(t*t));
+               return out.set(p0).mul(dt*dt).add(tmp.set(p1).mul(2*dt*t)).add(tmp.set(p2).mul(t*t));
        }
        
        /** Cubic Bezier curve
@@ -53,15 +55,17 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
         * @param p1 The second bezier point.
         * @param p2 The third bezier point.
         * @param p3 The fourth bezier point.
+        * @param tmp A temporary vector to be used by the calculation.
         * @return The value specified by out for chaining */
-       public static <T extends Vector<T>> T cubic(final T out, final float t, final T p0, final T p1, final T p2, final T p3) {
+       public static <T extends Vector<T>> T cubic(final T out, final float t, final T p0, final T p1, final T p2, final T p3, final T tmp) {
                final float dt = 1f - t;
                final float dt2 = dt * dt;
                final float t2 = t * t;
-               return out.set(p0).mul(dt2*dt).add(p1.tmp().mul(3*dt2*t)).add(p2.tmp().mul(3*dt*t2)).add(p3.tmp().mul(t2*t));
+               return out.set(p0).mul(dt2*dt).add(tmp.set(p1).mul(3*dt2*t)).add(tmp.set(p2).mul(3*dt*t2)).add(tmp.set(p3).mul(t2*t));
        }
        
        public Array<T> points = new Array<T>();
+       private T tmp;
        
        public Bezier() {       }
        public Bezier(final T... points) {
@@ -80,6 +84,8 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
        public Bezier set(final T[] points, final int offset, final int length) {
                if (length < 2 || length > 4)
                        throw new GdxRuntimeException("Only first, second and third degree Bezier curves are supported.");
+               if (tmp == null)
+                       tmp = points[0].cpy();
                this.points.clear();
                this.points.addAll(points, offset, length);
                return this;
@@ -87,6 +93,8 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
        public Bezier set(final Array<T> points, final int offset, final int length) {
                if (length < 2 || length > 4)
                        throw new GdxRuntimeException("Only first, second and third degree Bezier curves are supported.");
+               if (tmp == null)
+                       tmp = points.get(0).cpy();
                this.points.clear();
                this.points.addAll(points, offset, length);
                return this;
@@ -95,11 +103,11 @@ public class Bezier<T extends Vector<T>> implements Path<T> {
        public T valueAt(final T out, final float t) {
                final int n = points.size; 
                if (n == 2)
-                       linear(out, t, points.get(0), points.get(1));
+                       linear(out, t, points.get(0), points.get(1), tmp);
                else if (n == 3)
-                       quadratic(out, t, points.get(0), points.get(1), points.get(2));
+                       quadratic(out, t, points.get(0), points.get(1), points.get(2), tmp);
                else if (n == 4)
-                       cubic(out, t, points.get(0), points.get(1), points.get(2), points.get(3));
+                       cubic(out, t, points.get(0), points.get(1), points.get(2), points.get(3), tmp);
                return out;
        }