@Override protected double getRelativeError (Position a, Position b, Position c) { Vector vA = new Vector(a.getLongitude(), a.getLatitude()); Vector vB = new Vector(b.getLongitude(), b.getLatitude()); Vector vC = new Vector(c.getLongitude(), c.getLatitude()); // a, b, and c really should be Positions, but we're treating them as // straight vectors anyway. Vector ac = vC.subtract(vA); Vector ab = vB.subtract(vA); double lac2 = ac.vectorLengthSquared(); Vector bPerp = ab.subtract(ac.scale(ab.dot(ac)/lac2)); double lbPerp = bPerp.vectorLength(); double lac = Math.sqrt(lac2); return lbPerp/lac; }
public Vector cross (Vector v) { if (3 != size() || 3 != v.size()) throw new IllegalArgumentException("Attempt to take the cross product of non-3-vectors"); double xa = coord(0); double ya = coord(1); double za = coord(2); double xb = v.coord(0); double yb = v.coord(1); double zb = v.coord(2); return new Vector(ya*zb-za*yb, za*xb-xa*zb, xa*yb-ya*xb); }
/** * Get the coordinate-wise mean of the given vectors. */ static public Vector mean (List<? extends Vector> data) { if (data.isEmpty()) throw new IllegalArgumentException("Attempt to take the mean of 0 vectors"); Vector mean = zeroVector(data.get(0).size()); for (Vector datum: data) { mean = mean.add(datum); } mean = mean.scale(1.0/data.size()); return mean; }
/** * Get the square of the simple cartesian distance between this and another * vector. */ public double getDistanceSquared (Vector other) { return subtract(other).vectorLengthSquared(); }
int d = Ks.get(0).size(); if (n<2) return null; Vector K0 = Ks.get(0); Vector K1 = Ks.get(1); Ys.add(K0.add(K1.scale(2))); for (int i=1; i<n-1; ++i) { Vector Ki = Ks.get(i); Vector Ki1 = Ks.get(i+1); Ys.add(Ki.scale(4).add(Ki1.scale(2))); Ys.add(Knm1.scale(8).add(Kn)); p2Data[j] = 2*K.coord(j+1) - P1i.coord(j+1); p2Data[n-1] = 0.5*(K.coord(n) + P1i.coord(n-1)); Vector P2i = new Vector(p2Data);
public static Vector zeroVector (int size) { double[] coords = new double[size]; for (int i=0; i<size; ++i) coords[i] = 0.0; return new Vector(coords); }
@Override protected Position interpolate (Position start, Position end, double t) { Vector sV = start.getAsCartesian(); Vector eV = end.getAsCartesian(); Vector cartesianResult = sV.scale(1 - t).add(eV.scale(t)); Position p = new Position(cartesianResult.coord(0), cartesianResult.coord(1), cartesianResult.coord(2), !start.hasElevation()); p.setPrecision(start.getPrecision()); return p; }
double X = _cartesian.coord(2); double Y = _cartesian.coord(0); double Z = _cartesian.coord(1); double XXpYY = X*X+Y*Y; double sqrtXXpYY = Math.sqrt(XXpYY); _polar = new Vector(AngleUtilities.intoRangeDegrees(0.0, Math.toDegrees(lambda)), Math.toDegrees(phi), h); } else { _polar = new Vector(AngleUtilities.intoRangeDegrees(0.0, Math.toDegrees(lambda)), Math.toDegrees(phi)); _polar.setPrecision(_cartesian.getPrecision());
@Test public void testCrossProduct () { Vector X = new Vector(1, 0, 0); Vector Y = new Vector(0, 1, 0); Vector Z = new Vector(0, 0, 1); Assert.assertEquals(Z, X.cross(Y)); Assert.assertEquals(Y, Z.cross(X)); Assert.assertEquals(X, Y.cross(Z)); Assert.assertEquals(Z.scale(-1.0), Y.cross(X)); Assert.assertEquals(Y.scale(-1.0), X.cross(Z)); Assert.assertEquals(X.scale(-1.0), Z.cross(Y)); } }
b[0] = 2; c[0] = 1; r[0] = K.get(0).add(K.get(1).scale(2)); b[i] = 4; c[i] = 1; r[i] = K.get(i).scale(4).add(K.get(i + 1).scale(2)); b[n - 1] = 7; c[n - 1] = 0; r[n - 1] = K.get(n - 1).scale(8).add(K.get(n)); double m = a[i] / b[i - 1]; b[i] = b[i] - m * c[i - 1]; r[i] = r[i].add(r[i - 1].scale(-m)); P1.add(r[n - 1].scale(1.0 / b[n - 1])); for (int i = n - 2; i >= 0; --i) P1.add(0, r[i].add(P1.get(0).scale(-c[i])).scale(1.0 / b[i])); P2.add(K.get(i + 1).scale(2).subtract(P1.get(i + 1))); P2.add(K.get(n).add(P1.get(n - 1)).scale(0.5));
private void updateCartesian () { if (null != _cartesian) return; if (null == _polar) return; // from WorldWind: ElipsoidalGlobe.geodeticToCartesian double latitude = getLatitudeRadians(); double cosLat = Math.cos(latitude); double sinLat = Math.sin(latitude); double longitude = getLongitudeRadians(); double cosLon = Math.cos(longitude); double sinLon = Math.sin(longitude); double elevation = (_elevationUsed ? getElevation() : 0.0); // Get the radius (in meters) of the vertical in prime meridian double rpm = WGS84_EQUATORIAL_RADIUS / Math.sqrt(1.0 - WGS84_ES * sinLat * sinLat); // Y axis is the axis of rotation, just like in WorldWind. double x = (rpm + elevation) * cosLat * sinLon; double z = (rpm + elevation) * cosLat * cosLon; double y = (rpm * (1.0 - WGS84_ES) + elevation) * sinLat; _cartesian = new Vector(x, y, z); _cartesian.setPrecision(_polar.getPrecision()); }
public Vector getPoint (double relTime) { int n = getNumSegments(); for (int i=0; i<n; ++i) { double t0 = _times.get(i); double t1 = _times.get(i+1); if (t0 <= relTime && relTime <= t1) { // this segment Vector P0 = getControlPoint(i, 0); Vector P1 = getControlPoint(i, 1); Vector P2 = getControlPoint(i, 2); Vector P3 = getControlPoint(i, 3); double t = (relTime-t0)/(t1-t0); double nt = (1-t); return P0.scale(nt*nt*nt) .add(P1.scale(3*nt*nt*t)) .add(P2.scale(3*nt*t*t)) .add(P3.scale(t*t*t)); } } return null; }
@Test public void testVectorEquality () { Assert.assertFalse(new Vector(Double.NaN).equals(new Vector(0))); Assert.assertFalse(new Vector(0).equals(new Vector(Double.NaN))); Assert.assertTrue(new Vector(0).equals(new Vector(0))); Assert.assertTrue(new Vector(Double.NaN).equals(new Vector(Double.NaN))); }
/** * Get the direction one must travel from A to get to B. * * @param thetaA * The azimuth of point A * @param phiA * The polar angle of point A * @param thetaB * The azimuth of point B * @param phiB * The polar angle of point B * @return The angle from A to B, in radians, counterclockwise from due East */ public static double getAzimuth (double thetaA, double phiA, double thetaB, double phiB) { if (equal(0.0, phiA)) return -Math.PI / 2.0; if (equal(Math.PI, phiA)) return Math.PI / 2.0; Vector A = toUnitVector(thetaA, phiA); Vector B = toUnitVector(thetaB, phiB); Vector AB = A.cross(B).cross(A); Vector AN = A.cross(Z).cross(A); Vector AE = AN.cross(A); double deltaX = AB.dot(AE); double deltaY = AB.dot(AN); return Math.atan2(deltaY, deltaX); }
/** * Get the simple cartesian distance between this and another vector. */ public double getDistance (Vector other) { return Math.sqrt(getDistanceSquared(other)); }