]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.opencascade/src/org/simantics/opencascade/OCCTTool.java
Copyrights
[simantics/3d.git] / org.simantics.opencascade / src / org / simantics / opencascade / OCCTTool.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.opencascade;\r
13 \r
14 import java.util.List;\r
15 \r
16 import javax.vecmath.Matrix4d;\r
17 import javax.vecmath.Point3d;\r
18 \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
31 \r
32 public class OCCTTool {\r
33 public static double getBoundingBoxDiagonal(TopoDS_Shape shape) {\r
34         \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
39                 \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
46         \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
49 \r
50         //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
51 \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
55 \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
59 \r
60         double tol = 1.0e-6 * length;\r
61 \r
62         if((t0 < tol) || (t1 < tol) || (t2 < tol)) {\r
63             System.out.println("Shape seems to be 2D. Unable to proceed. Aborting.");\r
64             return 0;\r
65         }\r
66         return length;\r
67     }\r
68         \r
69  public static double getBoundingBoxVolume(TopoDS_Shape shape) {\r
70         \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
75                 \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
82         \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
85 \r
86         //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
87 \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
90 \r
91         return vol;\r
92     }\r
93  \r
94          private static GProp_GProps getGProp(TopoDS_Shape shape) {\r
95                  GProp_GProps GSystem = null;\r
96                 int type = 0;\r
97                 if (type == 0) {\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
106                 }\r
107                 return GSystem;\r
108          }\r
109         \r
110         public static double getMass( TopoDS_Shape shape) {\r
111                 \r
112                 GProp_GProps GSystem = getGProp(shape);\r
113                 \r
114                 \r
115                 double mass = GSystem.mass();\r
116                 //System.out.println("Mass " + mass);\r
117                 GSystem.delete();\r
118                 return mass;\r
119         }\r
120         \r
121         public static double[] getInertia(TopoDS_Shape shape) {\r
122                 GProp_GProps GSystem = getGProp(shape);\r
123                 \r
124                 double inertia[] = GSystem.matrixOfInertia();\r
125                 GSystem.delete();\r
126                 return inertia;\r
127         }\r
128         \r
129         public static double[] getCentreOfMass(TopoDS_Shape shape) {\r
130                 GProp_GProps GSystem = getGProp(shape);\r
131                 \r
132                 double cm[] = GSystem.centreOfMass();\r
133                 GSystem.delete();\r
134                 return cm;\r
135         }\r
136         \r
137         public static boolean appendToMesh (TopoDS_Face face, List<Double> meshPoints, List<Integer> meshTriangles)\r
138     {\r
139         TopLoc_Location Location = new TopLoc_Location();\r
140         \r
141         Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);\r
142 \r
143         if(triangulation == null) {\r
144                 Location.delete();\r
145                 System.out.println("Encountered empty triangulation after face");\r
146                 return false;\r
147         }\r
148                 \r
149         boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;\r
150         \r
151         int lastPoint = meshPoints.size() / 3;\r
152 \r
153         int[]triangles = triangulation.triangles();\r
154         double[]nodes = triangulation.nodes();\r
155 \r
156         int nofTriangles = triangulation.nbTriangles();\r
157         int nofNodes = triangulation.nbNodes();\r
158         \r
159         \r
160         triangulation.delete();\r
161 \r
162         if(nofTriangles < 1) {\r
163           System.out.println("No triangles for mesh on face");\r
164           Location.delete();\r
165           return false;\r
166         }\r
167 \r
168         if(nofNodes < 1) {\r
169             System.out.println("No nodes for mesh on face:");\r
170             Location.delete();\r
171             return false;\r
172         }\r
173        \r
174         for(int i = 0; i < nofTriangles; i++) \r
175         {\r
176           int n0, n1, n2;  \r
177           if (!reverse) {\r
178                   n0 = triangles[3 * i]; \r
179                   n1 = triangles[3 * i + 1]; \r
180                   n2 = triangles[3 * i + 2];\r
181           } else {\r
182                   n0 = triangles[3 * i + 2]; \r
183                   n1 = triangles[3 * i + 1]; \r
184                   n2 = triangles[3 * i];\r
185           }\r
186          \r
187           meshTriangles.add(n0 + lastPoint);\r
188           meshTriangles.add(n1 + lastPoint);\r
189           meshTriangles.add(n2 + lastPoint);\r
190         }\r
191 \r
192         \r
193         GP_Trsf transformation = Location.transformation();\r
194         Location.delete();\r
195 \r
196         double d_mat[] = new double[16];\r
197 \r
198         transformation.getValues(d_mat);\r
199         Matrix4d mat = new Matrix4d(d_mat);\r
200         \r
201         \r
202         for(int i = 0; i < nofNodes; i++) {     \r
203 \r
204                 Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);\r
205                 mat.transform(p);\r
206 \r
207                 meshPoints.add(p.x);\r
208                 meshPoints.add(p.y);\r
209                 meshPoints.add(p.z);\r
210                 \r
211         }\r
212         \r
213         transformation.delete();\r
214         \r
215         return true;\r
216     }\r
217 }\r