checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Triangle.java
index 24c90d0..18c73a0 100644 (file)
@@ -1,8 +1,52 @@
 package edu.berkeley.qfat.geom;
 import javax.media.opengl.*;
 
-public abstract class Triangle {
+/**
+ *  an oriented triangle, defined by three points in clockwise order.
+ */
+public abstract class Triangle implements HasBoundingBox {
     public abstract Point p1();
     public abstract Point p2();
     public abstract Point p3();
+
+    /** the face normal vector */
+    public Vec norm() {
+        return p2().minus(p1()).cross(p3().minus(p1())).norm();
+    }
+
+    /** the area of the triangle */
+    public float area() {
+        return
+            (float)Math.abs(0.5*p1().distance(p2())
+                            * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
+    }
+
+    /** issue gl.glVertex() for each of the triangle's points */
+    public void glVertices(GL gl) {
+        p1().glVertex(gl);
+        p2().glVertex(gl);
+        p3().glVertex(gl);
+    }
+
+    /** the triangle's centroid */
+    public Point centroid() {
+        return new Point((p1().x+p2().x+p3().x)/3,
+                         (p1().y+p2().y+p3().y)/3, 
+                         (p1().z+p2().z+p3().z)/3);
+    }
+
+    /** ratio of the area of the triangle to that of the square formed from its longest edge */
+    public float aspect() {
+        float max = Math.max(Math.max(p1().distance(p2()),
+                                      p2().distance(p3())),
+                             p3().distance(p1())) / 2;
+        return 1/(1+area()/(max*max));
+    }
+
+    public float getMaxX() { return Math.max(p1().x, Math.max(p2().x, p3().x)); }
+    public float getMinX() { return Math.min(p1().x, Math.min(p2().x, p3().x)); }
+    public float getMaxY() { return Math.max(p1().y, Math.max(p2().y, p3().y)); }
+    public float getMinY() { return Math.min(p1().y, Math.min(p2().y, p3().y)); }
+    public float getMaxZ() { return Math.max(p1().z, Math.max(p2().z, p3().z)); }
+    public float getMinZ() { return Math.min(p1().z, Math.min(p2().z, p3().z)); }
 }
\ No newline at end of file