]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java
3D framework (Simca 2012)
[simantics/3d.git] / org.simantics.opencascade / src / org / simantics / opencascade / OCCTTool.java
diff --git a/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java b/org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java
new file mode 100644 (file)
index 0000000..eba42a6
--- /dev/null
@@ -0,0 +1,206 @@
+package org.simantics.opencascade;\r
+\r
+import java.util.List;\r
+\r
+import javax.vecmath.Matrix4d;\r
+import javax.vecmath.Point3d;\r
+\r
+import org.jcae.opencascade.jni.BRepBndLib;\r
+import org.jcae.opencascade.jni.BRepGProp;\r
+import org.jcae.opencascade.jni.BRep_Tool;\r
+import org.jcae.opencascade.jni.Bnd_Box;\r
+import org.jcae.opencascade.jni.GP_Trsf;\r
+import org.jcae.opencascade.jni.GProp_GProps;\r
+import org.jcae.opencascade.jni.GProp_VelGProps;\r
+import org.jcae.opencascade.jni.Poly_Triangulation;\r
+import org.jcae.opencascade.jni.TopAbs_Orientation;\r
+import org.jcae.opencascade.jni.TopLoc_Location;\r
+import org.jcae.opencascade.jni.TopoDS_Face;\r
+import org.jcae.opencascade.jni.TopoDS_Shape;\r
+\r
+public class OCCTTool {\r
+public static double getBoundingBoxDiagonal(TopoDS_Shape shape) {\r
+        \r
+        double []min = new double[3];\r
+        double []max = new double[3];\r
+        double []mmm = new double[3];\r
+        double []bb = new double[6];\r
+                \r
+        // Compute bounding box:\r
+        //----------------------\r
+        Bnd_Box boundingBox = new Bnd_Box();\r
+        BRepBndLib.add(shape, boundingBox);\r
+        boundingBox.get(bb);\r
+        boundingBox.delete();\r
+        \r
+        min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];\r
+        max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];\r
+\r
+        //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
+\r
+        // The length of the space diagonal of cuboid\r
+        for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];\r
+        double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]);\r
+\r
+        double t0 = mmm[0]*mmm[0];\r
+        double t1 = mmm[1]*mmm[1];\r
+        double t2 = mmm[2]*mmm[2];\r
+\r
+        double tol = 1.0e-6 * length;\r
+\r
+        if((t0 < tol) || (t1 < tol) || (t2 < tol)) {\r
+            System.out.println("Shape seems to be 2D. Unable to proceed. Aborting.");\r
+            return 0;\r
+        }\r
+        return length;\r
+    }\r
+       \r
+ public static double getBoundingBoxVolume(TopoDS_Shape shape) {\r
+        \r
+        double []min = new double[3];\r
+        double []max = new double[3];\r
+        double []mmm = new double[3];\r
+        double []bb = new double[6];\r
+                \r
+        // Compute bounding box:\r
+        //----------------------\r
+        Bnd_Box boundingBox = new Bnd_Box();\r
+        BRepBndLib.add(shape, boundingBox);\r
+        boundingBox.get(bb);\r
+        boundingBox.delete();\r
+        \r
+        min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];\r
+        max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];\r
+\r
+        //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
+\r
+        for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];\r
+        double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]);\r
+\r
+        return vol;\r
+    }\r
\r
+        private static GProp_GProps getGProp(TopoDS_Shape shape) {\r
+                GProp_GProps GSystem = null;\r
+               int type = 0;\r
+               if (type == 0) {\r
+                       GSystem = new GProp_GProps();\r
+                       BRepGProp.volumeProperties(shape, GSystem,0.001);\r
+               } else if (type == 1) {\r
+                       GSystem = new GProp_VelGProps();\r
+                       BRepGProp.volumeProperties(shape, GSystem,0.001);\r
+               } else if (type == 2) {\r
+                       GSystem = new GProp_VelGProps();\r
+                       BRepGProp.volumePropertiesGK(shape, GSystem, 0.001);\r
+               }\r
+               return GSystem;\r
+        }\r
+       \r
+       public static double getMass( TopoDS_Shape shape) {\r
+               \r
+               GProp_GProps GSystem = getGProp(shape);\r
+               \r
+               \r
+               double mass = GSystem.mass();\r
+               //System.out.println("Mass " + mass);\r
+               GSystem.delete();\r
+               return mass;\r
+       }\r
+       \r
+       public static double[] getInertia(TopoDS_Shape shape) {\r
+               GProp_GProps GSystem = getGProp(shape);\r
+               \r
+               double inertia[] = GSystem.matrixOfInertia();\r
+               GSystem.delete();\r
+               return inertia;\r
+       }\r
+       \r
+       public static double[] getCentreOfMass(TopoDS_Shape shape) {\r
+               GProp_GProps GSystem = getGProp(shape);\r
+               \r
+               double cm[] = GSystem.centreOfMass();\r
+               GSystem.delete();\r
+               return cm;\r
+       }\r
+       \r
+       public static boolean appendToMesh (TopoDS_Face face, List<Double> meshPoints, List<Integer> meshTriangles)\r
+    {\r
+        TopLoc_Location Location = new TopLoc_Location();\r
+        \r
+        Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);\r
+\r
+        if(triangulation == null) {\r
+               Location.delete();\r
+               System.out.println("Encountered empty triangulation after face");\r
+               return false;\r
+        }\r
+                \r
+        boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;\r
+        \r
+        int lastPoint = meshPoints.size() / 3;\r
+\r
+        int[]triangles = triangulation.triangles();\r
+        double[]nodes = triangulation.nodes();\r
+\r
+        int nofTriangles = triangulation.nbTriangles();\r
+        int nofNodes = triangulation.nbNodes();\r
+        \r
+        \r
+        triangulation.delete();\r
+\r
+        if(nofTriangles < 1) {\r
+          System.out.println("No triangles for mesh on face");\r
+          Location.delete();\r
+          return false;\r
+        }\r
+\r
+        if(nofNodes < 1) {\r
+            System.out.println("No nodes for mesh on face:");\r
+            Location.delete();\r
+            return false;\r
+        }\r
+       \r
+        for(int i = 0; i < nofTriangles; i++) \r
+        {\r
+          int n0, n1, n2;  \r
+          if (!reverse) {\r
+                 n0 = triangles[3 * i]; \r
+                 n1 = triangles[3 * i + 1]; \r
+                 n2 = triangles[3 * i + 2];\r
+          } else {\r
+                 n0 = triangles[3 * i + 2]; \r
+                 n1 = triangles[3 * i + 1]; \r
+                 n2 = triangles[3 * i];\r
+          }\r
+         \r
+          meshTriangles.add(n0 + lastPoint);\r
+          meshTriangles.add(n1 + lastPoint);\r
+          meshTriangles.add(n2 + lastPoint);\r
+        }\r
+\r
+        \r
+        GP_Trsf transformation = Location.transformation();\r
+        Location.delete();\r
+\r
+        double d_mat[] = new double[16];\r
+\r
+        transformation.getValues(d_mat);\r
+        Matrix4d mat = new Matrix4d(d_mat);\r
+        \r
+        \r
+        for(int i = 0; i < nofNodes; i++) {     \r
+\r
+               Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);\r
+               mat.transform(p);\r
+\r
+               meshPoints.add(p.x);\r
+               meshPoints.add(p.y);\r
+               meshPoints.add(p.z);\r
+               \r
+        }\r
+        \r
+        transformation.delete();\r
+        \r
+        return true;\r
+    }\r
+}\r