package com.pixldevelopment.wastelandengine.other.math.Vector;
import org.lwjgl.BufferUtils;
import java.nio.FloatBuffer;
/**
* An position consisting of 3 floats.
*
* @author simengangstad
* @since 08.06.13
*/
public class Vector3 implements Vector<Vector3>
{
/**
* The floats in the vector3.
*/
private float x, y, z;
/**
* Initializes the vector3 with float data.
*
* @param x The x.
* @param y The y.
* @param z The z.
*/
public Vector3(float x, float y, float z)
{
this.setX(x);
this.setY(y);
this.setZ(z);
}
/**
* Initializes the vector3 without any data.
*/
public Vector3()
{
this(0.0f, 0.0f, 0.0f);
}
/**
* Initializes the vector3 with vector4 data.
*
* @param vector The vector4.
*/
public Vector3(Vector4 vector)
{
this(vector.getX(), vector.getY(), vector.getZ());
}
/**
* Initializes the vector3 with vector3 data.
*
* @param vector The vector3.
*/
public Vector3(Vector3 vector)
{
this(vector.getX(), vector.getY(), vector.getZ());
}
/**
* Initializes the vector3 with vector2 data.
*
* @param vector The vector2.
*/
public Vector3(Vector2 vector)
{
this(vector.getX(), vector.getY(), 0.0f);
}
/**
* @return The x value.
*/
public float getX()
{
return this.x;
}
/**
* Sets the x value of the vector3.
*
* @param x The x value.
*
* @return The new vector3.
*/
public Vector3 setX(float x)
{
this.x = x;
return this;
}
/**
* @return The y value.
*/
public float getY()
{
return this.y;
}
/**
* Sets the y value of the vector3.
*
* @param y The y value.
*
* @return The new vector3.
*/
public Vector3 setY(float y)
{
this.y = y;
return this;
}
/**
* @return The z value.
*/
public float getZ()
{
return this.z;
}
/**
* Sets the z value of the vector3.
*
* @param z The z value.
*
* @return The new vector3.
*/
public Vector3 setZ(float z)
{
this.z = z;
return this;
}
@Override
/**
* @return A copy of the current vector3.
*/
public Vector3 copy()
{
return new Vector3(this);
}
@Override
/**
* Clears the vector3.
*
* @return The new vector3.
*/
public Vector3 clear()
{
this.setX(0.0f);
this.setY(0.0f);
this.setZ(0.0f);
return this;
}
@Override
/**
* @return The length of the vector3.
*/
public float length()
{
return (float) Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() * this.getZ());
}
@Override
/**
* Normalizes the vector3.
*
* @return The new vector3.
*/
public Vector3 normalize()
{
this.setX(this.x /= this.length());
this.setY(this.y /= this.length());
this.setZ(this.z /= this.length());
return this;
}
@Override
/**
* Negates the vector3.
*
* @return The new vector3.
*/
public Vector3 negate()
{
this.setX(-this.getX());
this.setY(-this.getY());
this.setZ(-this.getZ());
return this;
}
/**
* Adds x, y and z values to the vector3.
*
* @param x The x value.
* @param y The y value.
* @param z The z value.
*
* @return The new vector3.
*/
public Vector3 add(float x, float y, float z)
{
this.setX(this.x += x);
this.setY(this.y += y);
this.setZ(this.z += z);
return this;
}
@Override
/**
* Adds a vector3 to the vector3.
*
* @param vector The vector3 that shall be added.
*
* @return The new vector3.
*/
public Vector3 add(Vector3 vector)
{
return this.add(vector.getX(), vector.getY(), vector.getZ());
}
/**
* Subtracts XYZ values of the vector3.
*
* @param x The x value.
* @param y The y value.
* @param z The z value.
*
* @return The new vector3.
*/
public Vector3 subtract(float x, float y, float z)
{
this.setX(this.x -= x);
this.setY(this.y -= y);
this.setZ(this.z -= z);
return this;
}
@Override
/**
* Subtracts a vector3 of the vector3.
*
* @param vector The vector3 that shall be subtracted.
*
* @return The new vector3.
*/
public Vector3 subtract(Vector3 vector)
{
return this.subtract(vector.getX(), vector.getY(), vector.getZ());
}
@Override
/**
* Multiplies the vector3 by a vector3.
*
* @param vector The vector3 the vector3 shall be multiplied with.
*
* @return The new vector3.
*/
public Vector3 multiply(Vector3 vector)
{
this.setX(this.x *= vector.getX());
this.setY(this.y *= vector.getY());
this.setZ(this.z *= vector.getZ());
return this;
}
@Override
/**
* Multiplies the vector3 by a value.
*
* @param value The value the vector3 shall be multiplied with.
*
* @return The new vector3.
*/
public Vector3 multiply(float value)
{
return this.multiply(new Vector3(value, value, value));
}
@Override
/**
* Divides the vector3 by a vector3.
*
* @param vector The vector3 the vector3 shall be divided with.
*
* @return The new vector3.
*/
public Vector3 divide(Vector3 vector)
{
this.setX(this.x /= vector.getX());
this.setY(this.y /= vector.getY());
this.setZ(this.z /= vector.getZ());
return this;
}
@Override
/**
* Divides the vector3 by a value.
*
* @param value The value the vector3 shall be divided with.
*
* @return The new vector3.
*/
public Vector3 divide(float value)
{
return this.divide(new Vector3(value, value, value));
}
/**
* Calculates the dot product of the current vector3 and another one.
*
* @param vector The another vector3.
*
* @return The dot product of the current vector3 and the another one.
*/
public float dotProduct(Vector3 vector)
{
return this.getX() * vector.getX() + this.getY() * vector.getY() + this.getZ() * vector.getZ();
}
/**
* Calculates the cross product of the current vector3 and another one.
*
* @param vector The another vector3.
*
* @return The cross product of the current vector3 and the another one.
*/
public Vector3 crossProduct(Vector3 vector)
{
return new Vector3(this.getY() * vector.getZ() - vector.getY() * this.getZ(), this.getZ() * vector.getX() - vector.getZ() * this.getX(), this.getX() * vector.getY() - vector.getX() * this.getY());
}
/**
* Computes a normal from three vertices.
*
* @param v0 The first vertex.
* @param v1 The second vertex.
* @param v2 The third vertex.
*
* @return A normal based on the three vertices.
*/
public Vector3 computeNormal(Vector3 v0, Vector3 v1, Vector3 v2)
{
Vector3 vert1 = new Vector3(v1.getX() - v0.getX(), v1.getY() - v0.getY(), v1.getZ() - v0.getZ());
Vector3 vert2 = new Vector3(v2.getX() - v0.getX(), v2.getY() - v0.getY(), v2.getZ() - v0.getZ());
return vert1.crossProduct(vert2).normalize();
}
@Override
/**
* @return The vector3 as an array of floats.
*/
public float[] toFloats()
{
return new float[] {this.getX(),
this.getY(),
this.getZ()};
}
@Override
/**
* @return The vector3 as a FloatBuffer.
*/
public FloatBuffer toBuffer()
{
return (FloatBuffer) BufferUtils.createFloatBuffer(3).put(this.toFloats()).flip();
}
@Override
/**
* Check if this vector3 and another vector3 equals.
*
* @param object The other vector3.
*
* @return If the vector3s equals.
*/
public boolean equals(Object object)
{
if (!(object instanceof Vector3))
{
return false;
}
Vector3 vector = (Vector3) object;
return (this.getX() == vector.getX() &&
this.getY() == vector.getY() &&
this.getZ() == vector.getZ());
}
@Override
/**
* @return The data of the vector3 in form of a String.
*/
public String toString()
{
return this.getX() + ", " + this.getY() + ", " + this.getZ();
}
}