Java AtomicReference getSplineCoefficients(int n, float x)

Here you can find the source of getSplineCoefficients(int n, float x)

Description

Gets the spline coefficients for a given order and offset

License

Open Source License

Parameter

Parameter Description
n the order of the spline
x the sub-sample offset

Declaration

public static float[] getSplineCoefficients(int n, float x) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.atomic.AtomicReference;

public class Main {
    /**/*  ww w  .  j a  va  2  s  .co m*/
     * The spline coefficient cache
     */
    private final static AtomicReference<float[][][]> coeffRoot = new AtomicReference<float[][][]>();

    /**
     * Gets the spline coefficients for a given order and offset
     * 
     * @param n the order of the spline
     * @param x the sub-sample offset
     * @return
     */
    public static float[] getSplineCoefficients(int n, float x) {
        float[][][] root = coeffRoot.get();
        if (root == null || root.length <= n) {
            expandSplineRoots(n);
            root = coeffRoot.get();
        }
        float[][] orderCoeff = root[n];

        float[] coeffs = new float[n + 1];

        for (int i = 0; i <= n; i++) {
            float[] current = orderCoeff[i];

            float temp = 0;
            for (int j = 0; j <= n; j++) {
                temp = (temp * x) + current[n - j];
            }

            coeffs[i] = temp;
        }

        return coeffs;

    }

    private static synchronized void expandSplineRoots(int n) {
        float[][][] oldRoot = coeffRoot.get();
        if (oldRoot != null && oldRoot.length > n) {
            return;
        }

        float[][][] newRoot = new float[n + 1][][];

        // Coefficient for zeroth order spline
        newRoot[0] = new float[][] { { 1.0F } };

        // Compute coefficients for each spline order based on the coefficients from the previous order
        for (int o = 1; o < n + 1; o++) {
            newRoot[o] = new float[o + 1][o + 1];
            for (int i = 0; i < o; i++) {
                for (int p = 0; p < o; p++) {
                    newRoot[o][i][p + 1] += newRoot[o - 1][i][p] / (p + 1);
                    newRoot[o][i + 1][p + 1] -= newRoot[o - 1][i][p] / (p + 1);
                    newRoot[o][i + 1][0] += newRoot[o - 1][i][p] / (p + 1);
                }
            }
        }

        coeffRoot.set(newRoot);
    }
}

Related

  1. cancel(Future future, boolean mayInterruptIfRunning)
  2. cancelAll(Future[] futures, boolean mayInterruptIfRunning)
  3. expandSplineRoots(int n)
  4. getDefaultBaseUri()
  5. getIdentityName(final String identityType, final String displayName, final String attribute, final String attribute2, final int uniqueUserID, final AtomicReference outResolvableName, final AtomicReference outDisplayableName)
  6. isTerminated(AtomicReference field)
  7. length(AtomicReferenceArray buf)
  8. mergeProperties(Properties... properties)
  9. pack(int count, Object[] array, AtomicReference edit, int idx)