/** * Get the {@code grid} as sample indices. * * @param s Sampling that this {@code grid} subsamples. * @param grid the subsample locations defined in terms of Sampling {@code s}. * @return the {@code grid} as sample indices. * @throws IllegalArgumentException if any of the grid locations are not valid with the given * Sampling {@code s}. */ public static int[] gridCoordsToSamples(Sampling s, float[] grid) { Almost a = new Almost(); float f = (float) s.getFirst(); float l = (float) s.getLast(); int ng = grid.length; int[] t = new int[ng]; // temp sample indices int count = 0; int is = -1; // save last index for (int ig = 0; ig < ng; ig++) { float v = grid[ig]; if (a.ge(v, f) && a.le(v, l)) { int i = s.indexOfNearest(v); if (i != is) { // no duplicate entries t[count] = i; count++; } is = i; } else { print("Error: value " + v + " is out of bounds! First=" + f + ", Last=" + l); } } if (count != ng) { print("Grid values:"); dump(grid); throw new IllegalArgumentException( "Error: Only " + count + " of " + ng + " input grid coordinates are valid " + "with the specified sampling " + s.toString()); } return copy(count, t); }
private static float[] getValues(Sampling s) { int n = s.getCount(); double f = s.getFirst(); double d = s.getDelta(); float[] x = new float[n]; for (int i = 0; i < n; i++) x[i] = (float) (f + i * d); return x; }
/** * Gets mappings computed from specified slopes and planarities. * * @param s1 sampling of 1st dimension. * @param s2 sampling of 2nd dimension. * @param p2 array of slopes of image features. * @param ep array of planarities of image features. */ public Mappings getMappingsFromSlopes( Sampling s1, Sampling s2, Sampling s3, float[][][] p2, float[][][] p3, float[][][] ep) { // Sampling parameters. final int n1 = s1.getCount(); final int n2 = s2.getCount(); final int n3 = s3.getCount(); float d1 = (float) s1.getDelta(); float d2 = (float) s2.getDelta(); float d3 = (float) s3.getDelta(); float f1 = (float) s1.getFirst(); // If necessary, convert units for slopes to samples per sample. if (d1 != d2) p2 = mul(d2 / d1, p2); if (d1 != d3) p3 = mul(d3 / d1, p3); // Compute shifts r(x1,x2,x3), in samples. float[][][] b = new float[n3][n2][n1]; // right-hand side float[][][] r = new float[n3][n2][n1]; // shifts, in samples VecArrayFloat3 vb = new VecArrayFloat3(b); VecArrayFloat3 vr = new VecArrayFloat3(r); Smoother3 smoother3 = new Smoother3(n1, n2, n3, _sigma1, _sigma2, _sigma3, ep); A3 a3 = new A3(smoother3, _weight1, ep, p2, p3); CgSolver cs = new CgSolver(_small, _niter); makeRhs(ep, p2, p3, b); smoother3.applyTranspose(b); cs.solve(a3, vb, vr); smoother3.apply(r); cleanShifts(r); // Compute u1(x1,x2,x3). final float[][][] u1 = r; for (int i3 = 0; i3 < n3; ++i3) { for (int i2 = 0; i2 < n2; ++i2) { for (int i1 = 0; i1 < n1; ++i1) { float x1i = f1 + i1 * d1; u1[i3][i2][i1] = x1i + r[i3][i2][i1] * d1; } } } // Compute x1(u1,u2). final float[][][] x1 = b; final InverseInterpolator ii = new InverseInterpolator(s1, s1); Parallel.loop( n3, new Parallel.LoopInt() { public void compute(int i3) { for (int i2 = 0; i2 < n2; ++i2) ii.invert(u1[i3][i2], x1[i3][i2]); } }); return new Mappings(s1, s2, s3, u1, x1); }
/** * Gets the flattening shifts s(u1,u2,u3) = u1 - x1(u1,u2,u3). * * @return the flattening shifts. */ public float[][][] getShiftsS() { int n1 = s1.getCount(); int n2 = s2.getCount(); int n3 = s3.getCount(); float[][][] s = new float[n3][n2][n1]; float d1 = (float) s1.getDelta(); float f1 = (float) s1.getFirst(); for (int i3 = 0; i3 < n3; ++i3) { for (int i2 = 0; i2 < n2; ++i2) { for (int i1 = 0; i1 < n1; ++i1) { float u1i = f1 + i1 * d1; s[i3][i2][i1] = u1i - x1[i3][i2][i1]; } } } return s; }
private float[][][] apply(final float[][][] ux, final float[][][] f) { final int n1 = s1.getCount(); final int n2 = s2.getCount(); final int n3 = s3.getCount(); final double d1 = s1.getDelta(); final double f1 = s1.getFirst(); final SincInterpolator si = new SincInterpolator(); final float[][][] g = new float[n3][n2][n1]; Parallel.loop( n3, new Parallel.LoopInt() { public void compute(int i3) { for (int i2 = 0; i2 < n2; ++i2) si.interpolate(n1, d1, f1, f[i3][i2], n1, ux[i3][i2], g[i3][i2]); } }); return g; }
public float[][][][] flattenWithShifts( final Sampling s1, final float[][][] r, final float[][][] f) { cleanShifts(r); final int n3 = r.length; final int n2 = r[0].length; final int n1 = r[0][0].length; // Compute u1(x1,x2,x3). final double f1 = s1.getFirst(); final double d1 = s1.getDelta(); final float[][][] u1 = r; for (int i3 = 0; i3 < n3; ++i3) { for (int i2 = 0; i2 < n2; ++i2) { for (int i1 = 0; i1 < n1; ++i1) { float x1i = (float) (f1 + i1 * d1); u1[i3][i2][i1] = (float) (x1i + r[i3][i2][i1] * d1); } } } // Compute x1(u1,u2). final float[][][] x1 = new float[n3][n2][n1]; final InverseInterpolator ii = new InverseInterpolator(s1, s1); Parallel.loop( n3, new Parallel.LoopInt() { public void compute(int i3) { for (int i2 = 0; i2 < n2; ++i2) ii.invert(u1[i3][i2], x1[i3][i2]); } }); final SincInterpolator si = new SincInterpolator(); final float[][][] g = new float[n3][n2][n1]; Parallel.loop( n3, new Parallel.LoopInt() { public void compute(int i3) { for (int i2 = 0; i2 < n2; ++i2) si.interpolate(n1, d1, f1, f[i3][i2], n1, x1[i3][i2], g[i3][i2]); } }); return new float[][][][] {g, u1}; }