Android Open Source - Look Vector3






From Project

Back to project page Look.

License

The source code is released under:

====================== LOOK! LICENSING TERMS ====================== look! is licensed under the BSD 3-Clause (also known as "BSD New" or "BSD Simplified"), as follows: Copyright (c) 2010-2012, Look...

If you think the Android project Look listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
*-----------------------------------------------------------------------------
* Copyright (c) 2012, Look! Development Team
* All rights reserved./* w  ww .ja  va 2s. c o m*/
*
* Distributed under the terms of the BSD Simplified License.
*
* The full license is in the LICENSE file, distributed with this software.
*-----------------------------------------------------------------------------
*/
package es.ucm.look.ar.math.geom;

/**
 * Represents a 3 dimensions vector
 * 
 * @author ??ngel Serrano
 * 
 */
public class Vector3 extends Point3 {

  /**
   * Vector module
   */
  protected float module;

  // Next fields are used to avoid module recalculation every time the method
  // is called. They store the coordinates values that was used to calculate
  // the module

  private float xModule;

  private float yModule;

  private float zModule;

  /**
   * Constructs a vector from its 3 coordinates
   * 
   * @param x
   *            x coordinate
   * @param y
   *            y coordinate
   * @param z
   *            z coordinate
   */
  public Vector3(float x, float y, float z) {
    super(x, y, z);
    updateModule();
  }

  /**
   * Constructs a vector that points from p1 to p2
   * 
   * @param p1
   *            Starting point for the vector
   * @param p2
   *            Finish point for the vector
   */
  public Vector3(Point3 p1, Point3 p2) {
    super(p2.x - p1.x, p2.y - p1.y, p2.z - p1.z);
  }

  /**
   * Constructs a vector from another vector. It just clones its coordinates
   * 
   * @param v
   */
  public Vector3(Point3 v) {
    super(v);
  }

  public void set(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
    updateModule();
  }

  /**
   * Returns the dot product with the given vector
   * 
   * @param v
   *            the vector
   * @return the dot product
   */
  public float dotProduct(Vector3 v) {
    return x * v.x + y * v.y + z * v.z;
  }

  /**
   * Returns vector module
   * 
   * @return vector module
   */
  public float module() {
    if (!(x == xModule && y == yModule && z == zModule)) {
      updateModule();
    }
    return module;
  }
  
  /**
   * Returns module's square, avoiding do the square root
   * @return
   */
  public float module2(){
    return x * x + y * y + z * z;
  }

  /**
   * Returns the formed angle with the given vector
   * 
   * @param v
   *            the vector
   * @return the tiniest angle between the two vectors at the plane formed by
   *         both
   */
  public float angle(Vector3 v) {
    float cosAngle = dotProduct(v) / (module() * v.module());
    return (float) Math.acos(cosAngle);
  }

  /**
   * Returns the cross product with the given vector
   * 
   * @param v
   *            the vector
   * @return the vector result from the cross product
   */
  public Vector3 crossProduct(Vector3 v) {
    float newX = y * v.z - z * v.y;
    float newY = z * v.x - x * v.z;
    float newZ = x * v.y - y * v.x;

    return new Vector3(newX, newY, newZ);
  }

  /**
   * Returns the normal vector to the plane formed by the 3 given points
   * 
   * @param p1
   *            point 1
   * @param p2
   *            point 2
   * @param p3
   *            point 3
   * @return the normal vector to the plane formed by the 3 given points
   */
  public static Vector3 normalVector(Point3 p1, Point3 p2, Point3 p3) {
    Vector3 v1 = new Vector3(p1, p2);
    Vector3 v2 = new Vector3(p1, p3);
    return v1.crossProduct(v2);
  }

  /**
   * Normalize the vector
   */
  public void normalize() {
    float module = this.module();
    this.x /= module;
    this.y /= module;
    this.z /= module;
    updateModule();
  }

  /**
   * Returns the index from the vector's greatest absolute coordinate
   * 
   * @return the index from the vector's greatest absolute coordinate. 0 for
   *         the x, 1 for the y, 2 for the z
   */
  public int getGreatestComponent() {
    int c = 0;
    if (Math.abs(y) > Math.abs(x) && Math.abs(y) > Math.abs(z))
      c = 1;
    else if (Math.abs(z) > Math.abs(y) && Math.abs(z) > Math.abs(x))
      c = 2;
    return c;
  }

