public static List<FaceMesh> createFaceMeshes(final TopoDS_Shape topoDS_Shape) { int meshIter = 3; TopExp_Explorer explorer = new TopExp_Explorer(); TopLoc_Location loc = new TopLoc_Location(); List<FaceMesh> faceMeshes = new ArrayList<FaceMesh>(); for (explorer.init(topoDS_Shape, TopAbs_ShapeEnum.FACE); explorer.more(); explorer.next()) { TopoDS_Shape s = explorer.current(); if (!(s instanceof TopoDS_Face)) continue; // should not happen! TopoDS_Face face = (TopoDS_Face) s; Poly_Triangulation pt = BRep_Tool.triangulation(face, loc); float error = 0.001f * getMaxBound(s) * 4; // float error=0.0001f; int iter = 0; while ((pt == null) & (iter < meshIter)) { new BRepMesh_IncrementalMesh(face, error, false); // new BRepMesh_IncrementalMesh(face,error, true); pt = BRep_Tool.triangulation(face, loc); error /= 10; iter++; } if (pt == null) { System.err.println( "Triangulation failed for face " + face + ". Trying other mesh parameters."); faceMeshes.add(new FaceMesh(new float[0], new int[0])); continue; } double[] dnodes = pt.nodes(); final int[] itriangles = pt.triangles(); if (face.orientation() == TopAbs_Orientation.REVERSED) { reverseMesh(itriangles); } final float[] fnodes = new float[dnodes.length]; if (loc.isIdentity()) { for (int i = 0; i < dnodes.length; i++) { fnodes[i] = (float) dnodes[i]; } } else transformMesh(loc, dnodes, fnodes); faceMeshes.add(new FaceMesh(fnodes, itriangles)); } return faceMeshes; }
public static void transformMesh(final TopLoc_Location loc, double[] src, float[] dst) { double[] matrix = new double[16]; loc.transformation().getValues(matrix); Matrix4d m4d = new Matrix4d(matrix); Point3d p3d = new Point3d(); for (int i = 0; i < src.length; i += 3) { p3d.x = src[i + 0]; p3d.y = src[i + 1]; p3d.z = src[i + 2]; m4d.transform(p3d); dst[i + 0] = (float) p3d.x; dst[i + 1] = (float) p3d.y; dst[i + 2] = (float) p3d.z; } }