Android Open Source - jmini3d Vector3






From Project

Back to project page jmini3d.

License

The source code is released under:

Copyright 2012 Mobialia http://www.mobialia.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to ...

If you think the Android project jmini3d 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

package jmini3d;
/*from   w ww . j a va2s  .com*/
public class Vector3 {
  public float x;
  public float y;
  public float z;

  private static Vector3 tmp = new Vector3();

  public Vector3() {
    x = 0;
    y = 0;
    z = 0;
  }

  public Vector3(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

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

  public void setAllFrom(Vector3 n) {
    x = n.x;
    y = n.y;
    z = n.z;
  }

  public void normalize() {
    float mod = (float) Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);

    if (mod != 0 && mod != 1) {
      mod = 1 / mod;
      this.x *= mod;
      this.y *= mod;
      this.z *= mod;
    }
  }

  public void add(Vector3 n) {
    this.x += n.x;
    this.y += n.y;
    this.z += n.z;
  }

  public void subtract(Vector3 n) {
    this.x -= n.x;
    this.y -= n.y;
    this.z -= n.z;
  }

  public void multiply(float f) {
    this.x *= f;
    this.y *= f;
    this.z *= f;
  }

  public float length() {
    return (float) Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  }

  public Vector3 clone() {
    return new Vector3(x, y, z);
  }

  public void rotateX(float angle) {
    float cosRY = (float) Math.cos(angle);
    float sinRY = (float) Math.sin(angle);

    tmp.setAll(this.x, this.y, this.z);

    this.y = (tmp.y * cosRY) - (tmp.z * sinRY);
    this.z = (tmp.y * sinRY) + (tmp.z * cosRY);
  }

  public void rotateY(float angle) {
    float cosRY = (float) Math.cos(angle);
    float sinRY = (float) Math.sin(angle);

    tmp.setAll(this.x, this.y, this.z);

    this.x = (tmp.x * cosRY) + (tmp.z * sinRY);
    this.z = (tmp.x * -sinRY) + (tmp.z * cosRY);
  }

  public void rotateZ(float angle) {
    float cosRY = (float) Math.cos(angle);
    float sinRY = (float) Math.sin(angle);

    tmp.setAll(this.x, this.y, this.z);

    this.x = (tmp.x * cosRY) - (tmp.y * sinRY);
    this.y = (tmp.x * sinRY) + (tmp.y * cosRY);
  }

  public void rotateAxis(Vector3 a, float angle) {
    float c = (float) Math.cos(angle);
    float s = (float) Math.sin(angle);
    float t = 1 - (float) Math.cos(angle);

    float nx = x * (t * a.x * a.x + c) + y * (t * a.x * a.y - s * a.z) + z * (t * a.x * a.z + s * a.y);
    float ny = x * (t * a.x * a.y + s * a.z) + y * (t * a.y * a.y + c) + z * (t * a.y * a.z - s * a.x);
    float nz = x * (t * a.x * a.z - s * a.y) + y * (t * a.y * a.z + s * a.x) + z * (t * a.z * a.z + c);

    x = nx;
    y = ny;
    z = nz;
  }

  public static Vector3 add(Vector3 a, Vector3 b) {
    return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  }

  public static Vector3 subtract(Vector3 a, Vector3 b) {
    return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  }

  public static Vector3 multiply(Vector3 a, Vector3 b) {
    return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  }

  public static Vector3 cross(Vector3 v, Vector3 w) {
    return new Vector3((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x));
  }

  public static void cross(Vector3 result, Vector3 v, Vector3 w) {
    result.setAll((w.y * v.z) - (w.z * v.y), (w.z * v.x) - (w.x * v.z), (w.x * v.y) - (w.y * v.x));
  }

  public static float dot(Vector3 v, Vector3 w) {
    return (v.x * w.x + v.y * w.y + w.z * v.z);
  }

  @Override
  public boolean equals(Object o) {
    if (o == null) {
      return false;
    }
    if (!(o instanceof Vector3)) {
      return false;
    }
    return x == (((Vector3) o).x) && (y == ((Vector3) o).y) && (z == ((Vector3) o).z);
  }

  @Override
  public String toString() {
    return x + "," + y + "," + z;
  }
}




Java Source Code List

cocoonjs.CocoonJsLinker.java
jmini3d.Blending.java
jmini3d.Camera.java
jmini3d.Color4.java
jmini3d.CubeMapTexture.java
jmini3d.Font.java
jmini3d.GpuObjectStatus.java
jmini3d.MatrixUtils.java
jmini3d.Object3d.java
jmini3d.Rect.java
jmini3d.SceneController.java
jmini3d.Scene.java
jmini3d.Texture.java
jmini3d.Utils.java
jmini3d.Vector3.java
jmini3d.android.Activity3d.java
jmini3d.android.GeometryBuffers.java
jmini3d.android.GlSurfaceView3d.java
jmini3d.android.GpuUploader.java
jmini3d.android.Program.java
jmini3d.android.Renderer3d.java
jmini3d.android.ResourceLoader.java
jmini3d.android.compat.CompatibilityWrapper5.java
jmini3d.android.demo.DemoActivity.java
jmini3d.android.input.InputController.java
jmini3d.demo.ArialFont.java
jmini3d.demo.CubeScene.java
jmini3d.demo.CubesScene.java
jmini3d.demo.DemoSceneController.java
jmini3d.demo.EnvMapCubeScene.java
jmini3d.demo.NormalMapScene.java
jmini3d.demo.ParentScene.java
jmini3d.demo.TeapotGeometry.java
jmini3d.demo.TeapotScene.java
jmini3d.geometry.BoxGeometry.java
jmini3d.geometry.Geometry.java
jmini3d.geometry.PlaneGeometry.java
jmini3d.geometry.SkyboxGeometry.java
jmini3d.geometry.SpriteGeometry.java
jmini3d.geometry.VariableGeometry.java
jmini3d.gwt.Canvas3d.java
jmini3d.gwt.EngineResources.java
jmini3d.gwt.EntryPoint3d.java
jmini3d.gwt.GeometryBuffers.java
jmini3d.gwt.GpuUploader.java
jmini3d.gwt.MyInt16Array.java
jmini3d.gwt.Program.java
jmini3d.gwt.Renderer3d.java
jmini3d.gwt.ResourceLoader.java
jmini3d.gwt.TextureLoadedListener.java
jmini3d.gwt.demo.DemoEntryPoint.java
jmini3d.gwt.input.InputController.java
jmini3d.input.KeyListener.java
jmini3d.input.TouchListener.java
jmini3d.input.TouchPointer.java
jmini3d.light.AmbientLight.java
jmini3d.light.DirectionalLight.java
jmini3d.light.Light.java
jmini3d.light.PointLight.java
jmini3d.material.Material.java
jmini3d.material.PhongMaterial.java
jmini3d.material.SpriteMaterial.java
jmini3d.utils.Fnt2Class.java
jmini3d.utils.Obj2Class.java