All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bimserver.geometry.Vector Maven / Gradle / Ivy

package org.bimserver.geometry;

/******************************************************************************
 * Copyright (C) 2009-2017  BIMserver.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see {@literal}.
 *****************************************************************************/

public class Vector {
    // dot product (3D) which allows vector operations in arguments
    public static float dot(float[] u,float[] v) {
        return ((u[X] * v[X]) + (u[Y] * v[Y]) + (u[Z] * v[Z]));
    }
    public static float[] minus(float[] u, float[] v){
        return new float[]{u[X]-v[X],u[Y]-v[Y],u[Z]-v[Z]};
    }
    public static float[] addition(float[] u, float[] v){
        return new float[]{u[X]+v[X],u[Y]+v[Y],u[Z]+v[Z]};
    }
    //scalar product
    public static float[] scalarProduct(float r, float[] u){
        return new float[]{u[X]*r,u[Y]*r,u[Z]*r};
    }
    // (cross product)
    public static float[] crossProduct(float[] u, float[] v){
        return new float[]{(u[Y]*v[Z]) - (u[Z]*v[Y]),(u[Z]*v[X]) - (u[X]*v[Z]),(u[X]*v[Y]) - (u[Y]*v[X])};
    }

    // (cross product)
    public static double[] crossProduct(double[] u, double[] v){
        return new double[]{(u[Y]*v[Z]) - (u[Z]*v[Y]),(u[Z]*v[X]) - (u[X]*v[Z]),(u[X]*v[Y]) - (u[Y]*v[X])};
    }

    //mangnatude or length
    public static float length(float[] u){
        return (float) Math.abs(Math.sqrt((u[X] *u[X]) + (u[Y] *u[Y]) + (u[Z] *u[Z])));
    }
 
    public static final int X = 0;
    public static final int Y = 1;
    public static final int Z = 2;
	public static void normalize(float[] rayDir) {
		float l = length(rayDir);
		rayDir[0] = rayDir[0] / l;
		rayDir[1] = rayDir[1] / l;
		rayDir[2] = rayDir[2] / l;
	}

	public static void dump(float[] v) {
		System.out.println(v[0] + ", " + v[1] + ", " + v[2] + (v.length == 4 ? (", " + v[3]) : ""));
	}

	public static void dump(String pre, float[] v) {
		System.out.println(pre + ": " + v[0] + ", " + v[1] + ", " + v[2] + (v.length == 4 ? (", " + v[3]) : ""));
	}
	
	public static float getArea(float[][] triangle) {
		float[] ab = minus(triangle[1], triangle[0]);
		float[] ac = minus(triangle[2], triangle[0]);
		float[] cross = crossProduct(ab, ac);
		return (float) (0.5f * Math.sqrt(cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]));
	}
	
	public static void main(String[] args) {
		float[][] triangle = new float[3][3];
		triangle[0][0] = -5;
		triangle[0][1] = 0;
		triangle[0][2] = 0;
		triangle[1][0] = 1;
		triangle[1][1] = 0;
		triangle[1][2] = 0;
		triangle[2][0] = 0;
		triangle[2][1] = 1;
		triangle[2][2] = 0;
		
		System.out.println(getArea(triangle));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy