Home | History | Annotate | Download | only in common
      1 /*******************************************************************************
      2  * Copyright (c) 2013, Daniel Murphy
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without modification,
      6  * are permitted provided that the following conditions are met:
      7  * 	* Redistributions of source code must retain the above copyright notice,
      8  * 	  this list of conditions and the following disclaimer.
      9  * 	* Redistributions in binary form must reproduce the above copyright notice,
     10  * 	  this list of conditions and the following disclaimer in the documentation
     11  * 	  and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     16  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     19  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     20  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     21  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     22  * POSSIBILITY OF SUCH DAMAGE.
     23  ******************************************************************************/
     24 package org.jbox2d.common;
     25 
     26 import java.io.Serializable;
     27 
     28 /**
     29  * @author Daniel Murphy
     30  */
     31 public class Vec3 implements Serializable {
     32   private static final long serialVersionUID = 1L;
     33 
     34   public float x, y, z;
     35 
     36   public Vec3() {
     37     x = y = z = 0f;
     38   }
     39 
     40   public Vec3(float argX, float argY, float argZ) {
     41     x = argX;
     42     y = argY;
     43     z = argZ;
     44   }
     45 
     46   public Vec3(Vec3 copy) {
     47     x = copy.x;
     48     y = copy.y;
     49     z = copy.z;
     50   }
     51 
     52   public Vec3 set(Vec3 vec) {
     53     x = vec.x;
     54     y = vec.y;
     55     z = vec.z;
     56     return this;
     57   }
     58 
     59   public Vec3 set(float argX, float argY, float argZ) {
     60     x = argX;
     61     y = argY;
     62     z = argZ;
     63     return this;
     64   }
     65 
     66   public Vec3 addLocal(Vec3 argVec) {
     67     x += argVec.x;
     68     y += argVec.y;
     69     z += argVec.z;
     70     return this;
     71   }
     72 
     73   public Vec3 add(Vec3 argVec) {
     74     return new Vec3(x + argVec.x, y + argVec.y, z + argVec.z);
     75   }
     76 
     77   public Vec3 subLocal(Vec3 argVec) {
     78     x -= argVec.x;
     79     y -= argVec.y;
     80     z -= argVec.z;
     81     return this;
     82   }
     83 
     84   public Vec3 sub(Vec3 argVec) {
     85     return new Vec3(x - argVec.x, y - argVec.y, z - argVec.z);
     86   }
     87 
     88   public Vec3 mulLocal(float argScalar) {
     89     x *= argScalar;
     90     y *= argScalar;
     91     z *= argScalar;
     92     return this;
     93   }
     94 
     95   public Vec3 mul(float argScalar) {
     96     return new Vec3(x * argScalar, y * argScalar, z * argScalar);
     97   }
     98 
     99   public Vec3 negate() {
    100     return new Vec3(-x, -y, -z);
    101   }
    102 
    103   public Vec3 negateLocal() {
    104     x = -x;
    105     y = -y;
    106     z = -z;
    107     return this;
    108   }
    109 
    110   public void setZero() {
    111     x = 0;
    112     y = 0;
    113     z = 0;
    114   }
    115 
    116   public Vec3 clone() {
    117     return new Vec3(this);
    118   }
    119 
    120   public String toString() {
    121     return "(" + x + "," + y + "," + z + ")";
    122   }
    123 
    124   @Override
    125   public int hashCode() {
    126     final int prime = 31;
    127     int result = 1;
    128     result = prime * result + Float.floatToIntBits(x);
    129     result = prime * result + Float.floatToIntBits(y);
    130     result = prime * result + Float.floatToIntBits(z);
    131     return result;
    132   }
    133 
    134   @Override
    135   public boolean equals(Object obj) {
    136     if (this == obj) return true;
    137     if (obj == null) return false;
    138     if (getClass() != obj.getClass()) return false;
    139     Vec3 other = (Vec3) obj;
    140     if (Float.floatToIntBits(x) != Float.floatToIntBits(other.x)) return false;
    141     if (Float.floatToIntBits(y) != Float.floatToIntBits(other.y)) return false;
    142     if (Float.floatToIntBits(z) != Float.floatToIntBits(other.z)) return false;
    143     return true;
    144   }
    145 
    146   public final static float dot(Vec3 a, Vec3 b) {
    147     return a.x * b.x + a.y * b.y + a.z * b.z;
    148   }
    149 
    150   public final static Vec3 cross(Vec3 a, Vec3 b) {
    151     return new Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
    152   }
    153 
    154   public final static void crossToOut(Vec3 a, Vec3 b, Vec3 out) {
    155     final float tempy = a.z * b.x - a.x * b.z;
    156     final float tempz = a.x * b.y - a.y * b.x;
    157     out.x = a.y * b.z - a.z * b.y;
    158     out.y = tempy;
    159     out.z = tempz;
    160   }
    161 
    162   public final static void crossToOutUnsafe(Vec3 a, Vec3 b, Vec3 out) {
    163     assert(out != b);
    164     assert(out != a);
    165     out.x = a.y * b.z - a.z * b.y;
    166     out.y = a.z * b.x - a.x * b.z;
    167     out.z = a.x * b.y - a.y * b.x;
    168   }
    169 }
    170