checkpoint
[anneal.git] / src / edu / berkeley / qfat / geom / Triangle.java
1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
3
4 /**
5  *  An oriented triangle, defined by three points in clockwise order;
6  *  note that the Point objects returned by p1/p2/p3 may vary over time.
7  */
8 public abstract class Triangle implements HasBoundingBox {
9     public abstract Point p1();
10     public abstract Point p2();
11     public abstract Point p3();
12
13     /** the face normal vector */
14     public Vec norm() {
15         return p2().minus(p1()).cross(p3().minus(p1())).norm();
16     }
17
18     /** the area of the triangle */
19     public float area() {
20         return
21             (float)Math.abs(0.5*p1().distance(p2())
22                             * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
23     }
24
25     /** issue gl.glVertex() for each of the triangle's points */
26     public void glVertices(GL gl, Matrix m) {
27         if (m==null) {
28             norm().glNormal(gl);
29             p1().glVertex(gl);
30             p2().glVertex(gl);
31             p3().glVertex(gl);
32         } else {
33             m.times(norm()).glNormal(gl);
34             m.times(p1()).glVertex(gl);
35             m.times(p2()).glVertex(gl);
36             m.times(p3()).glVertex(gl);
37         }
38     }
39
40     /** the triangle's centroid */
41     public Point centroid() {
42         return new Point((p1().x+p2().x+p3().x)/3,
43                          (p1().y+p2().y+p3().y)/3, 
44                          (p1().z+p2().z+p3().z)/3);
45     }
46
47     /** ratio of the area of the triangle to that of the square formed from its longest edge */
48     /*
49     public float aspect() {
50         float max = Math.max(Math.max(p1().distance(p2()),
51                                       p2().distance(p3())),
52                              p3().distance(p1())) / 2;
53         return 1/(1+area()/(max*max));
54     }
55     */
56
57     public float circumcircleRadius() {
58         double a = p1().distance(p2());
59         double b = p2().distance(p3());
60         double c = p3().distance(p1());
61         return (float)((a*b*c)/Math.sqrt((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)));
62     }
63
64     public float shortestEdgeLength() {
65         float a = p1().distance(p2());
66         float b = p2().distance(p3());
67         float c = p3().distance(p1());
68         return Math.min(a, Math.min(b,c));
69     }
70
71     /** a number ranging from 0..1 with 0 being lower quality */
72     public float quality() {
73         float d = shortestEdgeLength();
74         float r = circumcircleRadius();
75         if (r==0) throw new Error();
76         float ret = (float)((d*Math.cos(Math.PI/6))/(r*2));
77         if (ret < 0 || ret > 1) throw new Error("ret="+ret);
78         return ret;
79     }
80
81     // FIXME: I stole this off the net, and I need to credit whoever wrote it
82     /** decide if the segment from p1-p2 intersects this triangle */
83     public boolean intersects(Point p1, Point p2) {
84         double A0=p1().x, A1=p1().y, A2=p1().z;
85         double B0=p2().x, B1=p2().y, B2=p2().z;
86         double C0=p3().x, C1=p3().y, C2=p3().z;
87         double j0=p1.x, j1=p1.y, j2=p1.z;
88         double k0=p2.x, k1=p2.y, k2=p2.z;
89         double J0, J1, J2;
90         double K0, K1, K2;
91         double i0, i1, i2;
92         double a0, a1, a2;
93         double b0, b1, b2;
94         double c0, c1, c2;
95         double in_det;
96         double R00, R01, R02, R03,
97             R10, R11, R12, R13,
98             R20, R21, R22, R23,
99             R30, R31, R32, R33;
100
101
102         /* a = B - A */
103         a0 = B0 - A0; 
104         a1 = B1 - A1; 
105         a2 = B2 - A2;
106         /* b = C - B */
107         b0 = C0 - A0;
108         b1 = C1 - A1;
109         b2 = C2 - A2;
110         /* c = a &times; b */
111         c0 = a1 * b2 - a2 * b1;
112         c1 = a2 * b0 - a0 * b2;
113         c2 = a0 * b1 - a1 * b0;
114  
115         /* M^(-1) = (1/det(M)) * adj(M) */
116         in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
117         R00 = (b1 * c2 - b2 * c1) * in_det;
118         R01 = (b2 * c0 - b0 * c2) * in_det;
119         R02 = (b0 * c1 - b1 * c0) * in_det;
120         R10 = (c1 * a2 - c2 * a1) * in_det;
121         R11 = (c2 * a0 - c0 * a2) * in_det;
122         R12 = (c0 * a1 - c1 * a0) * in_det;
123         R20 = (c0) * in_det;
124         R21 = (c1) * in_det;
125         R22 = (c2) * in_det;
126   
127         /* O = M^(-1) * A */
128         R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
129         R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
130         R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
131  
132         /* fill in last row of 4x4 matrix */
133         R30 = R31 = R32 = 0;
134         R33 = 1;
135   
136         J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
137         K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
138         if (J2 * K2 >= 0) return false;
139
140         J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
141         K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
142         i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
143         if (i0 < 0 || i0 > 1) return false;
144   
145         J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
146         K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
147         i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
148         if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
149
150         return true;            
151     }
152
153     public float getMaxX() { return Math.max(p1().x, Math.max(p2().x, p3().x)); }
154     public float getMinX() { return Math.min(p1().x, Math.min(p2().x, p3().x)); }
155     public float getMaxY() { return Math.max(p1().y, Math.max(p2().y, p3().y)); }
156     public float getMinY() { return Math.min(p1().y, Math.min(p2().y, p3().y)); }
157     public float getMaxZ() { return Math.max(p1().z, Math.max(p2().z, p3().z)); }
158     public float getMinZ() { return Math.min(p1().z, Math.min(p2().z, p3().z)); }
159 }