00001 package edu.tum.cs.util.math; 00002 00003 import javax.vecmath.Matrix4f; 00004 00005 public class Vector3f extends javax.vecmath.Vector3f { 00006 private static final long serialVersionUID = 1L; 00007 00008 public Vector3f() { 00009 super(); 00010 } 00011 00012 public Vector3f(float x, float y, float z) { 00013 super(x,y,z); 00014 } 00015 00016 public Vector3f(javax.vecmath.Vector3f v) { 00017 super(v); 00018 } 00019 00020 public double distance(Vector3f other) { 00021 float xd = x-other.x; 00022 float yd = y-other.y; 00023 float zd = z-other.z; 00024 return Math.sqrt(xd*xd+yd*yd+zd*zd); 00025 } 00026 00027 public void subtract(Vector3f other) { 00028 x -= other.x; 00029 y -= other.y; 00030 z -= other.z; 00031 } 00032 00033 public void rotate(double theta, Vector3f axis) { 00034 Vector3f p = this; 00035 Vector3f q = new Vector3f(0.0f,0.0f,0.0f); 00036 double costheta, sintheta; 00037 00038 Vector3f r = new Vector3f(axis); 00039 r.normalize(); 00040 00041 costheta = Math.cos(theta); 00042 sintheta = Math.sin(theta); 00043 00044 q.x += (costheta + (1 - costheta) * r.x * r.x) * p.x; 00045 q.x += ((1 - costheta) * r.x * r.y - r.z * sintheta) * p.y; 00046 q.x += ((1 - costheta) * r.x * r.z + r.y * sintheta) * p.z; 00047 00048 q.y += ((1 - costheta) * r.x * r.y + r.z * sintheta) * p.x; 00049 q.y += (costheta + (1 - costheta) * r.y * r.y) * p.y; 00050 q.y += ((1 - costheta) * r.y * r.z - r.x * sintheta) * p.z; 00051 00052 q.z += ((1 - costheta) * r.x * r.z - r.y * sintheta) * p.x; 00053 q.z += ((1 - costheta) * r.y * r.z + r.x * sintheta) * p.y; 00054 q.z += (costheta + (1 - costheta) * r.z * r.z) * p.z; 00055 00056 this.x = q.x; 00057 this.y = q.y; 00058 this.z = q.z; 00059 } 00060 00061 public static Matrix4f getRotationMatrix(double theta, Vector3f axis) { 00062 Matrix4f ret = new Matrix4f(); 00063 float costheta, sintheta; 00064 00065 Vector3f r = new Vector3f(axis); 00066 r.normalize(); 00067 00068 costheta = (float)Math.cos(theta); 00069 sintheta = (float)Math.sin(theta); 00070 00071 ret.m00 = (costheta + (1 - costheta) * r.x * r.x); 00072 ret.m10 = ((1 - costheta) * r.x * r.y - r.z * sintheta); 00073 ret.m20 = ((1 - costheta) * r.x * r.z + r.y * sintheta); 00074 ret.m30 = 0; 00075 00076 ret.m01 = ((1 - costheta) * r.x * r.y + r.z * sintheta); 00077 ret.m11 = (costheta + (1 - costheta) * r.y * r.y); 00078 ret.m21 = ((1 - costheta) * r.y * r.z - r.x * sintheta); 00079 ret.m31 = 0; 00080 00081 ret.m02 = ((1 - costheta) * r.x * r.z - r.y * sintheta); 00082 ret.m12 = ((1 - costheta) * r.y * r.z + r.x * sintheta); 00083 ret.m22 = (costheta + (1 - costheta) * r.z * r.z); 00084 ret.m32 = 0; 00085 00086 ret.m03 = 0; 00087 ret.m13 = 0; 00088 ret.m23 = 0; 00089 ret.m33 = 1; 00090 00091 return ret; 00092 } 00093 }