1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 package com.javafx.experiments.utils3d.geom;
  34 
  35 /**
  36  * A 3-dimensional, double-precision, floating-point vector.
  37  *
  38  */
  39 public class Vec3d {
  40     /**
  41      * The x coordinate.
  42      */
  43     public double x;
  44 
  45     /**
  46      * The y coordinate.
  47      */
  48     public double y;
  49 
  50     /**
  51      * The z coordinate.
  52      */
  53     public double z;
  54 
  55     public Vec3d() { }
  56 
  57     public Vec3d(double x, double y, double z) {
  58         this.x = x;
  59         this.y = y;
  60         this.z = z;
  61     }
  62 
  63     public Vec3d(Vec3d v) {
  64         set(v);
  65     }
  66 
  67     public Vec3d(Vec3f v) {
  68         set(v);
  69     }
  70 
  71     public void set(Vec3f v) {
  72         this.x = v.x;
  73         this.y = v.y;
  74         this.z = v.z;
  75     }
  76 
  77     public void set(Vec3d v) {
  78         this.x = v.x;
  79         this.y = v.y;
  80         this.z = v.z;
  81     }
  82 
  83     public void set(double x, double y, double z) {
  84         this.x = x;
  85         this.y = y;
  86         this.z = z;
  87     }
  88 
  89     /**
  90      * Multiplies this vector by the specified scalar value.
  91      * @param scale the scalar value
  92      */
  93     public void mul(double scale) {
  94         x *= scale;
  95         y *= scale;
  96         z *= scale;
  97     }
  98 
  99     /**
 100      * Sets the value of this vector to the difference
 101      * of vectors t1 and t2 (this = t1 - t2).
 102      * @param t1 the first vector
 103      * @param t2 the second vector
 104      */
 105     public void sub(Vec3f t1, Vec3f t2) {
 106         this.x = t1.x - t2.x;
 107         this.y = t1.y - t2.y;
 108         this.z = t1.z - t2.z;
 109     }
 110 
 111     /**
 112      * Sets the value of this vector to the difference
 113      * of vectors t1 and t2 (this = t1 - t2).
 114      * @param t1 the first vector
 115      * @param t2 the second vector
 116      */
 117     public void sub(Vec3d t1, Vec3d t2) {
 118         this.x = t1.x - t2.x;
 119         this.y = t1.y - t2.y;
 120         this.z = t1.z - t2.z;
 121     }
 122 
 123     /**
 124      * Sets the value of this vector to the difference of
 125      * itself and vector t1 (this = this - t1) .
 126      * @param t1 the other vector
 127      */
 128     public void sub(Vec3d t1) {
 129         this.x -= t1.x;
 130         this.y -= t1.y;
 131         this.z -= t1.z;
 132     }
 133 
 134     /**
 135      * Sets the value of this vector to the sum
 136      * of vectors t1 and t2 (this = t1 + t2).
 137      * @param t1 the first vector
 138      * @param t2 the second vector
 139      */
 140     public void add(Vec3d t1, Vec3d t2) {
 141         this.x = t1.x + t2.x;
 142         this.y = t1.y + t2.y;
 143         this.z = t1.z + t2.z;
 144     }
 145 
 146     /**
 147      * Sets the value of this vector to the sum of
 148      * itself and vector t1 (this = this + t1) .
 149      * @param t1 the other vector
 150      */
 151     public void add(Vec3d t1) {
 152         this.x += t1.x;
 153         this.y += t1.y;
 154         this.z += t1.z;
 155     }
 156 
 157     /**
 158      * Returns the length of this vector.
 159      * @return the length of this vector
 160      */
 161     public double length() {
 162         return Math.sqrt(this.x*this.x + this.y*this.y + this.z*this.z);
 163     }
 164 
 165     /**
 166      * Normalize this vector.
 167      */
 168     public void normalize() {
 169         double norm = 1.0 / length();
 170         this.x = this.x * norm;
 171         this.y = this.y * norm;
 172         this.z = this.z * norm;
 173     }
 174 
 175     /**
 176      * Sets this vector to be the vector cross product of vectors v1 and v2.
 177      * @param v1 the first vector
 178      * @param v2 the second vector
 179      */
 180     public void cross(Vec3d v1, Vec3d v2) {
 181         double tmpX;
 182         double tmpY;
 183 
 184         tmpX = v1.y * v2.z - v1.z * v2.y;
 185         tmpY = v2.x * v1.z - v2.z * v1.x;
 186         this.z = v1.x * v2.y - v1.y * v2.x;
 187         this.x = tmpX;
 188         this.y = tmpY;
 189     }
 190 
 191     /**
 192      * Computes the dot product of this vector and vector v1.
 193      * @param v1 the other vector
 194      * @return the dot product of this vector and v1
 195      */
 196     public double dot(Vec3d v1) {
 197         return this.x * v1.x + this.y * v1.y + this.z * v1.z;
 198     }
 199 
 200     /**
 201      * Returns the hashcode for this <code>Vec3f</code>.
 202      * @return      a hash code for this <code>Vec3f</code>.
 203      */
 204     @Override
 205     public int hashCode() {
 206         long bits = 7L;
 207         bits = 31L * bits + Double.doubleToLongBits(x);
 208         bits = 31L * bits + Double.doubleToLongBits(y);
 209         bits = 31L * bits + Double.doubleToLongBits(z);
 210         return (int) (bits ^ (bits >> 32));
 211     }
 212 
 213     /**
 214      * Determines whether or not two 3D points or vectors are equal.
 215      * Two instances of <code>Vec3d</code> are equal if the values of their
 216      * <code>x</code>, <code>y</code> and <code>z</code> member fields,
 217      * representing their position in the coordinate space, are the same.
 218      * @param obj an object to be compared with this <code>Vec3d</code>
 219      * @return <code>true</code> if the object to be compared is
 220      *         an instance of <code>Vec3d</code> and has
 221      *         the same values; <code>false</code> otherwise.
 222      */
 223     @Override
 224     public boolean equals(Object obj) {
 225         if (obj == this) {
 226             return true;
 227         }
 228         if (obj instanceof Vec3d) {
 229             Vec3d v = (Vec3d) obj;
 230             return (x == v.x) && (y == v.y) && (z == v.z);
 231         }
 232         return false;
 233     }
 234 
 235     /**
 236      * Returns a <code>String</code> that represents the value
 237      * of this <code>Vec3f</code>.
 238      * @return a string representation of this <code>Vec3f</code>.
 239      */
 240     @Override
 241     public String toString() {
 242         return "Vec3d[" + x + ", " + y + ", " + z + "]";
 243     }
 244 }