org.bimserver.utils.math.Vector Maven / Gradle / Ivy
package org.bimserver.utils.math;
/******************************************************************************
* Copyright (C) 2009-2018 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 double dot(double[] u,double[] 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 double[] minus(double[] u, double[] v){
return new double[]{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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy