@Test public void testToVector3() { Vector4 x = new Vector4(3, 5, 6, 7); Vector3 y = new Vector3(3, 5, 6); assertTrue(x.toVector3().equals(y)); }
@Test public void testHashCode() { Vector4 x = new Vector4(5, 27, 1, 2); Vector4 y = new Vector4(5, -3, 0, 1); doAssertDouble(-1677538473, x.hashCode()); doAssertDouble(2032847703, y.hashCode()); }
@Test public void testNormalize() { Vector4 x = new Vector4(3, 4, 5, 6); Vector4 y = x.normalize(); doAssertDouble(0.323, y.x); doAssertDouble(0.431, y.y); doAssertDouble(1, y.length()); }
@Test public void testCompareTo() { assertTrue(Vector4.ZERO.compareTo(Vector4.ONE) < 0); Vector4 x = new Vector4(5, 3, 4, 6); Vector4 y = new Vector4(-2, 5, -2, 4); assertTrue(x.compareTo(y) >= 0); }
@Test public void testEquals() { Vector4 x = new Vector4(1, 1, 1, 1); Vector4 y = new Vector4(1, 1, 1, 1); Vector4 z = new Vector4(1, 2, 1, 1); assertTrue(x.equals(y)); assertFalse(x.equals(z)); }
@Test public void testRound() { Vector4 x = new Vector4(1.4, -0.2, -2.4, 3.4); Vector4 y = x.round(); doAssertDouble(1, y.x); doAssertDouble(0, y.y); doAssertDouble(-2, y.z); doAssertDouble(3, y.w); }
@Test public void testFloor() { Vector4 x = new Vector4(1.4, -0.2, -2.4, 3.4); Vector4 y = x.floor(); doAssertDouble(1, y.x); doAssertDouble(-1, y.y); doAssertDouble(-3, y.z); doAssertDouble(3, y.w); }
@Test public void testCeil() { Vector4 x = new Vector4(1.4, -0.2, -2.4, 3.4); Vector4 y = x.ceil(); doAssertDouble(2, y.x); doAssertDouble(0, y.y); doAssertDouble(-2, y.z); doAssertDouble(4, y.w); }
@Test public void testDot() { Vector4 x = new Vector4(2, 3, 4, 5); doAssertDouble("x dot x should be 54", 54, x.dot(x)); x = new Vector4(3, 2, 4, 5); Vector4 y = new Vector4(4, -1, 4, 2); doAssertDouble("x dot y should be 36", 36, x.dot(y)); }
@Test public void testAbs() { Vector4 x = new Vector4(1.4, -0.2, -2.4, 3.4); Vector4 y = x.abs(); doAssertDouble(1.4, y.x); doAssertDouble(0.2, y.y); doAssertDouble(2.4, y.z); doAssertDouble(3.4, y.w); }
@Test public void testAddDouble() { Vector4 a = new Vector4(1, -1, 3, 5); Vector4 c = a.add(2d, 6d, -2d, 5d); doAssertDouble(3, c.x); doAssertDouble(5, c.y); doAssertDouble(1, c.z); doAssertDouble(10, c.w); }
@Test public void testSubtractInt() { Vector4 a = new Vector4(1, -1, 4, -2); Vector4 c = a.subtract(2, 6, 4, 1); doAssertDouble(-1, c.x); doAssertDouble(-7, c.y); doAssertDouble(0, c.z); doAssertDouble(-3, c.w); }
@Test public void testSubtractDouble() { Vector4 a = new Vector4(1, -1, 4, -2); Vector4 c = a.subtract(2d, 6d, 4d, 1d); doAssertDouble(-1, c.x); doAssertDouble(-7, c.y); doAssertDouble(0, c.z); doAssertDouble(-3, c.w); }
@Test public void testSubtractFloat() { Vector4 a = new Vector4(1, -1, 4, -2); Vector4 c = a.subtract(2f, 6f, 4f, 1f); doAssertDouble(-1, c.x); doAssertDouble(-7, c.y); doAssertDouble(0, c.z); doAssertDouble(-3, c.w); }
@Test public void testAddInt() { Vector4 a = new Vector4(1, -1, 3, 5); Vector4 c = a.add(2, 6, -2, 5); doAssertDouble(3, c.x); doAssertDouble(5, c.y); doAssertDouble(1, c.z); doAssertDouble(10, c.w); }
@Test public void testDistance() { Vector4 x = new Vector4(2, 3, 4, 5); Vector4 y = new Vector4(1, 2, 3, 4); doAssertDouble(2, x.distance(y)); x = new Vector4(10, 3, 6, -5); y = new Vector4(7, 3, -2, 1); doAssertDouble(Math.sqrt(109), x.distance(y)); }
@Test public void testAddFloat() { Vector4 a = new Vector4(1, -1, 3, 5); Vector4 c = a.add(2f, 6f, -2f, 5f); doAssertDouble(3, c.x); doAssertDouble(5, c.y); doAssertDouble(1, c.z); doAssertDouble(10, c.w); }
@Test public void testToArray() { Vector4 x = new Vector4(5, 3, 6, 7); float[] r = x.toArray(); assertArrayEquals(new float[] {5, 3, 6, 7}, r, (float) eps); doAssertDouble(5, r[0]); doAssertDouble(3, r[1]); doAssertDouble(6, r[2]); doAssertDouble(7, r[3]); }
@Test public void testMultiplyVector4() { Vector4 a = new Vector4(1, -1, 4, 2); Vector4 b = new Vector4(2, 6, 4, 1); Vector4 c = a.multiply(b); doAssertDouble(2, c.x); doAssertDouble(-6, c.y); doAssertDouble(16, c.z); doAssertDouble(2, c.w); }
@Test public void testMultiplyFloat() { Vector4 a = new Vector4(1, -1, 4, 2); Vector4 b = a.multiply(2f, 6f, 4f, 1f); doAssertDouble(2, b.x); doAssertDouble(-6, b.y); doAssertDouble(16, b.z); doAssertDouble(2, b.w); Vector4 c = a.multiply(2.0F); doAssertDouble(2, c.x); doAssertDouble(-2, c.y); doAssertDouble(8, c.z); doAssertDouble(4, c.w); }
@Test public void testMultiplyDouble() { Vector4 a = new Vector4(1, -1, 4, 2); Vector4 b = a.multiply(2d, 6d, 4d, 1d); doAssertDouble(2, b.x); doAssertDouble(-6, b.y); doAssertDouble(16, b.z); doAssertDouble(2, b.w); Vector4 c = a.multiply(2.0d); doAssertDouble(2, c.x); doAssertDouble(-2, c.y); doAssertDouble(8, c.z); doAssertDouble(4, c.w); }
@Test public void testMultiplyInt() { Vector4 a = new Vector4(1, -1, 4, 2); Vector4 b = a.multiply(2, 6, 4, 1); doAssertDouble(2, b.x); doAssertDouble(-6, b.y); doAssertDouble(16, b.z); doAssertDouble(2, b.w); Vector4 c = a.multiply(2.0); doAssertDouble(2, c.x); doAssertDouble(-2, c.y); doAssertDouble(8, c.z); doAssertDouble(4, c.w); }
/** * Intersect a Sphere with a ray. Returns the t value(s) for the ray at the solution(s) (if any). * * @param sphere Sphere to intersect the Ray with * @param ray Ray on intersect the Sphere with * @return Solutions. May be 0, 1 or 2 solutions */ public static Solutions intersect(Sphere sphere, Ray ray) { double a = Vector4.dotProduct(ray.u, ray.u); Vector4 centerOrigin = new Vector4(sphere.center, ray.p0); double b = 2 * (Vector4.dotProduct(centerOrigin, ray.u)); double c = Vector4.dotProduct(centerOrigin, centerOrigin) - sphere.radius * sphere.radius; double det = b * b - 4 * a * c; if (det < 0) { // No solutions return new Solutions(0, 0, 0); } else if (det > 0) { // Two solutions double sol1 = (-b - Math.sqrt(det)) / (2 * a); double sol2 = (-b + Math.sqrt(det)) / (2 * a); return new Solutions(2, sol1, sol2); } else { // One solution double sol = (-b) / (2 * a); return new Solutions(1, sol, 0); } }
@Test public void testRand() { for (int i = 0; i < 100; ++i) { Vector4 x = Vector4.rand(); assertTrue(x.x >= -1); assertTrue(x.x <= 1); assertTrue(x.y >= -1); assertTrue(x.y <= 1); assertTrue(x.z >= -1); assertTrue(x.z <= 1); assertTrue(x.w >= -1); assertTrue(x.w <= 1); } }
@Test public void testPow() { Vector4 x = new Vector4(1, 2, 3, 4); Vector4 y = x.pow(3); doAssertDouble(1, y.x); doAssertDouble(8, y.y); doAssertDouble(27, y.z); doAssertDouble(64, y.w); x = new Vector4(1, 2, 3, 4); y = x.pow(2); doAssertDouble(1, y.x); doAssertDouble(4, y.y); doAssertDouble(9, y.z); doAssertDouble(16, y.w); x = new Vector4(25, 16, 9, 4); y = x.pow(0.5); doAssertDouble(5, y.x); doAssertDouble(4, y.y); doAssertDouble(3, y.z); doAssertDouble(2, y.w); }
static void vectorBasedMul(Matrix4 a, Matrix4 b, Matrix4 result) { Vector4[] row = new Vector4[4]; Vector4[] col = new Vector4[4]; for (int i = 0; i < 4; i++) { row[i] = new Vector4( a.values[i * 4], a.values[i * 4 + 1], a.values[i * 4 + 2], a.values[i * 4 + 3]); col[i] = new Vector4(b.values[i], b.values[i + 4], b.values[i + 8], b.values[i + 12]); } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result.values[i * 4 + j] = Vector4.dot(row[i], col[j]); } } }
public Vector4i(Vector4 vec) { vec.toVector4i(this); }
/** * Call the shader to determine the color a the point of intersection determined by the ray and * parameter minT * * @param ray ray that determines the point * @param minT value of parameter t * @return Colour determined by the shader for this point */ public Colour callShader(Ray ray, double minT) { Point point = ray.evaluate(minT); Vector4 normal = new Vector4(center, point); normal.normalize(); return Shader.computeColor(point, normal, material); }
/** * Returns the normal of the sphere at point p. Point p is assumed to be at the surface of the * sphere * * @param p point at the surface * @return normal at point p */ public Vector4 computeNormal(Point p) { Vector4 normal = new Vector4(center, p); normal.normalize(); return normal; }
@Test public void testMax() { Vector4 x = new Vector4(5, -15, 3, 1); Vector4 y = new Vector4(3, 2, 5, -1); assertEquals(new Vector4(5, 2, 5, 1), Vector4.max(x, y)); }