X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.opencascade%2Fsrc%2Forg%2Fsimantics%2Fopencascade%2FOCCTTool.java;h=c8b65a9e9de3e89fa085aea2981ecac1c4e0aae1;hb=53d55c24c779745f188bdb18d32f71d20acb61b2;hp=6eac6724457e40c4a14a262c54f76dc9689967ec;hpb=f36217aeeb09c0c46f99886ee99772156ce9cfe6;p=simantics%2F3d.git diff --git a/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java b/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java index 6eac6724..c8b65a9e 100644 --- a/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java +++ b/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java @@ -30,69 +30,69 @@ import org.jcae.opencascade.jni.TopoDS_Face; import org.jcae.opencascade.jni.TopoDS_Shape; public class OCCTTool { -public static double getBoundingBoxDiagonal(TopoDS_Shape shape) { - - double []min = new double[3]; - double []max = new double[3]; - double []mmm = new double[3]; - double []bb = new double[6]; - - // Compute bounding box: - //---------------------- - Bnd_Box boundingBox = new Bnd_Box(); - BRepBndLib.add(shape, boundingBox); - boundingBox.get(bb); - boundingBox.delete(); - - min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; - max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; - - //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); - - // The length of the space diagonal of cuboid - for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; - double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]); - - double t0 = mmm[0]*mmm[0]; - double t1 = mmm[1]*mmm[1]; - double t2 = mmm[2]*mmm[2]; - - double tol = 1.0e-6 * length; - - if((t0 < tol) || (t1 < tol) || (t2 < tol)) { - System.out.println("Shape seems to be 2D. Unable to proceed. Aborting."); - return 0; - } - return length; - } - - public static double getBoundingBoxVolume(TopoDS_Shape shape) { - - double []min = new double[3]; - double []max = new double[3]; - double []mmm = new double[3]; - double []bb = new double[6]; - - // Compute bounding box: - //---------------------- - Bnd_Box boundingBox = new Bnd_Box(); - BRepBndLib.add(shape, boundingBox); - boundingBox.get(bb); - boundingBox.delete(); - - min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; - max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; - - //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); - - for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; - double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]); - - return vol; - } - - private static GProp_GProps getGProp(TopoDS_Shape shape) { - GProp_GProps GSystem = null; + public static double getBoundingBoxDiagonal(TopoDS_Shape shape) { + + double []min = new double[3]; + double []max = new double[3]; + double []mmm = new double[3]; + double []bb = new double[6]; + + // Compute bounding box: + //---------------------- + Bnd_Box boundingBox = new Bnd_Box(); + BRepBndLib.add(shape, boundingBox); + boundingBox.get(bb); + boundingBox.delete(); + + min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; + max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; + + //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); + + // The length of the space diagonal of cuboid + for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; + double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]); + + double t0 = mmm[0]*mmm[0]; + double t1 = mmm[1]*mmm[1]; + double t2 = mmm[2]*mmm[2]; + + double tol = 1.0e-6 * length; + + if((t0 < tol) || (t1 < tol) || (t2 < tol)) { + System.out.println("Shape seems to be 2D. Unable to proceed. Aborting."); + return 0; + } + return length; + } + + public static double getBoundingBoxVolume(TopoDS_Shape shape) { + + double []min = new double[3]; + double []max = new double[3]; + double []mmm = new double[3]; + double []bb = new double[6]; + + // Compute bounding box: + //---------------------- + Bnd_Box boundingBox = new Bnd_Box(); + BRepBndLib.add(shape, boundingBox); + boundingBox.get(bb); + boundingBox.delete(); + + min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2]; + max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5]; + + //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]"); + + for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i]; + double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]); + + return vol; + } + + private static GProp_GProps getGProp(TopoDS_Shape shape) { + GProp_GProps GSystem = null; int type = 0; if (type == 0) { GSystem = new GProp_GProps(); @@ -105,113 +105,113 @@ public static double getBoundingBoxDiagonal(TopoDS_Shape shape) { BRepGProp.volumePropertiesGK(shape, GSystem, 0.001); } return GSystem; - } - + } + public static double getMass( TopoDS_Shape shape) { - + GProp_GProps GSystem = getGProp(shape); - - + + double mass = GSystem.mass(); //System.out.println("Mass " + mass); GSystem.delete(); return mass; } - + public static double[] getInertia(TopoDS_Shape shape) { GProp_GProps GSystem = getGProp(shape); - + double inertia[] = GSystem.matrixOfInertia(); GSystem.delete(); return inertia; } - + public static double[] getCentreOfMass(TopoDS_Shape shape) { GProp_GProps GSystem = getGProp(shape); - + double cm[] = GSystem.centreOfMass(); GSystem.delete(); return cm; } - + public static boolean appendToMesh (TopoDS_Face face, List meshPoints, List meshTriangles) - { - TopLoc_Location Location = new TopLoc_Location(); - - Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); - - if(triangulation == null) { - Location.delete(); - System.out.println("Encountered empty triangulation after face"); - return false; - } - - boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED; - - int lastPoint = meshPoints.size() / 3; - - int[]triangles = triangulation.triangles(); - double[]nodes = triangulation.nodes(); - - int nofTriangles = triangulation.nbTriangles(); - int nofNodes = triangulation.nbNodes(); - - - triangulation.delete(); - - if(nofTriangles < 1) { - System.out.println("No triangles for mesh on face"); - Location.delete(); - return false; - } - - if(nofNodes < 1) { - System.out.println("No nodes for mesh on face:"); - Location.delete(); - return false; - } - - for(int i = 0; i < nofTriangles; i++) - { - int n0, n1, n2; - if (!reverse) { - n0 = triangles[3 * i]; - n1 = triangles[3 * i + 1]; - n2 = triangles[3 * i + 2]; - } else { - n0 = triangles[3 * i + 2]; - n1 = triangles[3 * i + 1]; - n2 = triangles[3 * i]; - } - - meshTriangles.add(n0 + lastPoint); - meshTriangles.add(n1 + lastPoint); - meshTriangles.add(n2 + lastPoint); - } - - - GP_Trsf transformation = Location.transformation(); - Location.delete(); - - double d_mat[] = new double[16]; - - transformation.getValues(d_mat); - Matrix4d mat = new Matrix4d(d_mat); - - - for(int i = 0; i < nofNodes; i++) { - - Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); - mat.transform(p); - - meshPoints.add(p.x); - meshPoints.add(p.y); - meshPoints.add(p.z); - - } - - transformation.delete(); - - return true; - } + { + TopLoc_Location Location = new TopLoc_Location(); + + Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location); + + if(triangulation == null) { + Location.delete(); + System.out.println("Encountered empty triangulation after face"); + return false; + } + + boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED; + + int lastPoint = meshPoints.size() / 3; + + int[]triangles = triangulation.triangles(); + double[]nodes = triangulation.nodes(); + + int nofTriangles = triangulation.nbTriangles(); + int nofNodes = triangulation.nbNodes(); + + + triangulation.delete(); + + if(nofTriangles < 1) { + System.out.println("No triangles for mesh on face"); + Location.delete(); + return false; + } + + if(nofNodes < 1) { + System.out.println("No nodes for mesh on face:"); + Location.delete(); + return false; + } + + for(int i = 0; i < nofTriangles; i++) + { + int n0, n1, n2; + if (!reverse) { + n0 = triangles[3 * i]; + n1 = triangles[3 * i + 1]; + n2 = triangles[3 * i + 2]; + } else { + n0 = triangles[3 * i + 2]; + n1 = triangles[3 * i + 1]; + n2 = triangles[3 * i]; + } + + meshTriangles.add(n0 + lastPoint); + meshTriangles.add(n1 + lastPoint); + meshTriangles.add(n2 + lastPoint); + } + + + GP_Trsf transformation = Location.transformation(); + Location.delete(); + + double d_mat[] = new double[16]; + + transformation.getValues(d_mat); + Matrix4d mat = new Matrix4d(d_mat); + + + for(int i = 0; i < nofNodes; i++) { + + Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]); + mat.transform(p); + + meshPoints.add(p.x); + meshPoints.add(p.y); + meshPoints.add(p.z); + + } + + transformation.delete(); + + return true; + } }