package org.simantics.plant3d.geometry; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import javax.vecmath.Point3d; import javax.vecmath.Tuple3d; import javax.vecmath.Vector3d; import org.jcae.opencascade.jni.TopoDS_Shape; import org.simantics.db.Resource; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.shape.Mesh; import org.simantics.g3d.shape.Tube; import org.simantics.opencascade.OccTriangulator; //public class ReducerGeometryProvider extends BuiltinGeometryProvider { public class ReducerGeometryProvider extends BuiltinMeshProvider { public ReducerGeometryProvider(Resource resource) { super(resource); } private double radius = 0.01; private double radius2 = 0.02; private double offset = 0.0; // @Override public Collection getModel() throws Exception { if (radius < MathTools.NEAR_ZERO || radius2 < MathTools.NEAR_ZERO) return Collections.emptyList(); double length = Math.max(0.1, Math.abs(radius-radius2)*4.0); TopoDS_Shape shape; if (Math.abs(radius-radius2) < MathTools.NEAR_ZERO) { shape = OccTriangulator.makeCylinder(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius, length); } else { shape = OccTriangulator.makeCone(new double[] {-length*0.5, 0.0, 0.0}, new double[] { 1.0, 0.0, 0.0 }, radius,radius2, length); } return Collections.singletonList(shape); } @Override public Mesh getMesh() { double length = Math.max(0.1, Math.abs(radius-radius2)*4.0); if (length < .0001 || radius < MathTools.NEAR_ZERO || radius2 < MathTools.NEAR_ZERO) return null; Tube tube = new Tube(); tube.setResolution(16); List vertices = new ArrayList(); List radius = new ArrayList(); List tangents = new ArrayList(); vertices.add(new Point3d(-length*0.5, 0.0, 0.0)); vertices.add(new Point3d( length*0.5, offset, 0.0)); radius.add(this.radius); radius.add(this.radius2); tangents.add(new Vector3d(1.0,0.0,0.0)); tangents.add(new Vector3d(1.0,0.0,0.0)); tube.setVertices(vertices); tube.setRadiis(radius); tube.setTangents(tangents); tube.setCap(false); return tube.create(); } @Override public void setProperties(Map props) { if (props.containsKey("radius")) { radius = (Double)props.get("radius"); } if (props.containsKey("radius2")) { radius2 = (Double)props.get("radius2"); } if (props.containsKey("offset")) { offset = (Double)props.get("offset"); } } @Override public void updateCalculatedProperties(Map returnProps) { returnProps.put("length", Math.max(0.1, Math.abs(radius-radius2)*4.0)); } }