--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.occ.geometry;\r
+\r
+import java.io.IOException;\r
+import java.util.Collection;\r
+\r
+import javax.vecmath.AxisAngle4d;\r
+import javax.vecmath.Tuple3d;\r
+import javax.vecmath.Vector2d;\r
+\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeEdge;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeFace;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_MakeWire;\r
+import org.jcae.opencascade.jni.BRepBuilderAPI_Transform;\r
+import org.jcae.opencascade.jni.BRepOffsetAPI_ThruSections;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakePrism;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakeRevol;\r
+import org.jcae.opencascade.jni.BRepPrimAPI_MakeTorus;\r
+import org.jcae.opencascade.jni.BRep_Builder;\r
+import org.jcae.opencascade.jni.GC_MakeArcOfCircle;\r
+import org.jcae.opencascade.jni.GC_MakeSegment;\r
+import org.jcae.opencascade.jni.GP_Elips;\r
+import org.jcae.opencascade.jni.GP_Trsf;\r
+import org.jcae.opencascade.jni.TopoDS_Edge;\r
+import org.jcae.opencascade.jni.TopoDS_Face;\r
+import org.jcae.opencascade.jni.TopoDS_Shape;\r
+import org.jcae.opencascade.jni.TopoDS_Wire;\r
+import org.simantics.proconf.g3d.csg.stubs.Barrel;\r
+import org.simantics.proconf.g3d.csg.stubs.BooleanOperation;\r
+import org.simantics.proconf.g3d.csg.stubs.Box;\r
+import org.simantics.proconf.g3d.csg.stubs.CSGShape;\r
+import org.simantics.proconf.g3d.csg.stubs.Cone;\r
+import org.simantics.proconf.g3d.csg.stubs.Cylinder;\r
+import org.simantics.proconf.g3d.csg.stubs.EllipticCylinder;\r
+import org.simantics.proconf.g3d.csg.stubs.RectangularSolid;\r
+import org.simantics.proconf.g3d.csg.stubs.RegularPrism;\r
+import org.simantics.proconf.g3d.csg.stubs.Sphere;\r
+import org.simantics.proconf.g3d.csg.stubs.Torus;\r
+import org.simantics.layer0.utils.IEntity;\r
+import org.simantics.proconf.g3d.base.G3DTools;\r
+import org.simantics.proconf.g3d.base.GeometryProvider;\r
+import org.simantics.proconf.g3d.occ.OccResources;\r
+import org.simantics.utils.ErrorLogger;\r
+\r
+import com.jme.eclipse.test.ply.PLY_Loader;\r
+import com.jme.scene.Geometry;\r
+import com.jme.scene.Line;\r
+import com.jme.scene.TriMesh;\r
+import com.jme.util.geom.BufferUtils;\r
+\r
+public class OccTriangulator implements GeometryProvider{\r
+\r
+ \r
+ public static final double MIN_VALUE = 0.001;\r
+ \r
+ public OccTriangulator() {\r
+ \r
+ }\r
+ \r
+ \r
+ \r
+ public boolean canHandle(IEntity instance) {\r
+ if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ public Geometry[] getGeometryFromResource(IEntity resource, boolean transform) {\r
+ if (resource.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromResource(resource, transform);\r
+ Geometry[] g = OccTriangulator.getGeometry(shape);\r
+ shape.delete();\r
+ return g;\r
+ } \r
+ return null;\r
+ }\r
+ \r
+ public boolean reconstructGeometry(IEntity instance, boolean transform, Geometry[] geometry) {\r
+ if (instance.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromResource(instance, transform);\r
+ boolean b = OccTriangulator.getGeometry(shape,geometry);\r
+ shape.delete();\r
+ return b;\r
+ } \r
+ return false;\r
+ \r
+ }\r
+\r
+ public static Geometry[] getGeometry(TopoDS_Shape tds_shape) {\r
+ Geometry g[] = new Geometry[]{new TriMesh(),new Line()};\r
+ if (getGeometry(tds_shape, g))\r
+ return g;\r
+ return null;\r
+ \r
+ }\r
+ \r
+ public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) {\r
+ ViewableShapeImpl shape = new ViewableShapeImpl(tds_shape);\r
+ //System.out.println("Geometries in shape " + shape.numGeometries());\r
+ if (shape.numGeometries() > 0) {\r
+ int totalIndicesCount = 0;\r
+ int totalVerticesCount = 0;\r
+ boolean hasTCoords = true;\r
+ for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) {\r
+ IndexedGeometry geom = shape.getGeometry(geometryIndex);\r
+ totalIndicesCount += geom.getIndices().length;\r
+ totalVerticesCount += (geom.getCoordinates().length / 3);\r
+ if (geom.getTCoordinates() == null || geom.getTCoordinates().length == 0)\r
+ hasTCoords = false;\r
+ }\r
+ int currentVertex = 0;\r
+ int currentIndex = 0;\r
+ int indices[] = new int[totalIndicesCount];\r
+ \r
+ \r
+ \r
+ //TriMesh mesh = new TriMesh();\r
+ TriMesh mesh = (TriMesh)geometry[0];\r
+ float data[] = new float[totalVerticesCount*3];\r
+ float normals[] = new float[totalVerticesCount*3];\r
+ float tcoords[] = null;\r
+ if (hasTCoords)\r
+ tcoords = new float[totalVerticesCount*2];\r
+ for (int geometryIndex = 0; geometryIndex < shape.numGeometries(); geometryIndex++) {\r
+ IndexedGeometry geom = shape.getGeometry(geometryIndex); \r
+ System.arraycopy(geom.getCoordinates(), 0, data, currentVertex*3, geom.getCoordinates().length);\r
+ System.arraycopy(geom.getNormals(), 0, normals, currentVertex*3, geom.getCoordinates().length);\r
+ if (hasTCoords)\r
+ System.arraycopy(geom.getTCoordinates(), 0, tcoords, currentVertex*2, geom.getTCoordinates().length - 1);\r
+ \r
+ for (int i = 0; i < geom.getIndices().length; i++) {\r
+ indices[currentIndex + i] = geom.getIndices()[i] + currentVertex;\r
+ }\r
+ \r
+ currentVertex += geom.getCoordinates().length/3;\r
+ currentIndex += geom.getIndices().length;\r
+ \r
+ }\r
+ mesh.reconstruct(BufferUtils.createFloatBuffer(data),BufferUtils.createFloatBuffer(normals) , null, hasTCoords ? BufferUtils.createFloatBuffer(tcoords) : null, BufferUtils.createIntBuffer(indices));\r
+ \r
+ int numEdgeVertices = 0;\r
+ int currentEdgeVertex = 0;\r
+ for (int i = 0; i < shape.getNumEdges(); i++) {\r
+ numEdgeVertices += shape.getEdge(i).length/3;\r
+ }\r
+\r
+ Line lines = (Line)geometry[1];\r
+ data = new float[numEdgeVertices*3];\r
+ for (int i = 0; i < shape.getNumEdges(); i++) {\r
+ int index = currentEdgeVertex *3;\r
+ System.arraycopy(shape.getEdge(i), 0,data, index, shape.getEdge(i).length);\r
+ currentEdgeVertex+= shape.getEdge(i).length/3;\r
+ }\r
+ lines.reconstruct(BufferUtils.createFloatBuffer(data), null, null, null);\r
+ \r
+ tds_shape.delete();\r
+ return true;\r
+ } else {\r
+ tds_shape.delete();\r
+ return false;\r
+ }\r
+ }\r
+ /*\r
+ public static Geometry[] getGeometry(TopoDS_Shape tds_shape) {\r
+ return getGeometry(tds_shape,true);\r
+ }\r
+ */\r
+ /*\r
+ public static boolean getGeometry(TopoDS_Shape tds_shape, Geometry[] geometry) {\r
+ return getGeometry(tds_shape,true, geometry);\r
+ }\r
+ */\r
+ \r
+ private static TopoDS_Shape getPrimitiveFromResource(IEntity thing) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.Primitive)) \r
+ throw new IllegalArgumentException("Resource is not a primitive");\r
+ TopoDS_Shape shape = null;\r
+ if (thing.isInstanceOf(OccResources.csgResource.Box)) {\r
+ Box box = new Box(thing);\r
+ double sx = box.getXAxisSize()[0];\r
+ double sy = box.getYAxisSize()[0];\r
+ double sz = box.getZAxisSize()[0];\r
+ if (sx <= MIN_VALUE)\r
+ sx = MIN_VALUE;\r
+ if (sy <= MIN_VALUE)\r
+ sy = MIN_VALUE;\r
+ if (sz <= MIN_VALUE)\r
+ sz = MIN_VALUE;\r
+ \r
+ shape = makeBox(-sx * 0.5, -sy * 0.5, -sz * 0.5, sx * 0.5, sy * 0.5, sz * 0.5);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Cone)) {\r
+ Cone cone = new Cone(thing);\r
+ double h = cone.getHeight()[0];\r
+ double r1 = cone.getBottomRadius()[0];\r
+ double r2 = cone.getTopRadius()[0];\r
+ if (Math.abs(r1 - r2) > MIN_VALUE) { // OpenCASCADE won't work,\r
+ // if r1 == r2\r
+ shape = makeCone(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1,\r
+ r2, h);\r
+ } else {\r
+ shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 },\r
+ r1, h);\r
+ }\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Sphere)) {\r
+ Sphere sphere = new Sphere(thing);\r
+ double r = sphere.getRadius()[0];\r
+ if (r <= MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ shape = makeSphere(0.0, 0.0, 0.0, r);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Torus)) {\r
+ Torus torus = new Torus(thing);\r
+ double r1 = torus.getMajorRadius()[0];\r
+ double r2 = torus.getMinorRadius()[0];\r
+ if (r1 <= MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ if (r2 <= MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ shape = makeTorus(new double[] { 0.0, 0.0, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r1, r2);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Cylinder)) {\r
+ Cylinder cylinder = new Cylinder(thing);\r
+ double h = cylinder.getHeight()[0];\r
+ double r = cylinder.getRadius()[0];\r
+ if (r <= MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ if (h <= MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ shape = makeCylinder(new double[] { 0.0, -h * 0.5, 0.0 }, new double[] { 0.0, 1.0, 0.0 }, r, h);\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Barrel)) {\r
+ Barrel barrel = new Barrel(thing);\r
+ double h = barrel.getHeight()[0];\r
+ if (h <= 0.0)\r
+ h = 0.01;\r
+ double r1 = barrel.getMinorRadius()[0];\r
+ if (r1 <= MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ double r2 = barrel.getMajorRadius()[0];\r
+ if (r2 <= MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ if (Math.abs(r1 -r2)< MIN_VALUE)\r
+ r2 = r1 + MIN_VALUE;\r
+ double p0[] = new double[]{0.0,-h*0.5,0.0};\r
+ double p1[] = new double[]{0.0,-h*0.5,r1};\r
+ double p2[] = new double[]{0.0, 0.0 ,r2};\r
+ double p3[] = new double[]{0.0, h*0.5,r1};\r
+ double p4[] = new double[]{0.0, h*0.5,0.0};\r
+ GC_MakeArcOfCircle m = new GC_MakeArcOfCircle(p1,p2,p3);\r
+ GC_MakeSegment s1 = new GC_MakeSegment(p0,p1);\r
+ GC_MakeSegment s2 = new GC_MakeSegment(p3,p4);\r
+ TopoDS_Edge e1 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s1.value()).shape();\r
+ TopoDS_Edge e2 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(m.value()).shape();\r
+ TopoDS_Edge e3 = (TopoDS_Edge)new BRepBuilderAPI_MakeEdge(s2.value()).shape();\r
+ TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(e1,e2,e3).shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakeRevol(F,new double[]{0.0,0.0,0.0,0.0,1.0,0.0}).shape();\r
+ m.delete();\r
+ s1.delete();\r
+ s2.delete();\r
+ e1.delete();\r
+ e2.delete();\r
+ e3.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.EllipticCylinder)) {\r
+ EllipticCylinder cylinder = new EllipticCylinder(thing);\r
+ double h = cylinder.getHeight()[0];\r
+ if (h < MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ double r2 = cylinder.getMinorRadius()[0];\r
+ if (r2 < MIN_VALUE)\r
+ r2 = MIN_VALUE;\r
+ double r1 = cylinder.getMajorRadius()[0];\r
+ if (r1 < MIN_VALUE)\r
+ r1 = MIN_VALUE;\r
+ GP_Elips ellipse;\r
+ if (r1 < r2) {\r
+ // FIXME : ellipse should be rotated, but current JNI won't allow it since Ax2 is not separate object\r
+ ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r2,r1);\r
+ } else {\r
+ ellipse = new GP_Elips(new double[]{0.0,-h*0.5,0.0,0.0,1.0,0.0},r1,r2);\r
+ }\r
+ TopoDS_Edge ed = (TopoDS_Edge) new BRepBuilderAPI_MakeEdge(ellipse).shape();\r
+ TopoDS_Wire w = (TopoDS_Wire) new BRepBuilderAPI_MakeWire(ed).shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape();\r
+ ellipse.delete();\r
+ ed.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.RegularPrism)) {\r
+ RegularPrism prism = new RegularPrism(thing);\r
+ int n = prism.getCorners()[0];\r
+ if (n < 3)\r
+ n = 3;\r
+ double h = prism.getHeight()[0];\r
+ if (h < MIN_VALUE)\r
+ h = MIN_VALUE;\r
+ double r = prism.getRadius()[0];\r
+ if (r < MIN_VALUE)\r
+ r = MIN_VALUE;\r
+ Vector2d vertices[] = new Vector2d[n];\r
+ for (int i = 0; i < n; i++) {\r
+ vertices[i] = new Vector2d(Math.sin(Math.PI * 2.0 * i / n)*r,Math.cos(Math.PI * 2.0 * i / n)*r);\r
+ }\r
+ BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();\r
+ for (int i = 0; i < n; i++) {\r
+ Vector2d v1 = vertices[i];\r
+ Vector2d v2 = vertices[(i+1)%n];\r
+ 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());\r
+ }\r
+ TopoDS_Wire w = (TopoDS_Wire)wire.shape();\r
+ TopoDS_Face F = (TopoDS_Face) new BRepBuilderAPI_MakeFace(w).shape();\r
+ shape = new BRepPrimAPI_MakePrism(F, new double[] { 0.0, h, 0.0 }).shape(); \r
+ wire.delete();\r
+ w.delete();\r
+ F.delete();\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.RectangularSolid)) {\r
+ RectangularSolid solid = new RectangularSolid(thing);\r
+ double x1 = solid.getXAxisMinimumSize()[0];\r
+ double x2 = solid.getXAxisMaximumSize()[0];\r
+ double y = solid.getYAxisSize()[0];\r
+ double z1 = solid.getZAxisMinimumSize()[0];\r
+ double z2 = solid.getZAxisMaximumSize()[0];\r
+ if (x1 < MIN_VALUE)\r
+ x1 = MIN_VALUE;\r
+ if (x2 < MIN_VALUE)\r
+ x2 = MIN_VALUE;\r
+ if (y < MIN_VALUE)\r
+ y = MIN_VALUE;\r
+ if (z1 < MIN_VALUE)\r
+ z1 = MIN_VALUE;\r
+ if (z2 < MIN_VALUE)\r
+ z2 = MIN_VALUE;\r
+ x1 *= 0.5;\r
+ x2 *= 0.5;\r
+ y *= 0.5;\r
+ z1 *= 0.5;\r
+ z2 *= 0.5;\r
+ \r
+ BRepBuilderAPI_MakeWire wire = new BRepBuilderAPI_MakeWire();\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,z1},new double[]{x1,-y,-z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x1,-y,-z1},new double[]{-x1,-y,-z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,-z1},new double[]{-x1,-y,z1}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x1,-y,z1},new double[]{x1,-y,z1}).shape());\r
+ TopoDS_Wire w1 = (TopoDS_Wire)wire.shape();\r
+ wire.delete();\r
+ wire = new BRepBuilderAPI_MakeWire();\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,z2},new double[]{x2, y,-z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{x2, y,-z2},new double[]{-x2, y,-z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,-z2},new double[]{-x2, y,z2}).shape());\r
+ wire.add((TopoDS_Edge)new BRepBuilderAPI_MakeEdge(new double[]{-x2, y,z2},new double[]{x2, y,z2}).shape());\r
+ TopoDS_Wire w2 = (TopoDS_Wire)wire.shape();\r
+ BRepOffsetAPI_ThruSections generatorb = new BRepOffsetAPI_ThruSections(true, true);\r
+ generatorb.addWire(w1);\r
+ generatorb.addWire(w2);\r
+ generatorb.build();\r
+ shape = generatorb.shape();\r
+ wire.delete();\r
+ w1.delete();\r
+ w2.delete();\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported primitive");\r
+\r
+ }\r
+ return shape;\r
+ }\r
+ \r
+ private static TopoDS_Shape getBooleanOp(IEntity thing, TopoDS_Shape topoDSshape1, TopoDS_Shape topoDSshape2) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation))\r
+ throw new IllegalArgumentException("Resource is not a boolean operation");\r
+ TopoDS_Shape shape = null;\r
+ int type;\r
+ if (thing.isInstanceOf(OccResources.csgResource.Difference)) {\r
+ type = 0;\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Union)) {\r
+ type = 1;\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.Intersection)) {\r
+ type = 2;\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported boolean operation");\r
+ }\r
+ \r
+ switch (type) {\r
+ case 0:\r
+ shape = makeCut(topoDSshape1, topoDSshape2);\r
+ break;\r
+ case 1:\r
+ shape = makeFuse(topoDSshape1, topoDSshape2);\r
+ break;\r
+ case 2:\r
+ shape = makeCommon(topoDSshape1, topoDSshape2);\r
+ break;\r
+ }\r
+ topoDSshape1.delete();\r
+ topoDSshape2.delete();\r
+ return shape;\r
+ }\r
+ \r
+ private static TopoDS_Shape getBooleanOpFromResource(IEntity thing) {\r
+ if (!thing.isInstanceOf(OccResources.csgResource.BooleanOperation))\r
+ throw new IllegalArgumentException("Resource is not a boolean operation");\r
+ \r
+ TopoDS_Shape shape = null;\r
+ TopoDS_Shape topoDSshape1 = null;\r
+ TopoDS_Shape topoDSshape2 = null;\r
+ BooleanOperation op = new BooleanOperation(thing);\r
+ CSGShape mainShape = op.getMainShape();\r
+ Collection<CSGShape> secondaryShapes = op.getSecondaryShape();\r
+ if (mainShape == null || secondaryShapes.size() < 1) {\r
+ throw new RuntimeException("Cannot find requested Shapes for boolean operation");\r
+ }\r
+ topoDSshape1 = getShapeFromResource(mainShape);\r
+ for (CSGShape shape2 : secondaryShapes) {\r
+ topoDSshape2 = getShapeFromResource(shape2);\r
+ shape = getBooleanOp(thing,topoDSshape1,topoDSshape2);\r
+ topoDSshape1.delete();\r
+ topoDSshape1 = shape;\r
+ }\r
+ \r
+ topoDSshape2.delete();\r
+ return shape;\r
+ }\r
+\r
+ private static TopoDS_Shape getShapeFromResource(IEntity thing) {\r
+ return getShapeFromResource(thing, true);\r
+ }\r
+ \r
+ private static TopoDS_Shape getShapeFromResource(IEntity thing, boolean transform) {\r
+ if (thing.isInstanceOf(OccResources.csgResource.CSGShape)) {\r
+ TopoDS_Shape shape = null;\r
+ \r
+ if (thing.isInstanceOf(OccResources.csgResource.Primitive)) {\r
+ shape = getPrimitiveFromResource(thing);\r
+\r
+ } else if (thing.isInstanceOf(OccResources.csgResource.BooleanOperation)) {\r
+ shape = getBooleanOpFromResource(thing);\r
+\r
+ } else {\r
+ throw new UnsupportedOperationException("Shape must be a primitive or a boolean operation");\r
+ }\r
+ Tuple3d c = null;\r
+ CSGShape shapeType = new CSGShape(thing);\r
+ \r
+ if (shapeType.getCenter() != null)\r
+ c = G3DTools.getVector(shapeType.getCenter());\r
+ \r
+ TopoDS_Shape tShape = null;\r
+ if(c != null) {\r
+ tShape = makeTranslation(shape, c.x, c.y, c.z);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+ if (transform) {\r
+ //CSGShape shapeType = CSGShapeFactory.create(resource);\r
+ Tuple3d p = G3DTools.getVector(shapeType.getLocalPosition());\r
+ AxisAngle4d r = G3DTools.getOrientation(shapeType.getLocalOrientation());\r
+ \r
+ if (Math.abs(r.angle) > 0.01) {\r
+ tShape = makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+ tShape = makeTranslation(shape, p.x, p.y, p.z);\r
+ shape.delete();\r
+ shape = tShape;\r
+ }\r
+\r
+ return shape;\r
+\r
+ }\r
+ throw new UnsupportedOperationException("Resource is not a shape");\r
+ }\r
+\r
+ private static TopoDS_Shape getShapeFromFile(String filename) {\r
+ assert (filename != null);\r
+ if (filename.endsWith("stp")) {\r
+ TopoDS_Shape shape = importSTEP(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("step")) {\r
+ TopoDS_Shape shape = importSTEP(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("iges")) {\r
+ TopoDS_Shape shape = importIGES(filename);\r
+ return shape;\r
+ } else if (filename.endsWith("brep")) {\r
+ TopoDS_Shape shape = importBREP(filename);\r
+ return shape;\r
+ } else {\r
+ throw new UnsupportedOperationException("Unsupported format " + filename);\r
+ }\r
+\r
+ }\r
+\r
+ \r
+\r
+ public static Geometry[] getGeometryFromFile(String filename) {\r
+ if (filename.endsWith("ply")) {\r
+ try {\r
+ return new Geometry[]{PLY_Loader.loadPLY(filename)};\r
+ } catch (IOException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ return null;\r
+ }\r
+ }\r
+ TopoDS_Shape shape = OccTriangulator.getShapeFromFile(filename);\r
+ Geometry g[] = OccTriangulator.getGeometry(shape);\r
+ shape.delete();\r
+ return g;\r
+ }\r
+ \r
+ public static TopoDS_Shape importBREP(String filename) {\r
+ org.jcae.opencascade.jni.BRep_Builder aBuilder = new org.jcae.opencascade.jni.BRep_Builder();\r
+ org.jcae.opencascade.jni.TopoDS_Shape myShape = org.jcae.opencascade.jni.BRepTools.read(filename, aBuilder);\r
+ aBuilder.delete();\r
+ return myShape;\r
+ }\r
+\r
+ public static TopoDS_Shape importIGES(String filename) {\r
+ org.jcae.opencascade.jni.IGESControl_Reader aReader = new org.jcae.opencascade.jni.IGESControl_Reader();\r
+ aReader.readFile(filename);\r
+ aReader.clearShapes();\r
+ aReader.transferRoots();\r
+ TopoDS_Shape result = aReader.oneShape();\r
+ aReader.delete();\r
+ return result;\r
+ }\r
+ \r
+ public static TopoDS_Shape importSTEP(String filename) {\r
+ org.jcae.opencascade.jni.STEPControl_Reader aReader = new org.jcae.opencascade.jni.STEPControl_Reader();\r
+ aReader.readFile(filename);\r
+ aReader.clearShapes();\r
+ aReader.transferRoots();\r
+ TopoDS_Shape result = aReader.oneShape();\r
+ aReader.delete();\r
+ return result;\r
+ }\r
+\r
+ public static TopoDS_Shape archimede(TopoDS_Shape topoDS_Shape, double param, double param2, double param3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape suppressHolesInFaceOrShell(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape suppressHole(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1,\r
+ TopoDS_Shape[] topoDS_Shape2, TopoDS_Shape[] topoDS_Shape3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape[] suppressFaces(TopoDS_Shape topoDS_Shape, TopoDS_Shape[] topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape[] subShapeAll(TopoDS_Shape topoDS_Shape, short param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape subShape(TopoDS_Shape topoDS_Shape, short param, TopoDS_Shape[] topoDS_Shape2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static boolean setBlock(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape partitionKeepFaces(TopoDS_Shape[] topoDS_Shape, TopoDS_Shape topoDS_Shape1, boolean param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape orientationChange(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static short nbLabels() {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeWire(TopoDS_Shape[] topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeVertex(double param, double param1, double param2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeVector(double[] pointStruct, double[] pointStruct1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeTranslation(TopoDS_Shape aShape, double x, double y, double z) {\r
+ GP_Trsf theTransformation = new GP_Trsf();\r
+ theTransformation.setTranslation(new double[] { x, y, z });\r
+ BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);\r
+\r
+ TopoDS_Shape shape = bt.shape();\r
+ bt.delete();\r
+ theTransformation.delete();\r
+ return shape;\r
+ }\r
+\r
+ public static TopoDS_Shape makeTorus(double[] pointStruct, double[] dirStruct, double r1, double r2) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new BRepPrimAPI_MakeTorus(axe, r1, r2).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeSphere(double x, double y, double z, double radius) {\r
+ double[] c = new double[] { x, y, z };\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeSphere(c, radius)\r
+ .shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeSewingShape(TopoDS_Shape topoDS_Shape, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeSewing(TopoDS_Shape[] topoDS_Shape, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeScaleTransform(TopoDS_Shape topoDS_Shape, double[] pointStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeRotation(TopoDS_Shape aShape, double[] axisStruct, double angle) {\r
+\r
+ GP_Trsf theTransformation = new GP_Trsf();\r
+ theTransformation.setRotation(axisStruct, angle);\r
+ BRepBuilderAPI_Transform bt = new BRepBuilderAPI_Transform(aShape, theTransformation, true);\r
+ TopoDS_Shape shape = bt.shape();\r
+ bt.delete();\r
+ theTransformation.delete();\r
+ return shape;\r
+ }\r
+\r
+ public static TopoDS_Shape makeRevolution(TopoDS_Shape topoDS_Shape, double[] axisStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePrism(TopoDS_Shape topoDS_Shape, double[] pointStruct, double[] pointStruct2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePlane(double[] pointStruct, double[] dirStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePlacedBox(double param, double param1, double param2, double param3, double param4,\r
+ double param5) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePipe(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makePanel(TopoDS_Shape topoDS_Shape, short param, double param2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiTranslation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param,\r
+ short param3, double[] dirStruct4, double param5, short param6) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiTranslation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double param, short param3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiRotation2D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct,\r
+ double param, short param4, double param5, short param6) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMultiRotation1D(TopoDS_Shape topoDS_Shape, double[] dirStruct, double[] pointStruct,\r
+ short param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeMirrorByPlane(TopoDS_Shape topoDS_Shape, TopoDS_Shape topoDS_Shape1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeLine(double[] pointStruct, double[] dirStruct) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFilling(TopoDS_Shape topoDS_Shape, short param, short param2, double param3, double param4,\r
+ short param5) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFillet(TopoDS_Shape topoDS_Shape, double param, short param2, TopoDS_Shape[] topoDS_Shape3) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeFace(TopoDS_Shape topoDS_Shape, boolean param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeEdge(double[] pointStruct, double[] pointStruct1) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCylinder(double[] pointStruct, double[] dirStruct, double radius, double height) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCylinder(axe, radius,\r
+ height, 2 * Math.PI).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCopy(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCone(double[] pointStruct, double[] dirStruct, double radius1, double radius2, double height) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeCone(axe, radius1,\r
+ radius2, height, 2 * Math.PI).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCompound(TopoDS_Shape[] shapes) {\r
+ BRep_Builder builder = new BRep_Builder();\r
+ org.jcae.opencascade.jni.TopoDS_Compound comp = new org.jcae.opencascade.jni.TopoDS_Compound();\r
+ builder.makeCompound(comp);\r
+ for (int i = 0; i < shapes.length; i++) {\r
+\r
+ builder.add(comp, shapes[i]);\r
+ }\r
+ builder.delete();\r
+ return comp;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCircle(double[] pointStruct, double[] dirStruct, double param) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeChamfer(TopoDS_Shape topoDS_Shape, double param, double param2, short param3,\r
+ TopoDS_Shape[] topoDS_Shape4) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeCDG(TopoDS_Shape topoDS_Shape) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ public static TopoDS_Shape makeBox(double x1, double y1, double z1, double x2, double y2, double z2) {\r
+ double[] p1 = new double[] { x1, y1, z1 };\r
+ double[] p2 = new double[] { x2, y2, z2 };\r
+ org.jcae.opencascade.jni.TopoDS_Shape tds = new org.jcae.opencascade.jni.BRepPrimAPI_MakeBox(p1, p2).shape();\r
+ return tds;\r
+ }\r
+\r
+ public static TopoDS_Shape makeCut(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Cut(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+ \r
+ public static TopoDS_Shape makeCommon(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Common(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+ \r
+ public static TopoDS_Shape makeFuse(TopoDS_Shape shape1, TopoDS_Shape shape2) {\r
+\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepAlgoAPI_Fuse(shape1, shape2).shape();\r
+ return s;\r
+ }\r
+\r
+ public static TopoDS_Shape makeArc(double[] pointStruct, double[] pointStruct1, double[] pointStruct2) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
+ public static TopoDS_Shape makeWedge(double[] pointStruct, double[] dirStruct,double dx, double dy, double dz, double xmin, double zmin, double xmax, double zmax) {\r
+ double[] axe = new double[6];\r
+ System.arraycopy(pointStruct, 0, axe, 0, 3);\r
+ System.arraycopy(dirStruct, 0, axe, 3, 3);\r
+ org.jcae.opencascade.jni.TopoDS_Shape s = new org.jcae.opencascade.jni.BRepPrimAPI_MakeWedge(axe, dx, dy, dz, xmin, zmin, xmax, zmax).shape();\r
+ return s;\r
+ }\r
+\r
+ \r
+\r
+ public static void exportBREP(TopoDS_Shape shape, String filename) {\r
+ org.jcae.opencascade.jni.BRepTools.write(shape, filename);\r
+ }\r
+\r
+ public static void exportIGES(TopoDS_Shape shape, String filename) {\r
+ throw new UnsupportedOperationException();\r
+ \r
+// IGESControl_Writer writer=new IGESControl_Writer();\r
+// writer.addShape(shape); writer.write(filename,false);\r
+ \r
+ }\r
+\r
+\r
+\r
+\r
+ \r
+}\r
--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/* \r
+ This class is combination of jCAE's classes to generate meshes from shapes.\r
+ Faces are lisetd as individual meshes.\r
+ \r
+ Marko Luukkainen\r
+ \r
+ jCAE stand for Java Computer Aided Engineering. Features are : Small CAD\r
+ modeler, Finit element mesher, Plugin architecture.\r
+\r
+ Copyright (C) 2003 Jerome Robert <jeromerobert@users.sourceforge.net>\r
+\r
+ This library is free software; you can redistribute it and/or\r
+ modify it under the terms of the GNU Lesser General Public\r
+ License as published by the Free Software Foundation; either\r
+ version 2.1 of the License, or (at your option) any later version.\r
+\r
+ This library is distributed in the hope that it will be useful,\r
+ but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
+ Lesser General Public License for more details.\r
+\r
+ You should have received a copy of the GNU Lesser General Public\r
+ License along with this library; if not, write to the Free Software\r
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ */\r
+\r
+package org.simantics.proconf.g3d.occ.geometry;\r
+import org.jcae.opencascade.jni.BRepBndLib;\r
+import org.jcae.opencascade.jni.BRepMesh_IncrementalMesh;\r
+import org.jcae.opencascade.jni.BRepTools;\r
+import org.jcae.opencascade.jni.BRep_Tool;\r
+import org.jcae.opencascade.jni.Bnd_Box;\r
+import org.jcae.opencascade.jni.GCPnts_UniformDeflection;\r
+import org.jcae.opencascade.jni.GP_Trsf;\r
+import org.jcae.opencascade.jni.GeomAPI_ProjectPointOnSurf;\r
+import org.jcae.opencascade.jni.GeomAdaptor_Curve;\r
+import org.jcae.opencascade.jni.GeomLProp_SLProps;\r
+import org.jcae.opencascade.jni.Geom_Curve;\r
+import org.jcae.opencascade.jni.Geom_Surface;\r
+import org.jcae.opencascade.jni.Poly_Triangulation;\r
+import org.jcae.opencascade.jni.TopAbs_Orientation;\r
+import org.jcae.opencascade.jni.TopAbs_ShapeEnum;\r
+import org.jcae.opencascade.jni.TopExp_Explorer;\r
+import org.jcae.opencascade.jni.TopLoc_Location;\r
+import org.jcae.opencascade.jni.TopoDS_Edge;\r
+import org.jcae.opencascade.jni.TopoDS_Face;\r
+import org.jcae.opencascade.jni.TopoDS_Shape;\r
+import org.jcae.opencascade.jni.TopoDS_Vertex;\r
+\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+\r
+import javax.vecmath.Matrix4d;\r
+import javax.vecmath.Point3d;\r
+import javax.vecmath.Vector3f;\r
+\r
+\r
+public class ViewableShapeImpl \r
+{\r
+\r
+ \r
+ ArrayList<IndexedGeometry> faceMeshes =new ArrayList<IndexedGeometry>();\r
+ ArrayList<float[]> edges=new ArrayList<float[]>();\r
+\r
+ public ViewableShapeImpl(TopoDS_Shape g)\r
+ {\r
+ BRepTools.clean(g);\r
+ buildFaces(g);\r
+ buildEdges(g);\r
+ \r
+ }\r
+ /*\r
+ public ViewableShapeImpl(TopoDS_Shape g, boolean alternative)\r
+ {\r
+ \r
+ if (alternative) \r
+ buildFaces(g);\r
+ else {\r
+ BRepTools.clean(g);\r
+ buildFaces2(g);\r
+ }\r
+ \r
+ buildEdges(g);\r
+ \r
+ }\r
+ */\r
+ \r
+ public IndexedGeometry getGeometry(int i)\r
+ { \r
+ return faceMeshes.get(i);\r
+ }\r
+ \r
+ public int numGeometries()\r
+ {\r
+ return faceMeshes.size();\r
+\r
+ } \r
+ \r
+ public int getNumEdges() {\r
+ return edges.size();\r
+ }\r
+ \r
+ public float[] getEdge(int i) {\r
+ return edges.get(i);\r
+ }\r
+ \r
+ /**\r
+ * org.jcae.viewer3d.cad.occ.OCCEdgeDomain\r
+ * @param shape\r
+ * @param geom\r
+ */\r
+ private void buildFaces(TopoDS_Shape shape) {\r
+ TopExp_Explorer explorer = new TopExp_Explorer();\r
+ TopLoc_Location loc = new TopLoc_Location();\r
+ \r
+ int meshIter=4;\r
+ double deflection = deflection(shape);\r
+ for (explorer.init(shape, TopAbs_ShapeEnum.FACE); explorer.more(); explorer.next())\r
+ { \r
+ //System.out.println("Triangulation");\r
+ TopoDS_Shape s = explorer.current();\r
+ if (!(s instanceof TopoDS_Face)) continue; // should not happen!\r
+ TopoDS_Face face = (TopoDS_Face)s;\r
+ Poly_Triangulation pt = BRep_Tool.triangulation(face,loc);\r
+ \r
+ //float error=0.01f;\r
+ double error = deflection;\r
+ int iter=0;\r
+ // if shape was generated with program triangulation seems to be always null,\r
+ // but model is loaded from file, it may already contain triangulation\r
+ // So : in those cases can we delete existing triangulation and generate a new one ?\r
+ //if (pt == null) {\r
+ //System.out.println("Initial triangulation of face "+face+" not found. Using Incremental mesh");\r
+ //} else {\r
+ //System.out.println("Initial triangulation of face "+face+" found.");\r
+ //}\r
+ while((pt==null)&(iter<meshIter)){\r
+ //System.out.println("Triangulation failed for face "+face+". Trying other mesh parameters.");\r
+ \r
+ // using relatif in incremental mesh would cause visible seams between faces\r
+ BRepMesh_IncrementalMesh m = new BRepMesh_IncrementalMesh(face,error, false);\r
+ //new BRepMesh_IncrementalMesh(face,error, false,12.0);\r
+ pt = BRep_Tool.triangulation(face,loc); \r
+ error/=10;\r
+ iter++;\r
+ m.delete();\r
+ \r
+ }\r
+ \r
+ \r
+ if (pt==null)\r
+ {\r
+ System.out.println("Triangulation failed for face "+face+". Mesh not generated.");\r
+ continue;\r
+ \r
+ }\r
+ \r
+ GP_Trsf trsf = loc.transformation();\r
+ double[] mat = new double[16];\r
+ trsf.getValues(mat);\r
+ Matrix4d m = new Matrix4d(mat);\r
+ \r
+ double[] dnodes = pt.nodes();\r
+ final int[] itriangles = pt.triangles(); \r
+ boolean useNormals = false;\r
+ boolean useTCoord = false;\r
+ \r
+ float[] tnodes = null;\r
+ float[] normals = null;\r
+ \r
+ if (useTCoord)\r
+ tnodes = new float[dnodes.length / 3 * 2];\r
+ if (useNormals)\r
+ normals= new float[dnodes.length];\r
+ GeomLProp_SLProps prop = new GeomLProp_SLProps(2,0.001);\r
+ Geom_Surface surf = BRep_Tool.surface(face);\r
+ prop.setSurface(surf);\r
+ if (useTCoord || useNormals) {\r
+ for (int i = 0; i < dnodes.length; i += 3) {\r
+ double UV[] = new double[2];\r
+ double point[] = new double[] { dnodes[i], dnodes[i + 1],\r
+ dnodes[i + 2] };\r
+ GeomAPI_ProjectPointOnSurf pof = new GeomAPI_ProjectPointOnSurf(\r
+ point, surf);\r
+ pof.lowerDistanceParameters(UV);\r
+ // boolean b = GeomLib_Tool.parameters(surf, point, 0.01,\r
+ // UV);\r
+ prop.setParameters(UV[0], UV[1]);\r
+ if (useNormals) {\r
+ double normal[] = prop.normal();\r
+ normals[i] = (float) normal[0];\r
+ normals[i + 1] = (float) normal[1];\r
+ normals[i + 2] = (float) normal[2];\r
+ }\r
+ if (useTCoord) {\r
+ int index = i / 3 * 2;\r
+ tnodes[index] = (float) UV[0];\r
+ tnodes[index + 1] = (float) UV[1];\r
+ }\r
+ // System.out.println(dnodes[i]+ " " +dnodes[i+1]+ " "\r
+ // +dnodes[i+2]+ " " + " UV:" + UV[0] + ","+UV[1]+ " " + b);\r
+ // System.out.println(dnodes[i]+ " " +dnodes[i+1]+ " "\r
+ // +dnodes[i+2]+ " " + normals[i] + " " + normals[i+1] + " "\r
+ // + normals[i+2] + " UV:" + UV[0] + ","+UV[1]);\r
+ }\r
+ }\r
+ prop.delete();\r
+ \r
+ if(face.orientation()==TopAbs_Orientation.REVERSED)\r
+ {\r
+ flipFace(itriangles);\r
+ }\r
+ \r
+ final float[] fnodes=createFloatVertices(dnodes, m);\r
+ IndexedGeometry geom = new IndexedGeometry();\r
+ geom.setCoordinates(fnodes);\r
+ geom.setIndices(itriangles);\r
+ geom.setNormals(calcNormals(fnodes,itriangles));\r
+ if (useNormals)\r
+ geom.setNormals(normals);\r
+ if (useTCoord)\r
+ geom.setTCoordinates(tnodes);\r
+ faceMeshes.add(geom);\r
+ //System.out.println("Triangulation done");\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * org.jcae.viewer3d.cad.occ.OCCEdgeDomain\r
+ * @param shape\r
+ * @param geom\r
+ */\r
+ /*\r
+ private void buildFaces2(TopoDS_Shape shape) {\r
+\r
+ TopExp_Explorer explorer = new TopExp_Explorer();\r
+ TopLoc_Location loc = new TopLoc_Location();\r
+ \r
+ int meshIter=10;\r
+ \r
+ for (explorer.init(shape, TopAbs_ShapeEnum.FACE); explorer.more(); explorer.next())\r
+ { \r
+ TopoDS_Shape s = explorer.current();\r
+ if (!(s instanceof TopoDS_Face)) continue; // should not happen!\r
+ TopoDS_Face face = (TopoDS_Face)s;\r
+ \r
+ double error=0.01;\r
+ BRepMesh_Discret mesh = new BRepMesh_Discret(0.01,face,0.1,true,true);\r
+ Poly_Triangulation pt = BRep_Tool.triangulation(face,loc);\r
+ //float error=0.001f*getMaxBound(s)*4;\r
+ \r
+ if (pt == null) {\r
+ System.out.println("Triangulation failed for face "+face+". Using Incremental mesh");\r
+ }\r
+ \r
+ int iter=0;\r
+ while((pt==null)&(iter<meshIter)){\r
+ BRepMesh_IncrementalMesh m = new BRepMesh_IncrementalMesh(face,error, false);\r
+ //new BRepMesh_IncrementalMesh(face,error, true);\r
+ pt = BRep_Tool.triangulation(face,loc); \r
+ error/=100;\r
+ iter++;\r
+ System.out.println("Triangulation failed for face "+face+". Trying other mesh parameters.");\r
+ m.delete();\r
+ }\r
+ mesh.delete();\r
+ if (pt==null)\r
+ {\r
+ System.out.println("Triangulation failed for face "+face+". Mesh not generated.");\r
+ continue;\r
+ }\r
+ GP_Trsf trsf = loc.transformation();\r
+ double[] mat = new double[16];\r
+ trsf.getValues(mat);\r
+ Matrix4d m = new Matrix4d(mat);\r
+ \r
+ double[] dnodes = pt.nodes();\r
+ final int[] itriangles = pt.triangles(); \r
+\r
+ if(face.orientation()==TopAbs_Orientation.REVERSED)\r
+ {\r
+ flipFace(itriangles);\r
+ }\r
+ \r
+ final float[] fnodes=createFloatVertices(dnodes, m);\r
+ IndexedGeometry geom = new IndexedGeometry();\r
+ geom.setCoordinates(fnodes);\r
+ geom.setIndices(itriangles);\r
+ geom.setNormals(calcNormals(fnodes,itriangles));\r
+ faceMeshes.add(geom);\r
+ }\r
+ }\r
+ */\r
+ \r
+ private float[] createFloatVertices(double dnodes[], Matrix4d m) {\r
+ float[] fnodes=new float[dnodes.length];\r
+ Point3d p = new Point3d();\r
+ \r
+ for(int i=0; i<dnodes.length;)\r
+ {\r
+ p.x = dnodes[i];\r
+ p.y = dnodes[i+1];\r
+ p.z = dnodes[i+2];\r
+ m.transform(p);\r
+ fnodes[i++]=(float) p.x;\r
+ fnodes[i++]=(float) p.y;\r
+ fnodes[i++]=(float) p.z; \r
+ }\r
+ return fnodes;\r
+ }\r
+ private double deflection(TopoDS_Shape s) {\r
+ Bnd_Box box = new Bnd_Box(); \r
+ BRepBndLib.add(s,box);\r
+ \r
+ double[] bbox = box.get();\r
+ double boundingBoxDeflection=0.01*\r
+ Math.max(Math.max(bbox[3]-bbox[0], bbox[4]-bbox[1]), bbox[5]-bbox[2]);\r
+ box.delete();\r
+ return boundingBoxDeflection;\r
+ }\r
+ \r
+ private double edgeDeflection(TopoDS_Shape s) {\r
+ return 0.5 * deflection(s);\r
+ }\r
+ \r
+ /**\r
+ * org.jcae.viewer3d.cad.occ.OCCEdgeDomain\r
+ * @param shape\r
+ * @param geom\r
+ */\r
+ private void buildEdges(TopoDS_Shape shape) {\r
+ \r
+ \r
+ TopExp_Explorer explorer = new TopExp_Explorer();\r
+ HashSet<TopoDS_Edge> alreadyDone=new HashSet<TopoDS_Edge>();\r
+ double boundingBoxDeflection = edgeDeflection(shape);\r
+\r
+ for (explorer.init(shape, TopAbs_ShapeEnum.EDGE); explorer.more(); explorer.next())\r
+ {\r
+ TopoDS_Shape s = explorer.current(); \r
+ if (!(s instanceof TopoDS_Edge)) continue; // should not happen!\r
+ TopoDS_Edge e = (TopoDS_Edge)s;\r
+ \r
+ if(!alreadyDone.add(e))\r
+ continue;\r
+ \r
+ double[] range = BRep_Tool.range(e);\r
+ Geom_Curve gc = BRep_Tool.curve(e, range);\r
+ float[] array;\r
+ if(gc!=null)\r
+ {\r
+ GeomAdaptor_Curve adaptator = new GeomAdaptor_Curve(gc);\r
+ GCPnts_UniformDeflection deflector = new GCPnts_UniformDeflection();\r
+\r
+ deflector.initialize(adaptator, boundingBoxDeflection, range[0], range[1]);\r
+ int npts = deflector.nbPoints();\r
+ \r
+ // Allocate one additional point at each end = parametric value 0, 1\r
+ int size = 0;\r
+ if (npts > 2)\r
+ size = (npts-2)*2;\r
+ size+=2;\r
+ size*=3;\r
+ array = new float[size]; \r
+ int j=0;\r
+ double[] values = adaptator.value(range[0]);\r
+ array[j++] = (float) values[0];\r
+ array[j++] = (float) values[1];\r
+ array[j++] = (float) values[2];\r
+ // All intermediary points\r
+ for (int i=1; i<npts-1; ++i) {\r
+ values = adaptator.value(deflector.parameter(i+1));\r
+ array[j++] = (float) values[0];\r
+ array[j++] = (float) values[1];\r
+ array[j++] = (float) values[2];\r
+ array[j++] = (float) values[0];\r
+ array[j++] = (float) values[1];\r
+ array[j++] = (float) values[2];\r
+ }\r
+ // Add last point\r
+ values = adaptator.value(range[1]);\r
+ array[j++] = (float) values[0];\r
+ array[j++] = (float) values[1];\r
+ array[j++] = (float) values[2];\r
+ edges.add(array);\r
+ adaptator.delete();\r
+ deflector.delete();\r
+ }\r
+ else\r
+ {\r
+ if (!BRep_Tool.degenerated(e))\r
+ {\r
+ // So, there is no curve, and the edge is not degenerated?\r
+ // => draw lines between the vertices and ignore curvature \r
+ // best approximation we can do\r
+ ArrayList<double[]> aa = new ArrayList<double[]>(); // store points here\r
+ for (TopExp_Explorer explorer2 = new TopExp_Explorer(s, TopAbs_ShapeEnum.VERTEX);\r
+ explorer2.more(); explorer2.next())\r
+ {\r
+ TopoDS_Shape sv = explorer2.current();\r
+ if (!(sv instanceof TopoDS_Vertex)) continue; // should not happen!\r
+ TopoDS_Vertex v = (TopoDS_Vertex)sv;\r
+ aa.add(BRep_Tool.pnt(v));\r
+ }\r
+ int size = 0;\r
+ if (aa.size() > 2)\r
+ size = (aa.size()-2)*2;\r
+ size+=2;\r
+ double[] f=(double[])aa.get(0);\r
+ array = new float[size*3];\r
+ array[0]=(float) f[0];\r
+ array[1]=(float) f[1];\r
+ array[2]=(float) f[2];\r
+ for(int i=1, j=3; i<aa.size()-1; i++)\r
+ {\r
+ f=aa.get(i);\r
+ array[j++]=(float) f[0];\r
+ array[j++]=(float) f[1];\r
+ array[j++]=(float) f[2];\r
+ array[j++]=(float) f[0];\r
+ array[j++]=(float) f[1];\r
+ array[j++]=(float) f[2];\r
+ }\r
+ f=aa.get(aa.size()-1);\r
+ array[array.length-3]=(float) f[0];\r
+ array[array.length-2]=(float) f[1];\r
+ array[array.length-1]=(float) f[2];\r
+ edges.add(array);\r
+ }\r
+ }\r
+ } \r
+ }\r
+ \r
+ private float[] calcNormals(float[] fnodes, int[] trias) {\r
+ float nnodes[] = new float[fnodes.length];\r
+ for (int i = 0; i < nnodes.length; i++) nnodes[i] = 0.f;\r
+ Vector3f v1 = new Vector3f();\r
+ Vector3f v2 = new Vector3f();\r
+ Vector3f v3 = new Vector3f();\r
+ Vector3f t1 = new Vector3f();\r
+ Vector3f t2 = new Vector3f();\r
+ Vector3f n = new Vector3f();\r
+ for (int i = 0; i < trias.length; i+=3) {\r
+ v1.x = fnodes[trias[i]*3];\r
+ v1.y = fnodes[trias[i]*3+1];\r
+ v1.z = fnodes[trias[i]*3+2];\r
+ v2.x = fnodes[trias[i+1]*3];\r
+ v2.y = fnodes[trias[i+1]*3+1];\r
+ v2.z = fnodes[trias[i+1]*3+2];\r
+ v3.x = fnodes[trias[i+2]*3];\r
+ v3.y = fnodes[trias[i+2]*3+1];\r
+ v3.z = fnodes[trias[i+2]*3+2];\r
+ t1.sub(v3, v1);\r
+ t2.sub(v2, v1);\r
+ n.cross(t2, t1);\r
+ //n.normalize();\r
+ nnodes[trias[i]*3] += n.x;\r
+ nnodes[trias[i]*3+1] += n.y;\r
+ nnodes[trias[i]*3+2] += n.z;\r
+ nnodes[trias[i+1]*3] += n.x;\r
+ nnodes[trias[i+1]*3+1] += n.y;\r
+ nnodes[trias[i+1]*3+2] += n.z;\r
+ nnodes[trias[i+2]*3] += n.x;\r
+ nnodes[trias[i+2]*3+1] += n.y;\r
+ nnodes[trias[i+2]*3+2] += n.z;\r
+ }\r
+ for (int i = 0; i < nnodes.length; i+=3) {\r
+ n.x = nnodes[i];\r
+ n.y = nnodes[i+1];\r
+ n.z = nnodes[i+2];\r
+ n.normalize();\r
+ nnodes[i] = n.x;\r
+ nnodes[i+1] = n.y;\r
+ nnodes[i+2] = n.z;\r
+ }\r
+ return nnodes;\r
+ }\r
+ \r
+ private void flipFace(int[] trias) {\r
+ int newTrias[] = new int[trias.length];\r
+ System.arraycopy(trias, 0, newTrias, 0, trias.length);\r
+ for (int i = 0; i < trias.length; i++) {\r
+ trias[i] = newTrias[trias.length - i - 1];\r
+ }\r
+ } \r
+ \r
+}\r