public static Matrix lookAtLH(Vector eye, Vector target, Vector up) { Matrix res = new Matrix(); Vector zaxis = Vector.sub(target, eye).normalize(); if (zaxis.length() == 0) zaxis.Z = 1; Vector xaxis = Vector.cross(up, zaxis).normalize(); if (xaxis.length() == 0) { zaxis.X += 0.000001; xaxis = Vector.cross(up, zaxis).normalize(); } Vector yaxis = Vector.cross(zaxis, xaxis); res.m[0] = xaxis.X; res.m[1] = xaxis.Y; res.m[2] = xaxis.Z; res.m[3] = -Vector.dot(xaxis, eye); res.m[4] = yaxis.X; res.m[5] = yaxis.Y; res.m[6] = yaxis.Z; res.m[7] = -Vector.dot(yaxis, eye); res.m[8] = zaxis.X; res.m[9] = zaxis.Y; res.m[10] = zaxis.Z; res.m[11] = -Vector.dot(zaxis, eye); // already set during initialization of matrix /* * res.m[12] = 0; res.m[13] = 0; res.m[14] = 0; res.m[15] = 1; */ return res; }
@Override @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct( Vector<M> y, Vectors.Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[n][y.length()]; for (Map.Entry<Integer, Integer> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = e.getValue().intValue() * y.intValue(j); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[n][y.length()]; for (Map.Entry<Integer, Integer> e : entrySet()) { for (int j = 0; j < y.length(); j++) { data2[e.getKey()][j] = y.doubleValue(j) * e.getValue().intValue(); } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(n, y.length(), using); for (Map.Entry<Integer, Integer> e : entrySet()) { for (Map.Entry<Integer, M> e2 : y.entrySet()) { matrix.set( e.getKey(), e2.getKey(), e2.getValue().doubleValue() * e.getKey().doubleValue()); } } return matrix; } }
/** * Creates a copy of a given {@code Vector}. * * @param source The {@code Vector} to copy. * @return A copy of {@code source} with the same type. */ public static Vector copyOf(Vector source) { if (source instanceof DoubleVector) return copyOf((DoubleVector) source); if (source instanceof IntegerVector) return copyOf((IntegerVector) source); Vector result = new DenseVector(source.length()); for (int i = 0; i < source.length(); ++i) result.set(i, source.getValue(i)); return result; }
/** * Returns {@code true} if the two vectors are equal to one another. Two {@code Vector} insances * are considered equal if they contain the same number of elements and all corresponding pairs of * {@code Number} values are equal. Two values {@code n1} and {@code n2} are considered equal if * {@code n1.equals(n2)}. */ public static boolean equals(Vector v1, Vector v2) { if (v1.length() == v2.length()) { int length = v1.length(); for (int i = 0; i < length; ++i) { Number n1 = v1.getValue(i); Number n2 = v2.getValue(i); if (!n1.equals(n2)) return false; } return true; } return false; }
@Override public <M extends Number> void sub(Vector<M> vector) { assert (vector.length() == n); for (Map.Entry<Integer, M> e : vector.entrySet()) { sub(e.getKey(), e.getValue().intValue()); } }
/** Create a copy of this vector. */ public Vector copy() { Vector copy = new Vector(); copy.dx = dx; copy.dy = dy; copy.direction = direction; copy.length = length; return copy; }
public double angle(Vector other) { try { double c = this.multiply(other) / (this.length() * other.length()); return Math.acos(Math.max(-1.0, Math.min(1.0, c))); } catch (ArithmeticException a) { System.err.println("arithmetic exception " + a); return 0.0; } }
@Override public double product(Vector vector) { if (vector == null) { throw new IllegalArgumentException("Vector can't be null."); } if (length != vector.length()) { throw new IllegalArgumentException("Wrong vector length: " + vector.length()); } double result = 0.0; for (int i = 0; i < length; i++) { result += get(i) * vector.get(i); } return result; }
@Override public Vector add(Vector vector, Factory factory) { ensureFactoryIsNotNull(factory); if (vector == null) { throw new IllegalArgumentException("Vector can't be null."); } if (length != vector.length()) { throw new IllegalArgumentException("Worong vector length: " + vector.length()); } Vector result = blank(factory); for (int i = 0; i < length; i++) { result.set(i, get(i) + vector.get(i)); } return result; }
private static boolean unzipMatches(ZipEntry entry, Vector files) { if (files == Null.INSTANCE) { return true; } else { for (int i = 0; i != files.length(); ++i) { if (entry.getName().equals(files.getElementAsString(i))) { return true; } } return false; } }
public static PairList fromVector(Vector vector) { Builder builder = new Builder(); for (int i = 0; i != vector.length(); ++i) { String name = vector.getName(i); if (Strings.isNullOrEmpty(name)) { builder.add(vector.getElementAsSEXP(i)); } else { builder.add(name, vector.getElementAsSEXP(i)); } } return builder.build(); }
public int calculateBrithness(Matrix1x4 point, Vector vector) { Vector e = new Vector(point, viewer).normalize(); Vector l = new Vector(point, corners3D[0]); double len = l.length(); double brightness = (kd * vector.cosNorm(l) + ks * Math.pow(e.cosNorm(l), m)) / (len * len); brightness *= 256 * 256; if (brightness < 0) { brightness = 0; } return (int) brightness; }
@Override public <M extends Number, O extends Number> Vector<O> mult( Vector<M> x, Vectors.Factory<O> using) { assert (x.length() == alpha.length); final Vector<O> product = using.make(alpha.length, 0.0); for (int i = 0; i < alpha.length; i++) { double value = 0.0; if (i > 0) { value += x.doubleValue(i - 1) * beta[i - 1]; } value += x.doubleValue(i) * alpha[i]; if (i < beta.length) { value += x.doubleValue(i + 1) * beta[i]; } product.put(i, value); } return product; }
/** * Find w, such that Mw = v * * @param v the vector * @return w */ public Vector<Double> invMult(Vector<Double> v) { final int n = v.length(); if (n != alpha.length) { throw new IllegalArgumentException(); } double[] delta = new double[n - 1]; double[] gamma = new double[n - 1]; delta[0] = v.doubleValue(0) / alpha[0]; gamma[0] = -1.0 * beta[0] / alpha[0]; for (int i = 1; i < n - 1; i++) { final double bga = beta[i - 1] * gamma[i - 1] + alpha[i]; if (bga != 0.0) { delta[i] = (v.doubleValue(i) - beta[i - 1] * delta[i - 1]) / bga; gamma[i] = -1.0 * beta[i] / bga; } else { final double bga2 = beta[i] * gamma[i] + alpha[i + 1]; if (bga2 == 0.0) { // Value is 'free' delta[i] = delta[i + 1] = 1.0; } else { gamma[i + 1] = (v.doubleValue(i + 1) - beta[i] * delta[i]) / bga2; delta[i + 1] = -1.0 * beta[i + 1] / bga2; gamma[i] = -(alpha[i + 1] * gamma[i + 1] + beta[i + 1]) / beta[i]; delta[i] = beta[i + 1] * delta[i + 1] / beta[i] * gamma[i + 1]; i++; } } } double[] w = new double[n]; final double bga3 = beta[n - 2] * gamma[n - 2] + alpha[n - 1]; if (bga3 == 0.0) { w[n - 1] = 1.0; // value is 'free' } else { w[n - 1] = (v.doubleValue(n - 1) - beta[n - 2] * delta[n - 2]) / bga3; } for (int i = n - 2; i >= 0; i--) { w[i] = gamma[i] * w[i + 1] + delta[i]; } return new RealVector(w); }
@Override public boolean equals(Object object) { if (this == object) { return true; } if (object == null) { return false; } if (!(object instanceof Vector)) { return false; } Vector vector = (Vector) object; if (length != vector.length()) { return false; } boolean result = true; for (int i = 0; result && i < length; i++) { double a = get(i); double b = vector.get(i); double diff = Math.abs(a - b); result = result && (a == b) ? true : diff < Matrices.EPS ? true : diff / Math.max(Math.abs(a), Math.abs(b)) < Vectors.EPS; } return result; }
public static Transform lookAt(final Point source, final Point target, final Vector up) { final double[][] m = new double[4][4]; m[0][3] = source.getX(); m[1][3] = source.getY(); m[2][3] = source.getZ(); m[3][3] = 1.0D; final Vector direction = Vector.copyAndSubtract(target, source).normalize(); final Vector vector0 = Vector.copyAndNormalize(up); final Vector vector1 = Vector.toCrossProduct(vector0, direction); if (vector1.length() == 0.0D) { return newInstance(); } final Vector vector2 = vector1.normalize(); final Vector vector3 = Vector.toCrossProduct(direction, vector2); m[0][0] = vector2.getX(); m[1][0] = vector2.getY(); m[2][0] = vector2.getZ(); m[3][0] = 0.0D; m[0][1] = vector3.getX(); m[1][1] = vector3.getY(); m[2][1] = vector3.getZ(); m[3][1] = 0.0D; m[0][2] = direction.getX(); m[1][2] = direction.getY(); m[2][2] = direction.getZ(); m[3][2] = 0.0D; final Matrix matrix = Matrix.newInstance(m); return newInstance(matrix.inverse(), matrix); }
@Override public int length() { return source.length(); }
@Override public <M extends Number> double innerProduct(Vector<M> y) { assert (n == y.length()); if (y instanceof RealVector) { double[] y2 = ((RealVector) y).data(); if (defaultValue == 0) { double innerProduct = 0; for (Map.Entry<Integer, Integer> e : entrySet()) { innerProduct += y2[e.getKey()] * e.getValue(); } return innerProduct; } else { double innerProduct = 0; for (int i = 0; i < n; i++) { innerProduct += y2[i] * get(i); } return innerProduct; } } else if (y instanceof IntVector) { int[] y2 = ((IntVector) y).data(); if (defaultValue == 0) { int innerProduct = 0; for (Map.Entry<Integer, Integer> e : entrySet()) { innerProduct += e.getValue() * y2[e.getKey()]; } return innerProduct; } else { int innerProduct = 0; for (int i = 0; i < n; i++) { innerProduct += y2[i] * get(i); } return innerProduct; } } else { if (defaultValue == 0 || y.defaultValue().intValue() == 0) { int innerProduct = 0; if (this.size() <= y.size()) { for (Map.Entry<Integer, Integer> e : entrySet()) { innerProduct += e.getValue() * y.intValue(e.getKey()); } return innerProduct; } else { for (Map.Entry<Integer, M> e : y.entrySet()) { innerProduct += e.getValue().intValue() * this.intValue(e.getKey()); } return innerProduct; } } else { int innerProduct = 0; int notBothSparse = 0; for (Map.Entry<Integer, Integer> e : entrySet()) { innerProduct += e.getValue() * y.intValue(e.getKey()); notBothSparse++; } for (Map.Entry<Integer, M> e : y.entrySet()) { if (!super.containsKey(e.getKey())) { innerProduct += defaultValue * e.getValue().intValue(); notBothSparse++; } } return innerProduct + (n - notBothSparse) * defaultValue * y.defaultValue().intValue(); } } }
/** * Copies all of the values from one {@code Vector} into another. After the operation, all of the * values in {@code dest} will be the same as that of {@code source}. The legnth of {@code dest} * must be as long as the length of {@code source}. Once completed {@code dest} is returned. * * @param dest The {@code Vector} to copy values into. * @param source The {@code Vector} to copy values from. * @return {@code dest} after being copied from {@code source}. * @throws IllegalArgumentException if the length of {@code dest} is less than that of {@code * source}. */ public static Vector copy(Vector dest, Vector source) { for (int i = 0; i < source.length(); ++i) dest.set(i, source.getValue(i).doubleValue()); return dest; }
/** * @param v * @return a vector of length 1, same direction as v * @throws Exception */ public static Vector normalize(Vector v) throws Exception { return v.division(v.length()); }