Android Open Source - android-augment-reality-framework Vector






From Project

Back to project page android-augment-reality-framework.

License

The source code is released under:

GNU General Public License

If you think the Android project android-augment-reality-framework 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) 2010- Peer internet solutions
 * //from  w  ww  .  j  ava  2s  . c om
 * This file was an original part of mixare.
 * 
 * This program is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details. 
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program. If not, see <http://www.gnu.org/licenses/>
 */
package com.jwetherell.augmented_reality.common;

/**
 * A Vector representation which adds many of the mathematical operations
 * involved in Vectors.
 * 
 * This file was adapted from Mixare <http://www.mixare.org/>
 * 
 * @author Daniele Gobbetti <info@mixare.org>
 * @author Justin Wetherell <phishman3579@gmail.com>
 */
public class Vector {

    private final float[] matrixArray = new float[9];

    private float x = 0f;
    private float y = 0f;
    private float z = 0f;

    public Vector() {
        this(0, 0, 0);
    }

    public Vector(Vector v) {
        this(v.x, v.y, v.z);
    }

    /**
     * Set the vector's values.
     * 
     * @param x
     *            float x value.
     * @param y
     *            float y value.
     * @param z
     *            float z value.
     */
    public Vector(float x, float y, float z) {
        set(x, y, z);
    }

    public float getX() {
        return x;
    }

    public void setX(float x) {
        this.x = x;
    }

    public float getY() {
        return y;
    }

    public void setY(float y) {
        this.y = y;
    }

    public float getZ() {
        return z;
    }

    public void setZ(float z) {
        this.z = z;
    }

    /**
     * Get the vector's values.
     * 
     * array[0] = x; array[1] = y; array[2] = z;
     * 
     * @param array
     *            float array representing this vector.
     */
    public void get(float[] array) {
        if (array == null || array.length != 3) throw new IllegalArgumentException("get() array must be non-NULL and size of 3");

        array[0] = this.x;
        array[1] = this.y;
        array[2] = this.z;
    }

    /**
     * Set the vector from a given vector.
     * 
     * @param v
     *            Vector to copy values form.
     */
    public void set(Vector v) {
        if (v == null) return;

        // on set()
        set(v.x, v.y, v.z);
    }

    /**
     * Set the vector's values.
     * 
     * array[0] = x; array[1] = y; array[2] = z;
     * 
     * @param array
     *            float array representing this vector.
     */
    public void set(float[] array) {
        if (array == null || array.length != 3) throw new IllegalArgumentException("get() array must be non-NULL and size of 3");

        // on set()
        set(array[0], array[1], array[2]);
    }

    /**
     * Set the Vector to the given values.
     * 
     * @param x
     *            float x value.
     * @param y
     *            float y value.
     * @param z
     *            float z value.
     */
    public void set(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;

        Vector v = (Vector) obj;
        return (v.x == this.x && v.y == this.y && v.z == this.z);
    }

    public boolean equals(float x, float y, float z) {
        return (this.x == x && this.y == y && this.z == z);
    }

    public void add(Vector v) {
        if (v == null) return;

        // on add()
        add(v.x, v.y, v.z);
    }

    public void sub(float x, float y, float z) {

        // on add()
        add(-x, -y, -z);
    }

    public void sub(Vector v) {
        if (v == null) return;

        // on add()
        add(-v.x, -v.y, -v.z);
    }

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

    public void mult(float s) {
        this.x *= s;
        this.y *= s;
        this.z *= s;
    }

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

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

    public void norm() {
        // on divide()
        divide(length());
    }

    public void divide(float s) {
        this.x /= s;
        this.y /= s;
        this.z /= s;
    }

    public float dot(Vector v) {
        if (v == null) return 0f;

        return this.x * v.x + this.y * v.y + this.z * v.z;
    }

    public void cross(Vector u, Vector v) {
        if (v == null || u == null) return;

        float x = u.y * v.z - u.z * v.y;
        float y = u.z * v.x - u.x * v.z;
        float z = u.x * v.y - u.y * v.x;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void prod(Matrix m) {
        if (m == null) return;

        m.get(matrixArray);
        float xTemp = matrixArray[0] * this.x + matrixArray[1] * this.y + matrixArray[2] * this.z;
        float yTemp = matrixArray[3] * this.x + matrixArray[4] * this.y + matrixArray[5] * this.z;
        float zTemp = matrixArray[6] * this.x + matrixArray[7] * this.y + matrixArray[8] * this.z;

        this.x = xTemp;
        this.y = yTemp;
        this.z = zTemp;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "<" + this.x + ", " + this.y + ", " + this.z + ">";
    }
}




Java Source Code List

com.jwetherell.augmented_reality.activity.AugmentedReality.java
com.jwetherell.augmented_reality.activity.AugmentedView.java
com.jwetherell.augmented_reality.activity.Demo.java
com.jwetherell.augmented_reality.activity.SensorsActivity.java
com.jwetherell.augmented_reality.camera.CameraCompatibility.java
com.jwetherell.augmented_reality.camera.CameraModel.java
com.jwetherell.augmented_reality.camera.CameraSurface.java
com.jwetherell.augmented_reality.common.LowPassFilter.java
com.jwetherell.augmented_reality.common.Matrix.java
com.jwetherell.augmented_reality.common.Navigation.java
com.jwetherell.augmented_reality.common.Orientation.java
com.jwetherell.augmented_reality.common.ReusableString.java
com.jwetherell.augmented_reality.common.Vector.java
com.jwetherell.augmented_reality.data.ARData.java
com.jwetherell.augmented_reality.data.BuzzDataSource.java
com.jwetherell.augmented_reality.data.DataSource.java
com.jwetherell.augmented_reality.data.GooglePlacesDataSource.java
com.jwetherell.augmented_reality.data.LocalDataSource.java
com.jwetherell.augmented_reality.data.NetworkDataSource.java
com.jwetherell.augmented_reality.data.PhysicalLocation.java
com.jwetherell.augmented_reality.data.ScreenPosition.java
com.jwetherell.augmented_reality.data.TwitterDataSource.java
com.jwetherell.augmented_reality.data.WikipediaDataSource.java
com.jwetherell.augmented_reality.ui.IconMarker.java
com.jwetherell.augmented_reality.ui.Marker.java
com.jwetherell.augmented_reality.ui.Radar.java
com.jwetherell.augmented_reality.ui.objects.PaintableBox.java
com.jwetherell.augmented_reality.ui.objects.PaintableBoxedText.java
com.jwetherell.augmented_reality.ui.objects.PaintableCircle.java
com.jwetherell.augmented_reality.ui.objects.PaintableGps.java
com.jwetherell.augmented_reality.ui.objects.PaintableIcon.java
com.jwetherell.augmented_reality.ui.objects.PaintableLine.java
com.jwetherell.augmented_reality.ui.objects.PaintableObject.java
com.jwetherell.augmented_reality.ui.objects.PaintablePoint.java
com.jwetherell.augmented_reality.ui.objects.PaintablePosition.java
com.jwetherell.augmented_reality.ui.objects.PaintableRadarPoints.java
com.jwetherell.augmented_reality.ui.objects.PaintableText.java
com.jwetherell.augmented_reality.widget.VerticalSeekBar.java
com.jwetherell.augmented_reality.widget.VerticalTextView.java