![JAR search and dependency download from the Maven repository](/logo.png)
de.lessvoid.math.Vec3 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coregl-utils Show documentation
Show all versions of coregl-utils Show documentation
simple utility methods to make life with OpenGL core profile simpler
The newest version!
/*
* Copyright (c) 2002-2008 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package de.lessvoid.math;
import java.io.Serializable;
import java.nio.FloatBuffer;
/**
*
* Holds a 3-tuple vector.
*
* @author cix_foo
* @author void256 - removed the interfaces this class implements and remove the
* base class to make that a simple and plain self-contained class
* @version $Revision$ $Id$
*/
public class Vec3 implements Serializable {
private static final long serialVersionUID = 1L;
public float x, y, z;
/**
* Constructor for Vector3f.
*/
public Vec3() {
super();
}
/**
* Constructor
*/
public Vec3(final Vec3 src) {
set(src);
}
/**
* Constructor
*/
public Vec3(final float x, final float y, final float z) {
set(x, y, z);
}
/*
* (non-Javadoc)
*
* @see org.lwjgl.util.vector.WritableVector2f#set(float, float)
*/
public void set(final float x, final float y) {
this.x = x;
this.y = y;
}
/*
* (non-Javadoc)
*
* @see org.lwjgl.util.vector.WritableVector3f#set(float, float, float)
*/
public void set(final float x, final float y, final float z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Load from another Vector3f
*
* @param src
* The source vector
* @return this
*/
public Vec3 set(final Vec3 src) {
x = src.getX();
y = src.getY();
z = src.getZ();
return this;
}
/**
* @return the length squared of the vector
*/
public float lengthSquared() {
return x * x + y * y + z * z;
}
public float length() {
return (float) Math.sqrt(lengthSquared());
}
/**
* Translate a vector
*
* @param x
* The translation in x
* @param y
* the translation in y
* @return this
*/
public Vec3 translate(final float x, final float y, final float z) {
this.x += x;
this.y += y;
this.z += z;
return this;
}
/**
* Add a vector to another vector and place the result in a destination
* vector.
*
* @param left
* The LHS vector
* @param right
* The RHS vector
* @param dest
* The destination vector, or null if a new vector is to be created
* @return the sum of left and right in dest
*/
public static Vec3 add(final Vec3 left, final Vec3 right, final Vec3 dest) {
if (dest == null) return new Vec3(left.x + right.x, left.y + right.y, left.z + right.z);
else {
dest.set(left.x + right.x, left.y + right.y, left.z + right.z);
return dest;
}
}
/**
* Subtract a vector from another vector and place the result in a destination
* vector.
*
* @param left
* The LHS vector
* @param right
* The RHS vector
* @param dest
* The destination vector, or null if a new vector is to be created
* @return left minus right in dest
*/
public static Vec3 sub(final Vec3 left, final Vec3 right, final Vec3 dest) {
if (dest == null) return new Vec3(left.x - right.x, left.y - right.y, left.z - right.z);
else {
dest.set(left.x - right.x, left.y - right.y, left.z - right.z);
return dest;
}
}
/**
* The cross product of two vectors.
*
* @param left
* The LHS vector
* @param right
* The RHS vector
* @param dest
* The destination result, or null if a new vector is to be created
* @return left cross right
*/
public static Vec3 cross(final Vec3 left, final Vec3 right, Vec3 dest) {
if (dest == null) dest = new Vec3();
dest.set(left.y * right.z - left.z * right.y,
right.x * left.z - right.z * left.x,
left.x * right.y - left.y * right.x);
return dest;
}
/**
* Negate a vector
*
* @return this
*/
public Vec3 negate() {
x = -x;
y = -y;
z = -z;
return this;
}
/**
* Negate a vector and place the result in a destination vector.
*
* @param dest
* The destination vector or null if a new vector is to be created
* @return the negated vector
*/
public Vec3 negate(Vec3 dest) {
if (dest == null) dest = new Vec3();
dest.x = -x;
dest.y = -y;
dest.z = -z;
return dest;
}
/**
* Normalise this vector and place the result in another vector.
*
* @param dest
* The destination vector, or null if a new vector is to be created
* @return the normalised vector
*/
public Vec3 normalise(Vec3 dest) {
final float l = length();
if (dest == null) dest = new Vec3(x / l, y / l, z / l);
else dest.set(x / l, y / l, z / l);
return dest;
}
/**
* The dot product of two vectors is calculated as v1.x * v2.x + v1.y * v2.y +
* v1.z * v2.z
*
* @param left
* The LHS vector
* @param right
* The RHS vector
* @return left dot right
*/
public static float dot(final Vec3 left, final Vec3 right) {
return left.x * right.x + left.y * right.y + left.z * right.z;
}
/**
* Calculate the angle between two vectors, in radians
*
* @param a
* A vector
* @param b
* The other vector
* @return the angle between the two vectors, in radians
*/
public static float angle(final Vec3 a, final Vec3 b) {
float dls = dot(a, b) / (a.length() * b.length());
if (dls < -1f) dls = -1f;
else if (dls > 1.0f) dls = 1.0f;
return (float) Math.acos(dls);
}
/*
* (non-Javadoc)
*
* @see org.lwjgl.vector.Vector#load(FloatBuffer)
*/
public Vec3 load(final FloatBuffer buf) {
x = buf.get();
y = buf.get();
z = buf.get();
return this;
}
/*
* (non-Javadoc)
*
* @see org.lwjgl.vector.Vector#scale(float)
*/
public Vec3 scale(final float scale) {
x *= scale;
y *= scale;
z *= scale;
return this;
}
/*
* (non-Javadoc)
*
* @see org.lwjgl.vector.Vector#store(FloatBuffer)
*/
public Vec3 store(final FloatBuffer buf) {
buf.put(x);
buf.put(y);
buf.put(z);
return this;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
final StringBuilder sb = new StringBuilder(64);
sb.append("Vector3f[");
sb.append(x);
sb.append(", ");
sb.append(y);
sb.append(", ");
sb.append(z);
sb.append(']');
return sb.toString();
}
/**
* @return x
*/
public final float getX() {
return x;
}
/**
* @return y
*/
public final float getY() {
return y;
}
/**
* Set X
*
* @param x
*/
public final void setX(final float x) {
this.x = x;
}
/**
* Set Y
*
* @param y
*/
public final void setY(final float y) {
this.y = y;
}
/**
* Set Z
*
* @param z
*/
public void setZ(final float z) {
this.z = z;
}
/*
* (Overrides)
*
* @see org.lwjgl.vector.ReadableVector3f#getZ()
*/
public float getZ() {
return z;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy