]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.opencascade/src/org/simantics/opencascade/OccTriangulator.java
Turn component calculation with offset vector fixed
[simantics/3d.git] / org.simantics.opencascade / src / org / simantics / opencascade / OccTriangulator.java
1 package org.simantics.opencascade;
2
3 /*******************************************************************************
4  * Copyright (c) 2007, 2013- VTT Technical Research Centre of Finland.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  *     VTT Technical Research Centre of Finland - initial API and implementation
12  *******************************************************************************/
13
14 import javax.vecmath.Vector2d;
15
16 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge;
17 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeFace;
18 import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire;
19 import org.jcae.opencascade.jni.BRepBuilderAPI_Transform;
20 import org.jcae.opencascade.jni.BRepPrimAPI_MakePrism;
21 import org.jcae.opencascade.jni.BRepPrimAPI_MakeTorus;
22 import org.jcae.opencascade.jni.BRep_Builder;
23 import org.jcae.opencascade.jni.GP_Elips;
24 import org.jcae.opencascade.jni.GP_Trsf;
25 import org.jcae.opencascade.jni.TopoDS_Edge;
26 import org.jcae.opencascade.jni.TopoDS_Face;
27 import org.jcae.opencascade.jni.TopoDS_Shape;
28 import org.jcae.opencascade.jni.TopoDS_Wire;
29
30
31 public class OccTriangulator {
32
33     
34     public static final double MIN_VALUE = 0.001;
35     
36     public OccTriangulator() {
37         
38     }
39
40
41     public static TopoDS_Shape getShapeFromFile(String filename) {
42         assert (filename != null);
43         String lowerFileName = filename.toLowerCase(); 
44         if (lowerFileName.endsWith(".stp") || lowerFileName.endsWith(".step")) {
45             TopoDS_Shape shape = importSTEP(filename);
46             return shape;
47         } else if (lowerFileName.endsWith(".iges")) {
48             TopoDS_Shape shape = importIGES(filename);
49             return shape;
50         } else if (lowerFileName.endsWith(".brep")) {
51             TopoDS_Shape shape = importBREP(filename);
52             return shape;
53         } else {
54             throw new UnsupportedOperationException("Unsupported format " + filename);
55         }
56     }
57
58     public static TopoDS_Shape importBREP(String filename) {
59                 return importBREP(filename,1.0); // convert to meters.
60     }
61     public static TopoDS_Shape importBREP(String filename, double scale) {
62         org.jcae.opencascade.jni.BRep_Builder aBuilder = new org.jcae.opencascade.jni.BRep_Builder();
63         org.jcae.opencascade.jni.TopoDS_Shape myShape = org.jcae.opencascade.jni.BRepTools.read(filename, aBuilder);
64         aBuilder.delete();
65         if (Math.abs(scale-1.0) < 0.001)
66                 return myShape;
67         TopoDS_Shape scaled = makeScale(myShape, scale);
68         myShape.delete();
69         return scaled;
70     }
71
72     public static TopoDS_Shape importIGES(String filename) {
73         org.jcae.opencascade.jni.IGESControl_Reader aReader = new org.jcae.opencascade.jni.IGESControl_Reader();
74         aReader.setReadUnitM();
75         aReader.readFile(filename.getBytes());
76         aReader.clearShapes();
77         aReader.transferRoots();
78         TopoDS_Shape result = aReader.oneShape();
79         aReader.delete();
80         return result;
81     }
82     
83     public static TopoDS_Shape importSTEP(String filename) {
84         org.jcae.opencascade.jni.STEPControl_Reader aReader = new org.jcae.opencascade.jni.STEPControl_Reader();
85         aReader.setReadUnitM();
86         aReader.readFile(filename.getBytes());
87         aReader.clearShapes();
88         aReader.transferRoots();
89         TopoDS_Shape result = aReader.oneShape();
90         aReader.delete();
91         return result;
92     }
93
94
95
96     public static TopoDS_Shape makeTranslation(TopoDS_Shape aShape, double x, double y, double z) {
97         GP_Trsf theTransformation = new GP_Trsf();
98         theTransformation.setTranslation(new double[] { x, y, z });
99         BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
100
101         TopoDS_Shape shape = bt.shape();
102         bt.delete();
103         theTransformation.delete();
104         return shape;
105     }
106
107     public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2) {
108         double[] axe = new double[6];
109         System.arraycopy(pointStruct, 0, axe, 0, 3);
110         System.arraycopy(dirStruct, 0, axe, 3, 3);
111         BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2);
112         org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
113         torus.delete();
114         return tds;
115     }
116     
117
118     public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double[] dirStruct2, double r1, double r2) {
119         double[] axe = new double[9];
120         System.arraycopy(pointStruct, 0, axe, 0, 3);
121         System.arraycopy(dirStruct, 0, axe, 3, 3);
122         System.arraycopy(dirStruct2, 0, axe, 6, 3);
123         BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2);
124         org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
125         torus.delete();
126         return tds;
127     }
128     
129     public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2, double angle1, double angle2, double angle) {
130         double[] axe = new double[6];
131         System.arraycopy(pointStruct, 0, axe, 0, 3);
132         System.arraycopy(dirStruct, 0, axe, 3, 3);
133         BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2,angle1,angle2,angle);
134         org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
135         torus.delete();
136         return tds;
137     }
138     
139     public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double[] dirStruct2,  double r1, double r2, double angle1, double angle2, double angle) {
140         double[] axe = new double[9];
141         System.arraycopy(pointStruct, 0, axe, 0, 3);
142         System.arraycopy(dirStruct, 0, axe, 3, 3);
143         System.arraycopy(dirStruct2, 0, axe, 6, 3);
144         BRepPrimAPI_MakeTorus torus = new BRepPrimAPI_MakeTorus(axe, r1, r2,angle1,angle2,angle);
145         org.jcae.opencascade.jni.TopoDS_Shape tds = torus.shape();
146         torus.delete();
147         return tds;
148     }
149
150     public static TopoDS_Shape makeSphere(double x, double y, double z, double radius) {
151         double[] c = new double[] { x, y, z };
152         org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere sphere = new org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere(c, radius);
153         org.jcae.opencascade.jni.TopoDS_Shape tds = sphere.shape();
154         sphere.delete();
155         return tds;
156     }
157
158
159     public static TopoDS_Shape makeRotation(TopoDS_Shape aShape, double[] axisStruct, double angle) {
160
161         GP_Trsf theTransformation = new GP_Trsf();
162         theTransformation.setRotation(axisStruct, angle);
163         BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
164         TopoDS_Shape shape = bt.shape();
165         bt.delete();
166         theTransformation.delete();
167         return shape;
168     }
169
170     public static TopoDS_Shape makeScale(TopoDS_Shape aShape, double s) {
171
172         GP_Trsf theTransformation = new GP_Trsf();
173         theTransformation.setValues(s, 0, 0, 0,
174                                             0, s, 0, 0, 
175                                             0, 0, s, 0);
176         BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);
177         TopoDS_Shape shape = bt.shape();
178         bt.delete();
179         theTransformation.delete();
180         return shape;
181     }
182
183
184     public static TopoDS_Shape makeCylinder(double[] pointStruct, double[] dirStruct, double radius, double height) {
185         double[] axe = new double[6];
186         System.arraycopy(pointStruct, 0, axe, 0, 3);
187         System.arraycopy(dirStruct, 0, axe, 3, 3);
188         org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder cyl = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, height, 2 * Math.PI);
189         org.jcae.opencascade.jni.TopoDS_Shape tds = cyl.shape();
190         cyl.delete();
191         return tds;
192     }
193
194     public static TopoDS_Shape makeCopy(TopoDS_Shape topoDS_Shape) {
195         throw new UnsupportedOperationException();
196     }
197
198     public static TopoDS_Shape makeCone(double[] pointStruct, double[] dirStruct, double radius1, double radius2, double height) {
199         double[] axe = new double[6];
200         System.arraycopy(pointStruct, 0, axe, 0, 3);
201         System.arraycopy(dirStruct, 0, axe, 3, 3);
202         org.jcae.opencascade.jni.BRepPrimAPI_MakeCone cone = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCone(axe, radius1, radius2, height, 2 * Math.PI);
203         org.jcae.opencascade.jni.TopoDS_Shape tds = cone.shape();
204         cone.delete();
205         return tds;
206     }
207
208     public static TopoDS_Shape makeCompound(TopoDS_Shape[] shapes) {
209         BRep_Builder builder = new BRep_Builder();
210         org.jcae.opencascade.jni.TopoDS_Compound comp = new org.jcae.opencascade.jni.TopoDS_Compound();
211         builder.makeCompound(comp);
212         for (int i = 0; i < shapes.length; i++) {
213
214             builder.add(comp, shapes[i]);
215         }
216         builder.delete();
217         return comp;
218     }
219
220
221     public static TopoDS_Shape makeBox(double x1, double y1, double z1, double x2, double y2, double z2) {
222         double[] p1 = new double[] { x1, y1, z1 };
223         double[] p2 = new double[] { x2, y2, z2 };
224         org.jcae.opencascade.jni.BRepPrimAPI_MakeBox box = new org.jcae.opencascade.jni.BRepPrimAPI_MakeBox(p1, p2); 
225         org.jcae.opencascade.jni.TopoDS_Shape tds = box.shape();
226         box.delete();
227         return tds;
228     }
229
230     public static TopoDS_Shape makeCut(TopoDS_Shape shape1, TopoDS_Shape shape2) {
231         org.jcae.opencascade.jni.BRepAlgoAPI_Cut cut = new org.jcae.opencascade.jni.BRepAlgoAPI_Cut(shape1, shape2);
232         org.jcae.opencascade.jni.TopoDS_Shape s = cut.shape();
233         cut.delete();
234         return s;
235     }
236     
237     public static TopoDS_Shape makeCommon(TopoDS_Shape shape1, TopoDS_Shape shape2) {
238         org.jcae.opencascade.jni.BRepAlgoAPI_Common common = new org.jcae.opencascade.jni.BRepAlgoAPI_Common(shape1, shape2); 
239         org.jcae.opencascade.jni.TopoDS_Shape s = common.shape();
240         common.delete();
241         return s;
242     }
243     
244     public static TopoDS_Shape makeFuse(TopoDS_Shape shape1, TopoDS_Shape shape2) {
245         org.jcae.opencascade.jni.BRepAlgoAPI_Fuse fuse = new org.jcae.opencascade.jni.BRepAlgoAPI_Fuse(shape1, shape2);
246         org.jcae.opencascade.jni.TopoDS_Shape s = fuse.shape();
247         fuse.delete();
248         return s;
249     }
250
251     
252     public static TopoDS_Shape makeWedge(double[] pointStruct, double[] dirStruct,double dx, double dy, double dz, double xmin, double zmin, double xmax, double zmax) {
253         double[] axe = new double[6];
254         System.arraycopy(pointStruct, 0, axe, 0, 3);
255         System.arraycopy(dirStruct, 0, axe, 3, 3);
256         
257         org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge wedge = new org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge(axe,  dx,  dy,  dz,  xmin,  zmin, xmax, zmax); 
258         org.jcae.opencascade.jni.TopoDS_Shape s = wedge.shape();
259         wedge.delete();
260         return s;
261     }
262     
263     public static TopoDS_Shape makeEllipticCylinder(double h, double r1, double r2) {
264         GP_Elips ellipse;
265                 if (r1 < r2) {
266                         // FIXME : ellipse should be rotated, but current JNI won't allow it since Ax2 is not separate object
267                         ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r2,r1);
268                 } else {
269                         ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r1,r2);
270                 }
271                 BRepBuilderAPI_MakeEdge edge = new BRepBuilderAPI_MakeEdge(ellipse);
272                 TopoDS_Edge ed = (TopoDS_Edge) edge.shape();
273                 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire(ed);
274                 TopoDS_Wire w = (TopoDS_Wire) wire.shape();
275                 BRepBuilderAPI_MakeFace face = new BRepBuilderAPI_MakeFace(w); 
276         TopoDS_Face F = (TopoDS_Face) face.shape();
277         BRepPrimAPI_MakePrism prism = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }); 
278         TopoDS_Shape shape = prism.shape();
279         ellipse.delete();
280         edge.delete();
281         wire.delete();
282         face.delete();
283         ed.delete();
284         w.delete();
285         F.delete();
286         prism.delete();
287         return shape;
288     }
289
290     public static TopoDS_Shape makeReqularPrism(double h, double r, int n) {
291         if (n < 3)
292                 n = 3;
293         Vector2d vertices[] = new Vector2d[n];
294                 for (int i = 0; i < n; i++) {
295                         vertices[i] = new Vector2d(Math.sin(Math.PI * 2.0 * i / n)*r,Math.cos(Math.PI * 2.0 * i / n)*r);
296                 }
297                 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();
298                 for (int i = 0; i < n; i++) {
299                         Vector2d v1 = vertices[i];
300                         Vector2d v2 = vertices[(i+1)%n];
301                         BRepBuilderAPI_MakeEdge edge = new BRepBuilderAPI_MakeEdge(new double[]{v1.x,-h*0.5,v1.y},new double[]{v2.x,-h*0.5,v2.y}); 
302                         wire.add((TopoDS_Edge)edge.shape());
303                         edge.delete();
304                 }
305                 TopoDS_Wire w = (TopoDS_Wire)wire.shape();
306                 
307                 BRepBuilderAPI_MakeFace face = new BRepBuilderAPI_MakeFace(w);
308                 TopoDS_Face F = (TopoDS_Face) face.shape();
309                 face.delete();
310                 
311                 BRepPrimAPI_MakePrism prism = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }); 
312         TopoDS_Shape shape = prism.shape();     
313         prism.delete();
314         
315         wire.delete();
316         w.delete();
317         F.delete();
318         return shape;
319     }
320  
321
322     public static void exportBREP(TopoDS_Shape shape, String filename) {
323         org.jcae.opencascade.jni.BRepTools.write(shape, filename);
324     }
325     
326 }