From: luukkainen Date: Mon, 29 Sep 2008 11:10:37 +0000 (+0000) Subject: latest release (0.41), third attempt X-Git-Tag: simantics-1.19.0~23 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=59072db6abab08dc6c8e382a3ad98783564d14d1;p=simantics%2F3d.git latest release (0.41), third attempt git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@6849 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.proconf.g3d.occ/.classpath b/org.simantics.proconf.g3d.occ/.classpath new file mode 100644 index 00000000..02159672 --- /dev/null +++ b/org.simantics.proconf.g3d.occ/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.proconf.g3d.occ/.project b/org.simantics.proconf.g3d.occ/.project new file mode 100644 index 00000000..83bfecce --- /dev/null +++ b/org.simantics.proconf.g3d.occ/.project @@ -0,0 +1,28 @@ + + + org.simantics.proconf.g3d.occ + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.simantics.proconf.g3d.occ/META-INF/MANIFEST.MF b/org.simantics.proconf.g3d.occ/META-INF/MANIFEST.MF new file mode 100644 index 00000000..82853e1c --- /dev/null +++ b/org.simantics.proconf.g3d.occ/META-INF/MANIFEST.MF @@ -0,0 +1,21 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: G3D OpenCascade Plug-in +Bundle-SymbolicName: org.simantics.proconf.g3d.occ;singleton:=true +Bundle-Version: 1.0.0 +Bundle-Activator: org.simantics.proconf.g3d.occ.Activator +Bundle-Vendor: VTT +Require-Bundle: org.eclipse.ui, + org.eclipse.core.runtime, + org.simantics.proconf.g3d, + com.jme.eclipse, + javax.vecmath, + org.jcae.opencascade.jni, + org.simantics.db, + org.simantics.layer0.stubs, + org.simantics.layer0.utils, + org.simantics.utils, + org.simantics.proconf.ui, + org.simantics.proconf.g3d.csg +Eclipse-LazyStart: true +Export-Package: org.simantics.proconf.g3d.occ.geometry diff --git a/org.simantics.proconf.g3d.occ/build.properties b/org.simantics.proconf.g3d.occ/build.properties new file mode 100644 index 00000000..6f20375d --- /dev/null +++ b/org.simantics.proconf.g3d.occ/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + .,\ + plugin.xml diff --git a/org.simantics.proconf.g3d.occ/plugin.xml b/org.simantics.proconf.g3d.occ/plugin.xml new file mode 100644 index 00000000..3784ffec --- /dev/null +++ b/org.simantics.proconf.g3d.occ/plugin.xml @@ -0,0 +1,12 @@ + + + + + + + + + diff --git a/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/Activator.java b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/Activator.java new file mode 100644 index 00000000..a4f7cd42 --- /dev/null +++ b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/Activator.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * 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 + *******************************************************************************/ +package org.simantics.proconf.g3d.occ; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; +import org.simantics.proconf.g3d.csg.stubs.CSGResource; +import org.simantics.db.Graph; +import org.simantics.db.GraphRequestAdapter; +import org.simantics.db.GraphRequestStatus; +import org.simantics.proconf.g3d.stubs.G3DResource; +import org.simantics.proconf.ui.ProConfUI; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.simantics.proconf.g3d.occ"; + + // The shared instance + private static Activator plugin; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) + */ + public void start(BundleContext context) throws Exception { + super.start(context); + plugin = this; + + ProConfUI.getSession().syncRead(new GraphRequestAdapter() { + @Override + public GraphRequestStatus perform(Graph g) throws Exception { + OccResources.csgResource = CSGResource.getInstance(g); + OccResources.g3dResource = G3DResource.getInstance(g); + return GraphRequestStatus.transactionComplete(); + } + }); + } + + /* + * (non-Javadoc) + * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) + */ + public void stop(BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/OccResources.java b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/OccResources.java new file mode 100644 index 00000000..1023fb66 --- /dev/null +++ b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/OccResources.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * 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 + *******************************************************************************/ +package org.simantics.proconf.g3d.occ; + +import org.simantics.proconf.g3d.csg.stubs.CSGResource; +import org.simantics.proconf.g3d.stubs.G3DResource; + + +public class OccResources { + public static CSGResource csgResource; + public static G3DResource g3dResource; +} diff --git a/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/IndexedGeometry.java b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/IndexedGeometry.java new file mode 100644 index 00000000..cf3732d8 --- /dev/null +++ b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/IndexedGeometry.java @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * 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 + *******************************************************************************/ +package org.simantics.proconf.g3d.occ.geometry; + + +public class IndexedGeometry +{ + private float[] coordinates=new float[0]; + private float[] tcoordinates=new float[0]; + private float[] normals=new float[0]; + private int[] indices = new int[0]; + private float[] colors=new float[0]; + + public IndexedGeometry() + { + } + + + + public int[] getIndices() + { + return indices; + } + + public float[] getCoordinates() + { + return coordinates; + } + + public float[] getTCoordinates() + { + return tcoordinates; + } + + + public void setIndices(int[] value) + { + indices=value; + } + + public void setCoordinates(float[] value) + { + coordinates=value; + } + + public void setTCoordinates(float[] value) + { + tcoordinates=value; + } + + public float[] getColors() + { + return colors; + } + + public void setColors(float[] arg) + { + colors=arg; + } + + + public float[] getNormals() + { + return normals; + } + + public void setNormals(float[] arg) + { + normals=arg; + } + + +} diff --git a/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/OccTriangulator.java b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/OccTriangulator.java new file mode 100644 index 00000000..ca5af06a --- /dev/null +++ b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/OccTriangulator.java @@ -0,0 +1,820 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * 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 + *******************************************************************************/ +package org.simantics.proconf.g3d.occ.geometry; + +import java.io.IOException; +import java.util.Collection; + +import javax.vecmath.AxisAngle4d; +import javax.vecmath.Tuple3d; +import javax.vecmath.Vector2d; + +import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge; +import org.jcae.opencascade.jni.BRepBuilderAPI_MakeFace; +import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire; +import org.jcae.opencascade.jni.BRepBuilderAPI_Transform; +import org.jcae.opencascade.jni.BRepOffsetAPI_ThruSections; +import org.jcae.opencascade.jni.BRepPrimAPI_MakePrism; +import org.jcae.opencascade.jni.BRepPrimAPI_MakeRevol; +import org.jcae.opencascade.jni.BRepPrimAPI_MakeTorus; +import org.jcae.opencascade.jni.BRep_Builder; +import org.jcae.opencascade.jni.GC_MakeArcOfCircle; +import org.jcae.opencascade.jni.GC_MakeSegment; +import org.jcae.opencascade.jni.GP_Elips; +import org.jcae.opencascade.jni.GP_Trsf; +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_Wire; +import org.simantics.proconf.g3d.csg.stubs.Barrel; +import org.simantics.proconf.g3d.csg.stubs.BooleanOperation; +import org.simantics.proconf.g3d.csg.stubs.Box; +import org.simantics.proconf.g3d.csg.stubs.CSGShape; +import org.simantics.proconf.g3d.csg.stubs.Cone; +import org.simantics.proconf.g3d.csg.stubs.Cylinder; +import org.simantics.proconf.g3d.csg.stubs.EllipticCylinder; +import org.simantics.proconf.g3d.csg.stubs.RectangularSolid; +import org.simantics.proconf.g3d.csg.stubs.RegularPrism; +import org.simantics.proconf.g3d.csg.stubs.Sphere; +import org.simantics.proconf.g3d.csg.stubs.Torus; +import org.simantics.layer0.utils.IEntity; +import org.simantics.proconf.g3d.base.G3DTools; +import org.simantics.proconf.g3d.base.GeometryProvider; +import org.simantics.proconf.g3d.occ.OccResources; +import org.simantics.utils.ErrorLogger; + +import com.jme.eclipse.test.ply.PLY_Loader; +import com.jme.scene.Geometry; +import com.jme.scene.Line; +import com.jme.scene.TriMesh; +import com.jme.util.geom.BufferUtils; + +public class OccTriangulator implements GeometryProvider{ + + + public static final double MIN_VALUE = 0.001; + + public OccTriangulator() { + + } + + + + public boolean canHandle(IEntity instance) { + if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) { + return true; + } + return false; + } + + public Geometry[] getGeometryFromResource(IEntity resource, boolean transform) { + if (resource.isInstanceOf(OccResources.csgResource.CSGShape)) { + TopoDS_Shape shape = OccTriangulator.getShapeFromResource(resource, transform); + Geometry[] g = OccTriangulator.getGeometry(shape); + shape.delete(); + return g; + } + return null; + } + + public boolean reconstructGeometry(IEntity instance, boolean transform, Geometry[] geometry) { + if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) { + TopoDS_Shape shape = OccTriangulator.getShapeFromResource(instance, transform); + boolean b = OccTriangulator.getGeometry(shape,geometry); + shape.delete(); + return b; + } + return false; + + } + + public static Geometry[] getGeometry(TopoDS_Shape tds_shape) { + Geometry g[] = new Geometry[]{new TriMesh(),new Line()}; + if (getGeometry(tds_shape, g)) + return g; + return null; + + } + + public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) { + ViewableShapeImpl shape = new ViewableShapeImpl(tds_shape); + //System.out.println("Geometries in shape " + shape.numGeometries()); + if (shape.numGeometries() > 0) { + int totalIndicesCount = 0; + int totalVerticesCount = 0; + boolean hasTCoords = true; + for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) { + IndexedGeometry geom = shape.getGeometry(geometryIndex); + totalIndicesCount += geom.getIndices().length; + totalVerticesCount += (geom.getCoordinates().length / 3); + if (geom.getTCoordinates() == null || geom.getTCoordinates().length == 0) + hasTCoords = false; + } + int currentVertex = 0; + int currentIndex = 0; + int indices[] = new int[totalIndicesCount]; + + + + //TriMesh mesh = new TriMesh(); + TriMesh mesh = (TriMesh)geometry[0]; + float data[] = new float[totalVerticesCount*3]; + float normals[] = new float[totalVerticesCount*3]; + float tcoords[] = null; + if (hasTCoords) + tcoords = new float[totalVerticesCount*2]; + for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) { + IndexedGeometry geom = shape.getGeometry(geometryIndex); + System.arraycopy(geom.getCoordinates(), 0, data, currentVertex*3, geom.getCoordinates().length); + System.arraycopy(geom.getNormals(), 0, normals, currentVertex*3, geom.getCoordinates().length); + if (hasTCoords) + System.arraycopy(geom.getTCoordinates(), 0, tcoords, currentVertex*2, geom.getTCoordinates().length - 1); + + for (int i = 0; i < geom.getIndices().length; i++) { + indices[currentIndex + i] = geom.getIndices()[i] + currentVertex; + } + + currentVertex += geom.getCoordinates().length/3; + currentIndex += geom.getIndices().length; + + } + mesh.reconstruct(BufferUtils.createFloatBuffer(data),BufferUtils.createFloatBuffer(normals) , null, hasTCoords ? BufferUtils.createFloatBuffer(tcoords) : null, BufferUtils.createIntBuffer(indices)); + + int numEdgeVertices = 0; + int currentEdgeVertex = 0; + for (int i = 0; i < shape.getNumEdges(); i++) { + numEdgeVertices += shape.getEdge(i).length/3; + } + + Line lines = (Line)geometry[1]; + data = new float[numEdgeVertices*3]; + for (int i = 0; i < shape.getNumEdges(); i++) { + int index = currentEdgeVertex *3; + System.arraycopy(shape.getEdge(i), 0,data, index, shape.getEdge(i).length); + currentEdgeVertex+= shape.getEdge(i).length/3; + } + lines.reconstruct(BufferUtils.createFloatBuffer(data), null, null, null); + + tds_shape.delete(); + return true; + } else { + tds_shape.delete(); + return false; + } + } + /* + public static Geometry[] getGeometry(TopoDS_Shape tds_shape) { + return getGeometry(tds_shape,true); + } + */ + /* + public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) { + return getGeometry(tds_shape,true, geometry); + } + */ + + private static TopoDS_Shape getPrimitiveFromResource(IEntity thing) { + if (!thing.isInstanceOf(OccResources.csgResource.Primitive)) + throw new IllegalArgumentException("Resource is not a primitive"); + TopoDS_Shape shape = null; + if (thing.isInstanceOf(OccResources.csgResource.Box)) { + Box box = new Box(thing); + double sx = box.getXAxisSize()[0]; + double sy = box.getYAxisSize()[0]; + double sz = box.getZAxisSize()[0]; + if (sx <= MIN_VALUE) + sx = MIN_VALUE; + if (sy <= MIN_VALUE) + sy = MIN_VALUE; + if (sz <= MIN_VALUE) + sz = MIN_VALUE; + + shape = makeBox(-sx * 0.5, -sy * 0.5, -sz * 0.5, sx * 0.5, sy * 0.5, sz * 0.5); + } else if (thing.isInstanceOf(OccResources.csgResource.Cone)) { + Cone cone = new Cone(thing); + double h = cone.getHeight()[0]; + double r1 = cone.getBottomRadius()[0]; + double r2 = cone.getTopRadius()[0]; + if (Math.abs(r1 - r2) > MIN_VALUE) { // OpenCASCADE won't work, + // if r1 == r2 + shape = makeCone(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1, + r2, h); + } else { + shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, + r1, h); + } + } else if (thing.isInstanceOf(OccResources.csgResource.Sphere)) { + Sphere sphere = new Sphere(thing); + double r = sphere.getRadius()[0]; + if (r <= MIN_VALUE) + r = MIN_VALUE; + shape = makeSphere(0.0, 0.0, 0.0, r); + } else if (thing.isInstanceOf(OccResources.csgResource.Torus)) { + Torus torus = new Torus(thing); + double r1 = torus.getMajorRadius()[0]; + double r2 = torus.getMinorRadius()[0]; + if (r1 <= MIN_VALUE) + r1 = MIN_VALUE; + if (r2 <= MIN_VALUE) + r2 = MIN_VALUE; + shape = makeTorus(new double[] { 0.0, 0.0, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1, r2); + } else if (thing.isInstanceOf(OccResources.csgResource.Cylinder)) { + Cylinder cylinder = new Cylinder(thing); + double h = cylinder.getHeight()[0]; + double r = cylinder.getRadius()[0]; + if (r <= MIN_VALUE) + r = MIN_VALUE; + if (h <= MIN_VALUE) + h = MIN_VALUE; + shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r, h); + } else if (thing.isInstanceOf(OccResources.csgResource.Barrel)) { + Barrel barrel = new Barrel(thing); + double h = barrel.getHeight()[0]; + if (h <= 0.0) + h = 0.01; + double r1 = barrel.getMinorRadius()[0]; + if (r1 <= MIN_VALUE) + r1 = MIN_VALUE; + double r2 = barrel.getMajorRadius()[0]; + if (r2 <= MIN_VALUE) + r2 = MIN_VALUE; + if (Math.abs(r1 -r2)< MIN_VALUE) + r2 = r1 + MIN_VALUE; + double p0[] = new double[]{0.0,-h*0.5,0.0}; + double p1[] = new double[]{0.0,-h*0.5,r1}; + double p2[] = new double[]{0.0, 0.0 ,r2}; + double p3[] = new double[]{0.0, h*0.5,r1}; + double p4[] = new double[]{0.0, h*0.5,0.0}; + GC_MakeArcOfCircle m = new GC_MakeArcOfCircle(p1,p2,p3); + GC_MakeSegment s1 = new GC_MakeSegment(p0,p1); + GC_MakeSegment s2 = new GC_MakeSegment(p3,p4); + TopoDS_Edge e1 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s1.value()).shape(); + TopoDS_Edge e2 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(m.value()).shape(); + TopoDS_Edge e3 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s2.value()).shape(); + TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(e1,e2,e3).shape(); + TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape(); + shape = new BRepPrimAPI_MakeRevol(F,new double[]{0.0,0.0,0.0,0.0,1.0,0.0}).shape(); + m.delete(); + s1.delete(); + s2.delete(); + e1.delete(); + e2.delete(); + e3.delete(); + w.delete(); + F.delete(); + } else if (thing.isInstanceOf(OccResources.csgResource.EllipticCylinder)) { + EllipticCylinder cylinder = new EllipticCylinder(thing); + double h = cylinder.getHeight()[0]; + if (h < MIN_VALUE) + h = MIN_VALUE; + double r2 = cylinder.getMinorRadius()[0]; + if (r2 < MIN_VALUE) + r2 = MIN_VALUE; + double r1 = cylinder.getMajorRadius()[0]; + if (r1 < MIN_VALUE) + r1 = MIN_VALUE; + GP_Elips ellipse; + if (r1 < r2) { + // FIXME : ellipse should be rotated, but current JNI won't allow it since Ax2 is not separate object + ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r2,r1); + } else { + ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r1,r2); + } + TopoDS_Edge ed = (TopoDS_Edge) new BRepBuilderAPI_MakeEdge(ellipse).shape(); + TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(ed).shape(); + TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape(); + shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape(); + ellipse.delete(); + ed.delete(); + w.delete(); + F.delete(); + } else if (thing.isInstanceOf(OccResources.csgResource.RegularPrism)) { + RegularPrism prism = new RegularPrism(thing); + int n = prism.getCorners()[0]; + if (n < 3) + n = 3; + double h = prism.getHeight()[0]; + if (h < MIN_VALUE) + h = MIN_VALUE; + double r = prism.getRadius()[0]; + if (r < MIN_VALUE) + r = MIN_VALUE; + Vector2d vertices[] = new Vector2d[n]; + for (int i = 0; i < n; i++) { + vertices[i] = new Vector2d(Math.sin(Math.PI * 2.0 * i / n)*r,Math.cos(Math.PI * 2.0 * i / n)*r); + } + BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire(); + for (int i = 0; i < n; i++) { + Vector2d v1 = vertices[i]; + Vector2d v2 = vertices[(i+1)%n]; + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{v1.x,-h*0.5,v1.y},new double[]{v2.x,-h*0.5,v2.y}).shape()); + } + TopoDS_Wire w = (TopoDS_Wire)wire.shape(); + TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape(); + shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape(); + wire.delete(); + w.delete(); + F.delete(); + } else if (thing.isInstanceOf(OccResources.csgResource.RectangularSolid)) { + RectangularSolid solid = new RectangularSolid(thing); + double x1 = solid.getXAxisMinimumSize()[0]; + double x2 = solid.getXAxisMaximumSize()[0]; + double y = solid.getYAxisSize()[0]; + double z1 = solid.getZAxisMinimumSize()[0]; + double z2 = solid.getZAxisMaximumSize()[0]; + if (x1 < MIN_VALUE) + x1 = MIN_VALUE; + if (x2 < MIN_VALUE) + x2 = MIN_VALUE; + if (y < MIN_VALUE) + y = MIN_VALUE; + if (z1 < MIN_VALUE) + z1 = MIN_VALUE; + if (z2 < MIN_VALUE) + z2 = MIN_VALUE; + x1 *= 0.5; + x2 *= 0.5; + y *= 0.5; + z1 *= 0.5; + z2 *= 0.5; + + BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire(); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,z1},new double[]{x1,-y,-z1}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,-z1},new double[]{-x1,-y,-z1}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,-z1},new double[]{-x1,-y,z1}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,z1},new double[]{x1,-y,z1}).shape()); + TopoDS_Wire w1 = (TopoDS_Wire)wire.shape(); + wire.delete(); + wire = new BRepBuilderAPI_MakeWire(); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,z2},new double[]{x2, y,-z2}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,-z2},new double[]{-x2, y,-z2}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,-z2},new double[]{-x2, y,z2}).shape()); + wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,z2},new double[]{x2, y,z2}).shape()); + TopoDS_Wire w2 = (TopoDS_Wire)wire.shape(); + BRepOffsetAPI_ThruSections generatorb = new BRepOffsetAPI_ThruSections(true, true); + generatorb.addWire(w1); + generatorb.addWire(w2); + generatorb.build(); + shape = generatorb.shape(); + wire.delete(); + w1.delete(); + w2.delete(); + } else { + throw new UnsupportedOperationException("Unsupported primitive"); + + } + return shape; + } + + private static TopoDS_Shape getBooleanOp(IEntity thing, TopoDS_Shape topoDSshape1, TopoDS_Shape topoDSshape2) { + if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation)) + throw new IllegalArgumentException("Resource is not a boolean operation"); + TopoDS_Shape shape = null; + int type; + if (thing.isInstanceOf(OccResources.csgResource.Difference)) { + type = 0; + } else if (thing.isInstanceOf(OccResources.csgResource.Union)) { + type = 1; + } else if (thing.isInstanceOf(OccResources.csgResource.Intersection)) { + type = 2; + } else { + throw new UnsupportedOperationException("Unsupported boolean operation"); + } + + switch (type) { + case 0: + shape = makeCut(topoDSshape1, topoDSshape2); + break; + case 1: + shape = makeFuse(topoDSshape1, topoDSshape2); + break; + case 2: + shape = makeCommon(topoDSshape1, topoDSshape2); + break; + } + topoDSshape1.delete(); + topoDSshape2.delete(); + return shape; + } + + private static TopoDS_Shape getBooleanOpFromResource(IEntity thing) { + if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation)) + throw new IllegalArgumentException("Resource is not a boolean operation"); + + TopoDS_Shape shape = null; + TopoDS_Shape topoDSshape1 = null; + TopoDS_Shape topoDSshape2 = null; + BooleanOperation op = new BooleanOperation(thing); + CSGShape mainShape = op.getMainShape(); + Collection secondaryShapes = op.getSecondaryShape(); + if (mainShape == null || secondaryShapes.size() < 1) { + throw new RuntimeException("Cannot find requested Shapes for boolean operation"); + } + topoDSshape1 = getShapeFromResource(mainShape); + for (CSGShape shape2 : secondaryShapes) { + topoDSshape2 = getShapeFromResource(shape2); + shape = getBooleanOp(thing,topoDSshape1,topoDSshape2); + topoDSshape1.delete(); + topoDSshape1 = shape; + } + + topoDSshape2.delete(); + return shape; + } + + private static TopoDS_Shape getShapeFromResource(IEntity thing) { + return getShapeFromResource(thing, true); + } + + private static TopoDS_Shape getShapeFromResource(IEntity thing, boolean transform) { + if (thing.isInstanceOf(OccResources.csgResource.CSGShape)) { + TopoDS_Shape shape = null; + + if (thing.isInstanceOf(OccResources.csgResource.Primitive)) { + shape = getPrimitiveFromResource(thing); + + } else if (thing.isInstanceOf(OccResources.csgResource.BooleanOperation)) { + shape = getBooleanOpFromResource(thing); + + } else { + throw new UnsupportedOperationException("Shape must be a primitive or a boolean operation"); + } + Tuple3d c = null; + CSGShape shapeType = new CSGShape(thing); + + if (shapeType.getCenter() != null) + c = G3DTools.getVector(shapeType.getCenter()); + + TopoDS_Shape tShape = null; + if(c != null) { + tShape = makeTranslation(shape, c.x, c.y, c.z); + shape.delete(); + shape = tShape; + } + if (transform) { + //CSGShape shapeType = CSGShapeFactory.create(resource); + Tuple3d p = G3DTools.getVector(shapeType.getLocalPosition()); + AxisAngle4d r = G3DTools.getOrientation(shapeType.getLocalOrientation()); + + if (Math.abs(r.angle) > 0.01) { + tShape = makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle); + shape.delete(); + shape = tShape; + } + tShape = makeTranslation(shape, p.x, p.y, p.z); + shape.delete(); + shape = tShape; + } + + return shape; + + } + throw new UnsupportedOperationException("Resource is not a shape"); + } + + private static TopoDS_Shape getShapeFromFile(String filename) { + assert (filename != null); + if (filename.endsWith("stp")) { + TopoDS_Shape shape = importSTEP(filename); + return shape; + } else if (filename.endsWith("step")) { + TopoDS_Shape shape = importSTEP(filename); + return shape; + } else if (filename.endsWith("iges")) { + TopoDS_Shape shape = importIGES(filename); + return shape; + } else if (filename.endsWith("brep")) { + TopoDS_Shape shape = importBREP(filename); + return shape; + } else { + throw new UnsupportedOperationException("Unsupported format " + filename); + } + + } + + + + public static Geometry[] getGeometryFromFile(String filename) { + if (filename.endsWith("ply")) { + try { + return new Geometry[]{PLY_Loader.loadPLY(filename)}; + } catch (IOException e) { + ErrorLogger.defaultLogError(e); + return null; + } + } + TopoDS_Shape shape = OccTriangulator.getShapeFromFile(filename); + Geometry g[] = OccTriangulator.getGeometry(shape); + shape.delete(); + return g; + } + + public static TopoDS_Shape importBREP(String filename) { + org.jcae.opencascade.jni.BRep_Builder aBuilder = new org.jcae.opencascade.jni.BRep_Builder(); + org.jcae.opencascade.jni.TopoDS_Shape myShape = org.jcae.opencascade.jni.BRepTools.read(filename, aBuilder); + aBuilder.delete(); + return myShape; + } + + public static TopoDS_Shape importIGES(String filename) { + org.jcae.opencascade.jni.IGESControl_Reader aReader = new org.jcae.opencascade.jni.IGESControl_Reader(); + aReader.readFile(filename); + aReader.clearShapes(); + aReader.transferRoots(); + TopoDS_Shape result = aReader.oneShape(); + aReader.delete(); + return result; + } + + public static TopoDS_Shape importSTEP(String filename) { + org.jcae.opencascade.jni.STEPControl_Reader aReader = new org.jcae.opencascade.jni.STEPControl_Reader(); + aReader.readFile(filename); + aReader.clearShapes(); + aReader.transferRoots(); + TopoDS_Shape result = aReader.oneShape(); + aReader.delete(); + return result; + } + + public static TopoDS_Shape archimede(TopoDS_Shape topoDS_Shape, double param, double param2, double param3) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape suppressHolesInFaceOrShell(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape suppressHole(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1, + TopoDS_Shape[] topoDS_Shape2, TopoDS_Shape[] topoDS_Shape3) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape[] suppressFaces(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape[] subShapeAll(TopoDS_Shape topoDS_Shape, short param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape subShape(TopoDS_Shape topoDS_Shape, short param, TopoDS_Shape[] topoDS_Shape2) { + throw new UnsupportedOperationException(); + } + + public static boolean setBlock(TopoDS_Shape topoDS_Shape) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape partitionKeepFaces(TopoDS_Shape[] topoDS_Shape, TopoDS_Shape topoDS_Shape1, boolean param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape orientationChange(TopoDS_Shape topoDS_Shape) { + throw new UnsupportedOperationException(); + } + + public static short nbLabels() { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeWire(TopoDS_Shape[] topoDS_Shape) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeVertex(double param, double param1, double param2) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeVector(double[] pointStruct, double[] pointStruct1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeTranslation(TopoDS_Shape aShape, double x, double y, double z) { + GP_Trsf theTransformation = new GP_Trsf(); + theTransformation.setTranslation(new double[] { x, y, z }); + BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true); + + TopoDS_Shape shape = bt.shape(); + bt.delete(); + theTransformation.delete(); + return shape; + } + + public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2) { + double[] axe = new double[6]; + System.arraycopy(pointStruct, 0, axe, 0, 3); + System.arraycopy(dirStruct, 0, axe, 3, 3); + org.jcae.opencascade.jni.TopoDS_Shape tds = new BRepPrimAPI_MakeTorus(axe, r1, r2).shape(); + return tds; + } + + public static TopoDS_Shape makeSphere(double x, double y, double z, double radius) { + double[] c = new double[] { x, y, z }; + org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere(c, radius) + .shape(); + return tds; + } + + public static TopoDS_Shape makeSewingShape(TopoDS_Shape topoDS_Shape, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeSewing(TopoDS_Shape[] topoDS_Shape, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeScaleTransform(TopoDS_Shape topoDS_Shape, double[] pointStruct, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeRotation(TopoDS_Shape aShape, double[] axisStruct, double angle) { + + GP_Trsf theTransformation = new GP_Trsf(); + theTransformation.setRotation(axisStruct, angle); + BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true); + TopoDS_Shape shape = bt.shape(); + bt.delete(); + theTransformation.delete(); + return shape; + } + + public static TopoDS_Shape makeRevolution(TopoDS_Shape topoDS_Shape, double[] axisStruct, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makePrism(TopoDS_Shape topoDS_Shape, double[] pointStruct, double[] pointStruct2) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makePlane(double[] pointStruct, double[] dirStruct, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makePlacedBox(double param, double param1, double param2, double param3, double param4, + double param5) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makePipe(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makePanel(TopoDS_Shape topoDS_Shape, short param, double param2) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeMultiTranslation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param, + short param3, double[] dirStruct4, double param5, short param6) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeMultiTranslation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param, short param3) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeMultiRotation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct, + double param, short param4, double param5, short param6) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeMultiRotation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct, + short param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeMirrorByPlane(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeLine(double[] pointStruct, double[] dirStruct) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeFilling(TopoDS_Shape topoDS_Shape, short param, short param2, double param3, double param4, + short param5) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeFillet(TopoDS_Shape topoDS_Shape, double param, short param2, TopoDS_Shape[] topoDS_Shape3) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeFace(TopoDS_Shape topoDS_Shape, boolean param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeEdge(double[] pointStruct, double[] pointStruct1) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeCylinder(double[] pointStruct, double[] dirStruct, double radius, double height) { + double[] axe = new double[6]; + System.arraycopy(pointStruct, 0, axe, 0, 3); + System.arraycopy(dirStruct, 0, axe, 3, 3); + org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius, + height, 2 * Math.PI).shape(); + return tds; + } + + public static TopoDS_Shape makeCopy(TopoDS_Shape topoDS_Shape) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeCone(double[] pointStruct, double[] dirStruct, double radius1, double radius2, double height) { + double[] axe = new double[6]; + System.arraycopy(pointStruct, 0, axe, 0, 3); + System.arraycopy(dirStruct, 0, axe, 3, 3); + org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCone(axe, radius1, + radius2, height, 2 * Math.PI).shape(); + return tds; + } + + public static TopoDS_Shape makeCompound(TopoDS_Shape[] shapes) { + BRep_Builder builder = new BRep_Builder(); + org.jcae.opencascade.jni.TopoDS_Compound comp = new org.jcae.opencascade.jni.TopoDS_Compound(); + builder.makeCompound(comp); + for (int i = 0; i < shapes.length; i++) { + + builder.add(comp, shapes[i]); + } + builder.delete(); + return comp; + } + + public static TopoDS_Shape makeCircle(double[] pointStruct, double[] dirStruct, double param) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeChamfer(TopoDS_Shape topoDS_Shape, double param, double param2, short param3, + TopoDS_Shape[] topoDS_Shape4) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeCDG(TopoDS_Shape topoDS_Shape) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeBox(double x1, double y1, double z1, double x2, double y2, double z2) { + double[] p1 = new double[] { x1, y1, z1 }; + double[] p2 = new double[] { x2, y2, z2 }; + org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeBox(p1, p2).shape(); + return tds; + } + + public static TopoDS_Shape makeCut(TopoDS_Shape shape1, TopoDS_Shape shape2) { + + org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Cut(shape1, shape2).shape(); + return s; + } + + public static TopoDS_Shape makeCommon(TopoDS_Shape shape1, TopoDS_Shape shape2) { + + org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Common(shape1, shape2).shape(); + return s; + } + + public static TopoDS_Shape makeFuse(TopoDS_Shape shape1, TopoDS_Shape shape2) { + + org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Fuse(shape1, shape2).shape(); + return s; + } + + public static TopoDS_Shape makeArc(double[] pointStruct, double[] pointStruct1, double[] pointStruct2) { + throw new UnsupportedOperationException(); + } + + public static TopoDS_Shape makeWedge(double[] pointStruct, double[] dirStruct,double dx, double dy, double dz, double xmin, double zmin, double xmax, double zmax) { + double[] axe = new double[6]; + System.arraycopy(pointStruct, 0, axe, 0, 3); + System.arraycopy(dirStruct, 0, axe, 3, 3); + org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge(axe, dx, dy, dz, xmin, zmin, xmax, zmax).shape(); + return s; + } + + + + public static void exportBREP(TopoDS_Shape shape, String filename) { + org.jcae.opencascade.jni.BRepTools.write(shape, filename); + } + + public static void exportIGES(TopoDS_Shape shape, String filename) { + throw new UnsupportedOperationException(); + +// IGESControl_Writer writer=new IGESControl_Writer(); +// writer.addShape(shape); writer.write(filename,false); + + } + + + + + +} diff --git a/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/ViewableShapeImpl.java b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/ViewableShapeImpl.java new file mode 100644 index 00000000..e08c9b73 --- /dev/null +++ b/org.simantics.proconf.g3d.occ/src/org/simantics/proconf/g3d/occ/geometry/ViewableShapeImpl.java @@ -0,0 +1,498 @@ +/******************************************************************************* + * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. + * 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