1
votes

I have two polygons and want to turn them into a triangle mesh, using java3d. But whatever I try results in some kind of error. What am I missing?

Here is some code I've tried:

final int n = points.length;
final int m = opoints.length;
final GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
final Point3d[] npoints = Arrays.copyOf(points, n + m);
System.arraycopy(opoints, 0, npoints, n, m);
gi.setCoordinates(npoints);
gi.setStripCounts(new int[] { n, m });

gi.convertToIndexedTriangles();
final IndexedTriangleArray it = (IndexedTriangleArray) gi.getIndexedGeometryArray();

final Point3d[] newPoints = new Point3d[it.getVertexCount()];

it.getCoordinates(0, newPoints);
//      Exception in thread "main" java.lang.NullPointerException
//      at javax.media.j3d.GeometryArrayRetained.getCoordinates(GeometryArrayRetained.java:5425)
//      at javax.media.j3d.GeometryArray.getCoordinates(GeometryArray.java:3699)

final int[] nidxs = new int[it.getValidIndexCount()];
it.getCoordinateIndices(0, nidxs);
1

1 Answers

0
votes

I advise you to use the debug mode first to know exactly how the GeometryInfo mutates after the call of convertToIndexedTriangles.

As Java 3D 1.6.0 is open source, look at its source code: https://github.com/hharrison/java3d-utils/blob/master/src/classes/share/com/sun/j3d/utils/geometry/GeometryInfo.java#L507 https://github.com/hharrison/java3d-utils/blob/master/src/classes/share/com/sun/j3d/utils/geometry/Triangulator.java#L625

You can call getCoordinateIndices() and getCoordinates() directly on the GeometryInfo object as far as I know.

I cannot guarantee that my suggestion works with an obsolete version of Java 3D.