]> gerrit.simantics Code Review - simantics/3d.git/blob - org.jcae.opencascade/src-java-test/org/jcae/opencascade/jni/TrimmedSphere.java
Include old 64-bit versions of org.jcae.opencascade and vtk bundles
[simantics/3d.git] / org.jcae.opencascade / src-java-test / org / jcae / opencascade / jni / TrimmedSphere.java
1 /*
2  * Project Info:  http://jcae.sourceforge.net
3  *
4  * This program is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 2.1 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * (C) Copyright 2011, by EADS France
19  */
20
21 package org.jcae.opencascade.jni;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import org.jcae.opencascade.Shape;
29
30 /**
31  * Trim a sphere with square and mesh it.
32  * The square TopoDS_Wire is refined into more than 4 TopoDS_Edge.
33  * The created geometry is converted to nurbs to mesh both nurbified and
34  * original shapes.
35  * @see http://www.opencascade.org/org/forum/thread_22070/
36  * @see http://github.com/tpaviot/oce/issues/143
37  * @author Jerome Robert
38  */
39 public class TrimmedSphere
40 {
41         private static class Shape extends org.jcae.opencascade.Shape<Shape>
42         {
43                 private final static Factory<Shape> FACTORY=new Factory<Shape>()
44                 {
45                         public Shape create(TopoDS_Shape shape,
46                                 Map<TopoDS_Shape, Shape> map, Shape[] parents)
47                         {
48                                 return new Shape(shape, map, parents);
49                         }
50
51                         public Shape[] createArray(int length)
52                         {
53                                 return new Shape[length];
54                         }
55                 };
56                 public Shape(TopoDS_Shape shape)
57                 {
58                         this(shape, new HashMap<TopoDS_Shape, Shape>(), new Shape[0]);
59                 }
60                 protected Shape(TopoDS_Shape shape, Map<TopoDS_Shape, Shape> map, Shape[] parents)
61                 {
62                         super(shape, map, parents);
63                 }
64                 @Override
65                 protected Factory<Shape> getFactory() {
66                         return FACTORY;
67                 }
68
69                 @Override
70                 protected Shape getDerived() {
71                         return this;
72                 }
73
74                 public TopoDS_Shape getImpl() {
75                         return impl;
76                 }
77         }
78
79         private static List<TopoDS_Vertex> createMeshEdge(double[] p1, double[] p2, int n)
80         {
81                 TopoDS_Vertex[] toReturn = new TopoDS_Vertex[n];
82                 double[] pp1 = new double[3];
83                 for(int i = 0; i<n; i++)
84                 {
85                         for(int j = 0; j < 3; j++)
86                                 pp1[j] = p1[j] + ((double)i)/n*(p2[j] - p1[j]);
87                         toReturn[i] = (TopoDS_Vertex) new BRepBuilderAPI_MakeVertex(pp1).shape();
88                 }
89                 return Arrays.asList(toReturn);
90         }
91
92         private static TopoDS_Edge makeEdge(TopoDS_Vertex v1, TopoDS_Vertex v2)
93         {
94                 BRepBuilderAPI_MakeEdge me = new BRepBuilderAPI_MakeEdge(v1, v2);
95                 TopoDS_Edge toReturn = (TopoDS_Edge)me.shape();
96                 return toReturn;
97         }
98
99         private static TopoDS_Wire createWire(int n, double[] ... points)
100         {
101                 ArrayList<TopoDS_Vertex> vertices = new ArrayList<TopoDS_Vertex>();
102                 for(int i = 0; i<points.length-1; i++)
103                         vertices.addAll(createMeshEdge(points[i], points[i+1], n));
104                 vertices.addAll(createMeshEdge(points[points.length-1], points[0], n));
105
106                 BRepBuilderAPI_MakeWire b = new BRepBuilderAPI_MakeWire();
107                 for(int i = 0; i<vertices.size()-1; i++)
108                         b.add(makeEdge(vertices.get(i), vertices.get(i+1)));
109
110                 b.add(makeEdge(vertices.get(vertices.size()-1), vertices.get(0)));
111                 return (TopoDS_Wire) b.shape();
112         }
113
114         private static TopoDS_Wire createSquare(double s, double y, int n)
115         {
116                 TopoDS_Wire wire = createWire(n,
117                         new double[]{-s,y,-s},
118                         new double[]{s,y,-s},
119                         new double[]{s,y,s},
120                         new double[]{-s,y,s});
121                 return wire;
122         }
123
124         private static TopoDS_Wire wireFromCompound(TopoDS_Shape comp)
125         {
126                 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();
127                 int i = 0;
128                 for(Shape e:new Shape(comp).explore(TopAbs_ShapeEnum.EDGE))
129                 {
130                         wire.add((TopoDS_Edge)e.getImpl());
131                         i++;
132                 }
133                 TopoDS_Wire toReturn = (TopoDS_Wire) wire.shape();
134                 return (TopoDS_Wire) toReturn.reversed();
135         }
136         private static TopoDS_Face createTopoDSShape(boolean nurbs, int n)
137         {
138                 TopoDS_Shape sphere = new BRepPrimAPI_MakeSphere(new double[]{0,0,0}, 1).shape();
139                 TopoDS_Wire square = createSquare(0.5, 2, n);
140                 BRepOffsetAPI_NormalProjection projector = new BRepOffsetAPI_NormalProjection(sphere);
141                 projector.add(square);
142                 projector.build();
143                 Shape projection = new Shape(wireFromCompound(projector.projection()));
144                 Shape ssphere = new Shape(sphere).getShapeFromID(1, TopAbs_ShapeEnum.FACE);
145                 ssphere.getShapeFromID(1, TopAbs_ShapeEnum.WIRE).remove();
146                 ssphere.add(projection);
147                 if(nurbs)
148                         return (TopoDS_Face) new BRepBuilderAPI_NurbsConvert(ssphere.getImpl()).shape();
149                 else
150                         return (TopoDS_Face) ssphere.getImpl();
151         }
152
153         private static int mesh(TopoDS_Face shape)
154         {
155                 TopLoc_Location loc = new TopLoc_Location();
156                 BRepTools.clean(shape);
157                 new BRepMesh_IncrementalMesh(shape, 7E-3, true);
158                 Poly_Triangulation pt = BRep_Tool.triangulation(shape, loc);
159                 return pt.triangles().length / 3;
160         }
161
162         public static void main(String[] args)
163         {
164                 for(int i = 1; i<100; i++)
165                 {
166                         double nn = mesh(createTopoDSShape(true, i));
167                         double ns = mesh(createTopoDSShape(false, i));
168                         System.out.println(i+": "+(nn/ns));
169                 }
170         }
171 }