for (int fi = 0, nf = faces.size(); fi < nf; fi++) {
Face f = faces.get(fi);
if (f.texIndices != null) {
for (int tri = 0; tri < f.decomp.length; tri += 3) {
Point2 a = T.times(texCoords.get(f.texIndices[f.decomp[tri]]));
Point2 b = T.times(texCoords.get(f.texIndices[f.decomp[tri + 1]]));
Point2 c = T.times(texCoords.get(f.texIndices[f.decomp[tri + 2]]));
builder.reset();
builder.add(a);
builder.add(b);
builder.add(c);
Box2 bound = builder.getBoundingBox();
int i0 = MathUtil.clamp((int) Math.floor(bound.minimumX() * (double) triangleLookupGridSize), 0, triangleLookupGridSize - 1);
int i1 = MathUtil.clamp((int) Math.floor(bound.maximumX() * (double) triangleLookupGridSize), 0, triangleLookupGridSize - 1);
int j0 = MathUtil.clamp((int) Math.floor(bound.minimumY() * (double) triangleLookupGridSize), 0, triangleLookupGridSize - 1);
int j1 = MathUtil.clamp((int) Math.floor(bound.maximumY() * (double) triangleLookupGridSize), 0, triangleLookupGridSize - 1);
for (int i = i0; i <= i1; i++) {
double x0 = (double) i / (double) triangleLookupGridSize;
double x1 = (double) (i + 1) / (double) triangleLookupGridSize;
for (int j = j0; j <= j1; j++) {
double y0 = (double) j / (double) triangleLookupGridSize;
double y1 = (double) (j + 1) / (double) triangleLookupGridSize;
Box2 cell = new Box2(x0, y0, x1, y1);
if (GeometryUtil.boxIntersectsTriangle(cell, a, b, c)) {
int cellIndex = j * triangleLookupGridSize + i;
int triIndex = fi * maximumTrianglesPerFace + tri / 3;
IntegerArray list = triangleLookup.get(cellIndex);
if (list == null) {
list = new IntegerArray();
triangleLookup.put(cellIndex, list);
}
list.add(triIndex);
}
}
}
}
}
}
int count = 0;
for (IntegerArray list : triangleLookup.values()) {
int n = list.size();
for (int i = 1; i < n; i++) {
int tri1 = list.get(i);
int fi1 = tri1 / maximumTrianglesPerFace;
int ti1 = (tri1 % maximumTrianglesPerFace) * 3;
Face f1 = faces.get(fi1);
Point2 a1 = texCoords.get(f1.texIndices[f1.decomp[ti1]]);
Point2 b1 = texCoords.get(f1.texIndices[f1.decomp[ti1 + 1]]);
Point2 c1 = texCoords.get(f1.texIndices[f1.decomp[ti1 + 2]]);
for (int j = 0; j < i; j++) {
int tri2 = list.get(j);
int fi2 = tri2 / maximumTrianglesPerFace;
int ti2 = (tri2 % maximumTrianglesPerFace) * 3;
Face f2 = faces.get(fi2);
Point2 a2 = texCoords.get(f2.texIndices[f2.decomp[ti2]]);
Point2 b2 = texCoords.get(f2.texIndices[f2.decomp[ti2 + 1]]);
Point2 c2 = texCoords.get(f2.texIndices[f2.decomp[ti2 + 2]]);
if (GeometryUtil.triangleIntersectsTriangle(a1, b1, c1, a2, b2, c2)) {
System.err.println("WARNING: Triangles intersect -------------------------------");
System.err.printf("% 5d: (%5.3f, %5.3f) - (%5.3f, %5.3f) - (%5.3f, %5.3f)", tri1,
a1.x(), a1.y(),
b1.x(), b1.y(),
c1.x(), c1.y());
System.err.println();
System.err.printf("% 5d: (%5.3f, %5.3f) - (%5.3f, %5.3f) - (%5.3f, %5.3f)", tri2,
a2.x(), a2.y(),
b2.x(), b2.y(),
c2.x(), c2.y());
System.err.println();
count++;
}
}
}