1 package edu.berkeley.qfat.geom;
2 import javax.media.opengl.*;
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.
8 public abstract class Triangle implements HasBoundingBox {
9 public abstract Point p1();
10 public abstract Point p2();
11 public abstract Point p3();
13 /** the face normal vector */
15 return p2().minus(p1()).cross(p3().minus(p1())).norm();
18 /** the area of the triangle */
21 (float)Math.abs(0.5*p1().distance(p2())
22 * new Vec(p1(), p2()).norm().dot(new Vec(p2(), p3())));
25 /** issue gl.glVertex() for each of the triangle's points */
26 public void glVertices(GL gl) {
33 /** the triangle's centroid */
34 public Point centroid() {
35 return new Point((p1().x+p2().x+p3().x)/3,
36 (p1().y+p2().y+p3().y)/3,
37 (p1().z+p2().z+p3().z)/3);
40 /** ratio of the area of the triangle to that of the square formed from its longest edge */
41 public float aspect() {
42 float max = Math.max(Math.max(p1().distance(p2()),
44 p3().distance(p1())) / 2;
45 return 1/(1+area()/(max*max));
48 /** decide if the segment from p1-p2 intersects this triangle */
49 public boolean intersects(Point p1, Point p2) {
50 double A0=p1().x, A1=p1().y, A2=p1().z;
51 double B0=p2().x, B1=p2().y, B2=p2().z;
52 double C0=p3().x, C1=p3().y, C2=p3().z;
53 double j0=p1.x, j1=p1.y, j2=p1.z;
54 double k0=p2.x, k1=p2.y, k2=p2.z;
62 double R00, R01, R02, R03,
77 c0 = a1 * b2 - a2 * b1;
78 c1 = a2 * b0 - a0 * b2;
79 c2 = a0 * b1 - a1 * b0;
81 /* M^(-1) = (1/det(M)) * adj(M) */
82 in_det = 1 / (c0 * c0 + c1 * c1 + c2 * c2);
83 R00 = (b1 * c2 - b2 * c1) * in_det;
84 R01 = (b2 * c0 - b0 * c2) * in_det;
85 R02 = (b0 * c1 - b1 * c0) * in_det;
86 R10 = (c1 * a2 - c2 * a1) * in_det;
87 R11 = (c2 * a0 - c0 * a2) * in_det;
88 R12 = (c0 * a1 - c1 * a0) * in_det;
94 R03 = -(R00 * A0 + R01 * A1 + R02 * A2);
95 R13 = -(R10 * A0 + R11 * A1 + R12 * A2);
96 R23 = -(R20 * A0 + R21 * A1 + R22 * A2);
98 /* fill in last row of 4x4 matrix */
102 J2 = R20 * j0 + R21 * j1 + R22 * j2 + R23;
103 K2 = R20 * k0 + R21 * k1 + R22 * k2 + R23;
104 if (J2 * K2 >= 0) return false;
106 J0 = R00 * j0 + R01 * j1 + R02 * j2 + R03;
107 K0 = R00 * k0 + R01 * k1 + R02 * k2 + R03;
108 i0 = J0 + J2 * ((K0 - J0) / (J2 - K2));
109 if (i0 < 0 || i0 > 1) return false;
111 J1 = R10 * j0 + R11 * j1 + R12 * j2 + R13;
112 K1 = R10 * k0 + R11 * k1 + R12 * k2 + R13;
113 i1 = J1 + J2 * ((K1 - J1) / (J2 - K2));
114 if (i1 < 0 || i1 > 1 || i0 + i1 > 1) return false;
119 public float getMaxX() { return Math.max(p1().x, Math.max(p2().x, p3().x)); }
120 public float getMinX() { return Math.min(p1().x, Math.min(p2().x, p3().x)); }
121 public float getMaxY() { return Math.max(p1().y, Math.max(p2().y, p3().y)); }
122 public float getMinY() { return Math.min(p1().y, Math.min(p2().y, p3().y)); }
123 public float getMaxZ() { return Math.max(p1().z, Math.max(p2().z, p3().z)); }
124 public float getMinZ() { return Math.min(p1().z, Math.min(p2().z, p3().z)); }