/** * Divides the values of this vector by the given scalar value and returns the result in store. * * @param scalar * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new vector (this.x / scalar, this.y / scalar, this.z / scalar, this.w / scalar) */ @Override public Vector4 divide(final double scalar, final Vector4 store) { Vector4 result = store; if (result == null) { result = new Vector4(); } return result.set(getX() / scalar, getY() / scalar, getZ() / scalar, getW() / scalar); }
/** * Increments the values of this vector with the given x, y, z and w values. * * @param x * @param y * @param z * @param w * @return this vector for chaining */ public Vector4 addLocal(final double x, final double y, final double z, final double w) { return set(getX() + x, getY() + y, getZ() + z, getW() + w); }
/** * Creates a new unit length vector from this one by dividing by length. If the length is 0, (ie, if the vector is * 0, 0, 0, 0) then a new vector (0, 0, 0, 0) is returned. * * @param store * the vector to store the result in for return. If null, a new vector object is created and returned. * @return a new unit vector (or 0, 0, 0, 0 if this unit is 0 length) */ @Override public Vector4 normalize(final Vector4 store) { final double lengthSq = lengthSquared(); if (Math.abs(lengthSq) > MathUtils.EPSILON) { return multiply(MathUtils.inverseSqrt(lengthSq), store); } return store != null ? store.set(Vector4.ZERO) : new Vector4(Vector4.ZERO); }
@Test public void testGetSet() { final Vector4 vec1 = new Vector4(); vec1.setX(0); assertTrue(vec1.getX() == 0.0); vec1.setX(Double.POSITIVE_INFINITY); assertTrue(vec1.getX() == Double.POSITIVE_INFINITY); vec1.setX(Double.NEGATIVE_INFINITY); assertTrue(vec1.getX() == Double.NEGATIVE_INFINITY); assertTrue(vec1.getValue(0) == Double.NEGATIVE_INFINITY); vec1.setY(0); assertTrue(vec1.getY() == 0.0); vec1.setY(Double.POSITIVE_INFINITY); assertTrue(vec1.getY() == Double.POSITIVE_INFINITY); vec1.setY(Double.NEGATIVE_INFINITY); assertTrue(vec1.getY() == Double.NEGATIVE_INFINITY); assertTrue(vec1.getValue(1) == Double.NEGATIVE_INFINITY); vec1.setZ(0); assertTrue(vec1.getZ() == 0.0); vec1.setZ(Double.POSITIVE_INFINITY); assertTrue(vec1.getZ() == Double.POSITIVE_INFINITY); vec1.setZ(Double.NEGATIVE_INFINITY); assertTrue(vec1.getZ() == Double.NEGATIVE_INFINITY); assertTrue(vec1.getValue(2) == Double.NEGATIVE_INFINITY); vec1.setW(0); assertTrue(vec1.getW() == 0.0); vec1.setW(Double.POSITIVE_INFINITY);
final double dv = 1.0f / (double) (sizeY - 1); final Vector4 pointTop = Vector4.fetchTempInstance(); final Vector4 pointFinal = Vector4.fetchTempInstance(); final Vector4 pointBottom = Vector4.fetchTempInstance(); for (int y = smallerFrom; y < biggerTo; y++) { for (int x = 0; x < sizeX; x++) { pointTop.lerpLocal(intersectTopLeft, intersectTopRight, u); pointBottom.lerpLocal(intersectBottomLeft, intersectBottomRight, u); pointFinal.lerpLocal(pointTop, pointBottom, v); pointFinal.setX(pointFinal.getX() / pointFinal.getW()); pointFinal.setZ(pointFinal.getZ() / pointFinal.getW()); pointFinal.setY(heightGenerator.getHeight(pointFinal.getX(), pointFinal.getZ(), time)); vertBufArray[index++] = pointFinal.getXf(); vertBufArray[index++] = pointFinal.getYf(); vertBufArray[index++] = pointFinal.getZf(); Vector4.releaseTempInstance(pointTop); Vector4.releaseTempInstance(pointFinal); Vector4.releaseTempInstance(pointBottom);
public Vector3 getNormalizedDeviceCoordinates(final ReadOnlyVector3 worldPosition, Vector3 store) { if (store == null) { store = new Vector3(); } checkModelViewProjection(); final Vector4 position = Vector4.fetchTempInstance(); position.set(worldPosition.getX(), worldPosition.getY(), worldPosition.getZ(), 1); _modelViewProjection.applyPre(position, position); position.multiplyLocal(1.0 / position.getW()); store.setX(position.getX()); store.setY(position.getY()); store.setZ(position.getZ()); Vector4.releaseTempInstance(position); return store; }
private void setTextureData() { // x = left, y = top, z = right, w = bottom final FloatBuffer coords = _meshData.getTextureBuffer(0); coords.rewind(); coords.put(0).put(0); coords.put(_textureBorderOffsets.getXf()).put(0); coords.put(1f - _textureBorderOffsets.getZf()).put(0); coords.put(1).put(0); coords.put(0).put(_textureBorderOffsets.getWf()); coords.put(_textureBorderOffsets.getXf()).put(_textureBorderOffsets.getWf()); coords.put(1f - _textureBorderOffsets.getZf()).put(_textureBorderOffsets.getWf()); coords.put(1).put(_textureBorderOffsets.getWf()); coords.put(0).put(1f - _textureBorderOffsets.getYf()); coords.put(_textureBorderOffsets.getXf()).put(1f - _textureBorderOffsets.getYf()); coords.put(1f - _textureBorderOffsets.getZf()).put(1f - _textureBorderOffsets.getYf()); coords.put(1).put(1f - _textureBorderOffsets.getYf()); coords.put(0).put(1); coords.put(_textureBorderOffsets.getXf()).put(1); coords.put(1f - _textureBorderOffsets.getZf()).put(1); coords.put(1).put(1); _meshData.markBufferDirty(MeshData.KEY_TextureCoords0); }
if (value instanceof Vector4) { final Vector4 vec = (Vector4) value; return stack.mallocDouble(4).put(vec.getX()).put(vec.getY()).put(vec.getZ()).put(vec.getW()).flip(); if (value instanceof Vector4) { final Vector4 vec = (Vector4) value; return stack.mallocFloat(4).put(vec.getXf()).put(vec.getYf()).put(vec.getZf()).put(vec.getWf()) .flip();
private void calculateIntersection(final double planeHeight, final ReadOnlyVector2 screenPosition, final ReadOnlyMatrix4 modelViewProjectionInverseMatrix) { origin.set(screenPosition.getX() * 2 - 1, screenPosition.getY() * 2 - 1, -1, 1); direction.set(screenPosition.getX() * 2 - 1, screenPosition.getY() * 2 - 1, 1, 1); modelViewProjectionInverseMatrix.applyPre(origin, origin); modelViewProjectionInverseMatrix.applyPre(direction, direction); direction.subtractLocal(origin); // final double t = (planeHeight * origin.getW() - origin.getY()) // / (direction.getY() - planeHeight * direction.getW()); if (Math.abs(direction.getY()) > MathUtils.EPSILON) { final double t = (planeHeight - origin.getY()) / direction.getY(); direction.multiplyLocal(t); } else { direction.normalizeLocal(); direction.multiplyLocal(mainCamera.getFrustumFar()); } origin.addLocal(direction); }
/** * Sets the value of this vector to (0, 0, 0, 0) * * @return this vector for chaining */ public Vector4 zero() { return set(0, 0, 0, 0); }
@Override public Vector4 clone() { return new Vector4(this); }
@Test public void testApplyVector4() { final Matrix4 mat4 = new Matrix4().applyRotationX(MathUtils.HALF_PI); final Vector4 vec4 = new Vector4(0, 1, 0, 1); final Vector4 result = mat4.applyPost(vec4, null); assertTrue(Math.abs(new Vector4(0, 0, 1, 1).distance(result)) <= MathUtils.EPSILON); vec4.set(0, 1, 1, 1); mat4.applyPost(vec4, result); assertTrue(Math.abs(new Vector4(0, -1, 1, 1).distance(result)) <= MathUtils.EPSILON); vec4.set(0, 1, 1, 1); mat4.applyPre(vec4, result); assertTrue(Math.abs(new Vector4(0, 1, -1, 1).distance(result)) <= MathUtils.EPSILON); vec4.set(1, 1, 1, 1); assertTrue(Math.abs(new Vector4(1, 1, -1, 1).distance(mat4.applyPre(vec4, null))) <= MathUtils.EPSILON); }
@Test public void testAdd() { final Vector4 vec1 = new Vector4(); final Vector4 vec2 = new Vector4(Vector4.ONE); vec1.addLocal(1, 2, 3, 4); assertEquals(new Vector4(1, 2, 3, 4), vec1); vec1.addLocal(-1, -2, -3, -4); assertEquals(Vector4.ZERO, vec1); vec1.zero(); vec1.addLocal(vec2); assertEquals(Vector4.ONE, vec1); vec1.zero(); final Vector4 vec3 = vec1.add(vec2, new Vector4()); assertEquals(Vector4.ZERO, vec1); assertEquals(Vector4.ONE, vec3); final Vector4 vec4 = vec1.add(0, 0, 0, 1, null); assertEquals(Vector4.ZERO, vec1); assertEquals(Vector4.UNIT_W, vec4); }
@Test public void testNormalize() { final Vector4 vec1 = new Vector4(2, 1, 3, -1); assertTrue(vec1.length() == Math.sqrt(15)); final Vector4 vec2 = vec1.normalize(null); final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1 + 3 * 3 + -1 * -1); assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec2); vec1.normalizeLocal(); assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec1); vec1.zero(); vec1.normalize(vec2); assertEquals(vec1, vec2); // ensure no exception thrown vec1.normalizeLocal(); vec1.normalize(null); }
/** * Add to a Vector4 in-buffer. * * @param toAdd * the vector to add from * @param buf * the buffer to find the Vector4 within * @param index * the position (in terms of vectors, not floats) of the vector to add to */ public static void addInBuffer(final ReadOnlyVector4 toAdd, final FloatBuffer buf, final int index) { final Vector4 temp = Vector4.fetchTempInstance(); populateFromBuffer(temp, buf, index); temp.addLocal(toAdd); setInBuffer(temp, buf, index); Vector4.releaseTempInstance(temp); }
@Test public void testScaleAdd() { final Vector4 vec1 = new Vector4(1, 1, 1, 1); final Vector4 vec2 = vec1.scaleAdd(2.0, new Vector4(1, 2, 3, 4), null); final Vector4 vec2B = vec1.scaleAdd(2.0, new Vector4(1, 2, 3, 4), new Vector4()); assertEquals(new Vector4(3.0, 4.0, 5.0, 6.0), vec2); assertEquals(new Vector4(3.0, 4.0, 5.0, 6.0), vec2B); vec1.scaleAddLocal(2.0, new Vector4(1, 2, 3, 4)); assertEquals(vec2, vec1); }
@Test public void testNegate() { final Vector4 vec1 = new Vector4(3, 2, -1, 1); final Vector4 vec2 = vec1.negate(null); assertEquals(new Vector4(-3, -2, 1, -1), vec2); vec1.negateLocal(); assertEquals(vec2, vec1); }
@Test public void testSimpleHash() { // Just a simple sanity check. final Vector4 vec1 = new Vector4(1, 2, 3, 4); final Vector4 vec2 = new Vector4(1, 2, 3, 4); final Vector4 vec3 = new Vector4(2, 2, 2, 2); assertTrue(vec1.hashCode() == vec2.hashCode()); assertTrue(vec1.hashCode() != vec3.hashCode()); } }