Advertisement
Guest User

Vector3

a guest
Nov 26th, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.09 KB | None | 0 0
  1. package com.pixldevelopment.wastelandengine.other.math.Vector;
  2.  
  3. import org.lwjgl.BufferUtils;
  4.  
  5. import java.nio.FloatBuffer;
  6.  
  7. /**
  8.  * An position consisting of 3 floats.
  9.  *
  10.  * @author simengangstad
  11.  * @since 08.06.13
  12.  */
  13. public class Vector3 implements Vector<Vector3>
  14. {
  15.     /**
  16.      * The floats in the vector3.
  17.      */
  18.     private float x, y, z;
  19.  
  20.     /**
  21.      * Initializes the vector3 with float data.
  22.      *
  23.      * @param x The x.
  24.      * @param y The y.
  25.      * @param z The z.
  26.      */
  27.     public Vector3(float x, float y, float z)
  28.     {
  29.         this.setX(x);
  30.         this.setY(y);
  31.         this.setZ(z);
  32.     }
  33.  
  34.     /**
  35.      * Initializes the vector3 without any data.
  36.      */
  37.     public Vector3()
  38.     {
  39.         this(0.0f, 0.0f, 0.0f);
  40.     }
  41.  
  42.     /**
  43.      * Initializes the vector3 with vector4 data.
  44.      *
  45.      * @param vector The vector4.
  46.      */
  47.     public Vector3(Vector4 vector)
  48.     {
  49.         this(vector.getX(), vector.getY(), vector.getZ());
  50.     }
  51.  
  52.     /**
  53.      * Initializes the vector3 with vector3 data.
  54.      *
  55.      * @param vector The vector3.
  56.      */
  57.     public Vector3(Vector3 vector)
  58.     {
  59.        this(vector.getX(), vector.getY(), vector.getZ());
  60.     }
  61.  
  62.     /**
  63.      * Initializes the vector3 with vector2 data.
  64.      *
  65.      * @param vector The vector2.
  66.      */
  67.     public Vector3(Vector2 vector)
  68.     {
  69.         this(vector.getX(), vector.getY(), 0.0f);
  70.     }
  71.  
  72.     /**
  73.      * @return The x value.
  74.      */
  75.     public float getX()
  76.     {
  77.         return this.x;
  78.     }
  79.  
  80.     /**
  81.      * Sets the x value of the vector3.
  82.      *
  83.      * @param x The x value.
  84.      *
  85.      * @return The new vector3.
  86.      */
  87.     public Vector3 setX(float x)
  88.     {
  89.         this.x = x;
  90.  
  91.         return this;
  92.     }
  93.  
  94.     /**
  95.      * @return The y value.
  96.      */
  97.     public float getY()
  98.     {
  99.         return this.y;
  100.     }
  101.  
  102.     /**
  103.      * Sets the y value of the vector3.
  104.      *
  105.      * @param y The y value.
  106.      *
  107.      * @return The new vector3.
  108.      */
  109.     public Vector3 setY(float y)
  110.     {
  111.         this.y = y;
  112.  
  113.         return this;
  114.     }
  115.  
  116.     /**
  117.      * @return The z value.
  118.      */
  119.     public float getZ()
  120.     {
  121.         return this.z;
  122.     }
  123.  
  124.     /**
  125.      * Sets the z value of the vector3.
  126.      *
  127.      * @param z The z value.
  128.      *
  129.      * @return The new vector3.
  130.      */
  131.     public Vector3 setZ(float z)
  132.     {
  133.         this.z = z;
  134.  
  135.         return this;
  136.     }
  137.  
  138.     @Override
  139.     /**
  140.      * @return A copy of the current vector3.
  141.      */
  142.     public Vector3 copy()
  143.     {
  144.         return new Vector3(this);
  145.     }
  146.  
  147.     @Override
  148.     /**
  149.      * Clears the vector3.
  150.      *
  151.      * @return The new vector3.
  152.      */
  153.     public Vector3 clear()
  154.     {
  155.         this.setX(0.0f);
  156.         this.setY(0.0f);
  157.         this.setZ(0.0f);
  158.  
  159.         return this;
  160.     }
  161.  
  162.     @Override
  163.     /**
  164.      * @return The length of the vector3.
  165.      */
  166.     public float length()
  167.     {
  168.         return (float) Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() * this.getZ());
  169.     }
  170.  
  171.     @Override
  172.     /**
  173.      * Normalizes the vector3.
  174.      *
  175.      * @return The new vector3.
  176.      */
  177.     public Vector3 normalize()
  178.     {
  179.         this.setX(this.x /= this.length());
  180.         this.setY(this.y /= this.length());
  181.         this.setZ(this.z /= this.length());
  182.  
  183.         return this;
  184.     }
  185.  
  186.     @Override
  187.     /**
  188.      * Negates the vector3.
  189.      *
  190.      * @return The new vector3.
  191.      */
  192.     public Vector3 negate()
  193.     {
  194.         this.setX(-this.getX());
  195.         this.setY(-this.getY());
  196.         this.setZ(-this.getZ());
  197.  
  198.         return this;
  199.     }
  200.  
  201.     /**
  202.      * Adds x, y and z values to the vector3.
  203.      *
  204.      * @param x The x value.
  205.      * @param y The y value.
  206.      * @param z The z value.
  207.      *
  208.      * @return The new vector3.
  209.      */
  210.     public Vector3 add(float x, float y, float z)
  211.     {
  212.         this.setX(this.x += x);
  213.         this.setY(this.y += y);
  214.         this.setZ(this.z += z);
  215.  
  216.         return this;
  217.     }
  218.  
  219.     @Override
  220.     /**
  221.      * Adds a vector3 to the vector3.
  222.      *
  223.      * @param vector The vector3 that shall be added.
  224.      *
  225.      * @return The new vector3.
  226.      */
  227.     public Vector3 add(Vector3 vector)
  228.     {
  229.         return this.add(vector.getX(), vector.getY(), vector.getZ());
  230.     }
  231.  
  232.     /**
  233.      * Subtracts XYZ values of the vector3.
  234.      *
  235.      * @param x The x value.
  236.      * @param y The y value.
  237.      * @param z The z value.
  238.      *
  239.      * @return The new vector3.
  240.      */
  241.     public Vector3 subtract(float x, float y, float z)
  242.     {
  243.         this.setX(this.x -= x);
  244.         this.setY(this.y -= y);
  245.         this.setZ(this.z -= z);
  246.  
  247.         return this;
  248.     }
  249.  
  250.     @Override
  251.     /**
  252.      * Subtracts a vector3 of the vector3.
  253.      *
  254.      * @param vector The vector3 that shall be subtracted.
  255.      *
  256.      * @return The new vector3.
  257.      */
  258.     public Vector3 subtract(Vector3 vector)
  259.     {
  260.         return this.subtract(vector.getX(), vector.getY(), vector.getZ());
  261.     }
  262.  
  263.     @Override
  264.     /**
  265.      * Multiplies the vector3 by a vector3.
  266.      *
  267.      * @param vector The vector3 the vector3 shall be multiplied with.
  268.      *
  269.      * @return The new vector3.
  270.      */
  271.     public Vector3 multiply(Vector3 vector)
  272.     {
  273.         this.setX(this.x *= vector.getX());
  274.         this.setY(this.y *= vector.getY());
  275.         this.setZ(this.z *= vector.getZ());
  276.  
  277.         return this;
  278.     }
  279.  
  280.     @Override
  281.     /**
  282.      * Multiplies the vector3 by a value.
  283.      *
  284.      * @param value The value the vector3 shall be multiplied with.
  285.      *
  286.      * @return The new vector3.
  287.      */
  288.     public Vector3 multiply(float value)
  289.     {
  290.         return this.multiply(new Vector3(value, value, value));
  291.     }
  292.  
  293.     @Override
  294.     /**
  295.      * Divides the vector3 by a vector3.
  296.      *
  297.      * @param vector The vector3 the vector3 shall be divided with.
  298.      *
  299.      * @return The new vector3.
  300.      */
  301.     public Vector3 divide(Vector3 vector)
  302.     {
  303.         this.setX(this.x /= vector.getX());
  304.         this.setY(this.y /= vector.getY());
  305.         this.setZ(this.z /= vector.getZ());
  306.  
  307.         return this;
  308.     }
  309.  
  310.     @Override
  311.     /**
  312.      * Divides the vector3 by a value.
  313.      *
  314.      * @param value The value the vector3 shall be divided with.
  315.      *
  316.      * @return The new vector3.
  317.      */
  318.     public Vector3 divide(float value)
  319.     {
  320.         return this.divide(new Vector3(value, value, value));
  321.     }
  322.  
  323.     /**
  324.      * Calculates the dot product of the current vector3 and another one.
  325.      *
  326.      * @param vector The another vector3.
  327.      *
  328.      * @return The dot product of the current vector3 and the another one.
  329.      */
  330.     public float dotProduct(Vector3 vector)
  331.     {
  332.         return this.getX() * vector.getX() + this.getY() * vector.getY() + this.getZ() * vector.getZ();
  333.     }
  334.  
  335.     /**
  336.      * Calculates the cross product of the current vector3 and another one.
  337.      *
  338.      * @param vector The another vector3.
  339.      *
  340.      * @return The cross product of the current vector3 and the another one.
  341.      */
  342.     public Vector3 crossProduct(Vector3 vector)
  343.     {
  344.         return new Vector3(this.getY() * vector.getZ() - vector.getY() * this.getZ(), this.getZ() * vector.getX() - vector.getZ() * this.getX(), this.getX() * vector.getY() - vector.getX() * this.getY());
  345.     }
  346.  
  347.     /**
  348.      * Computes a normal from three vertices.
  349.      *
  350.      * @param v0 The first vertex.
  351.      * @param v1 The second vertex.
  352.      * @param v2 The third vertex.
  353.      *
  354.      * @return A normal based on the three vertices.
  355.      */
  356.     public Vector3 computeNormal(Vector3 v0, Vector3 v1, Vector3 v2)
  357.     {
  358.         Vector3 vert1 = new Vector3(v1.getX() - v0.getX(), v1.getY() - v0.getY(), v1.getZ() - v0.getZ());
  359.         Vector3 vert2 = new Vector3(v2.getX() - v0.getX(), v2.getY() - v0.getY(), v2.getZ() - v0.getZ());
  360.  
  361.         return vert1.crossProduct(vert2).normalize();
  362.     }
  363.  
  364.     @Override
  365.     /**
  366.      * @return The vector3 as an array of floats.
  367.      */
  368.     public float[] toFloats()
  369.     {
  370.         return new float[] {this.getX(),
  371.                             this.getY(),
  372.                             this.getZ()};
  373.     }
  374.  
  375.     @Override
  376.     /**
  377.      * @return The vector3 as a FloatBuffer.
  378.      */
  379.     public FloatBuffer toBuffer()
  380.     {
  381.         return (FloatBuffer) BufferUtils.createFloatBuffer(3).put(this.toFloats()).flip();
  382.     }
  383.  
  384.     @Override
  385.     /**
  386.      * Check if this vector3 and another vector3 equals.
  387.      *
  388.      * @param object The other vector3.
  389.      *
  390.      * @return If the vector3s equals.
  391.      */
  392.     public boolean equals(Object object)
  393.     {
  394.         if (!(object instanceof Vector3))
  395.         {
  396.             return false;
  397.         }
  398.  
  399.         Vector3 vector = (Vector3) object;
  400.  
  401.         return (this.getX() == vector.getX() &&
  402.                 this.getY() == vector.getY() &&
  403.                 this.getZ() == vector.getZ());
  404.     }
  405.  
  406.     @Override
  407.     /**
  408.      * @return The data of the vector3 in form of a String.
  409.      */
  410.     public String toString()
  411.     {
  412.         return this.getX() + ", " + this.getY() + ", " + this.getZ();
  413.     }
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement