/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ /* This class is combination of jCAE's classes to generate meshes from shapes. Faces are lisetd as individual meshes. Marko Luukkainen jCAE stand for Java Computer Aided Engineering. Features are : Small CAD modeler, Finit element mesher, Plugin architecture. Copyright (C) 2003 Jerome Robert This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package org.simantics.proconf.g3d.occ.geometry; import org.jcae.opencascade.jni.BRepBndLib; import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh; import org.jcae.opencascade.jni.BRepTools; import org.jcae.opencascade.jni.BRep_Tool; import org.jcae.opencascade.jni.Bnd_Box; import org.jcae.opencascade.jni.GCPnts_UniformDeflection; import org.jcae.opencascade.jni.GP_Trsf; import org.jcae.opencascade.jni.GeomAPI_ProjectPointOnSurf; import org.jcae.opencascade.jni.GeomAdaptor_Curve; import org.jcae.opencascade.jni.GeomLProp_SLProps; import org.jcae.opencascade.jni.Geom_Curve; import org.jcae.opencascade.jni.Geom_Surface; import org.jcae.opencascade.jni.Poly_Triangulation; import org.jcae.opencascade.jni.TopAbs_Orientation; import org.jcae.opencascade.jni.TopAbs_ShapeEnum; import org.jcae.opencascade.jni.TopExp_Explorer; import org.jcae.opencascade.jni.TopLoc_Location; import org.jcae.opencascade.jni.TopoDS_Edge; import org.jcae.opencascade.jni.TopoDS_Face; import org.jcae.opencascade.jni.TopoDS_Shape; import org.jcae.opencascade.jni.TopoDS_Vertex; import java.util.ArrayList; import java.util.HashSet; import javax.vecmath.Matrix4d; import javax.vecmath.Point3d; import javax.vecmath.Vector3f; public class ViewableShapeImpl { ArrayList faceMeshes =new ArrayList(); ArrayList edges=new ArrayList(); public ViewableShapeImpl(TopoDS_Shape g) { BRepTools.clean(g); buildFaces(g); buildEdges(g); } /* public ViewableShapeImpl(TopoDS_Shape g, boolean alternative) { if (alternative) buildFaces(g); else { BRepTools.clean(g); buildFaces2(g); } buildEdges(g); } */ public IndexedGeometry getGeometry(int i) { return faceMeshes.get(i); } public int numGeometries() { return faceMeshes.size(); } public int getNumEdges() { return edges.size(); } public float[] getEdge(int i) { return edges.get(i); } /** * org.jcae.viewer3d.cad.occ.OCCEdgeDomain * @param shape * @param geom */ private void buildFaces(TopoDS_Shape shape) { TopExp_Explorer explorer = new TopExp_Explorer(); TopLoc_Location loc = new TopLoc_Location(); int meshIter=4; double deflection = deflection(shape); for (explorer.init(shape, TopAbs_ShapeEnum.FACE); explorer.more(); explorer.next()) { //System.out.println("Triangulation"); TopoDS_Shape s = explorer.current(); if (!(s instanceof TopoDS_Face)) continue; // should not happen! TopoDS_Face face = (TopoDS_Face)s; Poly_Triangulation pt = BRep_Tool.triangulation(face,loc); //float error=0.01f; double error = deflection; int iter=0; // if shape was generated with program triangulation seems to be always null, // but model is loaded from file, it may already contain triangulation // So : in those cases can we delete existing triangulation and generate a new one ? //if (pt == null) { //System.out.println("Initial triangulation of face "+face+" not found. Using Incremental mesh"); //} else { //System.out.println("Initial triangulation of face "+face+" found."); //} while((pt==null)&(iter alreadyDone=new HashSet(); double boundingBoxDeflection = edgeDeflection(shape); for (explorer.init(shape, TopAbs_ShapeEnum.EDGE); explorer.more(); explorer.next()) { TopoDS_Shape s = explorer.current(); if (!(s instanceof TopoDS_Edge)) continue; // should not happen! TopoDS_Edge e = (TopoDS_Edge)s; if(!alreadyDone.add(e)) continue; double[] range = BRep_Tool.range(e); Geom_Curve gc = BRep_Tool.curve(e, range); float[] array; if(gc!=null) { GeomAdaptor_Curve adaptator = new GeomAdaptor_Curve(gc); GCPnts_UniformDeflection deflector = new GCPnts_UniformDeflection(); deflector.initialize(adaptator, boundingBoxDeflection, range[0], range[1]); int npts = deflector.nbPoints(); // Allocate one additional point at each end = parametric value 0, 1 int size = 0; if (npts > 2) size = (npts-2)*2; size+=2; size*=3; array = new float[size]; int j=0; double[] values = adaptator.value(range[0]); array[j++] = (float) values[0]; array[j++] = (float) values[1]; array[j++] = (float) values[2]; // All intermediary points for (int i=1; i draw lines between the vertices and ignore curvature // best approximation we can do ArrayList aa = new ArrayList(); // store points here for (TopExp_Explorer explorer2 = new TopExp_Explorer(s, TopAbs_ShapeEnum.VERTEX); explorer2.more(); explorer2.next()) { TopoDS_Shape sv = explorer2.current(); if (!(sv instanceof TopoDS_Vertex)) continue; // should not happen! TopoDS_Vertex v = (TopoDS_Vertex)sv; aa.add(BRep_Tool.pnt(v)); } int size = 0; if (aa.size() > 2) size = (aa.size()-2)*2; size+=2; double[] f=(double[])aa.get(0); array = new float[size*3]; array[0]=(float) f[0]; array[1]=(float) f[1]; array[2]=(float) f[2]; for(int i=1, j=3; i