]> gerrit.simantics Code Review - simantics/3d.git/blob - 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
1 package org.simantics.opencascade;\r
2 \r
3 import java.util.List;\r
4 \r
5 import javax.vecmath.Matrix4d;\r
6 import javax.vecmath.Point3d;\r
7 \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
20 \r
21 public class OCCTTool {\r
22 public static double getBoundingBoxDiagonal(TopoDS_Shape shape) {\r
23         \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
28                 \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
35         \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
38 \r
39         //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
40 \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
44 \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
48 \r
49         double tol = 1.0e-6 * length;\r
50 \r
51         if((t0 < tol) || (t1 < tol) || (t2 < tol)) {\r
52             System.out.println("Shape seems to be 2D. Unable to proceed. Aborting.");\r
53             return 0;\r
54         }\r
55         return length;\r
56     }\r
57         \r
58  public static double getBoundingBoxVolume(TopoDS_Shape shape) {\r
59         \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
64                 \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
71         \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
74 \r
75         //System.out.println("Bounding box: "+"[ "+min[0]+", "+min[1]+", " + min[2] + "] x "+"[ " +max[0] +", " +max[1] +", " +max[2] +"]");\r
76 \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
79 \r
80         return vol;\r
81     }\r
82  \r
83          private static GProp_GProps getGProp(TopoDS_Shape shape) {\r
84                  GProp_GProps GSystem = null;\r
85                 int type = 0;\r
86                 if (type == 0) {\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
95                 }\r
96                 return GSystem;\r
97          }\r
98         \r
99         public static double getMass( TopoDS_Shape shape) {\r
100                 \r
101                 GProp_GProps GSystem = getGProp(shape);\r
102                 \r
103                 \r
104                 double mass = GSystem.mass();\r
105                 //System.out.println("Mass " + mass);\r
106                 GSystem.delete();\r
107                 return mass;\r
108         }\r
109         \r
110         public static double[] getInertia(TopoDS_Shape shape) {\r
111                 GProp_GProps GSystem = getGProp(shape);\r
112                 \r
113                 double inertia[] = GSystem.matrixOfInertia();\r
114                 GSystem.delete();\r
115                 return inertia;\r
116         }\r
117         \r
118         public static double[] getCentreOfMass(TopoDS_Shape shape) {\r
119                 GProp_GProps GSystem = getGProp(shape);\r
120                 \r
121                 double cm[] = GSystem.centreOfMass();\r
122                 GSystem.delete();\r
123                 return cm;\r
124         }\r
125         \r
126         public static boolean appendToMesh (TopoDS_Face face, List<Double> meshPoints, List<Integer> meshTriangles)\r
127     {\r
128         TopLoc_Location Location = new TopLoc_Location();\r
129         \r
130         Poly_Triangulation triangulation = BRep_Tool.triangulation(face, Location);\r
131 \r
132         if(triangulation == null) {\r
133                 Location.delete();\r
134                 System.out.println("Encountered empty triangulation after face");\r
135                 return false;\r
136         }\r
137                 \r
138         boolean reverse = face.orientation()==TopAbs_Orientation.REVERSED;\r
139         \r
140         int lastPoint = meshPoints.size() / 3;\r
141 \r
142         int[]triangles = triangulation.triangles();\r
143         double[]nodes = triangulation.nodes();\r
144 \r
145         int nofTriangles = triangulation.nbTriangles();\r
146         int nofNodes = triangulation.nbNodes();\r
147         \r
148         \r
149         triangulation.delete();\r
150 \r
151         if(nofTriangles < 1) {\r
152           System.out.println("No triangles for mesh on face");\r
153           Location.delete();\r
154           return false;\r
155         }\r
156 \r
157         if(nofNodes < 1) {\r
158             System.out.println("No nodes for mesh on face:");\r
159             Location.delete();\r
160             return false;\r
161         }\r
162        \r
163         for(int i = 0; i < nofTriangles; i++) \r
164         {\r
165           int n0, n1, n2;  \r
166           if (!reverse) {\r
167                   n0 = triangles[3 * i]; \r
168                   n1 = triangles[3 * i + 1]; \r
169                   n2 = triangles[3 * i + 2];\r
170           } else {\r
171                   n0 = triangles[3 * i + 2]; \r
172                   n1 = triangles[3 * i + 1]; \r
173                   n2 = triangles[3 * i];\r
174           }\r
175          \r
176           meshTriangles.add(n0 + lastPoint);\r
177           meshTriangles.add(n1 + lastPoint);\r
178           meshTriangles.add(n2 + lastPoint);\r
179         }\r
180 \r
181         \r
182         GP_Trsf transformation = Location.transformation();\r
183         Location.delete();\r
184 \r
185         double d_mat[] = new double[16];\r
186 \r
187         transformation.getValues(d_mat);\r
188         Matrix4d mat = new Matrix4d(d_mat);\r
189         \r
190         \r
191         for(int i = 0; i < nofNodes; i++) {     \r
192 \r
193                 Point3d p = new Point3d(nodes[3 * i], nodes[3 * i + 1], nodes[3 * i + 2]);\r
194                 mat.transform(p);\r
195 \r
196                 meshPoints.add(p.x);\r
197                 meshPoints.add(p.y);\r
198                 meshPoints.add(p.z);\r
199                 \r
200         }\r
201         \r
202         transformation.delete();\r
203         \r
204         return true;\r
205     }\r
206 }\r