1 package org.simantics.opencascade;
\r
3 import java.util.List;
\r
5 import javax.vecmath.Matrix4d;
\r
6 import javax.vecmath.Point3d;
\r
8 import org.jcae.opencascade.jni.BRepBndLib;
\r
9 import org.jcae.opencascade.jni.BRepGProp;
\r
10 import org.jcae.opencascade.jni.BRep_Tool;
\r
11 import org.jcae.opencascade.jni.Bnd_Box;
\r
12 import org.jcae.opencascade.jni.GP_Trsf;
\r
13 import org.jcae.opencascade.jni.GProp_GProps;
\r
14 import org.jcae.opencascade.jni.GProp_VelGProps;
\r
15 import org.jcae.opencascade.jni.Poly_Triangulation;
\r
16 import org.jcae.opencascade.jni.TopAbs_Orientation;
\r
17 import org.jcae.opencascade.jni.TopLoc_Location;
\r
18 import org.jcae.opencascade.jni.TopoDS_Face;
\r
19 import org.jcae.opencascade.jni.TopoDS_Shape;
\r
21 public class OCCTTool {
\r
22 public static double getBoundingBoxDiagonal(TopoDS_Shape shape) {
\r
24 double []min = new double[3];
\r
25 double []max = new double[3];
\r
26 double []mmm = new double[3];
\r
27 double []bb = new double[6];
\r
29 // Compute bounding box:
\r
30 //----------------------
\r
31 Bnd_Box boundingBox = new Bnd_Box();
\r
32 BRepBndLib.add(shape, boundingBox);
\r
33 boundingBox.get(bb);
\r
34 boundingBox.delete();
\r
36 min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];
\r
37 max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];
\r
39 //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");
\r
41 // The length of the space diagonal of cuboid
\r
42 for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];
\r
43 double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]);
\r
45 double t0 = mmm[0]*mmm[0];
\r
46 double t1 = mmm[1]*mmm[1];
\r
47 double t2 = mmm[2]*mmm[2];
\r
49 double tol = 1.0e-6 * length;
\r
51 if((t0 < tol) || (t1 < tol) || (t2 < tol)) {
\r
52 System.out.println("Shape seems to be 2D. Unable to proceed. Aborting.");
\r
58 public static double getBoundingBoxVolume(TopoDS_Shape shape) {
\r
60 double []min = new double[3];
\r
61 double []max = new double[3];
\r
62 double []mmm = new double[3];
\r
63 double []bb = new double[6];
\r
65 // Compute bounding box:
\r
66 //----------------------
\r
67 Bnd_Box boundingBox = new Bnd_Box();
\r
68 BRepBndLib.add(shape, boundingBox);
\r
69 boundingBox.get(bb);
\r
70 boundingBox.delete();
\r
72 min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];
\r
73 max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];
\r
75 //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");
\r
77 for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];
\r
78 double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]);
\r
83 private static GProp_GProps getGProp(TopoDS_Shape shape) {
\r
84 GProp_GProps GSystem = null;
\r
87 GSystem = new GProp_GProps();
\r
88 BRepGProp.volumeProperties(shape, GSystem,0.001);
\r
89 } else if (type == 1) {
\r
90 GSystem = new GProp_VelGProps();
\r
91 BRepGProp.volumeProperties(shape, GSystem,0.001);
\r
92 } else if (type == 2) {
\r
93 GSystem = new GProp_VelGProps();
\r
94 BRepGProp.volumePropertiesGK(shape, GSystem, 0.001);
\r
99 public static double getMass( TopoDS_Shape shape) {
\r
101 GProp_GProps GSystem = getGProp(shape);
\r
104 double mass = GSystem.mass();
\r
105 //System.out.println("Mass " + mass);
\r
110 public static double[] getInertia(TopoDS_Shape shape) {
\r
111 GProp_GProps GSystem = getGProp(shape);
\r
113 double inertia[] = GSystem.matrixOfInertia();
\r
118 public static double[] getCentreOfMass(TopoDS_Shape shape) {
\r
119 GProp_GProps GSystem = getGProp(shape);
\r
121 double cm[] = GSystem.centreOfMass();
\r
126 public static boolean appendToMesh (TopoDS_Face face, List<Double> meshPoints, List<Integer> meshTriangles)
\r
128 TopLoc_Location Location = new TopLoc_Location();
\r
130 Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);
\r
132 if(triangulation == null) {
\r
134 System.out.println("Encountered empty triangulation after face");
\r
138 boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;
\r
140 int lastPoint = meshPoints.size() / 3;
\r
142 int[]triangles = triangulation.triangles();
\r
143 double[]nodes = triangulation.nodes();
\r
145 int nofTriangles = triangulation.nbTriangles();
\r
146 int nofNodes = triangulation.nbNodes();
\r
149 triangulation.delete();
\r
151 if(nofTriangles < 1) {
\r
152 System.out.println("No triangles for mesh on face");
\r
158 System.out.println("No nodes for mesh on face:");
\r
163 for(int i = 0; i < nofTriangles; i++)
\r
167 n0 = triangles[3 * i];
\r
168 n1 = triangles[3 * i + 1];
\r
169 n2 = triangles[3 * i + 2];
\r
171 n0 = triangles[3 * i + 2];
\r
172 n1 = triangles[3 * i + 1];
\r
173 n2 = triangles[3 * i];
\r
176 meshTriangles.add(n0 + lastPoint);
\r
177 meshTriangles.add(n1 + lastPoint);
\r
178 meshTriangles.add(n2 + lastPoint);
\r
182 GP_Trsf transformation = Location.transformation();
\r
185 double d_mat[] = new double[16];
\r
187 transformation.getValues(d_mat);
\r
188 Matrix4d mat = new Matrix4d(d_mat);
\r
191 for(int i = 0; i < nofNodes; i++) {
\r
193 Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);
\r
196 meshPoints.add(p.x);
\r
197 meshPoints.add(p.y);
\r
198 meshPoints.add(p.z);
\r
202 transformation.delete();
\r