Created
January 15, 2012 20:46
-
-
Save hfaulds/1617148 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package geometry; | |
| import java.util.Vector; | |
| import javax.media.opengl.GL2; | |
| import octree.collisions.Collidable; | |
| import octree.collisions.CollisionDetails; | |
| import octree.collisions.CollisionFrustrum; | |
| import octree.collisions.NullCollision; | |
| import rendering.GLRenderable; | |
| public class Vertex implements Cloneable, Collidable, GLRenderable { | |
| public Colour colour = new Colour(1,1,1,1); | |
| public int renderSize = 5; | |
| public double x,y,z; | |
| private double[] normal = new double[3]; | |
| public Vertex(double x, double y, double z) { | |
| this.x = x; | |
| this.y = y; | |
| this.z = z; | |
| normal[0] = normal[1] = normal[2] = 0; | |
| } | |
| public double[] toDouble() { | |
| return new double[]{x,y,z}; | |
| } | |
| public float[] toFloat() { | |
| return new float[]{(float) x,(float) y,(float) z}; | |
| } | |
| public double[] getNormal() { | |
| return normal; | |
| } | |
| public double getNormalX() { | |
| return normal[0]; | |
| } | |
| public double getNormalY() { | |
| return normal[1]; | |
| } | |
| public double getNormalZ() { | |
| return normal[2]; | |
| } | |
| public void setNormal(double nx, double ny, double nz) { | |
| normal[0] = nx; | |
| normal[1] = ny; | |
| normal[2] = nz; | |
| } | |
| public void addNormal(double anx, double any, double anz) { | |
| normal[0] += anx; | |
| normal[1] += any; | |
| normal[2] += anz; | |
| } | |
| public void normaliseNormal() { | |
| double mag = Math.sqrt(normal[0] * normal[0] + normal[1] * normal[1] | |
| + normal[2] * normal[2]); | |
| if (mag != 0.0) | |
| for (int i = 0; i < 3; i++) | |
| normal[i] /= mag; | |
| } | |
| public Object clone() { | |
| Vertex copy = new Vertex(x,y,z); | |
| for (int i = 0; i < 3; i++) { | |
| copy.normal[i] = normal[i]; | |
| } | |
| return copy; | |
| } | |
| public String toString() { | |
| return "[" + this.x + ", " + this.y + ", " + this.z + "]"; | |
| } | |
| public double distanceTo(Vertex vert) { | |
| double x = Math.pow(vert.x - this.x, 2); | |
| double y = Math.pow(vert.y - this.y, 2); | |
| double z = Math.pow(vert.z - this.z, 2); | |
| return Math.sqrt(x + y + z); | |
| } | |
| public Vertex add(Vertex vert) { | |
| double x = this.x + vert.x; | |
| double y = this.y + vert.y; | |
| double z = this.z + vert.z; | |
| return new Vertex(x, y, z); | |
| } | |
| public Vertex subtract(Vertex vert) { | |
| double x = this.x - vert.x; | |
| double y = this.y - vert.y; | |
| double z = this.z - vert.z; | |
| return new Vertex(x, y, z); | |
| } | |
| public Vertex crossProduct(Vertex vert) { | |
| double x = this.y * vert.z - this.z * vert.y; | |
| double y = this.z * vert.x - this.x * vert.z; | |
| double z = this.x * vert.y - this.y * vert.x; | |
| return new Vertex(x, y, z); | |
| } | |
| public double dotProduct(Vertex vert) { | |
| return (this.x * vert.x) + (this.y * vert.y) + (this.z * vert.z); | |
| } | |
| public double magnitude() { | |
| double a = Math.pow(x, 2); | |
| double b = Math.pow(y, 2); | |
| double c = Math.pow(z, 2); | |
| return Math.sqrt(a + b + c); | |
| } | |
| public CollisionDetails collide(Line line, double accuracy) { | |
| double distance = line.distanceTo(this); | |
| if (distance < accuracy) | |
| return new CollisionDetails(this, distance); | |
| else | |
| return new NullCollision(); | |
| } | |
| @Override | |
| public void render(GL2 gl) { | |
| gl.glPushMatrix(); | |
| gl.glPointSize(renderSize); | |
| gl.glColor3d(colour.r, colour.g, colour.b); | |
| gl.glBegin(GL2.GL_POINTS); | |
| gl.glVertex3d(x, y, z); | |
| gl.glEnd(); | |
| gl.glPopMatrix(); | |
| } | |
| @Override | |
| public Vector<CollisionDetails> collide(CollisionFrustrum box, double accuracy) { | |
| Vector<CollisionDetails> collisions = new Vector<CollisionDetails>(); | |
| if(box.contains(this, accuracy)) | |
| collisions.add(new CollisionDetails(this, 0)); | |
| return collisions; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment