Android Open Source - Look Matrix3






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./*from   w ww . ja v a  2  s  .c om*/
*
* 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;

/**
 * Represent a 3D Matrix that can be used with OpenGL. It has functions to
 * rotate, scale and translate
 * 
 * @author ??ngel Serrano
 * 
 */
public class Matrix3 {
  /**
   * Dimension for the matrix array
   */
  private static final int DIMENSION = 16;

  private float m[];

  /**
   * Constructs a matrix with the identity
   * 
   */
  public Matrix3() {
    m = new float[DIMENSION];
    loadIdentity(m);
  }

  /**
   * Returns the float's array representing the matrix in the form:
   * <p>
   * {@code [ m0 m4 m8  m12 ]}
   * </p>
   * <p>
   * {@code [ m1 m5 m9  m13 ]}
   * </p>
   * <p>
   * {@code [ m2 m6 m10 m14 ]}
   * </p>
   * <p>
   * {@code [ m3 m7 m11 m15 ]}
   * </p>
   * 
   * @return the float's array representing the matrix
   */
  public float[] getMatrix() {
    return m;
  }

  /**
   * Sets the identity for this matrix
   */
  public void setIdentity() {
    loadIdentity(m);
  }

  /**
   * Loads the identity matrix in an array
   * 
   * @param m
   *            the array
   */
  private void loadIdentity(float m[]) {
    for (int i = 0; i < DIMENSION; i++) {
      m[i] = 0;
    }
    m[0] = m[5] = m[10] = m[15] = 1;
  }

  /**
   * Translates with the given parameters
   * 
   * @param x
   *            translation in x axis
   * @param y
   *            translation in y axis
   * @param z
   *            translation in z axis
   */
  public void translate(float x, float y, float z) {
    float mt[] = new float[DIMENSION];
    loadIdentity(mt);
    mt[12] = x;
    mt[13] = y;
    mt[14] = z;
    postMultiply(mt);
  }

  /**
   * Multiplies m1*m2, and deposit the result at mresult
   * 
   * @param m1
   *            first matrix
   * @param m2
   *            second matrix
   * @param mresult
   *            result matrix
   */
  private void multiplyMatrix(float m1[], float m2[], float mresult[]) {
    float matrix[] = new float[DIMENSION];
    int row = 0, column = 0;

    for (int i = 0; i < 16; i++) {
      row = i % 4;
      column = i / 4;
      matrix[i] = 0;
      for (int j = 0; j < 4; j++) {
        matrix[i] += m1[row + j * 4] * m2[column * 4 + j];
      }
    }

    for (int i = 0; i < 16; i++)
      mresult[i] = matrix[i];
  }

  /**
   * Postmultiplies the current matrix with the given one
   * 
   * @param matrix
   *            matrix to postmultiply
   */
  public void postMultiply(float matrix[]) {
    multiplyMatrix(m, matrix, m);
  }

  /**
   * Premultiplies the current matrix with the given one
   * 
   * @param matrix
   *            matrix to premultiply
   */
  public void preMultipliy(float matrix[]) {
    multiplyMatrix(matrix, m, m);
  }

  /**
   * Scales the matrix with the given factors
   * 
   * @param x
   *            scale in x axis
   * @param y
   *            scale in y axis
   * @param z
   *            scale in z axis
   */
  public void scale(float x, float y, float z) {
    float matrix[] = new float[DIMENSION];
    loadIdentity(matrix);
    matrix[0] = x;
    matrix[5] = y;
    matrix[10] = z;
    postMultiply(matrix);
  }

  /**
   * Rotates the matrix with the given rotations (in radians)
   * 
   * @param x
   *            rotation in x axis
   * @param y
   *            rotation in y axis
   * @param z
   *            rotation in z axis
   */
  public void rotate(float x, float y, float z) {
    float mx[] = new float[DIMENSION];
    float my[] = new float[DIMENSION];
    float mz[] = new float[DIMENSION];
    float mi[] = new float[DIMENSION];
    float mr[] = new float[DIMENSION];
    initMatrixRotationX(mx, x);
    initRotationMatrixY(my, y);
    initMatrixRotationZ(mz, z);

    multiplyMatrix(mx, my, mi);
    multiplyMatrix(mi, mz, mr);
    postMultiply(mr);
  }

  /**
   * Creates in m the matrix for the given rotation in x
   * 
   * @param m
   *            where the result will be stored
   * @param radians
   *            radians to rotate
   */
  private void initMatrixRotationX(float m[], float radians) {
    loadIdentity(m);
    float cosR = (float) Math.cos(radians);
    float sinR = (float) Math.sin(radians);
    m[5] = m[10] = cosR;
    m[9] = -sinR;
    m[6] = sinR;
  }

  /**
   * Creates in m the matrix for the given rotation in y
   * 
   * @param m
   *            where the result will be stored
   * @param radians
   *            radians to rotate
   */
  private void initRotationMatrixY(float m[], float radians) {
    loadIdentity(m);
    float cosR = (float) Math.cos(radians);
    float sinR = (float) Math.sin(radians);
    m[0] = m[10] = cosR;
    m[2] = -sinR;
    m[8] = sinR;
  }

  /**
   * Creates in m the matrix for the given rotation in z
   * 
   * @param m
   *            where the result will be stored
   * @param radians
   *            radians to rotate
   */
  private void initMatrixRotationZ(float m[], float radians) {
    loadIdentity(m);
    float cosR = (float) Math.cos(radians);
    float sinR = (float) Math.sin(radians);
    m[0] = m[5] = cosR;
    m[4] = -sinR;
    m[1] = sinR;
  }

  public void set(float[] matrix) {
    if ( matrix != null && matrix.length == 16 ){
      m = matrix;
    }
  }
  
  public void transform( Point3 p ){
    float x = m[0] * p.x + m[4]* p.y + m[8]*p.z + m[12];
    float y = m[1] * p.x + m[5]* p.y + m[9]*p.z + m[13];
    float z = m[2] * p.x + m[6]* p.y + m[10]*p.z + m[14];
    p.set(x, y, z);
  }

}




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