|
public class cVector implements java.io.Serializable
|
{
|
static final long serialVersionUID = 218039003662855348L;
|
|
public cVector()
|
{
|
}
|
|
public cVector(double ix, double iy, double iz)
|
{
|
x = ix;
|
y = iy;
|
z = iz;
|
}
|
|
public cVector(cVector v)
|
{
|
set(v);
|
}
|
|
static final public cVector X = new cVector(1, 0, 0);
|
static final public cVector Y = new cVector(0, 1, 0);
|
static final public cVector Z = new cVector(0, 0, 1);
|
|
double x, y, z;
|
|
double get(int i)
|
{
|
switch (i)
|
{
|
case 0:
|
return x;
|
case 1:
|
return y;
|
case 2:
|
return z;
|
}
|
|
System.err.println("ARRAY OUT OF BOUND");
|
new Exception().printStackTrace();
|
return 0;
|
}
|
|
void set(int i, double value)
|
{
|
switch (i)
|
{
|
case 0:
|
x = value;
|
break;
|
case 1:
|
y = value;
|
break;
|
case 2:
|
z = value;
|
break;
|
default:
|
{
|
System.err.println("ARRAY OUT OF BOUND");
|
new Exception().printStackTrace();
|
}
|
}
|
}
|
|
cVector ToFloat()
|
{
|
x = (float)x;
|
y = (float)y;
|
z = (float)z;
|
|
return this;
|
}
|
|
void set(cVector v)
|
{
|
x = v.x;
|
y = v.y;
|
z = v.z;
|
}
|
|
void set(cVector v, double scale)
|
{
|
x = v.x * scale;
|
y = v.y * scale;
|
z = v.z * scale;
|
}
|
|
void set(double ix, double iy, double iz)
|
{
|
x = ix;
|
y = iy;
|
z = iz;
|
}
|
|
void mul(double f)
|
{
|
x *= f;
|
y *= f;
|
z *= f;
|
}
|
|
void mulEq(int i, double value)
|
{
|
switch (i)
|
{
|
case 0:
|
x *= value;
|
break;
|
case 1:
|
y *= value;
|
break;
|
case 2:
|
z *= value;
|
break;
|
default:
|
{
|
System.err.println("ARRAY OUT OF BOUND");
|
new Exception().printStackTrace();
|
}
|
}
|
}
|
|
void mulEq(double value)
|
{
|
x *= value;
|
y *= value;
|
z *= value;
|
}
|
|
void addEq(int i, double value)
|
{
|
switch (i)
|
{
|
case 0:
|
x += value;
|
break;
|
case 1:
|
y += value;
|
break;
|
case 2:
|
z += value;
|
break;
|
default:
|
{
|
System.err.println("ARRAY OUT OF BOUND");
|
new Exception().printStackTrace();
|
}
|
}
|
|
}
|
|
void addEq(double value)
|
{
|
x += value;
|
y += value;
|
z += value;
|
}
|
|
double distance(cVector v)
|
{
|
double dx = x - v.x;
|
double dy = y - v.y;
|
double dz = z - v.z;
|
|
return Math.sqrt(dx*dx + dy*dy + dz*dz);
|
}
|
|
double length()
|
{
|
double l2 = x * x + y * y + z * z;
|
|
if (l2 == 1)
|
{
|
return 1;
|
}
|
|
return Math.sqrt(l2);
|
}
|
|
double length2()
|
{
|
return x * x + y * y + z * z;
|
}
|
|
boolean normalize()
|
{
|
double l = length();
|
|
if (l == 0)
|
{
|
return false;
|
}
|
|
if (l == 1) // maybe rare...
|
return true;
|
|
x /= l;
|
y /= l;
|
z /= l;
|
|
return true;
|
}
|
|
boolean normalized()
|
{
|
if (true)
|
return true;
|
|
double l = length2();
|
|
return Math.abs(l - 1) < 1E-6;
|
}
|
|
double dot(cVector v)
|
{
|
return (double) x * v.x + y * v.y + z * v.z;
|
}
|
|
void sub(cVector v)
|
{
|
x -= v.x;
|
y -= v.y;
|
z -= v.z;
|
}
|
|
void add(cVector v)
|
{
|
x += v.x;
|
y += v.y;
|
z += v.z;
|
}
|
|
void add(cVector v, double scale)
|
{
|
x += v.x * scale;
|
y += v.y * scale;
|
z += v.z * scale;
|
}
|
|
void cross(cVector u, cVector v)
|
{
|
double tx = u.y * v.z - u.z * v.y;
|
double ty = u.z * v.x - u.x * v.z;
|
double tz = u.x * v.y - u.y * v.x;
|
|
x = tx; y = ty; z = tz;
|
}
|
|
public String toString()
|
{
|
double x0 = (int)(x*1000000) / 1000000.0;
|
double y0 = (int)(y*1000000) / 1000000.0;
|
double z0 = (int)(z*1000000) / 1000000.0;
|
|
return "[" + x0 + ", " + y0 + ", " + z0 + "]";
|
}
|
|
public boolean equals(Object o)
|
{
|
//if (true) return false;
|
|
cVector vert = (cVector) o;
|
|
double tolerance = 0.00001;
|
|
if (!Grafreed.epsequal)
|
tolerance = 0;
|
|
return Math.abs(x - vert.x) +
|
Math.abs(y - vert.y) +
|
Math.abs(z - vert.z) <= tolerance;
|
}
|
|
public int hashCode()
|
{
|
long lx = Double.doubleToRawLongBits(x);
|
long ly = Double.doubleToRawLongBits(y);
|
long lz = Double.doubleToRawLongBits(z);
|
|
if (Grafreed.epsequal)
|
{
|
return 0;
|
} else
|
{
|
return (int) (lx + (lx >> 32) + ly + (ly >> 32) + lz + (lz >> 32));
|
}
|
}
|
}
|