FitEllipsoid { public static double[][] testEllipsoid(final double a, final double b, final double c, final double angle, final double xCentre, final double yCentre, final double zCentre, final double noise, final int nPoints, final boolean random) { final int n = (int) Math.floor(-0.75 + Math.sqrt(1.0 + 8.0 * nPoints) / 4.0); final int h = 2 * n + 2; final int w = n + 1; final double[][] s = new double[h][w]; final double[][] t = new double[h][w]; final double theta = -Math.PI / 2.0; if (random) { for (int j = 0; j < w; j++) { for (int i = 0; i < h; i++) { s[i][j] = theta + rng.nextDouble() * 2 * Math.PI; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { t[i][j] = theta + rng.nextDouble() * 2 * Math.PI; } } } else { final double increment = Math.PI / (n - 1); for (int j = 0; j < w; j++) { final double alpha = theta + j * increment; for (int i = 0; i < h; i++) { s[i][j] = alpha; } } for (int i = 0; i < h; i++) { final double alpha = theta + i * increment; for (int j = 0; j < w; j++) { t[i][j] = alpha; } } } final double[][] x = new double[h][w]; final double[][] y = new double[h][w]; final double[][] z = new double[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { x[i][j] = a * Math.cos(s[i][j]) * Math.cos(t[i][j]); y[i][j] = b * Math.cos(s[i][j]) * Math.sin(t[i][j]); z[i][j] = c * Math.sin(s[i][j]); } } final double[][] xt = new double[h][w]; final double[][] yt = new double[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { xt[i][j] = x[i][j] * Math.cos(angle) - y[i][j] * Math.sin(angle); yt[i][j] = x[i][j] * Math.sin(angle) + y[i][j] * Math.cos(angle); } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { x[i][j] = xt[i][j] + xCentre; y[i][j] = yt[i][j] + yCentre; z[i][j] = z[i][j] + zCentre; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { x[i][j] = x[i][j] + rng.nextDouble() * noise; y[i][j] = y[i][j] + rng.nextDouble() * noise; z[i][j] = z[i][j] + rng.nextDouble() * noise; } } final double[][] ellipsoidPoints = new double[w * h][3]; int p = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { ellipsoidPoints[p][0] = x[i][j]; ellipsoidPoints[p][1] = y[i][j]; ellipsoidPoints[p][2] = z[i][j]; p++; } } return ellipsoidPoints; } private  FitEllipsoid(); static double[][] testEllipsoid(final double a, final double b,
		final double c, final double angle, final double xCentre,
		final double yCentre, final double zCentre, final double noise,
		final int nPoints, final boolean random); static Object[] yuryPetrov(final double[][] points);  }
@Test public void testTestEllipsoid() { final double r = 8.3; final double r2 = 8.3 * 8.3; double[][] xyz = FitEllipsoid.testEllipsoid(r, r, r, 0, 0, 0, 0, 0.0, 1000, true); for (int i = 0; i < xyz.length; i++) { final double xi = xyz[i][0]; final double yi = xyz[i][1]; final double zi = xyz[i][2]; final double equation = xi * xi / r2 + yi * yi / r2 + zi * zi / r2; assertEquals(1.0, equation, 1e-15); } xyz = FitEllipsoid.testEllipsoid(a, b, c, 0, 0, 0, 0, 0.0, 1000, true); for (int i = 0; i < xyz.length; i++) { final double xi = xyz[i][0]; final double yi = xyz[i][1]; final double zi = xyz[i][2]; final double equation = xi * xi / (a * a) + yi * yi / (b * b) + zi * zi / (c * c); assertEquals(1.0, equation, 1e-15); } }