private void read() { InputStream inStream; if (useResource) { inStream = mContext.getResources().openRawResource(this.resourceId); } else { throw new UnsupportedOperationException("Not yet implemented."); } Scanner s = new Scanner(inStream); String newLine; while (s.hasNextLine()) { newLine = s.nextLine(); if (newLine.startsWith("v ")) { Vertex v = new Vertex(); String[] coords = newLine.split(" "); v.x = Float.valueOf(coords[1]); v.y = Float.valueOf(coords[2]); v.z = Float.valueOf(coords[3]); vertices.add(v); } if (newLine.startsWith("vt ")) { Texture t = new Texture(); String[] coords = newLine.split(" "); t.x = Float.valueOf(coords[1]); t.y = Float.valueOf(coords[2]); textures.add(t); } if (newLine.startsWith("vn ")) { Normal n = new Normal(); String[] coords = newLine.split(" "); n.x = Float.valueOf(coords[1]); n.y = Float.valueOf(coords[2]); n.z = Float.valueOf(coords[3]); normals.add(n); } if (newLine.startsWith("f ")) { Face f = new Face(); String[] vertices = newLine.split(" "); for (int i = 1; i < vertices.length; i++) { String[] indices = vertices[i].split("/"); if (!indices[0].equals("")) { f.v[i - 1] = Integer.valueOf(indices[0]); } if (!indices[1].equals("")) { f.vt[i - 1] = Integer.valueOf(indices[1]); } if (!indices[2].equals("")) { f.vn[i - 1] = Integer.valueOf(indices[2]); } } faces.add(f); } } try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } }
/** {@inheritDoc} */ @Override public double getLogProbability(double value) { double prob = 0; int index = 0; for (Normal normal : this.terms) { prob = prob + this.coefficients[index] * normal.getProbability(value); index++; } return Math.log(prob); }
/** * Creates a new GaussianMixture distribution for a given variable. * * @param var1 a {@link Variable} object. */ public GaussianMixture(Variable var1) { this.var = var1; terms = new ArrayList<Normal>(); Normal aux = new Normal(var1); aux.setMean(0); aux.setVariance(1); terms.add(aux); coefficients = new double[1]; coefficients[0] = 1; }
/** {@inheritDoc} */ @Override public double[] getParameters() { int numParameters = 3 * coefficients.length; double[] parameters = new double[numParameters]; int index = 0; for (Normal normal : this.terms) { parameters[3 * index] = this.coefficients[index]; parameters[3 * index + 1] = normal.getMean(); parameters[3 * index + 2] = normal.getVariance(); index++; } return parameters; }
@Override protected void setupCoordinates() { super.setupCoordinates(); // Big tile map bigTiles = new boolean[map.length][map[0].length]; }
public static double qnbeta( double p, double a, double b, double ncp, boolean lower_tail, boolean log_p) { final double accu = 1e-15; final double Eps = 1e-14; /* must be > accu */ double ux, lx, nx, pp; if (DoubleVector.isNaN(p) || DoubleVector.isNaN(a) || DoubleVector.isNaN(b) || DoubleVector.isNaN(ncp)) { return p + a + b + ncp; } if (!DoubleVector.isFinite(a)) { return DoubleVector.NaN; } if (ncp < 0. || a <= 0. || b <= 0.) { return DoubleVector.NaN; } // R_Q_P01_boundaries(p, 0, 1); if ((log_p && p > 0) || (!log_p && (p < 0 || p > 1))) { return DoubleVector.NaN; } if (p == SignRank.R_DT_0(lower_tail, log_p)) { return 0.0; } if (p == SignRank.R_DT_1(lower_tail, log_p)) { return 1.0; } // end of R_Q_P01_boundaries p = Normal.R_DT_qIv(p, log_p ? 1.0 : 0.0, lower_tail ? 1.0 : 0.0); /* Invert pnbeta(.) : * 1. finding an upper and lower bound */ if (p > 1 - SignRank.DBL_EPSILON) { return 1.0; } pp = Math.min(1 - SignRank.DBL_EPSILON, p * (1 + Eps)); for (ux = 0.5; ux < 1 - SignRank.DBL_EPSILON && pnbeta(ux, a, b, ncp, true, false) < pp; ux = 0.5 * (1 + ux)) ; pp = p * (1 - Eps); for (lx = 0.5; lx > Double.MIN_VALUE && pnbeta(lx, a, b, ncp, true, false) > pp; lx *= 0.5) ; /* 2. interval (lx,ux) halving : */ do { nx = 0.5 * (lx + ux); if (pnbeta(nx, a, b, ncp, true, false) > p) { ux = nx; } else { lx = nx; } } while ((ux - lx) / nx > accu); return 0.5 * (ux + lx); }
/** * Sets the parameters of this GaussianMixture. * * @param params an Array of doubles containing the GaussianMixture parameters. */ public void setParameters(double[] params) { if (params.length % 3 != 0) { throw new UnsupportedOperationException( "The number of parameters for the Gaussian mixture is not valid"); } else { int numTerms = params.length / 3; this.coefficients = new double[numTerms]; this.terms = new ArrayList<>(numTerms); for (int index = 0; index < numTerms; index++) { this.coefficients[index] = params[3 * index]; Normal aux = new Normal(this.var); aux.setMean(params[3 * index + 1]); aux.setVariance(params[3 * index + 2]); this.terms.add(aux); } } };
private int valueInt(String s) { if (s == Normal.toString()) { return 0; } else if (s == Math.toString()) { return 1; } else if (s == Puzzle.toString()) { return 2; } else if (s == Shake.toString()) { return 3; } return -1; }
public static String valueString(int i) { switch (i) { case 0: return Normal.toString(); case 1: return Math.toString(); case 2: return Puzzle.toString(); case 3: return Shake.toString(); } return null; }
/** * Randomly initializes this GaussianMixture for a given number of terms. * * @param random a {@link java.util.Random} object. * @param numTerms a number of terms. */ public void randomInitialization(Random random, int numTerms) { this.coefficients = new double[numTerms]; this.terms = new ArrayList<>(numTerms); for (int k = 0; k < numTerms; k++) { this.coefficients[k] = random.nextDouble(); Normal aux = new Normal(this.var); aux.setMean(5 * random.nextGaussian()); aux.setVariance(random.nextDouble()); this.terms.add(aux); } ; DoubleStream aux = Arrays.stream(this.coefficients); double suma = aux.sum(); aux = Arrays.stream(this.coefficients); this.coefficients = aux.map(x -> x / suma).toArray(); // System.out.println(coefficients); // this.coefficients = this.coefficients / .map().sum(); }
// ! produit scalaire d'une normale et d'un vecteur. public static float dot(Normal n1, Vector v2) { return n1.getX() * v2.getX() + n1.getY() * v2.getY() + n1.getZ() * v2.getZ(); }
public double pdf(double x) { if (x < 0.0) { throw new IllegalArgumentException("x value out of range"); } return 2.0 * Normal.pdf(0.0, Math.PI / (2.0 * mTheta * mTheta), x); }
// ! scalaire * normale. public static Normal productNormFloat(float f, Normal n) { return n.productFloat(f); }
@Override public int compareTo(Normal otherNormal) { return Integer.compare(id, otherNormal.getID()); }
public double dot(Normal n) { return (x * n.x() + y * n.y() + z * n.z()); }
// ! renvoie une normale de meme direction, mais de longeur 1. public static Normal normalize(Normal v) throws Exception { return v.division(v.length()); }
public static Geoset read(BufferedReader mdl) { String line = MDLReader.nextLine(mdl); System.out.println("geo begins with " + line); if (line.contains("Geoset")) { line = MDLReader.nextLine(mdl); Geoset geo = new Geoset(); if (!line.contains("Vertices")) { JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Error: Vertices not found at beginning of Geoset!"); } while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) { geo.addVertex(GeosetVertex.parseText(line)); } line = MDLReader.nextLine(mdl); if (line.contains("Normals")) { // If we have normals: while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) { geo.addNormal(Normal.parseText(line)); } } while (((line = MDLReader.nextLine(mdl)).contains("TVertices"))) { geo.addUVLayer(UVLayer.read(mdl)); } if (!line.contains("VertexGroup")) { JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Error: VertexGroups missing or invalid!"); } int i = 0; while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) { geo.getVertex(i).setVertexGroup(MDLReader.readInt(line)); i++; } line = MDLReader.nextLine(mdl); if (!line.contains("Faces")) { JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Error: Faces missing or invalid!"); } line = MDLReader.nextLine(mdl); if (!line.contains("Triangles")) { System.out.println(line); JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Error: Triangles missing or invalid!"); } geo.setTriangles(Triangle.read(mdl, geo)); line = MDLReader.nextLine(mdl); // Throw away the \t} closer for faces line = MDLReader.nextLine(mdl); if (!line.contains("Groups")) { JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Error: Groups (Matrices) missing or invalid!"); } while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) { geo.addMatrix(Matrix.parseText(line)); } MDLReader.mark(mdl); line = MDLReader.nextLine(mdl); while (!line.contains("}") || line.contains("},")) { if (line.contains("Extent") || line.contains("BoundsRadius")) { System.out.println("Parsing geoset extLog:" + line); MDLReader.reset(mdl); geo.setExtLog(ExtLog.read(mdl)); System.out.println("Completed geoset extLog."); } else if (line.contains("Anim")) { MDLReader.reset(mdl); geo.add(Animation.read(mdl)); MDLReader.mark(mdl); } else if (line.contains("MaterialID")) { geo.materialID = MDLReader.readInt(line); MDLReader.mark(mdl); } else if (line.contains("SelectionGroup")) { geo.selectionGroup = MDLReader.readInt(line); MDLReader.mark(mdl); } else { geo.addFlag(MDLReader.readFlag(line)); System.out.println("Reading to geoFlag: " + line); MDLReader.mark(mdl); } line = MDLReader.nextLine(mdl); } // JOptionPane.showMessageDialog(MDLReader.getDefaultContainer(),"Geoset reading // completed!"); System.out.println("Geoset reading completed!"); return geo; } else { JOptionPane.showMessageDialog( MDLReader.getDefaultContainer(), "Unable to parse Geoset: Missing or unrecognized open statement '" + line + "'."); } return null; }
public static Vector vector(Normal n) { return new Vector(n.getX(), n.getY(), n.getZ()); }
public static double qsignrank(double x, double n, boolean lower_tail, boolean log_p) { double f, p, q; if (Double.isNaN(x) || Double.isNaN(n)) { return (x + n); } if (Double.isInfinite(x) || Double.isInfinite(n)) { return Double.NaN; } if ((log_p && x > 0) || (!log_p && (x < 0 || x > 1))) { return Double.NaN; } n = Math.floor(n + 0.5); if (n <= 0) { return Double.NaN; } if (x == R_DT_0(lower_tail, log_p)) { return (0); } if (x == R_DT_1(lower_tail, log_p)) { return (n * (n + 1) / 2); } if (log_p || !lower_tail) { // x = R_DT_qIv(x); /* lower_tail,non-log "p" */ x = Normal.R_DT_qIv(x, log_p ? 1. : 0., lower_tail ? 1. : 0.); } w_init_maybe((int) n); f = Math.exp(-n * Math.log(2.)); p = 0; q = 0; if (x <= 0.5) { x = x - 10 * DBL_EPSILON; for (; ; ) { p += csignrank((int) q, (int) n) * f; if (p >= x) { break; } q++; } } else { x = 1 - x + 10 * DBL_EPSILON; for (; ; ) { p += csignrank((int) q, (int) n) * f; if (p > x) { q = n * (n + 1) / 2 - q; break; } q++; } } return (q); }
// ! produit scalaire de 2 normales. public static float dot(Normal n1, Normal n2) { return n1.getX() * n2.getX() + n1.getY() * n2.getY() + n1.getZ() * n2.getZ(); }
public static double qnbinom( double p, double size, double prob, boolean lower_tail, boolean log_p) { double P, Q, mu, sigma, gamma, y; double[] z = new double[1]; if (DoubleVector.isNaN(p) || DoubleVector.isNaN(size) || DoubleVector.isNaN(prob)) { return p + size + prob; } if (prob <= 0 || prob > 1 || size <= 0) { return DoubleVector.NaN; } /* FIXME: size = 0 is well defined ! */ if (prob == 1) { return 0; } // R_Q_P01_boundaries(p, 0, ML_POSINF); // #define R_Q_P01_boundaries(p, _LEFT_, _RIGHT_) // This macro is defined in /src/nmath/dpq.h if (log_p) { if (p > 0) { return DoubleVector.NaN; } if (p == 0) { /* upper bound*/ return lower_tail ? Double.POSITIVE_INFINITY : 0; } if (p == Double.NEGATIVE_INFINITY) { return lower_tail ? 0 : Double.POSITIVE_INFINITY; } } else { /* !log_p */ if (p < 0 || p > 1) { return DoubleVector.NaN; } if (p == 0) { return lower_tail ? 0 : Double.POSITIVE_INFINITY; } if (p == 1) { return lower_tail ? Double.POSITIVE_INFINITY : 0; } } Q = 1.0 / prob; P = (1.0 - prob) * Q; mu = size * P; sigma = Math.sqrt(size * P * Q); gamma = (Q + P) / sigma; /* Note : "same" code in qpois.c, qbinom.c, qnbinom.c -- * FIXME: This is far from optimal [cancellation for p ~= 1, etc]: */ if (!lower_tail || log_p) { // p = R_DT_qIv(p); /* need check again (cancellation!): */ p = Normal.R_DT_qIv(p, lower_tail ? 1.0 : 0.0, log_p ? 1.0 : 0.0); if (p == SignRank.R_DT_0(lower_tail, log_p)) { return 0; } if (p == SignRank.R_DT_1(lower_tail, log_p)) { return Double.POSITIVE_INFINITY; } } /* temporary hack --- FIXME --- */ if (p + 1.01 * SignRank.DBL_EPSILON >= 1.) { return Double.POSITIVE_INFINITY; } /* y := approx.value (Cornish-Fisher expansion) : */ z[0] = Distributions.qnorm(p, 0., 1., /*lower_tail*/ true, /*log_p*/ false); y = Math.floor(mu + sigma * (z[0] + gamma * (z[0] * z[0] - 1) / 6) + 0.5); z[0] = Distributions.pnbinom(y, (int) size, prob, /*lower_tail*/ true, /*log_p*/ false); /* fuzz to ensure left continuity: */ p *= 1 - 64 * SignRank.DBL_EPSILON; /* If the C-F value is not too large a simple search is OK */ if (y < 1e5) { return do_search(y, z, p, size, prob, 1); } /* Otherwise be a bit cleverer in the search */ { double incr = Math.floor(y * 0.001), oldincr; do { oldincr = incr; y = do_search(y, z, p, size, prob, incr); incr = Math.max(1, Math.floor(incr / 100)); } while (oldincr > 1 && incr > y * 1e-15); return y; } }
/** * cross product of a normal and a vector * * @param v1 * @param v2 * @return */ public static Vector cross(Normal v1, Vector v2) { return new Vector( (v1.getY() * v2.getZ()) - (v1.getZ() * v2.getY()), (v1.getZ() * v2.getX()) - (v1.getX() * v2.getZ()), (v1.getX() * v2.getY()) - (v1.getY() * v2.getX())); }