/*
* Copyright (c) 2011, 2015, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* 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 Oracle Corporation 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 com.javafx.experiments.utils3d.geom;
/**
* A 3-dimensional, single-precision, floating-point vector.
*
*/
public class Vec3f {
/**
* The x coordinate.
*/
public float x;
/**
* The y coordinate.
*/
public float y;
/**
* The z coordinate.
*/
public float z;
public Vec3f() { }
public Vec3f(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vec3f(Vec3f v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void set(Vec3f v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
}
public void set(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public final void mul(float s) {
this.x *= s;
this.y *= s;
this.z *= s;
}
/**
* Sets the value of this vector to the difference
* of vectors t1 and t2 (this = t1 - t2).
* @param t1 the first vector
* @param t2 the second vector
*/
public void sub(Vec3f t1, Vec3f t2) {
this.x = t1.x - t2.x;
this.y = t1.y - t2.y;
this.z = t1.z - t2.z;
}
/**
* Sets the value of this vector to the difference of
* itself and vector t1 (this = this - t1) .
* @param t1 the other vector
*/
public void sub(Vec3f t1) {
this.x -= t1.x;
this.y -= t1.y;
this.z -= t1.z;
}
/**
* Sets the value of this vector to the sum
* of vectors t1 and t2 (this = t1 + t2).
* @param t1 the first vector
* @param t2 the second vector
*/
public void add(Vec3f t1, Vec3f t2) {
this.x = t1.x + t2.x;
this.y = t1.y + t2.y;
this.z = t1.z + t2.z;
}
/**
* Sets the value of this vector to the sum of
* itself and vector t1 (this = this + t1) .
* @param t1 the other vector
*/
public void add(Vec3f t1) {
this.x += t1.x;
this.y += t1.y;
this.z += t1.z;
}
/**
* Returns the length of this vector.
* @return the length of this vector
*/
public float length() {
return (float) Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
}
/**
* Normalize this vector.
*/
public void normalize() {
float norm = 1.0f / length();
this.x = this.x * norm;
this.y = this.y * norm;
this.z = this.z * norm;
}
/**
* Sets this vector to be the vector cross product of vectors v1 and v2.
* @param v1 the first vector
* @param v2 the second vector
*/
public void cross(Vec3f v1, Vec3f v2) {
float tmpX;
float tmpY;
tmpX = v1.y * v2.z - v1.z * v2.y;
tmpY = v2.x * v1.z - v2.z * v1.x;
this.z = v1.x * v2.y - v1.y * v2.x;
this.x = tmpX;
this.y = tmpY;
}
/**
* Computes the dot product of this vector and vector v1.
* @param v1 the other vector
* @return the dot product of this vector and v1
*/
public float dot(Vec3f v1) {
return this.x * v1.x + this.y * v1.y + this.z * v1.z;
}
/**
* Returns the hashcode for this Vec3f
.
* @return a hash code for this Vec3f
.
*/
@Override
public int hashCode() {
int bits = 7;
bits = 31 * bits + Float.floatToIntBits(x);
bits = 31 * bits + Float.floatToIntBits(y);
bits = 31 * bits + Float.floatToIntBits(z);
return bits;
}
/**
* Determines whether or not two 3D points or vectors are equal.
* Two instances of Vec3f
are equal if the values of their
* x
, y
and z
member fields,
* representing their position in the coordinate space, are the same.
* @param obj an object to be compared with this Vec3f
* @return true
if the object to be compared is
* an instance of Vec3f
and has
* the same values; false
otherwise.
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Vec3f) {
Vec3f v = (Vec3f) obj;
return (x == v.x) && (y == v.y) && (z == v.z);
}
return false;
}
/**
* Returns a String
that represents the value
* of this Vec3f
.
* @return a string representation of this Vec3f
.
*/
@Override
public String toString() {
return "Vec3f[" + x + ", " + y + ", " + z + "]";
}
}