1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.opencascade;
\r
14 import java.util.List;
\r
16 import javax.vecmath.Matrix4d;
\r
17 import javax.vecmath.Point3d;
\r
19 import org.jcae.opencascade.jni.BRepBndLib;
\r
20 import org.jcae.opencascade.jni.BRepGProp;
\r
21 import org.jcae.opencascade.jni.BRep_Tool;
\r
22 import org.jcae.opencascade.jni.Bnd_Box;
\r
23 import org.jcae.opencascade.jni.GP_Trsf;
\r
24 import org.jcae.opencascade.jni.GProp_GProps;
\r
25 import org.jcae.opencascade.jni.GProp_VelGProps;
\r
26 import org.jcae.opencascade.jni.Poly_Triangulation;
\r
27 import org.jcae.opencascade.jni.TopAbs_Orientation;
\r
28 import org.jcae.opencascade.jni.TopLoc_Location;
\r
29 import org.jcae.opencascade.jni.TopoDS_Face;
\r
30 import org.jcae.opencascade.jni.TopoDS_Shape;
\r
32 public class OCCTTool {
\r
33 public static double getBoundingBoxDiagonal(TopoDS_Shape shape) {
\r
35 double []min = new double[3];
\r
36 double []max = new double[3];
\r
37 double []mmm = new double[3];
\r
38 double []bb = new double[6];
\r
40 // Compute bounding box:
\r
41 //----------------------
\r
42 Bnd_Box boundingBox = new Bnd_Box();
\r
43 BRepBndLib.add(shape, boundingBox);
\r
44 boundingBox.get(bb);
\r
45 boundingBox.delete();
\r
47 min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];
\r
48 max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];
\r
50 //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");
\r
52 // The length of the space diagonal of cuboid
\r
53 for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];
\r
54 double length = Math.sqrt(mmm[2]*mmm[2] + mmm[1]*mmm[1] + mmm[0]*mmm[0]);
\r
56 double t0 = mmm[0]*mmm[0];
\r
57 double t1 = mmm[1]*mmm[1];
\r
58 double t2 = mmm[2]*mmm[2];
\r
60 double tol = 1.0e-6 * length;
\r
62 if((t0 < tol) || (t1 < tol) || (t2 < tol)) {
\r
63 System.out.println("Shape seems to be 2D. Unable to proceed. Aborting.");
\r
69 public static double getBoundingBoxVolume(TopoDS_Shape shape) {
\r
71 double []min = new double[3];
\r
72 double []max = new double[3];
\r
73 double []mmm = new double[3];
\r
74 double []bb = new double[6];
\r
76 // Compute bounding box:
\r
77 //----------------------
\r
78 Bnd_Box boundingBox = new Bnd_Box();
\r
79 BRepBndLib.add(shape, boundingBox);
\r
80 boundingBox.get(bb);
\r
81 boundingBox.delete();
\r
83 min[0] = bb[0]; min[1] = bb[1]; min[2] = bb[2];
\r
84 max[0] = bb[3]; max[1] = bb[4]; max[2] = bb[5];
\r
86 //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");
\r
88 for (int i = 0; i < 3; i++) mmm[i] = max[i] - min[i];
\r
89 double vol = Math.sqrt(mmm[2]*mmm[1]*mmm[0]);
\r
94 private static GProp_GProps getGProp(TopoDS_Shape shape) {
\r
95 GProp_GProps GSystem = null;
\r
98 GSystem = new GProp_GProps();
\r
99 BRepGProp.volumeProperties(shape, GSystem,0.001);
\r
100 } else if (type == 1) {
\r
101 GSystem = new GProp_VelGProps();
\r
102 BRepGProp.volumeProperties(shape, GSystem,0.001);
\r
103 } else if (type == 2) {
\r
104 GSystem = new GProp_VelGProps();
\r
105 BRepGProp.volumePropertiesGK(shape, GSystem, 0.001);
\r
110 public static double getMass( TopoDS_Shape shape) {
\r
112 GProp_GProps GSystem = getGProp(shape);
\r
115 double mass = GSystem.mass();
\r
116 //System.out.println("Mass " + mass);
\r
121 public static double[] getInertia(TopoDS_Shape shape) {
\r
122 GProp_GProps GSystem = getGProp(shape);
\r
124 double inertia[] = GSystem.matrixOfInertia();
\r
129 public static double[] getCentreOfMass(TopoDS_Shape shape) {
\r
130 GProp_GProps GSystem = getGProp(shape);
\r
132 double cm[] = GSystem.centreOfMass();
\r
137 public static boolean appendToMesh (TopoDS_Face face, List<Double> meshPoints, List<Integer> meshTriangles)
\r
139 TopLoc_Location Location = new TopLoc_Location();
\r
141 Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);
\r
143 if(triangulation == null) {
\r
145 System.out.println("Encountered empty triangulation after face");
\r
149 boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;
\r
151 int lastPoint = meshPoints.size() / 3;
\r
153 int[]triangles = triangulation.triangles();
\r
154 double[]nodes = triangulation.nodes();
\r
156 int nofTriangles = triangulation.nbTriangles();
\r
157 int nofNodes = triangulation.nbNodes();
\r
160 triangulation.delete();
\r
162 if(nofTriangles < 1) {
\r
163 System.out.println("No triangles for mesh on face");
\r
169 System.out.println("No nodes for mesh on face:");
\r
174 for(int i = 0; i < nofTriangles; i++)
\r
178 n0 = triangles[3 * i];
\r
179 n1 = triangles[3 * i + 1];
\r
180 n2 = triangles[3 * i + 2];
\r
182 n0 = triangles[3 * i + 2];
\r
183 n1 = triangles[3 * i + 1];
\r
184 n2 = triangles[3 * i];
\r
187 meshTriangles.add(n0 + lastPoint);
\r
188 meshTriangles.add(n1 + lastPoint);
\r
189 meshTriangles.add(n2 + lastPoint);
\r
193 GP_Trsf transformation = Location.transformation();
\r
196 double d_mat[] = new double[16];
\r
198 transformation.getValues(d_mat);
\r
199 Matrix4d mat = new Matrix4d(d_mat);
\r
202 for(int i = 0; i < nofNodes; i++) {
\r
204 Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);
\r
207 meshPoints.add(p.x);
\r
208 meshPoints.add(p.y);
\r
209 meshPoints.add(p.z);
\r
213 transformation.delete();
\r