3D向量类

隐藏行号 复制代码 源代码
  1. /*
    
  2.  * Copyright (c) 2003-2009 jMonkeyEngine
    
  3.  * All rights reserved.
    
  4.  *
    
  5.  * Redistribution and use in source and binary forms, with or without
    
  6.  * modification, are permitted provided that the following conditions are
    
  7.  * met:
    
  8.  *
    
  9.  * * Redistributions of source code must retain the above copyright
    
  10.  *   notice, this list of conditions and the following disclaimer.
    
  11.  *
    
  12.  * * Redistributions in binary form must reproduce the above copyright
    
  13.  *   notice, this list of conditions and the following disclaimer in the
    
  14.  *   documentation and/or other materials provided with the distribution.
    
  15.  *
    
  16.  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 
    
  17.  *   may be used to endorse or promote products derived from this software 
    
  18.  *   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 LIMITED
    
  22.  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    
  23.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    
  27.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    
  28.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    
  29.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    
  30.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
  31.  */
    
  32. 
    
  33. package com.jme.math;
    
  34.  
  35. import java.io.Externalizable;
    
  36. import java.io.IOException;
    
  37. import java.io.ObjectInput;
    
  38. import java.io.ObjectOutput;
    
  39. import java.util.logging.Logger;
    
  40.  
  41. import com.jme.util.export.InputCapsule;
    
  42. import com.jme.util.export.JMEExporter;
    
  43. import com.jme.util.export.JMEImporter;
    
  44. import com.jme.util.export.OutputCapsule;
    
  45. import com.jme.util.export.Savable;
    
  46.  
  47. /*
    
  48.  * -- Added *Local methods to cut down on object creation - JS
    
  49.  */
    
  50. 
    
  51. /**
    
  52.  * <code>Vector3f</code> defines a Vector for a three float value tuple.
    
  53.  * <code>Vector3f</code> can represent any three dimensional value, such as a
    
  54.  * vertex, a normal, etc. Utility methods are also included to aid in
    
  55.  * mathematical calculations.<br/>
    
  56.  * 3维向量类
    
  57.  * 
    
  58.  * @author Mark Powell
    
  59.  * @author Joshua Slack
    
  60.  */
    
  61. public class Vector3f implements Externalizable, Savable, Cloneable {
    
  62.     private static final Logger logger = Logger.getLogger(Vector3f.class
    
  63.             .getName());
    
  64.  
  65.     private static final long serialVersionUID = 1L;
    
  66.  
  67.     public final static Vector3f ZERO = new Vector3f(0, 0, 0);
    
  68.  
  69.     public final static Vector3f UNIT_X = new Vector3f(1, 0, 0);
    
  70.     public final static Vector3f UNIT_Y = new Vector3f(0, 1, 0);
    
  71.     public final static Vector3f UNIT_Z = new Vector3f(0, 0, 1);
    
  72.     public final static Vector3f UNIT_XYZ = new Vector3f(1, 1, 1);
    
  73.  
  74.     /**
    
  75.      * the x value of the vector.
    
  76.      */
    
  77.     public float x;
    
  78.  
  79.     /**
    
  80.      * the y value of the vector.
    
  81.      */
    
  82.     public float y;
    
  83.  
  84.     /**
    
  85.      * the z value of the vector.
    
  86.      */
    
  87.     public float z;
    
  88.  
  89.     /**
    
  90.      * Constructor instantiates a new <code>Vector3f</code> with default values
    
  91.      * of (0,0,0).
    
  92.      * 
    
  93.      */
    
  94.     public Vector3f() {
    
  95.         x = y = z = 0;
    
  96.     }
    
  97.  
  98.     /**
    
  99.      * Constructor instantiates a new <code>Vector3f</code> with provides
    
  100.      * values.
    
  101.      * 
    
  102.      * @param x
    
  103.      *            the x value of the vector.
    
  104.      * @param y
    
  105.      *            the y value of the vector.
    
  106.      * @param z
    
  107.      *            the z value of the vector.
    
  108.      */
    
  109.     public Vector3f(float x, float y, float z) {
    
  110.         this.x = x;
    
  111.         this.y = y;
    
  112.         this.z = z;
    
  113.     }
    
  114.  
  115.     /**
    
  116.      * Constructor instantiates a new <code>Vector3f</code> that is a copy of
    
  117.      * the provided vector
    
  118.      * 
    
  119.      * @param copy
    
  120.      *            The Vector3f to copy
    
  121.      */
    
  122.     public Vector3f(Vector3f copy) {
    
  123.         this.set(copy);
    
  124.     }
    
  125.  
  126.     /**
    
  127.      * <code>set</code> sets the x,y,z values of the vector based on passed
    
  128.      * parameters.
    
  129.      * 
    
  130.      * @param x
    
  131.      *            the x value of the vector.
    
  132.      * @param y
    
  133.      *            the y value of the vector.
    
  134.      * @param z
    
  135.      *            the z value of the vector.
    
  136.      * @return this vector
    
  137.      */
    
  138.     public Vector3f set(float x, float y, float z) {
    
  139.         this.x = x;
    
  140.         this.y = y;
    
  141.         this.z = z;
    
  142.         return this;
    
  143.     }
    
  144.  
  145.     /**
    
  146.      * <code>set</code> sets the x,y,z values of the vector by copying the
    
  147.      * supplied vector.
    
  148.      * 
    
  149.      * @param vect
    
  150.      *            the vector to copy.
    
  151.      * @return this vector
    
  152.      */
    
  153.     public Vector3f set(Vector3f vect) {
    
  154.         this.x = vect.x;
    
  155.         this.y = vect.y;
    
  156.         this.z = vect.z;
    
  157.         return this;
    
  158.     }
    
  159.  
  160.     /**
    
  161.      * 
    
  162.      * <code>add</code> adds a provided vector to this vector creating a
    
  163.      * resultant vector which is returned. If the provided vector is null, null
    
  164.      * is returned.<br/>
    
  165.      * 向量加法(不改变自身和参数)
    
  166.      * 
    
  167.      * Neither 'this' nor 'vec' are modified.
    
  168.      * 
    
  169.      * @param vec
    
  170.      *            the vector to add to this.
    
  171.      * @return the resultant vector.
    
  172.      */
    
  173.     public Vector3f add(Vector3f vec) {
    
  174.         if (null == vec) {
    
  175.             logger.warning("Provided vector is null, null returned.");
    
  176.             return null;
    
  177.         }
    
  178.         return new Vector3f(x + vec.x, y + vec.y, z + vec.z);
    
  179.     }
    
  180.  
  181.     /**
    
  182.      * 
    
  183.      * <code>add</code> adds the values of a provided vector storing the values
    
  184.      * in the supplied vector.<br/>
    
  185.      * 向量加法
    
  186.      * 
    
  187.      * @param vec
    
  188.      *            the vector to add to this
    
  189.      * @param result
    
  190.      *            the vector to store the result in
    
  191.      * @return result returns the supplied result vector.
    
  192.      */
    
  193.     public Vector3f add(Vector3f vec, Vector3f result) {
    
  194.         result.x = x + vec.x;
    
  195.         result.y = y + vec.y;
    
  196.         result.z = z + vec.z;
    
  197.         return result;
    
  198.     }
    
  199.  
  200.     /**
    
  201.      * <code>addLocal</code> adds a provided vector to this vector internally,
    
  202.      * and returns a handle to this vector for easy chaining of calls. If the
    
  203.      * provided vector is null, null is returned.<br/>
    
  204.      * 向量加法(自加)
    
  205.      * 
    
  206.      * @param vec
    
  207.      *            the vector to add to this vector.
    
  208.      * @return this
    
  209.      */
    
  210.     public Vector3f addLocal(Vector3f vec) {
    
  211.         if (null == vec) {
    
  212.             logger.warning("Provided vector is null, null returned.");
    
  213.             return null;
    
  214.         }
    
  215.         x += vec.x;
    
  216.         y += vec.y;
    
  217.         z += vec.z;
    
  218.         return this;
    
  219.     }
    
  220.  
  221.     /**
    
  222.      * 
    
  223.      * <code>add</code> adds the provided values to this vector, creating a new
    
  224.      * vector that is then returned.<br/>
    
  225.      * 向量加法
    
  226.      * 
    
  227.      * @param addX
    
  228.      *            the x value to add.
    
  229.      * @param addY
    
  230.      *            the y value to add.
    
  231.      * @param addZ
    
  232.      *            the z value to add.
    
  233.      * @return the result vector.
    
  234.      */
    
  235.     public Vector3f add(float addX, float addY, float addZ) {
    
  236.         return new Vector3f(x + addX, y + addY, z + addZ);
    
  237.     }
    
  238.  
  239.     /**
    
  240.      * <code>addLocal</code> adds the provided values to this vector internally,
    
  241.      * and returns a handle to this vector for easy chaining of calls.<br/>
    
  242.      * 向量加法(自加)
    
  243.      * 
    
  244.      * @param addX
    
  245.      *            value to add to x
    
  246.      * @param addY
    
  247.      *            value to add to y
    
  248.      * @param addZ
    
  249.      *            value to add to z
    
  250.      * @return this
    
  251.      */
    
  252.     public Vector3f addLocal(float addX, float addY, float addZ) {
    
  253.         x += addX;
    
  254.         y += addY;
    
  255.         z += addZ;
    
  256.         return this;
    
  257.     }
    
  258.  
  259.     /**
    
  260.      * 
    
  261.      * <code>scaleAdd</code> multiplies this vector by a scalar then adds the
    
  262.      * given Vector3f.<br/>
    
  263.      * 向量加乘(先乘一个标量,再加一个向量)
    
  264.      * 
    
  265.      * @param scalar
    
  266.      *            the value to multiply this vector by.
    
  267.      * @param add
    
  268.      *            the value to add
    
  269.      */
    
  270.     public void scaleAdd(float scalar, Vector3f add) {
    
  271.         x = x * scalar + add.x;
    
  272.         y = y * scalar + add.y;
    
  273.         z = z * scalar + add.z;
    
  274.     }
    
  275.  
  276.     /**
    
  277.      * 
    
  278.      * <code>scaleAdd</code> multiplies the given vector by a scalar then adds
    
  279.      * the given vector.<br/>
    
  280.      * 向量加乘(将mult乘以标量scalar,再加上向量add,返回this)
    
  281.      * 
    
  282.      * @param scalar
    
  283.      *            the value to multiply this vector by.
    
  284.      * @param mult
    
  285.      *            the value to multiply the scalar by
    
  286.      * @param add
    
  287.      *            the value to add
    
  288.      */
    
  289.     public void scaleAdd(float scalar, Vector3f mult, Vector3f add) {
    
  290.         this.x = mult.x * scalar + add.x;
    
  291.         this.y = mult.y * scalar + add.y;
    
  292.         this.z = mult.z * scalar + add.z;
    
  293.     }
    
  294.  
  295.     /**
    
  296.      * 
    
  297.      * <code>dot</code> calculates the dot product of this vector with a
    
  298.      * provided vector. If the provided vector is null, 0 is returned.<br/>
    
  299.      * 向量点乘
    
  300.      * 
    
  301.      * @param vec
    
  302.      *            the vector to dot with this vector.
    
  303.      * @return the resultant dot product of this vector and a given vector.
    
  304.      */
    
  305.     public float dot(Vector3f vec) {
    
  306.         if (null == vec) {
    
  307.             logger.warning("Provided vector is null, 0 returned.");
    
  308.             return 0;
    
  309.         }
    
  310.         return x * vec.x + y * vec.y + z * vec.z;
    
  311.     }
    
  312.  
  313.     /**
    
  314.      * Returns a new vector which is the cross product of this vector with the
    
  315.      * specified vector.<br/>
    
  316.      * 叉乘(不改变自身和参数)
    
  317.      * <P>
    
  318.      * Neither 'this' nor v are modified. The starting value of 'result'
    
  319.      * </P>
    
  320.      * 
    
  321.      * @param v
    
  322.      *            the vector to take the cross product of with this.
    
  323.      * @return the cross product vector.
    
  324.      */
    
  325.     public Vector3f cross(Vector3f v) {
    
  326.         return cross(v, null);
    
  327.     }
    
  328.  
  329.     /**
    
  330.      * <code>cross</code> calculates the cross product of this vector with a
    
  331.      * parameter vector v. The result is stored in <code>result</code><br/>
    
  332.      * 叉乘(this与v叉乘,结果存入result中。不改变自身和参数)
    
  333.      * <P>
    
  334.      * Neither 'this' nor v are modified. The starting value of 'result' (if
    
  335.      * any) is ignored.
    
  336.      * </P>
    
  337.      * 
    
  338.      * @param v
    
  339.      *            the vector to take the cross product of with this.
    
  340.      * @param result
    
  341.      *            the vector to store the cross product result.
    
  342.      * @return result, after recieving the cross product vector.
    
  343.      */
    
  344.     public Vector3f cross(Vector3f v, Vector3f result) {
    
  345.         return cross(v.x, v.y, v.z, result);
    
  346.     }
    
  347.  
  348.     /**
    
  349.      * <code>cross</code> calculates the cross product of this vector with a
    
  350.      * Vector comprised of the specified other* elements. The result is stored
    
  351.      * in <code>result</code>, without modifying either 'this' or the 'other*'
    
  352.      * values.<br/>
    
  353.      * 叉乘(this与参数叉乘,结果存在参数result中)
    
  354.      * 
    
  355.      * @param otherX
    
  356.      *            x component of the vector to take the cross product of with
    
  357.      *            this.
    
  358.      * @param otherY
    
  359.      *            y component of the vector to take the cross product of with
    
  360.      *            this.
    
  361.      * @param otherZ
    
  362.      *            z component of the vector to take the cross product of with
    
  363.      *            this.
    
  364.      * @param result
    
  365.      *            the vector to store the cross product result.
    
  366.      * @return result, after recieving the cross product vector.
    
  367.      */
    
  368.     public Vector3f cross(float otherX, float otherY, float otherZ,
    
  369.             Vector3f result) {
    
  370.         if (result == null)
    
  371.             result = new Vector3f();
    
  372.         float resX = ((y * otherZ) - (z * otherY));
    
  373.         float resY = ((z * otherX) - (x * otherZ));
    
  374.         float resZ = ((x * otherY) - (y * otherX));
    
  375.         result.set(resX, resY, resZ);
    
  376.         return result;
    
  377.     }
    
  378.  
  379.     /**
    
  380.      * <code>crossLocal</code> calculates the cross product of this vector with
    
  381.      * a parameter vector v.<br/>
    
  382.      * 自身叉乘(this与参数叉乘,结果存入自身)
    
  383.      * 
    
  384.      * @param v
    
  385.      *            the vector to take the cross product of with this.
    
  386.      * @return this.
    
  387.      */
    
  388.     public Vector3f crossLocal(Vector3f v) {
    
  389.         return crossLocal(v.x, v.y, v.z);
    
  390.     }
    
  391.  
  392.     /**
    
  393.      * <code>crossLocal</code> calculates the cross product of this vector with
    
  394.      * a parameter vector v.<br/>
    
  395.      * 自身叉乘(this与参数叉乘,结果存入自身)
    
  396.      * 
    
  397.      * @param otherX
    
  398.      *            x component of the vector to take the cross product of with
    
  399.      *            this.
    
  400.      * @param otherY
    
  401.      *            y component of the vector to take the cross product of with
    
  402.      *            this.
    
  403.      * @param otherZ
    
  404.      *            z component of the vector to take the cross product of with
    
  405.      *            this.
    
  406.      * @return this.
    
  407.      */
    
  408.     public Vector3f crossLocal(float otherX, float otherY, float otherZ) {
    
  409.         float tempx = (y * otherZ) - (z * otherY);
    
  410.         float tempy = (z * otherX) - (x * otherZ);
    
  411.         z = (x * otherY) - (y * otherX);
    
  412.         x = tempx;
    
  413.         y = tempy;
    
  414.         return this;
    
  415.     }
    
  416.  
  417.     /**
    
  418.      * <code>length</code> calculates the magnitude of this vector.<br/>
    
  419.      * 向量长度
    
  420.      * 
    
  421.      * @return the length or magnitude of the vector.
    
  422.      */
    
  423.     public float length() {
    
  424.         return FastMath.sqrt(lengthSquared());
    
  425.     }
    
  426.  
  427.     /**
    
  428.      * <code>lengthSquared</code> calculates the squared value of the magnitude
    
  429.      * of the vector.<br/>
    
  430.      * 向量长度平方
    
  431.      * 
    
  432.      * @return the magnitude squared of the vector.
    
  433.      */
    
  434.     public float lengthSquared() {
    
  435.         return x * x + y * y + z * z;
    
  436.     }
    
  437.  
  438.     /**
    
  439.      * <code>distanceSquared</code> calculates the distance squared between this
    
  440.      * vector and vector v.<br/>
    
  441.      * 向量this与参赛v间的距离平方
    
  442.      * 
    
  443.      * @param v
    
  444.      *            the second vector to determine the distance squared.
    
  445.      * @return the distance squared between the two vectors.
    
  446.      */
    
  447.     public float distanceSquared(Vector3f v) {
    
  448.         double dx = x - v.x;
    
  449.         double dy = y - v.y;
    
  450.         double dz = z - v.z;
    
  451.         return (float) (dx * dx + dy * dy + dz * dz);
    
  452.     }
    
  453.  
  454.     /**
    
  455.      * <code>distance</code> calculates the distance between this vector and
    
  456.      * vector v.<br/>
    
  457.      * 向量this与参赛v间的距离
    
  458.      * 
    
  459.      * @param v
    
  460.      *            the second vector to determine the distance.
    
  461.      * @return the distance between the two vectors.
    
  462.      */
    
  463.     public float distance(Vector3f v) {
    
  464.         return FastMath.sqrt(distanceSquared(v));
    
  465.     }
    
  466.  
  467.     /**
    
  468.      * <code>mult</code> multiplies this vector by a scalar. The resultant
    
  469.      * vector is returned. "this" is not modified.<br/>
    
  470.      * 向量标量乘
    
  471.      * 
    
  472.      * @param scalar
    
  473.      *            the value to multiply this vector by.
    
  474.      * @return the new vector.
    
  475.      */
    
  476.     public Vector3f mult(float scalar) {
    
  477.         return new Vector3f(x * scalar, y * scalar, z * scalar);
    
  478.     }
    
  479.  
  480.     /**
    
  481.      * 
    
  482.      * <code>mult</code> multiplies this vector by a scalar. The resultant
    
  483.      * vector is supplied as the second parameter and returned. "this" is not
    
  484.      * modified.<br/>
    
  485.      * 向量标量乘,结果存入product。
    
  486.      * 
    
  487.      * @param scalar
    
  488.      *            the scalar to multiply this vector by.
    
  489.      * @param product
    
  490.      *            the product to store the result in.
    
  491.      * @return product
    
  492.      */
    
  493.     public Vector3f mult(float scalar, Vector3f product) {
    
  494.         if (null == product) {
    
  495.             product = new Vector3f();
    
  496.         }
    
  497.  
  498.         product.x = x * scalar;
    
  499.         product.y = y * scalar;
    
  500.         product.z = z * scalar;
    
  501.         return product;
    
  502.     }
    
  503.  
  504.     /**
    
  505.      * <code>multLocal</code> multiplies this vector by a scalar internally, and
    
  506.      * returns a handle to this vector for easy chaining of calls.<br/>
    
  507.      * 向量标量乘this
    
  508.      * 
    
  509.      * @param scalar
    
  510.      *            the value to multiply this vector by.
    
  511.      * @return this
    
  512.      */
    
  513.     public Vector3f multLocal(float scalar) {
    
  514.         x *= scalar;
    
  515.         y *= scalar;
    
  516.         z *= scalar;
    
  517.         return this;
    
  518.     }
    
  519.  
  520.     /**
    
  521.      * <code>multLocal</code> multiplies a provided vector to this vector
    
  522.      * internally, and returns a handle to this vector for easy chaining of
    
  523.      * calls. If the provided vector is null, null is returned. The provided
    
  524.      * 'vec' is not modified.<br/>
    
  525.      * 向量标量乘一个向量(对应的x、y、z分量分别相乘),结果改变this
    
  526.      * 
    
  527.      * @param vec
    
  528.      *            the vector to mult to this vector.
    
  529. 
      
分享到:
评论

你可能感兴趣的:(3D)