Have a look for the geometry directory in xithtk, there is some code.
Please report to me if some code is missing or buggy.
This is the picking code:
Point3f point= new Point3f();
Vector3f vec = new Vector3f();
getCanvas3D().createPickRay(x, y, point, vec);
Ray ray = new Ray(point, vec);
Point3f p = null;
Triangle triangle = null;
for (Triangle t : getTriangles(shape)) {
Point3f temp = t.intersects(ray);
if (temp != null) {
p = temp;
triangle = t;
System.out.println("Coordinate of intersection=" + p);
break;
}
}
getTriangles() :
public Triangle[] getTriangles(Shape3D poShape3D) {
Triangle[] aoTriangle = null;
Vector3f oVector3f = new Vector3f();
poShape3D.getLocalToVworld().getTranslation(oVector3f);
Geometry oGeometry = poShape3D.getGeometry();
if (oGeometry instanceof GeomIndexedContainer) {
GeomIndexedContainer oGeomIndexedContainer = (GeomIndexedContainer)oGeometry;
int[] aiIndices = oGeomIndexedContainer.getIndex();
aoTriangle = new Triangle[aiIndices.length / 3];
for (int i = 0, j = 0; i < aiIndices.length; i++, j++) {
Point3f oPoint3f1 = new Point3f();
Point3f oPoint3f2 = new Point3f();
Point3f oPoint3f3 = new Point3f();
oGeometry.getVertex(aiIndices[i], oPoint3f1);
oGeometry.getVertex(aiIndices[i + 1], oPoint3f2);
oGeometry.getVertex(aiIndices[i + 2], oPoint3f3);
oPoint3f1.add(oVector3f);
oPoint3f2.add(oVector3f);
oPoint3f3.add(oVector3f);
aoTriangle[j] = new Triangle(oPoint3f1, oPoint3f2, oPoint3f3);
i += 2;
}
} else {
aoTriangle = new Triangle[oGeometry.getVertexCount() / 3];
for (int i = 0, j = 0; i < oGeometry.getVertexCount(); i++, j++) {
Point3f oPoint3f1 = new Point3f();
Point3f oPoint3f2 = new Point3f();
Point3f oPoint3f3 = new Point3f();
oGeometry.getVertex(i, oPoint3f1);
oGeometry.getVertex(i + 1, oPoint3f2);
oGeometry.getVertex(i + 2, oPoint3f3);
oPoint3f1.add(oVector3f);
oPoint3f2.add(oVector3f);
oPoint3f3.add(oVector3f);
aoTriangle[j] = new Triangle(oPoint3f1, oPoint3f2, oPoint3f3);
i += 2;
}
}
return aoTriangle;
}
I don’t know how to optimize it but please somebody try.
I don’t even know how to pick the whole universe, I only know how to check if a shape is being picked, but triangle and point of intersection is in for the moment ;).
Good luck and please optimize and add code.
Alonzo