2 * Project Info: http://jcae.sourceforge.net
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)
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
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.
18 * (C) Copyright 2011, by EADS France
21 package org.jcae.opencascade.jni;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.jcae.opencascade.Shape;
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
35 * @see http://www.opencascade.org/org/forum/thread_22070/
36 * @see http://github.com/tpaviot/oce/issues/143
37 * @author Jerome Robert
39 public class TrimmedSphere
41 private static class Shape extends org.jcae.opencascade.Shape<Shape>
43 private final static Factory<Shape> FACTORY=new Factory<Shape>()
45 public Shape create(TopoDS_Shape shape,
46 Map<TopoDS_Shape, Shape> map, Shape[] parents)
48 return new Shape(shape, map, parents);
51 public Shape[] createArray(int length)
53 return new Shape[length];
56 public Shape(TopoDS_Shape shape)
58 this(shape, new HashMap<TopoDS_Shape, Shape>(), new Shape[0]);
60 protected Shape(TopoDS_Shape shape, Map<TopoDS_Shape, Shape> map, Shape[] parents)
62 super(shape, map, parents);
65 protected Factory<Shape> getFactory() {
70 protected Shape getDerived() {
74 public TopoDS_Shape getImpl() {
79 private static List<TopoDS_Vertex> createMeshEdge(double[] p1, double[] p2, int n)
81 TopoDS_Vertex[] toReturn = new TopoDS_Vertex[n];
82 double[] pp1 = new double[3];
83 for(int i = 0; i<n; i++)
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();
89 return Arrays.asList(toReturn);
92 private static TopoDS_Edge makeEdge(TopoDS_Vertex v1, TopoDS_Vertex v2)
94 BRepBuilderAPI_MakeEdge me = new BRepBuilderAPI_MakeEdge(v1, v2);
95 TopoDS_Edge toReturn = (TopoDS_Edge)me.shape();
99 private static TopoDS_Wire createWire(int n, double[] ... points)
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));
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)));
110 b.add(makeEdge(vertices.get(vertices.size()-1), vertices.get(0)));
111 return (TopoDS_Wire) b.shape();
114 private static TopoDS_Wire createSquare(double s, double y, int n)
116 TopoDS_Wire wire = createWire(n,
117 new double[]{-s,y,-s},
118 new double[]{s,y,-s},
120 new double[]{-s,y,s});
124 private static TopoDS_Wire wireFromCompound(TopoDS_Shape comp)
126 BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();
128 for(Shape e:new Shape(comp).explore(TopAbs_ShapeEnum.EDGE))
130 wire.add((TopoDS_Edge)e.getImpl());
133 TopoDS_Wire toReturn = (TopoDS_Wire) wire.shape();
134 return (TopoDS_Wire) toReturn.reversed();
136 private static TopoDS_Face createTopoDSShape(boolean nurbs, int n)
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);
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);
148 return (TopoDS_Face) new BRepBuilderAPI_NurbsConvert(ssphere.getImpl()).shape();
150 return (TopoDS_Face) ssphere.getImpl();
153 private static int mesh(TopoDS_Face shape)
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;
162 public static void main(String[] args)
164 for(int i = 1; i<100; i++)
166 double nn = mesh(createTopoDSShape(true, i));
167 double ns = mesh(createTopoDSShape(false, i));
168 System.out.println(i+": "+(nn/ns));