  /**
   * Adds a vector and then, if normalize is <b>true</b> normalize the vector
   * 
   * @param v
   *            the vector to be added
   * @param normalize
   *            if the vector must be normalized after the addition
   */
  public void add(Vector3 v, boolean normalize) {
    add(v);
    if (normalize)
      normalize();

  }

  /**
   * Scales the vector
   * 
   * @param k
   *            scale factor
   */
  public void scale(float k) {
    x *= k;
    y *= k;
    z *= k;
    updateModule();
  }

  /**
   * Premultiply the vector with a matrix
   * 
   * @param m
   *            the matrix
   */
  public void preMultiply(float[] m) {
    int i = 0;
    float newX = x * m[i++] + y * m[i++] + z * m[i++];
    float newY = x * m[i++] + y * m[i++] + z * m[i++];
    float newZ = x * m[i++] + y * m[i++] + z * m[i++];
    set(newX, newY, newZ);
  }

  /**
   * Rotate the vector around x-axis
   * 
   * @param rotX
   *            rotation in radians
   */
  public void rotateX(float rotX) {
    float rX[] = new float[] { 1, 0, 0, 0, (float) Math.cos(rotX), (float) -Math.sin(rotX), 0, (float) Math.sin(rotX), (float) Math.cos(rotX) };
    preMultiply(rX);
  }

  /**
   * Rotate the vector around y-axis
   * 
   * @param rotY
   *            rotation in radians
   */
  public void rotateY(float angle) {
    float ry[] = { (float) Math.cos(angle), 0, (float) Math.sin(angle), 0, 1, 0, (float) -Math.sin(angle), 0, (float) Math.cos(angle) };
    preMultiply(ry);

  }

  /**
   * Rotate the vector around z-axis
   * 
   * @param rotZ
   *            rotation in radians
   */
  public void rotateZ(float rotZ) {
    float rZ[] = new float[] { (float) Math.cos(rotZ), (float) -Math.sin(rotZ), 0, (float) Math.sin(rotZ), (float) Math.cos(rotZ), 0, 0, 0, 1 };
    preMultiply(rZ);
  }

  private static Vector3 volatileVector = new Vector3(0, 0, 0);

  /**
   * Returns a volatile vector pointing from p1 to p2. We use a the volatile
   * vector when we want to do calculations with vectors, but it's not
   * necessary to keep the instance
   */
  public static Vector3 getVolatileVector(Point3 p1, Point3 p2) {
    volatileVector.x = p2.x - p1.x;
    volatileVector.y = p2.y - p1.y;
    volatileVector.z = p2.z - p1.z;
    volatileVector.updateModule();
    return volatileVector;
  }
  
  public static Vector3 getVolatileVector( float x1, float y1, float z1, float x2, float y2, float z2 ){
    volatileVector.x = x2 - x1;
    volatileVector.y = y2 - y1;
    volatileVector.z = z2 - z1;
    volatileVector.updateModule();
    return volatileVector;
  }

  private void updateModule() {
    xModule = x;
    yModule = y;
    zModule = z;
    module = (float) Math.sqrt(x * x + y * y + z * z);
  }

  /**
   * Creates a vector from point p1 to point p2
   * 
   * @param p1
   *            start point
   * @param p2
   *            end point
   */
  public void set(Point3 p1, Point3 p2) {
    x = p2.x - p1.x;
    y = p2.y - p1.y;
    z = p2.z - p1.z;
    updateModule();

  }

  /**
   * Creates the vector form point defined by (x1, y1, z1) to point defined by
   * (x2, y2, z2)
   * 
   * @param x1
   * @param y1
   * @param z1
   * @param x2
   * @param y2
   * @param z2
   */
  public void set(float x1, float y1, float z1, float x2, float y2, float z2) {
    x = x2 - x1;
    y = y2 - y1;
    z = z2 - z1;
    updateModule();
  }

}




Java Source Code List

