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 List<float[]> createEdgeArrays(final TopoDS_Shape topoDS_Shape) { final List edgeArrays = new ArrayList<float[]>(); TopExp_Explorer explorer = new TopExp_Explorer(); HashSet<TopoDS_Edge> alreadyDone = new HashSet<TopoDS_Edge>(); Bnd_Box box = new Bnd_Box(); BRepBndLib.add(topoDS_Shape, box); double[] bbox = box.get(); // double[] bbox=computeBoundingBox(); double boundingBoxDeflection = 0.0005 * Math.max(Math.max(bbox[3] - bbox[0], bbox[4] - bbox[1]), bbox[5] - bbox[2]); for (explorer.init(topoDS_Shape, TopAbs_ShapeEnum.EDGE); explorer.more(); explorer.next()) { TopoDS_Shape s = explorer.current(); if (!(s instanceof TopoDS_Edge)) continue; // should not happen! TopoDS_Edge e = (TopoDS_Edge) s; if (!alreadyDone.add(e)) continue; double[] range = BRep_Tool.range(e); Geom_Curve gc = BRep_Tool.curve(e, range); float[] array; if (gc != null) { GeomAdaptor_Curve adaptator = new GeomAdaptor_Curve(gc); GCPnts_UniformDeflection deflector = new GCPnts_UniformDeflection(); deflector.initialize(adaptator, boundingBoxDeflection, range[0], range[1]); int npts = deflector.nbPoints(); // Allocate one additional point at each end = parametric value 0, 1 array = new float[(npts + 2) * 3]; int j = 0; double[] values = adaptator.value(range[0]); array[j++] = (float) values[0]; array[j++] = (float) values[1]; array[j++] = (float) values[2]; // All intermediary points for (int i = 0; i < npts; ++i) { values = adaptator.value(deflector.parameter(i + 1)); array[j++] = (float) values[0]; array[j++] = (float) values[1]; array[j++] = (float) values[2]; } // Add last point values = adaptator.value(range[1]); array[j++] = (float) values[0]; array[j++] = (float) values[1]; array[j++] = (float) values[2]; edgeArrays.add(array); } else { if (!BRep_Tool.degenerated(e)) { // So, there is no curve, and the edge is not degenerated? // => draw lines between the vertices and ignore curvature // best approximation we can do ArrayList<double[]> aa = new ArrayList<double[]>(); // store points here for (TopExp_Explorer explorer2 = new TopExp_Explorer(s, TopAbs_ShapeEnum.VERTEX); explorer2.more(); explorer2.next()) { TopoDS_Shape sv = explorer2.current(); if (!(sv instanceof TopoDS_Vertex)) continue; // should not happen! TopoDS_Vertex v = (TopoDS_Vertex) sv; aa.add(BRep_Tool.pnt(v)); } array = new float[aa.size() * 3]; for (int i = 0, j = 0; i < aa.size(); i++) { double[] f = aa.get(i); array[j++] = (float) f[0]; array[j++] = (float) f[1]; array[j++] = (float) f[2]; } edgeArrays.add(array); } } } return edgeArrays; }