checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Vec.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3 import javax.media.opengl.glu.*;
4
5 /** vector in 3-space; immutable */
6 public final class Vec {
7     public final float x, y, z;
8     public Vec(double x, double y, double z) { this((float)x, (float)y, (float)z); }
9     public Vec(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
10     public Vec(Point p1, Point p2) { this(p2.x-p1.x, p2.y-p1.y, p2.z-p1.z); }
11     public Vec cross(Vec v) { return new Vec(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x); }
12     public Vec plus(Vec v) { return new Vec(x+v.x, y+v.y, z+v.z); }
13     public Point plus(Point p) { return p.plus(this); }
14     public Vec minus(Vec v) { return new Vec(x-v.x, y-v.y, z-v.z); }
15     public Vec norm() { return mag()==0 ? this : div(mag()); }
16     public float mag() { return (float)Math.sqrt(x*x+y*y+z*z); }
17     public float dot(Vec v) { return x*v.x + y*v.y + z*v.z; }
18     public Vec times(float mag) { return new Vec(x*mag, y*mag, z*mag); }
19     public Vec div(float mag) { return new Vec(x/mag, y/mag, z/mag); }
20     public String toString() { return "<"+x+","+y+","+z+">"; }
21     public void glNormal(GL gl) { gl.glNormal3f(x, y, z); }
22
23     /** fundamental error quadric for the plane with this normal passing through p */
24     public Matrix fundamentalQuadric(Point p) {
25         Vec n = this;
26         if (mag() != 1) n = norm();
27         float a = n.x;
28         float b = n.y;
29         float c = n.z;
30         float d = (-a * p.x) + (-b * p.y) + (-c * p.z);
31         return new Matrix(a*a, a*b, a*c, a*d,
32                           a*b, b*b, b*c, b*d,
33                           a*c, b*c, c*c, c*d,
34                           a*d, b*d, c*d, d*d);
35     }
36 }