es.ucm.look.ar.LookAR.java
es.ucm.look.ar.Preview.java
es.ucm.look.ar.ar2D.AR2D.java
es.ucm.look.ar.ar2D.Drawable2D.java
es.ucm.look.ar.ar2D.HUDElement.java
es.ucm.look.ar.ar2D.drawables.Circle2D.java
es.ucm.look.ar.ar2D.drawables.Image2D.java
es.ucm.look.ar.ar2D.drawables.Text2D.java
es.ucm.look.ar.ar3D.Drawable3D.java
es.ucm.look.ar.ar3D.Renderer3D.java
es.ucm.look.ar.ar3D.core.Color4.java
es.ucm.look.ar.ar3D.core.TextureFactory.java
es.ucm.look.ar.ar3D.core.camera.Camera3D.java
es.ucm.look.ar.ar3D.core.camera.OrientedCamera.java
es.ucm.look.ar.ar3D.core.drawables.DrawablesDataBase.java
es.ucm.look.ar.ar3D.core.drawables.Entity3D.java
es.ucm.look.ar.ar3D.core.drawables.Mesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.CirclePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Cube.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Grid.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.LinesLoopPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.ObjMesh3D.java
es.ucm.look.ar.ar3D.core.drawables.primitives.PointPrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.Ring.java
es.ucm.look.ar.ar3D.core.drawables.primitives.SquarePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.TrianglePrimitive.java
es.ucm.look.ar.ar3D.core.drawables.primitives.extra.ImagePrimitive.java
es.ucm.look.ar.ar3D.parser.MeshObjParser.java
es.ucm.look.ar.hud.ActionListener.java
es.ucm.look.ar.hud.BasicHud.java
es.ucm.look.ar.hud.Button.java
es.ucm.look.ar.hud.HUD.java
es.ucm.look.ar.listeners.CameraListener.java
es.ucm.look.ar.listeners.TouchListener.java
es.ucm.look.ar.math.collision.Armature.java
es.ucm.look.ar.math.collision.SphericalArmature.java
es.ucm.look.ar.math.collision.SquareArmature.java
es.ucm.look.ar.math.collision.debug.DebugArmature.java
es.ucm.look.ar.math.collision.debug.SphericalDebugArmature.java
es.ucm.look.ar.math.collision.debug.SquareDebugArmature.java
es.ucm.look.ar.math.geom.Matrix3.java
es.ucm.look.ar.math.geom.Plane.java
es.ucm.look.ar.math.geom.Point2.java
es.ucm.look.ar.math.geom.Point3.java
es.ucm.look.ar.math.geom.Ray.java
es.ucm.look.ar.math.geom.Triangle.java
es.ucm.look.ar.math.geom.Vector3.java
es.ucm.look.ar.util.CameraParametersHelper.java
es.ucm.look.ar.util.DeviceOrientation.java
es.ucm.look.ar.util.LookARUtil.java
es.ucm.look.ar.util.PositionTimerTask.java
es.ucm.look.data.EntityData.java
es.ucm.look.data.LookData.java
es.ucm.look.data.WorldEntityFactory.java
es.ucm.look.data.WorldEntity.java
es.ucm.look.data.World.java
es.ucm.look.data.filesManager.LookFilesManager.java
es.ucm.look.data.interfaces.DataGetter.java
es.ucm.look.data.interfaces.DataHandler.java
es.ucm.look.data.interfaces.DataSetter.java
es.ucm.look.data.local.BasicDataHandler.java
es.ucm.look.data.local.DBDataHandler.java
es.ucm.look.data.local.contentprovider.LookContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLContentProvider.java
es.ucm.look.data.local.contentprovider.sql.LookSQLHelper.java
es.ucm.look.data.remote.ConfigNet.java
es.ucm.look.data.remote.LookProperties.java
es.ucm.look.data.remote.RemoteDBHandler.java
es.ucm.look.data.remote.restful.LookService.java
es.ucm.look.data.remote.restful.RestMethod.java
es.ucm.look.data.remote.restful.ServiceManager.java
es.ucm.look.location.LocationManager.java
es.ucm.look.locationProvider.DeviceSensor.java
es.ucm.look.locationProvider.InertialNavigationSystem.java
es.ucm.look.locationProvider.LocationProvider.java
es.ucm.look.locationProvider.Motion.java
es.ucm.look.locationProvider.Positioning.java
es.ucm.look.locationProvider.Util.java
es.ucm.look.locationProviderWifi.Cliente.java
es.ucm.look.locationProviderWifi.WifiLocation.java
es.ucm.look.locationProviderWifi.WifiService.java
es.ucm.look.locationProviderWifi.util.DateUtils.java
es.ucm.look.locationProviderWifi.util.DeviceReader.java
es.ucm.look.locationProviderWifi.util.DeviceWriter.java
es.ucm.look.locationProviderWifi.wifi.Lugar.java
es.ucm.look.locationProviderWifi.wifi.Lugares.java
es.ucm.look.locationProviderWifi.wifi.NodoWifi.java
es.ucm.look.locationProvider.map.Mapa.java
es.ucm.look.locationProvider.test.java